From afc43b58321a9545c38ca3c39034da4fb6223f7e Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 22:07:19 +0100 Subject: [PATCH] feat(editor): the brush of models as one model or many in a circle MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The brush of the node bank put one model under the cursor every time it went the spacing further, with no idea of an area. It's two ways now: - Single model: LMB puts one model at the cross on the ground. - Many models: while LMB is held, models land in a circle of the radius chosen along the cursor's path, a new dab every half radius (or spacing). The circle fills at random (a density a 100 m², no closer than the spacing), uniformly (a grid of triangles as wide as the spacing, fixed to the world, so strokes over one another don't double it) or feathered (fewer towards the edge). A whole stroke is one step of the history. Every model is turned (fixed angles about x, y, z, or random up to ± degrees about each) and scaled (fixed percent along x, y, z, or random ± percent, one for all the axes or each its own). A cross under the cursor and the circle of the brush are drawn on the ground. Where the models go and how they're turned and scaled is in editor/editorScatter (engine-free), with tests in tests/unit/test_scatter.cpp. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ --- CMakeLists.txt | 2 + application/editormode.cpp | 61 +++------- application/editormode.h | 17 ++- application/editormodebrush.cpp | 198 ++++++++++++++++++++++++++++++++ application/editoruilayer.cpp | 7 +- application/editoruilayer.h | 3 +- application/editoruipanels.cpp | 76 +++++++++++- application/editoruipanels.h | 15 ++- editor/editorScatter.cpp | 120 +++++++++++++++++++ editor/editorScatter.hpp | 63 ++++++++++ lang/editor_pl.po | 90 +++++++++++++++ tests/unit/CMakeLists.txt | 1 + tests/unit/test_scatter.cpp | 134 +++++++++++++++++++++ 13 files changed, 733 insertions(+), 54 deletions(-) create mode 100644 application/editormodebrush.cpp create mode 100644 editor/editorScatter.cpp create mode 100644 editor/editorScatter.hpp create mode 100644 tests/unit/test_scatter.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e97e41d03..e24244e1f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -212,6 +212,7 @@ set(SOURCES "editor/editorGauge.cpp" "editor/editorInfra.cpp" "editor/editorTerrainStreamer.cpp" +"editor/editorScatter.cpp" "editor/editorOrthophoto.cpp" "editor/editorHttp.cpp" "editor/editorNmt.cpp" @@ -252,6 +253,7 @@ set(SOURCES "application/editormodelineside.cpp" "application/editormodevehicle.cpp" "application/editormodeselection.cpp" +"application/editormodebrush.cpp" "application/editormodeviewport.cpp" "application/editormodesignal.cpp" "application/editormodesweep.cpp" diff --git a/application/editormode.cpp b/application/editormode.cpp index e08e1f0e7..59e0467db 100644 --- a/application/editormode.cpp +++ b/application/editormode.cpp @@ -795,51 +795,6 @@ void editor_mode::snap_to_ground(scene::basic_node *node, bool const Alone, bool m_editor.translate(node, target, true); // true == apply the computed Y (free vertical move) } -void editor_mode::handle_brush_mouse_hold(int Action, int Button) -{ - auto const mode = ui()->mode(); - auto const rotation_mode = ui()->rot_mode(); - auto const fixed_rotation_value = ui()->rot_val(); - - if(mode != nodebank_panel::BRUSH) - return; - - GfxRenderer->Pick_Node_Callback( - [this, mode, rotation_mode, fixed_rotation_value, Action, Button](scene::basic_node * /*node*/) { - // picks are resolved a few frames later; drop the ones queued before the button went up, - // or for a press that turned out to belong to the UI - if (!mouseHold || !viewport_click()) - return; - - // spacing is measured in world space: the mouse offset is camera-relative and the camera may move mid-stroke - glm::dvec3 const newPos = Camera.Pos + clamp_mouse_offset_to_max(GfxRenderer->Mouse_Position()); - if (m_brush_has_last && glm::distance(newPos, oldPos) < ui()->getSpacing()) - return; - - // picked only when a placement is due, so a random pick from a set isn't drawn every tick - const std::string *src = ui()->get_active_node_template(); - if (!src || src->starts_with(editor_includes::directive_mark)) // scenery templates are placed one at a time - return; - - // NOTE: no name for what the brush puts in, as with thousands of nodes the names take their share of the time - // the scenery needs to load. a node which needs one, to be referred to by an event, gets it in the node properties - TAnimModel *cloned = simulation::State.create_model(*src, std::string{}, placement_on_ground(newPos)); - oldPos = newPos; - m_brush_has_last = true; - if (!cloned) - return; - - apply_rotation_for_new_node(cloned, rotation_mode, fixed_rotation_value); - - std::string as_text; - cloned->export_as_text(as_text); - push_snapshot(cloned, EditorSnapshot::Action::Add, as_text); - - m_node = cloned; - ui()->set_node(m_node); - }); -} - void editor_mode::add_to_hierarchy(scene::basic_node *node) { if (!node) return; @@ -1489,6 +1444,11 @@ bool editor_mode::update() if(ui()->mode() == nodebank_panel::BRUSH) handle_brush_mouse_hold(GLFW_REPEAT, GLFW_MOUSE_BUTTON_LEFT); } + else + { + // the button let go: the stroke's models make one step of the history + end_brush_stroke(); + } // decelerate camera velocity with thresholding Camera.Velocity *= 0.65f; @@ -1703,6 +1663,9 @@ bool editor_mode::update() // --- area fill: outline overlay while the mode is active (its settings are drawn in the node bank window) --- if (ui()->mode() == nodebank_panel::FILL) draw_area_fill_outline(); + // --- the brush of models: the cross on the ground under the cursor, the circle of the brush of many --- + if (ui()->mode() == nodebank_panel::BRUSH && false == ImGui::GetIO().WantCaptureMouse) + draw_brush_cursor(); background->PopClipRect(); draw_ortho_compass(); @@ -7677,6 +7640,14 @@ void editor_mode::on_mouse_button(int const Button, int const Action, int const mouseHold = true; m_brush_has_last = false; // every brush stroke starts with a placement + // the brush puts models, it doesn't select + if (mode == nodebank_panel::BRUSH) + { + brush_press(); + m_input.mouse.button(Button, Action); + return; + } + // in area fill mode the left button only adds outline points, it doesn't touch the selection if (mode == nodebank_panel::FILL) { diff --git a/application/editormode.h b/application/editormode.h index bcf22592c..32d8ba7e3 100644 --- a/application/editormode.h +++ b/application/editormode.h @@ -9,6 +9,8 @@ http://mozilla.org/MPL/2.0/. #pragma once +#include + #include "application/applicationmode.h" #include "application/editoruipanels.h" #include "input/editormouseinput.h" @@ -210,6 +212,14 @@ class editor_mode : public application_mode, private editor_track::observer, pri void restore_kilometrage(EditorSnapshot &Snapshot); bool m_history_busy{false}; // undo or redo under way void handle_brush_mouse_hold(int Action, int Button); + // the brush of models (editormodebrush.cpp): the press starts a stroke of many, or puts one model; the point on the ground under + // the cursor, the height of the ground near a height, a model of the template the brush paints, turned and scaled as it says + void brush_press(); + void end_brush_stroke(); + void draw_brush_cursor(); + bool brush_ground(glm::dvec3 &Point); + double brush_height(glm::dvec2 const &Point, double Around); + TAnimModel *place_brush_model(glm::dvec3 const &At); 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 @@ -238,8 +248,11 @@ class editor_mode : public application_mode, private editor_track::observer, pri std::vector> m_retired; // models replaced after changes of includes, with the frames left until they're destroyed bool m_takesnapshot{true}; // helper, hints whether snapshot of selected node(s) should be taken before modification bool m_dragging = false; - glm::dvec3 oldPos; // world position of the last brush placement - bool m_brush_has_last{false}; // false at the start of a brush stroke, so the first placement ignores spacing + glm::dvec3 oldPos; // world position of the last dab of the brush + 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::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}; float kMaxPlacementDistance = 200.0f; diff --git a/application/editormodebrush.cpp b/application/editormodebrush.cpp new file mode 100644 index 000000000..30c6066bf --- /dev/null +++ b/application/editormodebrush.cpp @@ -0,0 +1,198 @@ +/* +This Source Code Form is subject to the +terms of the Mozilla Public License, v. +2.0. If a copy of the MPL was not +distributed with this file, You can +obtain one at +http://mozilla.org/MPL/2.0/. +*/ + +// the brush of models of the Surroundings: one model where clicked, or many in a circle along the path of the cursor + +#include "stdafx.h" + +#include "application/editormode.h" +#include "application/editorprojection.h" +#include "application/editoruilayer.h" +#include "editor/editorFormat.hpp" +#include "editor/editorIncludeInfo.hpp" +#include "model/AnimModel.h" +#include "simulation/simulation.h" +#include "utilities/translation.h" + +#include +#include + +namespace +{ + +ImU32 constexpr kCrossColour{IM_COL32(255, 220, 70, 255)}; +ImU32 constexpr kCircleColour{IM_COL32(255, 220, 70, 170)}; +ImU32 constexpr kShadowColour{IM_COL32(0, 0, 0, 140)}; +double constexpr kPi{3.14159265358979323846}; + +// the last press of the left button landed in the viewport, not on a window (see editormode.cpp) +bool viewport_click() +{ + return false == ImGui::GetIO().MouseDownOwned[0]; +} + +} // namespace + +bool editor_mode::brush_ground(glm::dvec3 &Point) +{ + if (false == cursor_on_ground(Point)) + return false; + // no further than things are placed at all + auto const offset{Point - glm::dvec3{Camera.Pos}}; + Point = placement_on_ground(glm::dvec3{Camera.Pos} + clamp_mouse_offset_to_max(offset)); + return std::isfinite(Point.x) && std::isfinite(Point.y) && std::isfinite(Point.z); +} + +double editor_mode::brush_height(glm::dvec2 const &Point, double const Around) +{ + double height{Around}; + if (m_streamer.active() && m_streamer.height_at(Point.x, Point.y, height)) + return height; + return placement_on_ground({Point.x, Around, Point.y}).y; +} + +TAnimModel *editor_mode::place_brush_model(glm::dvec3 const &At) +{ + auto const *source{ui()->get_active_node_template()}; + // the scenery templates are put one at a time with the insert mode + if (source == nullptr || source->starts_with(editor_includes::directive_mark)) + return nullptr; + // NOTE: no name, as with thousands of nodes the names take their share of the time the scenery needs to load. a node which + // needs one, to be referred to by an event, gets it in the node properties + auto *model{simulation::State.create_model(*source, std::string{}, At)}; + if (model == nullptr) + 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); + add_to_hierarchy(model); + return model; +} + +void editor_mode::brush_press() +{ + mouseHold = true; + m_brush_has_last = false; + m_brush_taken.clear(); + auto const single{ui()->brush().way == brush_object_list::kind::single}; + if (false == single) + { + // one step of the history for the whole stroke, the models added to it as they're put + trim_history(); + EditorSnapshot stroke; + stroke.action = EditorSnapshot::Action::Array; + m_history.push_back(std::move(stroke)); + g_redo.clear(); + m_brush_stroke = true; + return; + } + // the point is read once the renderer has the depth under the cursor of the press + GfxRenderer->Pick_Node_Callback([this](scene::basic_node * /*node*/) { + if (false == viewport_click()) + return; + glm::dvec3 at; + if (false == brush_ground(at)) + return; + auto *model{place_brush_model(at)}; + if (model == nullptr) + return; + std::string text; + model->export_as_text(text); + push_snapshot(model, EditorSnapshot::Action::Add, text); + m_node = model; + ui()->set_node(m_node); + }); +} + +void editor_mode::handle_brush_mouse_hold(int /*Action*/, int /*Button*/) +{ + if (ui()->mode() != nodebank_panel::BRUSH || false == m_brush_stroke) + return; + GfxRenderer->Pick_Node_Callback([this](scene::basic_node * /*node*/) { + // picks are resolved a few frames later; drop the ones queued before the button went up, or for a press that turned out + // to belong to the UI + if (false == mouseHold || false == m_brush_stroke || false == viewport_click() || m_history.empty()) + return; + glm::dvec3 centre; + if (false == brush_ground(centre)) + return; + auto const &settings{ui()->brush().dab}; + // the next dab once the cursor went far enough, measured on the ground, whatever the camera does meanwhile + if (m_brush_has_last && glm::distance(glm::dvec2{centre.x, centre.z}, glm::dvec2{oldPos.x, oldPos.z}) < scatter::dab_step(settings)) + return; + 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 *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 + m_brush_taken.push_back(point); + if (model == nullptr) + continue; + EditorSnapshot::array_copy copy; + copy.model = model; + copy.uuid = model->uuid; + stroke.layer = model->layer(); + stroke.copies.push_back(std::move(copy)); + } + }); +} + +void editor_mode::end_brush_stroke() +{ + if (false == m_brush_stroke) + return; + m_brush_stroke = false; + if (m_history.empty() || m_history.back().action != EditorSnapshot::Action::Array) + return; + auto const count{m_history.back().copies.size()}; + if (count == 0) + { + // nothing put: no empty step in the history + m_history.pop_back(); + return; + } + ui()->set_status(format(STR_C("Brush: %zu models put; Ctrl+Z takes the whole stroke back"), count)); +} + +void editor_mode::draw_brush_cursor() +{ + glm::dvec3 centre; + if (false == brush_ground(centre)) + return; + screen_projection const view; + auto *drawlist{ImGui::GetBackgroundDrawList(ImGui::GetMainViewport())}; + auto const &settings{ui()->brush()}; + auto const many{settings.way == brush_object_list::kind::multi}; + // 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 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))}; + for (auto const &[a, b] : arms) + { + view.line(drawlist, a, b, kShadowColour, 4.0f); + view.line(drawlist, a, b, kCrossColour, 2.0f); + } + if (false == many) + return; + // the circle, laid on the ground + int constexpr pieces{64}; + auto const radius{std::max(0.1, settings.dab.radius)}; + auto previous{ground(centre.x + radius, centre.z)}; + for (int i = 1; i <= pieces; ++i) + { + auto const angle{2.0 * kPi * i / pieces}; + auto const next{ground(centre.x + radius * std::cos(angle), centre.z + radius * std::sin(angle))}; + view.line(drawlist, previous, next, kShadowColour, 3.5f); + view.line(drawlist, previous, next, kCircleColour, 1.8f); + previous = next; + } +} diff --git a/application/editoruilayer.cpp b/application/editoruilayer.cpp index 17139d907..dea6fc180 100644 --- a/application/editoruilayer.cpp +++ b/application/editoruilayer.cpp @@ -711,9 +711,8 @@ void editor_ui::render_mode_options(nodebank_panel::edit_mode const Mode) break; } case nodebank_panel::BRUSH: - ImGui::TextDisabled(STR_C("Hold LMB: paint models along the cursor path")); + // the brush turns the models itself (Turn), the rotation settings of the insert mode don't apply to it m_brushobjects.render_options(m_nodebankpanel); - m_functionspanel.render_controls(); break; case nodebank_panel::FILL: if (m_filloptions) @@ -1153,9 +1152,9 @@ void editor_ui::set_mode(nodebank_panel::edit_mode const Mode) m_nodebankpanel.mode = Mode; m_nodebankpanel.is_open = true; } -float editor_ui::getSpacing() +brush_object_list const &editor_ui::brush() const { - return m_brushobjects.spacing; + return m_brushobjects; } functions_panel::rotation_mode editor_ui::rot_mode() diff --git a/application/editoruilayer.h b/application/editoruilayer.h index 759621aee..6e0a60527 100644 --- a/application/editoruilayer.h +++ b/application/editoruilayer.h @@ -45,7 +45,8 @@ class editor_ui : public ui_layer const std::string *get_active_node_template(bool bypassRandom = false); nodebank_panel::edit_mode mode(); void set_mode(nodebank_panel::edit_mode const Mode); - float getSpacing(); + // the settings of the brush of models + brush_object_list const &brush() const; // node bank and model set access (set selection widgets, set contents) nodebank_panel &nodebank() { diff --git a/application/editoruipanels.cpp b/application/editoruipanels.cpp index da3b56741..fa3e52083 100644 --- a/application/editoruipanels.cpp +++ b/application/editoruipanels.cpp @@ -26,6 +26,7 @@ http://mozilla.org/MPL/2.0/. #include "editor/editorSettings.hpp" #include "imgui/imgui_internal.h" +#include #include #include @@ -589,7 +590,42 @@ std::string *brush_object_list::GetRandomObject() void brush_object_list::render_options(nodebank_panel &Bank) { - ImGui::SliderFloat(STR_C("Spacing"), &spacing, 0.1f, 20.0f, "%.1f m"); + // one model at a click, or many in the circle while the button is held + auto many{way == kind::multi}; + if (ImGui::RadioButton(STR_C("Single model##brush"), false == many)) + way = kind::single; + ImGui::SameLine(); + if (ImGui::RadioButton(STR_C("Many models##brush"), many)) + way = kind::multi; + if (way == kind::single) + ImGui::TextDisabled("%s", STR_C("LMB: one model at the cross on the ground")); + else + { + ImGui::TextDisabled("%s", STR_C("Hold LMB: models in the circle along the cursor's path; one Ctrl+Z takes the whole stroke back")); + auto radius{static_cast(dab.radius)}; + if (ImGui::SliderFloat(STR_C("Radius##brush"), &radius, 0.5f, 100.0f, "%.1f m", ImGuiSliderFlags_Logarithmic)) + dab.radius = radius; + int fillmode{static_cast(dab.mode)}; + std::array const fills{STR_C("Random, anywhere in the circle"), STR_C("Uniform (grid)"), STR_C("Feathered to the edge")}; + ImGui::SetNextItemWidth(220.0f); + if (ImGui::Combo(STR_C("Fill##brush"), &fillmode, fills.data(), static_cast(fills.size()))) + dab.mode = static_cast(fillmode); + auto spacing{static_cast(dab.spacing)}; + if (ImGui::SliderFloat(STR_C("Spacing##brush"), &spacing, 0.1f, 50.0f, "%.1f m", ImGuiSliderFlags_Logarithmic)) + dab.spacing = spacing; + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("%s", dab.mode == scatter::fill::uniform ? STR_C("The step of the grid the models stand on") : STR_C("The least distance between two models")); + if (dab.mode != scatter::fill::uniform) + { + auto density{static_cast(dab.density)}; + if (ImGui::SliderFloat(STR_C("Density##brush"), &density, 0.1f, 200.0f, STR_C("%.1f per 100 m²"), ImGuiSliderFlags_Logarithmic)) + dab.density = density; + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("%s", STR_C("How many models a 100 m² the circle gets, as many as the spacing lets in")); + } + } + render_look(); + ImGui::Separator(); ImGui::Checkbox(STR_C("Random model from set"), &useRandom); if (false == useRandom) { @@ -608,6 +644,44 @@ void brush_object_list::render_options(nodebank_panel &Bank) } } +void brush_object_list::render_look() +{ + ImGui::SeparatorText(STR_C("Turn##brush")); + auto turn_random{look.turn == scatter::vary::random}; + if (ImGui::RadioButton(STR_C("Fixed##brushturn"), false == turn_random)) + look.turn = scatter::vary::fixed; + ImGui::SameLine(); + if (ImGui::RadioButton(STR_C("Random##brushturn"), turn_random)) + look.turn = scatter::vary::random; + if (look.turn == scatter::vary::fixed) + ImGui::DragFloat3(STR_C("Angles x y z##brush"), &look.angles.x, 0.5f, -180.0f, 180.0f, "%.1f°"); + else + { + ImGui::DragFloat3(STR_C("Each way x y z##brushturn"), &look.angle_range.x, 0.5f, 0.0f, 180.0f, "±%.1f°"); + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("%s", STR_C("Each model turned by a random angle up to this much each way: x and z tilt it, y turns it round (180: any way)")); + } + ImGui::SeparatorText(STR_C("Scale##brush")); + auto size_random{look.size == scatter::vary::random}; + if (ImGui::RadioButton(STR_C("Fixed##brushscale"), false == size_random)) + look.size = scatter::vary::fixed; + ImGui::SameLine(); + if (ImGui::RadioButton(STR_C("Random##brushscale"), size_random)) + look.size = scatter::vary::random; + if (look.size == scatter::vary::fixed) + ImGui::DragFloat3(STR_C("Percent x y z##brush"), &look.scale.x, 0.5f, 1.0f, 1000.0f, "%.0f%%"); + else + { + ImGui::Checkbox(STR_C("The same on every axis##brush"), &look.same_scale); + if (look.same_scale) + ImGui::DragFloat(STR_C("Each way##brushscale"), &look.scale_range.x, 0.5f, 0.0f, 99.0f, "±%.0f%%"); + else + ImGui::DragFloat3(STR_C("Each way x y z##brushscale"), &look.scale_range.x, 0.5f, 0.0f, 99.0f, "±%.0f%%"); + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("%s", STR_C("Each model scaled by a random share up to this much each way from its own size")); + } +} + nodebank_panel::nodebank_panel(std::string const &Name, bool const Isopen) : ui_panel(Name, Isopen) { size_min = {100, 50}; diff --git a/application/editoruipanels.h b/application/editoruipanels.h index 1b39e9a2b..b9d8d2c12 100644 --- a/application/editoruipanels.h +++ b/application/editoruipanels.h @@ -13,6 +13,7 @@ http://mozilla.org/MPL/2.0/. #include "utilities/Classes.h" #include "editor/editorIncludeInfo.hpp" #include "editor/editorPreviews.hpp" +#include "editor/editorScatter.hpp" #include /* @@ -111,12 +112,24 @@ class brush_object_list public: void render_options(nodebank_panel &Bank); + // one model where clicked, or many in the circle of the brush while the button is held + enum class kind + { + single, + multi, + }; + // class use std::vector Objects; // manual list, used when source is set to manual std::string *GetRandomObject(); bool useRandom = {false}; model_set_ref source; - float spacing{1.0f}; + kind way{kind::multi}; + scatter::dab_settings dab; // the circle of the many and how they fill it + scatter::look_settings look; // the turn and the scale of each model + + private: + void render_look(); }; class nodebank_panel : public ui_panel diff --git a/editor/editorScatter.cpp b/editor/editorScatter.cpp new file mode 100644 index 000000000..c2cdf1807 --- /dev/null +++ b/editor/editorScatter.cpp @@ -0,0 +1,120 @@ +#include "editor/editorScatter.hpp" + +#include +#include +#include + +namespace scatter +{ + +namespace +{ + +double constexpr kPi{3.14159265358979323846}; + +bool clear_of(glm::dvec2 const &Point, double const Spacing, std::vector const &Taken, std::vector const &Made) +{ + auto const least{Spacing * Spacing}; + auto const far_enough = [&](glm::dvec2 const &Other) { + auto const d{Point - Other}; + return glm::dot(d, d) >= least; + }; + return std::all_of(Taken.begin(), Taken.end(), far_enough) && std::all_of(Made.begin(), Made.end(), 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 +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) + { + 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) + { + 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); + } + } + return made; +} + +} // namespace + +std::vector dab(glm::dvec2 const &Centre, dab_settings const &Settings, std::vector const &Taken, std::mt19937 &Random) +{ + 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}; + std::copy_if(Taken.begin(), Taken.end(), 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 + int constexpr most{2000}; + auto const wanted{static_cast(std::min(most, std::lround(std::max(0.0, Settings.density) * kPi * radius * radius / 100.0)))}; + std::uniform_real_distribution unit{0.0, 1.0}; + std::vector made; + int constexpr tries_each{30}; + for (int attempt = 0; attempt < wanted * tries_each && static_cast(made.size()) < wanted; ++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 * kPi * 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); + } + return made; +} + +double dab_step(dab_settings const &Settings) +{ + return std::max({0.05, Settings.spacing, 0.5 * Settings.radius}); +} + +look pick(look_settings const &Settings, std::mt19937 &Random) +{ + 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 diff --git a/editor/editorScatter.hpp b/editor/editorScatter.hpp new file mode 100644 index 000000000..d095e1739 --- /dev/null +++ b/editor/editorScatter.hpp @@ -0,0 +1,63 @@ +#pragma once + +// 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 + +namespace scatter +{ + +// how the models fill the circle of the brush +enum class fill +{ + random, // anywhere in the circle, no closer to each other than the spacing + uniform, // on a grid of triangles as wide as the spacing, fixed to the world: strokes over one another don't double it + feathered, // anywhere, fewer and fewer towards the edge of the circle +}; + +struct dab_settings +{ + double radius{5.0}; // m + fill mode{fill::random}; + double spacing{2.0}; // m: the least distance between two models; the step of the grid of the uniform fill + double density{4.0}; // models a 100 m² for the random and the feathered fills, as much as the spacing lets +}; + +// 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); + +// the distance the cursor goes before the next dab: the circles of two dabs overlap by half +double dab_step(dab_settings const &Settings); + +// a setting the same for every model, or drawn for each from a range +enum class vary +{ + fixed, + random, +}; + +struct look_settings +{ + vary turn{vary::random}; + glm::vec3 angles{0.0f}; // degrees about x, y, z, for the fixed turn + glm::vec3 angle_range{0.0f, 180.0f, 0.0f}; // degrees each way about x, y, z, for the random turn + vary size{vary::fixed}; + glm::vec3 scale{100.0f}; // percent along x, y, z, for the fixed scale + glm::vec3 scale_range{0.0f}; // percent each way along x, y, z, for the random scale + bool same_scale{true}; // the random scale one for all the axes (the x range), the model keeps its shape +}; + +struct look +{ + glm::vec3 angles{0.0f}; // degrees about x, y, z + glm::vec3 scale{1.0f}; // factors along x, y, z, never under 1% +}; + +// the turn and the scale of one model +look pick(look_settings const &Settings, std::mt19937 &Random); + +} // namespace scatter diff --git a/lang/editor_pl.po b/lang/editor_pl.po index 668792f28..f63ebc645 100644 --- a/lang/editor_pl.po +++ b/lang/editor_pl.po @@ -14240,3 +14240,93 @@ msgstr "Kursor rysowany w obrazie (stream)" msgid "The pointer drawn by the editor into the frame instead of the one of the system: seen by those watching a stream\nor a recording of the window (Discord, OBS, the capture of the system), which leave the system pointer out" msgstr "Wskaźnik rysowany przez edytor w klatce zamiast systemowego: widać go w streamie\ni w nagraniu okna (Discord, OBS, przechwytywanie systemowe), które pomijają wskaźnik systemu" + +msgid "Single model##brush" +msgstr "Pojedynczy model##brush" + +msgid "Many models##brush" +msgstr "Wiele modeli##brush" + +msgid "LMB: one model at the cross on the ground" +msgstr "LPM: jeden model w miejscu krzyżyka na gruncie" + +msgid "Hold LMB: models in the circle along the cursor's path; one Ctrl+Z takes the whole stroke back" +msgstr "Przytrzymaj LPM: modele w okręgu wzdłuż drogi kursora; jedno Ctrl+Z cofa całe pociągnięcie" + +msgid "Radius##brush" +msgstr "Promień##brush" + +msgid "Random, anywhere in the circle" +msgstr "Losowe" + +msgid "Uniform (grid)" +msgstr "Równomierne (siatka)" + +msgid "Feathered to the edge" +msgstr "Rozmyte do krawędzi" + +msgid "Fill##brush" +msgstr "Wypełnienie##brush" + +msgid "Spacing##brush" +msgstr "Odstęp##brush" + +msgid "The step of the grid the models stand on" +msgstr "Krok siatki, na której stoją modele" + +msgid "The least distance between two models" +msgstr "Najmniejsza odległość między dwoma modelami" + +msgid "Density##brush" +msgstr "Gęstość##brush" + +msgid "%.1f per 100 m²" +msgstr "%.1f na 100 m²" + +msgid "How many models a 100 m² the circle gets, as many as the spacing lets in" +msgstr "Ile modeli na 100 m² dostaje okrąg, o ile pozwala na to odstęp" + +msgid "Turn##brush" +msgstr "Obrót##brush" + +msgid "Fixed##brushturn" +msgstr "Stały##brushturn" + +msgid "Random##brushturn" +msgstr "Losowy##brushturn" + +msgid "Angles x y z##brush" +msgstr "Kąty x y z##brush" + +msgid "Each way x y z##brushturn" +msgstr "W obie strony x y z##brushturn" + +msgid "Each model turned by a random angle up to this much each way: x and z tilt it, y turns it round (180: any way)" +msgstr "Każdy model obrócony o losowy kąt do tylu stopni w obie strony: x i z go przechylają, y obraca wokół pionu (180: dowolnie)" + +msgid "Scale##brush" +msgstr "Skala##brush" + +msgid "Fixed##brushscale" +msgstr "Stała##brushscale" + +msgid "Random##brushscale" +msgstr "Losowa##brushscale" + +msgid "Percent x y z##brush" +msgstr "Procent x y z##brush" + +msgid "The same on every axis##brush" +msgstr "Jednakowa na wszystkich osiach##brush" + +msgid "Each way##brushscale" +msgstr "W obie strony##brushscale" + +msgid "Each way x y z##brushscale" +msgstr "W obie strony x y z##brushscale" + +msgid "Each model scaled by a random share up to this much each way from its own size" +msgstr "Każdy model przeskalowany losowo o tyle procent w obie strony od własnego rozmiaru" + +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" diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index c1ad98f35..62a122fe4 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -32,6 +32,7 @@ add_executable(${UNIT_TESTS_NAME} # and the fouling points of the switches, widened in the curves as the gauge is "${CMAKE_SOURCE_DIR}/editor/editorGauge.cpp" "${CMAKE_SOURCE_DIR}/editor/editorFouling.cpp" + "${CMAKE_SOURCE_DIR}/editor/editorScatter.cpp" ${UNIT_TEST_CASES}) # the core library brings the game's build settings along diff --git a/tests/unit/test_scatter.cpp b/tests/unit/test_scatter.cpp new file mode 100644 index 000000000..d7afe4a91 --- /dev/null +++ b/tests/unit/test_scatter.cpp @@ -0,0 +1,134 @@ +#include "stdafx.h" + +#include "doctest.h" + +#include "editor/editorScatter.hpp" + +#include +#include + +namespace +{ + +double closest_pair(std::vector 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; + auto const points{scatter::dab({100.0, -50.0}, settings, {}, random)}; + // 5 a 100 m² over 314 m²: 16, the spacing lets them all in + CHECK(points.size() == 16); + 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; + auto const first{scatter::dab({0.0, 0.0}, settings, {}, random)}; + auto const second{scatter::dab({2.0, 0.0}, settings, first, 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; + auto const first{scatter::dab({0.3, 0.2}, settings, {}, random)}; + CHECK(first.size() > 15); + CHECK(closest_pair(first) == doctest::Approx(2.0)); + auto taken{first}; + auto const second{scatter::dab({2.3, 0.2}, settings, taken, random)}; + for (auto const &p : second) + for (auto const &q : first) + CHECK(glm::distance(p, q) > 1.9); + // the same place again: nothing new + taken.insert(taken.end(), second.begin(), second.end()); + CHECK(scatter::dab({0.3, 0.2}, settings, taken, 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; + auto const points{scatter::dab({0.0, 0.0}, settings, {}, random)}; + int inner{0}, 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: 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); + } +}