From 19a115260a01c937150f4afa67b97b0069c49afa Mon Sep 17 00:00:00 2001 From: Vojtech Bubnik Date: Thu, 15 Oct 2020 07:58:13 +0200 Subject: [PATCH] A little bit cheaper triangle random sampling + documentation. --- src/libslic3r/SLA/SupportPointGenerator.cpp | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/libslic3r/SLA/SupportPointGenerator.cpp b/src/libslic3r/SLA/SupportPointGenerator.cpp index 7e884b6e3..77cee77a9 100644 --- a/src/libslic3r/SLA/SupportPointGenerator.cpp +++ b/src/libslic3r/SLA/SupportPointGenerator.cpp @@ -357,13 +357,26 @@ std::vector sample_expolygon(const ExPolygon &expoly, float samples_per_m double r = random_triangle(rng); size_t idx_triangle = std::min(std::upper_bound(areas.begin(), areas.end(), (float)r) - areas.begin(), areas.size() - 1) * 3; // Select a random point on the triangle. - double u = float(std::sqrt(random_float(rng))); - double v = float(random_float(rng)); const Vec2f &a = triangles[idx_triangle ++]; const Vec2f &b = triangles[idx_triangle++]; const Vec2f &c = triangles[idx_triangle]; - const Vec2f x = a * (1.f - u) + b * (u * (1.f - v)) + c * (v * u); - out.emplace_back(x); +#if 0 + // https://www.cs.princeton.edu/~funk/tog02.pdf + // page 814, formula 1. + double u = float(std::sqrt(random_float(rng))); + double v = float(random_float(rng)); + out.emplace_back(a * (1.f - u) + b * (u * (1.f - v)) + c * (v * u)); +#else + // Greg Turk, Graphics Gems + // https://devsplorer.wordpress.com/2019/08/07/find-a-random-point-on-a-plane-using-barycentric-coordinates-in-unity/ + double u = float(random_float(rng)); + double v = float(random_float(rng)); + if (u + v >= 1.f) { + u = 1.f - u; + v = 1.f - v; + } + out.emplace_back(a + u * (b - a) + v * (c - a)); +#endif } } return out;