DetectorChamber vtk handler
This commit is contained in:
@@ -29,63 +29,147 @@
|
||||
#define U_CONTAINERBOX_H
|
||||
|
||||
#include "Geometry.h"
|
||||
#include "Math/Dense.h"
|
||||
#include "Math/Transform.h"
|
||||
#include <utility>
|
||||
|
||||
|
||||
namespace uLib {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Represents an oriented bounding box (OBB) within a hierarchical transformation system.
|
||||
*
|
||||
* ContainerBox inherits from AffineTransform, which defines its parent coordinate system.
|
||||
* It contains an internal local transformation (m_LocalT) that defines the box's
|
||||
* specific origin and size relative to its own coordinate system.
|
||||
*/
|
||||
class ContainerBox : public AffineTransform {
|
||||
public:
|
||||
ContainerBox() : m_LocalT(this) {}
|
||||
|
||||
typedef AffineTransform BaseClass;
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Default constructor.
|
||||
* Initializes the local transformation with this instance as its parent.
|
||||
*/
|
||||
ContainerBox() :
|
||||
m_LocalT(this) // BaseClass is Parent of m_LocalTransform
|
||||
{}
|
||||
|
||||
/**
|
||||
* @brief Copy constructor.
|
||||
* @param copy The ContainerBox instance to copy from.
|
||||
*/
|
||||
ContainerBox(const ContainerBox ©) :
|
||||
m_LocalT(this),
|
||||
m_LocalT(this), // BaseClass is Parent of m_LocalTransform
|
||||
AffineTransform(copy)
|
||||
{
|
||||
// FIX for performance //
|
||||
this->SetOrigin(copy.GetOrigin());
|
||||
this->SetSize(copy.GetSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* @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); }
|
||||
|
||||
/**
|
||||
* @brief Gets the box origin relative to its coordinate system.
|
||||
* @return The origin position vector.
|
||||
*/
|
||||
inline Vector3f GetOrigin() const { return m_LocalT.GetPosition(); }
|
||||
|
||||
/**
|
||||
* @brief Sets the size of the box.
|
||||
* Re-initializes the local transformation and applies the new scale.
|
||||
* @param v The size vector (width, height, depth).
|
||||
*/
|
||||
void SetSize(const Vector3f &v) {
|
||||
Vector3f pos = this->GetOrigin();
|
||||
m_LocalT = AffineTransform(this);
|
||||
m_LocalT = AffineTransform(this); // regenerate local transform
|
||||
m_LocalT.Scale(v);
|
||||
m_LocalT.SetPosition(pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Gets the current size (scale) of the box.
|
||||
* @return The size vector.
|
||||
*/
|
||||
inline Vector3f GetSize() const { return m_LocalT.GetScale(); }
|
||||
|
||||
// FIX... //
|
||||
/**
|
||||
* @brief Swaps two local axes of the box.
|
||||
* @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)
|
||||
{ m_LocalT.FlipAxes(first,second); }
|
||||
|
||||
/**
|
||||
* @brief Returns the world transformation matrix of the box's volume.
|
||||
* @return A 4x4 transformation matrix.
|
||||
*/
|
||||
Matrix4f GetWorldMatrix() const { return m_LocalT.GetWorldMatrix(); }
|
||||
|
||||
/**
|
||||
* @brief Returns the local transformation matrix of the box's volume.
|
||||
* @return A 4x4 transformation matrix.
|
||||
*/
|
||||
Matrix4f GetLocalMatrix() const { return m_LocalT.GetMatrix(); }
|
||||
|
||||
/**
|
||||
* @brief Transforms a point from box-local space to world space.
|
||||
* @param v The local point (4D homogeneous vector).
|
||||
* @return The transformed point in world space.
|
||||
*/
|
||||
inline Vector4f GetWorldPoint(const Vector4f &v) const {
|
||||
return m_LocalT.GetWorldMatrix() * v;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Transforms a point from box-local space coordinates to world space.
|
||||
* @param x X coordinate in local space.
|
||||
* @param y Y coordinate in local space.
|
||||
* @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) {
|
||||
return this->GetWorldPoint(Vector4f(x,y,z,1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Transforms a point from world space to box-local space.
|
||||
* @param v The world point (4D homogeneous vector).
|
||||
* @return The transformed point in box-local space.
|
||||
*/
|
||||
inline Vector4f GetLocalPoint(const Vector4f &v) const {
|
||||
return m_LocalT.GetWorldMatrix().inverse() * v;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Transforms a point from world space coordinates to box-local space.
|
||||
* @param x X coordinate in world space.
|
||||
* @param y Y coordinate in world space.
|
||||
* @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) {
|
||||
return this->GetLocalPoint(Vector4f(x,y,z,1));
|
||||
}
|
||||
|
||||
/** Translate using transformation chain */
|
||||
using BaseClass::Translate;
|
||||
|
||||
protected:
|
||||
/** Rotate using transformation chain */
|
||||
using BaseClass::Rotate;
|
||||
|
||||
/** Scale using transformation chain */
|
||||
using BaseClass::Scale;
|
||||
|
||||
|
||||
private:
|
||||
AffineTransform m_LocalT;
|
||||
};
|
||||
|
||||
@@ -36,7 +36,7 @@ namespace uLib {
|
||||
class Geometry : public AffineTransform {
|
||||
public:
|
||||
|
||||
inline Vector4f GetWorldPoint(const Vector4f &v) const {
|
||||
inline Vector4f GetWorldPoint(const Vector4f v) const {
|
||||
return this->GetWorldMatrix() * v;
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ public:
|
||||
return this->GetWorldPoint(Vector4f(x,y,z,1));
|
||||
}
|
||||
|
||||
inline Vector4f GetLocalPoint(const Vector4f &v) const {
|
||||
inline Vector4f GetLocalPoint(const Vector4f v) const {
|
||||
return this->GetWorldMatrix().inverse() * v;
|
||||
}
|
||||
|
||||
|
||||
@@ -84,8 +84,8 @@ public:
|
||||
|
||||
inline void SetParent(AffineTransform *name) { this->m_Parent = name; }
|
||||
|
||||
inline void SetMatrix (Matrix4f &mat) { m_T.matrix() = mat; }
|
||||
inline Matrix4f& GetMatrix () { return m_T.matrix(); }
|
||||
inline void SetMatrix (Matrix4f mat) { m_T.matrix() = mat; }
|
||||
inline Matrix4f GetMatrix() const { return m_T.matrix(); }
|
||||
|
||||
Matrix4f GetWorldMatrix() const
|
||||
{
|
||||
@@ -93,22 +93,22 @@ public:
|
||||
else return m_Parent->GetWorldMatrix() * m_T.matrix(); // T = B * A //
|
||||
}
|
||||
|
||||
inline void SetPosition(const Vector3f &v) { this->m_T.translation() = v; }
|
||||
inline void SetPosition(const Vector3f v) { this->m_T.translation() = v; }
|
||||
|
||||
inline Vector3f GetPosition() const { return this->m_T.translation(); }
|
||||
|
||||
inline void SetRotation(const Matrix3f &m) { this->m_T.linear() = m; }
|
||||
inline void SetRotation(const Matrix3f m) { this->m_T.linear() = m; }
|
||||
|
||||
inline Matrix3f GetRotation() const { return this->m_T.rotation(); }
|
||||
|
||||
inline void Translate(const Vector3f &v) { this->m_T.translate(v); }
|
||||
inline void Translate(const Vector3f v) { this->m_T.translate(v); }
|
||||
|
||||
inline void Scale(const Vector3f &v) { this->m_T.scale(v); }
|
||||
inline void Scale(const Vector3f v) { this->m_T.scale(v); }
|
||||
|
||||
inline Vector3f GetScale() const { return this->m_T.linear() * Vector3f(1,1,1); } // FIXXXXXXX
|
||||
|
||||
|
||||
inline void Rotate(const Matrix3f &m) { this->m_T.rotate(m); }
|
||||
inline void Rotate(const Matrix3f m) { this->m_T.rotate(m); }
|
||||
|
||||
inline void Rotate(const float angle, Vector3f axis)
|
||||
{
|
||||
@@ -122,12 +122,12 @@ public:
|
||||
Rotate(angle,euler_axis);
|
||||
}
|
||||
|
||||
inline void PreRotate(const Matrix3f &m) { this->m_T.prerotate(m); }
|
||||
inline void PreRotate(const Matrix3f m) { this->m_T.prerotate(m); }
|
||||
|
||||
inline void QuaternionRotate(const Vector4f &q)
|
||||
inline void QuaternionRotate(const Vector4f q)
|
||||
{ this->m_T.rotate(Eigen::Quaternion<float>(q)); }
|
||||
|
||||
inline void EulerYZYRotate(const Vector3f &e) {
|
||||
inline void EulerYZYRotate(const Vector3f e) {
|
||||
Matrix3f mat;
|
||||
mat = Eigen::AngleAxisf(e.x(), Vector3f::UnitY())
|
||||
* Eigen::AngleAxisf(e.y(), Vector3f::UnitZ())
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#include "Math/Dense.h"
|
||||
#include "Math/ContainerBox.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
|
||||
@@ -52,41 +53,82 @@ int main()
|
||||
|
||||
BEGIN_TESTING(Math ContainerBox);
|
||||
|
||||
ContainerBox Cnt;
|
||||
|
||||
// // Local transform:
|
||||
Cnt.SetOrigin(Vector3f(-1,-1,-1));
|
||||
Cnt.SetSize(Vector3f(2,2,2)); // scaling //
|
||||
std::cout << "Container scale is: " << Cnt.GetSize().transpose() << "\n";
|
||||
std::cout << "Container scale is: " << Cnt.GetSize().transpose() << "\n";
|
||||
TEST0( Vector4f0(Cnt.GetSize().homogeneous() - HVector3f(2,2,2)) );
|
||||
{
|
||||
ContainerBox Cnt;
|
||||
Cnt.SetOrigin(Vector3f(0,0,0));
|
||||
Cnt.SetSize(Vector3f(2,2,2));
|
||||
TEST0( Vector4f0(Cnt.GetOrigin().homogeneous() - HVector3f(0,0,0)) );
|
||||
TEST0( Vector4f0(Cnt.GetSize().homogeneous() - HVector3f(2,2,2)) );
|
||||
|
||||
|
||||
ContainerBox Box;
|
||||
HPoint3f pt = Cnt.GetLocalPoint(HPoint3f(0,0,0));
|
||||
HPoint3f wp = Cnt.GetWorldPoint(pt);
|
||||
TEST0( Vector4f0(wp - HPoint3f(0,0,0)) );
|
||||
|
||||
Box.SetPosition(Vector3f(1,1,1));
|
||||
Box.SetSize(Vector3f(2,2,2));
|
||||
Box.EulerYZYRotate(Vector3f(0,0,0));
|
||||
HPoint3f pt = Box.GetLocalPoint(HPoint3f(2,3,2));
|
||||
HPoint3f wp = Box.GetWorldPoint(pt);
|
||||
TEST0( Vector4f0(wp - HPoint3f(2,3,2)) );
|
||||
HPoint3f pt2 = Cnt.GetLocalPoint(HPoint3f(2,2,2));
|
||||
HPoint3f wp2 = Cnt.GetWorldPoint(pt2);
|
||||
TEST0( Vector4f0(wp2 - HPoint3f(2,2,2)) );
|
||||
|
||||
HPoint3f pt3 = Cnt.GetLocalPoint(HPoint3f(1,1,1));
|
||||
HPoint3f wp3 = Cnt.GetWorldPoint(pt3);
|
||||
TEST0( Vector4f0(wp3 - HPoint3f(1,1,1)) );
|
||||
|
||||
//// // Global
|
||||
// Cnt.SetPosition(Vector3f(1,1,1));
|
||||
// Cnt.EulerYZYRotate(Vector3f(M_PI_2,M_PI_2,0));
|
||||
// HPoint3f p = Cnt.GetWorldPoint(1,1,1);
|
||||
// //std::cout << p.transpose() << "\n";
|
||||
// TEST0( Vector4f0(p - HVector3f(2,1,2)) );
|
||||
// p = Cnt.GetWorldPoint(1,2,3);
|
||||
// //std::cout << p.transpose() << "\n";
|
||||
// TEST0( Vector4f0(p - HVector3f(4,1,3)) );
|
||||
HPoint3f pt4 = Cnt.GetLocalPoint(HPoint3f(1,2,3));
|
||||
HPoint3f wp4 = Cnt.GetWorldPoint(pt4);
|
||||
TEST0( Vector4f0(wp4 - HPoint3f(1,2,3)) );
|
||||
}
|
||||
|
||||
{
|
||||
ContainerBox Cnt;
|
||||
Cnt.SetOrigin(Vector3f(0,0,0));
|
||||
Cnt.SetSize(Vector3f(2,2,2));
|
||||
Cnt.EulerYZYRotate(Vector3f(M_PI,0,0));
|
||||
|
||||
HPoint3f pt = Cnt.GetLocalPoint(HPoint3f(0,0,0));
|
||||
HPoint3f wp = Cnt.GetWorldPoint(pt);
|
||||
TEST0( Vector4f0(wp - HPoint3f(0,0,0)) );
|
||||
|
||||
// // scaling //
|
||||
HPoint3f pt2 = Cnt.GetLocalPoint(HPoint3f(2,2,2));
|
||||
HPoint3f wp2 = Cnt.GetWorldPoint(pt2);
|
||||
TEST0( Vector4f0(wp2 - HPoint3f(2,2,2)) );
|
||||
|
||||
pt2 = HPoint3f(1,1,1);
|
||||
wp2 = Cnt.GetWorldPoint(pt2);
|
||||
TEST0( Vector4f0(wp2 - HPoint3f(-2,2,-2)) );
|
||||
|
||||
pt2 = HPoint3f(1,2,3);
|
||||
wp2 = Cnt.GetWorldPoint(pt2);
|
||||
TEST0( Vector4f0(wp2 - HPoint3f(-2,4,-6)) );
|
||||
}
|
||||
|
||||
{
|
||||
ContainerBox Cnt;
|
||||
Cnt.SetOrigin(Vector3f(-1,-1,-1));
|
||||
Cnt.SetSize(Vector3f(2,2,2)); // scaling //
|
||||
|
||||
HPoint3f pt2 = HPoint3f(.5,.5,.5);
|
||||
HPoint3f wp2 = Cnt.GetWorldPoint(pt2);
|
||||
TEST0( Vector4f0(wp2 - HPoint3f(0,0,0)) );
|
||||
|
||||
pt2 = HPoint3f(0,0,0);
|
||||
wp2 = Cnt.GetWorldPoint(pt2);
|
||||
TEST0( Vector4f0(wp2 - HPoint3f(-1,-1,-1)) );
|
||||
|
||||
Cnt.EulerYZYRotate(Vector3f(M_PI,0,0));
|
||||
pt2 = HPoint3f(0,0,0);
|
||||
wp2 = Cnt.GetWorldPoint(pt2);
|
||||
TEST0( Vector4f0(wp2 - HPoint3f(1,-1,1)) );
|
||||
}
|
||||
|
||||
{
|
||||
ContainerBox Box;
|
||||
Box.SetPosition(Vector3f(1,1,1));
|
||||
Box.SetSize(Vector3f(2,2,2));
|
||||
Box.EulerYZYRotate(Vector3f(0,0,0));
|
||||
HPoint3f pt = Box.GetLocalPoint(HPoint3f(2,3,2));
|
||||
HPoint3f wp = Box.GetWorldPoint(pt);
|
||||
TEST0( Vector4f0(wp - HPoint3f(2,3,2)) );
|
||||
}
|
||||
|
||||
END_TESTING;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user