mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-03 06:35:21 +01:00
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DDcMn1p6qPUo2Cgx4KaVYU
This commit is contained in:
@@ -234,7 +234,7 @@ class editor_mode : public application_mode, private editor_track::observer, pri
|
||||
std::vector<glm::dvec2> 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);
|
||||
|
||||
@@ -21,6 +21,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include "utilities/translation.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <numbers>
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -128,7 +128,7 @@ class brush_object_list
|
||||
enum class shape
|
||||
{
|
||||
circle,
|
||||
line,
|
||||
along_line,
|
||||
};
|
||||
|
||||
kind way{kind::multi};
|
||||
|
||||
@@ -80,8 +80,10 @@ void grid_row(long long const Row, glm::dvec2 const &Centre, double const Radius
|
||||
auto const z{static_cast<double>(Row) * row};
|
||||
auto const first{static_cast<long long>(std::floor((Centre.x - Radius - shift) / Spacing))};
|
||||
auto const last{static_cast<long long>(std::ceil((Centre.x + Radius - shift) / Spacing))};
|
||||
for (auto c = first; c <= last && static_cast<int>(Made.size()) < dab_most; ++c)
|
||||
for (auto c = first; c <= last; ++c)
|
||||
{
|
||||
if (static_cast<int>(Made.size()) >= dab_most)
|
||||
break;
|
||||
glm::dvec2 const point{static_cast<double>(c) * Spacing + shift, z};
|
||||
// the points of one dab are Spacing apart by the grid itself; only a point an earlier dab put is there already, the same
|
||||
// point again, a hair away at most
|
||||
@@ -98,8 +100,12 @@ std::vector<glm::dvec2> 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<long long>(std::floor((Centre.y - Radius) / row))};
|
||||
auto const last_row{static_cast<long long>(std::ceil((Centre.y + Radius) / row))};
|
||||
for (auto r = first_row; r <= last_row && static_cast<int>(made.size()) < dab_most; ++r)
|
||||
for (auto r = first_row; r <= last_row; ++r)
|
||||
{
|
||||
if (static_cast<int>(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<long long>(std::floor((line.length() + 1e-9 - shift) / spacing))};
|
||||
for (long long step = 0; step <= steps && static_cast<int>(Made.size()) < line_most; ++step)
|
||||
for (long long step = 0; step <= steps; ++step)
|
||||
{
|
||||
if (static_cast<int>(Made.size()) >= line_most)
|
||||
break;
|
||||
auto const at{shift + static_cast<double>(step) * spacing};
|
||||
auto const along_off{jitter > 0.0 ? jitter * each_way(random) : 0.0};
|
||||
auto const side_off{jitter > 0.0 ? jitter * each_way(random) : 0.0};
|
||||
@@ -249,8 +257,10 @@ struct regular_line
|
||||
auto const rows{static_cast<long long>(std::floor(std::max(0.0, settings.width) / spacing + 1e-9))};
|
||||
// the row on the line first, then the pairs of rows outwards: cut short by the limit, the band stays even
|
||||
row(0, taken, made);
|
||||
for (long long r = 1; r <= rows && static_cast<int>(made.size()) < line_most; ++r)
|
||||
for (long long r = 1; r <= rows; ++r)
|
||||
{
|
||||
if (static_cast<int>(made.size()) >= line_most)
|
||||
break;
|
||||
row(r, taken, made);
|
||||
row(-r, taken, made);
|
||||
}
|
||||
|
||||
+17
-11
@@ -7,6 +7,7 @@
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <numbers>
|
||||
#include <random>
|
||||
#include <set>
|
||||
#include <utility>
|
||||
@@ -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<glm::dvec2> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user