LightWrap++
AC++wrapperfortheLightWave3DSDK
vector3d.h
Go to the documentation of this file.
1 
5 #ifndef Vector3_H
6 #define Vector3_H
7 
8 #include "lwpp/math.h"
9 
10 namespace lwpp
11 {
12 
13 #ifndef PI
14  #define PI (3.14159265358979324)
15 #endif
16 #ifndef HALFPI
17  #define HALFPI (PI*0.5)
18 #endif
19 #ifndef TWOPI
20  #define TWOPI (PI*2.0)
21 #endif
22 
23  template <typename T> class Point3;
24 
27  template <typename T>
28  class Vector3 {
29  public:
30  T x, y, z;
31  public:
33 
36  explicit Vector3 (T n = 0.0)
37  : x(n), y(n), z(n) {}
39 
44  Vector3 (T _x, T _y, T _z)
45  : x(_x), y(_y), z(_z) {};
47 
50  explicit Vector3 (const T n[3])
51  : x(n[0]), y(n[1]), z(n[2]) {}
52  explicit Vector3 (const Point3<T> &p);
54  Vector3 (T theta, T phi)
55  : x( -sin(theta) * cos(phi) ),
56  y( -sin(theta) * sin(phi) ),
57  z( cos(theta) )
58  {}
59 
60  inline T *asLWVector() {return &x;}
62  inline T Dot(Vector3 a) const;
63  inline Vector3 Cross(Vector3 a) const;
65  inline T Magnitude() const {return sqrt(Sqr(x) + Sqr(y) + Sqr(z));}
67  inline Vector3 &Normalize();
68 
69  T &X() {return x;}
70  T &Y() {return y;}
71  T &Z() {return z;}
72 
73  inline void Translate (Vector3 dir, T distance) {
74  x += dir.x * distance;
75  y += dir.y * distance;
76  z += dir.z * distance;
77  }
81  x += b.x;
82  y += b.y;
83  z += b.z;
84  return *this;
85  }
88  {
89  return Vector3 (x + b.x, y + b.y, z + b.z);
90  }
93  x -= b.x;
94  y -= b.y;
95  z -= b.z;
96  return *this;
97  }
100  {
101  return Vector3 (x - b.x, y - b.y, z - b.z);
102  }
103 
105  x *= b.x;
106  y *= b.y;
107  z *= b.z;
108  return *this;
109  }
111  {
112  return Vector3 (x * b.x, y * b.y, z * b.z);
113  }
114 
115  inline Vector3& operator*= (T b)
116  {
117  x *= b;
118  y *= b;
119  z *= b;
120  return *this;
121  }
122  inline Vector3 operator* (double b)
123  {
124  return Vector3 (x * b, y * b, z * b);
125  }
126 
128  {
129  x /= b.x;
130  y /= b.y;
131  z /= b.z;
132  return *this;
133  }
135  {
136  return Vector3 (x / b.x, y / b.y, z / b.z);
137  }
138  inline Vector3& operator/= (T b) {
139  T ib = 1.0 / b;
140  x *= ib;
141  y *= ib;
142  z *= ib;
143  return *this;
144  }
145  inline Vector3 operator/ (T b)
146  {
147  T ib = 1.0 / b;
148  return Vector3 (x * ib, y * ib, z * ib);
149  }
150 
151  inline T SphericalTheta()
152  {
153  return acos(-y);
154  }
155  inline T SphericalPhi()
156  {
157  float p = atan2(x, z);
158  return (p < 0.0) ? p + TWOPI : p;
159  }
160 
161  inline void Abs()
162  {
163  x = x > 0 ? x : -x;
164  y = y > 0 ? y : -y;
165  z = z > 0 ? z : -z;
166  }
167  };
168 
170 
175  template <typename T>
177  {
178  return Vector3<T>(a.y * b.z - a.z * b.y,
179  a.z * b.x - a.x * b.z,
180  a.x * b.y - a.y * b.x);
181  }
182 
183  template <typename T>
184  inline T Vector3<T>::Dot(Vector3<T> a) const
185  {
186  return (x*a.x + y*a.y + z*a.z);
187  }
188 
189  template <typename T>
191  {
192  return Vector3<T>( y*a.z - z*a.y, z*a.x - x*a.z, x*a.y - y*a.x );
193  }
194 
195  template <typename T>
197  {
198  *this /= Magnitude();
199  return *this;
200  }
201 
203  template <typename T>
204  inline void CoordinateSystem(const Vector3<T> &v1, Vector3<T> *v2, Vector3<T> *v3)
205  {
206  if (abs(v1.x) > abs(v1.y))
207  {
208  T invLen = 1.0 / sqrt(v1.x * v1.x + v1.z * v1.z);
209  *v2 = Vector3<T>(-v1.z * invLen, 0.0, v1.x * invLen);
210  }
211  else
212  {
213  T invLen = 1.0 / sqrt(v1.y * v1.y + v1.z * v1.z);
214  *v2 = Vector3<T>(0.0, v1.z * invLen, -v1.y * invLen);
215  }
216  *v3 = Cross(v1, *v2);
217  }
219  template <typename T>
220  inline void CoordinateSystem(const Vector3<T> &view, const Vector3<T> &normal, Vector3<T> *right, Vector3<T> *forward)
221  {
222  if (view.Dot(normal) < .1)
223  {
224  CoordinateSystem(normal, right, forward);
225  }
226  else
227  {
228  *right = Cross(view, normal);
229  *forward = Cross(*right, normal);
230  }
231  }
232 
233  typedef Vector3<double> Vector3d;
234  typedef Vector3<float> Vector3f;
235 
236 } // end namespace lwpp
237 #endif // Vector3_H