VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
graphNode.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Created by Arnis Lektauers on 05.09.2025.
5//
6#pragma once
7
8#include <functional>
9#include <string>
10#include <vector>
11
12#include "core/eventHandler.h"
13#include "core/tags.h"
15
16namespace visutwin::canvas
17{
27 class GraphNode : public EventHandler
28 {
29 public:
30 explicit GraphNode(const std::string& name = "Untitled")
31 : _name(name), _tags(this), _localScale(1, 1, 1),
32 _localTransform(Matrix4::identity()), _worldTransform(Matrix4::identity()) {}
33 virtual ~GraphNode();
34
35 const std::string& name() const { return _name; }
36 void setName(const std::string& name) { _name = name; }
37
39 void setRotation(const Quaternion& rotation);
41
42 const Matrix4& worldTransform();
43
44 const Vector3& localPosition() const { return _localPosition; }
45 const Quaternion& localRotation() const { return _localRotation; }
46 const Vector3& localScale() const { return _localScale; }
47 void setLocalScale(float x, float y, float z);
48 void setLocalScale(const Vector3& scale);
49
50 void setLocalEulerAngles(float x, float y, float z);
51
56 void rotateLocal(float x, float y, float z);
57
62 void translateLocal(float x, float y, float z);
63
64 void setLocalPosition(float x, float y, float z);
66
72 const Vector3 position();
73
74 void setPosition(float x, float y, float z);
75 void setPosition(const Vector3& position);
76
77 void addChild(GraphNode* node);
78
79 const std::vector<GraphNode*>& children() const { return _children; }
80
81 GraphNode* findByName(const std::string& name);
82
84 std::vector<GraphNode*> find(const std::function<bool(GraphNode*)>& predicate);
85
86 bool isDescendantOf(const GraphNode* node) const;
87
88 GraphNode* parent() const { return _parent; }
89
90 void removeChild(GraphNode* child);
91
95 void remove();
96
97 bool enabled() const { return _enabled && _enabledInHierarchy; }
98
103 bool enabledLocal() const { return _enabled; }
104
109 void setEnabled(bool value);
110
115 void setEnabledInHierarchy(bool value) { _enabledInHierarchy = value; }
116
117 int aabbVer() const { return _aabbVer;}
118 float worldScaleSign();
119
120 protected:
121 virtual void onHierarchyStateChanged(bool enabled);
122
124
125 private:
126 void dirtifyLocal();
127
128 void dirtifyWorld();
129
130 void unfreezeParentToRoot();
131
132 void dirtifyWorldInternal();
133
134 void sync();
135
136 void fireOnHierarchy(const std::string& name, const std::string& nameHierarchy, GraphNode* parent);
137
138 void prepareInsertChild(GraphNode* node);
139
140 void onInsertChild(GraphNode* node);
141
142 void updateGraphDepth();
143
144 std::string _name;
145
146 Tags _tags;
147
148 // Local space properties of transform
149 Vector3 _localPosition;
150 Quaternion _localRotation;
151 Vector3 _localScale;
152
153 Vector3 _position;
154
155 // Transform matrices
156 Matrix4 _localTransform;
157 Matrix4 _worldTransform;
158
159 // Version tracking
160 int _aabbVer = 0;
161 int _worldScaleSign = 0;
162
163 GraphNode* _parent = nullptr;
164 std::vector<GraphNode*> _children;
165 int _graphDepth = 0;
166
167 bool _dirtyLocal = false;
168 bool _dirtyWorld = false;
169 bool _dirtyNormal = true;
170 bool _frozen = false;
171
172 bool _scaleCompensation = false;
173
174 bool _enabled = true;
175 bool _enabledInHierarchy = false;
176 };
177}
Hierarchical scene graph node with local/world transforms and parent-child relationships.
Definition graphNode.h:28
GraphNode * findByName(const std::string &name)
bool isDescendantOf(const GraphNode *node) const
void setPosition(float x, float y, float z)
GraphNode(const std::string &name="Untitled")
Definition graphNode.h:30
const Vector3 & localScale() const
Definition graphNode.h:46
void notifyHierarchyStateChanged(GraphNode *node, bool enabled)
void setLocalEulerAngles(float x, float y, float z)
void setLocalPosition(float x, float y, float z)
void addChild(GraphNode *node)
const Quaternion & localRotation() const
Definition graphNode.h:45
void setEnabledInHierarchy(bool value)
Definition graphNode.h:115
const Vector3 & localPosition() const
Definition graphNode.h:44
void setName(const std::string &name)
Definition graphNode.h:36
virtual void onHierarchyStateChanged(bool enabled)
std::vector< GraphNode * > find(const std::function< bool(GraphNode *)> &predicate)
Find all descendants (and self) matching a predicate.
void translateLocal(float x, float y, float z)
void setRotation(const Quaternion &rotation)
Definition graphNode.cpp:65
const std::vector< GraphNode * > & children() const
Definition graphNode.h:79
GraphNode * parent() const
Definition graphNode.h:88
void removeChild(GraphNode *child)
const Matrix4 & worldTransform()
Definition graphNode.cpp:86
const std::string & name() const
Definition graphNode.h:35
void setEnabled(bool value)
void setLocalRotation(const Quaternion &rotation)
Definition graphNode.cpp:77
void rotateLocal(float x, float y, float z)
void setLocalScale(float x, float y, float z)
4x4 column-major transformation matrix with SIMD acceleration.
Definition matrix4.h:31
Unit quaternion for rotation representation with SIMD-accelerated slerp and multiply.
Definition quaternion.h:20
3D vector for positions, directions, and normals with multi-backend SIMD acceleration.
Definition vector3.h:29