Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
Vec.hh
Go to the documentation of this file.
1#ifndef VEC_HH_INCLUDED
2#define VEC_HH_INCLUDED
3
4#include <cmath>
5#include <iostream>
6#include <sstream>
7#include "Rtypes.h"
8
9/**
10 * The Vec class is a straightforward 3-d vector, which also works in pyroot.
11 */
12struct Vec
13{
14 double x,y,z;
15
16 /**
17 * Constructor.
18 *
19 * \param x_ x position
20 * \param y_ y position
21 * \param z_ z position
22 */
23 Vec(double x_, double y_, double z_) : x(x_), y(y_), z(z_) {}
24
25 /**
26 * Default constructor.
27 */
28 Vec():x(0),y(0),z(0) {}
29
30 /**
31 * Get dot product.
32 *
33 * \param v vector
34 * \return dot product
35 */
36 double dot(const Vec& v) const { return v.x*x + v.y*y+ v.z*z;}
37
38 /**
39 * Get cross product.
40 *
41 * \param r vector
42 * \return cross product
43 */
44 Vec cross(const Vec r) const { return Vec ( y*r.z-z*r.y, z*r.x-x*r.z, x*r.y-y*r.x);}
45
46 /**
47 * Add vector.
48 *
49 * \param v vector
50 * \return this vector
51 */
52 Vec& operator+=(const Vec& v) { x+=v.x; y+=v.y; z+=v.z; return *this;}
53
54 /**
55 * Subtract vector.
56 *
57 * \param v vector
58 * \return this vector
59 */
60 Vec& operator-=(const Vec& v) { x-=v.x; y-=v.y; z-=v.z; return *this;}
61
62 /**
63 * Multiply vector.
64 *
65 * \param d factor
66 * \return this vector
67 */
68 Vec& operator*=(double d) { x*=d; y*=d; z*=d; return *this;}
69
70 /**
71 * Divide vector.
72 *
73 * \param d factor
74 * \return this vector
75 */
76 Vec& operator/=(double d) { return operator*=( 1.0 / d ); }
77
78 /**
79 * Negate vector.
80 *
81 * \return vector
82 */
83 Vec operator-() const { return Vec(-x,-y,-z); }
84
85 /**
86 * Check equality with given vector.
87 *
88 * \param v vector
89 * \return true if (x,y,z) positions of two vectors are equal; else false
90 */
91 bool operator==( const Vec& v ) const { return x==v.x && y==v.y && z==v.z ; }
92
93 /**
94 * Check in-equality with given vector.
95 *
96 * \param v vector
97 * \return true if one of (x,y,z) positions of two vectors are not equal; else false
98 */
99 bool operator!=( const Vec& v ) const { return x!=v.x || y!=v.y || z!=v.z ; }
100
101 /**
102 * Set vector.
103 *
104 * \param xx x position
105 * \param yy y position
106 * \param zz z position
107 * \return this vector
108 */
109 Vec& set(double xx, double yy, double zz) { x=xx; y=yy; z=zz; return *this;}
110
111 /**
112 * Set vector according given zenith and azimuth angles.
113 *
114 * \param theta zenith angle [rad]
115 * \param phi azimuth angle [rad]
116 * \return this vector
117 */
118 Vec& set_angles(double theta, double phi)
119 {
120 x = sin ( theta ) * cos( phi );
121 y = sin ( theta ) * sin( phi );
122 z = cos ( theta );
123 return *this;
124 }
125
126 /**
127 * Get azimuth angle.
128 *
129 * \return angle [rad]
130 */
131 double phi() const { return atan2( y,x ); }
132
133 /**
134 * Get zenith angle.
135 *
136 * \return angle [rad]
137 */
138 double theta() const { return acos(z); }
139
140 /**
141 * Get length.
142 *
143 * \return length
144 */
145 double len() const { double l = dot(*this); return (l > 0)? sqrt(l) : 0; }
146
147 /**
148 * Get length of (x,y) component.
149 *
150 * \return length
151 */
152 double lenxy() const { const double r2 = x*x + y*y; return (r2>0) ? sqrt(r2) :0; }
153
154 /**
155 * Normalise this vector.
156 *
157 * \return this vector
158 */
159 Vec& normalize() { return operator/=( len() ); }
160
161 /**
162 * Print vector.
163 *
164 * \param out output stream
165 */
166 void print( std::ostream& out = std::cout ) const
167 {
168 out << "Vec:" << x << " " << y << " " << z;
169 }
170
171 /**
172 * Get string representation of this vector
173 *
174 * \return string
175 */
176 const char* __repr__() const
177 {
178 static std::string buffer;
179
180 std::ostringstream s;
181
182 print(s);
183
184 buffer = s.str();
185
186 return buffer.c_str();
187 }
188
189 /**
190 * Add vector.
191 *
192 * \param v vector
193 * \return vector
194 */
195 Vec __add__(const Vec& v) const { Vec r=*this; return r+=v; }
196
197 /**
198 * Subtract vector.
199 *
200 * \param v vector
201 * \return vector
202 */
203 Vec __sub__(const Vec& v) const { Vec r=*this; return r-=v; }
204
205 /**
206 * Multiply vector.
207 *
208 * \param d factor
209 * \return vector
210 */
211 Vec __mul__(double d ) const { Vec r=*this; return r*=d; }
212
213 /**
214 * Multiply vector.
215 *
216 * \param d factor
217 * \return vector
218 */
219 Vec __rmul__(double d ) const { return __mul__(d); }
220
221 /**
222 * Divide vector.
223 *
224 * \param d factor
225 * \return vector
226 */
227 Vec __div__(double d ) const { Vec r=*this; return r/=d; }
228
229 /**
230 * Rotate around z-axis with given angle.
231 *
232 * \param ang angle [rad]
233 * \return this vector
234 */
235 Vec& rotate_z(double ang)
236 {
237 const Vec o = *this;
238 x = o.x *cos(ang) - o.y * sin(ang);
239 y = o.x *sin(ang) + o.y * cos(ang);
240 z = o.z;
241 return *this;
242 }
243
244 /**
245 * Rotate around x-axis with given angle.
246 *
247 * \param ang angle [rad]
248 * \return this vector
249 */
250 Vec& rotate_x(double ang)
251 {
252 const Vec o = *this;
253 x = o.x;
254 y = o.y *cos(ang) + o.z * -sin(ang);
255 z = o.y *sin(ang) + o.z * cos(ang);
256 return *this;
257 }
258
259 /**
260 * Rotate around y-axis with given angle.
261 *
262 * \param ang angle [rad]
263 * \return this vector
264 */
265 Vec& rotate_y(double ang)
266 {
267 const Vec o = *this;
268 x = o.x *cos(ang) + o.z * sin(ang);
269 y = o.y;
270 z = -o.x *sin(ang) + o.z * cos(ang);
271 return *this;
272 }
273
274 ClassDefNV(Vec,3)
275};
276
277/**
278 * Write vector to output stream.
279 *
280 * \param out output stream
281 * \param v vector
282 * \return output stream
283 */
284inline std::ostream& operator<<( std::ostream& out , const Vec& v )
285{
286 out << v.x << " " << v.y << " " << v.z << " ";
287 return out;
288}
289
290/**
291 * Read vector from input stream.
292 *
293 * \param in input stream
294 * \param v vector
295 * \return input stream
296 */
297inline std::istream& operator>>(std::istream& in, Vec& v)
298{
299 in >> v.x >> v.y >> v.z ; return in;
300}
301
302/**
303 * Get cosine of space angle between two vectors.
304 *
305 * \param a first vector
306 * \param b second vector
307 * \return cosine
308 */
309inline double cos_angle_between( const Vec& a, const Vec& b)
310{
311 const double n = a.len() * b.len();
312 return a.dot(b) / n;
313}
314
315/**
316 * Get space angle between two vectors.
317 *
318 * \param a first vector
319 * \param b second vector
320 * \return angle [rad]
321 */
322inline double angle_between( const Vec& a, const Vec& b )
323{
324 double c = cos_angle_between( a, b );
325 if ( c < -1 ) return M_PI;
326 if ( c > 1 ) return 0;
327 return acos( c );
328}
329
330/**
331 * Add two vectors.
332 *
333 * \param a first vector
334 * \param b second vector
335 * \return vector
336 */
337inline Vec operator+(const Vec& a, const Vec& b) { Vec r(a); return r+=b;}
338
339/**
340 * Subtract two vectors.
341 *
342 * \param a first vector
343 * \param b second vector
344 * \return vector
345 */
346inline Vec operator-(const Vec& a, const Vec& b) { Vec r(a); return r-=b;}
347
348/**
349 * Multiply vector.
350 *
351 * \param a factor
352 * \param v vector
353 * \return vector
354 */
355inline Vec operator*(double a, const Vec& v) { return Vec(a*v.x,a*v.y,a*v.z);}
356
357/**
358 * Multiply vector.
359 *
360 * \param v vector
361 * \param a factor
362 * \return vector
363 */
364inline Vec operator*(const Vec& v, double a) { return Vec(a*v.x,a*v.y,a*v.z);}
365
366/**
367 * Divide vector.
368 *
369 * \param v vector
370 * \param a factor
371 * \return vector
372 */
373inline Vec operator/(const Vec& v, double a) { return Vec(v.x/a,v.y/a,v.z/a);}
374
375#endif
#define ClassDefNV(name, version)
Definition JROOT_t.hh:33
Vec operator-(const Vec &a, const Vec &b)
Subtract two vectors.
Definition Vec.hh:346
std::istream & operator>>(std::istream &in, Vec &v)
Read vector from input stream.
Definition Vec.hh:297
Vec operator*(double a, const Vec &v)
Multiply vector.
Definition Vec.hh:355
Vec operator+(const Vec &a, const Vec &b)
Add two vectors.
Definition Vec.hh:337
double cos_angle_between(const Vec &a, const Vec &b)
Get cosine of space angle between two vectors.
Definition Vec.hh:309
Vec operator/(const Vec &v, double a)
Divide vector.
Definition Vec.hh:373
std::ostream & operator<<(std::ostream &out, const Vec &v)
Write vector to output stream.
Definition Vec.hh:284
double angle_between(const Vec &a, const Vec &b)
Get space angle between two vectors.
Definition Vec.hh:322
The Vec class is a straightforward 3-d vector, which also works in pyroot.
Definition Vec.hh:13
Vec & normalize()
Normalise this vector.
Definition Vec.hh:159
double len() const
Get length.
Definition Vec.hh:145
Vec cross(const Vec r) const
Get cross product.
Definition Vec.hh:44
double dot(const Vec &v) const
Get dot product.
Definition Vec.hh:36
double z
Definition Vec.hh:14
Vec & set_angles(double theta, double phi)
Set vector according given zenith and azimuth angles.
Definition Vec.hh:118
Vec & operator-=(const Vec &v)
Subtract vector.
Definition Vec.hh:60
bool operator!=(const Vec &v) const
Check in-equality with given vector.
Definition Vec.hh:99
Vec & set(double xx, double yy, double zz)
Set vector.
Definition Vec.hh:109
Vec & rotate_z(double ang)
Rotate around z-axis with given angle.
Definition Vec.hh:235
const char * __repr__() const
Get string representation of this vector.
Definition Vec.hh:176
Vec __div__(double d) const
Divide vector.
Definition Vec.hh:227
double theta() const
Get zenith angle.
Definition Vec.hh:138
double x
Definition Vec.hh:14
Vec & rotate_x(double ang)
Rotate around x-axis with given angle.
Definition Vec.hh:250
double y
Definition Vec.hh:14
Vec operator-() const
Negate vector.
Definition Vec.hh:83
Vec __sub__(const Vec &v) const
Subtract vector.
Definition Vec.hh:203
Vec __mul__(double d) const
Multiply vector.
Definition Vec.hh:211
Vec __rmul__(double d) const
Multiply vector.
Definition Vec.hh:219
Vec & operator*=(double d)
Multiply vector.
Definition Vec.hh:68
Vec & operator+=(const Vec &v)
Add vector.
Definition Vec.hh:52
Vec(double x_, double y_, double z_)
Constructor.
Definition Vec.hh:23
bool operator==(const Vec &v) const
Check equality with given vector.
Definition Vec.hh:91
Vec __add__(const Vec &v) const
Add vector.
Definition Vec.hh:195
void print(std::ostream &out=std::cout) const
Print vector.
Definition Vec.hh:166
double lenxy() const
Get length of (x,y) component.
Definition Vec.hh:152
double phi() const
Get azimuth angle.
Definition Vec.hh:131
Vec()
Default constructor.
Definition Vec.hh:28
Vec & rotate_y(double ang)
Rotate around y-axis with given angle.
Definition Vec.hh:265
Vec & operator/=(double d)
Divide vector.
Definition Vec.hh:76