#include "stdafx.h" #include "editor/editorScatter.hpp" #include #include #include #include #include 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(bounded(Value.x, Least, Most, Fallback)), static_cast(bounded(Value.y, Least, Most, Fallback)), static_cast(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 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 const &Taken, std::vector 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 const &Taken, std::vector &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(Row) * row}; auto const first{static_cast(std::floor((Centre.x - Radius - shift) / Spacing))}; auto const last{static_cast(std::ceil((Centre.x + Radius - shift) / Spacing))}; for (auto c = first; c <= last; ++c) { if (static_cast(Made.size()) >= dab_most) break; glm::dvec2 const point{static_cast(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 grid_points(glm::dvec2 const &Centre, double const Radius, double const Spacing, std::vector const &Taken) { std::vector made; auto const row{Spacing * std::sqrt(3.0) * 0.5}; auto const first_row{static_cast(std::floor((Centre.y - Radius) / row))}; auto const last_row{static_cast(std::ceil((Centre.y + Radius) / row))}; for (auto r = first_row; r <= last_row; ++r) { if (static_cast(made.size()) >= dab_most) break; 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 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::distance(m_lengths.begin(), next))}; return std::clamp(index, 1, m_points.size() - 1) - 1; } std::vector m_points; std::vector 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 cell(glm::dvec2 const &Point) const { return {static_cast(std::floor(Point.x / m_spacing)), static_cast(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> 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 &Made) const { auto const offset{static_cast(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(std::floor((line.length() + 1e-9 - shift) / spacing))}; for (long long step = 0; step <= steps; ++step) { if (static_cast(Made.size()) >= line_most) break; auto const at{shift + static_cast(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 make() const { std::vector made; spaced_places taken{spacing}; auto const rows{static_cast(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; ++r) { if (static_cast(made.size()) >= line_most) break; row(r, taken, made); row(-r, taken, made); } return made; } }; // the irregular fill: random places in the band, the spacing kept std::vector 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(std::min(static_cast(line_most), std::round(amount)))}; std::uniform_real_distribution unit{0.0, 1.0}; std::vector made; spaced_places taken{Spacing}; int constexpr tries_each{30}; auto attempts{static_cast(wanted) * tries_each}; while (attempts > 0 && static_cast(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(std::min(static_cast(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 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 places(int const Count, double const Spacing, std::vector const &Nearby, std::mt19937 &Random) const // NOSONAR(cpp:S2245) placement of scenery models, not security { std::vector 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 spaced_place(double const Spacing, std::vector const &Nearby, std::vector 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 m_before; }; } // namespace std::vector along(std::vector 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(std::lerp(1.0, edge, bounded(Side, 0.0, 1.0, 0.0))); } std::vector dab(glm::dvec2 const &Centre, dab_settings const &Asked, stroke &Stroke, std::mt19937 &Random, std::vector 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 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 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 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