This commit is contained in:
you2xie
2018-11-27 11:06:44 +01:00
parent 43628b8f74
commit 2fb9a6be67
4 changed files with 152 additions and 94 deletions

View File

@@ -0,0 +1,11 @@
#include "SphereSystemSimulator.h"
std::function<float(float)> SphereSystemSimulator::m_Kernels[5] = {
[](float x) {return 1.0f; }, // Constant, m_iKernel = 0
[](float x) {return 1.0f - x; }, // Linear, m_iKernel = 1, as given in the exercise Sheet, x = d/2r
[](float x) {return (1.0f - x)*(1.0f - x); }, // Quadratic, m_iKernel = 2
[](float x) {return 1.0f / (x)-1.0f; }, // Weak Electric Charge, m_iKernel = 3
[](float x) {return 1.0f / (x*x) - 1.0f; }, // Electric Charge, m_iKernel = 4
};
// SphereSystemSimulator member functions

View File

@@ -0,0 +1,49 @@
#ifndef SPHSYSTEMSIMULATOR_h
#define SPHSYSTEMSIMULATOR_h
#include "Simulator.h"
//#include "spheresystem.h", add your sphere system header file
#define NAIVEACC 0
#define GRIDACC 1
class SphereSystemSimulator:public Simulator{
public:
// Construtors
SphereSystemSimulator();
// Functions
const char * getTestCasesStr();
void initUI(DrawingUtilitiesClass * DUC);
void reset();
void drawFrame(ID3D11DeviceContext* pd3dImmediateContext);
void notifyCaseChanged(int testCase);
void externalForcesCalculations(float timeElapsed);
void simulateTimestep(float timeStep);
void onClick(int x, int y);
void onMouse(int x, int y);
protected:
// Attributes
Vec3 externalForce;
Point2D m_mouse;
Point2D m_trackmouse;
Point2D m_oldtrackmouse;
float m_fMass;
float m_fRadius;
float m_fForceScaling;
float m_fDamping;
int m_iNumSpheres;
int m_iKernel; // index of the m_Kernels[5], more detials in SphereSystemSimulator.cpp
static std::function<float(float)> m_Kernels[5];
int m_iAccelerator; // switch between NAIVEACC and GRIDACC, (optionally, KDTREEACC, 2)
//SphereSystem * m_pSphereSystem; // add your own sphere system member!
// for Demo 3 only:
// you will need multiple SphereSystem objects to do comparisons in Demo 3
// m_iAccelerator should be ignored.
// SphereSystem * m_pSphereSystemGrid;
};
#endif

View File

@@ -20,10 +20,10 @@ using namespace GamePhysics;
//#define ADAPTIVESTEP //#define ADAPTIVESTEP
#define TEMPLATE_DEMO //#define TEMPLATE_DEMO
//#define MASS_SPRING_SYSTEM //#define MASS_SPRING_SYSTEM
//#define RIGID_BODY_SYSTEM //#define RIGID_BODY_SYSTEM
//#define SPH_SYSTEM #define SPHERE_SYSTEM
#ifdef TEMPLATE_DEMO #ifdef TEMPLATE_DEMO
#include "TemplateSimulator.h" #include "TemplateSimulator.h"
@@ -32,10 +32,10 @@ using namespace GamePhysics;
#include "MassSpringSystemSimulator.h" #include "MassSpringSystemSimulator.h"
#endif #endif
#ifdef RIGID_BODY_SYSTEM #ifdef RIGID_BODY_SYSTEM
//#include "RigidBodySystemSimulator.h" #include "RigidBodySystemSimulator.h"
#endif #endif
#ifdef SPH_SYSTEM #ifdef SPHERE_SYSTEM
//#include "SPHSystemSimulator.h" #include "SphereSystemSimulator.h"
#endif #endif
DrawingUtilitiesClass * g_pDUC; DrawingUtilitiesClass * g_pDUC;
@@ -204,10 +204,8 @@ void CALLBACK OnMouse( bool bLeftButtonDown, bool bRightButtonDown, bool bMiddle
g_pSimulator->onClick(xPos,yPos); g_pSimulator->onClick(xPos,yPos);
} }
else else
{
g_pSimulator->onMouse(xPos, yPos); g_pSimulator->onMouse(xPos, yPos);
} }
}
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
@@ -365,10 +363,10 @@ int main(int argc, char* argv[])
g_pSimulator= new MassSpringSystemSimulator(); g_pSimulator= new MassSpringSystemSimulator();
#endif #endif
#ifdef RIGID_BODY_SYSTEM #ifdef RIGID_BODY_SYSTEM
//g_pSimulator= new RigidBodySystemSimulator(); g_pSimulator= new RigidBodySystemSimulator();
#endif #endif
#ifdef SPH_SYSTEM #ifdef SPHERE_SYSTEM
//g_pSimulator= new SPHSystemSimulator(); g_pSimulator= new SphereSystemSimulator();
#endif #endif
g_pSimulator->reset(); g_pSimulator->reset();

View File

@@ -14,29 +14,29 @@ namespace GamePhysics
// basic inlined vector class // basic inlined vector class
template<class Scalar> template<class Scalar>
class ntlVector4Dim class vector4Dim
{ {
public: public:
//! Constructor //! Constructor
inline ntlVector4Dim() : x(0),y(0),z(0),t(0) {} inline vector4Dim() : x(0),y(0),z(0),t(0) {}
//! Copy-Constructor //! Copy-Constructor
inline ntlVector4Dim ( const ntlVector4Dim<Scalar> &v ) : x(v.x), y(v.y), z(v.z),t(v.t) {} inline vector4Dim ( const vector4Dim<Scalar> &v ) : x(v.x), y(v.y), z(v.z),t(v.t) {}
//! Copy-Constructor //! Copy-Constructor
inline ntlVector4Dim ( const float * v) : x((Scalar)v[0]), y((Scalar)v[1]), z((Scalar)v[2]), t((Scalar)v[3]) {} inline vector4Dim ( const float * v) : x((Scalar)v[0]), y((Scalar)v[1]), z((Scalar)v[2]), t((Scalar)v[3]) {}
//! Copy-Constructor //! Copy-Constructor
inline ntlVector4Dim ( const double * v) : x((Scalar)v[0]), y((Scalar)v[1]), z((Scalar)v[2]), t((Scalar)v[3]) {} inline vector4Dim ( const double * v) : x((Scalar)v[0]), y((Scalar)v[1]), z((Scalar)v[2]), t((Scalar)v[3]) {}
//! Construct a vector from one Scalar //! Construct a vector from one Scalar
inline ntlVector4Dim ( Scalar v) : x(v), y(v), z(v), t(v) {} inline vector4Dim ( Scalar v) : x(v), y(v), z(v), t(v) {}
//! Construct a vector from four Ss //! Construct a vector from four Ss
inline ntlVector4Dim ( Scalar vx, Scalar vy, Scalar vz, Scalar vw) : x(vx), y(vy), z(vz), t(vw) {} inline vector4Dim ( Scalar vx, Scalar vy, Scalar vz, Scalar vw) : x(vx), y(vy), z(vz), t(vw) {}
//! Construct a vector from four Ss //! Construct a vector from four Ss
//inline ntlVector4Dim(DirectX::XMVECTOR &v ); // TODO CHECK! //inline vector4Dim(DirectX::XMVECTOR &v ); // TODO CHECK!
// get address of array for OpenGL // get address of array for OpenGL
Scalar *getAddress() { return value; } Scalar *getAddress() { return value; }
@@ -44,7 +44,7 @@ public:
// Operators // Operators
//! Assignment operator //! Assignment operator
inline const ntlVector4Dim<Scalar>& operator= ( const ntlVector4Dim<Scalar>& v ) { inline const vector4Dim<Scalar>& operator= ( const vector4Dim<Scalar>& v ) {
x = v.x; x = v.x;
y = v.y; y = v.y;
z = v.z; z = v.z;
@@ -52,12 +52,12 @@ public:
return *this; return *this;
} }
//! Assignment operator //! Assignment operator
inline const ntlVector4Dim<Scalar>& operator= ( Scalar s ) { inline const vector4Dim<Scalar>& operator= ( Scalar s ) {
x = y = z = t = s; x = y = z = t = s;
return *this; return *this;
} }
//! Assign and add operator //! Assign and add operator
inline const ntlVector4Dim<Scalar>& operator+= ( const ntlVector4Dim<Scalar>& v ) { inline const vector4Dim<Scalar>& operator+= ( const vector4Dim<Scalar>& v ) {
x += v.x; x += v.x;
y += v.y; y += v.y;
z += v.z; z += v.z;
@@ -65,7 +65,7 @@ public:
return *this; return *this;
} }
//! Assign and add operator //! Assign and add operator
inline const ntlVector4Dim<Scalar>& operator+= ( Scalar s ) { inline const vector4Dim<Scalar>& operator+= ( Scalar s ) {
x += s; x += s;
y += s; y += s;
z += s; z += s;
@@ -73,7 +73,7 @@ public:
return *this; return *this;
} }
//! Assign and sub operator //! Assign and sub operator
inline const ntlVector4Dim<Scalar>& operator-= ( const ntlVector4Dim<Scalar>& v ) { inline const vector4Dim<Scalar>& operator-= ( const vector4Dim<Scalar>& v ) {
x -= v.x; x -= v.x;
y -= v.y; y -= v.y;
z -= v.z; z -= v.z;
@@ -81,7 +81,7 @@ public:
return *this; return *this;
} }
//! Assign and sub operator //! Assign and sub operator
inline const ntlVector4Dim<Scalar>& operator-= ( Scalar s ) { inline const vector4Dim<Scalar>& operator-= ( Scalar s ) {
x -= s; x -= s;
y -= s; y -= s;
z -= s; z -= s;
@@ -89,7 +89,7 @@ public:
return *this; return *this;
} }
//! Assign and mult operator //! Assign and mult operator
inline const ntlVector4Dim<Scalar>& operator*= ( const ntlVector4Dim<Scalar>& v ) { inline const vector4Dim<Scalar>& operator*= ( const vector4Dim<Scalar>& v ) {
x *= v.x; x *= v.x;
y *= v.y; y *= v.y;
z *= v.z; z *= v.z;
@@ -97,7 +97,7 @@ public:
return *this; return *this;
} }
//! Assign and mult operator //! Assign and mult operator
inline const ntlVector4Dim<Scalar>& operator*= ( Scalar s ) { inline const vector4Dim<Scalar>& operator*= ( Scalar s ) {
x *= s; x *= s;
y *= s; y *= s;
z *= s; z *= s;
@@ -105,7 +105,7 @@ public:
return *this; return *this;
} }
//! Assign and div operator //! Assign and div operator
inline const ntlVector4Dim<Scalar>& operator/= ( const ntlVector4Dim<Scalar>& v ) { inline const vector4Dim<Scalar>& operator/= ( const vector4Dim<Scalar>& v ) {
x /= v.x; x /= v.x;
y /= v.y; y /= v.y;
z /= v.z; z /= v.z;
@@ -113,7 +113,7 @@ public:
return *this; return *this;
} }
//! Assign and div operator //! Assign and div operator
inline const ntlVector4Dim<Scalar>& operator/= ( Scalar s ) { inline const vector4Dim<Scalar>& operator/= ( Scalar s ) {
x /= s; x /= s;
y /= s; y /= s;
z /= s; z /= s;
@@ -121,29 +121,29 @@ public:
return *this; return *this;
} }
inline void safeDivide (const ntlVector4Dim<Scalar>& v); inline void safeDivide (const vector4Dim<Scalar>& v);
//! Negation operator //! Negation operator
inline ntlVector4Dim<Scalar> operator- () const { inline vector4Dim<Scalar> operator- () const {
return ntlVector4Dim<Scalar> (-x, -y, -z, -t); return vector4Dim<Scalar> (-x, -y, -z, -t);
} }
// binary operator add // binary operator add
inline ntlVector4Dim<Scalar> operator+ (const ntlVector4Dim<Scalar>&) const; inline vector4Dim<Scalar> operator+ (const vector4Dim<Scalar>&) const;
// binary operator add // binary operator add
inline ntlVector4Dim<Scalar> operator+ (Scalar) const; inline vector4Dim<Scalar> operator+ (Scalar) const;
// binary operator sub // binary operator sub
inline ntlVector4Dim<Scalar> operator- (const ntlVector4Dim<Scalar>&) const; inline vector4Dim<Scalar> operator- (const vector4Dim<Scalar>&) const;
// binary operator sub // binary operator sub
inline ntlVector4Dim<Scalar> operator- (Scalar) const; inline vector4Dim<Scalar> operator- (Scalar) const;
// binary operator mult // binary operator mult
inline ntlVector4Dim<Scalar> operator* (const ntlVector4Dim<Scalar>&) const; inline vector4Dim<Scalar> operator* (const vector4Dim<Scalar>&) const;
// binary operator mult // binary operator mult
inline ntlVector4Dim<Scalar> operator* (Scalar) const; inline vector4Dim<Scalar> operator* (Scalar) const;
// binary operator div // binary operator div
inline ntlVector4Dim<Scalar> operator/ (const ntlVector4Dim<Scalar>&) const; inline vector4Dim<Scalar> operator/ (const vector4Dim<Scalar>&) const;
// binary operator div // binary operator div
inline ntlVector4Dim<Scalar> operator/ (Scalar) const; inline vector4Dim<Scalar> operator/ (Scalar) const;
//! Get smallest component //! Get smallest component
//inline Scalar min() const { return ( x<y ) ? ( ( x<z ) ? x:z ) : ( ( y<z ) ? y:z ); // todo t!!} //inline Scalar min() const { return ( x<y ) ? ( ( x<z ) ? x:z ) : ( ( y<z ) ? y:z ); // todo t!!}
@@ -185,7 +185,7 @@ public:
}; };
// zero element // zero element
static const ntlVector4Dim<Scalar> ZERO; static const vector4Dim<Scalar> ZERO;
protected: protected:
@@ -196,9 +196,9 @@ protected:
//************************************************************************ //************************************************************************
//! Addition operator //! Addition operator
template<class Scalar> template<class Scalar>
inline ntlVector4Dim<Scalar> ntlVector4Dim<Scalar>::operator+ ( const ntlVector4Dim<Scalar> &v) const inline vector4Dim<Scalar> vector4Dim<Scalar>::operator+ ( const vector4Dim<Scalar> &v) const
{ {
return ntlVector4Dim<Scalar> (value[0]+v.value[0], return vector4Dim<Scalar> (value[0]+v.value[0],
value[1]+v.value[1], value[1]+v.value[1],
value[2]+v.value[2], value[2]+v.value[2],
value[3]+v.value[3]); value[3]+v.value[3]);
@@ -206,42 +206,42 @@ inline ntlVector4Dim<Scalar> ntlVector4Dim<Scalar>::operator+ ( const ntlVector4
//! Addition operator //! Addition operator
template<class Scalar> template<class Scalar>
inline ntlVector4Dim<Scalar> inline vector4Dim<Scalar>
ntlVector4Dim<Scalar>::operator+(Scalar s) const vector4Dim<Scalar>::operator+(Scalar s) const
{ {
return ntlVector4Dim<Scalar>(value[0]+s, return vector4Dim<Scalar>(value[0]+s,
value[1]+s, value[1]+s,
value[2]+s, value[2]+s,
value[3]+s); value[3]+s);
} }
template<class Scalar> template<class Scalar>
inline ntlVector4Dim<Scalar> inline vector4Dim<Scalar>
operator+(float s, ntlVector4Dim<Scalar> v) operator+(float s, vector4Dim<Scalar> v)
{ {
return v + s; return v + s;
} }
template<class Scalar> template<class Scalar>
inline ntlVector4Dim<Scalar> inline vector4Dim<Scalar>
operator+(double s, ntlVector4Dim<Scalar> v) operator+(double s, vector4Dim<Scalar> v)
{ {
return v + s; return v + s;
} }
template<class Scalar> template<class Scalar>
inline ntlVector4Dim<Scalar> inline vector4Dim<Scalar>
operator+(int s, ntlVector4Dim<Scalar> v) operator+(int s, vector4Dim<Scalar> v)
{ {
return v + s; return v + s;
} }
//! Subtraction operator //! Subtraction operator
template<class Scalar> template<class Scalar>
inline ntlVector4Dim<Scalar> inline vector4Dim<Scalar>
ntlVector4Dim<Scalar>::operator-( const ntlVector4Dim<Scalar> &v ) const vector4Dim<Scalar>::operator-( const vector4Dim<Scalar> &v ) const
{ {
return ntlVector4Dim<Scalar>(value[0]-v.value[0], return vector4Dim<Scalar>(value[0]-v.value[0],
value[1]-v.value[1], value[1]-v.value[1],
value[2]-v.value[2], value[2]-v.value[2],
value[3]-v.value[3]); value[3]-v.value[3]);
@@ -249,10 +249,10 @@ ntlVector4Dim<Scalar>::operator-( const ntlVector4Dim<Scalar> &v ) const
//! Subtraction operator //! Subtraction operator
template<class Scalar> template<class Scalar>
inline ntlVector4Dim<Scalar> inline vector4Dim<Scalar>
ntlVector4Dim<Scalar>::operator-(Scalar s ) const vector4Dim<Scalar>::operator-(Scalar s ) const
{ {
return ntlVector4Dim<Scalar>(value[0]-s, return vector4Dim<Scalar>(value[0]-s,
value[1]-s, value[1]-s,
value[2]-s, value[2]-s,
value[3]-s,); value[3]-s,);
@@ -260,50 +260,50 @@ ntlVector4Dim<Scalar>::operator-(Scalar s ) const
//! Multiplication operator //! Multiplication operator
template<class Scalar> template<class Scalar>
inline ntlVector4Dim<Scalar> inline vector4Dim<Scalar>
ntlVector4Dim<Scalar>::operator* ( const ntlVector4Dim<Scalar>& v ) const vector4Dim<Scalar>::operator* ( const vector4Dim<Scalar>& v ) const
{ {
return ntlVector4Dim<Scalar>(value[0]*v.value[0], return vector4Dim<Scalar>(value[0]*v.value[0],
value[1]*v.value[1], value[1]*v.value[1],
value[2]*v.value[2], value[2]*v.value[2],
value[3]*v.value[3]); value[3]*v.value[3]);
} }
//! Multiplication operator //! Multiplication operator
template<class Scalar> template<class Scalar>
inline ntlVector4Dim<Scalar> inline vector4Dim<Scalar>
ntlVector4Dim<Scalar>::operator* (Scalar s) const vector4Dim<Scalar>::operator* (Scalar s) const
{ {
return ntlVector4Dim<Scalar>(value[0]*s, value[1]*s, value[2]*s, value[3]*s); return vector4Dim<Scalar>(value[0]*s, value[1]*s, value[2]*s, value[3]*s);
} }
//! Multiplication operator //! Multiplication operator
template<class Scalar> template<class Scalar>
inline ntlVector4Dim<Scalar> inline vector4Dim<Scalar>
operator* (float s, ntlVector4Dim<Scalar> v) operator* (float s, vector4Dim<Scalar> v)
{ {
return v * s; return v * s;
} }
template<class Scalar> template<class Scalar>
inline ntlVector4Dim<Scalar> inline vector4Dim<Scalar>
operator*(double s, ntlVector4Dim<Scalar> v) operator*(double s, vector4Dim<Scalar> v)
{ {
return v * s; return v * s;
} }
template<class Scalar> template<class Scalar>
inline ntlVector4Dim<Scalar> inline vector4Dim<Scalar>
operator*(int s, ntlVector4Dim<Scalar> v) operator*(int s, vector4Dim<Scalar> v)
{ {
return v * s; return v * s;
} }
//! Division operator //! Division operator
template<class Scalar> template<class Scalar>
inline ntlVector4Dim<Scalar> inline vector4Dim<Scalar>
ntlVector4Dim<Scalar>::operator/ (const ntlVector4Dim<Scalar> & v) const vector4Dim<Scalar>::operator/ (const vector4Dim<Scalar> & v) const
{ {
return ntlVector4Dim<Scalar> (value[0]/v.value[0], return vector4Dim<Scalar> (value[0]/v.value[0],
value[1]/v.value[1], value[1]/v.value[1],
value[2]/v.value[2], value[2]/v.value[2],
value[3]/v.value[3]); value[3]/v.value[3]);
@@ -311,10 +311,10 @@ ntlVector4Dim<Scalar>::operator/ (const ntlVector4Dim<Scalar> & v) const
//! Division operator //! Division operator
template<class Scalar> template<class Scalar>
inline ntlVector4Dim<Scalar> inline vector4Dim<Scalar>
ntlVector4Dim<Scalar>::operator / (Scalar s) const vector4Dim<Scalar>::operator / (Scalar s) const
{ {
return ntlVector4Dim<Scalar> (value[0]/s, return vector4Dim<Scalar> (value[0]/s,
value[1]/s, value[1]/s,
value[2]/s, value[2]/s,
value[3]/s); value[3]/s);
@@ -322,7 +322,7 @@ ntlVector4Dim<Scalar>::operator / (Scalar s) const
//! Safe divide //! Safe divide
template<class Scalar> template<class Scalar>
inline void ntlVector4Dim<Scalar>::safeDivide( const ntlVector4Dim<Scalar> &v ) inline void vector4Dim<Scalar>::safeDivide( const vector4Dim<Scalar> &v )
{ {
value[0] = (v.value[0]!=0) ? (value[0] / v.value[0]) : 0; value[0] = (v.value[0]!=0) ? (value[0] / v.value[0]) : 0;
value[1] = (v.value[1]!=0) ? (value[1] / v.value[1]) : 0; value[1] = (v.value[1]!=0) ? (value[1] / v.value[1]) : 0;
@@ -336,14 +336,14 @@ inline void ntlVector4Dim<Scalar>::safeDivide( const ntlVector4Dim<Scalar> &v )
//! Dot product //! Dot product
template<class Scalar> template<class Scalar>
inline Scalar dot ( const ntlVector4Dim<Scalar> &t, const ntlVector4Dim<Scalar> &v ) { inline Scalar dot ( const vector4Dim<Scalar> &t, const vector4Dim<Scalar> &v ) {
return t.x*v.x + t.y*v.y + t.z*v.z + t.t*v.t; return t.x*v.x + t.y*v.y + t.z*v.z + t.t*v.t;
} }
//! Cross product //! Cross product
/*template<class Scalar> /*template<class Scalar>
inline ntlVector4Dim<Scalar> cross ( const ntlVector4Dim<Scalar> &t, const ntlVector4Dim<Scalar> &v ) { inline vector4Dim<Scalar> cross ( const vector4Dim<Scalar> &t, const vector4Dim<Scalar> &v ) {
NYI ntlVector4Dim<Scalar> cp ( NYI vector4Dim<Scalar> cp (
( ( t.y*v.z ) - ( t.z*v.y ) ), ( ( t.y*v.z ) - ( t.z*v.y ) ),
( ( t.z*v.x ) - ( t.x*v.z ) ), ( ( t.z*v.x ) - ( t.x*v.z ) ),
( ( t.x*v.y ) - ( t.y*v.x ) ) ); ( ( t.x*v.y ) - ( t.y*v.x ) ) );
@@ -353,36 +353,36 @@ inline ntlVector4Dim<Scalar> cross ( const ntlVector4Dim<Scalar> &t, const ntlVe
//! Compute the magnitude (length) of the vector //! Compute the magnitude (length) of the vector
template<class Scalar> template<class Scalar>
inline Scalar norm ( const ntlVector4Dim<Scalar>& v ) { inline Scalar norm ( const vector4Dim<Scalar>& v ) {
Scalar l = v.x*v.x + v.y*v.y + v.z*v.z + v.t*v.t; Scalar l = v.x*v.x + v.y*v.y + v.z*v.z + v.t*v.t;
return ( fabs ( l-1. ) < VECTOR_EPSILON*VECTOR_EPSILON ) ? 1. : sqrt ( l ); return ( fabs ( l-1. ) < VECTOR_EPSILON*VECTOR_EPSILON ) ? 1. : sqrt ( l );
} }
//! Compute squared magnitude //! Compute squared magnitude
template<class Scalar> template<class Scalar>
inline Scalar normSquare ( const ntlVector4Dim<Scalar>& v ) { inline Scalar normSquare ( const vector4Dim<Scalar>& v ) {
return v.x*v.x + v.y*v.y + v.z*v.z + v.t*v.t; return v.x*v.x + v.y*v.y + v.z*v.z + v.t*v.t;
} }
//! Returns a normalized vector //! Returns a normalized vector
template<class Scalar> template<class Scalar>
inline ntlVector4Dim<Scalar> getNormalized ( const ntlVector4Dim<Scalar>& v ) { inline vector4Dim<Scalar> getNormalized ( const vector4Dim<Scalar>& v ) {
Scalar l = v.x*v.x + v.y*v.y + v.z*v.z + v.t*v.t; Scalar l = v.x*v.x + v.y*v.y + v.z*v.z + v.t*v.t;
if ( fabs ( l-1. ) < VECTOR_EPSILON*VECTOR_EPSILON ) if ( fabs ( l-1. ) < VECTOR_EPSILON*VECTOR_EPSILON )
return v; /* normalized "enough"... */ return v; /* normalized "enough"... */
else if ( l > VECTOR_EPSILON*VECTOR_EPSILON ) else if ( l > VECTOR_EPSILON*VECTOR_EPSILON )
{ {
Scalar fac = 1./sqrt ( l ); Scalar fac = 1./sqrt ( l );
return ntlVector4Dim<Scalar> ( v.x*fac, v.y*fac, v.z*fac , v.t*fac ); return vector4Dim<Scalar> ( v.x*fac, v.y*fac, v.z*fac , v.t*fac );
} }
else else
return ntlVector4Dim<Scalar> ( ( Scalar ) 0 ); return vector4Dim<Scalar> ( ( Scalar ) 0 );
} }
//! Compute the norm of the vector and normalize it. //! Compute the norm of the vector and normalize it.
/*! \return The value of the norm */ /*! \return The value of the norm */
template<class Scalar> template<class Scalar>
inline Scalar normalize ( ntlVector4Dim<Scalar> &v ) { inline Scalar normalize ( vector4Dim<Scalar> &v ) {
Scalar norm; Scalar norm;
Scalar l = v.x*v.x + v.y*v.y + v.z*v.z + v.t*v.t; Scalar l = v.x*v.x + v.y*v.y + v.z*v.z + v.t*v.t;
if ( fabs ( l-1. ) < VECTOR_EPSILON*VECTOR_EPSILON ) { if ( fabs ( l-1. ) < VECTOR_EPSILON*VECTOR_EPSILON ) {
@@ -391,14 +391,14 @@ inline Scalar normalize ( ntlVector4Dim<Scalar> &v ) {
norm = sqrt ( l ); norm = sqrt ( l );
v *= 1./norm; v *= 1./norm;
} else { } else {
v = ntlVector4Dim<Scalar>::ZERO; v = vector4Dim<Scalar>::ZERO;
norm = 0.; norm = 0.;
} }
return ( Scalar ) norm; return ( Scalar ) norm;
} }
template<class Scalar> template<class Scalar>
inline bool equal(const ntlVector4Dim<Scalar> &v, const ntlVector4Dim<Scalar> &c) inline bool equal(const vector4Dim<Scalar> &v, const vector4Dim<Scalar> &c)
{ {
return (ABS(v[0]-c[0]) + return (ABS(v[0]-c[0]) +
ABS(v[1]-c[1]) + ABS(v[1]-c[1]) +
@@ -407,7 +407,7 @@ inline bool equal(const ntlVector4Dim<Scalar> &v, const ntlVector4Dim<Scalar> &c
} }
//! Outputs the object in human readable form as string //! Outputs the object in human readable form as string
template<class Scalar> std::string ntlVector4Dim<Scalar>::toString() const { template<class Scalar> std::string vector4Dim<Scalar>::toString() const {
char buf[256]; char buf[256];
snprintf ( buf,256,"<%f,%f,%f,%f>", ( double ) ( *this ) [0], ( double ) ( *this ) [1], ( double ) ( *this ) [2] , ( double ) ( *this ) [3] ); snprintf ( buf,256,"<%f,%f,%f,%f>", ( double ) ( *this ) [0], ( double ) ( *this ) [1], ( double ) ( *this ) [2] , ( double ) ( *this ) [3] );
return std::string ( buf ); return std::string ( buf );
@@ -415,7 +415,7 @@ template<class Scalar> std::string ntlVector4Dim<Scalar>::toString() const {
//! Outputs the object in human readable form to stream //! Outputs the object in human readable form to stream
template<class Scalar> template<class Scalar>
std::ostream& operator<< ( std::ostream& os, const ntlVector4Dim<Scalar>& i ) { std::ostream& operator<< ( std::ostream& os, const vector4Dim<Scalar>& i ) {
char buf[256]; char buf[256];
snprintf ( buf,256,"[%d,%d,%d,%d]", (double) i[0], (double) i[1], (double) i[2] , (double) i[3] ); snprintf ( buf,256,"[%d,%d,%d,%d]", (double) i[0], (double) i[1], (double) i[2] , (double) i[3] );
os << std::string ( buf ); os << std::string ( buf );
@@ -424,7 +424,7 @@ std::ostream& operator<< ( std::ostream& os, const ntlVector4Dim<Scalar>& i ) {
//! Reads the contents of the object from a stream //! Reads the contents of the object from a stream
template<class Scalar> template<class Scalar>
std::istream& operator>> ( std::istream& is, ntlVector4Dim<Scalar>& i ) { std::istream& operator>> ( std::istream& is, vector4Dim<Scalar>& i ) {
char c; char c;
char dummy[4]; char dummy[4];
is >> c >> i[0] >> dummy >> i[1] >> dummy >> i[2] >> dummy >> i[3] >> c; is >> c >> i[0] >> dummy >> i[1] >> dummy >> i[2] >> dummy >> i[3] >> c;
@@ -436,16 +436,16 @@ std::istream& operator>> ( std::istream& is, ntlVector4Dim<Scalar>& i ) {
/**************************************************************************/ /**************************************************************************/
//! 3D vector class of type Real (typically float) //! 3D vector class of type Real (typically float)
typedef ntlVector4Dim<Real> Vec4; typedef vector4Dim<Real> Vec4;
// a 3D vector with double precision // a 3D vector with double precision
typedef ntlVector4Dim<double> nVec4d; typedef vector4Dim<double> nVec4d;
// a 3D vector with single precision // a 3D vector with single precision
typedef ntlVector4Dim<float> nVec4f; typedef vector4Dim<float> nVec4f;
//! 3D vector class of type int //! 3D vector class of type int
typedef ntlVector4Dim<int> nVec4i; typedef vector4Dim<int> nVec4i;
/* convert int,float and double vectors */ /* convert int,float and double vectors */
template<class T> inline nVec4i vec42I(T v) { return nVec4i((int)v[0],(int)v[1],(int)v[2],(int)v[3]); } template<class T> inline nVec4i vec42I(T v) { return nVec4i((int)v[0],(int)v[1],(int)v[2],(int)v[3]); }