00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef CAL_VECTOR_H
00012 #define CAL_VECTOR_H
00013
00014
00015
00016
00017
00018 #include "cal3d/global.h"
00019 #include "cal3d/matrix.h"
00020
00021
00022
00023
00024
00025 class CalQuaternion;
00026
00027
00028
00029
00030
00031
00032
00036 class CAL3D_API CalVector
00037 {
00038
00039 public:
00040 float x ,y ,z;
00041
00042
00043 public:
00044 inline CalVector(): x(0.0f), y(0.0f), z(0.0f) {};
00045 inline CalVector(const CalVector& v) : x(v.x), y(v.y), z(v.z) {};
00046 inline CalVector(float vx, float vy, float vz): x(vx), y(vy), z(vz) {};
00047 inline ~CalVector() {};
00048
00049
00050 public:
00051 inline float& operator[](unsigned int i)
00052 {
00053 return (&x)[i];
00054 }
00055
00056 inline const float& operator[](unsigned int i) const
00057 {
00058 return (&x)[i];
00059 }
00060
00061 inline void operator=(const CalVector& v)
00062 {
00063 x = v.x;
00064 y = v.y;
00065 z = v.z;
00066 }
00067
00068 inline void operator+=(const CalVector& v)
00069 {
00070 x += v.x;
00071 y += v.y;
00072 z += v.z;
00073 }
00074
00075
00076 inline void operator-=(const CalVector& v)
00077 {
00078 x -= v.x;
00079 y -= v.y;
00080 z -= v.z;
00081 }
00082
00083 inline void operator*=(const float d)
00084 {
00085 x *= d;
00086 y *= d;
00087 z *= d;
00088 }
00089
00090 void operator*=(const CalQuaternion& q);
00091
00092 inline void operator*=(const CalMatrix &m)
00093 {
00094 float ox = x;
00095 float oy = y;
00096 float oz = z;
00097 x = m.dxdx*ox + m.dxdy*oy + m.dxdz*oz;
00098 y = m.dydx*ox + m.dydy*oy + m.dydz*oz;
00099 z = m.dzdx*ox + m.dzdy*oy + m.dzdz*oz;
00100 }
00101
00102 inline void operator/=(const float d)
00103 {
00104 x /= d;
00105 y /= d;
00106 z /= d;
00107 }
00108
00109 inline bool operator==(const CalVector& v) const
00110 {
00111 return ((x == v.x) && (y == v.y) && (z == v.z));
00112 }
00113
00114 inline bool operator!=(const CalVector& v) const
00115 {
00116 return !operator==(v);
00117 }
00118
00119 inline void blend(float d, const CalVector& v)
00120 {
00121 x += d * (v.x - x);
00122 y += d * (v.y - y);
00123 z += d * (v.z - z);
00124 }
00125
00126 inline void clear()
00127 {
00128 x=0.0f;
00129 y=0.0f;
00130 z=0.0f;
00131 }
00132
00133 inline float length() const
00134 {
00135 return (float)sqrt(x * x + y * y + z * z);
00136 }
00137 inline float normalize()
00138 {
00139
00140 float length;
00141 length = (float) sqrt(x * x + y * y + z * z);
00142
00143 float lenRecip = 1.0f / length;
00144
00145
00146 x *= lenRecip;
00147 y *= lenRecip;
00148 z *= lenRecip;
00149
00150 return length;
00151 }
00152
00153 void set(float vx, float vy, float vz)
00154 {
00155 x = vx;
00156 y = vy;
00157 z = vz;
00158 }
00159
00160 };
00161
00162 static inline CalVector operator+(const CalVector& v, const CalVector& u)
00163 {
00164 return CalVector(v.x + u.x, v.y + u.y, v.z + u.z);
00165 }
00166
00167 static inline CalVector operator-(const CalVector& v, const CalVector& u)
00168 {
00169 return CalVector(v.x - u.x, v.y - u.y, v.z - u.z);
00170 }
00171
00172 static inline CalVector operator*(const CalVector& v, const float d)
00173 {
00174 return CalVector(v.x * d, v.y * d, v.z * d);
00175 }
00176
00177 static inline CalVector operator*(const float d, const CalVector& v)
00178 {
00179 return CalVector(v.x * d, v.y * d, v.z * d);
00180 }
00181
00182 static inline CalVector operator/(const CalVector& v, const float d)
00183 {
00184 return CalVector(v.x / d, v.y / d, v.z / d);
00185 }
00186
00187 static inline float operator*(const CalVector& v, const CalVector& u)
00188 {
00189 return v.x * u.x + v.y * u.y + v.z * u.z;
00190 }
00191
00192 static inline CalVector operator%(const CalVector& v, const CalVector& u)
00193 {
00194 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);
00195 }
00196
00197
00198
00203 class CAL3D_API CalPlane
00204 {
00205 public:
00206 float a,b,c,d;
00207
00208
00209
00210
00211 float eval(const CalVector &p);
00212 float dist(CalVector &p);
00213 void setPosition(const CalVector &p);
00214 void setNormal(CalVector &p);
00215 };
00216
00217
00222 class CAL3D_API CalBoundingBox
00223 {
00224 public:
00225 CalPlane plane[6];
00226
00227 void computePoints(CalVector *p);
00228
00229 };
00230
00231
00232
00233 #endif
00234
00235