mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 18:47:06 +01:00
6828e52a45
The window of an EU07M model becomes the field of work "Models" (F7) beside the surroundings, the tracks, the roads, the terrain and the catenary. The node bank says what is edited and lists the vehicles, the tool options hold the page of the tool chosen in the toolbar (Lights, Parts of the vehicle, All parts, Animations), and the inspector the settings of the part picked, in plain words; the fields of the format are kept under Advanced. - A click on the model picks its part (the smallest box of geometry of a part shown holding the point); a light is added where the model is clicked, a spot shining towards the camera. - A light which nothing else points at is taken out (eu07m::node_removable / remove_node, with TModel3d::UnlinkEditorSubmodel / LinkEditorSubmodel on the model the game has). - Ctrl+Z / Ctrl+Y take the changes of the model back and make them again, a light added or taken out among them; another model asked for with changes not saved asks first. - "Import to EU07M" writes <model>.eu07m beside the .e3d or .t3d of a model without one, made of the model as the game loaded it (eu07m::import_model), so a lamp post or a cab gets its lights here. - "Put a model in the scenery" puts a model of the folder of the models in front of the camera, "Whole model" moves the scenery model with the gizmo of the editor, "Put a vehicle on a track..." opens the Trainset tool. - The inspector of a scenery model with an .eu07m file offers "Edit this model (F7)"; the floating window is gone. - Review: the model is written to its .tmp, closed and checked before it replaces the file, and a failed .tmp is removed; play_clip counts From only for a positive pace again, so a pace of 0 holds the clip and a negative one runs it backwards. New icons at the end of editor_icons.png (the cells before them are unchanged), the strings in lang/editor_pl.po, new self-test commands (modelmode, modeltool, modeladdlight, modelundo, modelredo, modelremove, modelplace, modelimport) and unit tests of remove_node. No asset in the SVN is needed or changed; the import writes an .eu07m file into the game data only when the user asks for it.
631 lines
25 KiB
C++
631 lines
25 KiB
C++
/*
|
|
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 "model/eu07m/eu07m_build.h"
|
|
|
|
#include <glm/gtc/quaternion.hpp>
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstring>
|
|
#include <limits>
|
|
#include <numbers>
|
|
#include <unordered_map>
|
|
|
|
namespace eu07m {
|
|
|
|
namespace {
|
|
|
|
constexpr float snorm16_scale{32767.f};
|
|
constexpr float minimum_length{1e-12f};
|
|
|
|
glm::vec3 perpendicular(glm::vec3 const &Normal) {
|
|
// the axis least along the normal gives the steadiest cross product
|
|
auto const absolute{glm::abs(Normal)};
|
|
glm::vec3 axis{1.f, 0.f, 0.f};
|
|
if (absolute.y < absolute.x && absolute.y <= absolute.z)
|
|
axis = {0.f, 1.f, 0.f};
|
|
else if (absolute.z < absolute.x && absolute.z < absolute.y)
|
|
axis = {0.f, 0.f, 1.f};
|
|
return glm::normalize(glm::cross(axis, Normal));
|
|
}
|
|
|
|
std::int16_t pack_snorm16(float const Value) {
|
|
return static_cast<std::int16_t>(std::lround(std::clamp(Value, -1.f, 1.f) * snorm16_scale));
|
|
}
|
|
|
|
glm::mat4 compose(node_record const &Node) {
|
|
glm::quat const rotation{Node.rotation[3], Node.rotation[0], Node.rotation[1], Node.rotation[2]};
|
|
auto transform{glm::mat4_cast(glm::normalize(rotation))};
|
|
transform[0] *= Node.scale[0];
|
|
transform[1] *= Node.scale[1];
|
|
transform[2] *= Node.scale[2];
|
|
transform[3] = glm::vec4{Node.translation[0], Node.translation[1], Node.translation[2], 1.f};
|
|
return transform;
|
|
}
|
|
|
|
std::unordered_map<std::uint32_t, std::size_t> matrix_lookup(model const &Model) {
|
|
std::unordered_map<std::uint32_t, std::size_t> lookup;
|
|
for (std::size_t idx = 0; idx < Model.transforms.size(); ++idx)
|
|
lookup.try_emplace(Model.transforms[idx].node, idx);
|
|
return lookup;
|
|
}
|
|
|
|
glm::mat4 node_transform(model const &Model, std::uint32_t const Node, std::unordered_map<std::uint32_t, std::size_t> const &Matrices) {
|
|
auto const &node{Model.nodes[Node]};
|
|
if ((node.flags & node_flags::matrix) != 0) {
|
|
if (auto const lookup{Matrices.find(Node)}; lookup != Matrices.end())
|
|
return glm::make_mat4(Model.transforms[lookup->second].matrix.data());
|
|
}
|
|
return compose(node);
|
|
}
|
|
|
|
std::string lowercase(std::string_view const Text) {
|
|
std::string result{Text};
|
|
for (auto &character : result) {
|
|
if (character >= 'A' && character <= 'Z')
|
|
character = static_cast<char>(character - 'A' + 'a');
|
|
}
|
|
return result;
|
|
}
|
|
|
|
struct box {
|
|
glm::vec3 min{std::numeric_limits<float>::max()};
|
|
glm::vec3 max{std::numeric_limits<float>::lowest()};
|
|
bool empty() const {
|
|
return min.x > max.x;
|
|
}
|
|
void add(glm::vec3 const &Point) {
|
|
min = glm::min(min, Point);
|
|
max = glm::max(max, Point);
|
|
}
|
|
glm::vec3 centre() const {
|
|
return empty() ? glm::vec3{0.f} : (min + max) * 0.5f;
|
|
}
|
|
};
|
|
|
|
glm::vec3 to_vec3(position const &Position) {
|
|
return {Position[0], Position[1], Position[2]};
|
|
}
|
|
|
|
// the sphere around the centre of the box of the vertices, reaching the farthest of them
|
|
std::array<float, 4> sphere_of(model const &Model, std::size_t const First, std::size_t const Count, box &Box) {
|
|
for (auto idx = First; idx < First + Count; ++idx)
|
|
Box.add(to_vec3(Model.positions[idx]));
|
|
auto const centre{Box.centre()};
|
|
float radius{0.f};
|
|
for (auto idx = First; idx < First + Count; ++idx)
|
|
radius = std::max(radius, glm::length(to_vec3(Model.positions[idx]) - centre));
|
|
return {centre.x, centre.y, centre.z, radius};
|
|
}
|
|
|
|
bounds_record to_bounds(box const &Box, float const Radius) {
|
|
bounds_record bounds;
|
|
if (Box.empty())
|
|
return bounds;
|
|
auto const centre{Box.centre()};
|
|
bounds.min = {Box.min.x, Box.min.y, Box.min.z};
|
|
bounds.max = {Box.max.x, Box.max.y, Box.max.z};
|
|
bounds.sphere = {centre.x, centre.y, centre.z, Radius};
|
|
return bounds;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
std::array<std::int16_t, 4> encode_qtangent(tangent_frame const &Frame) {
|
|
auto const normal{glm::dot(Frame.normal, Frame.normal) > minimum_length ? glm::normalize(Frame.normal) : glm::vec3{0.f, 0.f, 1.f}};
|
|
auto tangent{Frame.tangent - normal * glm::dot(normal, Frame.tangent)};
|
|
tangent = glm::dot(tangent, tangent) > minimum_length ? glm::normalize(tangent) : perpendicular(normal);
|
|
auto const bitangent{glm::cross(normal, tangent)};
|
|
auto rotation{glm::normalize(glm::quat_cast(glm::mat3{tangent, bitangent, normal}))};
|
|
if (rotation.w < 0.f)
|
|
rotation = -rotation;
|
|
// w keeps its sign through the packing, the sign of the bitangent rides on it
|
|
constexpr float bias{1.f / snorm16_scale};
|
|
if (rotation.w < bias) {
|
|
auto const scale{std::sqrt(1.f - bias * bias) / std::max(glm::length(glm::vec3{rotation.x, rotation.y, rotation.z}), minimum_length)};
|
|
rotation = glm::quat{bias, rotation.x * scale, rotation.y * scale, rotation.z * scale};
|
|
}
|
|
if (Frame.handedness < 0.f)
|
|
rotation = -rotation;
|
|
return {pack_snorm16(rotation.x), pack_snorm16(rotation.y), pack_snorm16(rotation.z), pack_snorm16(rotation.w)};
|
|
}
|
|
|
|
packed_rotation encode_rotation(std::array<float, 4> const &Quaternion) {
|
|
auto quaternion{Quaternion};
|
|
auto const length{std::sqrt(quaternion[0] * quaternion[0] + quaternion[1] * quaternion[1] + quaternion[2] * quaternion[2] + quaternion[3] * quaternion[3])};
|
|
if (false == std::isfinite(length) || length <= 0.f)
|
|
return {};
|
|
std::size_t largest{0};
|
|
for (std::size_t idx = 0; idx < 4; ++idx) {
|
|
quaternion[idx] /= length;
|
|
if (std::abs(quaternion[idx]) > std::abs(quaternion[largest]))
|
|
largest = idx;
|
|
}
|
|
auto const sign{quaternion[largest] < 0.f ? -1.f : 1.f};
|
|
packed_rotation packed;
|
|
packed.largest = static_cast<std::uint16_t>(largest);
|
|
std::size_t slot{0};
|
|
for (std::size_t idx = 0; idx < 4; ++idx) {
|
|
if (idx == largest)
|
|
continue;
|
|
// the other components lie within +-1/sqrt(2)
|
|
auto const scaled{std::clamp(sign * quaternion[idx] * std::numbers::sqrt2_v<float>, -1.f, 1.f)};
|
|
packed.components[slot] = static_cast<std::int16_t>(std::lround(scaled * 32767.f));
|
|
++slot;
|
|
}
|
|
return packed;
|
|
}
|
|
|
|
std::array<float, 4> decode_rotation(packed_rotation const &Packed) {
|
|
std::array<float, 4> quaternion{0.f, 0.f, 0.f, 1.f};
|
|
auto const largest{static_cast<std::size_t>(std::min<std::uint16_t>(Packed.largest, 3))};
|
|
auto sum{0.f};
|
|
std::size_t slot{0};
|
|
for (std::size_t idx = 0; idx < 4; ++idx) {
|
|
if (idx == largest)
|
|
continue;
|
|
quaternion[idx] = static_cast<float>(Packed.components[slot]) / 32767.f / std::numbers::sqrt2_v<float>;
|
|
++slot;
|
|
sum += quaternion[idx] * quaternion[idx];
|
|
}
|
|
quaternion[largest] = std::sqrt(std::max(0.f, 1.f - sum));
|
|
return quaternion;
|
|
}
|
|
|
|
tangent_frame decode_qtangent(std::array<std::int16_t, 4> const &Packed) {
|
|
// the first and the third column of the rotation matrix of the quaternion, which 2 / |q|^2 makes unit without a square root:
|
|
// what glm::mat3_cast(glm::normalize(q)) gives, for the two columns needed, at a part of the cost (this runs for every vertex)
|
|
auto const x{Packed[0] / snorm16_scale};
|
|
auto const y{Packed[1] / snorm16_scale};
|
|
auto const z{Packed[2] / snorm16_scale};
|
|
auto const w{Packed[3] / snorm16_scale};
|
|
tangent_frame frame;
|
|
frame.handedness = w < 0.f ? -1.f : 1.f;
|
|
auto const norm{x * x + y * y + z * z + w * w};
|
|
if (norm < 1e-12f)
|
|
return frame;
|
|
auto const s{2.f / norm};
|
|
frame.tangent = {1.f - s * (y * y + z * z), s * (x * y + w * z), s * (x * z - w * y)};
|
|
frame.normal = {s * (x * z + w * y), s * (y * z - w * x), 1.f - s * (x * x + y * y)};
|
|
return frame;
|
|
}
|
|
|
|
glm::mat4 local_transform(model const &Model, std::uint32_t const Node) {
|
|
return node_transform(Model, Node, matrix_lookup(Model));
|
|
}
|
|
|
|
std::vector<glm::mat4> local_transforms(model const &Model) {
|
|
auto const matrices{matrix_lookup(Model)};
|
|
std::vector<glm::mat4> transforms(Model.nodes.size());
|
|
for (std::uint32_t idx = 0; idx < Model.nodes.size(); ++idx)
|
|
transforms[idx] = node_transform(Model, idx, matrices);
|
|
return transforms;
|
|
}
|
|
|
|
std::vector<glm::mat4> model_transforms(model const &Model) {
|
|
auto const matrices{matrix_lookup(Model)};
|
|
std::vector<glm::mat4> transforms(Model.nodes.size());
|
|
for (std::uint32_t idx = 0; idx < Model.nodes.size(); ++idx) {
|
|
auto const local{node_transform(Model, idx, matrices)};
|
|
auto const parent{Model.nodes[idx].parent};
|
|
transforms[idx] = (parent >= 0 && parent < static_cast<std::int64_t>(idx)) ? transforms[parent] * local : local;
|
|
}
|
|
return transforms;
|
|
}
|
|
|
|
bool set_transform(node_record &Node, glm::mat4 const &Matrix, float const Tolerance) {
|
|
glm::vec3 scale{glm::length(glm::vec3{Matrix[0]}), glm::length(glm::vec3{Matrix[1]}), glm::length(glm::vec3{Matrix[2]})};
|
|
glm::mat3 rotation{1.f};
|
|
if (scale.x > minimum_length && scale.y > minimum_length && scale.z > minimum_length) {
|
|
rotation = glm::mat3{glm::vec3{Matrix[0]} / scale.x, glm::vec3{Matrix[1]} / scale.y, glm::vec3{Matrix[2]} / scale.z};
|
|
// a mirror goes into the scale, the rotation stays proper
|
|
if (glm::determinant(rotation) < 0.f) {
|
|
scale.x = -scale.x;
|
|
rotation[0] = -rotation[0];
|
|
}
|
|
}
|
|
auto const quaternion{glm::normalize(glm::quat_cast(rotation))};
|
|
Node.translation = {Matrix[3].x, Matrix[3].y, Matrix[3].z};
|
|
Node.rotation = {quaternion.x, quaternion.y, quaternion.z, quaternion.w};
|
|
Node.scale = {scale.x, scale.y, scale.z};
|
|
auto const composed{compose(Node)};
|
|
for (int column = 0; column < 4; ++column) {
|
|
for (int row = 0; row < 4; ++row) {
|
|
auto const original{Matrix[column][row]};
|
|
if (std::abs(composed[column][row] - original) > Tolerance * std::max(1.f, std::abs(original)))
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void build_name_index(model &Model) {
|
|
Model.name_index.clear();
|
|
for (std::uint32_t idx = 0; idx < Model.nodes.size(); ++idx) {
|
|
if (auto const name{Model.nodes[idx].name}; name != no_index && name < Model.strings.size())
|
|
Model.name_index.push_back({hash_name(Model.strings[name]), idx, 0});
|
|
}
|
|
std::ranges::sort(Model.name_index, [](name_index_record const &Left, name_index_record const &Right) { return Left.hash != Right.hash ? Left.hash < Right.hash : Left.node < Right.node; });
|
|
}
|
|
|
|
std::vector<std::uint32_t> find_nodes(model const &Model, std::string_view const Name) {
|
|
auto const key{lowercase(Name)};
|
|
std::vector<std::uint32_t> found;
|
|
auto const matches{[&Model, &key](std::uint32_t const Node) { return lowercase(Model.string(Model.nodes[Node].name)) == key; }};
|
|
if (Model.name_index.empty()) {
|
|
for (std::uint32_t idx = 0; idx < Model.nodes.size(); ++idx) {
|
|
if (Model.nodes[idx].name != no_index && matches(idx))
|
|
found.push_back(idx);
|
|
}
|
|
return found;
|
|
}
|
|
auto const hash{hash_name(Name)};
|
|
auto const range{std::ranges::equal_range(Model.name_index, hash, {}, &name_index_record::hash)};
|
|
for (auto const &entry : range) {
|
|
if (matches(entry.node))
|
|
found.push_back(entry.node);
|
|
}
|
|
return found;
|
|
}
|
|
|
|
void compute_bounds(model &Model) {
|
|
std::vector<box> boxes(Model.nodes.size());
|
|
std::vector<float> radii(Model.nodes.size(), 0.f);
|
|
for (auto &primitive : Model.primitives) {
|
|
if (primitive.node >= Model.nodes.size() || primitive.vertex_count == 0)
|
|
continue;
|
|
box own;
|
|
primitive.sphere = sphere_of(Model, primitive.base_vertex, primitive.vertex_count, own);
|
|
auto &nodebox{boxes[primitive.node]};
|
|
nodebox.add(own.min);
|
|
nodebox.add(own.max);
|
|
}
|
|
// the radius of the node around the centre of its box, from all its vertices
|
|
for (auto const &primitive : Model.primitives) {
|
|
if (primitive.node >= Model.nodes.size() || primitive.vertex_count == 0)
|
|
continue;
|
|
auto const centre{boxes[primitive.node].centre()};
|
|
auto &radius{radii[primitive.node]};
|
|
for (auto idx = std::size_t{primitive.base_vertex}; idx < std::size_t{primitive.base_vertex} + primitive.vertex_count; ++idx)
|
|
radius = std::max(radius, glm::length(to_vec3(Model.positions[idx]) - centre));
|
|
}
|
|
Model.bounds.clear();
|
|
Model.bounds.reserve(Model.nodes.size() + 1);
|
|
auto const transforms{model_transforms(Model)};
|
|
box whole;
|
|
for (std::size_t idx = 0; idx < Model.nodes.size(); ++idx) {
|
|
Model.bounds.push_back(to_bounds(boxes[idx], radii[idx]));
|
|
if (boxes[idx].empty())
|
|
continue;
|
|
for (int corner = 0; corner < 8; ++corner) {
|
|
glm::vec3 const point{(corner & 1) ? boxes[idx].max.x : boxes[idx].min.x, (corner & 2) ? boxes[idx].max.y : boxes[idx].min.y, (corner & 4) ? boxes[idx].max.z : boxes[idx].min.z};
|
|
whole.add(glm::vec3{transforms[idx] * glm::vec4{point, 1.f}});
|
|
}
|
|
}
|
|
Model.bounds.push_back(to_bounds(whole, whole.empty() ? 0.f : glm::length(whole.max - whole.centre())));
|
|
}
|
|
|
|
namespace {
|
|
|
|
// how the indices of the nodes change: after a node put in at At, those from At on move one place on; after the node At taken
|
|
// out, those after it move one place back and At itself is gone
|
|
struct node_shift {
|
|
std::uint32_t at{0};
|
|
bool removal{false};
|
|
|
|
void operator()(std::uint32_t &Node) const {
|
|
if (Node == no_index)
|
|
return;
|
|
if (false == removal) {
|
|
if (Node >= at)
|
|
++Node;
|
|
return;
|
|
}
|
|
if (Node == at)
|
|
Node = no_index;
|
|
else if (Node > at)
|
|
--Node;
|
|
}
|
|
};
|
|
|
|
template <typename Type_> bool read_at(std::span<std::byte const> const Data, std::size_t const Offset, Type_ &Value) {
|
|
if (Offset + sizeof(Type_) > Data.size())
|
|
return false;
|
|
std::memcpy(&Value, Data.data() + Offset, sizeof(Type_));
|
|
return true;
|
|
}
|
|
|
|
template <typename Type_> void write_at(std::span<std::byte> const Data, std::size_t const Offset, Type_ const &Value) {
|
|
std::memcpy(Data.data() + Offset, &Value, sizeof(Type_));
|
|
}
|
|
|
|
// Count indices from Offset of the data, each given to Visit, which may change it
|
|
template <typename Visit_> void visit_index_list(std::span<std::byte> const Data, std::size_t const Offset, std::size_t const Count, Visit_ const &Visit) {
|
|
for (std::size_t idx = 0; idx < Count; ++idx) {
|
|
auto const offset{Offset + idx * sizeof(std::uint32_t)};
|
|
std::uint32_t index{0};
|
|
if (false == read_at(Data, offset, index))
|
|
return;
|
|
Visit(index);
|
|
write_at(Data, offset, index);
|
|
}
|
|
}
|
|
|
|
template <typename Visit_> void visit_properties(std::span<std::byte> const Data, Visit_ const &Visit) {
|
|
std::uint32_t count{0};
|
|
if (false == read_at(Data, 0, count))
|
|
return;
|
|
for (std::uint32_t idx = 0; idx < count; ++idx) {
|
|
auto const offset{sizeof(std::uint32_t) + idx * sizeof(payload::property)};
|
|
payload::property property;
|
|
if (false == read_at(Data, offset, property))
|
|
return;
|
|
if (property.type != payload::value_type::node)
|
|
continue;
|
|
Visit(property.value[0]);
|
|
write_at(Data, offset, property);
|
|
}
|
|
}
|
|
|
|
template <typename Visit_> void visit_spring(std::span<std::byte> const Data, Visit_ const &Visit) {
|
|
payload::spring spring;
|
|
if (false == read_at(Data, 0, spring))
|
|
return;
|
|
Visit(spring.anchor_a);
|
|
Visit(spring.anchor_b);
|
|
write_at(Data, 0, spring);
|
|
}
|
|
|
|
template <typename Visit_> void visit_chain(std::span<std::byte> const Data, Visit_ const &Visit) {
|
|
if (payload::chain chain; read_at(Data, 0, chain))
|
|
visit_index_list(Data, sizeof(chain), chain.joint_count, Visit);
|
|
}
|
|
|
|
template <typename Visit_> void visit_variants(std::span<std::byte> const Data, Visit_ const &Visit) {
|
|
payload::variants variants;
|
|
if (false == read_at(Data, 0, variants))
|
|
return;
|
|
std::size_t listsize{0};
|
|
for (std::uint32_t idx = 0; idx < variants.state_count; ++idx) {
|
|
if (payload::variant_state state; read_at(Data, sizeof(variants) + idx * sizeof(state), state))
|
|
listsize = std::max<std::size_t>(listsize, std::size_t{state.node_first} + state.node_count);
|
|
}
|
|
visit_index_list(Data, sizeof(variants) + variants.state_count * sizeof(payload::variant_state), listsize, Visit);
|
|
}
|
|
|
|
template <typename Visit_> void visit_light_slot(std::span<std::byte> const Data, Visit_ const &Visit) {
|
|
if (payload::light_slot slot; read_at(Data, 0, slot))
|
|
visit_index_list(Data, sizeof(slot), std::size_t{slot.on_count} + slot.off_count, Visit);
|
|
}
|
|
|
|
// the data of the component, none when it runs past the data of the model
|
|
std::span<std::byte> payload_of(model &Model, component_record const &Record) {
|
|
if (std::size_t{Record.data_offset} + Record.data_size > Model.component_data.size())
|
|
return {};
|
|
return {Model.component_data.data() + Record.data_offset, Record.data_size};
|
|
}
|
|
|
|
// the nodes in the data of the component, each given to Visit, which may change it
|
|
template <typename Visit_> void visit_payload_nodes(model &Model, component_record const &Record, Visit_ const &Visit) {
|
|
auto const data{payload_of(Model, Record)};
|
|
switch (Record.type) {
|
|
case component::properties: visit_properties(data, Visit); break;
|
|
case component::spring: visit_spring(data, Visit); break;
|
|
case component::chain: visit_chain(data, Visit); break;
|
|
case component::variants: visit_variants(data, Visit); break;
|
|
case component::light_slot: visit_light_slot(data, Visit); break;
|
|
default: break;
|
|
}
|
|
}
|
|
|
|
// the components the data of the component points at, each given to Visit, which may change it: the targets of the bindings of a
|
|
// channel other than clips, and the light sources of a light slot
|
|
template <typename Visit_> void visit_payload_components(model &Model, component_record const &Record, Visit_ const &Visit) {
|
|
auto const data{payload_of(Model, Record)};
|
|
if (Record.type == component::light_slot) {
|
|
if (payload::light_slot slot; read_at(data, 0, slot))
|
|
visit_index_list(data, sizeof(slot) + (std::size_t{slot.on_count} + slot.off_count) * sizeof(std::uint32_t), slot.light_count, Visit);
|
|
return;
|
|
}
|
|
payload::channel channel;
|
|
if ((Record.type != component::channel) || (false == read_at(data, 0, channel)))
|
|
return;
|
|
for (std::uint16_t idx = 0; idx < channel.binding_count; ++idx) {
|
|
auto const offset{sizeof(channel) + idx * sizeof(payload::channel_binding)};
|
|
payload::channel_binding binding;
|
|
if (false == read_at(data, offset, binding))
|
|
return;
|
|
if (binding.kind == payload::binding_kind::clip)
|
|
continue;
|
|
Visit(binding.target);
|
|
write_at(data, offset, binding);
|
|
}
|
|
}
|
|
|
|
// each place but the tree itself where the model points at a node, given to Visit, which may change it: the transforms, the
|
|
// primitives, the joints of the skins, the tracks of the clips, the targets of the components and the nodes in their data. the
|
|
// roots of the skins are signed, the callers see to them
|
|
template <typename Visit_> void visit_node_references(model &Model, Visit_ const &Visit) {
|
|
for (auto &transform : Model.transforms)
|
|
Visit(transform.node);
|
|
for (auto &primitive : Model.primitives)
|
|
Visit(primitive.node);
|
|
for (auto &joint : Model.skin_joints)
|
|
Visit(joint.node);
|
|
for (auto &track : Model.clip_tracks)
|
|
Visit(track.node);
|
|
for (auto &record : Model.components) {
|
|
if (record.kind == target_kind::node)
|
|
Visit(record.target);
|
|
visit_payload_nodes(Model, record, Visit);
|
|
}
|
|
}
|
|
|
|
// the subtrees which end at At and hold Parent take the new node in; the others after it move on
|
|
void shift_subtrees(model &Model, std::int32_t const Parent, std::uint32_t const At) {
|
|
std::vector<bool> holding(Model.nodes.size(), false);
|
|
for (auto node{Parent}; node >= 0 && node < static_cast<std::int32_t>(Model.nodes.size()); node = Model.nodes[node].parent)
|
|
holding[node] = true;
|
|
for (std::uint32_t idx = 0; idx < Model.nodes.size(); ++idx) {
|
|
auto &node{Model.nodes[idx]};
|
|
if (holding[idx] || node.subtree_end > At)
|
|
++node.subtree_end;
|
|
if (node.parent >= static_cast<std::int32_t>(At))
|
|
++node.parent;
|
|
}
|
|
}
|
|
|
|
// something other than its own components points at the node: the geometry, a transform, a skin, a clip, the data of a component
|
|
bool node_referenced(model &Model, std::uint32_t const Node) {
|
|
auto found{false};
|
|
auto const check = [Node, &found](std::uint32_t const &Index) { found |= (Index == Node); };
|
|
for (auto const &transform : Model.transforms)
|
|
check(transform.node);
|
|
for (auto const &primitive : Model.primitives)
|
|
check(primitive.node);
|
|
for (auto const &joint : Model.skin_joints)
|
|
check(joint.node);
|
|
for (auto const &track : Model.clip_tracks)
|
|
check(track.node);
|
|
for (auto const &record : Model.components)
|
|
visit_payload_nodes(Model, record, check);
|
|
found |= std::ranges::any_of(Model.skins, [Node](skin_record const &Skin) { return Skin.root == static_cast<std::int32_t>(Node); });
|
|
return found;
|
|
}
|
|
|
|
// the data of another component points at the component: a binding of a channel, the light sources of a light slot
|
|
bool component_referenced(model &Model, std::uint32_t const Component) {
|
|
auto found{false};
|
|
auto const check = [Component, &found](std::uint32_t const &Index) { found |= (Index == Component); };
|
|
for (auto const &record : Model.components)
|
|
visit_payload_components(Model, record, check);
|
|
return found;
|
|
}
|
|
|
|
// a range of components after the component taken out of the table
|
|
void close_range(std::uint32_t &First, std::uint32_t &Count, std::uint32_t const Component) {
|
|
if (First > Component)
|
|
--First;
|
|
else if (std::size_t{Component} < std::size_t{First} + Count)
|
|
--Count;
|
|
}
|
|
|
|
// the component taken out of the table: the ranges of the nodes and the vertex groups and the references in the data of the others
|
|
// close up. its data stays where it is, nothing points at it any more
|
|
void erase_component(model &Model, std::uint32_t const Component) {
|
|
Model.components.erase(Model.components.begin() + Component);
|
|
for (auto &node : Model.nodes)
|
|
close_range(node.component_first, node.component_count, Component);
|
|
for (auto &group : Model.vertex_groups)
|
|
close_range(group.component_first, group.component_count, Component);
|
|
auto const close = [Component](std::uint32_t &Index) {
|
|
if ((Index != no_index) && (Index > Component))
|
|
--Index;
|
|
};
|
|
for (auto const &record : Model.components)
|
|
visit_payload_components(Model, record, close);
|
|
}
|
|
|
|
// the components aimed at the node
|
|
std::vector<std::uint32_t> components_of(model const &Model, std::uint32_t const Node) {
|
|
std::vector<std::uint32_t> components;
|
|
for (std::uint32_t idx = 0; idx < Model.components.size(); ++idx) {
|
|
if ((Model.components[idx].kind == target_kind::node) && (Model.components[idx].target == Node))
|
|
components.push_back(idx);
|
|
}
|
|
return components;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
std::uint32_t insert_node(model &Model, std::int32_t const Parent, node_record Node) {
|
|
auto const valid_parent{Parent >= 0 && Parent < static_cast<std::int32_t>(Model.nodes.size())};
|
|
auto const at{valid_parent ? Model.nodes[Parent].subtree_end : static_cast<std::uint32_t>(Model.nodes.size())};
|
|
shift_subtrees(Model, valid_parent ? Parent : -1, at);
|
|
visit_node_references(Model, node_shift{at, false});
|
|
for (auto &skin : Model.skins) {
|
|
if (skin.root >= static_cast<std::int32_t>(at))
|
|
++skin.root;
|
|
}
|
|
Node.parent = valid_parent ? Parent : -1;
|
|
Node.subtree_end = at + 1;
|
|
Model.nodes.insert(Model.nodes.begin() + at, Node);
|
|
if (false == Model.name_index.empty())
|
|
build_name_index(Model);
|
|
if (false == Model.bounds.empty())
|
|
compute_bounds(Model);
|
|
return at;
|
|
}
|
|
|
|
bool node_removable(model &Model, std::uint32_t const Node) {
|
|
if ((Node >= Model.nodes.size()) || (Model.nodes[Node].subtree_end != Node + 1) || (Model.nodes[Node].mesh >= 0) || node_referenced(Model, Node))
|
|
return false;
|
|
auto const components{components_of(Model, Node)};
|
|
return std::ranges::none_of(components, [&Model](std::uint32_t const Component) { return component_referenced(Model, Component); });
|
|
}
|
|
|
|
bool remove_node(model &Model, std::uint32_t const Node) {
|
|
if (false == node_removable(Model, Node))
|
|
return false;
|
|
// its components first, the last of them first so the indices of the others hold
|
|
auto const components{components_of(Model, Node)};
|
|
for (auto component{components.rbegin()}; component != components.rend(); ++component)
|
|
erase_component(Model, *component);
|
|
visit_node_references(Model, node_shift{Node, true});
|
|
for (auto &skin : Model.skins) {
|
|
if (skin.root > static_cast<std::int32_t>(Node))
|
|
--skin.root;
|
|
}
|
|
// the subtrees which held it, and those after it, end one place sooner
|
|
for (auto &node : Model.nodes) {
|
|
if (node.subtree_end > Node)
|
|
--node.subtree_end;
|
|
if (node.parent > static_cast<std::int32_t>(Node))
|
|
--node.parent;
|
|
}
|
|
Model.nodes.erase(Model.nodes.begin() + Node);
|
|
if (false == Model.name_index.empty())
|
|
build_name_index(Model);
|
|
if (false == Model.bounds.empty())
|
|
compute_bounds(Model);
|
|
return true;
|
|
}
|
|
|
|
std::uint32_t add_component(model &Model, std::uint32_t const Type, target_kind const Kind, std::uint32_t const Target, std::span<std::byte const> const Data) {
|
|
// the data of each component starts at a multiple of 4
|
|
Model.component_data.resize((Model.component_data.size() + 3) & ~std::size_t{3});
|
|
component_record record;
|
|
record.type = Type;
|
|
record.kind = Kind;
|
|
record.target = Target;
|
|
record.data_offset = static_cast<std::uint32_t>(Model.component_data.size());
|
|
record.data_size = static_cast<std::uint32_t>(Data.size());
|
|
Model.component_data.insert(Model.component_data.end(), Data.begin(), Data.end());
|
|
auto const index{static_cast<std::uint32_t>(Model.components.size())};
|
|
Model.components.push_back(record);
|
|
auto const list = [index](std::uint32_t &First, std::uint32_t &Count) {
|
|
if (Count == 0) {
|
|
First = index;
|
|
Count = 1;
|
|
}
|
|
};
|
|
if (Kind == target_kind::node && Target < Model.nodes.size())
|
|
list(Model.nodes[Target].component_first, Model.nodes[Target].component_count);
|
|
if (Kind == target_kind::vertex_group && Target < Model.vertex_groups.size())
|
|
list(Model.vertex_groups[Target].component_first, Model.vertex_groups[Target].component_count);
|
|
return index;
|
|
}
|
|
|
|
|
|
} // namespace eu07m
|