home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_DEV08B.LHA / gerlib / libg++ / etc / PlotFile3D / Vec3D.h < prev   
Encoding:
C/C++ Source or Header  |  1993-12-12  |  5.1 KB  |  210 lines

  1. // Header file for Vec3D class -*- C++ -*-
  2. /* 
  3. Copyright (C) 1990 Free Software Foundation
  4.     written by J. Thomas Ngo, Harvard University
  5.  
  6. This file is part of the GNU C++ Library.
  7.  
  8. The GNU C++ Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY.  No author or distributor accepts
  10. responsibility to anyone for the consequences of using it or for
  11. whether it serves any particular purpose or works at all, unless he
  12. says so in writing.  Refer to the GNU General Public License for full
  13. details.
  14.  
  15. Everyone is granted permission to copy, modify and redistribute The
  16. GNU C++ Library, but only under the conditions described in the GNU
  17. General Public License.  A copy of this license is supposed to have
  18. been given to you along with The GNU C++ Library so you can know your
  19. rights and responsibilities.  It should be in a file named COPYING.
  20. Among other things, the copyright notice and this notice must be
  21. preserved on all copies.  
  22. */
  23.  
  24.  
  25. #ifndef _Vec3D_h
  26. #define _Vec3D_h 1
  27.  
  28. #include <math.h>
  29. #include <builtin.h>
  30.  
  31. //===========================================================================
  32. // This class is used by PlotFile3D
  33. //===========================================================================
  34.  
  35. class Vec3D 
  36. {
  37. protected:
  38.   double          n[3];
  39.  
  40. public:
  41.  
  42.   // Constructors and destructor
  43.  
  44.                   Vec3D(); // initialize to (0,0,0)
  45.                   Vec3D(const double x, const double y, const double z);
  46.                   Vec3D(const Vec3D& that);
  47.  
  48.                  ~Vec3D();
  49.  
  50.   // Assignment operators
  51.  
  52.   Vec3D&          operator  = ( const Vec3D& that );
  53.   Vec3D&          operator += ( const Vec3D& that );
  54.   Vec3D&          operator -= ( const Vec3D& that );
  55.   Vec3D&          operator *= ( const double that );
  56.   Vec3D&          operator /= ( const double that );
  57.  
  58.   // You can read, but not set coordinates individually:
  59.  
  60.   double          operator [] (const int i) const;   // Vector notation
  61.  
  62.   double          x() const;   // Point notation
  63.   double          y() const;
  64.   double          z() const;
  65.  
  66.   // Error handling
  67.   volatile void  error(const char* s) const;
  68. };
  69.  
  70. // associated operators defined inline below
  71.  
  72. double  mod(const Vec3D& x); // sqrt of sum of squares
  73.  
  74. Vec3D   operator - (const Vec3D& a);
  75. Vec3D   operator + (const Vec3D& a, const Vec3D& b);
  76. Vec3D   operator - (const Vec3D& a, const Vec3D& b);
  77. Vec3D   operator * (const Vec3D& a, const double sc); // scale
  78. double  operator * (const Vec3D& a, const Vec3D& b);  // dot
  79. Vec3D   operator ^ (const Vec3D& a, const Vec3D& b);  // cross prod
  80. int     operator == (const Vec3D& a, const Vec3D& b);
  81. int     operator != (const Vec3D& a, const Vec3D& b);
  82.  
  83.  
  84. // Error handling
  85.  
  86. inline volatile void Vec3D::error(const char* s) const 
  87. {
  88.   (*lib_error_handler)("Vec3D",s);
  89. }
  90.  
  91. inline Vec3D::Vec3D() 
  92.   n[0] = n[1] = n[2] = 0; 
  93. }
  94.  
  95. inline Vec3D::Vec3D(const double x, const double y, const double z)
  96.   n[0]=x; n[1]=y; n[2]=z; 
  97. }
  98.  
  99.  
  100. inline Vec3D::Vec3D(const Vec3D& that) 
  101.   n[0] = that.n[0]; n[1] = that.n[1]; n[2] = that.n[2]; 
  102. }
  103.  
  104. inline Vec3D::~Vec3D() {}
  105.  
  106. inline Vec3D& Vec3D::operator = (const Vec3D& that) 
  107.   n[0] = that.n[0]; n[1] = that.n[1]; n[2] = that.n[2]; 
  108.   return *this; 
  109. }
  110.  
  111. inline double Vec3D::operator [] (const int i) const 
  112. {
  113.   if( i<0 || i>2 ) error("index out of range");
  114.   return n[i]; 
  115. }
  116.  
  117. inline double Vec3D::x() const
  118. {
  119.   return n[0];
  120. }
  121.  
  122. inline double Vec3D::y() const
  123. {
  124.   return n[1];
  125. }
  126.  
  127. inline double Vec3D::z() const
  128. {
  129.   return n[2];
  130. }
  131.  
  132.  
  133. inline Vec3D operator - (const Vec3D& a)
  134.   return Vec3D( -a.x(), -a.y(), -a.z() ); 
  135. }
  136.  
  137. inline double mod(const Vec3D& p)
  138.   return sqrt(sqr(p.x()) + sqr(p.y()) + sqr(p.z()));
  139. }
  140.  
  141. inline Vec3D operator + (const Vec3D& a, const Vec3D& b)
  142.   return Vec3D( a.x()+b.x(), a.y()+b.y(), a.z()+b.z() ); 
  143. }
  144.  
  145. inline Vec3D operator - (const Vec3D& a, const Vec3D& b)
  146.   return Vec3D( a.x()-b.x(), a.y()-b.y(), a.z()-b.z() ); 
  147. }
  148.  
  149. inline Vec3D operator * (const Vec3D& a, const double sc) // scale
  150.   return Vec3D( a.x()*sc, a.y()*sc, a.z()*sc ); 
  151. }
  152.  
  153. inline double operator * (const Vec3D& a, const Vec3D& b) // dot
  154.   return a.x()*b.x() + a.y()*b.y() + a.z()*b.z(); 
  155. }
  156.  
  157. inline Vec3D operator ^ (const Vec3D& a, const Vec3D& b) // cross
  158.   return Vec3D(a.y()*b.z() - a.z()*b.y(),                // UK style
  159.                a.z()*b.x() - a.x()*b.z(),                // notation
  160.                a.x()*b.y() - a.y()*b.x()); 
  161. }
  162.  
  163. inline Vec3D& Vec3D::operator += ( const Vec3D& that ) 
  164.   n[0] += that.x(); n[1] += that.y(); n[2] += that.z(); 
  165.   return *this; 
  166. }
  167.  
  168. inline Vec3D& Vec3D::operator -= ( const Vec3D& that ) 
  169.   n[0] -= that.x(); n[1] -= that.y(); n[2] -= that.z(); 
  170.   return *this; 
  171. }
  172.  
  173. inline Vec3D& Vec3D::operator *= ( const double that ) 
  174.   n[0] *= that; n[1] *= that; n[2] *= that; 
  175.   return *this; 
  176. }
  177.  
  178. inline Vec3D& Vec3D::operator /= ( const double that ) 
  179.   n[0] /= that; n[1] /= that; n[2] /= that; 
  180.   return *this; 
  181. }
  182.  
  183. inline int operator == (const Vec3D& a, const Vec3D& b)
  184. {
  185.   return a.x() == b.x() && a.y() == b.y() && a.z() == b.z();
  186. }
  187.  
  188. inline int operator != (const Vec3D& a, const Vec3D& b)
  189. {
  190.   return !(a == b);
  191. }
  192.  
  193.  
  194. #endif                          // _Vec3D_h
  195.