[930] | 1 | //////////////////////////////////////////////////////////////////////////////////////////
|
---|
| 2 | // VECTOR3D.cpp
|
---|
| 3 | // Function definitions for 3d vector class
|
---|
| 4 | // You may use this code however you wish, but if you do, please credit me and
|
---|
| 5 | // provide a link to my website in a readme file or similar
|
---|
| 6 | // Downloaded from: www.paulsprojects.net
|
---|
| 7 | // Created: 20th July 2002
|
---|
| 8 | // Modified: 8th November 2002 - Changed Constructor layout
|
---|
| 9 | // - Some speed Improvements
|
---|
| 10 | // - Corrected Lerp
|
---|
| 11 | // 17th December 2002 - Converted from radians to degrees
|
---|
| 12 | //////////////////////////////////////////////////////////////////////////////////////////
|
---|
| 13 |
|
---|
| 14 | #include "Maths.h"
|
---|
| 15 |
|
---|
| 16 | void VECTOR3D::Normalize()
|
---|
| 17 | {
|
---|
| 18 | float length=GetLength();
|
---|
| 19 |
|
---|
| 20 | if(length==1 || length==0) //return if length is 1 or 0
|
---|
| 21 | return;
|
---|
| 22 |
|
---|
| 23 | float scalefactor = 1.0f/length;
|
---|
| 24 | x *= scalefactor;
|
---|
| 25 | y *= scalefactor;
|
---|
| 26 | z *= scalefactor;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | VECTOR3D VECTOR3D::GetNormalized() const
|
---|
| 30 | {
|
---|
| 31 | VECTOR3D result(*this);
|
---|
| 32 |
|
---|
| 33 | result.Normalize();
|
---|
| 34 |
|
---|
| 35 | return result;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | VECTOR3D VECTOR3D::GetRotatedX(double angle) const
|
---|
| 39 | {
|
---|
| 40 | if(angle==0.0)
|
---|
| 41 | return (*this);
|
---|
| 42 |
|
---|
| 43 | float sinAngle=(float)sin(M_PI*angle/180);
|
---|
| 44 | float cosAngle=(float)cos(M_PI*angle/180);
|
---|
| 45 |
|
---|
| 46 | return VECTOR3D( x,
|
---|
| 47 | y*cosAngle - z*sinAngle,
|
---|
| 48 | y*sinAngle + z*cosAngle);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | void VECTOR3D::RotateX(double angle)
|
---|
| 52 | {
|
---|
| 53 | (*this)=GetRotatedX(angle);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | VECTOR3D VECTOR3D::GetRotatedY(double angle) const
|
---|
| 57 | {
|
---|
| 58 | if(angle==0.0)
|
---|
| 59 | return (*this);
|
---|
| 60 |
|
---|
| 61 | float sinAngle=(float)sin(M_PI*angle/180);
|
---|
| 62 | float cosAngle=(float)cos(M_PI*angle/180);
|
---|
| 63 |
|
---|
| 64 | return VECTOR3D( x*cosAngle + z*sinAngle,
|
---|
| 65 | y,
|
---|
| 66 | -x*sinAngle + z*cosAngle);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | void VECTOR3D::RotateY(double angle)
|
---|
| 70 | {
|
---|
| 71 | (*this)=GetRotatedY(angle);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | VECTOR3D VECTOR3D::GetRotatedZ(double angle) const
|
---|
| 75 | {
|
---|
| 76 | if(angle==0.0)
|
---|
| 77 | return (*this);
|
---|
| 78 |
|
---|
| 79 | float sinAngle=(float)sin(M_PI*angle/180);
|
---|
| 80 | float cosAngle=(float)cos(M_PI*angle/180);
|
---|
| 81 |
|
---|
| 82 | return VECTOR3D(x*cosAngle - y*sinAngle,
|
---|
| 83 | x*sinAngle + y*cosAngle,
|
---|
| 84 | z);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | void VECTOR3D::RotateZ(double angle)
|
---|
| 88 | {
|
---|
| 89 | (*this)=GetRotatedZ(angle);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | VECTOR3D VECTOR3D::GetRotatedAxis(double angle, const VECTOR3D & axis) const
|
---|
| 93 | {
|
---|
| 94 | if(angle==0.0)
|
---|
| 95 | return (*this);
|
---|
| 96 |
|
---|
| 97 | VECTOR3D u=axis.GetNormalized();
|
---|
| 98 |
|
---|
| 99 | VECTOR3D rotMatrixRow0, rotMatrixRow1, rotMatrixRow2;
|
---|
| 100 |
|
---|
| 101 | float sinAngle=(float)sin(M_PI*angle/180);
|
---|
| 102 | float cosAngle=(float)cos(M_PI*angle/180);
|
---|
| 103 | float oneMinusCosAngle=1.0f-cosAngle;
|
---|
| 104 |
|
---|
| 105 | rotMatrixRow0.x=(u.x)*(u.x) + cosAngle*(1-(u.x)*(u.x));
|
---|
| 106 | rotMatrixRow0.y=(u.x)*(u.y)*(oneMinusCosAngle) - sinAngle*u.z;
|
---|
| 107 | rotMatrixRow0.z=(u.x)*(u.z)*(oneMinusCosAngle) + sinAngle*u.y;
|
---|
| 108 |
|
---|
| 109 | rotMatrixRow1.x=(u.x)*(u.y)*(oneMinusCosAngle) + sinAngle*u.z;
|
---|
| 110 | rotMatrixRow1.y=(u.y)*(u.y) + cosAngle*(1-(u.y)*(u.y));
|
---|
| 111 | rotMatrixRow1.z=(u.y)*(u.z)*(oneMinusCosAngle) - sinAngle*u.x;
|
---|
| 112 |
|
---|
| 113 | rotMatrixRow2.x=(u.x)*(u.z)*(oneMinusCosAngle) - sinAngle*u.y;
|
---|
| 114 | rotMatrixRow2.y=(u.y)*(u.z)*(oneMinusCosAngle) + sinAngle*u.x;
|
---|
| 115 | rotMatrixRow2.z=(u.z)*(u.z) + cosAngle*(1-(u.z)*(u.z));
|
---|
| 116 |
|
---|
| 117 | return VECTOR3D( this->DotProduct(rotMatrixRow0),
|
---|
| 118 | this->DotProduct(rotMatrixRow1),
|
---|
| 119 | this->DotProduct(rotMatrixRow2));
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | void VECTOR3D::RotateAxis(double angle, const VECTOR3D & axis)
|
---|
| 123 | {
|
---|
| 124 | (*this)=GetRotatedAxis(angle, axis);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 | void VECTOR3D::PackTo01()
|
---|
| 129 | {
|
---|
| 130 | (*this)=GetPackedTo01();
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | VECTOR3D VECTOR3D::GetPackedTo01() const
|
---|
| 134 | {
|
---|
| 135 | VECTOR3D temp(*this);
|
---|
| 136 |
|
---|
| 137 | temp.Normalize();
|
---|
| 138 |
|
---|
| 139 | temp=temp*0.5f+VECTOR3D(0.5f, 0.5f, 0.5f);
|
---|
| 140 |
|
---|
| 141 | return temp;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | VECTOR3D operator*(float scaleFactor, const VECTOR3D & rhs)
|
---|
| 145 | {
|
---|
| 146 | return rhs*scaleFactor;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | bool VECTOR3D::operator==(const VECTOR3D & rhs) const
|
---|
| 150 | {
|
---|
| 151 | if(x==rhs.x && y==rhs.y && z==rhs.z)
|
---|
| 152 | return true;
|
---|
| 153 |
|
---|
| 154 | return false;
|
---|
| 155 | }
|
---|
| 156 |
|
---|