vec3.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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
00040
00041
00042
00043 template<typename T>class vec3_t
00044 {
00045 public:
00046 T x,
00047 y,
00048 z;
00049
00050
00051 vec3_t () {}
00052
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
00059 void Set (T xc, T yc, T zc)
00060 { x=xc; y=yc; z=zc; }
00061
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
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
00087 T Length () const
00088 { return sqrt (x*x + y*y + z*z); }
00089
00090
00091 T SquaredLength () const
00092 { return (x*x + y*y + z*z); }
00093
00094 const vec3_t operator- () const
00095 { return vec3_t (-x, -y, -z); }
00096
00097 void Flip ()
00098 { x=-x; y=-y; z=-z; }
00099
00100 const vec3_t operator+ (const vec3_t &vec) const
00101 { return vec3_t (x+vec.x, y+vec.y, z+vec.z); }
00102
00103 vec3_t &operator+= (const vec3_t &vec)
00104 { x+=vec.x; y+=vec.y; z+=vec.z; return *this; }
00105
00106 const vec3_t operator- (const vec3_t &vec) const
00107 { return vec3_t (x-vec.x, y-vec.y, z-vec.z); }
00108
00109 vec3_t &operator-= (const vec3_t &vec)
00110 { x-=vec.x; y-=vec.y; z-=vec.z; return *this; }
00111
00112 const vec3_t operator* (T fact) const
00113 { return vec3_t (x*fact, y*fact, z*fact); }
00114
00115 const vec3_t operator/ (T fact) const
00116 { T xfact = T(1)/fact; return vec3_t (x*xfact, y*xfact, z*xfact); }
00117
00118 vec3_t &operator*= (T fact)
00119 { x*=fact; y*=fact; z*=fact; return *this; }
00120 };
00121
00122
00123
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
00128
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
00134
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
00143 typedef vec3_t<float64> vec3;
00144
00145 typedef vec3_t<float32> vec3f;
00146
00147
00148
00149 #endif