18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 06:35:21 +01:00

feat(editor): infrastructure bound to the paths follows their changes

This commit is contained in:
maj00r
2026-10-04 00:14:03 +02:00
parent 7371b18910
commit 8778353b22
15 changed files with 2083 additions and 1 deletions
+2
View File
@@ -173,11 +173,13 @@ set(SOURCES
"editor/editorTrack.cpp"
"editor/editorAlignment.cpp"
"editor/editorProfile.cpp"
"editor/editorInfra.cpp"
"editor/editorTerrainStreamer.cpp"
"editor/editorOrthophoto.cpp"
"application/editormode.cpp"
"application/editormodetrack.cpp"
"application/editormodeprofile.cpp"
"application/editormodeinfra.cpp"
"input/editormouseinput.cpp"
"application/editoruilayer.cpp"
"application/editoruipanels.cpp"
+7
View File
@@ -344,6 +344,7 @@ editor_mode::editor_mode() {
m_orthophoto.ground_source([](glm::dvec2 const &Min, glm::dvec2 const &Max, std::vector<world_triangle> &Out) {
gather_ground_triangles(Min, Max, true, 50.0f, Out);
});
infra_hooks();
}
editor_ui *editor_mode::ui() const
@@ -869,6 +870,9 @@ bool editor_mode::update()
simulation::State.update_clocks();
simulation::Environment.update();
// bindings are matched with the objects by their locations, before anything gets moved
if (false == m_bindings_loaded && simulation::is_ready)
infra_load();
auto const deltarealtime = Timer::GetDeltaRenderTime();
@@ -1023,6 +1027,8 @@ bool editor_mode::update()
render_track_window();
draw_profile_overlay();
render_profile_window();
draw_infra_overlay();
render_infra_window();
}
// --- area fill: outline overlay while the mode is active (its settings are drawn in the node bank window) ---
@@ -1637,6 +1643,7 @@ void editor_mode::save()
if (m_profile.changed)
profile_store();
infra_store();
auto const result = scene::Layers.save(rootstatements);
if (result.success && false == rootstatements.empty())
{
+53 -1
View File
@@ -22,6 +22,7 @@ http://mozilla.org/MPL/2.0/.
#include "editor/editorTrack.hpp"
#include "editor/editorAlignment.hpp"
#include "editor/editorProfile.hpp"
#include "editor/editorInfra.hpp"
#include <array>
#include <chrono>
@@ -107,7 +108,7 @@ class editor_mode : public application_mode
std::vector<std::pair<TTrack *, editor_track::state>> tracks;
std::vector<TTrack *> created;
std::vector<TTrack *> removed;
std::vector<infra::object_state> infra; // objects which followed the paths
};
void push_snapshot(scene::basic_node *node, EditorSnapshot::Action Action = EditorSnapshot::Action::Move, std::string const &Serialized = std::string());
@@ -545,6 +546,57 @@ class editor_mode : public application_mode
void render_profile_point();
void draw_profile_overlay() const;
// infrastructure along the track: objects bound to the paths follow their changes. bindings are kept in the
// scenery files as "//$b" comment lines
struct infra_candidate
{
infra::binding binding;
double offset{0.0}; // sideways from the axis
std::string reason;
bool chosen{false};
bool bound{false};
};
struct infra_window
{
bool open{false};
int scope{1}; // 0: the selected path, 1: the line through it, 2: the route of the vertical profile
float reach{1500.0f};
float corridor{8.0f};
bool follow{true};
bool show{true};
std::vector<infra::rule> rules{infra::default_rules()};
std::vector<TTrack *> tracks; // of the scope
std::vector<infra_candidate> candidates;
int hovered{-1};
std::string status;
std::string error;
};
infra_window m_infra;
std::vector<infra::binding> m_bindings;
std::vector<std::pair<scene::layer_handle, std::string>> m_bindings_unresolved; // lines kept as they are
bool m_bindings_loaded{false};
bool m_bindings_changed{false};
std::vector<infra::object_state> m_infra_buffer; // states of the objects before the change in progress
bool m_infra_suspended{false};
void infra_hooks();
void infra_load();
void infra_store();
std::vector<TTrack *> infra_scope() const;
void infra_recognize();
void infra_bind_chosen();
void infra_unbind(std::vector<std::size_t> Indices);
infra::binding *infra_find(infra::binding const &Binding);
infra::object_state infra_state_of(infra::binding const &Binding) const;
void infra_restore_state(infra::object_state const &State);
void infra_buffer(infra::binding const &Binding, bool const Refresh);
void infra_capture(TTrack const &Track);
void infra_follow(std::vector<TTrack *> const &Tracks);
void infra_split(TTrack &Original, TTrack &Created, segment_data const &First);
void infra_move(infra::binding &Binding);
void infra_attach(EditorSnapshot &Snapshot);
void render_infra_window();
void draw_infra_overlay() const;
bool m_route_gizmo_using{false};
glm::mat4 m_route_gizmo{1.0f};
float m_track_snap_radius{1.0f};
File diff suppressed because it is too large Load Diff
+20
View File
@@ -342,6 +342,7 @@ void editor_mode::push_track_snapshot(std::vector<std::pair<TTrack *, editor_tra
snap.uuid = track->uuid;
snap.tracks = std::move(States);
snap.created = std::move(Created);
infra_attach(snap);
m_history.push_back(std::move(snap));
g_redo.clear();
}
@@ -354,6 +355,7 @@ void editor_mode::restore_track_snapshot(EditorSnapshot const &Snapshot, std::ve
std::vector<TTrack *> tracks;
for (auto const &entry : Snapshot.tracks)
tracks.emplace_back(entry.first);
m_infra_suspended = true;
EditorSnapshot current{Snapshot};
current.tracks.clear();
auto involved{tracks};
@@ -361,6 +363,10 @@ void editor_mode::restore_track_snapshot(EditorSnapshot const &Snapshot, std::ve
involved.insert(involved.end(), Snapshot.created.begin(), Snapshot.created.end());
for (auto *track : involved)
current.tracks.emplace_back(track, editor_track::capture(*track));
current.infra.clear();
for (auto const &entry : Snapshot.infra)
if (auto const *binding{infra_find(entry.saved)})
current.infra.push_back(infra_state_of(*binding));
Opposite.push_back(std::move(current));
if (Undo)
@@ -383,6 +389,10 @@ void editor_mode::restore_track_snapshot(EditorSnapshot const &Snapshot, std::ve
for (auto const &entry : Snapshot.tracks)
editor_track::apply(*entry.first, entry.second);
editor_track::commit(tracks);
for (auto const &entry : Snapshot.infra)
infra_restore_state(entry);
m_infra_buffer.clear();
m_infra_suspended = false;
if (false == m_route.chain.tracks.empty())
{
std::string error;
@@ -420,6 +430,9 @@ void editor_mode::render_track_ui()
profile_open_run(*track);
m_profile.open = true;
}
ImGui::SameLine();
if (ImGui::Button("Infrastructure"))
m_infra.open = true;
if (ImGui::CollapsingHeader("Straights in the scenery"))
render_straights_ui();
render_switch_ui();
@@ -470,6 +483,13 @@ void editor_mode::render_track_window()
}
if (ImGui::SmallButton("Vertical profile along this line") && selected_track() != nullptr)
profile_open_run(*selected_track());
ImGui::SameLine();
if (ImGui::SmallButton("Infrastructure along this line"))
{
m_infra.open = true;
m_infra.scope = 1;
infra_recognize();
}
ImGui::End();
}
+304
View File
@@ -0,0 +1,304 @@
/*
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/.
*/
#include "stdafx.h"
#include "editor/editorInfra.hpp"
#include "world/Track.h"
#include <algorithm>
#include <cctype>
#include <cmath>
#include <sstream>
namespace infra
{
namespace
{
int const kSteps{128};
struct curve
{
glm::dvec3 p0, p1, p2, p3;
};
curve curve_of(segment_data const &Path)
{
auto const &start{Path.points[segment_data::point::start]};
auto const &end{Path.points[segment_data::point::end]};
auto const &control1{Path.points[segment_data::point::control1]};
auto const &control2{Path.points[segment_data::point::control2]};
if (control1 == glm::dvec3{} && control2 == glm::dvec3{})
return {start, start + (end - start) / 3.0, end + (start - end) / 3.0, end};
return {start, start + control1, end + control2, end};
}
glm::dvec3 point(curve const &Curve, double const T)
{
auto const u{1.0 - T};
return Curve.p0 * (u * u * u) + Curve.p1 * (3.0 * u * u * T) + Curve.p2 * (3.0 * u * T * T) + Curve.p3 * (T * T * T);
}
glm::dvec3 derivative(curve const &Curve, double const T)
{
auto const u{1.0 - T};
return (Curve.p1 - Curve.p0) * (3.0 * u * u) + (Curve.p2 - Curve.p1) * (6.0 * u * T) + (Curve.p3 - Curve.p2) * (3.0 * T * T);
}
double plan(glm::dvec3 const &A, glm::dvec3 const &B)
{
return std::hypot(A.x - B.x, A.z - B.z);
}
// arc length in the plan at the evenly spread parameters
std::array<double, kSteps + 1> lengths(curve const &Curve)
{
std::array<double, kSteps + 1> result{};
auto previous{Curve.p0};
for (int i = 1; i <= kSteps; ++i)
{
auto const next{point(Curve, static_cast<double>(i) / kSteps)};
result[i] = result[i - 1] + plan(previous, next);
previous = next;
}
return result;
}
double parameter(std::array<double, kSteps + 1> const &Lengths, double const S)
{
if (S <= 0.0)
return 0.0;
if (S >= Lengths.back())
return 1.0;
auto const next{std::upper_bound(Lengths.begin(), Lengths.end(), S)};
auto const index{static_cast<int>(std::distance(Lengths.begin(), next)) - 1};
auto const run{Lengths[index + 1] - Lengths[index]};
auto const fraction{run > 1e-12 ? (S - Lengths[index]) / run : 0.0};
return (index + fraction) / kSteps;
}
double length_at(std::array<double, kSteps + 1> const &Lengths, double const T)
{
auto const position{std::clamp(T, 0.0, 1.0) * kSteps};
auto const index{std::min(static_cast<int>(position), kSteps - 1)};
return Lengths[index] + (Lengths[index + 1] - Lengths[index]) * (position - index);
}
glm::dvec2 plan_direction(curve const &Curve, double const T)
{
auto tangent{derivative(Curve, T)};
glm::dvec2 result{tangent.x, tangent.z};
if (glm::length(result) < 1e-9)
result = {Curve.p3.x - Curve.p0.x, Curve.p3.z - Curve.p0.z};
return glm::length(result) > 1e-9 ? glm::normalize(result) : glm::dvec2{0.0, 1.0};
}
std::string lower(std::string Text)
{
std::transform(Text.begin(), Text.end(), Text.begin(), [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
return Text;
}
} // namespace
char const *name(kind const Kind)
{
switch (Kind)
{
case kind::model: return "model";
case kind::include: return "include";
case kind::traction: return "traction";
case kind::memcell: return "memcell";
case kind::launcher: return "eventlauncher";
}
return "";
}
char const *name(category const Category)
{
switch (Category)
{
case category::signal: return "Signals";
case category::sign: return "Signs";
case category::pole: return "Poles and gantries";
case category::catenary: return "Catenary";
case category::logic: return "Logic";
case category::other: return "Other";
}
return "";
}
std::vector<category> const &categories()
{
static std::vector<category> const result{category::signal, category::sign, category::pole, category::catenary, category::logic, category::other};
return result;
}
double plan_length(segment_data const &Path)
{
return lengths(curve_of(Path)).back();
}
std::vector<glm::dvec3> outline(segment_data const &Path, double const Step)
{
auto const shape{curve_of(Path)};
// the speed along the curve doesn't exceed three times the longest leg of the control polygon
auto const leg{std::max({plan(shape.p0, shape.p1), plan(shape.p1, shape.p2), plan(shape.p2, shape.p3)})};
auto const count{std::clamp(static_cast<int>(std::ceil(3.0 * leg / std::max(Step, 0.1))), 1, 4096)};
std::vector<glm::dvec3> result;
result.reserve(count + 1);
for (int i = 0; i <= count; ++i)
result.push_back(point(shape, static_cast<double>(i) / count));
return result;
}
frame frame_at(segment_data const &Path, double const S)
{
auto const shape{curve_of(Path)};
auto const t{parameter(lengths(shape), S)};
return {point(shape, t), plan_direction(shape, t)};
}
frame frame_at(station const &Station)
{
if (Station.track == nullptr || Station.path < 0 || Station.path >= static_cast<int>(Station.track->m_paths.size()))
return {};
return frame_at(Station.track->m_paths[Station.path], Station.s);
}
double project(segment_data const &Path, glm::dvec3 const &Point, double &S, bool &Interior)
{
auto const shape{curve_of(Path)};
auto const table{lengths(shape)};
auto const distance = [&](double const T) { return plan(point(shape, T), Point); };
auto best{0};
auto bestdistance{distance(0.0)};
for (int i = 1; i <= kSteps; ++i)
{
auto const d{distance(static_cast<double>(i) / kSteps)};
if (d < bestdistance)
{
bestdistance = d;
best = i;
}
}
// golden section search around the nearest sample
auto low{std::max(0, best - 1) / static_cast<double>(kSteps)};
auto high{std::min(kSteps, best + 1) / static_cast<double>(kSteps)};
double const ratio{0.6180339887498949};
auto a{high - ratio * (high - low)};
auto b{low + ratio * (high - low)};
auto da{distance(a)}, db{distance(b)};
for (int i = 0; i < 40; ++i)
{
if (da < db)
{
high = b;
b = a;
db = da;
a = high - ratio * (high - low);
da = distance(a);
}
else
{
low = a;
a = b;
da = db;
b = low + ratio * (high - low);
db = distance(b);
}
}
auto t{(low + high) * 0.5};
if (distance(0.0) <= distance(t))
t = 0.0;
if (distance(1.0) <= distance(t))
t = 1.0;
S = length_at(table, t);
Interior = (t > 1e-6 && t < 1.0 - 1e-6);
return distance(t);
}
double project(TTrack &Track, glm::dvec3 const &Point, station &Station, bool &Interior)
{
auto best{-1.0};
for (int i = 0; i < static_cast<int>(Track.m_paths.size()); ++i)
{
double s;
bool interior;
auto const d{project(Track.m_paths[i], Point, s, interior)};
// an interior point takes precedence over an end shared with the other path
if (best < 0.0 || d < best - 1e-6 || (std::abs(d - best) <= 1e-6 && interior && false == Interior))
{
best = d;
Station = {&Track, i, s};
Interior = interior;
}
}
return best;
}
double heading(glm::dvec2 const &Forward)
{
return glm::degrees(std::atan2(Forward.x, Forward.y));
}
anchor make_anchor(station const &At, glm::dvec3 const &Point)
{
anchor result;
result.at = At;
auto const axis{frame_at(At)};
glm::dvec2 const side{axis.forward.y, -axis.forward.x};
result.offset = glm::dot(glm::dvec2{Point.x - axis.point.x, Point.z - axis.point.z}, side);
result.height = Point.y - axis.point.y;
result.foot = axis.point;
if (At.track != nullptr && At.path >= 0 && At.path < static_cast<int>(At.track->m_paths.size()))
{
auto const &path{At.track->m_paths[At.path]};
result.start = path.points[segment_data::point::start];
result.end = path.points[segment_data::point::end];
result.length = plan_length(path);
}
return result;
}
glm::dvec3 place(anchor &Anchor)
{
auto const axis{frame_at(Anchor.at)};
Anchor.foot = axis.point;
glm::dvec2 const side{axis.forward.y, -axis.forward.x};
return {axis.point.x + side.x * Anchor.offset, axis.point.y + Anchor.height, axis.point.z + side.y * Anchor.offset};
}
std::vector<rule> default_rules()
{
return {
{category::signal, "semafor sem_ sem- tarcza tarcz_ tz_ tm_ tzw powtarz sygnaliz sygnal signal"},
{category::sign, "wskaz wsk_ wsk- w4 w8 w11 w24 w27 kilometr hektometr slupek_km znak tablic sign"},
{category::catenary, "trakcj siec_ siec- catenary przewod lina_nosna"},
{category::pole, "slup maszt wysieg bramk konstrukcj kotw odciag pole gantry"},
};
}
category classify(std::string const &Name, std::vector<rule> const &Rules)
{
auto const text{lower(Name)};
for (auto const &rule : Rules)
{
std::istringstream words{lower(rule.keywords)};
std::string word;
while (words >> word)
if (text.find(word) != std::string::npos)
return rule.group;
}
return category::other;
}
} // namespace infra
+128
View File
@@ -0,0 +1,128 @@
/*
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/.
*/
#pragma once
#include <array>
#include <string>
#include <vector>
#include <glm/glm.hpp>
#include "world/Segment.h"
#include "scene/scenenode.h"
class TTrack;
// objects which belong to the railway infrastructure (signals, signs, catenary, poles, the logic placed along the
// line) bound to the paths they stand by, so they follow the changes of the track geometry
namespace infra
{
enum class kind
{
model,
include,
traction,
memcell,
launcher
};
enum class category
{
signal,
sign,
pole,
catenary,
logic,
other
};
char const *name(kind Kind);
char const *name(category Category);
std::vector<category> const &categories();
// point of a path, measured in the plan from the start of the path
struct station
{
TTrack *track{nullptr};
int path{0};
double s{0.0};
};
struct frame
{
glm::dvec3 point{0.0};
glm::dvec2 forward{0.0, 1.0}; // in the plan, x and z
};
double plan_length(segment_data const &Path);
// points of the path, no farther apart in the plan than about specified step; the ends included
std::vector<glm::dvec3> outline(segment_data const &Path, double Step);
frame frame_at(segment_data const &Path, double S);
frame frame_at(station const &Station);
// nearest point of specified path in the plan. returns: distance in the plan; Interior tells the nearest point
// isn't one of the ends of the path
double project(segment_data const &Path, glm::dvec3 const &Point, double &S, bool &Interior);
// nearest point among the paths of the track
double project(TTrack &Track, glm::dvec3 const &Point, station &Station, bool &Interior);
// angle around the vertical axis which turns a model to face specified direction, in degrees
double heading(glm::dvec2 const &Forward);
// location of a point relative to the rail axis: the station, a sideways offset and a height above the rail top
struct anchor
{
station at;
double offset{0.0}; // positive to the right of the direction of the path
double height{0.0};
glm::dvec3 foot{0.0}; // point of the axis the location was measured from
// ends and length of the path when the station was measured, to tell whether the path changed since
glm::dvec3 start{0.0}, end{0.0};
double length{0.0};
};
anchor make_anchor(station const &At, glm::dvec3 const &Point);
// location for the current geometry of the path
glm::dvec3 place(anchor &Anchor);
// rules telling infrastructure apart by the names of the files and nodes
struct rule
{
category group{category::other};
std::string keywords; // separated by spaces, matched as parts of the lower case name
};
std::vector<rule> default_rules();
// first rule in the list which matches. returns: category::other if none does
category classify(std::string const &Name, std::vector<rule> const &Rules);
struct binding
{
kind type{kind::model};
category group{category::other};
scene::basic_node *node{nullptr}; // the object, unless it's an include
scene::instance_handle include{0};
std::vector<anchor> anchors; // one per point which defines the object: four for a traction piece
double yaw{0.0}; // rotation of the object relative to the direction of the path
bool turns{false}; // the object has a rotation which follows the path
std::string label;
bool lost{false}; // none of the paths around could take the object over
bool same(binding const &Other) const
{
return type == Other.type && node == Other.node && include == Other.include;
}
};
// placement of a bound object, to take a change back
struct object_state
{
binding saved;
glm::dvec3 location{0.0};
glm::vec3 angles{0.f};
std::array<glm::dvec3, 4> points{};
std::string directive;
bool moved{false}; // the object followed a change since the state was taken
};
} // namespace infra
+13
View File
@@ -124,8 +124,15 @@ bool adjoining_direction(TTrack const *Neighbour, glm::dvec3 const &Joint, bool
}
std::function<void(TTrack const &)> editor_track::on_capture;
std::function<void(std::vector<TTrack *> const &)> editor_track::on_commit;
std::function<void(TTrack &)> editor_track::on_retire;
std::function<void(TTrack &, TTrack &, segment_data const &)> editor_track::on_split;
editor_track::state editor_track::capture(TTrack const &Track)
{
if (on_capture)
on_capture(Track);
state result;
result.paths = Track.m_paths;
result.velocity = Track.fVelocity;
@@ -235,6 +242,8 @@ void editor_track::commit(std::vector<TTrack *> const &Tracks)
for (auto *track : affected)
for (auto *vehicle : track->Dynamics)
vehicle->Move(0.000001);
if (on_commit)
on_commit(Tracks);
}
void editor_track::commit_parameters(TTrack &Track)
@@ -2300,6 +2309,8 @@ TTrack *editor_track::split_path(TTrack &Track, double const T)
second.rolls[0] = roll;
auto *created{create_path(Track, second)};
Track.m_paths.front() = first;
if (on_split)
on_split(Track, *created, first);
commit({&Track, created});
return created;
}
@@ -2336,6 +2347,8 @@ void editor_track::retire(TTrack &Track)
update_transition(*neighbour);
rebuild_geometry(*neighbour);
}
if (on_retire)
on_retire(Track);
}
void editor_track::revive(TTrack &Track)
+7
View File
@@ -227,6 +227,13 @@ class editor_track
static void velocity(TTrack &Track, double const Velocity);
static void damage(TTrack &Track, int const Damage);
// let the objects which belong to the paths follow their changes
static std::function<void(TTrack const &)> on_capture;
static std::function<void(std::vector<TTrack *> const &)> on_commit;
static std::function<void(TTrack &)> on_retire;
// called before the commit; the first argument keeps the beginning of the path, given in the last one
static std::function<void(TTrack &, TTrack &, segment_data const &)> on_split;
private:
static void disconnect(TTrack &Track);
static void join(TTrack &Track);
+24
View File
@@ -503,6 +503,30 @@ basic_cell::erase( TMemCell *Memorycell ) {
std::end( m_memorycells ) );
}
void
basic_cell::erase( TTraction *Traction ) {
m_traction.erase(
std::remove( std::begin( m_traction ), std::end( m_traction ), Traction ),
std::end( m_traction ) );
}
void
basic_cell::unregister_end( TTraction *Traction ) {
m_directories.traction.erase(
std::remove( std::begin( m_directories.traction ), std::end( m_directories.traction ), Traction ),
std::end( m_directories.traction ) );
}
void
basic_cell::erase( TEventLauncher *Launcher ) {
m_eventlaunchers.erase(
std::remove( std::begin( m_eventlaunchers ), std::end( m_eventlaunchers ), Launcher ),
std::end( m_eventlaunchers ) );
}
// registers provided path in the lookup directory of the cell
void
basic_cell::register_end( TTrack *Path ) {
+8
View File
@@ -155,6 +155,14 @@ public:
// removes provided memory cell from the cell
void
erase( TMemCell *Memorycell );
// removes provided traction piece from the cell
void
erase( TTraction *Traction );
void
unregister_end( TTraction *Traction );
// removes provided event launcher from the cell
void
erase( TEventLauncher *Launcher );
// find a vehicle located nearest to specified point, within specified radius. reurns: located vehicle and distance
std::tuple<TDynamicObject *, float>
find( glm::dvec3 const &Point, float const Radius, bool const Onlycontrolled, bool const Findbycoupler ) const;
+5
View File
@@ -292,6 +292,11 @@ class node_layers
{
return Instance != 0 && Instance <= m_instances.size();
}
// handles of the includes run from 1 up to this number
std::size_t instance_count() const
{
return m_instances.size();
}
// grants access to specified include. NOTE: the include has to be tracked
include_instance const &instance(instance_handle const Instance) const
{
+112
View File
@@ -14,6 +14,9 @@ http://mozilla.org/MPL/2.0/.
#include "simulation/simulation.h"
#include "model/AnimModel.h"
#include "world/MemCell.h"
#include "world/Traction.h"
#include "world/EvLaunch.h"
#include "world/Event.h"
#include "utilities/Globals.h"
#include "utilities/Logs.h"
#include "utilities/utilities.h"
@@ -233,6 +236,54 @@ bool patch_memcell(std::string &Text, glm::dvec3 const &Location, std::string &E
return true;
}
// definition layout: node <max> <min> <name> traction <supply> <voltage> <current> <resistivity> <material> <thickness> <damage>
// <point 1> <point 2> <point 3> <point 4> <minimal height> <segment length> <wires> <offset> <vis> [parallel <name>] endtraction
bool patch_traction(std::string &Text, TTraction const &Traction, glm::dvec3 const &Offset, std::string &Error)
{
auto const tokens{tokenize(Text)};
if (tokens.size() < 30 || tokens[0].text != "node" || tokens[4].text != "traction" || tokens.back().text != "endtraction")
{
Error = "unexpected layout of traction definition";
return false;
}
std::vector<text_change> changes;
std::size_t index{12};
for (auto const &point : {Traction.pPoint1, Traction.pPoint2, Traction.pPoint3, Traction.pPoint4})
{
auto const local{point - Offset};
for (auto const value : {local.x, local.y, local.z})
{
changes.push_back({tokens[index].begin, tokens[index].end, number(value)});
++index;
}
}
auto const &p1{Traction.pPoint1};
auto const &p2{Traction.pPoint2};
auto const &p3{Traction.pPoint3};
auto const &p4{Traction.pPoint4};
changes.push_back({tokens[24].begin, tokens[24].end, number((p3.y - p1.y + p4.y - p2.y) * 0.5 - Traction.fHeightDifference)});
if (Traction.iNumSections > 0)
{
// the load truncates length / segment length, the margin keeps the rounding of the number from losing a section
changes.push_back({tokens[25].begin, tokens[25].end, number(glm::length(p1 - p2) / (Traction.iNumSections + 0.001))});
}
apply(Text, changes);
return true;
}
// definition layout: node <max> <min> <name> eventlauncher <x> <y> <z> <radius> ... end
bool patch_launcher(std::string &Text, glm::dvec3 const &Location, std::string &Error)
{
auto const tokens{tokenize(Text)};
if (tokens.size() < 9 || tokens[0].text != "node" || tokens[4].text != "eventlauncher")
{
Error = "unexpected layout of event launcher definition";
return false;
}
apply(Text, {{tokens[5].begin, tokens[5].end, number(Location.x)}, {tokens[6].begin, tokens[6].end, number(Location.y)}, {tokens[7].begin, tokens[7].end, number(Location.z)}});
return true;
}
std::string detect_eol(std::string const &Text, std::string const &Fallback)
{
auto const linebreak{Text.find('\n')};
@@ -730,6 +781,63 @@ save_result node_layers::save(std::vector<std::string> const &Rootstatements)
return fail(state.error);
}
}
// traction and event launchers are rewritten when the editor moved them along with the track they belong to
std::vector<basic_node *> patchednodes;
auto const patch_node = [&](basic_node *Node, auto const &Patch) {
auto const lookup{m_sources.find(Node)};
if (lookup == m_sources.end() || false == Node->dirty())
{
return true;
}
auto const &source{lookup->second};
if (false == is_output(resolve(source.layer)) || false == writable(source.layer))
{
return true;
}
if (false == load(source.layer))
{
return false;
}
auto const &content{state.content[source.layer]};
if (source.span.end > static_cast<std::streamoff>(content.size()))
{
state.error = "definition of \"" + Node->name() + "\" is out of bounds of file \"" + layer(source.layer).name + "\"";
return false;
}
auto text{content.substr(static_cast<std::size_t>(source.span.begin), static_cast<std::size_t>(source.span.end - source.span.begin))};
std::string error;
if (false == Patch(text, source, error))
{
state.error = error + " of \"" + Node->name() + "\" in file \"" + layer(source.layer).name + "\"";
return false;
}
file_patch::edit edit;
edit.begin = source.span.begin;
edit.end = source.span.end;
edit.length = text.size();
edit.text = std::move(text);
edit.node = Node;
state.patches[source.layer].edits.emplace_back(std::move(edit));
rewritten.emplace(Node);
patchednodes.push_back(Node);
return true;
};
for (auto *traction : simulation::Traction.sequence())
{
if (traction != nullptr &&
false == patch_node(traction, [&](std::string &Text, node_source const &Source, std::string &Error) { return patch_traction(Text, *traction, Source.context.offset, Error); }))
{
return fail(state.error);
}
}
for (auto *launcher : simulation::Events.launchers())
{
if (launcher != nullptr &&
false == patch_node(launcher, [&](std::string &Text, node_source const &Source, std::string &Error) { return patch_launcher(Text, Source.context.to_local(launcher->location()), Error); }))
{
return fail(state.error);
}
}
std::vector<TTrack *> savedpaths;
std::vector<TTrack const *> droppedpaths;
auto const path_text = [](TTrack &Path, glm::dvec3 const &Offset) {
@@ -1281,6 +1389,10 @@ save_result node_layers::save(std::vector<std::string> const &Rootstatements)
{
path->m_dirty = false;
}
for (auto *node : patchednodes)
{
node->m_dirty = false;
}
for (auto const *path : droppedpaths)
{
m_sources.erase(path);
+2
View File
@@ -510,6 +510,7 @@ state_serializer::deserialize_node( cParser &Input, scene::scratch_data &Scratch
}
scene::Groups.insert( scene::Groups.handle(), traction );
simulation::Region->insert_and_register( traction );
scene::Layers.track( traction, { sourcebegin, Input.TokenEnd() } );
}
else if( nodedata.type == "tractionpowersource" ) {
@@ -656,6 +657,7 @@ state_serializer::deserialize_node( cParser &Input, scene::scratch_data &Scratch
simulation::Region->insert( eventlauncher );
}
}
scene::Layers.track( eventlauncher, { sourcebegin, Input.TokenEnd() } );
}
else if( nodedata.type == "sound" ) {
+8
View File
@@ -699,6 +699,14 @@ public:
// returns all eventlaunchers in radius ignoring height
std::vector<TEventLauncher *>
find_eventlaunchers(glm::vec2 center, float radius) const;
// all event launchers, global ones included
std::vector<TEventLauncher *>
launchers() const {
auto const &input { m_inputdrivenlaunchers.sequence() };
auto const &radio { m_radiodrivenlaunchers.sequence() };
std::vector<TEventLauncher *> result( input.begin(), input.end() );
result.insert( result.end(), radio.begin(), radio.end() );
return result; }
private:
// types