context in core

This commit is contained in:
AndreaRigoni
2026-03-21 16:30:56 +00:00
parent cd95f16221
commit 0bff36f8ba
6 changed files with 163 additions and 26 deletions

View File

@@ -10,6 +10,7 @@ set(HEADERS
Macros.h Macros.h
Mpl.h Mpl.h
Object.h Object.h
ObjectsContext.h
Options.h Options.h
Serializable.h Serializable.h
Signal.h Signal.h
@@ -26,6 +27,7 @@ set(SOURCES
Archives.cpp Archives.cpp
Debug.cpp Debug.cpp
Object.cpp Object.cpp
ObjectsContext.cpp
Options.cpp Options.cpp
Serializable.cpp Serializable.cpp
Signal.cpp Signal.cpp

View File

@@ -0,0 +1,47 @@
#include "Core/ObjectsContext.h"
#include <algorithm>
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<Object*>& 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

59
src/Core/ObjectsContext.h Normal file
View File

@@ -0,0 +1,59 @@
#ifndef U_CORE_OBJECTS_CONTEXT_H
#define U_CORE_OBJECTS_CONTEXT_H
#include "Core/Object.h"
#include <vector>
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<Object*>& 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<Object*> m_objects;
};
} // namespace uLib
#endif // U_CORE_OBJECTS_CONTEXT_H

View File

@@ -77,13 +77,13 @@ public:
* @brief Sets the box origin relative to its coordinate system. * @brief Sets the box origin relative to its coordinate system.
* @param v The origin position vector. * @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. * @brief Gets the box origin relative to its coordinate system.
* @return The origin position vector. * @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. * @brief Sets the size of the box.
@@ -101,7 +101,7 @@ public:
* @brief Gets the current size (scale) of the box. * @brief Gets the current size (scale) of the box.
* @return The size vector. * @return The size vector.
*/ */
inline Vector3f GetSize() const { Vector3f GetSize() const {
Vector3f s = this->GetScale(); Vector3f s = this->GetScale();
Vector3f ls = m_LocalT.GetScale(); Vector3f ls = m_LocalT.GetScale();
return Vector3f(s(0) * ls(0), s(1) * ls(1), s(2) * ls(2)); 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 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). * @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); m_LocalT.FlipAxes(first, second);
} }
@@ -133,7 +133,7 @@ public:
* @param v The local point (4D homogeneous vector). * @param v The local point (4D homogeneous vector).
* @return The transformed point in world space. * @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; return m_LocalT.GetWorldMatrix() * v;
} }
@@ -144,7 +144,7 @@ public:
* @param z Z coordinate in local space. * @param z Z coordinate in local space.
* @return The transformed point in world 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)); return this->GetWorldPoint(Vector4f(x, y, z, 1));
} }
@@ -153,7 +153,7 @@ public:
* @param v The world point (4D homogeneous vector). * @param v The world point (4D homogeneous vector).
* @return The transformed point in box-local space. * @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; return m_LocalT.GetWorldMatrix().inverse() * v;
} }
@@ -164,7 +164,7 @@ public:
* @param z Z coordinate in world space. * @param z Z coordinate in world space.
* @return The transformed point in box-local 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)); return this->GetLocalPoint(Vector4f(x, y, z, 1));
} }

View File

@@ -81,12 +81,12 @@ public:
Eigen::Affine3f& GetTransform() { return m_T; } 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; } void SetMatrix (Matrix4f mat) { m_T.matrix() = mat; }
inline Matrix4f GetMatrix() const { return m_T.matrix(); } Matrix4f GetMatrix() const { return m_T.matrix(); }
Matrix4f GetWorldMatrix() const Matrix4f GetWorldMatrix() const
{ {
@@ -94,45 +94,45 @@ public:
else return m_Parent->GetWorldMatrix() * m_T.matrix(); // T = B * A // 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(), return Vector3f(m_T.linear().col(0).norm(),
m_T.linear().col(1).norm(), m_T.linear().col(1).norm(),
m_T.linear().col(2).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 ) axis.normalize(); // prehaps not necessary ( see eigens )
Eigen::AngleAxisf ax(angle,axis); Eigen::AngleAxisf ax(angle,axis);
this->m_T.rotate(Eigen::Quaternion<float>(ax)); this->m_T.rotate(Eigen::Quaternion<float>(ax));
} }
inline void Rotate(const Vector3f euler_axis) { void Rotate(const Vector3f euler_axis) {
float angle = euler_axis.norm(); float angle = euler_axis.norm();
Rotate(angle,euler_axis); 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<float>(q)); } { this->m_T.rotate(Eigen::Quaternion<float>(q)); }
inline void EulerYZYRotate(const Vector3f e) { void EulerYZYRotate(const Vector3f e) {
Matrix3f mat; Matrix3f mat;
mat = Eigen::AngleAxisf(e.x(), Vector3f::UnitY()) mat = Eigen::AngleAxisf(e.x(), Vector3f::UnitY())
* Eigen::AngleAxisf(e.y(), Vector3f::UnitZ()) * Eigen::AngleAxisf(e.y(), Vector3f::UnitZ())
@@ -140,7 +140,7 @@ public:
m_T.rotate(mat); m_T.rotate(mat);
} }
inline void FlipAxes(int first, int second) void FlipAxes(int first, int second)
{ {
Matrix3f mat = Matrix3f::Identity(); Matrix3f mat = Matrix3f::Identity();
mat.col(first).swap(mat.col(second)); mat.col(first).swap(mat.col(second));

View File

@@ -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; END_TESTING;
} }