home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 3 / RISC_DISC_3.iso / resources / etexts / gems / gemsv / ch7_7 / libgm / gmvec4.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-07  |  5.6 KB  |  255 lines

  1. // gmVector4.h - 4 element vector class
  2. //
  3. // libgm++: Graphics Math Library
  4. // Ferdi Scheepers and Stephen F May
  5. // 15 June 1994
  6.  
  7. #ifndef GMVECTOR4_H
  8. #define GMVECTOR4_H
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <assert.h>
  13. #include <gmUtils.h>
  14.  
  15. class gmVector4 {
  16.  
  17. protected:
  18.   double v_[4];
  19.  
  20. public:
  21.   gmVector4();
  22.   gmVector4(const gmVector4&);
  23.   gmVector4(double, double, double, double);
  24.  
  25.   // array access
  26.  
  27.   double& operator [](int);
  28.   const double& operator [](int) const;
  29.  
  30.   // assignment
  31.  
  32.   gmVector4& assign(double, double, double, double);
  33.   gmVector4& operator =(const gmVector4&);
  34.  
  35.   // math operators
  36.  
  37.   gmVector4& operator +=(const gmVector4&);
  38.   gmVector4& operator -=(const gmVector4&);
  39.   gmVector4& operator *=(double);
  40.   gmVector4& operator /=(double);
  41.  
  42.   gmVector4 operator +(const gmVector4&) const;
  43.   gmVector4 operator -(const gmVector4&) const;
  44.   gmVector4 operator -() const;
  45.   gmVector4 operator *(double) const;
  46.   gmVector4 operator /(double) const;
  47.  
  48. friend gmVector4 operator *(double, const gmVector4&);
  49.  
  50.   bool operator ==(const gmVector4&) const;
  51.   bool operator !=(const gmVector4&) const;
  52.  
  53.   // operations
  54.  
  55.   gmVector4& clamp(double, double);
  56.   double length() const;
  57.   double lengthSquared() const;
  58.   gmVector4& normalize();
  59.  
  60.   void copyTo(float [4]) const;
  61.   void copyTo(double [4]) const;
  62.  
  63. friend double distance(const gmVector4&, const gmVector4&);
  64. friend double distanceSquared(const gmVector4&, const gmVector4&);
  65. friend double dot(const gmVector4&, const gmVector4&);
  66. friend gmVector4 lerp(double, const gmVector4&, const gmVector4&);
  67.  
  68.   // output
  69.  
  70. friend ostream & operator << ( ostream &, const gmVector4 & );
  71. };
  72.  
  73. // CONSTRUCTORS
  74.  
  75. inline gmVector4::gmVector4()
  76. {
  77.   v_[0] = v_[1] = v_[2] = v_[3] = 0;
  78. }
  79.  
  80. inline gmVector4::gmVector4(const gmVector4& v)
  81. {
  82.   v_[0] = v.v_[0]; v_[1] = v.v_[1]; v_[2] = v.v_[2]; v_[3] = v.v_[3];
  83. }
  84.  
  85. inline gmVector4::gmVector4(double x, double y, double z, double a)
  86. {
  87.   v_[0] = x; v_[1] = y; v_[2] = z; v_[3] = a;
  88. }
  89.  
  90. // ARRAY ACCESS
  91.  
  92. inline double& gmVector4::operator [](int i) 
  93. {
  94.   assert(i == 0 || i == 1 || i == 2 || i == 3);
  95.   return v_[i];
  96. }
  97.  
  98. inline const double& gmVector4::operator [](int i) const
  99. {
  100.   assert(i == 0 || i == 1 || i == 2 || i == 3);
  101.   return v_[i];
  102. }
  103.  
  104. // ASSIGNMENT
  105.  
  106. inline gmVector4& gmVector4::assign(double x, double y, double z, double a)
  107. {
  108.   v_[0] = x; v_[1] = y; v_[2] = z; v_[3] = a;
  109.   return *this;
  110. }
  111.  
  112. inline gmVector4& gmVector4::operator =(const gmVector4& v)
  113. {
  114.   v_[0] = v[0]; v_[1] = v[1]; v_[2] = v[2]; v_[3] = v[3];
  115.   return *this;
  116. }
  117.  
  118. // MATH OPERATORS
  119.  
  120. inline gmVector4& gmVector4::operator +=(const gmVector4& v)
  121. {
  122.   v_[0] += v[0]; v_[1] += v[1]; v_[2] += v[2]; v_[3] += v[3];
  123.   return *this;
  124. }
  125.  
  126. inline gmVector4& gmVector4::operator -=(const gmVector4& v)
  127. {
  128.   v_[0] -= v[0]; v_[1] -= v[1]; v_[2] -= v[2]; v_[3] -= v[3];
  129.   return *this;
  130. }
  131.  
  132. inline gmVector4& gmVector4::operator *=(double c)
  133. {
  134.   v_[0] *= c; v_[1] *= c; v_[2] *= c; v_[3] *= c;
  135.   return *this;
  136. }
  137.  
  138. inline gmVector4& gmVector4::operator /=(double c)
  139. {
  140.   assert(!gmIsZero(c));
  141.   v_[0] /= c; v_[1] /= c; v_[2] /= c; v_[3] /= c;
  142.   return *this;
  143. }
  144.  
  145. inline gmVector4 gmVector4::operator +(const gmVector4& v) const
  146. {
  147.   return gmVector4(v_[0] + v[0], v_[1] + v[1], v_[2] + v[2], v_[3] + v[3]);
  148. }
  149.  
  150. inline gmVector4 gmVector4::operator -(const gmVector4& v) const
  151. {
  152.   return gmVector4(v_[0] - v[0], v_[1] - v[1], v_[2] - v[2], v_[3] - v[3]);
  153. }
  154.  
  155. inline gmVector4 gmVector4::operator -() const
  156. {
  157.   return gmVector4(-v_[0], -v_[1], -v_[2], -v_[3]);
  158. }
  159.  
  160. inline gmVector4 gmVector4::operator *(double c) const
  161. {
  162.   return gmVector4(v_[0] * c, v_[1] * c, v_[2] * c, v_[3] * c);
  163. }
  164.  
  165. inline gmVector4 gmVector4::operator /(double c) const
  166. {
  167.   assert(!gmIsZero(c));
  168.   return gmVector4(v_[0] / c, v_[1] / c, v_[2] / c, v_[3] / c);
  169. }
  170.  
  171. inline gmVector4 operator *(double c, const gmVector4& v)
  172. {
  173.   return gmVector4(c * v[0], c * v[1], c * v[2], c * v[3]);
  174. }
  175.  
  176. inline bool gmVector4::operator ==(const gmVector4& v) const
  177. {
  178.   return (gmFuzEQ(v_[0], v[0]) && gmFuzEQ(v_[1], v[1]) &&
  179.           gmFuzEQ(v_[2], v[2]) && gmFuzEQ(v_[3], v[3]));
  180. }
  181.  
  182. inline bool gmVector4::operator !=(const gmVector4& v) const
  183. {
  184.   return (!(*this == v));
  185. }
  186.  
  187. // OPERATIONS
  188.  
  189. inline gmVector4& gmVector4::clamp(double lo, double hi)
  190. {
  191.   gmClamp(v_[0], lo, hi); gmClamp(v_[1], lo, hi);
  192.   gmClamp(v_[2], lo, hi); gmClamp(v_[3], lo, hi);
  193.   return *this;
  194. }
  195.  
  196. inline double gmVector4::length() const
  197. {
  198.   return sqrt(gmSqr(v_[0]) + gmSqr(v_[1]) + gmSqr(v_[2]) + gmSqr(v_[3]));
  199. }
  200.  
  201. inline double gmVector4::lengthSquared() const
  202. {
  203.   return gmSqr(v_[0]) + gmSqr(v_[1]) + gmSqr(v_[2]) + gmSqr(v_[3]);
  204. }
  205.  
  206. inline gmVector4& gmVector4::normalize()
  207. {
  208.   double len = length();
  209.   assert(!gmIsZero(len));
  210.   *this /= len;
  211.   return *this;
  212. }
  213.  
  214. inline void gmVector4::copyTo(float f[4]) const
  215. {
  216.   f[0] = v_[0]; f[1] = v_[1]; f[2] = v_[2]; f[3] = v_[3];
  217. }
  218.  
  219. inline void gmVector4::copyTo(double f[4]) const
  220. {
  221.   f[0] = v_[0]; f[1] = v_[1]; f[2] = v_[2]; f[3] = v_[3];
  222. }
  223.  
  224. inline double distance(const gmVector4& v1, const gmVector4& v2)
  225. {
  226.   return sqrt(gmSqr(v1[0] - v2[0]) + gmSqr(v1[1] - v2[1]) +
  227.               gmSqr(v1[2] - v2[2]) + gmSqr(v1[3] - v2[3]));
  228. }
  229.  
  230. inline double distanceSquared(const gmVector4& v1, const gmVector4& v2)
  231. {
  232.   return gmSqr(v1[0] - v2[0]) + gmSqr(v1[1] - v2[1]) +
  233.          gmSqr(v1[2] - v2[2]) + gmSqr(v1[3] - v2[3]);
  234. }
  235.  
  236. inline double dot(const gmVector4& v1, const gmVector4& v2)
  237. {
  238.   return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2] + v1[3] * v2[3];
  239. }
  240.  
  241. inline gmVector4 lerp(double f, const gmVector4& v1, const gmVector4& v2)
  242. {
  243.   return v1 + ((v2 - v1) * f);
  244. }
  245.  
  246. // OUTPUT
  247.  
  248. inline ostream & operator << ( ostream& os, const gmVector4& v)
  249. {
  250.   os << "< " << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " >";
  251.   return os;
  252. }
  253.  
  254. #endif 
  255.