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

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