From 1a6d2bbc2fb848b96996549a4f999dcb2aee2113 Mon Sep 17 00:00:00 2001 From: miloszunio Date: Tue, 1 Dec 2026 20:29:37 +0100 Subject: [PATCH] fix(editor): the brush keeps its models when the step of the stroke is gone from the history (Sonar) A stroke held long enough for its step to be trimmed from the history took the step as null and the next model crashed the editor (Sonar S2259 in editormodebrush.cpp). The rest of the stroke now goes to a step of its own, which Ctrl+Z takes back. The other notes of the analysis, with no change of behaviour: the fill loops test their counter only and stop on the limit inside (S886), brush_on_line() is const, the step is found with std::to_address, the line shape of the brush is shape::along_line (it shadowed the line settings, S1117), and the test of the picks without finite settings is a helper (S134), with std::numbers::pi in the density test. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DDcMn1p6qPUo2Cgx4KaVYU --- application/editormode.h | 2 +- application/editormodebrush.cpp | 16 +++++++++++----- application/editoruipanels.cpp | 6 +++--- application/editoruipanels.h | 2 +- editor/editorScatter.cpp | 18 ++++++++++++++---- tests/unit/test_scatter.cpp | 28 +++++++++++++++++----------- 6 files changed, 47 insertions(+), 25 deletions(-) diff --git a/application/editormode.h b/application/editormode.h index 4221d8701..1a4e59186 100644 --- a/application/editormode.h +++ b/application/editormode.h @@ -234,7 +234,7 @@ class editor_mode : public application_mode, private editor_track::observer, pri std::vector brush_standing(glm::dvec2 const &Centre, double Reach) const; // 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 // the brush is the one of many models along a line: the keys of the line (Enter, Backspace, Esc) are its - bool brush_on_line(); + bool brush_on_line() const; void brush_line_point(); void brush_line_finish(); void draw_brush_line(glm::dvec3 const *Cursor); diff --git a/application/editormodebrush.cpp b/application/editormodebrush.cpp index 49f3730f0..27fd9ef0d 100644 --- a/application/editormodebrush.cpp +++ b/application/editormodebrush.cpp @@ -21,6 +21,7 @@ http://mozilla.org/MPL/2.0/. #include "utilities/translation.h" #include +#include #include #include #include @@ -82,7 +83,7 @@ void editor_mode::brush_press() m_brush_has_last = false; m_brush_done = {}; auto const &settings{ui()->brush()}; - if (settings.way != brush_object_list::kind::single && settings.stroke == brush_object_list::shape::line) + if (settings.way != brush_object_list::kind::single && settings.stroke == brush_object_list::shape::along_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(); }); @@ -160,6 +161,8 @@ void editor_mode::brush_dab() // the step of the stroke is looked up by its serial for each model, never taken for the last one of the history: a // step another key pushed while the button is held (Del) doesn't get the models of the stroke auto *stroke{brush_step(true)}; + if (stroke == nullptr) + continue; EditorSnapshot::array_copy copy; copy.model = model; copy.uuid = model->uuid; @@ -174,7 +177,10 @@ editor_mode::EditorSnapshot *editor_mode::brush_step(bool const Make) { // from the end: the step of the stroke is the last one, or among the last auto const found{std::ranges::find_if(m_history.rbegin(), m_history.rend(), [this](EditorSnapshot const &Step) { return Step.serial == m_brush_step; })}; - return found != m_history.rend() ? &*found : nullptr; + if (found != m_history.rend()) + return std::to_address(found); + // the step is gone from the history (trimmed while the button is held): the rest of the stroke goes to a step of its own + m_brush_step = 0; } if (false == Make) return nullptr; @@ -214,10 +220,10 @@ void editor_mode::end_brush_stroke() ui()->set_status(format(STR_C("Brush: %zu models put; Ctrl+Z takes the whole stroke back"), step->copies.size())); } -bool editor_mode::brush_on_line() +bool editor_mode::brush_on_line() const { auto const &settings{ui()->brush()}; - return ui()->mode() == nodebank_panel::BRUSH && settings.way == brush_object_list::kind::multi && settings.stroke == brush_object_list::shape::line; + return ui()->mode() == nodebank_panel::BRUSH && settings.way == brush_object_list::kind::multi && settings.stroke == brush_object_list::shape::along_line; } void editor_mode::brush_line_point() @@ -313,7 +319,7 @@ 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}; + auto const on_line{many && settings.stroke == brush_object_list::shape::along_line}; glm::dvec3 centre; auto const on_ground{brush_ground(centre)}; if (on_line) diff --git a/application/editoruipanels.cpp b/application/editoruipanels.cpp index e9517f4ae..8e34d5126 100644 --- a/application/editoruipanels.cpp +++ b/application/editoruipanels.cpp @@ -625,13 +625,13 @@ void brush_object_list::render_options(nodebank_panel &Bank) void brush_object_list::render_dab() { - auto on_line{stroke == shape::line}; + auto on_line{stroke == shape::along_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) + stroke = shape::along_line; + if (stroke == shape::along_line) { render_line(); return; diff --git a/application/editoruipanels.h b/application/editoruipanels.h index 97767c29c..a07d512ea 100644 --- a/application/editoruipanels.h +++ b/application/editoruipanels.h @@ -128,7 +128,7 @@ class brush_object_list enum class shape { circle, - line, + along_line, }; kind way{kind::multi}; diff --git a/editor/editorScatter.cpp b/editor/editorScatter.cpp index daf7b6565..d05149f7f 100644 --- a/editor/editorScatter.cpp +++ b/editor/editorScatter.cpp @@ -80,8 +80,10 @@ void grid_row(long long const Row, glm::dvec2 const &Centre, double const Radius 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) + 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 @@ -98,8 +100,12 @@ std::vector grid_points(glm::dvec2 const &Centre, double const Radiu 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 && static_cast(made.size()) < dab_most; ++r) + 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; } @@ -228,8 +234,10 @@ struct regular_line 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 && static_cast(Made.size()) < line_most; ++step) + 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}; @@ -249,8 +257,10 @@ struct regular_line 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) + for (long long r = 1; r <= rows; ++r) { + if (static_cast(made.size()) >= line_most) + break; row(r, taken, made); row(-r, taken, made); } diff --git a/tests/unit/test_scatter.cpp b/tests/unit/test_scatter.cpp index 370e99ca8..016d3d722 100644 --- a/tests/unit/test_scatter.cpp +++ b/tests/unit/test_scatter.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -14,6 +15,20 @@ namespace { +// both ways of the scale: the turn and the scale picked are finite, the scale not below its floor +void check_picks_finite(scatter::look_settings Look, std::mt19937 &Random) +{ + for (auto const same : {true, false}) + { + Look.same_scale = same; + auto const picked{scatter::pick(Look, Random)}; + CHECK(std::isfinite(picked.angles.x + picked.angles.y + picked.angles.z)); + CHECK(std::isfinite(picked.scale.x + picked.scale.y + picked.scale.z)); + CHECK(picked.scale.x >= 0.01f); + CHECK(picked.scale.y >= 0.01f); + CHECK(picked.scale.z >= 0.01f); + } +} double closest_pair(std::vector const &Points) { @@ -117,7 +132,7 @@ TEST_CASE("scatter: a stroke leaves the density asked for on the ground it went for (int i = 0; i * scatter::dab_step(settings) <= 400.0; ++i) count += scatter::dab({i * scatter::dab_step(settings), 0.0}, settings, stroke, random).size(); // the ground gone over: 400 m by 20 m and the two half circles at the ends, 8314 m², so 416 models - auto const asked{settings.density * (400.0 * 20.0 + 3.14159265 * 100.0) / 100.0}; + auto const asked{settings.density * (400.0 * 20.0 + std::numbers::pi * 100.0) / 100.0}; CHECK(count > 0.85 * asked); CHECK(count < 1.15 * asked); // as dense at the end of the stroke as at its start: the 100 m in the middle of each half hold a quarter of the models @@ -472,16 +487,7 @@ TEST_CASE("scatter: settings which aren't finite numbers give places and transfo look.angle_range = glm::vec3{bad}; look.scale = glm::vec3{bad}; look.scale_range = glm::vec3{bad}; - for (auto const same : {true, false}) - { - look.same_scale = same; - auto const picked{scatter::pick(look, random)}; - CHECK(std::isfinite(picked.angles.x + picked.angles.y + picked.angles.z)); - CHECK(std::isfinite(picked.scale.x + picked.scale.y + picked.scale.z)); - CHECK(picked.scale.x >= 0.01f); - CHECK(picked.scale.y >= 0.01f); - CHECK(picked.scale.z >= 0.01f); - } + check_picks_finite(look, random); } } }