Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-27 09:15:55

0001 /**
0002  *  @file   PandoraSDK/include/Objects/CartesianVector.h
0003  * 
0004  *  @brief  Header file for the cartesian vector class.
0005  * 
0006  *  $Log: $
0007  */
0008 #ifndef PANDORA_CARTESIAN_VECTOR_H
0009 #define PANDORA_CARTESIAN_VECTOR_H 1
0010 
0011 #include "Pandora/StatusCodes.h"
0012 
0013 #include <cmath>
0014 #include <iostream>
0015 #include <limits>
0016 
0017 namespace pandora
0018 {
0019 
0020 /**
0021  *  @brief  CartesianVector class
0022  */
0023 class CartesianVector
0024 {
0025 public:
0026     /**
0027      *  @brief  Constructor, create a vector from the cartesian coordinates of the end point,
0028      *          origin at (0,0,0,)
0029      * 
0030      *  @param  x the end point x coordinate
0031      *  @param  y the end point y coordinate
0032      *  @param  z the end point z coordinate
0033      */
0034     CartesianVector(float x, float y, float z);
0035 
0036     /**
0037      *  @brief  Copy constructor
0038      * 
0039      *  @param  rhs the cartesian vector to copy
0040      */
0041     CartesianVector(const CartesianVector &rhs);
0042 
0043     /**
0044      *  @brief  Set the values of cartesian vector components
0045      * 
0046      *  @param  x the x coordinate
0047      *  @param  y the y coordinate
0048      *  @param  z the z coordinate
0049      */
0050     void SetValues(float x, float y, float z);
0051 
0052     /**
0053      *  @brief  Get the cartesian x coordinate
0054      * 
0055      *  @return The cartesian x coordinate
0056      */
0057     float GetX() const;
0058 
0059     /**
0060      *  @brief  Get the cartesian y coordinate
0061      * 
0062      *  @return The cartesian y coordinate
0063      */
0064     float GetY() const;
0065 
0066     /**
0067      *  @brief  Get the cartesian z coordinate
0068      * 
0069      *  @return The cartesian z coordinate
0070      */
0071     float GetZ() const;
0072 
0073     /**
0074      *  @brief  Get the magnitude
0075      * 
0076      *  @return The magnitude
0077      */
0078     float GetMagnitude() const;
0079 
0080     /**
0081      *  @brief  Get the magnitude squared
0082      * 
0083      *  @return The magnitude squared
0084      */
0085     float GetMagnitudeSquared() const;
0086 
0087     /**
0088      *  @brief  Get the dot product of the cartesian vector with a second cartesian vector
0089      * 
0090      *  @param  rhs the second cartesian vector
0091      * 
0092      *  @return The dot product
0093      */
0094     float GetDotProduct(const CartesianVector &rhs) const;
0095 
0096     /**
0097      *  @brief  Get the cross product of the cartesian vector with a second cartesian vector
0098      * 
0099      *  @param  rhs the second cartesian vector
0100      * 
0101      *  @return The cross product
0102      */
0103     CartesianVector GetCrossProduct(const CartesianVector &rhs) const;
0104 
0105     /**
0106      *  @brief  Get the cosine of the opening angle of the cartesian vector with respect to a second cartesian vector
0107      * 
0108      *  @param  rhs the second cartesian vector
0109      * 
0110      *  @return The cosine of the opening angle
0111      */
0112     float GetCosOpeningAngle(const CartesianVector &rhs) const;
0113 
0114     /**
0115      *  @brief  Get the opening angle of the cartesian vector with respect to a second cartesian vector
0116      * 
0117      *  @param  rhs the second cartesian vector
0118      * 
0119      *  @return The opening angle
0120      */
0121     float GetOpeningAngle(const CartesianVector &rhs) const;
0122 
0123     /**
0124      *  @brief  Get the distance squared of a cartesian vector with respect to a second cartesian vector
0125      *
0126      *  @param  rhs the second cartesian vector
0127      *
0128      *  @return The distance squared
0129      */
0130     float GetDistanceSquared(const CartesianVector &rhs) const;
0131 
0132     /**
0133      *  @brief  Get the spherical coordinates of the cartesian vector
0134      * 
0135      *  @param  radius the magnitude of the vector
0136      *  @param  phi the azimuth of the vector
0137      *  @param  theta the inclination of the vector
0138      */
0139     void GetSphericalCoordinates(float &radius, float &phi, float &theta) const;
0140 
0141     /**
0142      *  @brief  Get the cylindrical coordinates of the cartesian vector (x/y .. radius, z .. z)
0143      * 
0144      *  @param  radius the radius (x,y-plane) of the vector
0145      *  @param  phi the azimuth of the vector
0146      *  @param  z the z position of the vector
0147      */
0148     void GetCylindricalCoordinates(float &radius, float &phi, float &z) const;
0149 
0150     /**
0151      *  @brief  Get a unit vector in the direction of the cartesian vector
0152      * 
0153      *  @return The unit vector
0154      */
0155     CartesianVector GetUnitVector() const;
0156 
0157     /**
0158      *  @brief  Cartesian vector assignment operator
0159      * 
0160      *  @param  rhs the cartesian vector to assign
0161      */
0162     CartesianVector &operator=(const CartesianVector &rhs);
0163 
0164     /**
0165      *  @brief  Cartesian vector += operator
0166      * 
0167      *  @param  rhs the cartesian vector to add
0168      */
0169     CartesianVector &operator+=(const CartesianVector &rhs);
0170 
0171     /**
0172      *  @brief  Cartesian vector -= operator
0173      * 
0174      *  @param  rhs the cartesian vector to subtract
0175      */
0176     CartesianVector &operator-=(const CartesianVector &rhs);
0177 
0178     /**
0179      *  @brief  Cartesian vector *= operator
0180      * 
0181      *  @param  scalar the scalar to multiply
0182      */
0183     CartesianVector &operator*=(const double scalar);
0184 
0185     /**
0186      *  @brief  Cartesian vector == operator
0187      * 
0188      *  @param  rhs the cartesian vector to compare
0189      */
0190     bool operator==(const CartesianVector &rhs) const;
0191 
0192 private:
0193     float   m_x;                ///< The x coordinate
0194     float   m_y;                ///< The y coordinate
0195     float   m_z;                ///< The z coordinate
0196 };
0197 
0198 /**
0199  *  @brief  Cartesian vector addition operator
0200  * 
0201  *  @param  lhs first cartesian vector, to which the second is added
0202  *  @param  rhs second cartesian vector, which is added to the first
0203  */
0204 CartesianVector operator+(const CartesianVector &lhs, const CartesianVector &rhs);
0205 
0206 /**
0207  *  @brief  Cartesian vector subtraction operator
0208  * 
0209  *  @param  lhs first cartesian vector, from which the second is subtracted
0210  *  @param  rhs second cartesian vector, which is subtracted from the first
0211  */
0212 CartesianVector operator-(const CartesianVector &lhs, const CartesianVector &rhs);
0213 
0214 /**
0215  *  @brief  Cartesian vector multiplication with scalar operator
0216  * 
0217  *  @param  lhs the cartesian vector to be multiplied by the scalar
0218  *  @param  scalar the value of the scalar
0219  */
0220 CartesianVector operator*(const CartesianVector &lhs, const double scalar);
0221 
0222 /**
0223  *  @brief  Operator to dump cartesian vector properties to an ostream
0224  *
0225  *  @param  stream the target ostream
0226  *  @param  cartesianVector the cartesian vector
0227  */
0228 std::ostream &operator<<(std::ostream & stream, const CartesianVector& cartesianVector);
0229 
0230 //------------------------------------------------------------------------------------------------------------------------------------------
0231 
0232 inline CartesianVector::CartesianVector(float x, float y, float z) :
0233     m_x(x),
0234     m_y(y),
0235     m_z(z)
0236 {
0237 }
0238 
0239 //------------------------------------------------------------------------------------------------------------------------------------------
0240 
0241 inline CartesianVector::CartesianVector(const CartesianVector &rhs) :
0242     m_x(rhs.m_x),
0243     m_y(rhs.m_y),
0244     m_z(rhs.m_z)
0245 {
0246 }
0247 
0248 //------------------------------------------------------------------------------------------------------------------------------------------
0249 
0250 inline void CartesianVector::SetValues(float x, float y, float z)
0251 {
0252     m_x = x;
0253     m_y = y;
0254     m_z = z;
0255 }
0256 
0257 //------------------------------------------------------------------------------------------------------------------------------------------
0258 
0259 inline float CartesianVector::GetX() const
0260 {
0261     return m_x;
0262 }
0263 
0264 //------------------------------------------------------------------------------------------------------------------------------------------
0265 
0266 inline float CartesianVector::GetY() const
0267 {
0268     return m_y;
0269 }
0270 
0271 //------------------------------------------------------------------------------------------------------------------------------------------
0272 
0273 inline float CartesianVector::GetZ() const
0274 {
0275     return m_z;
0276 }
0277 
0278 //------------------------------------------------------------------------------------------------------------------------------------------
0279 
0280 inline float CartesianVector::GetMagnitude() const
0281 {
0282     return std::sqrt(this->GetMagnitudeSquared());
0283 }
0284 
0285 //------------------------------------------------------------------------------------------------------------------------------------------
0286 
0287 inline float CartesianVector::GetMagnitudeSquared() const
0288 {
0289     return ((m_x * m_x) + (m_y * m_y) + (m_z * m_z));
0290 }
0291 
0292 //------------------------------------------------------------------------------------------------------------------------------------------
0293 
0294 inline float CartesianVector::GetDotProduct(const CartesianVector &rhs) const
0295 {
0296     return ((m_x * rhs.m_x) + (m_y * rhs.m_y) + (m_z * rhs.m_z));
0297 }
0298 
0299 //------------------------------------------------------------------------------------------------------------------------------------------
0300 
0301 inline CartesianVector CartesianVector::GetCrossProduct(const CartesianVector &rhs) const
0302 {
0303     return CartesianVector( (m_y * rhs.m_z) - (rhs.m_y * m_z),
0304                             (m_z * rhs.m_x) - (rhs.m_z * m_x),
0305                             (m_x * rhs.m_y) - (rhs.m_x * m_y));
0306 }
0307 
0308 //------------------------------------------------------------------------------------------------------------------------------------------
0309 
0310 inline float CartesianVector::GetOpeningAngle(const CartesianVector &rhs) const
0311 {
0312     return std::acos(this->GetCosOpeningAngle(rhs));
0313 }
0314 
0315 //------------------------------------------------------------------------------------------------------------------------------------------
0316 
0317 inline float CartesianVector::GetDistanceSquared(const CartesianVector &rhs) const
0318 {
0319     return (  (m_x - rhs.m_x) * (m_x - rhs.m_x)
0320             + (m_y - rhs.m_y) * (m_y - rhs.m_y)
0321             + (m_z - rhs.m_z) * (m_z - rhs.m_z));
0322 }
0323 
0324 //------------------------------------------------------------------------------------------------------------------------------------------
0325 
0326 inline CartesianVector &CartesianVector::operator=(const CartesianVector &rhs)
0327 {
0328     this->SetValues(rhs.m_x, rhs.m_y, rhs.m_z);
0329     return *this;
0330 }
0331 
0332 //------------------------------------------------------------------------------------------------------------------------------------------
0333 
0334 inline CartesianVector &CartesianVector::operator+=(const CartesianVector &rhs)
0335 {
0336     this->SetValues(m_x + rhs.m_x, m_y + rhs.m_y, m_z + rhs.m_z);
0337     return *this;
0338 }
0339 
0340 //------------------------------------------------------------------------------------------------------------------------------------------
0341 
0342 inline CartesianVector &CartesianVector::operator-=(const CartesianVector &rhs)
0343 {
0344     this->SetValues(m_x - rhs.m_x, m_y - rhs.m_y, m_z - rhs.m_z);
0345     return *this;
0346 }
0347 
0348 //------------------------------------------------------------------------------------------------------------------------------------------
0349 
0350 inline CartesianVector &CartesianVector::operator*=(const double scalar)
0351 {
0352     this->SetValues(static_cast<float>(m_x * scalar), static_cast<float>(m_y * scalar), static_cast<float>(m_z * scalar));
0353     return *this;
0354 }
0355 
0356 //------------------------------------------------------------------------------------------------------------------------------------------
0357 
0358 inline bool CartesianVector::operator==(const CartesianVector &rhs) const
0359 {
0360     return ( (std::fabs(m_x - rhs.m_x) < std::numeric_limits<float>::epsilon()) &&
0361         (std::fabs(m_y - rhs.m_y) < std::numeric_limits<float>::epsilon()) &&
0362         (std::fabs(m_z - rhs.m_z) < std::numeric_limits<float>::epsilon()) );
0363 }
0364 
0365 //------------------------------------------------------------------------------------------------------------------------------------------
0366 //------------------------------------------------------------------------------------------------------------------------------------------
0367 
0368 inline CartesianVector operator+(const CartesianVector &lhs, const CartesianVector &rhs)
0369 {
0370     return CartesianVector(lhs.GetX() + rhs.GetX(), lhs.GetY() + rhs.GetY(), lhs.GetZ() + rhs.GetZ());
0371 }
0372 
0373 //------------------------------------------------------------------------------------------------------------------------------------------
0374 
0375 inline CartesianVector operator-(const CartesianVector &lhs, const CartesianVector &rhs)
0376 {
0377     return CartesianVector(lhs.GetX() - rhs.GetX(), lhs.GetY() - rhs.GetY(), lhs.GetZ() - rhs.GetZ());
0378 }
0379 
0380 //------------------------------------------------------------------------------------------------------------------------------------------
0381 
0382 inline CartesianVector operator*(const CartesianVector &lhs, const double scalar)
0383 {
0384     return CartesianVector(static_cast<float>(lhs.GetX() * scalar), static_cast<float>(lhs.GetY() * scalar), static_cast<float>(lhs.GetZ() * scalar));
0385 }
0386 
0387 } // namespace pandora
0388 
0389 #endif // #ifndef PANDORA_CARTESIAN_VECTOR_H