Debian Codegraph @

cextern/healpix/src/cxx/cxxsupport/vec3.h

raw
1 /*
2 * This file is part of libcxxsupport.
3 *
4 * libcxxsupport is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * libcxxsupport is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with libcxxsupport; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 /*
20 * libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
21 * and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
22 * (DLR).
23 */
24
25 /*! \file vec3.h
26 * Class representing 3D cartesian vectors
27 *
28 * Copyright (C) 2003, 2006 Max-Planck-Society
29 * \author Martin Reinecke
30 */
31
32 #ifndef PLANCK_VEC3_H
33 #define PLANCK_VEC3_H
34
35 #include <cmath>
36 #include <iostream>
37 #include "datatypes.h"
38
39 /*! \defgroup vec3group 3D vectors */
40 /*! \{ */
41
42 /*! Class representing a 3D cartesian vector. */
43 template<typename T>class vec3_t
44 {
45 public:
46 T x, /*!< x-coordinate */
47 y, /*!< y-coordinate */
48 z; /*!< z-coordinate */
49
50 /*! Default constructor. Does not initialize \a x, \a y, and \a z. */
51 vec3_t () {}
52 /*! Creates a vector with the coordinates \a xc, \a yc, and \a zc. */
53 vec3_t (T xc, T yc, T zc)
54 : x(xc), y(yc), z(zc) {}
55 template<typename T2> explicit vec3_t (const vec3_t<T2> &orig)
56 : x(orig.x), y(orig.y), z(orig.z) {}
57
58 /*! Sets the vector components to \a xc, \a yc, and \a zc. */
59 void Set (T xc, T yc, T zc)
60 { x=xc; y=yc; z=zc; }
61 /*! Creates a unit vector from a z coordinate and an azimuthal angle. */
62 void set_z_phi (T z_, T phi)
63 {
64 using namespace std;
65 T sintheta = sqrt((T(1)-z_)*(T(1)+z_));
66 x = sintheta*cos(phi);
67 y = sintheta*sin(phi);
68 z = z_;
69 }
70
71 /*! Normalizes the vector to length 1. */
72 void Normalize ()
73 {
74 using namespace std;
75 T l = T(1)/sqrt (x*x + y*y + z*z);
76 x*=l; y*=l; z*=l;
77 }
78
79 vec3_t Norm() const
80 {
81 vec3_t res(*this);
82 res.Normalize();
83 return res;
84 }
85
86 /*! Returns the length of the vector. */
87 T Length () const
88 { return sqrt (x*x + y*y + z*z); }
89
90 /*! Returns the squared length of the vector. */
91 T SquaredLength () const
92 { return (x*x + y*y + z*z); }
93 /*! Returns the vector with the signs of all coordinates flipped. */
94 const vec3_t operator- () const
95 { return vec3_t (-x, -y, -z); }
96 /*! Flips the signs of all coordinates. */
97 void Flip ()
98 { x=-x; y=-y; z=-z; }
99 /*! Returns (\a *this + \a vec). */
100 const vec3_t operator+ (const vec3_t &vec) const
101 { return vec3_t (x+vec.x, y+vec.y, z+vec.z); }
102 /*! Adds \a vec to \a *this. */
103 vec3_t &operator+= (const vec3_t &vec)
104 { x+=vec.x; y+=vec.y; z+=vec.z; return *this; }
105 /*! Returns (\a *this - \a vec). */
106 const vec3_t operator- (const vec3_t &vec) const
107 { return vec3_t (x-vec.x, y-vec.y, z-vec.z); }
108 /*! Subtracts \a vec from \a *this. */
109 vec3_t &operator-= (const vec3_t &vec)
110 { x-=vec.x; y-=vec.y; z-=vec.z; return *this; }
111 /*! Returns the vector scaled by \a fact. */
112 const vec3_t operator* (T fact) const
113 { return vec3_t (x*fact, y*fact, z*fact); }
114 /*! Returns the vector scaled by \a 1/fact. */
115 const vec3_t operator/ (T fact) const
116 { T xfact = T(1)/fact; return vec3_t (x*xfact, y*xfact, z*xfact); }
117 /*! Scales the vector by \a fact. */
118 vec3_t &operator*= (T fact)
119 { x*=fact; y*=fact; z*=fact; return *this; }
120 };
121
122 /*! Returns the dot product of \a v1 and \a v2.
123 \relates vec3_t */
124 template<typename T> inline T dotprod(const vec3_t<T> &v1, const vec3_t<T> &v2)
125 { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z; }
126
127 /*! Returns the cross product of \a a and \a b.
128 \relates vec3_t */
129 template<typename T> inline vec3_t<T> crossprod
130 (const vec3_t<T> &a, const vec3_t<T> &b)
131 { 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); }
132
133 /*! Writes \a v to \a os.
134 \relates vec3_t */
135 template<typename T> inline std::ostream &operator<<
136 (std::ostream &os, const vec3_t<T> &v)
137 {
138 os << v.x << ", " << v.y << ", " << v.z << std::endl;
139 return os;
140 }
141
142 /*! Specialisation of vec3_t for 64-bit floating point components */
143 typedef vec3_t<float64> vec3;
144 /*! Specialisation of vec3_t for 32-bit floating point components */
145 typedef vec3_t<float32> vec3f;
146
147 /*! \} */
148
149 #endif