#include "stdafx.h" #include "doctest.h" #include "editor/editorScatter.hpp" #include #include #include #include #include #include #include #include namespace { // both ways of the scale: the turn and the scale picked are finite, the scale not below its floor void check_picks_finite(scatter::look_settings Look, std::mt19937 &Random) { for (auto const same : {true, false}) { Look.same_scale = same; auto const picked{scatter::pick(Look, Random)}; CHECK(std::isfinite(picked.angles.x + picked.angles.y + picked.angles.z)); CHECK(std::isfinite(picked.scale.x + picked.scale.y + picked.scale.z)); CHECK(picked.scale.x >= 0.01f); CHECK(picked.scale.y >= 0.01f); CHECK(picked.scale.z >= 0.01f); } } double closest_pair(std::vector const &Points) { double least{1e30}; for (std::size_t i = 0; i < Points.size(); ++i) for (std::size_t j = i + 1; j < Points.size(); ++j) least = std::min(least, glm::distance(Points[i], Points[j])); return least; } } // namespace TEST_CASE("scatter: the random fill keeps to the circle and the spacing, as dense as asked for") { std::mt19937 random{7}; // NOSONAR(cpp:S2245) a repeatable test, not security scatter::dab_settings settings; settings.radius = 10.0; settings.spacing = 1.5; settings.density = 5.0; scatter::stroke stroke; auto const points{scatter::dab({100.0, -50.0}, settings, stroke, random)}; // 5 a 100 m² over 314 m²: 15.7, so 15 or 16, the spacing lets them all in CHECK(points.size() >= 15); CHECK(points.size() <= 16); // the dab is added to the stroke CHECK(stroke.taken.size() == points.size()); CHECK(stroke.dabs.size() == 1); for (auto const &p : points) CHECK(glm::distance(p, glm::dvec2{100.0, -50.0}) <= 10.0 + 1e-9); CHECK(closest_pair(points) >= 1.5 - 1e-9); } TEST_CASE("scatter: a dab keeps clear of what the stroke put before") { std::mt19937 random{3}; // NOSONAR(cpp:S2245) a repeatable test, not security scatter::dab_settings settings; settings.radius = 4.0; settings.spacing = 2.0; settings.density = 30.0; scatter::stroke stroke; auto const first{scatter::dab({0.0, 0.0}, settings, stroke, random)}; auto const second{scatter::dab({2.0, 0.0}, settings, stroke, random)}; for (auto const &p : second) for (auto const &q : first) CHECK(glm::distance(p, q) >= 2.0 - 1e-9); } TEST_CASE("scatter: the uniform fill is a grid of the world, a second dab over the first adds only the new points") { std::mt19937 random{1}; // NOSONAR(cpp:S2245) a repeatable test, not security scatter::dab_settings settings; settings.mode = scatter::fill::uniform; settings.radius = 5.0; settings.spacing = 2.0; scatter::stroke stroke; auto const first{scatter::dab({0.3, 0.2}, settings, stroke, random)}; CHECK(first.size() > 15); CHECK(closest_pair(first) == doctest::Approx(2.0)); auto const second{scatter::dab({2.3, 0.2}, settings, stroke, random)}; for (auto const &p : second) for (auto const &q : first) CHECK(glm::distance(p, q) > 1.9); // the same place again: nothing new CHECK(scatter::dab({0.3, 0.2}, settings, stroke, random).empty()); } TEST_CASE("scatter: the feathered fill thins out towards the edge") { std::mt19937 random{11}; // NOSONAR(cpp:S2245) a repeatable test, not security scatter::dab_settings settings; settings.mode = scatter::fill::feathered; settings.radius = 20.0; settings.spacing = 0.1; settings.density = 40.0; scatter::stroke stroke; auto const points{scatter::dab({0.0, 0.0}, settings, stroke, random)}; int inner{0}; int outer{0}; for (auto const &p : points) { auto const d{glm::length(p)}; // rings of equal area: the inner one within r·√½, the outer one beyond it if (d < 20.0 * std::sqrt(0.5)) ++inner; else ++outer; } CHECK(inner > 2 * outer); } TEST_CASE("scatter: a stroke leaves the density asked for on the ground it went over, however much its dabs overlap") { std::mt19937 random{21}; // NOSONAR(cpp:S2245) a repeatable test, not security scatter::dab_settings settings; settings.radius = 10.0; settings.spacing = 0.2; settings.density = 5.0; scatter::stroke stroke; // 400 m straight on, a dab every step of the brush (half the radius): each place under four circles or so std::size_t count{0}; for (int i = 0; i * scatter::dab_step(settings) <= 400.0; ++i) count += scatter::dab({i * scatter::dab_step(settings), 0.0}, settings, stroke, random).size(); // the ground gone over: 400 m by 20 m and the two half circles at the ends, 8314 m², so 416 models auto const asked{settings.density * (400.0 * 20.0 + std::numbers::pi * 100.0) / 100.0}; CHECK(count > 0.85 * asked); CHECK(count < 1.15 * asked); // as dense at the end of the stroke as at its start: the 100 m in the middle of each half hold a quarter of the models auto const within = [&stroke](double const From, double const To) { return std::ranges::count_if(stroke.taken, [&](glm::dvec2 const &Point) { return Point.x >= From && Point.x < To; }); }; CHECK(within(50.0, 150.0) > 0.75 * settings.density * 20.0); CHECK(within(50.0, 150.0) < 1.25 * settings.density * 20.0); CHECK(within(250.0, 350.0) > 0.75 * settings.density * 20.0); CHECK(within(250.0, 350.0) < 1.25 * settings.density * 20.0); // and across it: the middle half of its width holds half of them, not more auto const middle{std::ranges::count_if(stroke.taken, [](glm::dvec2 const &Point) { return Point.x >= 50.0 && Point.x < 350.0 && std::abs(Point.y) < 5.0; })}; auto const whole{within(50.0, 350.0)}; CHECK(middle > 0.4 * whole); CHECK(middle < 0.6 * whole); } TEST_CASE("scatter: the cursor back over the ground the stroke has filled adds nothing there") { std::mt19937 random{22}; // NOSONAR(cpp:S2245) a repeatable test, not security scatter::dab_settings settings; settings.radius = 8.0; settings.spacing = 0.2; settings.density = 20.0; scatter::stroke stroke; for (int i = 0; i <= 20; ++i) scatter::dab({4.0 * i, 0.0}, settings, stroke, random); auto const there{stroke.taken.size()}; CHECK(there > 200); // all the way back, between the places of the dabs made on the way there for (int i = 19; i >= 0; --i) CHECK(scatter::dab({4.0 * i + 2.0, 0.0}, settings, stroke, random).size() <= 2); // next to nothing: only the slivers at the edges between two circles of the way there CHECK(stroke.taken.size() < there + there / 20); } TEST_CASE("scatter: a circle too small for a whole model a dab still gets its share over the stroke") { std::mt19937 random{23}; // NOSONAR(cpp:S2245) a repeatable test, not security scatter::dab_settings settings; settings.radius = 1.0; settings.spacing = 0.2; settings.density = 4.0; // 0.13 of a model in the circle: rounded, no dab would ever put one CHECK(scatter::dab_wanted(settings) < 0.5); scatter::stroke stroke; for (int i = 0; i <= 4000; ++i) scatter::dab({0.5 * i, 0.0}, settings, stroke, random); // 2 km by 2 m: 160 models CHECK(stroke.taken.size() > 120); CHECK(stroke.taken.size() < 200); } TEST_CASE("scatter: the feathered fill of a stroke is as dense as asked for in its middle, thinner towards its edges") { std::mt19937 random{24}; // NOSONAR(cpp:S2245) a repeatable test, not security scatter::dab_settings settings; settings.mode = scatter::fill::feathered; settings.radius = 10.0; settings.spacing = 0.1; settings.density = 20.0; scatter::stroke stroke; for (int i = 0; i <= 80; ++i) scatter::dab({5.0 * i, 0.0}, settings, stroke, random); auto const band = [&stroke](double const From, double const To) { return static_cast(std::ranges::count_if(stroke.taken, [&](glm::dvec2 const &Point) { return Point.x >= 50.0 && Point.x < 350.0 && std::abs(Point.y) >= From && std::abs(Point.y) < To; })); }; // the metre each side of the middle of the stroke, 600 m²: nearly the 20 a 100 m² asked for (the dabs are 5 m apart, the // feather of each takes a little off between them) CHECK(band(0.0, 1.0) > 0.75 * 120.0); CHECK(band(0.0, 1.0) < 1.15 * 120.0); // fewer and fewer outwards CHECK(band(4.0, 5.0) < band(0.0, 1.0)); CHECK(band(8.0, 9.0) < 0.5 * band(4.0, 5.0)); } TEST_CASE("scatter: the turn and the scale of a model") { std::mt19937 random{5}; // NOSONAR(cpp:S2245) a repeatable test, not security scatter::look_settings settings; settings.turn = scatter::vary::fixed; settings.angles = {2.0f, 45.0f, -3.0f}; settings.size = scatter::vary::fixed; settings.scale = {120.0f, 80.0f, 100.0f}; auto const fixed{scatter::pick(settings, random)}; CHECK(fixed.angles.y == doctest::Approx(45.0f)); CHECK(fixed.scale.x == doctest::Approx(1.2f)); CHECK(fixed.scale.y == doctest::Approx(0.8f)); settings.turn = scatter::vary::random; settings.angle_range = {5.0f, 180.0f, 0.0f}; settings.size = scatter::vary::random; settings.scale_range = {20.0f, 0.0f, 0.0f}; settings.same_scale = true; for (int i = 0; i < 200; ++i) { auto const drawn{scatter::pick(settings, random)}; CHECK(std::abs(drawn.angles.x) <= 5.0f); CHECK(std::abs(drawn.angles.y) <= 180.0f); CHECK(drawn.angles.z == 0.0f); CHECK(drawn.scale.x >= 0.8f - 1e-6f); CHECK(drawn.scale.x <= 1.2f + 1e-6f); CHECK(drawn.scale.y == drawn.scale.x); // one for all the axes CHECK(drawn.scale.z == drawn.scale.x); } // never scaled to nothing settings.same_scale = false; settings.scale_range = {500.0f, 500.0f, 500.0f}; for (int i = 0; i < 50; ++i) { auto const drawn{scatter::pick(settings, random)}; CHECK(drawn.scale.x >= 0.01f - 1e-6f); CHECK(drawn.scale.y >= 0.01f - 1e-6f); CHECK(drawn.scale.z >= 0.01f - 1e-6f); } } TEST_CASE("scatter: a uniform dab too wide for its spacing stops at the limit at once") { std::mt19937 random{2}; // NOSONAR(cpp:S2245) a repeatable test, not security scatter::dab_settings settings; settings.mode = scatter::fill::uniform; settings.radius = 100.0; settings.spacing = 0.1; // some 3.6 million points of the grid in the circle: cut short at the limit, not gone through scatter::stroke stroke; auto const points{scatter::dab({0.0, 0.0}, settings, stroke, random)}; CHECK(static_cast(points.size()) == scatter::dab_most); } TEST_CASE("scatter: the regular fill along a line, in rows, the height of the line followed") { std::mt19937 random{4}; // NOSONAR(cpp:S2245) a repeatable test, not security scatter::line_settings settings; settings.mode = scatter::line_fill::regular; settings.spacing = 5.0; // one row on a line of no width, a model every 5 m of 100 m, the ends included std::vector const line{{0.0, 10.0, 0.0}, {100.0, 20.0, 0.0}}; auto const one{scatter::along(line, settings, random)}; CHECK(one.size() == 21); for (auto const &place : one) { CHECK(place.point.y == doctest::Approx(0.0)); CHECK(place.side == doctest::Approx(0.0)); CHECK(place.height == doctest::Approx(10.0 + place.point.x / 10.0)); } // 10 m each way: the rows at 0, ±5 and ±10 m, the ones beyond the edge of the band left out settings.width = 10.0; auto const band{scatter::along(line, settings, random)}; for (auto const &place : band) { CHECK(std::abs(place.point.y) <= 10.0 + 1e-9); CHECK(place.side == doctest::Approx(std::abs(place.point.y) / 10.0)); } std::set rows; for (auto const &place : band) rows.insert(std::lround(place.point.y)); CHECK(rows == std::set{-10, -5, 0, 5, 10}); } TEST_CASE("scatter: a line bent back on itself puts no two models on one place") { std::mt19937 random{9}; // NOSONAR(cpp:S2245) a repeatable test, not security scatter::line_settings settings; settings.spacing = 2.0; settings.width = 6.0; std::vector const line{{0.0, 0.0, 0.0}, {40.0, 0.0, 0.0}, {40.0, 0.0, 3.0}, {0.0, 0.0, 3.0}}; auto const places{scatter::along(line, settings, random)}; std::vector points; for (auto const &place : places) points.push_back(place.point); CHECK(false == points.empty()); CHECK(closest_pair(points) >= 1.0 - 1e-9); } TEST_CASE("scatter: the last point of a line given twice puts nothing past its end") { std::mt19937 random{5}; // NOSONAR(cpp:S2245) a repeatable test, not security scatter::line_settings settings; settings.mode = scatter::line_fill::regular; settings.jitter = 0.0; settings.spacing = 5.0; settings.width = 10.0; // a double-click on the end: the piece of no length has no direction, the rows stay to the side of the line std::vector const line{{0.0, 0.0, 0.0}, {0.0, 0.0, 100.0}, {0.0, 0.0, 100.0}}; auto const places{scatter::along(line, settings, random)}; CHECK(false == places.empty()); for (auto const &place : places) { CHECK(std::abs(place.point.x) <= 10.0 + 1e-9); CHECK(place.point.y >= -1e-9); CHECK(place.point.y <= 100.0 + 1e-9); } } TEST_CASE("scatter: a regular line too long for its spacing stops at the limit") { std::mt19937 random{6}; // NOSONAR(cpp:S2245) a repeatable test, not security scatter::line_settings settings; settings.mode = scatter::line_fill::regular; settings.jitter = 0.0; settings.spacing = 0.1; settings.width = 0.0; // 10 km a model every 0.1 m: 100001 places, cut short at the limit std::vector const line{{0.0, 0.0, 0.0}, {10000.0, 0.0, 0.0}}; auto const places{scatter::along(line, settings, random)}; CHECK(static_cast(places.size()) == scatter::line_most); } TEST_CASE("scatter: the irregular fill along a line keeps to the band and the spacing, thinner at the edges") { std::mt19937 random{13}; // NOSONAR(cpp:S2245) a repeatable test, not security scatter::line_settings settings; settings.mode = scatter::line_fill::irregular; settings.width = 20.0; settings.spacing = 0.5; settings.density = 20.0; settings.thin_out = true; std::vector const line{{0.0, 0.0, 0.0}, {0.0, 0.0, 200.0}}; auto const places{scatter::along(line, settings, random)}; CHECK(places.size() > 500); int inner{0}; int outer{0}; std::vector points; for (auto const &place : places) { CHECK(std::abs(place.point.x) <= 20.0 + 1e-9); CHECK(place.point.y >= -1e-9); CHECK(place.point.y <= 200.0 + 1e-9); if (place.side < 0.5) ++inner; else ++outer; points.push_back(place.point); } CHECK(inner > 2 * outer); CHECK(closest_pair(points) >= 0.5 - 1e-9); } TEST_CASE("scatter: the size of a model across the band, full on the line, as set at the edge") { scatter::line_settings settings; settings.edge_scale = 40.0; CHECK(scatter::side_scale(settings, 0.0) == doctest::Approx(1.0f)); CHECK(scatter::side_scale(settings, 0.5) == doctest::Approx(0.7f)); CHECK(scatter::side_scale(settings, 1.0) == doctest::Approx(0.4f)); settings.edge_scale = 150.0; CHECK(scatter::side_scale(settings, 1.0) == doctest::Approx(1.5f)); // one point is no line std::mt19937 random{1}; // NOSONAR(cpp:S2245) a repeatable test, not security CHECK(scatter::along({{0.0, 0.0, 0.0}}, settings, random).empty()); } TEST_CASE("scatter: a second uniform stroke over the ground of the first adds nothing where its models stand") { std::mt19937 random{11}; // NOSONAR(cpp:S2245) a repeatable test, not security scatter::dab_settings settings; settings.radius = 6.0; settings.mode = scatter::fill::uniform; settings.spacing = 2.0; // the first stroke: two dabs scatter::stroke first; auto standing{scatter::dab({10.0, 20.0}, settings, first, random)}; auto const more{scatter::dab({13.0, 20.0}, settings, first, random)}; standing.insert(standing.end(), more.begin(), more.end()); REQUIRE(standing.size() > 20); // the button let go and pressed again: a stroke of its own, which knows nothing of the first but the models standing scatter::stroke second; CHECK(scatter::dab({10.0, 20.0}, settings, second, random, standing).empty()); CHECK(scatter::dab({13.0, 20.0}, settings, second, random, standing).empty()); // a little further on it puts only the points of the grid nothing stands at, each of them once auto const further{scatter::dab({16.0, 20.0}, settings, second, random, standing)}; CHECK(false == further.empty()); for (auto const &p : further) for (auto const &q : standing) CHECK(glm::distance(p, q) >= 2.0 - 1e-9); // without the models standing (the first stroke taken back with Ctrl+Z) the ground is filled as the first time scatter::stroke again; std::set> first_points; std::set> again_points; auto const key{[](glm::dvec2 const &Point) { return std::pair{std::llround(Point.x * 1000.0), std::llround(Point.y * 1000.0)}; }}; scatter::stroke one; for (auto const &p : scatter::dab({10.0, 20.0}, settings, one, random)) first_points.insert(key(p)); for (auto const &p : scatter::dab({10.0, 20.0}, settings, again, random, {})) again_points.insert(key(p)); CHECK(first_points == again_points); // a model of the scene standing between the points of the grid, further than half the spacing from each, takes none of them scatter::stroke beside; std::vector const off{{10.0, 20.0 + 1.0e3}}; CHECK(scatter::dab({10.0, 20.0}, settings, beside, random, off).size() == first_points.size()); // the other fills don't look at the models standing: a second random stroke adds its models as before settings.mode = scatter::fill::random; settings.density = 20.0; scatter::stroke with; scatter::stroke without; std::mt19937 same_a{5}; // NOSONAR(cpp:S2245) a repeatable test, not security std::mt19937 same_b{5}; // NOSONAR(cpp:S2245) a repeatable test, not security CHECK(scatter::dab({10.0, 20.0}, settings, with, same_a, standing).size() == scatter::dab({10.0, 20.0}, settings, without, same_b).size()); } TEST_CASE("scatter: settings which aren't finite numbers give places and transforms which are") { auto const nan{std::numeric_limits::quiet_NaN()}; auto const inf{std::numeric_limits::infinity()}; std::mt19937 random{2}; // NOSONAR(cpp:S2245) a repeatable test, not security auto const finite{[](glm::dvec2 const &Point) { return std::isfinite(Point.x) && std::isfinite(Point.y); }}; for (auto const mode : {scatter::fill::random, scatter::fill::uniform, scatter::fill::feathered}) { for (auto const bad : {nan, inf, -inf, 1.0e39}) { CAPTURE(static_cast(mode)); CAPTURE(bad); scatter::dab_settings settings; settings.mode = mode; settings.radius = bad; settings.spacing = bad; settings.density = bad; scatter::stroke stroke; auto const points{scatter::dab({0.0, 0.0}, settings, stroke, random)}; CHECK(static_cast(points.size()) <= scatter::dab_most); CHECK(std::ranges::all_of(points, finite)); CHECK(std::isfinite(scatter::dab_wanted(settings))); CHECK(std::isfinite(scatter::dab_step(settings))); CHECK(scatter::dab_step(settings) > 0.0); // one of them at a time, the others as the panel gives them scatter::dab_settings wide; wide.mode = mode; wide.radius = bad; scatter::stroke wide_stroke; auto const wide_points{scatter::dab({0.0, 0.0}, wide, wide_stroke, random)}; CHECK(static_cast(wide_points.size()) <= scatter::dab_most); CHECK(std::ranges::all_of(wide_points, finite)); } } // a place of the cursor which isn't one puts nothing scatter::dab_settings plain; scatter::stroke nowhere; CHECK(scatter::dab({nan, 0.0}, plain, nowhere, random).empty()); CHECK(scatter::dab({0.0, inf}, plain, nowhere, random).empty()); CHECK(nowhere.dabs.empty()); // the turn and the scale for (auto const bad : {std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity(), -std::numeric_limits::infinity()}) { CAPTURE(bad); scatter::look_settings look; for (auto const turn : {scatter::vary::fixed, scatter::vary::random}) { for (auto const size : {scatter::vary::fixed, scatter::vary::random}) { look.turn = turn; look.size = size; look.angles = glm::vec3{bad}; look.angle_range = glm::vec3{bad}; look.scale = glm::vec3{bad}; look.scale_range = glm::vec3{bad}; check_picks_finite(look, random); } } } // along a line std::vector const line{{0.0, 0.0, 0.0}, {50.0, 1.0, 0.0}}; for (auto const mode : {scatter::line_fill::regular, scatter::line_fill::irregular}) { for (auto const bad : {nan, inf, -inf}) { CAPTURE(bad); scatter::line_settings settings; settings.mode = mode; settings.width = bad; settings.spacing = bad; settings.jitter = bad; settings.density = bad; settings.edge_scale = bad; auto const places{scatter::along(line, settings, random)}; CHECK(static_cast(places.size()) <= scatter::line_most); for (auto const &place : places) { CHECK(finite(place.point)); CHECK(std::isfinite(place.height)); CHECK(std::isfinite(place.side)); } CHECK(std::isfinite(scatter::side_scale(settings, 0.5))); CHECK(std::isfinite(scatter::side_scale(settings, bad))); } } } TEST_CASE("scatter: within the ranges of the panel the bounds of the settings change nothing") { // the same seed, the same places as before the settings were bounded: the grid of a uniform dab is a matter of the settings alone scatter::dab_settings settings; settings.mode = scatter::fill::uniform; settings.radius = 100.0; settings.spacing = 50.0; std::mt19937 random{1}; // NOSONAR(cpp:S2245) a repeatable test, not security scatter::stroke stroke; auto const points{scatter::dab({0.0, 0.0}, settings, stroke, random)}; // rows 43.3 m apart, the points 50 m apart in them, every other row shifted by 25 m: 19 within 100 m of the middle CHECK(points.size() == 19); scatter::look_settings look; look.turn = scatter::vary::fixed; look.angles = {-180.0f, 180.0f, 90.0f}; look.size = scatter::vary::fixed; look.scale = {1.0f, 1000.0f, 100.0f}; auto const picked{scatter::pick(look, random)}; CHECK(picked.angles == glm::vec3{-180.0f, 180.0f, 90.0f}); CHECK(picked.scale.x == doctest::Approx(0.01f)); CHECK(picked.scale.y == doctest::Approx(10.0f)); CHECK(picked.scale.z == doctest::Approx(1.0f)); }