1 //****************************************************************************//
2 // vector.h                                                                   //
3 // Copyright (C) 2001, 2002 Bruno 'Beosil' Heidelberger                       //
4 //****************************************************************************//
5 // This library is free software; you can redistribute it and/or modify it    //
6 // under the terms of the GNU Lesser General Public License as published by   //
7 // the Free Software Foundation; either version 2.1 of the License, or (at    //
8 // your option) any later version.                                            //
9 //****************************************************************************//
10 
11 #ifndef CAL_VECTOR_H
12 #define CAL_VECTOR_H
13 
14 //****************************************************************************//
15 // Includes                                                                   //
16 //****************************************************************************//
17 
18 #include "cal3d/global.h"
19 #include "cal3d/matrix.h"
20 
21 //****************************************************************************//
22 // Forward declarations                                                       //
23 //****************************************************************************//
24 
25 class CalQuaternion;
26 //class CalMatrix;
27 
28 //****************************************************************************//
29 // Class declaration                                                          //
30 //****************************************************************************//
31 
32  /*****************************************************************************/
33 /** The vector class.
34   *****************************************************************************/
35 
36 class CAL3D_API CalVector
37 {
38 // member variables
39 public:
40   float x ,y ,z;
41 
42 // constructors/destructor
43 public:
CalVector()44   inline CalVector(): x(0.0f), y(0.0f), z(0.0f) {};
CalVector(const CalVector & v)45   inline CalVector(const CalVector& v) : x(v.x), y(v.y), z(v.z) {};
CalVector(float vx,float vy,float vz)46   inline CalVector(float vx, float vy, float vz): x(vx), y(vy), z(vz) {};
~CalVector()47   inline ~CalVector() {};
48 
49 // member functions
50 public:
51   inline float& operator[](unsigned int i)
52   {
53 	  return (&x)[i];
54   }
55 
56   inline const float& operator[](unsigned int i) const
57   {
58 	  return (&x)[i];
59   }
60 
61   inline void operator=(const CalVector& v)
62   {
63 	  x = v.x;
64 	  y = v.y;
65 	  z = v.z;
66   }
67 
68   inline void operator+=(const CalVector& v)
69   {
70 	  x += v.x;
71 	  y += v.y;
72 	  z += v.z;
73   }
74 
75 
76   inline void operator-=(const CalVector& v)
77   {
78 	  x -= v.x;
79 	  y -= v.y;
80 	  z -= v.z;
81   }
82 
83   inline void operator*=(const float d)
84   {
85 	  x *= d;
86 	  y *= d;
87 	  z *= d;
88   }
89 
90   void operator*=(const CalQuaternion& q);
91 
92   inline void operator*=(const CalMatrix &m)
93   {
94 	  float ox = x;
95 	  float oy = y;
96 	  float oz = z;
97 	  x = m.dxdx*ox + m.dxdy*oy + m.dxdz*oz;
98 	  y = m.dydx*ox + m.dydy*oy + m.dydz*oz;
99 	  z = m.dzdx*ox + m.dzdy*oy + m.dzdz*oz;
100   }
101 
102   inline void operator/=(const float d)
103   {
104 	  x /= d;
105 	  y /= d;
106 	  z /= d;
107   }
108 
109   inline bool operator==(const CalVector& v) const
110   {
111 	  return ((x == v.x) && (y == v.y) && (z == v.z));
112   }
113 
114   inline bool operator!=(const CalVector& v) const
115   {
116     return !operator==(v);
117   }
118 
blend(float d,const CalVector & v)119   inline void blend(float d, const CalVector& v)
120   {
121 	  x += d * (v.x - x);
122 	  y += d * (v.y - y);
123 	  z += d * (v.z - z);
124   }
125 
clear()126   inline void clear()
127   {
128 	  x=0.0f;
129 	  y=0.0f;
130 	  z=0.0f;
131   }
132 
length()133   inline float length() const
134   {
135 	  return (float)sqrt(x * x + y * y + z * z);
136   }
normalize()137   inline float normalize()
138   {
139 	  // calculate the length of the vector
140 	  float length;
141 	  length = (float) sqrt(x * x + y * y + z * z);
142 
143 	  // normalize the vector
144 	  x /= length;
145 	  y /= length;
146 	  z /= length;
147 
148 	  return length;
149   }
150 
set(float vx,float vy,float vz)151   void set(float vx, float vy, float vz)
152   {
153 	  x = vx;
154 	  y = vy;
155 	  z = vz;
156   }
157 
158 };
159 
160 static inline CalVector operator+(const CalVector& v, const CalVector& u)
161 {
162   return CalVector(v.x + u.x, v.y + u.y, v.z + u.z);
163 }
164 
165 static inline CalVector operator-(const CalVector& v, const CalVector& u)
166 {
167 	return CalVector(v.x - u.x, v.y - u.y, v.z - u.z);
168 }
169 
170 static inline CalVector operator*(const CalVector& v, const float d)
171 {
172 	return CalVector(v.x * d, v.y * d, v.z * d);
173 }
174 
175 static inline CalVector operator*(const float d, const CalVector& v)
176 {
177 	return CalVector(v.x * d, v.y * d, v.z * d);
178 }
179 
180 static inline CalVector operator/(const CalVector& v, const float d)
181 {
182 	return CalVector(v.x / d, v.y / d, v.z / d);
183 }
184 
185 static inline float operator*(const CalVector& v, const CalVector& u)
186 {
187 	return v.x * u.x + v.y * u.y + v.z * u.z;
188 }
189 
190 static inline CalVector operator%(const CalVector& v, const CalVector& u)
191 {
192 	return CalVector(v.y * u.z - v.z * u.y, v.z * u.x - v.x * u.z, v.x * u.y - v.y * u.x);
193 }
194 
195 
196  /*****************************************************************************/
197 /** The plane class.
198   *****************************************************************************/
199 
200 
201 class CAL3D_API CalPlane
202 {
203    public:
204       float a,b,c,d;
205 
206       // These methods are made only to calculate the bounding boxes,
207       // don't use them in you program
208 
209       float eval(CalVector &p);
210 	  float dist(CalVector &p);
211       void setPosition(CalVector &p);
212       void setNormal(CalVector &p);
213 };
214 
215  /*****************************************************************************/
216 /** The bounding box class.
217   *****************************************************************************/
218 
219 
220 class CAL3D_API CalBoundingBox
221 {
222    public:
223      CalPlane plane[6];
224 
225      void computePoints(CalVector *p);
226 
227 };
228 
229 
230 
231 #endif
232 
233 //****************************************************************************//
234