18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 06:35:21 +01:00

fix(editor): a stroke of the brush leaves the density set on the ground it goes over

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.
This commit is contained in:
miloszunio
2026-11-30 12:20:57 +00:00
parent 71bd8d95f4
commit cb3ac22a8d
7 changed files with 244 additions and 51 deletions
+97 -27
View File
@@ -6,6 +6,7 @@
#include <cmath>
#include <iterator>
#include <numbers>
#include <optional>
namespace scatter
{
@@ -242,6 +243,88 @@ std::vector<line_place> irregular_line(polyline const &Line, line_settings const
return made;
}
// the share of the full density a dab leaves at a place, Share of its radius from its middle: all of it anywhere in the circle
// for the random fill, falling from all in the middle to none at the edge for the feathered one
double dab_weight(fill const Mode, double const Share)
{
if (Share >= 1.0)
return 0.0;
if (Mode != fill::feathered)
return 1.0;
auto const inner{1.0 - Share * Share};
return inner * inner;
}
// a number of tries with the Expected count on average: its whole part, and one more with the chance of its fraction. rounded
// instead, a small circle or a low density would get nothing from any of the dabs
int tries_for(double const Expected, std::mt19937 &Random) // NOSONAR(cpp:S2245) placement of scenery models, not security
{
auto const whole{std::floor(Expected)};
std::uniform_real_distribution unit{0.0, 1.0};
auto const more{unit(Random) < Expected - whole ? 1.0 : 0.0};
return static_cast<int>(std::min(static_cast<double>(dab_most), whole + more));
}
// the ground of a dab the stroke hasn't filled yet: the dab adds only what its circle is short of
class fresh_ground
{
public:
fresh_ground(glm::dvec2 const &Centre, double const Radius, fill const Mode, std::vector<dab_circle> const &Dabs) : m_centre{Centre}, m_radius{Radius}, m_mode{Mode}
{
// only the circles which reach into this one
std::ranges::copy_if(Dabs, std::back_inserter(m_before), [&](dab_circle const &Dab) { return glm::distance(Dab.centre, Centre) < Radius + Dab.radius; });
}
// the places of the models, Spacing from each other and from the Nearby ones at least. Count of them are drawn evenly over
// the whole circle (as many as the density asks for in it), each kept with the chance of what the ground there is short of:
// the density comes out the same wherever the circles of the stroke overlap, however many of them do
std::vector<glm::dvec2> places(int const Count, double const Spacing, std::vector<glm::dvec2> const &Nearby, std::mt19937 &Random) const // NOSONAR(cpp:S2245) placement of scenery models, not security
{
std::vector<glm::dvec2> made;
for (int i = 0; i < Count; ++i)
{
if (auto const place{spaced_place(Spacing, Nearby, made, Random)}; place.has_value())
made.push_back(*place);
}
return made;
}
private:
// what the earlier dabs left at the Point already, as a share of the full density
double filled(glm::dvec2 const &Point) const
{
auto most{0.0};
for (auto const &dab : m_before)
most = std::max(most, dab_weight(m_mode, glm::distance(Point, dab.centre) / dab.radius));
return most;
}
// one place drawn in the circle and kept with the chance of what the ground there is short of; one too close to the
// others is drawn again, a number of times before it's given up for the spacing
std::optional<glm::dvec2> spaced_place(double const Spacing, std::vector<glm::dvec2> const &Nearby, std::vector<glm::dvec2> const &Made, std::mt19937 &Random) const // NOSONAR(cpp:S2245) placement of scenery models, not security
{
std::uniform_real_distribution unit{0.0, 1.0};
int constexpr tries_each{30};
for (int attempt = 0; attempt < tries_each; ++attempt)
{
// even over the area: the square root of the share of the radius
auto const share{std::sqrt(unit(Random))};
auto const angle{2.0 * std::numbers::pi * unit(Random)};
glm::dvec2 const point{m_centre.x + m_radius * share * std::cos(angle), m_centre.y + m_radius * share * std::sin(angle)};
if (unit(Random) >= dab_weight(m_mode, share) - filled(point))
return std::nullopt;
if (clear_of(point, Spacing, Nearby, Made))
return point;
}
return std::nullopt;
}
glm::dvec2 m_centre;
double m_radius;
fill m_mode;
std::vector<dab_circle> m_before;
};
} // namespace
std::vector<line_place> along(std::vector<glm::dvec3> const &Points, line_settings const &Settings, std::mt19937 &Random) // NOSONAR(cpp:S2245) placement of scenery models, not security
@@ -263,43 +346,30 @@ float side_scale(line_settings const &Settings, double const Side)
return static_cast<float>(std::lerp(1.0, edge, std::clamp(Side, 0.0, 1.0)));
}
std::vector<glm::dvec2> dab(glm::dvec2 const &Centre, dab_settings const &Settings, std::vector<glm::dvec2> const &Taken, std::mt19937 &Random) // NOSONAR(cpp:S2245) placement of scenery models, not security
std::vector<glm::dvec2> dab(glm::dvec2 const &Centre, dab_settings const &Settings, stroke &Stroke, std::mt19937 &Random) // NOSONAR(cpp:S2245) placement of scenery models, not security
{
auto const radius{std::max(0.1, Settings.radius)};
auto const spacing{std::max(0.05, Settings.spacing)};
// only what the stroke put near this circle matters: a long stroke isn't gone through whole for every point
std::vector<glm::dvec2> nearby;
auto const reach{radius + spacing};
std::ranges::copy_if(Taken, std::back_inserter(nearby), [&](glm::dvec2 const &Point) { return glm::distance(Point, Centre) <= reach; });
if (Settings.mode == fill::uniform)
return grid_points(Centre, radius, spacing, nearby);
// as many as the density asks for in the circle (no more than a dab can put at once), each tried a number of times before
// it's given up for the spacing
auto const wanted{std::min(dab_most, static_cast<int>(std::lround(std::max(0.0, Settings.density) * std::numbers::pi * radius * radius / 100.0)))};
std::uniform_real_distribution unit{0.0, 1.0};
std::ranges::copy_if(Stroke.taken, std::back_inserter(nearby), [&](glm::dvec2 const &Point) { return glm::distance(Point, Centre) <= reach; });
std::vector<glm::dvec2> made;
int constexpr tries_each{30};
auto attempts{wanted * tries_each};
while (attempts > 0 && static_cast<int>(made.size()) < wanted)
{
--attempts;
// even over the area: the square root of the share of the radius
auto const share{std::sqrt(unit(Random))};
auto const angle{2.0 * std::numbers::pi * unit(Random)};
if (Settings.mode == fill::feathered)
{
// fewer towards the edge: kept with the chance falling from 1 in the middle to 0 at the edge
auto const keep{(1.0 - share * share) * (1.0 - share * share)};
if (unit(Random) > keep)
continue;
}
glm::dvec2 const point{Centre.x + radius * share * std::cos(angle), Centre.y + radius * share * std::sin(angle)};
if (clear_of(point, spacing, nearby, made))
made.push_back(point);
}
if (Settings.mode == fill::uniform)
made = grid_points(Centre, radius, spacing, nearby);
else
made = fresh_ground{Centre, radius, Settings.mode, Stroke.dabs}.places(tries_for(dab_wanted(Settings), Random), spacing, nearby, Random);
Stroke.taken.insert(Stroke.taken.end(), made.begin(), made.end());
Stroke.dabs.emplace_back(Centre, radius);
return made;
}
double dab_wanted(dab_settings const &Settings)
{
auto const radius{std::max(0.1, Settings.radius)};
return std::max(0.0, Settings.density) * std::numbers::pi * radius * radius / 100.0;
}
double dab_step(dab_settings const &Settings)
{
return std::max({0.05, Settings.spacing, 0.5 * Settings.radius});