Remove wrapper around std::mt19937

This commit is contained in:
tamasmeszaros 2020-01-15 17:44:15 +01:00
parent 402ae12db2
commit e6bdec4aa5
4 changed files with 25 additions and 52 deletions

View file

@ -49,26 +49,6 @@ float SupportPointGenerator::distance_limit(float angle) const
return 1./(2.4*get_required_density(angle));
}*/
class SupportPointGenerator::RandomGen {
std::mt19937 m_;
public:
using result_type = long;
RandomGen()
{
std::random_device rd;
m_.seed(rd());
}
explicit RandomGen(long seedval) { seed(seedval); }
void seed(long s) { m_.seed(std::mt19937::result_type(s)); }
long operator() () { return long(m_()); }
long min() const { return m_.min(); }
long max() const { return m_.max(); }
};
SupportPointGenerator::SupportPointGenerator(
const sla::EigenMesh3D &emesh,
const std::vector<ExPolygons> &slices,
@ -78,6 +58,8 @@ SupportPointGenerator::SupportPointGenerator(
std::function<void(int)> statusfn)
: SupportPointGenerator(emesh, config, throw_on_cancel, statusfn)
{
std::random_device rd;
m_rng.seed(rd());
execute(slices, heights);
}
@ -96,17 +78,7 @@ SupportPointGenerator::SupportPointGenerator(
void SupportPointGenerator::execute(const std::vector<ExPolygons> &slices,
const std::vector<float> & heights)
{
RandomGen rng;
process(slices, heights, rng);
project_onto_mesh(m_output);
}
void SupportPointGenerator::execute(const std::vector<ExPolygons> &slices,
const std::vector<float> & heights,
long seed)
{
RandomGen rng(seed);
process(slices, heights, rng);
process(slices, heights);
project_onto_mesh(m_output);
}
@ -230,7 +202,7 @@ static std::vector<SupportPointGenerator::MyLayer> make_layers(
return layers;
}
void SupportPointGenerator::process(const std::vector<ExPolygons>& slices, const std::vector<float>& heights, RandomGen &rng)
void SupportPointGenerator::process(const std::vector<ExPolygons>& slices, const std::vector<float>& heights)
{
#ifdef SLA_SUPPORTPOINTGEN_DEBUG
std::vector<std::pair<ExPolygon, coord_t>> islands;
@ -285,15 +257,15 @@ void SupportPointGenerator::process(const std::vector<ExPolygons>& slices, const
//float force_deficit = s.support_force_deficit(m_config.tear_pressure());
if (s.islands_below.empty()) { // completely new island - needs support no doubt
uniformly_cover({ *s.polygon }, s, point_grid, rng, true);
uniformly_cover({ *s.polygon }, s, point_grid, true);
} else if (! s.dangling_areas.empty()) {
// Let's see if there's anything that overlaps enough to need supports:
// What we now have in polygons needs support, regardless of what the forces are, so we can add them.
//FIXME is it an island point or not? Vojtech thinks it is.
uniformly_cover(s.dangling_areas, s, point_grid, rng);
uniformly_cover(s.dangling_areas, s, point_grid);
} else if (! s.overhangs_slopes.empty()) {
//FIXME add the support force deficit as a parameter, only cover until the defficiency is covered.
uniformly_cover(s.overhangs_slopes, s, point_grid, rng);
uniformly_cover(s.overhangs_slopes, s, point_grid);
}
}
@ -312,7 +284,7 @@ void SupportPointGenerator::process(const std::vector<ExPolygons>& slices, const
}
}
std::vector<Vec2f> sample_expolygon(const ExPolygon &expoly, float samples_per_mm2, SupportPointGenerator::RandomGen &rng)
std::vector<Vec2f> sample_expolygon(const ExPolygon &expoly, float samples_per_mm2, std::mt19937 &rng)
{
// Triangulate the polygon with holes into triplets of 3D points.
std::vector<Vec2f> triangles = Slic3r::triangulate_expolygon_2f(expoly);
@ -352,7 +324,7 @@ std::vector<Vec2f> sample_expolygon(const ExPolygon &expoly, float samples_per_m
return out;
}
std::vector<Vec2f> sample_expolygon_with_boundary(const ExPolygon &expoly, float samples_per_mm2, float samples_per_mm_boundary, SupportPointGenerator::RandomGen &rng)
std::vector<Vec2f> sample_expolygon_with_boundary(const ExPolygon &expoly, float samples_per_mm2, float samples_per_mm_boundary, std::mt19937 &rng)
{
std::vector<Vec2f> out = sample_expolygon(expoly, samples_per_mm2, rng);
double point_stepping_scaled = scale_(1.f) / samples_per_mm_boundary;
@ -365,7 +337,7 @@ std::vector<Vec2f> sample_expolygon_with_boundary(const ExPolygon &expoly, float
return out;
}
std::vector<Vec2f> sample_expolygon_with_boundary(const ExPolygons &expolys, float samples_per_mm2, float samples_per_mm_boundary, SupportPointGenerator::RandomGen &rng)
std::vector<Vec2f> sample_expolygon_with_boundary(const ExPolygons &expolys, float samples_per_mm2, float samples_per_mm_boundary, std::mt19937 &rng)
{
std::vector<Vec2f> out;
for (const ExPolygon &expoly : expolys)
@ -488,7 +460,7 @@ static inline std::vector<Vec2f> poisson_disk_from_samples(const std::vector<Vec
return out;
}
void SupportPointGenerator::uniformly_cover(const ExPolygons& islands, Structure& structure, PointGrid3D &grid3d, RandomGen &rng, bool is_new_island, bool just_one)
void SupportPointGenerator::uniformly_cover(const ExPolygons& islands, Structure& structure, PointGrid3D &grid3d, bool is_new_island, bool just_one)
{
//int num_of_points = std::max(1, (int)((island.area()*pow(SCALING_FACTOR, 2) * m_config.tear_pressure)/m_config.support_force));
@ -510,7 +482,7 @@ void SupportPointGenerator::uniformly_cover(const ExPolygons& islands, Structure
//FIXME share the random generator. The random generator may be not so cheap to initialize, also we don't want the random generator to be restarted for each polygon.
std::vector<Vec2f> raw_samples = sample_expolygon_with_boundary(islands, samples_per_mm2, 5.f / poisson_radius, rng);
std::vector<Vec2f> raw_samples = sample_expolygon_with_boundary(islands, samples_per_mm2, 5.f / poisson_radius, m_rng);
std::vector<Vec2f> poisson_samples;
for (size_t iter = 0; iter < 4; ++ iter) {
poisson_samples = poisson_disk_from_samples(raw_samples, poisson_radius,
@ -541,7 +513,7 @@ void SupportPointGenerator::uniformly_cover(const ExPolygons& islands, Structure
// assert(! poisson_samples.empty());
if (poisson_samples_target < poisson_samples.size()) {
std::shuffle(poisson_samples.begin(), poisson_samples.end(), rng);
std::shuffle(poisson_samples.begin(), poisson_samples.end(), m_rng);
poisson_samples.erase(poisson_samples.begin() + poisson_samples_target, poisson_samples.end());
}
for (const Vec2f &pt : poisson_samples) {