Pandora
Pandora source code navigator
Loading...
Searching...
No Matches
CartesianVector.h
Go to the documentation of this file.
1
8#ifndef PANDORA_CARTESIAN_VECTOR_H
9#define PANDORA_CARTESIAN_VECTOR_H 1
10
11#include "Pandora/StatusCodes.h"
12
13#include <cmath>
14#include <iostream>
15#include <limits>
16
17namespace pandora
18{
19
24{
25public:
34 CartesianVector(float x, float y, float z);
35
42
50 void SetValues(float x, float y, float z);
51
57 float GetX() const;
58
64 float GetY() const;
65
71 float GetZ() const;
72
78 float GetMagnitude() const;
79
85 float GetMagnitudeSquared() const;
86
94 float GetDotProduct(const CartesianVector &rhs) const;
95
104
112 float GetCosOpeningAngle(const CartesianVector &rhs) const;
113
121 float GetOpeningAngle(const CartesianVector &rhs) const;
122
130 float GetDistanceSquared(const CartesianVector &rhs) const;
131
139 void GetSphericalCoordinates(float &radius, float &phi, float &theta) const;
140
148 void GetCylindricalCoordinates(float &radius, float &phi, float &z) const;
149
156
163
170
177
183 CartesianVector &operator*=(const double scalar);
184
190 bool operator==(const CartesianVector &rhs) const;
191
192private:
193 float m_x;
194 float m_y;
195 float m_z;
196};
197
205
213
220CartesianVector operator*(const CartesianVector &lhs, const double scalar);
221
228std::ostream &operator<<(std::ostream & stream, const CartesianVector& cartesianVector);
229
230//------------------------------------------------------------------------------------------------------------------------------------------
231
232inline CartesianVector::CartesianVector(float x, float y, float z) :
233 m_x(x),
234 m_y(y),
235 m_z(z)
236{
237}
238
239//------------------------------------------------------------------------------------------------------------------------------------------
240
242 m_x(rhs.m_x),
243 m_y(rhs.m_y),
244 m_z(rhs.m_z)
245{
246}
247
248//------------------------------------------------------------------------------------------------------------------------------------------
249
250inline void CartesianVector::SetValues(float x, float y, float z)
251{
252 m_x = x;
253 m_y = y;
254 m_z = z;
255}
256
257//------------------------------------------------------------------------------------------------------------------------------------------
258
259inline float CartesianVector::GetX() const
260{
261 return m_x;
262}
263
264//------------------------------------------------------------------------------------------------------------------------------------------
265
266inline float CartesianVector::GetY() const
267{
268 return m_y;
269}
270
271//------------------------------------------------------------------------------------------------------------------------------------------
272
273inline float CartesianVector::GetZ() const
274{
275 return m_z;
276}
277
278//------------------------------------------------------------------------------------------------------------------------------------------
279
281{
282 return std::sqrt(this->GetMagnitudeSquared());
283}
284
285//------------------------------------------------------------------------------------------------------------------------------------------
286
288{
289 return ((m_x * m_x) + (m_y * m_y) + (m_z * m_z));
290}
291
292//------------------------------------------------------------------------------------------------------------------------------------------
293
295{
296 return ((m_x * rhs.m_x) + (m_y * rhs.m_y) + (m_z * rhs.m_z));
297}
298
299//------------------------------------------------------------------------------------------------------------------------------------------
300
302{
303 return CartesianVector( (m_y * rhs.m_z) - (rhs.m_y * m_z),
304 (m_z * rhs.m_x) - (rhs.m_z * m_x),
305 (m_x * rhs.m_y) - (rhs.m_x * m_y));
306}
307
308//------------------------------------------------------------------------------------------------------------------------------------------
309
311{
312 return std::acos(this->GetCosOpeningAngle(rhs));
313}
314
315//------------------------------------------------------------------------------------------------------------------------------------------
316
318{
319 return ( (m_x - rhs.m_x) * (m_x - rhs.m_x)
320 + (m_y - rhs.m_y) * (m_y - rhs.m_y)
321 + (m_z - rhs.m_z) * (m_z - rhs.m_z));
322}
323
324//------------------------------------------------------------------------------------------------------------------------------------------
325
327{
328 this->SetValues(rhs.m_x, rhs.m_y, rhs.m_z);
329 return *this;
330}
331
332//------------------------------------------------------------------------------------------------------------------------------------------
333
335{
336 this->SetValues(m_x + rhs.m_x, m_y + rhs.m_y, m_z + rhs.m_z);
337 return *this;
338}
339
340//------------------------------------------------------------------------------------------------------------------------------------------
341
343{
344 this->SetValues(m_x - rhs.m_x, m_y - rhs.m_y, m_z - rhs.m_z);
345 return *this;
346}
347
348//------------------------------------------------------------------------------------------------------------------------------------------
349
351{
352 this->SetValues(static_cast<float>(m_x * scalar), static_cast<float>(m_y * scalar), static_cast<float>(m_z * scalar));
353 return *this;
354}
355
356//------------------------------------------------------------------------------------------------------------------------------------------
357
358inline bool CartesianVector::operator==(const CartesianVector &rhs) const
359{
360 return ( (std::fabs(m_x - rhs.m_x) < std::numeric_limits<float>::epsilon()) &&
361 (std::fabs(m_y - rhs.m_y) < std::numeric_limits<float>::epsilon()) &&
362 (std::fabs(m_z - rhs.m_z) < std::numeric_limits<float>::epsilon()) );
363}
364
365//------------------------------------------------------------------------------------------------------------------------------------------
366//------------------------------------------------------------------------------------------------------------------------------------------
367
369{
370 return CartesianVector(lhs.GetX() + rhs.GetX(), lhs.GetY() + rhs.GetY(), lhs.GetZ() + rhs.GetZ());
371}
372
373//------------------------------------------------------------------------------------------------------------------------------------------
374
376{
377 return CartesianVector(lhs.GetX() - rhs.GetX(), lhs.GetY() - rhs.GetY(), lhs.GetZ() - rhs.GetZ());
378}
379
380//------------------------------------------------------------------------------------------------------------------------------------------
381
382inline CartesianVector operator*(const CartesianVector &lhs, const double scalar)
383{
384 return CartesianVector(static_cast<float>(lhs.GetX() * scalar), static_cast<float>(lhs.GetY() * scalar), static_cast<float>(lhs.GetZ() * scalar));
385}
386
387} // namespace pandora
388
389#endif // #ifndef PANDORA_CARTESIAN_VECTOR_H
Header file defining status codes and relevant preprocessor macros.
CartesianVector class.
CartesianVector & operator+=(const CartesianVector &rhs)
Cartesian vector += operator.
float m_y
The y coordinate.
void SetValues(float x, float y, float z)
Set the values of cartesian vector components.
float GetCosOpeningAngle(const CartesianVector &rhs) const
Get the cosine of the opening angle of the cartesian vector with respect to a second cartesian vector...
float GetMagnitudeSquared() const
Get the magnitude squared.
float GetX() const
Get the cartesian x coordinate.
void GetSphericalCoordinates(float &radius, float &phi, float &theta) const
Get the spherical coordinates of the cartesian vector.
float GetDistanceSquared(const CartesianVector &rhs) const
Get the distance squared of a cartesian vector with respect to a second cartesian vector.
CartesianVector & operator*=(const double scalar)
Cartesian vector *= operator.
float m_x
The x coordinate.
CartesianVector GetUnitVector() const
Get a unit vector in the direction of the cartesian vector.
bool operator==(const CartesianVector &rhs) const
Cartesian vector == operator.
CartesianVector & operator-=(const CartesianVector &rhs)
Cartesian vector -= operator.
float GetZ() const
Get the cartesian z coordinate.
float GetDotProduct(const CartesianVector &rhs) const
Get the dot product of the cartesian vector with a second cartesian vector.
float GetMagnitude() const
Get the magnitude.
CartesianVector GetCrossProduct(const CartesianVector &rhs) const
Get the cross product of the cartesian vector with a second cartesian vector.
CartesianVector & operator=(const CartesianVector &rhs)
Cartesian vector assignment operator.
float GetOpeningAngle(const CartesianVector &rhs) const
Get the opening angle of the cartesian vector with respect to a second cartesian vector.
CartesianVector(float x, float y, float z)
Constructor, create a vector from the cartesian coordinates of the end point, origin at (0,...
float GetY() const
Get the cartesian y coordinate.
float m_z
The z coordinate.
void GetCylindricalCoordinates(float &radius, float &phi, float &z) const
Get the cylindrical coordinates of the cartesian vector (x/y .. radius, z .. z)
std::ostream & operator<<(std::ostream &stream, const CartesianVector &cartesianVector)
Operator to dump cartesian vector properties to an ostream.
CartesianVector operator+(const CartesianVector &lhs, const CartesianVector &rhs)
Cartesian vector addition operator.
CartesianVector operator*(const CartesianVector &lhs, const double scalar)
Cartesian vector multiplication with scalar operator.
CartesianVector operator-(const CartesianVector &lhs, const CartesianVector &rhs)
Cartesian vector subtraction operator.