Main Page | Modules | Alphabetical List | Class List | File List | Class Members | File Members

vec3.h

Go to the documentation of this file.
00001 /*
00002  *  This file is part of Healpix_cxx.
00003  *
00004  *  Healpix_cxx 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  *  Healpix_cxx 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 Healpix_cxx; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  *
00018  *  For more information about HEALPix, see http://healpix.jpl.nasa.gov
00019  */
00020 
00021 /*
00022  *  Healpix_cxx is being developed at the Max-Planck-Institut fuer Astrophysik
00023  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00024  *  (DLR).
00025  */
00026 
00027 /*! \file vec3.h
00028  *  Class representing 3D cartesian vectors
00029  *
00030  *  Copyright (C) 2003 Max-Planck-Society
00031  *  \author Martin Reinecke
00032  */
00033 
00034 #ifndef PLANCK_VEC3_H
00035 #define PLANCK_VEC3_H
00036 
00037 #include <cmath>
00038 #include <iostream>
00039 
00040 /*! \defgroup vec3group 3D vectors */
00041 /*! \{ */
00042 
00043 /*! Class representing a 3D cartesian vector. */
00044 class vec3
00045   {
00046   public:
00047     double x, /*!< x-coordinate */
00048            y, /*!< y-coordinate */
00049            z; /*!< z-coordinate */
00050 
00051     /*! Default constructor. Does not initialize \a x, \a y, and \a z. */
00052     vec3 () {}
00053     /*! Creates a vector with the coordinates \a xc, \a yc, and \a zc. */
00054     vec3 (double xc, double yc, double zc)
00055       : x(xc), y(yc), z(zc) {}
00056 
00057     /*! Normalizes the vector to length 1. */
00058     void Normalize ()
00059       {
00060       using namespace std;
00061       double l = 1.0/sqrt (x*x + y*y + z*z);
00062       x*=l; y*=l; z*=l;
00063       }
00064 
00065     /*! Returns the length of the vector. */
00066     double Length () const
00067       { return sqrt (x*x + y*y + z*z); }
00068 
00069     /*! Returns the squared length of the vector. */
00070     double SquaredLength () const
00071       { return (x*x + y*y + z*z); }
00072     /*! Returns the vector with the signs of all coordinates flipped. */
00073     const vec3 operator- () const
00074       { return vec3 (-x, -y, -z); }
00075     /*! Flips the signs of all coordinates. */
00076     void Flip ()
00077       { x=-x; y=-y; z=-z; }
00078     /*! Subtracts \a vec from the vector. */
00079     const vec3 operator- (const vec3 &vec) const
00080       { return vec3 (x-vec.x, y-vec.y, z-vec.z); }
00081     /*! Adds \a vec to the vector. */
00082     const vec3 operator+ (const vec3 &vec) const
00083       { return vec3 (x+vec.x, y+vec.y, z+vec.z); }
00084     /*! Returns the vector scaled by \a fact. */
00085     const vec3 operator* (double fact) const
00086       { return vec3 (x*fact, y*fact, z*fact); }
00087     /*! Returns the vector scaled by \a 1/fact. */
00088     const vec3 operator/ (double fact) const
00089       { double xfact = 1./fact; return vec3 (x*xfact, y*xfact, z*xfact); }
00090     /*! Scales the vector by \a fact. */
00091     vec3 &operator*= (double fact)
00092       { x*=fact; y*=fact; z*=fact; return *this; }
00093   };
00094 
00095 /*! Returns the dot product of \a v1 and \a v2.
00096     \relates vec3 */
00097 inline double dotprod(const vec3 &v1, const vec3 &v2)
00098   { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z; }
00099 
00100 /*! Returns the cross product of \a a and \a b.
00101     \relates vec3 */
00102 inline vec3 crossprod(const vec3 &a, const vec3 &b)
00103   { return vec3 (a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x); }
00104 
00105 /*! Writes \a v to \a os.
00106     \relates vec3 */
00107 inline std::ostream &operator<< (std::ostream &os, const vec3 &v)
00108   {
00109   os << v.x << ", " << v.y << ", " << v.z << std::endl;
00110   return os;
00111   }
00112 
00113 /*! \} */
00114 
00115 #endif

Generated on Fri Jul 8 09:37:14 2005 for LevelS C++ support library