include/vamos/geometry/Three_Vector.h

Go to the documentation of this file.
00001 //  Vamos Automotive Simulator
00002 //  Copyright (C) 2001--2002 Sam Varner
00003 //
00004 //  This program is free software; you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation; either version 2 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program; if not, write to the Free Software
00016 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 #ifndef _THREE_VECTOR_H_
00019 #define _THREE_VECTOR_H_
00020 
00021 #include <math.h>
00022 #include <iostream>
00023 
00024 // A three-element vector.  Useful for representing physical quantites
00025 // like position, veocity or force.
00026 
00027 namespace Vamos_Geometry
00028 {
00029   class Three_Vector
00030   {
00031           
00032           public:
00033                   
00034     // The components of the vector.
00035     double m_vec [3];
00036 
00037     Three_Vector ();
00038     Three_Vector (double x, double y, double z);
00039     // Initialize with a C-style vector.
00040     Three_Vector (double [3]);
00041     // Copy constructor.
00042     inline Three_Vector (const Three_Vector& vec);
00043     // Copy assignment.
00044     Three_Vector& operator = (const Three_Vector& vec);
00045 
00046     // Subscript operators.
00047     double& operator [] (int index) { return m_vec [index]; }
00048     const double& operator [] (int index) const { return m_vec [index]; }
00049 
00050     // Set each element to zero.
00051     void zero ();
00052     // Return the magnitude (absolute value).
00053     inline double abs () const;
00054         inline double magnitude() const {return abs();}
00055     // Return the dot product with VEC.
00056     double dot (const Three_Vector& vec) const;
00057     // Return the cross product with VEC.
00058     Three_Vector cross (const Three_Vector& vec) const;
00059     // Return the projection onto VEC.
00060     Three_Vector project (const Three_Vector& vec) const;
00061     Three_Vector back_project (const Three_Vector& vec) const;
00062 
00063     // Return the pependicular distance between vectors parallel to
00064     // this one passing through the given points.
00065     double perp_distance (const Three_Vector& point1,
00066                           const Three_Vector& point2) const;
00067 
00068     // The unit vector that points along this vector.
00069     Three_Vector unit () const;
00070     // The component of this vector projected onto another vector.
00071     double component (const Three_Vector& vec) const;
00072     // The angle between this vector and another.
00073     double angle (const Three_Vector& vec) const;
00074         
00075         Three_Vector rotate (double x, double y, double z) const;
00076 
00077     // These operators must be members of the class because they modify
00078     // class members.
00079     Three_Vector& operator += (const Three_Vector& vec);
00080     Three_Vector& operator -= (const Three_Vector& vec);
00081     Three_Vector& operator *= (double factor);
00082     Three_Vector& operator /= (double factor);
00083     
00084     static const Three_Vector X;
00085     static const Three_Vector Y;
00086     static const Three_Vector Z;
00087   };
00088 
00089   // Copy constructor, needed so that elements of the vector are copied rather
00090   // than the pointer to the element array.
00091   Three_Vector::Three_Vector (const Three_Vector& vec)
00092   {
00093     m_vec [0] = vec [0];
00094     m_vec [1] = vec [1];
00095     m_vec [2] = vec [2];
00096   }
00097 
00098   // Return the magnitude.
00099   double
00100   Three_Vector::abs () const
00101   {
00102     return sqrt (m_vec [0] * m_vec [0] 
00103                  + m_vec [1] * m_vec [1] 
00104                  + m_vec [2] * m_vec [2]);
00105   }
00106 
00107   // Addition operator.
00108   const Three_Vector operator + (const Three_Vector& vec1, 
00109                                  const Three_Vector& vec2);
00110   // Subtraction operator.
00111   const Three_Vector operator - (const Three_Vector& vec1, 
00112                                  const Three_Vector& vec2);
00113   // Negation operator.
00114   const Three_Vector operator - (const Three_Vector& vec);
00115   // Scalar multiplication operators.
00116   const Three_Vector operator * (const Three_Vector& vec1, double factor);
00117   const Three_Vector operator * (double factor, const Three_Vector& vec1);
00118   // Scalar division operators.
00119   const Three_Vector operator / (const Three_Vector& vec1, double factor);
00120   const Three_Vector operator / (double factor, const Three_Vector& vec1);
00121 
00122   bool operator == (const Three_Vector& vec1, const Three_Vector& vec2);
00123 
00124   // Stream operators.
00125   std::istream& operator >> (std::istream& is, Three_Vector& vec);
00126   std::ostream& operator << (std::ostream& os, const Three_Vector& vec);
00127 }
00128 
00129 #endif

Generated on Thu Oct 19 04:05:48 2006 by  doxygen 1.4.6