vec3.h

Go to the documentation of this file.
00001 /*
00002  *  This file is part of libcxxsupport.
00003  *
00004  *  libcxxsupport 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  *  libcxxsupport 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 libcxxsupport; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  */
00018 
00019 /*
00020  *  libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
00021  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00022  *  (DLR).
00023  */
00024 
00025 /*! \file vec3.h
00026  *  Class representing 3D cartesian vectors
00027  *
00028  *  Copyright (C) 2003, 2006 Max-Planck-Society
00029  *  \author Martin Reinecke
00030  */
00031 
00032 #ifndef PLANCK_VEC3_H
00033 #define PLANCK_VEC3_H
00034 
00035 #include <cmath>
00036 #include <iostream>
00037 #include "datatypes.h"
00038 
00039 /*! \defgroup vec3group 3D vectors */
00040 /*! \{ */
00041 
00042 /*! Class representing a 3D cartesian vector. */
00043 template<typename T>class vec3_t
00044   {
00045   public:
00046     T x, /*!< x-coordinate */
00047       y, /*!< y-coordinate */
00048       z; /*!< z-coordinate */
00049 
00050     /*! Default constructor. Does not initialize \a x, \a y, and \a z. */
00051     vec3_t () {}
00052     /*! Creates a vector with the coordinates \a xc, \a yc, and \a zc. */
00053     vec3_t (T xc, T yc, T zc)
00054       : x(xc), y(yc), z(zc) {}
00055     template<typename T2> explicit vec3_t (const vec3_t<T2> &orig)
00056       : x(orig.x), y(orig.y), z(orig.z) {}
00057 
00058     /*! Sets the vector components to \a xc, \a yc, and \a zc. */
00059     void Set (T xc, T yc, T zc)
00060       { x=xc; y=yc; z=zc; }
00061     /*! Creates a unit vector from a z coordinate and an azimuthal angle. */
00062     void set_z_phi (T z_, T phi)
00063       {
00064       using namespace std;
00065       T sintheta = sqrt((T(1)-z_)*(T(1)+z_));
00066       x = sintheta*cos(phi);
00067       y = sintheta*sin(phi);
00068       z = z_;
00069       }
00070 
00071     /*! Normalizes the vector to length 1. */
00072     void Normalize ()
00073       {
00074       using namespace std;
00075       T l = T(1)/sqrt (x*x + y*y + z*z);
00076       x*=l; y*=l; z*=l;
00077       }
00078 
00079     vec3_t Norm() const
00080       {
00081       vec3_t res(*this);
00082       res.Normalize();
00083       return res;
00084       }
00085 
00086     /*! Returns the length of the vector. */
00087     T Length () const
00088       { return sqrt (x*x + y*y + z*z); }
00089 
00090     /*! Returns the squared length of the vector. */
00091     T SquaredLength () const
00092       { return (x*x + y*y + z*z); }
00093     /*! Returns the vector with the signs of all coordinates flipped. */
00094     const vec3_t operator- () const
00095       { return vec3_t (-x, -y, -z); }
00096     /*! Flips the signs of all coordinates. */
00097     void Flip ()
00098       { x=-x; y=-y; z=-z; }
00099     /*! Returns (\a *this + \a vec). */
00100     const vec3_t operator+ (const vec3_t &vec) const
00101       { return vec3_t (x+vec.x, y+vec.y, z+vec.z); }
00102     /*! Adds \a vec to \a *this. */
00103     vec3_t &operator+= (const vec3_t &vec)
00104       { x+=vec.x; y+=vec.y; z+=vec.z; return *this; }
00105     /*! Returns (\a *this - \a vec). */
00106     const vec3_t operator- (const vec3_t &vec) const
00107       { return vec3_t (x-vec.x, y-vec.y, z-vec.z); }
00108     /*! Subtracts \a vec from \a *this. */
00109     vec3_t &operator-= (const vec3_t &vec)
00110       { x-=vec.x; y-=vec.y; z-=vec.z; return *this; }
00111     /*! Returns the vector scaled by \a fact. */
00112     const vec3_t operator* (T fact) const
00113       { return vec3_t (x*fact, y*fact, z*fact); }
00114     /*! Returns the vector scaled by \a 1/fact. */
00115     const vec3_t operator/ (T fact) const
00116       { T xfact = T(1)/fact; return vec3_t (x*xfact, y*xfact, z*xfact); }
00117     /*! Scales the vector by \a fact. */
00118     vec3_t &operator*= (T fact)
00119       { x*=fact; y*=fact; z*=fact; return *this; }
00120   };
00121 
00122 /*! Returns the dot product of \a v1 and \a v2.
00123     \relates vec3_t */
00124 template<typename T> inline T dotprod(const vec3_t<T> &v1, const vec3_t<T> &v2)
00125   { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z; }
00126 
00127 /*! Returns the cross product of \a a and \a b.
00128     \relates vec3_t */
00129 template<typename T> inline vec3_t<T> crossprod
00130   (const vec3_t<T> &a, const vec3_t<T> &b)
00131   { return vec3_t<T>(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x); }
00132 
00133 /*! Writes \a v to \a os.
00134     \relates vec3_t */
00135 template<typename T> inline std::ostream &operator<<
00136   (std::ostream &os, const vec3_t<T> &v)
00137   {
00138   os << v.x << ", " << v.y << ", " << v.z << std::endl;
00139   return os;
00140   }
00141 
00142 /*! Specialisation of vec3_t for 64-bit floating point components */
00143 typedef vec3_t<float64> vec3;
00144 /*! Specialisation of vec3_t for 32-bit floating point components */
00145 typedef vec3_t<float32> vec3f;
00146 
00147 /*! \} */
00148 
00149 #endif

Generated on Thu Oct 8 14:48:51 2015 for LevelS C++ support library