home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / graphtal / vector.c < prev    next >
C/C++ Source or Header  |  1992-10-23  |  4KB  |  206 lines

  1. /*
  2.  * Vector.C - methods for class Vector.
  3.  *
  4.  * Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  */
  17.  
  18. #include "Vector.h"
  19.  
  20. //___________________________________________________________ Vector
  21.  
  22. Vector::Vector()
  23. { v[0] = v[1] = v[2] = 0.0; }
  24.  
  25. Vector::Vector(const real x1, const real x2, const real x3) 
  26. { v[0] = x1; v[1] = x2; v[2] = x3; }
  27.  
  28. Vector::Vector(const Vector& vec)
  29. { v[0] = vec.v[0]; v[1] = vec.v[1]; v[2] = vec.v[2]; }
  30.  
  31. const Vector& Vector::operator=(const Vector& vec)
  32. {
  33.   v[0] = vec.v[0]; v[1] = vec.v[1]; v[2] = vec.v[2];
  34.   return *this;
  35. }
  36.  
  37. real& Vector::operator[](int i)
  38.   if (i<0 || i>2)
  39.     Error(ERR_PANIC, "Vector::operator[] index out of range");
  40.  
  41.   return v[i]; 
  42. }
  43.  
  44. real Vector::operator[](int i) const 
  45.   if (i<0 || i>2)
  46.     Error(ERR_PANIC, "Vector::operator[] index out of range");
  47.  
  48.   return v[i]; 
  49. }
  50.  
  51. Vector& Vector::operator+=(const Vector& vec)
  52. {
  53.   v[0] += vec[0];  v[1] += vec[1];  v[2] += vec[2];
  54.   return *this;
  55. }
  56.  
  57. Vector& Vector::operator-=(const Vector& vec)
  58. {
  59.   v[0] -= vec[0];  v[1] -= vec[1];  v[2] -= vec[2];
  60.   return *this;
  61. }
  62.  
  63. Vector& Vector::operator*=(const Vector& vec)
  64. {
  65.   static real tmp[3];
  66.  
  67.   tmp[0] = v[0]; tmp[1] = v[1]; tmp[2] = v[2]; 
  68.  
  69.   v[0] = tmp[1]*vec[2] - tmp[2]*vec[1];
  70.   v[1] = tmp[2]*vec[0] - tmp[0]*vec[2];
  71.   v[2] = tmp[0]*vec[1] - tmp[1]*vec[0];
  72.  
  73.   return *this;
  74. }
  75.  
  76. Vector& Vector::operator*=(const real x)
  77. {
  78.   v[0] *= x; v[1] *= x; v[2] *= x;
  79.   return *this;
  80. }
  81.  
  82. Vector& Vector::operator/=(const real x)
  83. {
  84.   if (equal(x, 0))
  85.     Error(ERR_PANIC, "Vector::operator/= division by zero");
  86.  
  87.   real d = 1/x;
  88.   v[0] *= d; v[1] *= d; v[2] *= d;
  89.   return *this;
  90. }
  91.  
  92. int Vector::operator==(const Vector& vec) const
  93. {
  94.   return (equal(v[0], vec[0]) && equal(v[1], vec[1]) && equal(v[2], vec[2]));
  95. }
  96.  
  97. int Vector::operator!=(const Vector& vec) const
  98. {
  99.   return !(*this == vec);
  100. }
  101.  
  102. real Vector::sqr() const
  103. {
  104.   return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
  105. }
  106.  
  107. real Vector::length() const
  108. {
  109.   return sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
  110. }
  111.  
  112. int Vector::zero() const
  113. {
  114.   return (equal(fabs(v[0]), 0) &&
  115.       equal(fabs(v[1]), 0) &&
  116.       equal(fabs(v[2]), 0));
  117. }
  118.  
  119. real Vector::distance(const Vector& vec) const
  120. {
  121.   return (*this - vec).length();
  122. }
  123.  
  124. real Vector::normalize()
  125. {
  126.   real len = length();
  127.   if (equal(len, 0))
  128.     return 0;
  129.   else {
  130.     real d = 1/len;
  131.     v[0] *= d; v[1] *= d; v[2] *= d;
  132.     return len;
  133.   }
  134. }
  135.  
  136. Vector Vector::normalized() const
  137. {
  138.   real len = length();
  139.   if (equal(len, 0)) {
  140.     Error(ERR_WARN, "Vector::normalized length is zero");
  141.     return Vector(0,0,0);
  142.   }
  143.   else
  144.     return Vector(*this / len);
  145. }
  146.  
  147. Vector Vector::operator-() const
  148. {
  149.   return Vector(-v[0], -v[1], -v[2]);
  150. }
  151.  
  152. Vector Vector::operator+(const Vector& vec) const
  153. {
  154.   return Vector(v[0] + vec[0], v[1] + vec[1], v[2] + vec[2]);
  155. }
  156.  
  157. Vector Vector::operator-(const Vector& vec) const
  158. {
  159.   return Vector(v[0] - vec[0], v[1] - vec[1], v[2] - vec[2]);
  160. }
  161.  
  162. Vector Vector::operator*(const Vector& vec) const
  163. {
  164.   return Vector(v[1]*vec[2] - v[2]*vec[1],
  165.         v[2]*vec[0] - v[0]*vec[2],
  166.         v[0]*vec[1] - v[1]*vec[0]);
  167. }
  168.  
  169. real Vector::operator^(const Vector& vec) const
  170. {
  171.   return (v[0]*vec[0] + v[1]*vec[1] + v[2]*vec[2]);
  172. }
  173.  
  174. Vector Vector::operator/(const real x) const
  175. {
  176.   if (equal(x, 0))
  177.     Error(ERR_PANIC, "Vector::operator/ division by zero");
  178.  
  179.   real d = 1/x;
  180.   return Vector(v[0]*d, v[1]*d, v[2]*d);
  181. }
  182.  
  183. Vector operator*(real x, const Vector& vec)
  184. {
  185.   return Vector(x*vec[0], x*vec[1], x*vec[2]);
  186. }
  187.  
  188. Vector operator*(const Vector& vec, real x) 
  189. {
  190.   return Vector(vec[0]*x, vec[1]*x, vec[2]*x);
  191. }
  192.  
  193. ostream& operator<<(ostream& os, const Vector& vec)
  194. {
  195.   return os << "(" 
  196.             << vec[0] << ", " << vec[1] << ", " << vec[2]
  197.         << ")";
  198. }
  199.  
  200. void Vector::swap(Vector& v1, Vector& v2)
  201. {
  202.   Vector tmp = v1; v1 = v2; v2 = tmp;
  203. }
  204.