2006-12-10

This commit is contained in:
Anonymous Maarten committed 2015-12-03 01:37:02 +01:00
commit 78c27f03c8
153 files changed
+24678

No files matched your search

+114
View File
@@ -0,0 +1,114 @@
#ifndef GOMEZ_Basis_H
#define GOMEZ_Basis_H
/**
* Taken from:
* http://www.gamasutra.com/features/19990702/data_structures_01.htm
* http://www.gamasutra.com/features/19991018/Gomez_1.htm
*
* Both by Miguel Gomez
*/
#include "matrix.hpp"
namespace GomezMath {
// An orthonormal basis with respect to a parent
//
class Basis
{
public:
Matrix R;
public:
Basis ()
{
}
Basis (const Vector & v0,
const Vector & v1, const Vector & v2):R (v0, v1, v2)
{
}
Basis (const Matrix & m):R (m)
{
}
const Vector & operator [] (long i) const
{
return R.C[i];
}
const Vector & x () const
{
return R.C[0];
}
const Vector & y () const
{
return R.C[1];
}
const Vector & z () const
{
return R.C[2];
}
const Matrix & basis () const
{
return R;
}
void basis (const Vector & v0, const Vector & v1, const Vector & v2)
{
this->R[0] = v0;
this->R[1] = v1;
this->R[2] = v2;
}
// Right-Handed Rotations
void rotateAboutX (const Scalar & a)
{
if (0 != a) //don’t rotate by 0
{
Vector b1 = this->y () * cos (a) + this->z () * sin (a);
Vector b2 = -this->y () * sin (a) + this->z () * cos (a);
//set basis
this->R[1] = b1;
this->R[2] = b2;
//x is unchanged
}
}
void rotateAboutY (const Scalar & a)
{
if (0 != a) //don’t rotate by 0
{
Vector b2 = this->z () * cos (a) + this->x () * sin (a); //rotate z
Vector b0 = -this->z () * sin (a) + this->x () * cos (a); //rotate x
//set basis
this->R[2] = b2;
this->R[0] = b0;
//y is unchanged
}
}
void rotateAboutZ (const Scalar & a)
{
if (0 != a) //don’t rotate by 0
{
//don’t over-write basis before calculation is done
Vector b0 = this->x () * cos (a) + this->y () * sin (a); //rotate x
Vector b1 = -this->x () * sin (a) + this->y () * cos (a); //rotate y
//set basis
this->R[0] = b0;
this->R[1] = b1;
//z is unchanged
}
}
//rotate the basis about the unit axis u by theta (radians)
void rotate (const Scalar & theta, const Vector & u);
//rotate, length of da is theta, unit direction of da is u
void rotate (const Vector & da);
// Transformations
const Vector transformVectorToLocal (const Vector & v) const
{
return Vector (R.C[0].dot (v), R.C[1].dot (v), R.C[2].dot (v));
}
const Point transformVectorToParent (const Vector & v) const
{
return R.C[0] * v.x + R.C[1] * v.y + R.C[2] * v.z;
}
};
}
#endif
+60
View File
@@ -0,0 +1,60 @@
#ifndef GOMEZ_COORDFRAME_H
#define GOMEZ_COORDFRAME_H
/**
* Taken from:
* http://www.gamasutra.com/features/19990702/data_structures_01.htm
* http://www.gamasutra.com/features/19991018/Gomez_1.htm
*
* Both by Miguel Gomez
*/
#include "basis.hpp"
namespace GomezMath {
// A coordinate frame (basis and origin) with respect to a parent
//
class CoordFrame:public Basis
{
public:
Point O; //this coordinate frame’s origin, relative to its parent frame
public:
CoordFrame ()
{
}
CoordFrame (const Point & o,
const Vector & v0,
const Vector & v1, const Vector & v2): Basis (v0, v1, v2), O(o)
{
}
CoordFrame (const Point & o, const Basis & b): Basis (b), O(o)
{
}
const Point & position () const
{
return O;
}
void position (const Point & p)
{
O = p;
}
const Point transformPointToLocal (const Point & p) const
{
//translate to this frame’s origin, then project onto this basis
return transformVectorToLocal (p - O);
}
const Point transformPointToParent (const Point & p) const
{
//transform the coordinate vector and translate by this origin
return transformVectorToParent (p) + O;
}
//translate the origin by the given vector
void translate (const Vector & v)
{
O += v;
}
};
}
#endif
+11
View File
@@ -0,0 +1,11 @@
GMATH_FILES = obb.cpp
GMATH_OBJ = $(GMATH_FILES:%.cpp=%.o)
all: libgomezmath.a
libgomezmath.a: $(GMATH_OBJ)
rm -f $@
ar cq $@ $(GMATH_OBJ)
clean:
rm -f libgomezmath.a $(GMATH_OBJ)
+151
View File
@@ -0,0 +1,151 @@
#ifndef GOMEZ_Matrix_H
#define GOMEZ_Matrix_H
#include "vector.hpp"
namespace GomezMath {
/**
* Taken from:
* http://www.gamasutra.com/features/19990702/data_structures_01.htm
* http://www.gamasutra.com/features/19991018/Gomez_1.htm
*
* Both by Miguel Gomez
*/
// A 3x3 matrix
//
class Matrix
{
public:
Vector C[3]; //column vectors
public:
Matrix ()
{
//identity matrix
C[0].x = 1;
C[1].y = 1;
C[2].z = 1;
}
Matrix (const Vector & c0, const Vector & c1, const Vector & c2)
{
C[0] = c0;
C[1] = c1;
C[2] = c2;
}
//index a column, allow assignment
//NOTE: using this index operator along with the vector index
//gives you M[column][row], not the standard M[row][column]
Vector & operator [](long i)
{
return C[i];
}
//compare
const bool operator == (const Matrix & m) const
{
return C[0] == m.C[0] && C[1] == m.C[1] && C[2] == m.C[2];
}
const bool operator != (const Matrix & m) const
{
return !(m == *this);
}
//assign
const Matrix & operator = (const Matrix & m)
{
C[0] = m.C[0];
C[1] = m.C[1];
C[2] = m.C[2];
return *this;
}
//increment
const Matrix & operator += (const Matrix & m)
{
C[0] += m.C[0];
C[1] += m.C[1];
C[2] += m.C[2];
return *this;
}
//decrement
const Matrix & operator -= (const Matrix & m)
{
C[0] -= m.C[0];
C[1] -= m.C[1];
C[2] -= m.C[2];
return *this;
}
//self-multiply by a scalar
const Matrix & operator *= (const Scalar & s)
{
C[0] *= s;
C[1] *= s;
C[2] *= s;
return *this;
}
//self-multiply by a matrix
const Matrix & operator *= (const Matrix & m)
{
//NOTE: don’t change the columns
//in the middle of the operation
Matrix temp = (*this);
C[0] = temp * m.C[0];
C[1] = temp * m.C[1];
C[2] = temp * m.C[2];
return *this;
}
//add
const Matrix operator + (const Matrix & m) const
{
return Matrix (C[0] + m.C[0], C[1] + m.C[1], C[2] + m.C[2]);
}
//subtract
const Matrix operator - (const Matrix & m) const
{
return Matrix (C[0] - m.C[0], C[1] - m.C[1], C[2] - m.C[2]);
}
//post-multiply by a scalar
const Matrix operator * (const Scalar & s) const
{
return Matrix (C[0] * s, C[1] * s, C[2] * s);
}
//pre-multiply by a scalar
friend inline const Matrix operator * (const Scalar & s, const Matrix & m)
{
return m * s;
}
//post-multiply by a vector
const Vector operator * (const Vector & v) const
{
return (C[0] * v.x + C[1] * v.y + C[2] * v.z);
}
//pre-multiply by a vector
inline friend const Vector operator * (const Vector & v, const Matrix & m)
{
return Vector (m.C[0].dot (v), m.C[1].dot (v), m.C[2].dot (v));
}
//post-multiply by a matrix
const Matrix operator * (const Matrix & m) const
{
return Matrix ((*this) * m.C[0], (*this) * m.C[1], (*this) * m.C[2]);
}
//transpose
Matrix transpose () const
{
//turn columns on their side
return Matrix (Vector (C[0].x, C[1].x, C[2].x), //column 0
Vector (C[0].y, C[1].y, C[2].y), //column 1
Vector (C[0].z, C[1].z, C[2].z) //column 2
);
}
//scalar determinant
const Scalar determinant () const
{
//Lang, "Linear Algebra", p. 143
return C[0].dot (C[1].cross (C[2]));
}
//matrix inverse
const Matrix inverse () const;
};
}
#endif
+126
View File
@@ -0,0 +1,126 @@
#ifndef GOMEZ_OBB_H
#define GOMEZ_OBB_H
/**
* Taken from:
* http://www.gamasutra.com/features/19990702/data_structures_01.htm
* http://www.gamasutra.com/features/19991018/Gomez_1.htm
*
* Both by Miguel Gomez
*/
#include "obb.hpp"
#include "vector.hpp"
namespace GomezMath {
//check if two oriented bounding boxes overlap
const bool OBBOverlap (
//A
Vector & a, //extents
Vector & Pa, //position
Vector * A, //orthonormal basis
//B
Vector & b, //extents
Vector & Pb, //position
Vector * B //orthonormal basis
)
{
//translation, in parent frame
Vector v = Pb - Pa;
//translation, in A's frame
Vector T (v.dot (A[0]), v.dot (A[1]), v.dot (A[2]));
//B's basis with respect to A's local frame
Scalar R[3][3];
float ra, rb, t;
long i, k;
//calculate rotation matrix
for (i = 0; i < 3; i++)
for (k = 0; k < 3; k++)
R[i][k] = A[i].dot (B[k]);
/*ALGORITHM: Use the separating axis test for all 15 potential
separating axes. If a separating axis could not be found, the two
boxes overlap. */
//A's basis vectors
for (i = 0; i < 3; i++)
{
ra = a[i];
rb =
b[0] * fabs (R[i][0]) + b[1] * fabs (R[i][1]) + b[2] * fabs (R[i][2]);
t = fabs (T[i]);
if (t > ra + rb)
return false;
}
//B's basis vectors
for (k = 0; k < 3; k++)
{
ra =
a[0] * fabs (R[0][k]) + a[1] * fabs (R[1][k]) + a[2] * fabs (R[2][k]);
rb = b[k];
t = fabs (T[0] * R[0][k] + T[1] * R[1][k] + T[2] * R[2][k]);
if (t > ra + rb)
return false;
}
//9 cross products
//L = A0 x B0
ra = a[1] * fabs (R[2][0]) + a[2] * fabs (R[1][0]);
rb = b[1] * fabs (R[0][2]) + b[2] * fabs (R[0][1]);
t = fabs (T[2] * R[1][0] - T[1] * R[2][0]);
if (t > ra + rb)
return false;
//L = A0 x B1
ra = a[1] * fabs (R[2][1]) + a[2] * fabs (R[1][1]);
rb = b[0] * fabs (R[0][2]) + b[2] * fabs (R[0][0]);
t = fabs (T[2] * R[1][1] - T[1] * R[2][1]);
if (t > ra + rb)
return false;
//L = A0 x B2
ra = a[1] * fabs (R[2][2]) + a[2] * fabs (R[1][2]);
rb = b[0] * fabs (R[0][1]) + b[1] * fabs (R[0][0]);
t = fabs (T[2] * R[1][2] - T[1] * R[2][2]);
if (t > ra + rb)
return false;
//L = A1 x B0
ra = a[0] * fabs (R[2][0]) + a[2] * fabs (R[0][0]);
rb = b[1] * fabs (R[1][2]) + b[2] * fabs (R[1][1]);
t = fabs (T[0] * R[2][0] - T[2] * R[0][0]);
if (t > ra + rb)
return false;
//L = A1 x B1
ra = a[0] * fabs (R[2][1]) + a[2] * fabs (R[0][1]);
rb = b[0] * fabs (R[1][2]) + b[2] * fabs (R[1][0]);
t = fabs (T[0] * R[2][1] - T[2] * R[0][1]);
if (t > ra + rb)
return false;
//L = A1 x B2
ra = a[0] * fabs (R[2][2]) + a[2] * fabs (R[0][2]);
rb = b[0] * fabs (R[1][1]) + b[1] * fabs (R[1][0]);
t = fabs (T[0] * R[2][2] - T[2] * R[0][2]);
if (t > ra + rb)
return false;
//L = A2 x B0
ra = a[0] * fabs (R[1][0]) + a[1] * fabs (R[0][0]);
rb = b[1] * fabs (R[2][2]) + b[2] * fabs (R[2][1]);
t = fabs (T[1] * R[0][0] - T[0] * R[1][0]);
if (t > ra + rb)
return false;
//L = A2 x B1
ra = a[0] * fabs (R[1][1]) + a[1] * fabs (R[0][1]);
rb = b[0] * fabs (R[2][2]) + b[2] * fabs (R[2][0]);
t = fabs (T[1] * R[0][1] - T[0] * R[1][1]);
if (t > ra + rb)
return false;
//L = A2 x B2
ra = a[0] * fabs (R[1][2]) + a[1] * fabs (R[0][2]);
rb = b[0] * fabs (R[2][1]) + b[1] * fabs (R[2][0]);
t = fabs (T[1] * R[0][2] - T[0] * R[1][2]);
if (t > ra + rb)
return false;
/*no separating axis found,
the two boxes overlap */
return true;
}
}
#endif
+42
View File
@@ -0,0 +1,42 @@
#ifndef GOMEZ_OBB_H
#define GOMEZ_OBB_H
/**
* Taken from:
* http://www.gamasutra.com/features/19990702/data_structures_01.htm
* http://www.gamasutra.com/features/19991018/Gomez_1.htm
*
* Both by Miguel Gomez
*/
#include "coord_frame.hpp"
namespace GomezMath {
class OBB : public CoordFrame
{
public:
Vector E; //extents
OBB (const Vector & e) : E (e)
{
}
OBB (const Vector & e, const Vector & origin,
const Vector & v0, const Vector & v1, const Vector & v2) :
CoordFrame(origin, v0, v1, v2), E(e)
{
}
};
const bool OBBOverlap (
//A
Vector & a, //extents
Vector & Pa, //position
Vector * A, //orthonormal basis
//B
Vector & b, //extents
Vector & Pb, //position
Vector * B //orthonormal basis
);
}
#endif
+164
View File
@@ -0,0 +1,164 @@
#include "obox.h"
// --------------------------
//
// Oriented Bounding Box Class
//
// --------------------------
//
// Check if a point is in this bounding box
//
bool OBox::IsPointInBox(const Vector3D &InP)
{
// Rotate the point into the box's coordinates
Vector3D P = Transform(InP, m_M.Inverse());
// Now just use an axis-aligned check
if ( fabs(P.x) < m_Extent.x && fabs(P.y) < m_Extent.y && fabs(P.z) < m_Extent.z )
return true;
return false;
}
//
// Check if a sphere overlaps any part of this bounding box
//
bool OBox::IsSphereInBox( const Vector3D &InP, float fRadius)
{
float fDist;
float fDistSq = 0;
Vector3D P = Transform(InP, m_M.Inverse());
// Add distance squared from sphere centerpoint to box for each axis
for ( int i = 0; i < 3; i++ )
{
if ( fabs(P[i]) > m_Extent[i] )
{
fDist = fabs(P[i]) - m_Extent[i];
fDistSq += fDist*fDist;
}
}
return ( fDistSq <= fRadius*fRadius );
}
//
// Check if the bounding box is completely behind a plane( defined by a normal and a point )
//
bool OBox::BoxOutsidePlane( const Vector3D &InNorm, const Vector3D &InP )
{
// Plane Normal in Box Space
Vector3D Norm = rotateVector(InNorm, m_M.Inverse() );
Norm = Vector3D( fabs( Norm.x ), fabs( Norm.y ), fabs( Norm.z ) );
float Extent = Norm * m_Extent; //Norm.Dot( m_Extent ); // Box Extent along the plane normal
//float Distance = InNorm.Dot( GetCenterPoint() - InP ); // Distance from Box Center to the Plane
float Distance = InNorm * (GetCenterPoint() - InP);
// If Box Centerpoint is behind the plane further than its extent, the Box is outside the plane
if ( Distance < -Extent ) return true;
return false;
}
//
// Does the Line (L1, L2) intersect the Box?
//
bool OBox::IsLineInBox( const Vector3D& L1, const Vector3D& L2 )
{
// Put line in box space
Matrix3D MInv = m_M.Inverse();
Vector3D LB1 = Transform(L1, MInv);
Vector3D LB2 = Transform(L2, MInv);
// Get line midpoint and extent
Vector3D LMid = (LB1 + LB2) * 0.5f;
Vector3D L = (LB1 - LMid);
Vector3D LExt = Vector3D( fabs(L.x), fabs(L.y), fabs(L.z) );
// Use Separating Axis Test
// Separation vector from box center to line center is LMid, since the line is in box space
if ( fabs( LMid.x ) > m_Extent.x + LExt.x ) return false;
if ( fabs( LMid.y ) > m_Extent.y + LExt.y ) return false;
if ( fabs( LMid.z ) > m_Extent.z + LExt.z ) return false;
// Crossproducts of line and each axis
if ( fabs( LMid.y * L.z - LMid.z * L.y) > (m_Extent.y * LExt.z + m_Extent.z * LExt.y) ) return false;
if ( fabs( LMid.x * L.z - LMid.z * L.x) > (m_Extent.x * LExt.z + m_Extent.z * LExt.x) ) return false;
if ( fabs( LMid.x * L.y - LMid.y * L.x) > (m_Extent.x * LExt.y + m_Extent.y * LExt.x) ) return false;
// No separating axis, the line intersects
return true;
}
//
// Returns a 3x3 rotation matrix as vectors
//
inline void OBox::GetInvRot( Vector3D *pvRot )
{
pvRot[0] = Vector3D( m_M.m[0][0], m_M.m[0][1], m_M.m[0][2] );
pvRot[1] = Vector3D( m_M.m[1][0], m_M.m[1][1], m_M.m[1][2] );
pvRot[2] = Vector3D( m_M.m[2][0], m_M.m[2][1], m_M.m[2][2] );
}
//
// Check if any part of a box is inside any part of another box
// Uses the separating axis test.
//
bool OBox::IsBoxInBox( OBox &BBox )
{
Vector3D SizeA = m_Extent;
Vector3D SizeB = BBox.m_Extent;
Vector3D RotA[3], RotB[3];
GetInvRot( RotA );
BBox.GetInvRot( RotB );
float R[3][3]; // Rotation from B to A
float AR[3][3]; // absolute values of R matrix, to use with box extents
float ExtentA, ExtentB, Separation;
int i, k;
// Calculate B to A rotation matrix
for( i = 0; i < 3; i++ )
for( k = 0; k < 3; k++ )
{
R[i][k] = RotA[i] * RotB[k];
AR[i][k] = fabs(R[i][k]);
}
// Vector separating the centers of Box B and of Box A
Vector3D vSepWS = BBox.GetCenterPoint() - GetCenterPoint();
// Rotated into Box A's coordinates
Vector3D vSepA( vSepWS * RotA[0], vSepWS * RotA[1], vSepWS * RotA[2] );
// Test if any of A's basis vectors separate the box
for( i = 0; i < 3; i++ )
{
ExtentA = SizeA[i];
ExtentB = SizeB * Vector3D( AR[i][0], AR[i][1], AR[i][2] );
Separation = fabs( vSepA[i] );
if( Separation > ExtentA + ExtentB ) return false;
}
// Test if any of B's basis vectors separate the box
for( k = 0; k < 3; k++ )
{
ExtentA = SizeA * Vector3D( AR[0][k], AR[1][k], AR[2][k] );
ExtentB = SizeB[k];
Separation = fabs( vSepA * Vector3D(R[0][k],R[1][k],R[2][k]) );
if( Separation > ExtentA + ExtentB ) return false;
}
// Now test Cross Products of each basis vector combination ( A[i], B[k] )
for( i=0 ; i<3 ; i++ )
for( k=0 ; k<3 ; k++ )
{
int i1 = (i+1)%3, i2 = (i+2)%3;
int k1 = (k+1)%3, k2 = (k+2)%3;
ExtentA = SizeA[i1] * AR[i2][k] + SizeA[i2] * AR[i1][k];
ExtentB = SizeB[k1] * AR[i][k2] + SizeB[k2] * AR[i][k1];
Separation = fabs( vSepA[i2] * R[i1][k] - vSepA[i1] * R[i2][k] );
if( Separation > ExtentA + ExtentB ) return false;
}
// No separating axis found, the boxes overlap
return true;
}
+43
View File
@@ -0,0 +1,43 @@
#include "math3d.h"
// see: from: http://www.3dkingdoms.com/weekly/weekly.php?a=21
// basically the same as bbox.h/.cpp but using coldet math
class OBox
{
public:
OBox() {}
OBox( const Matrix3D & m, const Vector3D & extent )
{ Set( m, extent ); }
OBox( const Matrix3D & m, const Vector3D & low, const Vector3D & high )
{ Set( m, low, high ); }
void Set( const Matrix3D & m, const Vector3D & extent )
{
m_M = m;
m_Extent = extent;
}
void Set( const Matrix3D & m, const Vector3D & low, const Vector3D & high )
{
m_M = m;
m_M.Translate( 0.5f * (low + high) );
m_Extent = 0.5f * (high - low);
}
Vector3D GetSize()
{ return 2.0f * m_Extent; }
Vector3D GetCenterPoint()
{ return m_M.GetTranslate(); }
void GetInvRot( Vector3D *pvRot );
bool IsPointInBox( const Vector3D & p );
bool IsBoxInBox( OBox & box );
bool IsSphereInBox( const Vector3D & p, float fRadius );
bool IsLineInBox( const Vector3D & l1, const Vector3D & l2 );
bool BoxOutsidePlane( const Vector3D & normal, const Vector3D & p );
// Data
Matrix3D m_M;
Vector3D m_Extent;
};
+184
View File
@@ -0,0 +1,184 @@
#ifndef QUATERNION_MATH
#define QUATERNION_MATH
#include <cmath>
namespace Util { namespace Math {
/*!
* \namespace Util::Math
* \brief Vector/Matrix/Quaternion interpolation
*
* Found at http://www.flipcode.com/files/code/vquats.cpp
*
*
* I think this is very sleek, so I kept it.
*
* Editor's note:
* COTD Entry: Vector Math & Quaternions by Tim Sweeney [tim@epicgames.com]
This is some fairly generic vector math code. Here I want to
illustrate two things:
1. The "vectorSpace" template is a nice way to represent the
general mathematical notion of vectors without regard to
particular data types. The idea is that you can declare
a vector type (like vec4 below), and the vectorSpace template
automatically generates the appropriate vector operators.
I wrote this code for clarity and generality. For performance, you would
want to specialize the vectorSpace template for common types, like 4-
component floating-point vectors. You could implement these using
KNI and 3DNow instructions, for example.
2. Useful quaternion functions, most notably "exp" and "ln".
The traditional and limited way programmers used quaternion
rotations in the past was to use "spherical linear interpolation",
interpolating a rotational arc between two quaternions at a constant
angular rate. Given quaternions q1 and q2, you might have used
a function lik "slerp(q1,q2,0.25)" to find a rotation
one-quarter between the two quaternions. This approach
was unsatisfactory because it was difficult to perform
smooth, higher order, i.e. hermite or bezier, interpolation
between quaternions.
New approach: take the logarithm ("ln" function below) of
your quaternions, then use ordinary linear interpolation
(sampleLinear below) or any higher-order interpolation
scheme -- and then take the exponent ("exp") of the result.
The resulting rotations will have the desired smooth angular
movement rates.
Why does this work? Read up on "geometric algebra" to find out.
Unit quaternions are just a special 3-dimensional case of
spinors, which are the most general representation of
n-dimensional rotations possible.
*/
// Template implementing a "vector space" (in the precise mathematical sense).
// A vector space is a type "u", implemented as
// an array of "n" elements of type "t".
template<class t,int n,class u> struct vectorSpace {
t x[n];
vectorSpace() {for(int i=0; i<n; i++) x[i]=t();}
inline t& operator[](int i) {/*assert(i>=0 && i<n);*/ return x[i];}
inline const t& operator[](int i) const {/*assert(i>=0 && i<n);*/ return x[i];}
friend bool operator== (const u& a,const u& b) {bool c=true; for(int i=0; i<n; i++) c=c&&a[i]==b[i]; return c;}
friend bool operator!= (const u& a,const u& b) {bool c=false; for(int i=0; i<n; i++) c=c||a[i]!=b[i]; return c;}
friend inline t vectorSum (const u& a ) {t c=0; for(int i=0; i<n; i++) c+=a[i]; return c;}
friend inline t vectorProd(const u& a ) {t c=1; for(int i=0; i<n; i++) c*=a[i]; return c;}
friend inline u operator- (const u& a ) {u c; for(int i=0; i<n; i++) c[i] = -a[i]; return c;}
friend inline u operator+ (const u& a,const u& b) {u c; for(int i=0; i<n; i++) c[i] =a[i]+b[i]; return c;}
friend inline u operator- (const u& a,const u& b) {u c; for(int i=0; i<n; i++) c[i] =a[i]-b[i]; return c;}
friend inline u operator* (const float a,const u& b) {u c; for(int i=0; i<n; i++) c[i] =a *b[i]; return c;}
friend inline u operator* (const u& a,const float b) {u c; for(int i=0; i<n; i++) c[i] =a[i]*b; return c;}
friend inline u operator/ (const u& a,const float b) {u c; for(int i=0; i<n; i++) c[i] =a[i]/b; return c;}
friend inline u operator+=( u& a,const u& b) { for(int i=0; i<n; i++) a[i]+=b[i]; return a;}
friend inline u operator-=( u& a,const u& b) { for(int i=0; i<n; i++) a[i]-=b[i]; return a;}
friend inline u operator*=( u& a,const float b) { for(int i=0; i<n; i++) a[i]*=b; return a;}
friend inline u operator/=( u& a,const float b) { for(int i=0; i<n; i++) a[i]/=b; return a;}
};
template<class t> inline t sampleLinear(const t& a,const t& b,float s) {
return (1-s)*a+s*b;
}
template<class t> inline t sampleHermite(const t& a,const t& b,const t& at,const t& bt,float s) {
return
(+2*s*s*s -3*s*s +1)*a+
(-2*s*s*s +3*s*s )*b+
(+1*s*s*s -2*s*s +s )*at+
(+1*s*s*s -1*s*s )*bt;
}
template<class t> inline t sampleBezier(const t& a,const t& b,const t& ac,const t& bc,float s) {
return
(-1*s*s*s +3*s*s -3*s +1)*a+
(+3*s*s*s -6*s*s +3*s )*b+
(-3*s*s*s +3*s*s )*ac+
(+1*s*s*s )*bc;
}
// 4-component floating point vectors.
struct vec4: public vectorSpace<float,4,vec4> {
inline vec4() {x[0]=x[1]=x[2]=x[3]=0;}
inline vec4(float _x,float _y,float _z,float _w) {x[0]=_x; x[1]=_y; x[2]=_z; x[3]=_w;}
};
// 4x4 matrices.
struct mtx44: public vectorSpace<vec4,4,mtx44> {
mtx44() {}
mtx44(const vec4& _x,const vec4& _y, const vec4& _z,const vec4& _w) {x[0]=_x; x[1]=_y; x[2]=_z; x[3]=_w;}
};
// A 3-dimensional (4-component) quaternion.
struct quat: public vectorSpace<float,4,quat> {
quat() {x[0]=x[1]=x[2]=0; x[3]=0;}
quat(float _x,float _y,float _z,float _w) {x[0]=_x; x[1]=_y; x[2]=_z; x[3]=_w;}
};
inline quat operator*(const quat& a,const quat& b) {
return quat(
+a[0]*b[3] +a[1]*b[2] -a[2]*b[1] +a[3]*b[0],
-a[0]*b[2] +a[1]*b[3] +a[2]*b[0] +a[3]*b[1],
+a[0]*b[1] -a[1]*b[0] +a[2]*b[3] +a[3]*b[2],
-a[0]*b[0] -a[1]*b[1] -a[2]*b[2] +a[3]*b[3]
);
}
inline float norm(const quat& a) {
return a[0]*a[0] + a[1]*a[1] + a[2]*a[2] + a[3]*a[3];
}
inline float mag(const quat& a) {
return sqrt(norm(a));
}
inline quat normal(const quat& a) {
return a/mag(a);
}
inline quat conj(const quat& a) {
return quat(-a[0],-a[1],-a[2],a[3]);
}
inline quat inv(const quat& a) {
return 1.f/norm(a)*conj(a);
}
inline quat exp(const quat& a) {
float r = sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]);
float et = std::exp(a[3]);
float s = r>=0.00001f? et*sin(r)/r: 0.f;
return quat(s*a[0],s*a[1],s*a[2],et*cos(r));
}
inline quat ln(const quat& a) {
float r = sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]);
float t = r>0.00001f? atan2(r,a[3])/r: 0.f;
return quat(t*a[0],t*a[1],t*a[2],0.5*log(norm(a)));
}
inline vec4 quatToRotationAxisAngle(const quat& a) {
float t = a[3]*a[3];
float rsa = t<0.99999999? 1.f/sqrt(1.f-t): 1.f;
return vec4(a[0]*rsa,a[1]*rsa,a[2]*rsa,acos(a[3])*2.f);
}
inline quat quatFromRotationAxisAngle(const vec4& a) {
float s = sin(a[3]/2.f);
float c = cos(a[3]/2.f);
return quat(a[0]*s,a[1]*s,a[2]*s,c);
}
inline mtx44 quatToRotationMatrix(const quat& a) {
float xx=a[0]*a[0];
float xy=a[0]*a[1], yy=a[1]*a[1];
float xz=a[0]*a[2], yz=a[1]*a[2], zz=a[2]*a[2];
float xw=a[0]*a[3], yw=a[1]*a[3], zw=a[2]*a[3], ww=a[3]*a[3];
return mtx44(
vec4(+xx-yy-zz+ww, +xy+zw+xy+zw, +xz-yw+xz-yw, 0),
vec4(+xy-zw+xy-zw, -xx+yy-zz+ww, +yz+xw+yz+xw, 0),
vec4(+xz+yw+xz+yw, +yz-xw+yz-xw, -xx-yy+zz+ww, 0),
vec4(0 , 0 , 0 , 1)
);
}
inline quat quatFromRotationMatrix(const mtx44& m) {
float w=0.5*sqrt(m[0][0]+m[1][1]+m[2][2]+m[3][3]);
float s=0.25/w;
return quat(
(m[1][2]-m[2][1])*s,
(m[2][0]-m[0][2])*s,
(m[0][1]-m[1][0])*s,
w
);
}
}
}
#endif
+159
View File
@@ -0,0 +1,159 @@
#ifndef GOMEZ_Vector_H
#define GOMEZ_Vector_H
#include <cmath>
/**
* Taken from:
* http://www.gamasutra.com/features/19990702/data_structures_01.htm
* http://www.gamasutra.com/features/19991018/Gomez_1.htm
*
* Both by Miguel Gomez
*/
namespace GomezMath {
// A floating point number
//
typedef float Scalar;
//
// A 3D vector
//
class Vector
{
public:
Scalar x, y, z; //x,y,z coordinates
public:
Vector ():x (0), y (0), z (0)
{
}
Vector (const Scalar & a, const Scalar & b, const Scalar & c):x (a), y (b),
z (c)
{
}
//index a component
//NOTE: returning a reference allows
//you to assign the indexed element
Scalar & operator [](const long i)
{
return *((&x) + i);
}
//compare
const bool operator == (const Vector & v) const
{
return (v.x == x && v.y == y && v.z == z);
}
const bool operator != (const Vector & v) const
{
return !(v == *this);
}
//negate
const Vector operator - () const
{
return Vector (-x, -y, -z);
}
//assign
const Vector & operator = (const Vector & v)
{
x = v.x;
y = v.y;
z = v.z;
return *this;
}
//increment
const Vector & operator += (const Vector & v)
{
x += v.x;
y += v.y;
z += v.z;
return *this;
}
//decrement
const Vector & operator -= (const Vector & v)
{
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
//self-multiply
const Vector & operator *= (const Scalar & s)
{
x *= s;
y *= s;
z *= s;
return *this;
}
//self-divide
const Vector & operator /= (const Scalar & s)
{
const Scalar r = 1 / s;
x *= r;
y *= r;
z *= r;
return *this;
}
//add
const Vector operator + (const Vector & v) const
{
return Vector (x + v.x, y + v.y, z + v.z);
}
//subtract
const Vector operator - (const Vector & v) const
{
return Vector (x - v.x, y - v.y, z - v.z);
}
//post-multiply by a scalar
const Vector operator * (const Scalar & s) const
{
return Vector (x * s, y * s, z * s);
}
//pre-multiply by a scalar
friend inline const Vector operator * (const Scalar & s, const Vector & v)
{
return v * s;
}
//divide
const Vector operator / (Scalar s) const
{
s = 1 / s;
return Vector (s * x, s * y, s * z);
}
//cross product
const Vector cross (const Vector & v) const
{
//Davis, Snider, "Introduction to Vector Analysis", p. 44
return Vector (y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
}
//scalar dot product
const Scalar dot (const Vector & v) const
{
return x * v.x + y * v.y + z * v.z;
}
//length
const Scalar length () const
{
return (Scalar) sqrt ((double) this->dot (*this));
}
//unit vector
const Vector unit () const
{
return (*this) / length ();
}
//make this a unit vector
void normalize ()
{
(*this) /= length ();
}
//equal within an error ‘e’
const bool nearlyEquals (const Vector & v, const Scalar e) const
{
return fabs (x - v.x) < e && fabs (y - v.y) < e && fabs (z - v.z) < e;
}
};
//
// A 3D position
//
typedef Vector Point;
}
#endif