Skip to content

Replaced std::random_shuffle with std::shuffle in tri #24062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Oct 28, 2022
Merged
8 changes: 8 additions & 0 deletions doc/api/next_api_changes/behavior/24062-tb.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
``TrapezoidMapTriFinder`` uses different random number generator
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The random number generator used to determine the order of insertion of
triangle edges in ``TrapezoidMapTriFinder`` has changed. This can result in a
different triangle index being returned for a point that lies exactly on an
edge between two triangles. This can also affect triangulation interpolation
and refinement algorithms that use ``TrapezoidMapTriFinder``.
18 changes: 3 additions & 15 deletions src/tri/_tri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <algorithm>
#include <set>
#include <random>


TriEdge::TriEdge()
Expand Down Expand Up @@ -1465,8 +1466,8 @@ TrapezoidMapTriFinder::initialize()
_tree->assert_valid(false);

// Randomly shuffle all edges other than first 2.
RandomNumberGenerator rng(1234);
std::random_shuffle(_edges.begin()+2, _edges.end(), rng);
std::mt19937 rng(1234);
std::shuffle(_edges.begin()+2, _edges.end(), rng);

// Add edges, one at a time, to tree.
size_t nedges = _edges.size();
Expand Down Expand Up @@ -2056,16 +2057,3 @@ TrapezoidMapTriFinder::Trapezoid::set_upper_right(Trapezoid* upper_right_)
if (upper_right != 0)
upper_right->upper_left = this;
}



RandomNumberGenerator::RandomNumberGenerator(unsigned long seed)
: _m(21870), _a(1291), _c(4621), _seed(seed % _m)
{}

unsigned long
RandomNumberGenerator::operator()(unsigned long max_value)
{
_seed = (_seed*_a + _c) % _m;
return (_seed*max_value) / _m;
}
24 changes: 0 additions & 24 deletions src/tri/_tri.h
Original file line number Diff line number Diff line change
Expand Up @@ -791,28 +791,4 @@ class TrapezoidMapTriFinder
Node* _tree; // Root node of the trapezoid map search tree. Owned.
};



/* Linear congruential random number generator. Edges in the triangulation are
* randomly shuffled before being added to the trapezoid map. Want the
* shuffling to be identical across different operating systems and the same
* regardless of previous random number use. Would prefer to use a STL or
* Boost random number generator, but support is not consistent across
* different operating systems so implementing own here.
*
* This is not particularly random, but is perfectly adequate for the use here.
* Coefficients taken from Numerical Recipes in C. */
class RandomNumberGenerator
{
public:
RandomNumberGenerator(unsigned long seed);

// Return random integer in the range 0 to max_value-1.
unsigned long operator()(unsigned long max_value);

private:
const unsigned long _m, _a, _c;
unsigned long _seed;
};

#endif