18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 06:35:21 +01:00
Files
maszyna/editor/editorScatter.cpp
T
miloszunio a87a94d36d fix(editor): the brush after the review of 30.11: a second uniform stroke doubles nothing, the step of a stroke by its serial
- the uniform fill is told of the models which stand near the dab already (scatter::dab(), Standing) and leaves the points of
  its grid alone where one stands: a second stroke over the same ground adds nothing, as the description says. After a Ctrl+Z
  of the first the ground is filled again
- the step of the history of a stroke is made with its first model and looked up by its serial for each model
  (editor_mode::brush_step()): a step pushed while the button is held (Del) no longer takes the later models of the stroke,
  and a press which puts nothing (on the sky, no model chosen) leaves the steps to redo as they were
- the settings of the brush are bounded before they are used (scatter: sane()): a number typed into a field which isn't
  finite gives no place nor transform which isn't, and the fields clamp what is typed (ImGuiSliderFlags_AlwaysClamp).
  Within the ranges of the panel nothing changes
- tests: the second stroke, the settings which aren't numbers, the ranges of the panel
- redo_last() ends the stroke after the step of the terrain, and tests/unit/CMakeLists.txt lists editorScatter.cpp apart from
  the lines #311, #430 and #431 add, so the four merge without a conflict
2026-11-30 22:16:26 +00:00

464 lines
18 KiB
C++

#include "stdafx.h"
#include "editor/editorScatter.hpp"
#include <algorithm>
#include <cmath>
#include <iterator>
#include <numbers>
#include <optional>
namespace scatter
{
namespace
{
// a setting typed into a field of the panel may be anything a float can hold, "inf" and "nan" too, and what the fields
// don't stop reaches here. one which isn't a finite number is the Fallback, a finite one is kept between Least and Most: far
// more than the panel offers, so within its ranges nothing changes, and no place or transform of a model comes out of a
// number which isn't one
double bounded(double const Value, double const Least, double const Most, double const Fallback)
{
return std::isfinite(Value) ? std::clamp(Value, Least, Most) : Fallback;
}
glm::vec3 bounded(glm::vec3 const &Value, float const Least, float const Most, float const Fallback)
{
return {static_cast<float>(bounded(Value.x, Least, Most, Fallback)), static_cast<float>(bounded(Value.y, Least, Most, Fallback)), static_cast<float>(bounded(Value.z, Least, Most, Fallback))};
}
double constexpr most_metres{1000.0}; // of a radius, a spacing or a width
double constexpr most_density{1.0e6};
dab_settings sane(dab_settings Settings)
{
Settings.radius = bounded(Settings.radius, 0.0, most_metres, 0.0);
Settings.spacing = bounded(Settings.spacing, 0.0, most_metres, 0.0);
Settings.density = bounded(Settings.density, 0.0, most_density, 0.0);
return Settings;
}
line_settings sane(line_settings Settings)
{
Settings.width = bounded(Settings.width, 0.0, most_metres, 0.0);
Settings.spacing = bounded(Settings.spacing, 0.0, most_metres, 0.0);
Settings.jitter = bounded(Settings.jitter, 0.0, 0.5, 0.0);
Settings.density = bounded(Settings.density, 0.0, most_density, 0.0);
Settings.edge_scale = bounded(Settings.edge_scale, 1.0, 10000.0, 100.0);
return Settings;
}
look_settings sane(look_settings Settings)
{
Settings.angles = bounded(Settings.angles, -36000.0f, 36000.0f, 0.0f);
Settings.angle_range = bounded(Settings.angle_range, 0.0f, 36000.0f, 0.0f);
Settings.scale = bounded(Settings.scale, 1.0f, 100000.0f, 100.0f);
Settings.scale_range = bounded(Settings.scale_range, 0.0f, 10000.0f, 0.0f);
return Settings;
}
bool clear_of(glm::dvec2 const &Point, double const Spacing, std::vector<glm::dvec2> const &Points)
{
auto const least{Spacing * Spacing};
return std::ranges::all_of(Points, [&](glm::dvec2 const &Other) {
auto const d{Point - Other};
return glm::dot(d, d) >= least;
});
}
bool clear_of(glm::dvec2 const &Point, double const Spacing, std::vector<glm::dvec2> const &Taken, std::vector<glm::dvec2> const &Made)
{
return clear_of(Point, Spacing, Taken) && clear_of(Point, Spacing, Made);
}
// one row of the grid of triangles in the circle, added to Made until it holds dab_most points
void grid_row(long long const Row, glm::dvec2 const &Centre, double const Radius, double const Spacing, std::vector<glm::dvec2> const &Taken, std::vector<glm::dvec2> &Made)
{
auto const row{Spacing * std::sqrt(3.0) * 0.5};
auto const shift{(Row % 2 == 0) ? 0.0 : 0.5 * Spacing};
auto const z{static_cast<double>(Row) * row};
auto const first{static_cast<long long>(std::floor((Centre.x - Radius - shift) / Spacing))};
auto const last{static_cast<long long>(std::ceil((Centre.x + Radius - shift) / Spacing))};
for (auto c = first; c <= last && static_cast<int>(Made.size()) < dab_most; ++c)
{
glm::dvec2 const point{static_cast<double>(c) * Spacing + shift, z};
// the points of one dab are Spacing apart by the grid itself; only a point an earlier dab put is there already, the same
// point again, a hair away at most
if (glm::distance(point, Centre) <= Radius && clear_of(point, 0.5 * Spacing, Taken))
Made.push_back(point);
}
}
// the points of the grid of triangles of the world (rows Spacing·√3/2 apart, every other one shifted by half) in the circle,
// dab_most at most
std::vector<glm::dvec2> grid_points(glm::dvec2 const &Centre, double const Radius, double const Spacing, std::vector<glm::dvec2> const &Taken)
{
std::vector<glm::dvec2> made;
auto const row{Spacing * std::sqrt(3.0) * 0.5};
auto const first_row{static_cast<long long>(std::floor((Centre.y - Radius) / row))};
auto const last_row{static_cast<long long>(std::ceil((Centre.y + Radius) / row))};
for (auto r = first_row; r <= last_row && static_cast<int>(made.size()) < dab_most; ++r)
grid_row(r, Centre, Radius, Spacing, Taken, made);
return made;
}
// the line drawn, measured along its length: a point at a distance along it and an offset to its side
class polyline
{
public:
explicit polyline(std::vector<glm::dvec3> const &Points)
{
m_points.reserve(Points.size());
m_lengths.reserve(Points.size());
auto total{0.0};
for (auto const &point : Points)
{
// a point given twice (a double-click) makes a piece of no length, with no direction to put the rows to the side of
if (false == m_points.empty())
{
auto const step{glm::distance(flat(m_points.back()), flat(point))};
if (step < 1e-6)
continue;
total += step;
}
m_points.push_back(point);
m_lengths.push_back(total);
}
}
double length() const
{
return m_lengths.back();
}
// the place At metres along the line, Offset metres to its left (negative: to the right)
line_place at(double const At, double const Offset, double const Width) const
{
auto const piece{segment(At)};
auto const &a{m_points[piece]};
auto const &b{m_points[piece + 1]};
auto const span{m_lengths[piece + 1] - m_lengths[piece]};
auto const t{span > 0.0 ? std::clamp((At - m_lengths[piece]) / span, 0.0, 1.0) : 0.0};
auto const direction{span > 0.0 ? (flat(b) - flat(a)) / span : glm::dvec2{1.0, 0.0}};
glm::dvec2 const normal{-direction.y, direction.x};
line_place place;
place.point = glm::mix(flat(a), flat(b), t) + normal * Offset;
place.height = std::lerp(a.y, b.y, t);
place.side = Width > 0.0 ? std::min(1.0, std::abs(Offset) / Width) : 0.0;
return place;
}
private:
static glm::dvec2 flat(glm::dvec3 const &Point)
{
return {Point.x, Point.z};
}
// the piece of the line At metres along it lies on
std::size_t segment(double const At) const
{
auto const next{std::ranges::upper_bound(m_lengths, At)};
auto const index{static_cast<std::size_t>(std::distance(m_lengths.begin(), next))};
return std::clamp<std::size_t>(index, 1, m_points.size() - 1) - 1;
}
std::vector<glm::dvec3> m_points;
std::vector<double> m_lengths;
};
// the places taken by a line, sorted into square cells as wide as the spacing: a long line with thousands of models isn't gone
// through whole for every new one
class spaced_places
{
public:
explicit spaced_places(double const Spacing) : m_spacing{Spacing}
{
}
bool clear(glm::dvec2 const &Point, double const Least) const
{
auto const [cx, cz]{cell(Point)};
for (auto x = cx - 1; x <= cx + 1; ++x)
{
for (auto z = cz - 1; z <= cz + 1; ++z)
{
if (auto const found{m_cells.find(key(x, z))}; found != m_cells.end() && false == clear_of(Point, Least, found->second))
return false;
}
}
return true;
}
void add(glm::dvec2 const &Point)
{
auto const [cx, cz]{cell(Point)};
m_cells[key(cx, cz)].push_back(Point);
}
private:
std::pair<long long, long long> cell(glm::dvec2 const &Point) const
{
return {static_cast<long long>(std::floor(Point.x / m_spacing)), static_cast<long long>(std::floor(Point.y / m_spacing))};
}
static long long key(long long const X, long long const Z)
{
// the cells of a scenery, tens of kilometres across at most, fit the 32 bits of each half
return (X << 32) ^ (Z & 0xffffffffLL);
}
double m_spacing;
std::unordered_map<long long, std::vector<glm::dvec2>> m_cells;
};
// the regular fill: rows along the line, each a list of places a spacing apart
struct regular_line
{
polyline const &line;
line_settings const &settings;
double spacing;
std::mt19937 &random; // NOSONAR(cpp:S2245) placement of scenery models, not security
void row(long long const Row, spaced_places &Taken, std::vector<line_place> &Made) const
{
auto const offset{static_cast<double>(Row) * spacing};
auto const shift{(Row % 2 == 0) ? 0.0 : 0.5 * spacing};
auto const jitter{std::clamp(settings.jitter, 0.0, 0.5) * spacing};
std::uniform_real_distribution each_way{-1.0, 1.0};
// the places of the row counted in whole steps: no error added up along a long line
auto const steps{static_cast<long long>(std::floor((line.length() + 1e-9 - shift) / spacing))};
for (long long step = 0; step <= steps && static_cast<int>(Made.size()) < line_most; ++step)
{
auto const at{shift + static_cast<double>(step) * spacing};
auto const along_off{jitter > 0.0 ? jitter * each_way(random) : 0.0};
auto const side_off{jitter > 0.0 ? jitter * each_way(random) : 0.0};
auto place{line.at(std::clamp(at + along_off, 0.0, line.length()), offset + side_off, settings.width)};
// where the line bends, the rows on the inner side of the bend run into each other: one model of the two
if (false == Taken.clear(place.point, 0.5 * spacing))
continue;
Taken.add(place.point);
Made.push_back(place);
}
}
std::vector<line_place> make() const
{
std::vector<line_place> made;
spaced_places taken{spacing};
auto const rows{static_cast<long long>(std::floor(std::max(0.0, settings.width) / spacing + 1e-9))};
// the row on the line first, then the pairs of rows outwards: cut short by the limit, the band stays even
row(0, taken, made);
for (long long r = 1; r <= rows && static_cast<int>(made.size()) < line_most; ++r)
{
row(r, taken, made);
row(-r, taken, made);
}
return made;
}
};
// the irregular fill: random places in the band, the spacing kept
std::vector<line_place> irregular_line(polyline const &Line, line_settings const &Settings, double const Spacing, std::mt19937 &Random) // NOSONAR(cpp:S2245) placement of scenery models, not security
{
auto const width{std::max(0.0, Settings.width)};
auto const density{std::max(0.0, Settings.density)};
// over the area of the band; a line of no width gets the density per spacing of its length
auto const amount{width > 0.0 ? density * Line.length() * 2.0 * width / 100.0 : density * Line.length() / Spacing};
auto const wanted{static_cast<int>(std::min(static_cast<double>(line_most), std::round(amount)))};
std::uniform_real_distribution unit{0.0, 1.0};
std::vector<line_place> made;
spaced_places taken{Spacing};
int constexpr tries_each{30};
auto attempts{static_cast<long long>(wanted) * tries_each};
while (attempts > 0 && static_cast<int>(made.size()) < wanted)
{
--attempts;
auto const across{2.0 * unit(Random) - 1.0};
// fewer towards the edges: kept with the chance falling from 1 on the line to 0 at the edge
if (Settings.thin_out && unit(Random) > (1.0 - across * across) * (1.0 - across * across))
continue;
auto const place{Line.at(Line.length() * unit(Random), width * across, width)};
if (false == taken.clear(place.point, Spacing))
continue;
taken.add(place.point);
made.push_back(place);
}
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 &Asked, std::mt19937 &Random) // NOSONAR(cpp:S2245) placement of scenery models, not security
{
if (Points.size() < 2)
return {};
polyline const line{Points};
if (line.length() <= 0.0)
return {};
auto const Settings{sane(Asked)};
auto const spacing{std::max(0.1, Settings.spacing)};
if (Settings.mode == line_fill::irregular)
return irregular_line(line, Settings, spacing, Random);
return regular_line{line, Settings, spacing, Random}.make();
}
float side_scale(line_settings const &Settings, double const Side)
{
auto const edge{std::max(1.0, sane(Settings).edge_scale) / 100.0};
return static_cast<float>(std::lerp(1.0, edge, bounded(Side, 0.0, 1.0, 0.0)));
}
std::vector<glm::dvec2> dab(glm::dvec2 const &Centre, dab_settings const &Asked, stroke &Stroke, std::mt19937 &Random, std::vector<glm::dvec2> const &Standing) // NOSONAR(cpp:S2245) placement of scenery models, not security
{
if (false == std::isfinite(Centre.x) || false == std::isfinite(Centre.y))
return {};
auto const Settings{sane(Asked)};
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};
auto const within{[&Centre, reach](glm::dvec2 const &Point) { return glm::distance(Point, Centre) <= reach; }};
std::ranges::copy_if(Stroke.taken, std::back_inserter(nearby), within);
std::vector<glm::dvec2> made;
if (Settings.mode == fill::uniform)
{
// the grid is fixed to the world, so a stroke over the ground of an earlier one comes to the very same points: those
// where a model stands already are left alone
std::ranges::copy_if(Standing, std::back_inserter(nearby), within);
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 &Asked)
{
auto const Settings{sane(Asked)};
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 &Asked)
{
auto const Settings{sane(Asked)};
return std::max({0.05, Settings.spacing, 0.5 * Settings.radius});
}
look pick(look_settings const &Asked, std::mt19937 &Random) // NOSONAR(cpp:S2245) placement of scenery models, not security
{
auto const Settings{sane(Asked)};
look out;
auto const spread = [&](float const Range) {
if (Range <= 0.0f)
return 0.0f;
std::uniform_real_distribution<float> each_way{-Range, Range};
return each_way(Random);
};
if (Settings.turn == vary::fixed)
out.angles = Settings.angles;
else
out.angles = {spread(Settings.angle_range.x), spread(Settings.angle_range.y), spread(Settings.angle_range.z)};
glm::vec3 percent{Settings.scale};
if (Settings.size == vary::random)
{
if (Settings.same_scale)
{
auto const common{spread(Settings.scale_range.x)};
percent = glm::vec3{100.0f + common};
}
else
percent = glm::vec3{100.0f + spread(Settings.scale_range.x), 100.0f + spread(Settings.scale_range.y), 100.0f + spread(Settings.scale_range.z)};
}
out.scale = glm::max(percent, glm::vec3{1.0f}) / 100.0f;
return out;
}
} // namespace scatter