Initial commit.
This commit is contained in:
366
code/ravl/CBounds.cpp
Normal file
366
code/ravl/CBounds.cpp
Normal file
@@ -0,0 +1,366 @@
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// RAVEN STANDARD TEMPLATE LIBRARY
|
||||
// (c) 2002 Activision
|
||||
//
|
||||
//
|
||||
// Vector Library
|
||||
// --------------
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// NOTES:
|
||||
// 05/31/02 - CREATED
|
||||
//
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#if !defined(ASSERT_H_INC)
|
||||
#include <assert.h>
|
||||
#define ASSERT_H_INC
|
||||
#endif
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <float.h>
|
||||
#include "CBounds.h"
|
||||
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
/*void CBBox::ThroughMatrix(const CBBox &from, const CMatrix4 &mat)
|
||||
{
|
||||
Clear();
|
||||
CVec3 bb,t;
|
||||
int i;
|
||||
const CVec3 &xmn=from.GetMin();
|
||||
const CVec3 &xmx=from.GetMax();
|
||||
for ( i = 0; i < 8; i++ )
|
||||
{
|
||||
if ( i & 1 )
|
||||
bb[0] = xmn[0];
|
||||
else
|
||||
bb[0] = xmx[0];
|
||||
if ( i & 2 )
|
||||
bb[1] = xmn[1];
|
||||
else
|
||||
bb[1] = xmx[1];
|
||||
if ( i & 4 )
|
||||
bb[2] = xmn[2];
|
||||
else
|
||||
bb[2] = xmx[2];
|
||||
mat.XFormPoint(t,bb);
|
||||
AddPoint(t);
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
float CBBox::LargestAxisSize() const
|
||||
{
|
||||
CVec3 Work(mMax);
|
||||
Work-=mMin;
|
||||
return Work.MaxElement();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
float CBBox::DistanceEstimate(const CVec3 &p) const
|
||||
{
|
||||
float ret=0.0f;
|
||||
|
||||
// X Axis
|
||||
//--------
|
||||
if (p[0]>mMax[0])
|
||||
{
|
||||
ret=p[0]-mMax[0];
|
||||
}
|
||||
else if (p[0]<mMin[0])
|
||||
{
|
||||
ret=mMax[0]-p[0];
|
||||
}
|
||||
|
||||
// Y Axis
|
||||
//--------
|
||||
if (p[1]>mMax[1])
|
||||
{
|
||||
ret+=p[1]-mMax[1];
|
||||
}
|
||||
else if (p[1]<mMin[1])
|
||||
{
|
||||
ret+=mMax[1]-p[1];
|
||||
}
|
||||
|
||||
// Z Axis
|
||||
//--------
|
||||
if (p[2]>mMax[2])
|
||||
{
|
||||
ret+=p[2]-mMax[2];
|
||||
}
|
||||
else if (p[2]<mMin[2])
|
||||
{
|
||||
ret+=mMax[2]-p[2];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
float CBBox::AreaEstimate(const CVec3 &p) const
|
||||
{
|
||||
float Distance=DistanceEstimate(p);
|
||||
if (Distance)
|
||||
{
|
||||
return LargestAxisSize()/Distance;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
void CBBox::Intersect(const CBBox &b2)
|
||||
{
|
||||
mMin.Max(b2.mMin);
|
||||
mMax.Min(b2.mMax);
|
||||
Validate();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
void CBBox::Union(const CBBox &b2)
|
||||
{
|
||||
mMin.Min(b2.mMin);
|
||||
mMax.Max(b2.mMax);
|
||||
Validate();
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
ESide CBBox::InOutTest(const CVec3 &v) const
|
||||
{
|
||||
if (v>mMin && v<mMax)
|
||||
{
|
||||
return Side_In;
|
||||
}
|
||||
return Side_Out;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
ESide CBBox::InOutTest(const CVec3 &v,float tolout,float tolin) const
|
||||
{
|
||||
if (v[0]<mMin[0]-tolout||v[0]>mMax[0]+tolout||
|
||||
v[1]<mMin[1]-tolout||v[1]>mMax[1]+tolout||
|
||||
v[2]<mMin[2]-tolout||v[2]>mMax[2]+tolout)
|
||||
{
|
||||
return Side_Out;
|
||||
}
|
||||
if (v[0]>mMin[0]+tolin&&v[0]<mMax[0]-tolin&&
|
||||
v[1]>mMin[1]+tolin&&v[1]<mMax[1]-tolin&&
|
||||
v[2]>mMin[2]+tolin&&v[2]<mMax[2]-tolin)
|
||||
{
|
||||
return Side_In;
|
||||
}
|
||||
return Side_None;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
bool CBBox::BoxTouchTest(const CBBox &b2,float tolout) const
|
||||
{
|
||||
if (mMin[0]-tolout>b2.mMax[0] ||
|
||||
mMin[1]-tolout>b2.mMax[1] ||
|
||||
mMin[2]-tolout>b2.mMax[2] ||
|
||||
b2.mMin[0]-tolout>mMax[0] ||
|
||||
b2.mMin[1]-tolout>mMax[1] ||
|
||||
b2.mMin[2]-tolout>mMax[2])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
bool CBBox::SphereTouchTest(const CVec3 &v,float rad) const
|
||||
{
|
||||
if (v[0]<mMin[0]-rad||v[0]>mMax[0]+rad||
|
||||
v[1]<mMin[1]-rad||v[1]>mMax[1]+rad||
|
||||
v[2]<mMin[2]-rad||v[2]>mMax[2]+rad)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
TPlanes CBBox::PlaneFlags(const CVec3 &p)
|
||||
{
|
||||
TPlanes ret=0;
|
||||
if (p[0]<mMin[0])
|
||||
{
|
||||
ret|=1;
|
||||
}
|
||||
else if (p[0]>mMax[0])
|
||||
{
|
||||
ret|=2;
|
||||
}
|
||||
if (p[1]<mMin[1])
|
||||
{
|
||||
ret|=4;
|
||||
}
|
||||
else if (p[1]>mMax[1])
|
||||
{
|
||||
ret|=8;
|
||||
}
|
||||
if (p[2]<mMin[2])
|
||||
{
|
||||
ret|=16;
|
||||
}
|
||||
else if (p[2]>mMax[2])
|
||||
{
|
||||
ret|=32;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// return true if the segment intersect the box, in that case, return the first contact.
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
bool CBBox::HitTest(CBTrace& Tr) const
|
||||
{
|
||||
// Quick Box Cull
|
||||
//----------------
|
||||
CBBox tmp;
|
||||
tmp.AddPoint(Tr.mStart);
|
||||
tmp.AddPoint(Tr.mStop);
|
||||
if (!BoxTouchTest(tmp))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Initialize Our Ranges
|
||||
//-----------------------
|
||||
Tr.mRange =-1E30f;
|
||||
Tr.mRangeMax = 1E30f;
|
||||
|
||||
|
||||
// For Each Non Zero Axis Of The Aim Vector
|
||||
//------------------------------------------
|
||||
float tmax,tmin,temp;
|
||||
for (int axis=0; axis<3; axis++)
|
||||
{
|
||||
if (fabs(Tr.mAim[axis])>1E-6f)
|
||||
{
|
||||
// Find Mins And Maxs From The Start Along The Axis Of Aim
|
||||
//---------------------------------------------------------
|
||||
tmax = ((mMax[axis]-Tr.mStart[axis])/Tr.mAim[axis]);
|
||||
tmin = ((mMin[axis]-Tr.mStart[axis])/Tr.mAim[axis]);
|
||||
if (tmax<tmin)
|
||||
{
|
||||
temp = tmax;
|
||||
tmax = tmin;
|
||||
tmin = temp;
|
||||
}
|
||||
|
||||
// Adjust Range Max
|
||||
//------------------
|
||||
if (tmax<Tr.mRangeMax)
|
||||
{
|
||||
Tr.mRangeMax=tmax;
|
||||
}
|
||||
|
||||
// Adjust Range Min
|
||||
//------------------
|
||||
if (tmin>Tr.mRange)
|
||||
{
|
||||
Tr.mRange=tmin;
|
||||
Tr.mNormal.Clear();
|
||||
Tr.mNormal[axis]=-1.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Missed?
|
||||
//---------
|
||||
if (Tr.mRangeMax<Tr.mRange || Tr.mRangeMax<0.0f || Tr.mRange>Tr.mLength)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Start Solid Conditions
|
||||
//------------------------
|
||||
if (Tr.mRange<0.0f)
|
||||
{
|
||||
Tr.mRange = 0.0f;
|
||||
Tr.mPoint = Tr.mStart;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// Calculate The End Point
|
||||
//-------------------------
|
||||
Tr.mPoint = Tr.mAim;
|
||||
Tr.mPoint *= Tr.mRange;
|
||||
Tr.mPoint += Tr.mStart;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
void CBBox::FromStr(const char *s)
|
||||
{
|
||||
assert(s && s[0]);
|
||||
|
||||
char MinS[256];
|
||||
char MaxS[266];
|
||||
sscanf(s, "(%s|%s)", MinS, MaxS);
|
||||
|
||||
mMin.FromStr(MinS);
|
||||
mMax.FromStr(MaxS);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
void CBBox::ToStr(char* s)
|
||||
{
|
||||
assert(s && s[0]);
|
||||
|
||||
char MinS[256];
|
||||
char MaxS[266];
|
||||
|
||||
mMin.ToStr(MinS);
|
||||
mMax.ToStr(MaxS);
|
||||
sprintf(s, "(%s|%s)", MinS, MaxS);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
void CBBox::Validate()
|
||||
{
|
||||
assert(mMax>=mMin);
|
||||
}
|
||||
|
||||
188
code/ravl/CBounds.h
Normal file
188
code/ravl/CBounds.h
Normal file
@@ -0,0 +1,188 @@
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// RAVEN STANDARD TEMPLATE LIBRARY
|
||||
// (c) 2002 Activision
|
||||
//
|
||||
//
|
||||
// Vector Library
|
||||
// --------------
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// NOTES:
|
||||
// 05/31/02 - CREATED
|
||||
//
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
#if !defined(RAVL_BOUNDS_INC)
|
||||
#define RAVL_BOUNDS_INC
|
||||
//namespace ravl
|
||||
//{
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Includes
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
#include "CVec.h"
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Defines
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
#define RAVL_BB_EMPTY_MIN ( 1.234567E30f) // Empty Value
|
||||
#define RAVL_BB_EMPTY_MAX (-1.234567E30f) // Empty Value
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Enums And Typedefs
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
typedef unsigned char TPlanes;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// The Bounds Trace
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
class CBTrace
|
||||
{
|
||||
public:
|
||||
CBTrace(const CVec3& Start, const CVec3& Stop) :
|
||||
mStart(Start),
|
||||
mStop(Stop),
|
||||
mAim(Stop),
|
||||
mRange(0),
|
||||
mRangeMax(0),
|
||||
mPoint(Stop)
|
||||
{
|
||||
mAim-=Start;
|
||||
mLength = mAim.Norm();
|
||||
}
|
||||
|
||||
CBTrace& operator =(const CBTrace& T)
|
||||
{
|
||||
mStart = (T.mStart);
|
||||
mStop = (T.mStop);
|
||||
mAim = (T.mAim);
|
||||
mRange = (T.mRange);
|
||||
mRangeMax = (T.mRangeMax);
|
||||
mPoint = (T.mPoint);
|
||||
return (*this);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Setup Values, Do Not Change
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
public:
|
||||
CVec3 mStart;
|
||||
CVec3 mStop;
|
||||
CVec3 mAim;
|
||||
float mLength;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Results
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
public:
|
||||
float mRange;
|
||||
float mRangeMax;
|
||||
CVec3 mPoint;
|
||||
CVec3 mNormal;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// The Bounding Box Class
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
class CBBox
|
||||
{
|
||||
public:
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Constructors
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
CBBox() {mMin.Set(RAVL_BB_EMPTY_MIN); mMax.Set(RAVL_BB_EMPTY_MAX);}
|
||||
CBBox(float Radius) {mMin.Set(-Radius); mMax.Set(Radius);}
|
||||
CBBox(const CVec3& t) {mMin=t; mMax=t;}
|
||||
CBBox(const CVec3& min, const CVec3& max) {mMin=min; mMax=max;}
|
||||
CBBox(const CBBox& t) {mMin=t.mMin; mMax=t.mMax;}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Initializers
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
void Set(const CVec3& min, const CVec3& max) {mMin=min; mMax=max; Validate();}
|
||||
void Clear() {mMin.Set(RAVL_BB_EMPTY_MIN); mMax.Set(RAVL_BB_EMPTY_MAX);}
|
||||
void AddPoint(const CVec3 &p) {mMin.Min(p); mMax.Max(p); Validate();}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Accessors
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
bool IsEmpty() const {return (mMin[0]==RAVL_BB_EMPTY_MIN);}
|
||||
CVec3 Center() const {return (mMin+mMax)*0.5;}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Translation, Rotation, Expansion
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
void Translate(const CVec3 &f) {mMin+=f; mMax+=f;}
|
||||
void Expand(float x) {mMin-=x; mMax+=x;}
|
||||
void Expand(const CVec3 &f) {mMin-=f; mMax+=f;}
|
||||
// void ThroughMatrix(const CBBox &from, const CMatrix4 &mat);
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Volumetric & Area Operations
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
float Volume() const {return (mMax[0]-mMin[0])*(mMax[1]-mMin[1])*(mMax[2]-mMin[2]);}
|
||||
float AxisSize(int axis) const {return (mMax[axis]-mMin[axis]);}
|
||||
float LargestAxisSize() const;
|
||||
float DistanceEstimate(const CVec3 &p) const; // Manhattan Distance
|
||||
float AreaEstimate(const CVec3 &p) const; // Manhattan Distance * LargestAxisSize()
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Set Operations
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
void Intersect(const CBBox &b2);
|
||||
void Union(const CBBox &b2);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Tests
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
ESide InOutTest(const CVec3 &p) const;
|
||||
ESide InOutTest(const CVec3 &p, float tolout, float tolin) const;
|
||||
bool BoxTouchTest(const CBBox &b2, float tolout=0.0f) const;
|
||||
bool SphereTouchTest(const CVec3 &c, float rad) const;
|
||||
TPlanes PlaneFlags(const CVec3 &p);
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Hit Tests
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
bool HitTest(CBTrace& Tr) const;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// String Operations
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
void FromStr(const char *s);
|
||||
void ToStr(char* s);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Debug Operations
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
void Validate();
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Data
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
public:
|
||||
CVec3 mMin;
|
||||
CVec3 mMax;
|
||||
};
|
||||
|
||||
//};
|
||||
#endif
|
||||
165
code/ravl/CMatrix.h
Normal file
165
code/ravl/CMatrix.h
Normal file
@@ -0,0 +1,165 @@
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// RAVEN STANDARD TEMPLATE LIBRARY
|
||||
// (c) 2002 Activision
|
||||
//
|
||||
//
|
||||
// Matrix Library
|
||||
// --------------
|
||||
//
|
||||
//
|
||||
//
|
||||
// NOTES:
|
||||
//
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
#if !defined(RAVL_MATRIX_INC)
|
||||
#define RAVL_MATRIX_INC
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Includes
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
#if defined(RA_DEBUG_LINKING)
|
||||
#pragma message("...including CMatrix.h")
|
||||
#endif
|
||||
#if !defined(RAVL_VEC_INC)
|
||||
#include "CVec.h"
|
||||
#endif
|
||||
//namespace ravl
|
||||
//{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// The Matrix
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
class CMatrix
|
||||
{
|
||||
public:
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Constructors
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
CMatrix() {}
|
||||
CMatrix(const CVec4& x,const CVec4& y,const CVec4& z, const CVec4& w) {v[0]=x; v[1]=y; v[2]=z; v[3]=w;}
|
||||
CMatrix(const CMatrix& t) {v[0]=t.v[0]; v[1]=t.v[1]; v[2]=t.v[2]; v[3]=t.v[3];}
|
||||
CMatrix(const float t[16]) {v[0]=t[0]; v[1]=t[4]; v[2]=t[8]; v[3]=t[12];}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Initializers
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
void Set(const CVec4& x,const CVec4& y,const CVec4& z, const CVec4& w) {v[0]=x; v[1]=y; v[2]=z; v[3]=w;}
|
||||
void Set(const CMatrix& t) {v[0]=t.v[0]; v[1]=t.v[1]; v[2]=t.v[2]; v[3]=t.v[3];}
|
||||
void Set(const float t[16]) {v[0]=t[0]; v[1]=t[4]; v[2]=t[8]; v[3]=t[12];}
|
||||
|
||||
void Clear() {v[0].Set(0,0,0,0); v[1].Set(0,0,0,0); v[2].Set(0,0,0,0); v[3].Set(0,0,0,0);}
|
||||
void Itentity() {v[0].Set(1,0,0,0); v[1].Set(0,1,0,0); v[2].Set(0,0,1,0); v[3].Set(0,0,0,1);}
|
||||
void Translate(const float x, const float y, const float z) {v[0].Set(1,0,0,0); v[1].Set(0,1,0,0); v[2].Set(0,0,1,0); v[3].Set(x,y,z,1);}
|
||||
void Scale(const float x, const float y, const float z) {v[0].Set(x,0,0,0); v[1].Set(0,y,0,0); v[2].Set(0,0,z,0); v[3].Set(0,0,0,1);}
|
||||
void Rotate(int axis, const float s/*sin(angle)*/, const float c/*cos(angle)*/)
|
||||
{
|
||||
switch(axis)
|
||||
{
|
||||
case 0:
|
||||
v[0].Set( 1, 0, 0, 0);
|
||||
v[1].Set( 0, c,-s, 0);
|
||||
v[2].Set( 0, s, c, 0);
|
||||
break;
|
||||
case 1:
|
||||
v[0].Set( c, 0, s, 0);
|
||||
v[1].Set( 0, 1, 0, 0);
|
||||
v[2].Set(-s, 0, c, 0);
|
||||
break;
|
||||
case 2:
|
||||
v[0].Set( c,-s, 0, 0);
|
||||
v[1].Set( s, c, 0, 0);
|
||||
v[2].Set( 0, 0, 1, 0);
|
||||
break;
|
||||
}
|
||||
v[3].Set( 0, 0, 0, 1);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Member Accessors
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
const CVec4& operator[](int i) const {return v[i];}
|
||||
CVec4& operator[](int i) {return v[i];}
|
||||
|
||||
CVec4& up() {return v[0];}
|
||||
CVec4& left() {return v[1];}
|
||||
CVec4& fwd() {return v[2];}
|
||||
CVec4& origin() {return v[3];}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Equality / Inequality Operators
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
bool operator== (const CMatrix& t) const {return (v[0]==t.v[0] && v[1]==t.v[1] && v[2]==t.v[2] && v[3]==t.v[3]);}
|
||||
bool operator!= (const CMatrix& t) const {return !(v[0]==t.v[0] && v[1]==t.v[1] && v[2]==t.v[2] && v[3]==t.v[3]);}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Basic Arithimitic Operators
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
const CMatrix &operator= (const CMatrix& t) {v[0]=t.v[0]; v[1]=t.v[1]; v[2]=t.v[2]; v[3]=t.v[3]; return *this;}
|
||||
const CMatrix &operator+= (const CMatrix& t) {v[0]+=t.v[0]; v[1]+=t.v[1]; v[2]+=t.v[2]; v[3]+=t.v[3];return *this;}
|
||||
const CMatrix &operator-= (const CMatrix& t) {v[0]-=t.v[0]; v[1]-=t.v[1]; v[2]-=t.v[2]; v[3]-=t.v[3];return *this;}
|
||||
|
||||
CMatrix operator+ (const CMatrix &t) const {return CMatrix(v[0]+t.v[0], v[1]+t.v[1], v[2]+t.v[2], v[3]+t.v[3]);}
|
||||
CMatrix operator- (const CMatrix &t) const {return CMatrix(v[0]-t.v[0], v[1]-t.v[1], v[2]-t.v[2], v[3]-t.v[3]);}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Matrix Scale
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
const CMatrix &operator*= (const float d) {v[0]*=d; v[1]*=d; v[2]*=d; v[3]*=d; return *this;}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Matrix To Matrix Multiply
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
CMatrix operator* (const CMatrix &t) const
|
||||
{
|
||||
// assert(this!=&t); // Don't Multiply With Self
|
||||
|
||||
CMatrix Result; // The Resulting Matrix
|
||||
int i,j,k; // Counters
|
||||
float Accumulator; // Current Value Of The Dot Product
|
||||
for (i=0; i<4; i++)
|
||||
{
|
||||
for (j=0; j<4; j++)
|
||||
{
|
||||
Accumulator = 0.0f; // Reset The Accumulator
|
||||
for(k=0; k<4; k++)
|
||||
{
|
||||
Accumulator += v[i][k]*t[k][j]; // Calculate Dot Product Of The Two Vectors
|
||||
}
|
||||
Result[i][j]=Accumulator; // Place In Result
|
||||
}
|
||||
}
|
||||
|
||||
return Result;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// Vector To Matrix Multiply
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
CVec4 operator* (const CVec4 &t) const
|
||||
{
|
||||
CVec4 Result;
|
||||
|
||||
Result[0] = v[0][0]*t[0] + v[1][0]*t[1] + v[2][0]*t[2] + v[3][0];
|
||||
Result[1] = v[0][1]*t[0] + v[1][1]*t[1] + v[2][1]*t[2] + v[3][1];
|
||||
Result[2] = v[0][2]*t[0] + v[1][2]*t[1] + v[2][2]*t[2] + v[3][2];
|
||||
|
||||
return Result;
|
||||
}
|
||||
|
||||
public:
|
||||
CVec4 v[4];
|
||||
};
|
||||
|
||||
|
||||
|
||||
//}
|
||||
#endif
|
||||
1154
code/ravl/CVec.cpp
Normal file
1154
code/ravl/CVec.cpp
Normal file
File diff suppressed because it is too large
Load Diff
1002
code/ravl/CVec.h
Normal file
1002
code/ravl/CVec.h
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user