From 0bff36f8ba6eef6f54341329424006f8ad6fab35 Mon Sep 17 00:00:00 2001 From: AndreaRigoni Date: Sat, 21 Mar 2026 16:30:56 +0000 Subject: [PATCH] context in core --- src/Core/CMakeLists.txt | 2 + src/Core/ObjectsContext.cpp | 47 +++++++++++++++++++++ src/Core/ObjectsContext.h | 59 +++++++++++++++++++++++++++ src/Math/ContainerBox.h | 16 ++++---- src/Math/Transform.h | 36 ++++++++-------- src/Math/testing/ContainerBoxTest.cpp | 29 +++++++++++++ 6 files changed, 163 insertions(+), 26 deletions(-) create mode 100644 src/Core/ObjectsContext.cpp create mode 100644 src/Core/ObjectsContext.h diff --git a/src/Core/CMakeLists.txt b/src/Core/CMakeLists.txt index 98de62a..cb18814 100644 --- a/src/Core/CMakeLists.txt +++ b/src/Core/CMakeLists.txt @@ -10,6 +10,7 @@ set(HEADERS Macros.h Mpl.h Object.h + ObjectsContext.h Options.h Serializable.h Signal.h @@ -26,6 +27,7 @@ set(SOURCES Archives.cpp Debug.cpp Object.cpp + ObjectsContext.cpp Options.cpp Serializable.cpp Signal.cpp diff --git a/src/Core/ObjectsContext.cpp b/src/Core/ObjectsContext.cpp new file mode 100644 index 0000000..d14f8a6 --- /dev/null +++ b/src/Core/ObjectsContext.cpp @@ -0,0 +1,47 @@ +#include "Core/ObjectsContext.h" +#include + +namespace uLib { + +ObjectsContext::ObjectsContext() : Object() {} + +ObjectsContext::~ObjectsContext() {} + +void ObjectsContext::AddObject(Object* obj) { + if (obj && std::find(m_objects.begin(), m_objects.end(), obj) == m_objects.end()) { + m_objects.push_back(obj); + this->Updated(); // Signal that the context has been updated + } +} + +void ObjectsContext::RemoveObject(Object* obj) { + auto it = std::find(m_objects.begin(), m_objects.end(), obj); + if (it != m_objects.end()) { + m_objects.erase(it); + this->Updated(); // Signal that the context has been updated + } +} + +void ObjectsContext::Clear() { + if (!m_objects.empty()) { + m_objects.clear(); + this->Updated(); + } +} + +const std::vector& ObjectsContext::GetObjects() const { + return m_objects; +} + +size_t ObjectsContext::GetCount() const { + return m_objects.size(); +} + +Object* ObjectsContext::GetObject(size_t index) const { + if (index < m_objects.size()) { + return m_objects[index]; + } + return nullptr; +} + +} // namespace uLib diff --git a/src/Core/ObjectsContext.h b/src/Core/ObjectsContext.h new file mode 100644 index 0000000..1dcfe83 --- /dev/null +++ b/src/Core/ObjectsContext.h @@ -0,0 +1,59 @@ +#ifndef U_CORE_OBJECTS_CONTEXT_H +#define U_CORE_OBJECTS_CONTEXT_H + +#include "Core/Object.h" +#include + +namespace uLib { + +/** + * @brief ObjectsContext represents a collection of Object instances. + */ +class ObjectsContext : public Object { +public: + ObjectsContext(); + virtual ~ObjectsContext(); + + /** + * @brief Adds an object to the context. + * @param obj Pointer to the object to add. + */ + void AddObject(Object* obj); + + /** + * @brief Removes an object from the context. + * @param obj Pointer to the object to remove. + */ + void RemoveObject(Object* obj); + + /** + * @brief Clears all objects from the context. + */ + void Clear(); + + /** + * @brief Returns the collection of objects. + * @return Const reference to the vector of object pointers. + */ + const std::vector& GetObjects() const; + + /** + * @brief Returns the number of objects in the context. + * @return Size of the collection. + */ + size_t GetCount() const; + + /** + * @brief Returns an object by index. + * @param index The index of the object. + * @return Pointer to the object or nullptr if index is out of bounds. + */ + Object* GetObject(size_t index) const; + +private: + std::vector m_objects; +}; + +} // namespace uLib + +#endif // U_CORE_OBJECTS_CONTEXT_H diff --git a/src/Math/ContainerBox.h b/src/Math/ContainerBox.h index 588225a..09b865f 100644 --- a/src/Math/ContainerBox.h +++ b/src/Math/ContainerBox.h @@ -77,13 +77,13 @@ public: * @brief Sets the box origin relative to its coordinate system. * @param v The origin position vector. */ - inline void SetOrigin(const Vector3f &v) { m_LocalT.SetPosition(v); } + void SetOrigin(const Vector3f &v) { m_LocalT.SetPosition(v); } /** * @brief Gets the box origin relative to its coordinate system. * @return The origin position vector. */ - inline Vector3f GetOrigin() const { return m_LocalT.GetPosition(); } + Vector3f GetOrigin() const { return m_LocalT.GetPosition(); } /** * @brief Sets the size of the box. @@ -101,7 +101,7 @@ public: * @brief Gets the current size (scale) of the box. * @return The size vector. */ - inline Vector3f GetSize() const { + Vector3f GetSize() const { Vector3f s = this->GetScale(); Vector3f ls = m_LocalT.GetScale(); return Vector3f(s(0) * ls(0), s(1) * ls(1), s(2) * ls(2)); @@ -112,7 +112,7 @@ public: * @param first Index of the first axis (0=X, 1=Y, 2=Z). * @param second Index of the second axis (0=X, 1=Y, 2=Z). */ - inline void FlipLocalAxes(int first, int second) { + void FlipLocalAxes(int first, int second) { m_LocalT.FlipAxes(first, second); } @@ -133,7 +133,7 @@ public: * @param v The local point (4D homogeneous vector). * @return The transformed point in world space. */ - inline Vector4f GetWorldPoint(const Vector4f &v) const { + Vector4f GetWorldPoint(const Vector4f &v) const { return m_LocalT.GetWorldMatrix() * v; } @@ -144,7 +144,7 @@ public: * @param z Z coordinate in local space. * @return The transformed point in world space. */ - inline Vector4f GetWorldPoint(const float x, const float y, const float z) { + Vector4f GetWorldPoint(const float x, const float y, const float z) { return this->GetWorldPoint(Vector4f(x, y, z, 1)); } @@ -153,7 +153,7 @@ public: * @param v The world point (4D homogeneous vector). * @return The transformed point in box-local space. */ - inline Vector4f GetLocalPoint(const Vector4f &v) const { + Vector4f GetLocalPoint(const Vector4f &v) const { return m_LocalT.GetWorldMatrix().inverse() * v; } @@ -164,7 +164,7 @@ public: * @param z Z coordinate in world space. * @return The transformed point in box-local space. */ - inline Vector4f GetLocalPoint(const float x, const float y, const float z) { + Vector4f GetLocalPoint(const float x, const float y, const float z) { return this->GetLocalPoint(Vector4f(x, y, z, 1)); } diff --git a/src/Math/Transform.h b/src/Math/Transform.h index c267581..d640eee 100644 --- a/src/Math/Transform.h +++ b/src/Math/Transform.h @@ -81,12 +81,12 @@ public: Eigen::Affine3f& GetTransform() { return m_T; } - inline AffineTransform *GetParent() const { return this->m_Parent; } + AffineTransform *GetParent() const { return this->m_Parent; } - inline void SetParent(AffineTransform *name) { this->m_Parent = name; } + void SetParent(AffineTransform *name) { this->m_Parent = name; } - inline void SetMatrix (Matrix4f mat) { m_T.matrix() = mat; } - inline Matrix4f GetMatrix() const { return m_T.matrix(); } + void SetMatrix (Matrix4f mat) { m_T.matrix() = mat; } + Matrix4f GetMatrix() const { return m_T.matrix(); } Matrix4f GetWorldMatrix() const { @@ -94,45 +94,45 @@ public: else return m_Parent->GetWorldMatrix() * m_T.matrix(); // T = B * A // } - inline void SetPosition(const Vector3f v) { this->m_T.translation() = v; } + void SetPosition(const Vector3f v) { this->m_T.translation() = v; } - inline Vector3f GetPosition() const { return this->m_T.translation(); } + Vector3f GetPosition() const { return this->m_T.translation(); } - inline void SetRotation(const Matrix3f m) { this->m_T.linear() = m; } + void SetRotation(const Matrix3f m) { this->m_T.linear() = m; } - inline Matrix3f GetRotation() const { return this->m_T.rotation(); } + Matrix3f GetRotation() const { return this->m_T.rotation(); } - inline void Translate(const Vector3f v) { this->m_T.pretranslate(v); } + void Translate(const Vector3f v) { this->m_T.pretranslate(v); } - inline void Scale(const Vector3f v) { this->m_T.scale(v); } + void Scale(const Vector3f v) { this->m_T.scale(v); } - inline Vector3f GetScale() const { + Vector3f GetScale() const { return Vector3f(m_T.linear().col(0).norm(), m_T.linear().col(1).norm(), m_T.linear().col(2).norm()); } - inline void Rotate(const Matrix3f m) { this->m_T.rotate(m); } + void Rotate(const Matrix3f m) { this->m_T.rotate(m); } - inline void Rotate(const float angle, Vector3f axis) + void Rotate(const float angle, Vector3f axis) { axis.normalize(); // prehaps not necessary ( see eigens ) Eigen::AngleAxisf ax(angle,axis); this->m_T.rotate(Eigen::Quaternion(ax)); } - inline void Rotate(const Vector3f euler_axis) { + void Rotate(const Vector3f euler_axis) { float angle = euler_axis.norm(); Rotate(angle,euler_axis); } - inline void PreRotate(const Matrix3f m) { this->m_T.prerotate(m); } + void PreRotate(const Matrix3f m) { this->m_T.prerotate(m); } - inline void QuaternionRotate(const Vector4f q) + void QuaternionRotate(const Vector4f q) { this->m_T.rotate(Eigen::Quaternion(q)); } - inline void EulerYZYRotate(const Vector3f e) { + void EulerYZYRotate(const Vector3f e) { Matrix3f mat; mat = Eigen::AngleAxisf(e.x(), Vector3f::UnitY()) * Eigen::AngleAxisf(e.y(), Vector3f::UnitZ()) @@ -140,7 +140,7 @@ public: m_T.rotate(mat); } - inline void FlipAxes(int first, int second) + void FlipAxes(int first, int second) { Matrix3f mat = Matrix3f::Identity(); mat.col(first).swap(mat.col(second)); diff --git a/src/Math/testing/ContainerBoxTest.cpp b/src/Math/testing/ContainerBoxTest.cpp index dec6b09..61539d5 100644 --- a/src/Math/testing/ContainerBoxTest.cpp +++ b/src/Math/testing/ContainerBoxTest.cpp @@ -162,6 +162,35 @@ int main() + { // test parent-child relationship + ContainerBox Parent; + Parent.SetOrigin(Vector3f(1,1,1)); + Parent.SetSize(Vector3f(2,2,2)); + + ContainerBox Child; + Child.SetParent(&Parent); + Child.SetOrigin(Vector3f(1,1,1)); + Child.SetSize(Vector3f(2,2,2)); + + HPoint3f pt = Child.GetLocalPoint(HPoint3f(0,0,0)); + HPoint3f wp = Child.GetWorldPoint(pt); + TEST0( Vector4f0(wp - HPoint3f(0,0,0)) ); + + pt = HPoint3f(1,1,1); + wp = Child.GetWorldPoint(pt); + TEST0( Vector4f0(wp - HPoint3f(2,2,2)) ); + + pt = HPoint3f(1,2,3); + wp = Child.GetWorldPoint(pt); + std::cout << "Child.GetWorldPoint(HPoint3f(1,2,3)): " << wp << std::endl; + TEST0( Vector4f0(wp - HPoint3f(2,3,4)) ); + + pt = HPoint3f(1,2,3); + wp = Child.GetWorldPoint(pt); + TEST0( Vector4f0(wp - HPoint3f(2,3,4)) ); + } + + END_TESTING; }