18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 14:07:06 +01:00
Files
maszyna/tests/unit/test_eu07m_edit.cpp
T
maj00r af3815eee3 feat(editor): a part of an EU07M model taken out with what it draws
"Remove this part" (Del) in the Models mode takes out any part without parts under it which nothing else
in the model points at (an animation, a light slot, a skin): with its components, its transform and its own
mesh. eu07m::remove_node now takes such a node with its geometry: its primitives go, and their vertices and
indices leave the streams where no other primitive shares them. The primitives after them move back, and
so do the meshes, the vertex groups and the references to the geometry store. A part whose mesh another
node draws too, or whose mesh has vertex groups, stays. Ctrl+Z puts the part back and picks it again.

So the flat glowing patch under a lamp post of E3D (placek) is taken out and a real light source put in the
lamp instead. Unit tests take out a part with geometry, check the streams and the primitive moved into its
place, write and read the model back, and keep a part whose mesh is shared.
2026-11-30 00:33:41 +01:00

236 lines
8.9 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "model/eu07m/eu07m_build.h"
#include "model/eu07m/eu07m_io.h"
#include <cstring>
#include <vector>
namespace {
// root (0) { lamp (1), door (2) }, rear (3); a light slot of the root lists the door and the rear node, a joint is aimed at the
// rear node and a clip moves it
eu07m::model lamp_model() {
eu07m::model model;
auto const node = [&model](std::string_view const Name, std::int32_t const Parent, std::uint32_t const End) {
eu07m::node_record record;
record.name = model.intern(Name);
record.parent = Parent;
record.subtree_end = End;
model.nodes.push_back(record);
};
node("root", -1, 3);
node("lamp", 0, 2);
node("door", 0, 3);
node("rear", -1, 4);
eu07m::payload::light_slot slot;
slot.on_count = 1;
slot.off_count = 1;
std::vector<std::byte> data(sizeof(slot) + 2 * sizeof(std::uint32_t));
std::memcpy(data.data(), &slot, sizeof(slot));
std::uint32_t const lit{2};
std::uint32_t const dark{3};
std::memcpy(data.data() + sizeof(slot), &lit, sizeof(lit));
std::memcpy(data.data() + sizeof(slot) + sizeof(lit), &dark, sizeof(dark));
eu07m::add_component(model, eu07m::component::light_slot, eu07m::target_kind::node, 0, data);
eu07m::payload::joint joint;
std::vector<std::byte> jointdata(sizeof(joint));
std::memcpy(jointdata.data(), &joint, sizeof(joint));
eu07m::add_component(model, eu07m::component::joint, eu07m::target_kind::node, 3, jointdata);
eu07m::clip_record clip;
clip.name = model.intern("open");
clip.track_first = 0;
clip.track_count = 1;
model.clips.push_back(clip);
eu07m::track_record track;
track.node = 3;
track.key_count = 1;
model.clip_tracks.push_back(track);
model.key_times.push_back(0.f);
model.key_vectors.push_back({0.f, 0.f, 0.f});
eu07m::build_name_index(model);
return model;
}
std::uint32_t listed_node(eu07m::model const &Model, std::uint32_t const Component, std::size_t const Item) {
std::uint32_t node{0};
std::memcpy(&node, Model.component_data.data() + Model.components[Component].data_offset + sizeof(eu07m::payload::light_slot) + Item * sizeof(node), sizeof(node));
return node;
}
} // namespace
TEST_CASE("eu07m: a node put in under a parent moves the references to the nodes after it") {
auto model{lamp_model()};
REQUIRE(eu07m::validate(model).empty());
eu07m::node_record light;
light.name = model.intern("light_1");
auto const index{eu07m::insert_node(model, 0, light)};
CHECK(index == 3);
REQUIRE(model.nodes.size() == 5);
// the root holds it, the lamp and the door keep their subtrees, the rear node moves on
CHECK(model.nodes[0].subtree_end == 4);
CHECK(model.nodes[1].subtree_end == 2);
CHECK(model.nodes[2].subtree_end == 3);
CHECK(model.nodes[3].parent == 0);
CHECK(model.nodes[3].subtree_end == 4);
CHECK(model.string(model.nodes[4].name) == "rear");
CHECK(model.nodes[4].parent == -1);
CHECK(model.nodes[4].subtree_end == 5);
// the joint, the track and the light slot follow the rear node; the door stays
CHECK(model.components[1].target == 4);
CHECK(model.clip_tracks[0].node == 4);
CHECK(listed_node(model, 0, 0) == 2);
CHECK(listed_node(model, 0, 1) == 4);
CHECK(eu07m::find_nodes(model, "light_1") == std::vector<std::uint32_t>{3});
CHECK(eu07m::find_nodes(model, "rear") == std::vector<std::uint32_t>{4});
CHECK(eu07m::validate(model).empty());
}
TEST_CASE("eu07m: a node put in without a parent goes last, and a component added is listed by it") {
auto model{lamp_model()};
eu07m::node_record light;
light.name = model.intern("light_2");
auto const index{eu07m::insert_node(model, -1, light)};
CHECK(index == 4);
CHECK(model.nodes[3].subtree_end == 4);
CHECK(model.nodes[4].subtree_end == 5);
eu07m::payload::light payload;
std::vector<std::byte> data(sizeof(payload));
std::memcpy(data.data(), &payload, sizeof(payload));
auto const component{eu07m::add_component(model, eu07m::component::light, eu07m::target_kind::node, index, data)};
CHECK(model.nodes[index].component_first == component);
CHECK(model.nodes[index].component_count == 1);
CHECK(model.components[component].data_offset % 4 == 0);
CHECK(eu07m::validate(model).empty());
// the file written and read again holds the same
eu07m::model read;
REQUIRE(eu07m::read(eu07m::write(model), read).ok);
CHECK(read.nodes.size() == 5);
CHECK(read.components.size() == 3);
}
TEST_CASE("eu07m: a light put in and taken out again leaves the model as it was") {
auto model{lamp_model()};
auto const before{model.nodes};
eu07m::node_record light;
light.name = model.intern("light_3");
auto const index{eu07m::insert_node(model, 0, light)};
eu07m::payload::light payload;
std::vector<std::byte> data(sizeof(payload));
std::memcpy(data.data(), &payload, sizeof(payload));
eu07m::add_component(model, eu07m::component::light, eu07m::target_kind::node, index, data);
REQUIRE(eu07m::node_removable(model, index));
REQUIRE(eu07m::remove_node(model, index));
REQUIRE(model.nodes.size() == before.size());
for (std::size_t idx = 0; idx < before.size(); ++idx) {
CHECK(model.nodes[idx].parent == before[idx].parent);
CHECK(model.nodes[idx].subtree_end == before[idx].subtree_end);
CHECK(model.nodes[idx].name == before[idx].name);
}
// the components of the light are gone, the others point where they did
REQUIRE(model.components.size() == 2);
CHECK(model.components[1].target == 3);
CHECK(model.clip_tracks[0].node == 3);
CHECK(listed_node(model, 0, 0) == 2);
CHECK(listed_node(model, 0, 1) == 3);
CHECK(eu07m::find_nodes(model, "rear") == std::vector<std::uint32_t>{3});
CHECK(eu07m::validate(model).empty());
}
TEST_CASE("eu07m: a node with children, or one something points at, isn't taken out") {
auto model{lamp_model()};
auto const nodes{model.nodes.size()};
// the root has children, the door is listed by the light slot, the rear node is moved by a clip
CHECK_FALSE(eu07m::remove_node(model, 0));
CHECK_FALSE(eu07m::remove_node(model, 2));
CHECK_FALSE(eu07m::remove_node(model, 3));
CHECK_FALSE(eu07m::remove_node(model, 9));
CHECK(model.nodes.size() == nodes);
// the lamp is a leaf nothing points at
CHECK(eu07m::remove_node(model, 1));
CHECK(model.nodes.size() == nodes - 1);
CHECK(model.nodes[0].subtree_end == 2);
CHECK(listed_node(model, 0, 0) == 1);
CHECK(listed_node(model, 0, 1) == 2);
CHECK(eu07m::validate(model).empty());
}
namespace {
// root (0) { decal (1), lamp (2) }: the decal and the lamp have a triangle of their own each, the lamp's after the decal's in the
// streams; a vertex has a position and its attributes
eu07m::model geometry_model() {
eu07m::model model;
auto const node = [&model](std::string_view const Name, std::int32_t const Parent, std::uint32_t const End, std::int32_t const Mesh) {
eu07m::node_record record;
record.name = model.intern(Name);
record.parent = Parent;
record.subtree_end = End;
record.mesh = Mesh;
model.nodes.push_back(record);
};
node("root", -1, 3, -1);
node("decal", 0, 2, 0);
node("lamp", 0, 3, 1);
for (std::uint32_t mesh = 0; mesh < 2; ++mesh) {
eu07m::mesh_record record;
record.primitive_first = mesh;
record.primitive_count = 1;
model.meshes.push_back(record);
eu07m::primitive_record primitive;
primitive.first_index = mesh * 3;
primitive.index_count = 3;
primitive.base_vertex = mesh * 3;
primitive.vertex_count = 3;
primitive.node = mesh + 1;
primitive.flags = static_cast<std::uint32_t>(eu07m::topology::triangles) | eu07m::primitive_flags::indexed | eu07m::primitive_flags::opaque;
model.primitives.push_back(primitive);
for (std::uint16_t vertex = 0; vertex < 3; ++vertex) {
model.positions.push_back({static_cast<float>(mesh), static_cast<float>(vertex), 0.f});
model.vertex_attributes.emplace_back();
model.indices16.push_back(vertex);
}
}
eu07m::build_name_index(model);
return model;
}
} // namespace
TEST_CASE("eu07m: a part taken out takes its geometry, and the parts after it move into its place") {
auto model{geometry_model()};
REQUIRE(eu07m::validate(model).empty());
REQUIRE(eu07m::node_removable(model, 1));
REQUIRE(eu07m::remove_node(model, 1));
REQUIRE(model.nodes.size() == 2);
CHECK(model.string(model.nodes[1].name) == "lamp");
CHECK(model.nodes[0].subtree_end == 2);
REQUIRE(model.meshes.size() == 1);
CHECK(model.nodes[1].mesh == 0);
REQUIRE(model.primitives.size() == 1);
CHECK(model.meshes[0].primitive_first == 0);
CHECK(model.primitives[0].base_vertex == 0);
CHECK(model.primitives[0].first_index == 0);
CHECK(model.primitives[0].node == 1);
// the lamp's vertices are the ones left
REQUIRE(model.positions.size() == 3);
CHECK(model.positions[0][0] == 1.f);
CHECK(model.vertex_attributes.size() == 3);
CHECK(model.indices16.size() == 3);
CHECK(eu07m::validate(model).empty());
eu07m::model read;
REQUIRE(eu07m::read(eu07m::write(model), read).ok);
CHECK(read.primitives.size() == 1);
}
TEST_CASE("eu07m: a part whose mesh another part draws too isn't taken out") {
auto model{geometry_model()};
model.nodes[2].mesh = 0;
CHECK_FALSE(eu07m::node_removable(model, 1));
CHECK_FALSE(eu07m::remove_node(model, 1));
CHECK(model.primitives.size() == 2);
}