diff --git a/application/editormode.cpp b/application/editormode.cpp index 59e0467db..659f0c3ba 100644 --- a/application/editormode.cpp +++ b/application/editormode.cpp @@ -969,6 +969,8 @@ void editor_mode::restore_terrain(EditorSnapshot &Snapshot) void editor_mode::undo_last() { + // Ctrl+Z with the brush still held: the stroke so far is a step of its own, the one taken back, and the dabs stop there + end_brush_stroke(); first_step_done(first_step::undo); // the warning by the cursor told of the step taken back now: "Ctrl+Z takes it back" is done ui()->drop_refusal(); @@ -1168,6 +1170,7 @@ void editor_mode::undo_one() void editor_mode::redo_last() { + end_brush_stroke(); // a change of the terrain made since the undo goes to the history, and the redo steps are dropped with it m_history_busy = true; terrain_history_step(); @@ -7390,6 +7393,12 @@ void editor_mode::on_key(int const Key, int const Scancode, int const Action, in area_finish(false); break; } + // the line of the brush being clicked given up + if (is_press(Action) && ui()->mode() == nodebank_panel::BRUSH && false == m_brush_line.empty()) + { + m_brush_line.clear(); + break; + } if (is_press(Action)) { road_cancel(); @@ -7462,6 +7471,9 @@ void editor_mode::on_key(int const Key, int const Scancode, int const Action, in // area fill: remove the last outline point if (is_press(Action) && ui()->mode() == nodebank_panel::FILL && !m_fill_points.empty()) m_fill_points.pop_back(); + // the line of the brush: the last point taken back + if (is_press(Action) && ui()->mode() == nodebank_panel::BRUSH && false == m_brush_line.empty()) + m_brush_line.pop_back(); if (is_press(Action) && ui()->mode() == nodebank_panel::TRACK && m_lay.active && !m_lay.points.empty()) lay_back(); if (is_press(Action) && m_route.trace.active && route_active() && !m_route.trace.points.empty()) @@ -7482,6 +7494,9 @@ void editor_mode::on_key(int const Key, int const Scancode, int const Action, in } if (is_press(Action) && ui()->mode() == nodebank_panel::TRACK && m_lay.active) lay_enter(); + // the models of the brush put along the line clicked + if (is_press(Action) && ui()->mode() == nodebank_panel::BRUSH && false == m_brush_line.empty()) + brush_line_finish(); if (is_press(Action) && m_route.trace.active && route_active()) route_trace_end(true); break; diff --git a/application/editormode.h b/application/editormode.h index 9ea401c7c..9025358a4 100644 --- a/application/editormode.h +++ b/application/editormode.h @@ -221,7 +221,11 @@ class editor_mode : public application_mode, private editor_track::observer, pri double brush_height(glm::dvec2 const &Point, double Around); // one dab of the stroke of many models, at the cursor on the ground void brush_dab(); - TAnimModel *place_brush_model(glm::dvec3 const &At); + // the many along a line: a point of it at the cursor, the models put along it, the line and its band drawn on the ground + void brush_line_point(); + void brush_line_finish(); + void draw_brush_line(glm::dvec3 const *Cursor); + TAnimModel *place_brush_model(glm::dvec3 const &At, float Scale = 1.0f); void apply_rotation_for_new_node(scene::basic_node *node, int rotation_mode, float fixed_rotation_value); // members state_backup m_statebackup; // helper, cached variables to be restored on mode exit @@ -254,6 +258,7 @@ class editor_mode : public application_mode, private editor_track::observer, pri bool m_brush_has_last{false}; // false at the start of a brush stroke, so the first dab ignores the step bool m_brush_stroke{false}; // a stroke of the brush of many models is on: the last step of the history takes its models std::vector m_brush_taken; // the places the stroke put models on (x, z) + std::vector m_brush_line; // the points of the line the brush of many puts models along, in the order clicked std::mt19937 m_brush_random{std::random_device{}()}; // NOSONAR(cpp:S2245) where the brush puts models, not security bool m_draw_cursor{false}; // the cursor drawn into the picture (View menu), on only while the editor is bool mouseHold{false}; diff --git a/application/editormodebrush.cpp b/application/editormodebrush.cpp index bb8c08c7f..b425997f6 100644 --- a/application/editormodebrush.cpp +++ b/application/editormodebrush.cpp @@ -56,7 +56,7 @@ double editor_mode::brush_height(glm::dvec2 const &Point, double const Around) return placement_on_ground({Point.x, Around, Point.y}).y; } -TAnimModel *editor_mode::place_brush_model(glm::dvec3 const &At) +TAnimModel *editor_mode::place_brush_model(glm::dvec3 const &At, float const Scale) { auto const *source{ui()->get_active_node_template()}; // the scenery templates are put one at a time with the insert mode @@ -69,7 +69,7 @@ TAnimModel *editor_mode::place_brush_model(glm::dvec3 const &At) return nullptr; auto const look{scatter::pick(ui()->brush().look, m_brush_random)}; m_editor.rotate(model, look.angles, 0.0f); - model->Scale(model->Scale() * look.scale); + model->Scale(model->Scale() * look.scale * Scale); add_to_hierarchy(model); return model; } @@ -79,7 +79,14 @@ void editor_mode::brush_press() mouseHold = true; m_brush_has_last = false; m_brush_taken.clear(); - if (ui()->brush().way != brush_object_list::kind::single) + auto const &settings{ui()->brush()}; + if (settings.way != brush_object_list::kind::single && settings.stroke == brush_object_list::shape::line) + { + // a point of the line, read once the renderer has the depth under the cursor + GfxRenderer->Pick_Node_Callback([this](scene::basic_node * /*node*/) { brush_line_point(); }); + return; + } + if (settings.way != brush_object_list::kind::single) { // one step of the history for the whole stroke, the models added to it as they're put trim_history(); @@ -131,7 +138,10 @@ void editor_mode::brush_dab() oldPos = centre; m_brush_has_last = true; auto &stroke{m_history.back()}; - for (auto const &point : scatter::dab({centre.x, centre.z}, settings, m_brush_taken, m_brush_random)) + auto const points{scatter::dab({centre.x, centre.z}, settings, m_brush_taken, m_brush_random)}; + if (static_cast(points.size()) >= scatter::dab_most) + ui()->set_status(format(STR_C("Brush: a dab puts %d models at most; widen the spacing or narrow the circle for the rest"), scatter::dab_most)); + for (auto const &point : points) { auto *model{place_brush_model({point.x, brush_height(point, centre.y), point.y})}; // the place is taken either way, so a model which failed isn't tried there on every move @@ -163,17 +173,106 @@ void editor_mode::end_brush_stroke() ui()->set_status(format(STR_C("Brush: %zu models put; Ctrl+Z takes the whole stroke back"), count)); } -void editor_mode::draw_brush_cursor() +void editor_mode::brush_line_point() { - glm::dvec3 centre; - if (false == brush_ground(centre)) + if (false == viewport_click()) + return; + glm::dvec3 at; + if (brush_ground(at)) + m_brush_line.push_back(at); +} + +void editor_mode::brush_line_finish() +{ + if (m_brush_line.size() < 2) + { + ui()->set_status(STR_C("Brush: a line needs two points at least")); + return; + } + auto const &settings{ui()->brush()}; + auto const places{scatter::along(m_brush_line, settings.line, m_brush_random)}; + // the whole line one step of the history, as a stroke is + EditorSnapshot step; + step.action = EditorSnapshot::Action::Array; + for (auto const &place : places) + { + auto *model{place_brush_model({place.point.x, brush_height(place.point, place.height), place.point.y}, scatter::side_scale(settings.line, place.side))}; + if (model == nullptr) + continue; + EditorSnapshot::array_copy copy; + copy.model = model; + copy.uuid = model->uuid; + step.layer = model->layer(); + step.copies.push_back(std::move(copy)); + } + m_brush_line.clear(); + auto const count{step.copies.size()}; + if (count == 0) + { + ui()->set_status(STR_C("Brush: no models put along the line")); + return; + } + trim_history(); + m_history.push_back(std::move(step)); + g_redo.clear(); + if (static_cast(places.size()) >= scatter::line_most) + ui()->set_status(format(STR_C("Brush: %zu models put along the line, the most one line puts; Ctrl+Z takes them back"), count)); + else + ui()->set_status(format(STR_C("Brush: %zu models put along the line; Ctrl+Z takes them back"), count)); +} + +void editor_mode::draw_brush_line(glm::dvec3 const *Cursor) +{ + if (m_brush_line.empty()) return; screen_projection const view; auto *drawlist{ImGui::GetBackgroundDrawList(ImGui::GetMainViewport())}; + auto const width{std::max(0.0, ui()->brush().line.width)}; + auto const lifted = [](glm::dvec3 const &Point) { return Point + glm::dvec3{0.0, 0.05, 0.0}; }; + auto const side = [this, &lifted](glm::dvec3 const &Point, glm::dvec2 const &Offset) { + glm::dvec2 const at{Point.x + Offset.x, Point.z + Offset.y}; + return lifted({at.x, brush_height(at, Point.y), at.y}); + }; + for (std::size_t i = 1; i < m_brush_line.size(); ++i) + { + auto const &a{m_brush_line[i - 1]}; + auto const &b{m_brush_line[i]}; + view.line(drawlist, lifted(a), lifted(b), kShadowColour, 4.0f); + view.line(drawlist, lifted(a), lifted(b), kCrossColour, 2.0f); + // the edges of the band, piece by piece + glm::dvec2 const run{b.x - a.x, b.z - a.z}; + auto const length{glm::length(run)}; + if (width <= 0.0 || length <= 0.0) + continue; + auto const normal{glm::dvec2{-run.y, run.x} * (width / length)}; + view.line(drawlist, side(a, normal), side(b, normal), kCircleColour, 1.5f); + view.line(drawlist, side(a, -normal), side(b, -normal), kCircleColour, 1.5f); + } + // the piece the next click would add + if (Cursor != nullptr) + view.line(drawlist, lifted(m_brush_line.back()), lifted(*Cursor), kCircleColour, 1.5f); + for (auto const &point : m_brush_line) + { + if (ImVec2 screen; view.project(lifted(point), screen)) + drawlist->AddCircleFilled(screen, 4.0f, kCrossColour); + } +} + +void editor_mode::draw_brush_cursor() +{ auto const &settings{ui()->brush()}; auto const many{settings.way == brush_object_list::kind::multi}; + auto const on_line{many && settings.stroke == brush_object_list::shape::line}; + glm::dvec3 centre; + auto const on_ground{brush_ground(centre)}; + if (on_line) + draw_brush_line(on_ground ? ¢re : nullptr); + if (false == on_ground) + return; + screen_projection const view; + auto *drawlist{ImGui::GetBackgroundDrawList(ImGui::GetMainViewport())}; // the cross on the ground, as large as a tenth of the circle (a metre for the single model), between 0.5 and 3 m - auto const arm{std::clamp(many ? 0.1 * settings.dab.radius : 1.0, 0.5, 3.0)}; + auto const arm{std::clamp(many && false == on_line ? 0.1 * settings.dab.radius : 1.0, 0.5, 3.0)}; auto const ground = [&](double const X, double const Z) { return glm::dvec3{X, brush_height({X, Z}, centre.y) + 0.05, Z}; }; std::array, 2> const arms{std::make_pair(ground(centre.x - arm, centre.z), ground(centre.x + arm, centre.z)), std::make_pair(ground(centre.x, centre.z - arm), ground(centre.x, centre.z + arm))}; @@ -182,7 +281,7 @@ void editor_mode::draw_brush_cursor() view.line(drawlist, a, b, kShadowColour, 4.0f); view.line(drawlist, a, b, kCrossColour, 2.0f); } - if (false == many) + if (false == many || on_line) return; // the circle, laid on the ground int constexpr pieces{64}; diff --git a/application/editoruipanels.cpp b/application/editoruipanels.cpp index a65b6239d..643dbe865 100644 --- a/application/editoruipanels.cpp +++ b/application/editoruipanels.cpp @@ -623,6 +623,17 @@ void brush_object_list::render_options(nodebank_panel &Bank) void brush_object_list::render_dab() { + auto on_line{stroke == shape::line}; + if (ImGui::RadioButton(STR_C("In a circle##brushshape"), false == on_line)) + stroke = shape::circle; + ImGui::SameLine(); + if (ImGui::RadioButton(STR_C("Along a line##brushshape"), on_line)) + stroke = shape::line; + if (stroke == shape::line) + { + render_line(); + return; + } ImGui::TextDisabled("%s", STR_C("Hold LMB: models in the circle along the cursor's path; one Ctrl+Z takes the whole stroke back")); if (auto radius{static_cast(dab.radius)}; ImGui::SliderFloat(STR_C("Radius##brush"), &radius, 0.5f, 100.0f, "%.1f m", ImGuiSliderFlags_Logarithmic)) dab.radius = radius; @@ -642,6 +653,41 @@ void brush_object_list::render_dab() ImGui::SetTooltip("%s", STR_C("How many models a 100 m² the circle gets, as many as the spacing lets in")); } +void brush_object_list::render_line() +{ + ImGui::TextDisabled("%s", STR_C("LMB: the points of the line; Enter puts the models, Backspace takes the last point back, Esc drops the line")); + if (auto width{static_cast(line.width)}; ImGui::SliderFloat(STR_C("Width each side##brushline"), &width, 0.0f, 100.0f, "%.1f m")) + line.width = width; + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("%s", STR_C("How far to each side of the line the models go; 0: one row on the line itself")); + std::array const fills{STR_C("Regular, in rows"), STR_C("Irregular, anywhere in the band")}; + ImGui::SetNextItemWidth(220.0f); + if (int fillmode{static_cast(line.mode)}; ImGui::Combo(STR_C("Fill##brushline"), &fillmode, fills.data(), static_cast(fills.size()))) + line.mode = static_cast(fillmode); + auto const regular{line.mode == scatter::line_fill::regular}; + if (auto spacing{static_cast(line.spacing)}; ImGui::SliderFloat(STR_C("Spacing##brushline"), &spacing, 0.1f, 50.0f, "%.1f m", ImGuiSliderFlags_Logarithmic)) + line.spacing = spacing; + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("%s", regular ? STR_C("The step along the line and between the rows") : STR_C("The least distance between two models")); + if (regular) + { + if (auto jitter{static_cast(line.jitter * 100.0)}; ImGui::SliderFloat(STR_C("Irregularity##brushline"), &jitter, 0.0f, 50.0f, "%.0f%%")) + line.jitter = jitter / 100.0; + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("%s", STR_C("Each model moved off its place in the rows by up to this share of the spacing each way; 0: exactly in rows")); + } + else + { + if (auto density{static_cast(line.density)}; ImGui::SliderFloat(STR_C("Density##brushline"), &density, 0.1f, 200.0f, STR_C("%.1f per 100 m²"), ImGuiSliderFlags_Logarithmic)) + line.density = density; + ImGui::Checkbox(STR_C("Fewer towards the edges##brushline"), &line.thin_out); + } + if (auto edge{static_cast(line.edge_scale)}; ImGui::SliderFloat(STR_C("Size at the edges##brushline"), &edge, 1.0f, 200.0f, "%.0f%%")) + line.edge_scale = edge; + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("%s", STR_C("The size of the models at the edges of the band, as a share of their size on the line; between, it goes evenly from one to the other")); +} + void brush_object_list::render_look() { using enum scatter::vary; diff --git a/application/editoruipanels.h b/application/editoruipanels.h index 7757cf48f..bded5a0ad 100644 --- a/application/editoruipanels.h +++ b/application/editoruipanels.h @@ -124,14 +124,25 @@ class brush_object_list std::string *GetRandomObject(); bool useRandom = {false}; model_set_ref source; + // the many put in the circle along the path of the cursor, or along a line clicked on the ground point by point + enum class shape + { + circle, + line, + }; + kind way{kind::multi}; + shape stroke{shape::circle}; scatter::dab_settings dab; // the circle of the many and how they fill it + scatter::line_settings line; // the band along the line and how it's filled scatter::look_settings look; // the turn and the scale of each model private: void render_look(); // the settings of the stroke of many models void render_dab(); + // the settings of the many along a line + void render_line(); }; class nodebank_panel : public ui_panel diff --git a/editor/editorScatter.cpp b/editor/editorScatter.cpp index a421035da..23c68da03 100644 --- a/editor/editorScatter.cpp +++ b/editor/editorScatter.cpp @@ -1,3 +1,5 @@ +#include "stdafx.h" + #include "editor/editorScatter.hpp" #include @@ -11,42 +13,245 @@ namespace scatter namespace { -bool clear_of(glm::dvec2 const &Point, double const Spacing, std::vector const &Taken, std::vector const &Made) +bool clear_of(glm::dvec2 const &Point, double const Spacing, std::vector const &Points) { auto const least{Spacing * Spacing}; - auto const far_enough = [&](glm::dvec2 const &Other) { + return std::ranges::all_of(Points, [&](glm::dvec2 const &Other) { auto const d{Point - Other}; return glm::dot(d, d) >= least; - }; - return std::ranges::all_of(Taken, far_enough) && std::ranges::all_of(Made, far_enough); + }); } -// the points of the grid of triangles of the world (rows Spacing·√3/2 apart, every other one shifted by half) in the circle +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 && static_cast(Made.size()) < dab_most; ++c) + { + 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) + for (auto r = first_row; r <= last_row && static_cast(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 const &Points) : m_points{Points} { - auto const shift{(r % 2 == 0) ? 0.0 : 0.5 * Spacing}; - auto const z{static_cast(r) * 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) + m_lengths.reserve(m_points.size()); + auto total{0.0}; + m_lengths.push_back(total); + for (std::size_t i = 1; i < m_points.size(); ++i) { - glm::dvec2 const point{static_cast(c) * Spacing + shift, z}; - // a point of the grid put by an earlier dab is there: the same point again, a hair away at most - if (glm::distance(point, Centre) <= Radius && clear_of(point, 0.5 * Spacing, Taken, made)) - made.push_back(point); + total += glm::distance(flat(m_points[i - 1]), flat(m_points[i])); + 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 const &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}; + for (auto at = shift; at <= line.length() + 1e-9 && static_cast(Made.size()) < line_most; at += 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 && static_cast(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 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; } } // namespace +std::vector along(std::vector const &Points, line_settings const &Settings, 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 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, Settings.edge_scale) / 100.0}; + return static_cast(std::lerp(1.0, edge, std::clamp(Side, 0.0, 1.0))); +} + std::vector dab(glm::dvec2 const &Centre, dab_settings const &Settings, std::vector const &Taken, std::mt19937 &Random) // NOSONAR(cpp:S2245) placement of scenery models, not security { auto const radius{std::max(0.1, Settings.radius)}; @@ -59,8 +264,7 @@ std::vector dab(glm::dvec2 const &Centre, dab_settings const &Settin 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 - int constexpr most{2000}; - auto const wanted{std::min(most, static_cast(std::lround(std::max(0.0, Settings.density) * std::numbers::pi * radius * radius / 100.0)))}; + auto const wanted{std::min(dab_most, static_cast(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::vector made; int constexpr tries_each{30}; diff --git a/editor/editorScatter.hpp b/editor/editorScatter.hpp index 3588130b6..9d9c5cf68 100644 --- a/editor/editorScatter.hpp +++ b/editor/editorScatter.hpp @@ -3,6 +3,7 @@ // where the brush of models puts them, and how it turns and scales each: engine-free, so it can be tested on its own #include +#include #include #include @@ -26,6 +27,9 @@ struct dab_settings double density{4.0}; // models a 100 m² for the random and the feathered fills, as much as the spacing lets }; +// the most models one dab puts, whatever the fill: a dab wider or denser than that is cut short +int constexpr dab_most{2000}; + // the places of the models of one dab of the brush round Centre (x, z): inside the circle, Spacing from each other and from those // already Taken (by the stroke so far) at least std::vector dab(glm::dvec2 const &Centre, dab_settings const &Settings, std::vector const &Taken, std::mt19937 &Random); // NOSONAR(cpp:S2245) placement of scenery models, not security @@ -33,6 +37,41 @@ std::vector dab(glm::dvec2 const &Centre, dab_settings const &Settin // the distance the cursor goes before the next dab: the circles of two dabs overlap by half double dab_step(dab_settings const &Settings); +// how the models stand along a line drawn on the ground +enum class line_fill +{ + regular, // in rows along the line, Spacing apart along it and across it, every other row shifted by half + irregular, // anywhere in the band, no closer to each other than the spacing +}; + +struct line_settings +{ + double width{0.0}; // m each side of the line: 0 is one row on the line itself + line_fill mode{line_fill::regular}; + double spacing{4.0}; // m: the step along the line and between the rows; the least distance for the irregular fill + double jitter{0.0}; // the regular fill moved off its places by up to this share of the spacing each way, 0 to 0.5 + double density{4.0}; // models a 100 m² of the band for the irregular fill (on a line of no width: per spacing of it) + bool thin_out{false}; // the irregular fill fewer and fewer towards the edges of the band + double edge_scale{100.0}; // percent of their size the models at the edges of the band get, down from the full size on the line +}; + +// a place along the line: where (x, z), the height of the line there, and how far out in the band it is (0 on the line, 1 at the edge) +struct line_place +{ + glm::dvec2 point; + double height; + double side; +}; + +// the most models one line puts +int constexpr line_most{20000}; + +// the places of the models along the line through Points (x, height, z), two points at least +std::vector along(std::vector const &Points, line_settings const &Settings, std::mt19937 &Random); // NOSONAR(cpp:S2245) placement of scenery models, not security + +// the share of its size a model gets at the place Side of the band (0 on the line, 1 at the edge) +float side_scale(line_settings const &Settings, double Side); + // a setting the same for every model, or drawn for each from a range enum class vary { diff --git a/lang/editor_pl.po b/lang/editor_pl.po index f63ebc645..af89ac8aa 100644 --- a/lang/editor_pl.po +++ b/lang/editor_pl.po @@ -14330,3 +14330,66 @@ msgstr "Każdy model przeskalowany losowo o tyle procent w obie strony od własn msgid "Brush: %zu models put; Ctrl+Z takes the whole stroke back" msgstr "Pędzel: postawiono %zu modeli; Ctrl+Z cofa całe pociągnięcie" + +msgid "Brush: a dab puts %d models at most; widen the spacing or narrow the circle for the rest" +msgstr "Pędzel: jedno dotknięcie stawia najwyżej %d modeli; zwiększ odstęp albo zmniejsz koło, by objąć resztę" + +msgid "In a circle##brushshape" +msgstr "W kole##brushshape" + +msgid "Along a line##brushshape" +msgstr "Wzdłuż linii##brushshape" + +msgid "LMB: the points of the line; Enter puts the models, Backspace takes the last point back, Esc drops the line" +msgstr "LPM: punkty linii; Enter stawia modele, Backspace cofa ostatni punkt, Esc porzuca linię" + +msgid "Width each side##brushline" +msgstr "Szerokość w każdą stronę##brushline" + +msgid "How far to each side of the line the models go; 0: one row on the line itself" +msgstr "Jak daleko w każdą stronę od linii sięgają modele; 0: jeden rząd na samej linii" + +msgid "Regular, in rows" +msgstr "Regularnie, w rzędach" + +msgid "Irregular, anywhere in the band" +msgstr "Nieregularnie, gdziekolwiek w pasie" + +msgid "Fill##brushline" +msgstr "Wypełnienie##brushline" + +msgid "Spacing##brushline" +msgstr "Odstęp##brushline" + +msgid "The step along the line and between the rows" +msgstr "Krok wzdłuż linii i odstęp między rzędami" + +msgid "Irregularity##brushline" +msgstr "Nieregularność##brushline" + +msgid "Each model moved off its place in the rows by up to this share of the spacing each way; 0: exactly in rows" +msgstr "Każdy model przesunięty ze swojego miejsca w rzędzie o najwyżej taką część odstępu w każdą stronę; 0: dokładnie w rzędach" + +msgid "Density##brushline" +msgstr "Gęstość##brushline" + +msgid "Fewer towards the edges##brushline" +msgstr "Rzadziej ku krawędziom##brushline" + +msgid "Size at the edges##brushline" +msgstr "Rozmiar na krawędziach##brushline" + +msgid "The size of the models at the edges of the band, as a share of their size on the line; between, it goes evenly from one to the other" +msgstr "Rozmiar modeli na krawędziach pasa jako część ich rozmiaru na linii; pomiędzy zmienia się równomiernie od jednego do drugiego" + +msgid "Brush: a line needs two points at least" +msgstr "Pędzel: linia potrzebuje co najmniej dwóch punktów" + +msgid "Brush: no models put along the line" +msgstr "Pędzel: wzdłuż linii nie postawiono żadnego modelu" + +msgid "Brush: %zu models put along the line, the most one line puts; Ctrl+Z takes them back" +msgstr "Pędzel: postawiono %zu modeli wzdłuż linii, najwięcej, ile stawia jedna linia; Ctrl+Z je cofa" + +msgid "Brush: %zu models put along the line; Ctrl+Z takes them back" +msgstr "Pędzel: postawiono %zu modeli wzdłuż linii; Ctrl+Z je cofa" diff --git a/tests/unit/test_scatter.cpp b/tests/unit/test_scatter.cpp index d3b198621..f9b0b7474 100644 --- a/tests/unit/test_scatter.cpp +++ b/tests/unit/test_scatter.cpp @@ -6,6 +6,7 @@ #include #include +#include namespace { @@ -133,3 +134,104 @@ TEST_CASE("scatter: the turn and the scale of a model") 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 + auto const points{scatter::dab({0.0, 0.0}, settings, {}, 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 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()); +}