Morph-SynRJ
view mth_vector3.h @ 0:22de913c2d84
morph-synrj intro
| author | "Cedric Pinson <cedric.pinson@alcove.fr> <mornifle@plopbyte.net>" |
|---|---|
| date | Tue Nov 27 15:23:52 2007 +0100 (2007-11-27) |
| parents | |
| children |
line source
1 /** @file mth_vector3.h
2 *
3 * @brief 3 dimensional vectors
4 *
5 *****************************************************************************
6 *
7 * @author psc80
8 *
9 * @date Created 2001/05
10 *
11 * @version $Id: mth_vector3.h,v 1.3 2004/01/22 10:53:04 psc80 Exp $
12 *
13 ****************************************************************************/
15 #ifndef MTH_VECTOR3_H
16 #define MTH_VECTOR3_H
19 #include <cmath>
20 #include "mth.h"
21 #include "sys_assert.h"
23 namespace mth {
26 class Vector3_t {
28 protected:
30 float c[3];
32 public:
35 /// do nothing
36 Vector3_t() {}
39 /// constructo with scalar
40 Vector3_t(float _a,float _b,float _c) {
41 Init(_a,_b,_c);
42 }
46 /// Initialize the vector with another vector
47 Vector3_t(const Vector3_t& _v) {
48 c[0]=(float)_v[0]; c[1]=(float)_v[1]; c[2]=(float)_v[2];
49 }
52 void InitRandomVectorInBox(float _sizeBox) {
53 for (int j=0;j<3;j++)
54 c[j]=-_sizeBox/2 + (_sizeBox*mth::Rand()/(mth::RANDOM_MAX+1));
55 }
58 /// Set the vector to 0
59 void Init() {
60 c[0]=0; c[1]=0; c[2]=0;
61 }
64 /// Set the vector to 0
65 void Init(float _a,float _b,float _c) {
66 c[0]=_a; c[1]=_b; c[2]=_c;
67 }
70 /// Initialize the vector with another vector
71 void Init(const Vector3_t& _v) {
72 c[0]=(float)_v.c[0]; c[1]=(float)_v.c[1]; c[2]=(float)_v.c[2];
73 }
77 /// Compute the dot product against another vector
78 float Dot(const Vector3_t& _v) const {
79 return c[0]*_v.c[0]+c[1]*_v.c[1]+c[2]*_v.c[2];
80 }
84 /// Multiply by row
85 Vector3_t Multiply(const Vector3_t& _v) const {
86 return Vector3_t(c[0]*_v.c[0],c[1]*_v.c[1],c[2]*_v.c[2]);
87 }
91 /// Return the squared magnitude (length) of the vector
92 float MagSqr() const {
93 return c[0]*c[0]+c[1]*c[1]+c[2]*c[2];
94 }
97 /// Return the magnitude (length) of the vector
98 float Mag() const { return Sqrt(MagSqr()); }
102 /// Return the squared distance between two vectors
103 float DistSqr(const Vector3_t& _v) const {
104 return (*this-_v).MagSqr();}
107 /// Return the distance between two vectors
108 float Dist(const Vector3_t& _v) const {
109 return (*this-_v).Mag(); }
114 /** Normalize this vector
115 *
116 * @return int 0 if succeded, -1 if the vector was zero or too small
117 * (its squared magnitude was less than MTH_EPSILON)
118 */
119 int Normalize() {
120 float a = MagSqr();
121 SYS_ASSERT(a>=0);
122 *this *= (float)(1./Sqrt(a));
123 return 0;
124 }
127 /// Cross product
128 Vector3_t Cross( const Vector3_t& _v ) const {
129 return Vector3_t(c[1]*_v.c[2] - c[2]*_v.c[1],
130 c[2]*_v.c[0] - c[0]*_v.c[2],
131 c[0]*_v.c[1] - c[1]*_v.c[0]);
132 }
139 Vector3_t operator ^ (const Vector3_t& _v) const {
140 return this->Cross(_v); }
143 /// Coordinates accessor operator
144 float& operator [] (int _i) { return c[_i]; }
147 /// Const coordinates accessor operator
148 const float& operator [] (int _i) const { return c[_i]; }
151 /// Operator * (dot product)
152 float operator * (const Vector3_t& _v) const {
153 return this->Dot(_v);}
156 /// Addition of two vectors
157 Vector3_t operator + (const Vector3_t& _v) const {
158 return Vector3_t(c[0]+_v.c[0],c[1]+_v.c[1],c[2]+_v.c[2]);
159 }
162 /// Substract of two vectors
163 Vector3_t operator - (const Vector3_t& _v) const {
164 return Vector3_t(c[0]-_v.c[0],c[1]-_v.c[1],c[2]-_v.c[2]);
165 }
168 /// Comparaison operator
169 bool operator == ( const Vector3_t& _v ) const {
170 return c[0]==_v.c[0] && c[1]==_v.c[1] && c[2]==_v.c[2];
171 }
174 /// Comparaison operator
175 bool operator != ( const Vector3_t& _v ) const {
176 return !(*this==_v);}
179 /// += operator
180 const Vector3_t& operator += ( const Vector3_t& _v ) {
181 c[0]+=_v.c[0]; c[1]+=_v.c[1]; c[2]+=_v.c[2];
182 return *this;}
185 /// -= operator
186 const Vector3_t& operator -= ( const Vector3_t& _v ) {
187 c[0]-=_v.c[0]; c[1]-=_v.c[1]; c[2]-=_v.c[2];
188 return *this;}
191 /// *= operator
192 const Vector3_t& operator *= ( const float& _v ) {
193 c[0]*=_v; c[1]*=_v; c[2]*=_v;
194 return *this;}
197 /// /= operator
198 const Vector3_t& operator /= ( const float& _v ) {
199 float a=(float)1.0/_v;
200 c[0]*=a; c[1]*=a; c[2]*=a;
201 return *this;}
205 /// Operator -
206 Vector3_t operator - () const {
207 return Vector3_t(-c[0],-c[1],-c[2]);
208 }
211 /// Mutliplication by a scalar value (to the right)
212 Vector3_t operator * ( const float& _a) const {
213 return Vector3_t(c[0]*_a,c[1]*_a,c[2]*_a);
214 }
217 /// Division by a scalar value
218 Vector3_t operator / (const float& _v) const {
219 float a=(float)1.0/_v;
220 return Vector3_t(c[0]*a,c[1]*a,c[2]*a);
221 }
225 typedef const float* ConversionForGL;
226 operator ConversionForGL() const { return &c[0];}
229 };
232 // Vector3_t operator * (const float& _a, const Vector3_t& _v) {
233 // return Vector3_t(_a*_v[0], _a*_v[1], _a*_v[2]); }
236 }
238 #endif
