NASA - Jet Propulsion Laboratory
    + View the NASA Portal
Search JPL
Jet Propulsion Laboratory Home Earth Solar System Stars & Galaxies Technology
Introduction Background Software Links

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, 2006 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     /*! Creates a unit vector from a z coordinate and an azimuthal angle. */
00058     void set_z_phi (double z_, double phi)
00059       {
00060       using namespace std;
00061       double sintheta = sqrt((1.-z_)*(1.+z_));
00062       x = sintheta*cos(phi);
00063       y = sintheta*sin(phi);
00064       z = z_;
00065       }
00066 
00067     /*! Normalizes the vector to length 1. */
00068     void Normalize ()
00069       {
00070       using namespace std;
00071       double l = 1.0/sqrt (x*x + y*y + z*z);
00072       x*=l; y*=l; z*=l;
00073       }
00074 
00075     /*! Returns the length of the vector. */
00076     double Length () const
00077       { return sqrt (x*x + y*y + z*z); }
00078 
00079     /*! Returns the squared length of the vector. */
00080     double SquaredLength () const
00081       { return (x*x + y*y + z*z); }
00082     /*! Returns the vector with the signs of all coordinates flipped. */
00083     const vec3 operator- () const
00084       { return vec3 (-x, -y, -z); }
00085     /*! Flips the signs of all coordinates. */
00086     void Flip ()
00087       { x=-x; y=-y; z=-z; }
00088     /*! Subtracts \a vec from the vector. */
00089     const vec3 operator- (const vec3 &vec) const
00090       { return vec3 (x-vec.x, y-vec.y, z-vec.z); }
00091     /*! Adds \a vec to the vector. */
00092     const vec3 operator+ (const vec3 &vec) const
00093       { return vec3 (x+vec.x, y+vec.y, z+vec.z); }
00094     /*! Returns the vector scaled by \a fact. */
00095     const vec3 operator* (double fact) const
00096       { return vec3 (x*fact, y*fact, z*fact); }
00097     /*! Returns the vector scaled by \a 1/fact. */
00098     const vec3 operator/ (double fact) const
00099       { double xfact = 1./fact; return vec3 (x*xfact, y*xfact, z*xfact); }
00100     /*! Scales the vector by \a fact. */
00101     vec3 &operator*= (double fact)
00102       { x*=fact; y*=fact; z*=fact; return *this; }
00103   };
00104 
00105 /*! Returns the dot product of \a v1 and \a v2.
00106     \relates vec3 */
00107 inline double dotprod(const vec3 &v1, const vec3 &v2)
00108   { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z; }
00109 
00110 /*! Returns the cross product of \a a and \a b.
00111     \relates vec3 */
00112 inline vec3 crossprod(const vec3 &a, const vec3 &b)
00113   { 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); }
00114 
00115 /*! Writes \a v to \a os.
00116     \relates vec3 */
00117 inline std::ostream &operator<< (std::ostream &os, const vec3 &v)
00118   {
00119   os << v.x << ", " << v.y << ", " << v.z << std::endl;
00120   return os;
00121   }
00122 
00123 /*! \} */
00124 
00125 #endif

Generated on Fri Jun 18 16:12:29 2010 for LevelS C++ support library
Privacy / Copyright
FIRST GOV Contact: NASA Home Page Site Manager:
Webmaster:

CL 03-2650