mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-03 06:35:21 +01:00
cb3ac22a8d
The density of the random and the feathered fills was counted for the whole circle of every dab. The circles of a stroke overlap (a dab every half of the radius), so each place got its share from every dab that went over it: 5 models a 100 m² set came to some 15 on the ground, and more wherever the cursor came back. The count was also rounded for each dab, so a circle too small for half a model (1 m at 4 a 100 m²) never got any. A stroke now keeps the circles of its dabs beside the places of its models (scatter::stroke). A dab draws as many places as the density asks for in its circle and keeps each with the chance of what the ground there is short of: nothing where an earlier dab of the stroke has filled it, so the overlaps and the way back add no more. The count is the whole part of what is asked for and one more with the chance of its fraction, so small circles and low densities get their share over the stroke. The feathered fill is as dense as set in the middle of the stroke and thins out towards its edge; a single dab of it used to squeeze the whole count of the circle into its middle. The uniform fill (the grid) is as it was. The tooltip of the density says what it counts.
367 lines
14 KiB
C++
367 lines
14 KiB
C++
#include "stdafx.h"
|
|
|
|
#include "doctest.h"
|
|
|
|
#include "editor/editorScatter.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <set>
|
|
|
|
namespace
|
|
{
|
|
|
|
double closest_pair(std::vector<glm::dvec2> 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 + 3.14159265 * 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<double>(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<int>(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<glm::dvec3> 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<long long> rows;
|
|
for (auto const &place : band)
|
|
rows.insert(std::lround(place.point.y));
|
|
CHECK(rows == std::set<long long>{-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<glm::dvec3> 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<glm::dvec2> 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<glm::dvec3> 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<glm::dvec3> 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<int>(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<glm::dvec3> 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<glm::dvec2> 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());
|
|
}
|