File indexing completed on 2024-04-14 03:42:34

0001 
0002 //#     Filename:       SpatialVector.cpp
0003 //#
0004 //#     The SpatialVector class is defined here.
0005 //#
0006 //#     Author:         Peter Z. Kunszt based on A. Szalay's code
0007 //#
0008 //#     Date:           October 15, 1998
0009 //#
0010 //#     SPDX-FileCopyrightText: 2000 Peter Z. Kunszt Alex S. Szalay, Aniruddha R. Thakar
0011 //#                     The Johns Hopkins University
0012 //#     Modification History:
0013 //#
0014 //#     Oct 18, 2001 : Dennis C. Dinge -- Replaced ValVec with std::vector
0015 //#
0016 
0017 #include "SpatialVector.h"
0018 #include "SpatialException.h"
0019 
0020 //==============================================================
0021 //
0022 // This 3D vector lives on the surface of the sphere.
0023 // Its length is always 1.
0024 //
0025 //==============================================================
0026 
0027 /////////////CONSTRUCTOR//////////////////////////////////
0028 //
0029 SpatialVector::SpatialVector() : x_(1), y_(0), z_(0), ra_(0), dec_(0), okRaDec_(true)
0030 {
0031 }
0032 
0033 SpatialVector::SpatialVector(float64 x, float64 y, float64 z) : x_(x), y_(y), z_(z), okRaDec_(false)
0034 {
0035 }
0036 
0037 /////////////CONSTRUCTOR//////////////////////////////////
0038 //
0039 SpatialVector::SpatialVector(float64 ra, float64 dec) : ra_(ra), dec_(dec), okRaDec_(true)
0040 {
0041     updateXYZ();
0042     updateRaDec();
0043 }
0044 
0045 /////////////SET//////////////////////////////////////////
0046 //
0047 void SpatialVector::set(const float64 &x, const float64 &y, const float64 &z)
0048 {
0049     x_ = x;
0050     y_ = y;
0051     z_ = z;
0052     normalize();
0053     updateRaDec();
0054 }
0055 /////////////SET//////////////////////////////////////////
0056 //
0057 void SpatialVector::set(const float64 &ra, const float64 &dec)
0058 {
0059     ra_  = ra;
0060     dec_ = dec;
0061     updateXYZ();
0062 }
0063 
0064 /////////////GET//////////////////////////////////////////
0065 //
0066 void SpatialVector::get(float64 &x, float64 &y, float64 &z) const
0067 {
0068     x = x_;
0069     y = y_;
0070     z = z_;
0071 }
0072 
0073 /////////////GET//////////////////////////////////////////
0074 //
0075 void SpatialVector::get(float64 &ra, float64 &dec)
0076 {
0077     if (!okRaDec_)
0078     {
0079         normalize();
0080         updateRaDec();
0081     }
0082     ra  = ra_;
0083     dec = dec_;
0084 }
0085 
0086 float64 SpatialVector::ra()
0087 {
0088     if (!okRaDec_)
0089     {
0090         normalize();
0091         updateRaDec();
0092     }
0093     return ra_;
0094 }
0095 
0096 float64 SpatialVector::dec()
0097 {
0098     if (!okRaDec_)
0099     {
0100         normalize();
0101         updateRaDec();
0102     }
0103     return dec_;
0104 }
0105 
0106 /////////////NORMALIZE////////////////////////////////////
0107 //
0108 void SpatialVector::normalize()
0109 {
0110     float64 sum = x_ * x_ + y_ * y_ + z_ * z_;
0111 
0112     sum = sqrt(sum);
0113     if (sum == 0)
0114     {
0115         x_ = 0;
0116         y_ = 0;
0117         z_ = 0;
0118         return;
0119     }
0120 
0121     x_ /= sum;
0122     y_ /= sum;
0123     z_ /= sum;
0124 }
0125 
0126 /////////////LENGTH///////////////////////////////////////
0127 //
0128 float64 SpatialVector::length() const
0129 {
0130     float64 sum;
0131     sum = x_ * x_ + y_ * y_ + z_ * z_;
0132     return sum > gEpsilon ? sqrt(sum) : 0.0;
0133 }
0134 
0135 /////////////UPDATERADEC//////////////////////////////////
0136 //
0137 void SpatialVector::updateRaDec()
0138 {
0139     dec_       = asin(z_) / gPr; // easy.
0140     float64 cd = cos(dec_ * gPr);
0141     if (cd > gEpsilon || cd < -gEpsilon)
0142         if (y_ > gEpsilon || y_ < -gEpsilon)
0143             if (y_ < 0.0)
0144                 ra_ = 360 - acos(x_ / cd) / gPr;
0145             else
0146                 ra_ = acos(x_ / cd) / gPr;
0147         else
0148             ra_ = (x_ < 0.0 ? 180.0 : 0.0);
0149     else
0150         ra_ = 0.0;
0151     okRaDec_ = true;
0152 }
0153 
0154 /////////////UPDATEXYZ////////////////////////////////////
0155 //
0156 void SpatialVector::updateXYZ()
0157 {
0158     float64 cd = cos(dec_ * gPr);
0159     x_         = cos(ra_ * gPr) * cd;
0160     y_         = sin(ra_ * gPr) * cd;
0161     z_         = sin(dec_ * gPr);
0162 }
0163 /////////////OPERATOR *=//////////////////////////////////
0164 //
0165 SpatialVector &SpatialVector::operator*=(float64 a)
0166 {
0167     x_       = a * x_;
0168     y_       = a * y_;
0169     z_       = a * z_;
0170     okRaDec_ = false;
0171     return *this;
0172 }
0173 
0174 /////////////OPERATOR *=//////////////////////////////////
0175 //
0176 SpatialVector &SpatialVector::operator*=(int a)
0177 {
0178     x_       = a * x_;
0179     y_       = a * y_;
0180     z_       = a * z_;
0181     okRaDec_ = false;
0182     return *this;
0183 }
0184 
0185 /////////////OPERATOR *///////////////////////////////////
0186 // Multiply with a number
0187 //
0188 SpatialVector operator*(float64 a, const SpatialVector &v)
0189 {
0190     return SpatialVector(a * v.x_, a * v.y_, a * v.z_);
0191 }
0192 
0193 /////////////OPERATOR *///////////////////////////////////
0194 // Multiply with a number
0195 //
0196 SpatialVector operator*(const SpatialVector &v, float64 a)
0197 {
0198     return SpatialVector(a * v.x_, a * v.y_, a * v.z_);
0199 }
0200 
0201 /////////////OPERATOR *///////////////////////////////////
0202 // Multiply with a number
0203 //
0204 SpatialVector operator*(int a, const SpatialVector &v)
0205 {
0206     return SpatialVector(a * v.x_, a * v.y_, a * v.z_);
0207 }
0208 
0209 /////////////OPERATOR *///////////////////////////////////
0210 // Multiply with a number
0211 //
0212 SpatialVector operator*(const SpatialVector &v, int a)
0213 {
0214     return SpatialVector(a * v.x_, a * v.y_, a * v.z_);
0215 }
0216 
0217 /////////////OPERATOR *///////////////////////////////////
0218 // dot product
0219 //
0220 float64 SpatialVector::operator*(const SpatialVector &v) const
0221 {
0222     return (x_ * v.x_) + (y_ * v.y_) + (z_ * v.z_);
0223 }
0224 
0225 /////////////OPERATOR +///////////////////////////////////
0226 //
0227 SpatialVector SpatialVector::operator+(const SpatialVector &v) const
0228 {
0229     return SpatialVector(x_ + v.x_, y_ + v.y_, z_ + v.z_);
0230 }
0231 
0232 /////////////OPERATOR -///////////////////////////////////
0233 //
0234 SpatialVector SpatialVector::operator-(const SpatialVector &v) const
0235 {
0236     return SpatialVector(x_ - v.x_, y_ - v.y_, z_ - v.z_);
0237 }
0238 
0239 /////////////OPERATOR ^///////////////////////////////////
0240 // cross product
0241 //
0242 SpatialVector SpatialVector::operator^(const SpatialVector &v) const
0243 {
0244     return SpatialVector(y_ * v.z_ - v.y_ * z_, z_ * v.x_ - v.z_ * x_, x_ * v.y_ - v.x_ * y_);
0245 }
0246 
0247 /////////////OPERATOR ==//////////////////////////////////
0248 //
0249 int SpatialVector::operator==(const SpatialVector &v) const
0250 {
0251     return ((x_ == v.x_ && y_ == v.y_ && z_ == v.z_) ? 1 : 0);
0252 }