Kstars

SpatialVector.cpp
1
2//# Filename: SpatialVector.cpp
3//#
4//# The SpatialVector class is defined here.
5//#
6//# Author: Peter Z. Kunszt based on A. Szalay's code
7//#
8//# Date: October 15, 1998
9//#
10//# SPDX-FileCopyrightText: 2000 Peter Z. Kunszt Alex S. Szalay, Aniruddha R. Thakar
11//# The Johns Hopkins University
12//# Modification History:
13//#
14//# Oct 18, 2001 : Dennis C. Dinge -- Replaced ValVec with std::vector
15//#
16
17#include "SpatialVector.h"
18#include "SpatialException.h"
19
20//==============================================================
21//
22// This 3D vector lives on the surface of the sphere.
23// Its length is always 1.
24//
25//==============================================================
26
27/////////////CONSTRUCTOR//////////////////////////////////
28//
29SpatialVector::SpatialVector() : x_(1), y_(0), z_(0), ra_(0), dec_(0), okRaDec_(true)
30{
31}
32
33SpatialVector::SpatialVector(float64 x, float64 y, float64 z) : x_(x), y_(y), z_(z), okRaDec_(false)
34{
35}
36
37/////////////CONSTRUCTOR//////////////////////////////////
38//
39SpatialVector::SpatialVector(float64 ra, float64 dec) : ra_(ra), dec_(dec), okRaDec_(true)
40{
41 updateXYZ();
42 updateRaDec();
43}
44
45/////////////SET//////////////////////////////////////////
46//
47void SpatialVector::set(const float64 &x, const float64 &y, const float64 &z)
48{
49 x_ = x;
50 y_ = y;
51 z_ = z;
52 normalize();
53 updateRaDec();
54}
55/////////////SET//////////////////////////////////////////
56//
57void SpatialVector::set(const float64 &ra, const float64 &dec)
58{
59 ra_ = ra;
60 dec_ = dec;
61 updateXYZ();
62}
63
64/////////////GET//////////////////////////////////////////
65//
66void SpatialVector::get(float64 &x, float64 &y, float64 &z) const
67{
68 x = x_;
69 y = y_;
70 z = z_;
71}
72
73/////////////GET//////////////////////////////////////////
74//
75void SpatialVector::get(float64 &ra, float64 &dec)
76{
77 if (!okRaDec_)
78 {
79 normalize();
80 updateRaDec();
81 }
82 ra = ra_;
83 dec = dec_;
84}
85
87{
88 if (!okRaDec_)
89 {
90 normalize();
91 updateRaDec();
92 }
93 return ra_;
94}
95
97{
98 if (!okRaDec_)
99 {
100 normalize();
101 updateRaDec();
102 }
103 return dec_;
104}
105
106/////////////NORMALIZE////////////////////////////////////
107//
109{
110 float64 sum = x_ * x_ + y_ * y_ + z_ * z_;
111
112 sum = sqrt(sum);
113 if (sum == 0)
114 {
115 x_ = 0;
116 y_ = 0;
117 z_ = 0;
118 return;
119 }
120
121 x_ /= sum;
122 y_ /= sum;
123 z_ /= sum;
124}
125
126/////////////LENGTH///////////////////////////////////////
127//
129{
130 float64 sum;
131 sum = x_ * x_ + y_ * y_ + z_ * z_;
132 return sum > gEpsilon ? sqrt(sum) : 0.0;
133}
134
135/////////////UPDATERADEC//////////////////////////////////
136//
137void SpatialVector::updateRaDec()
138{
139 dec_ = asin(z_) / gPr; // easy.
140 float64 cd = cos(dec_ * gPr);
141 if (cd > gEpsilon || cd < -gEpsilon)
142 if (y_ > gEpsilon || y_ < -gEpsilon)
143 if (y_ < 0.0)
144 ra_ = 360 - acos(x_ / cd) / gPr;
145 else
146 ra_ = acos(x_ / cd) / gPr;
147 else
148 ra_ = (x_ < 0.0 ? 180.0 : 0.0);
149 else
150 ra_ = 0.0;
151 okRaDec_ = true;
152}
153
154/////////////UPDATEXYZ////////////////////////////////////
155//
156void SpatialVector::updateXYZ()
157{
158 float64 cd = cos(dec_ * gPr);
159 x_ = cos(ra_ * gPr) * cd;
160 y_ = sin(ra_ * gPr) * cd;
161 z_ = sin(dec_ * gPr);
162}
163/////////////OPERATOR *=//////////////////////////////////
164//
165SpatialVector &SpatialVector::operator*=(float64 a)
166{
167 x_ = a * x_;
168 y_ = a * y_;
169 z_ = a * z_;
170 okRaDec_ = false;
171 return *this;
172}
173
174/////////////OPERATOR *=//////////////////////////////////
175//
176SpatialVector &SpatialVector::operator*=(int a)
177{
178 x_ = a * x_;
179 y_ = a * y_;
180 z_ = a * z_;
181 okRaDec_ = false;
182 return *this;
183}
184
185/////////////OPERATOR *///////////////////////////////////
186// Multiply with a number
187//
188SpatialVector operator*(float64 a, const SpatialVector &v)
189{
190 return SpatialVector(a * v.x_, a * v.y_, a * v.z_);
191}
192
193/////////////OPERATOR *///////////////////////////////////
194// Multiply with a number
195//
196SpatialVector operator*(const SpatialVector &v, float64 a)
197{
198 return SpatialVector(a * v.x_, a * v.y_, a * v.z_);
199}
200
201/////////////OPERATOR *///////////////////////////////////
202// Multiply with a number
203//
204SpatialVector operator*(int a, const SpatialVector &v)
205{
206 return SpatialVector(a * v.x_, a * v.y_, a * v.z_);
207}
208
209/////////////OPERATOR *///////////////////////////////////
210// Multiply with a number
211//
212SpatialVector operator*(const SpatialVector &v, int a)
213{
214 return SpatialVector(a * v.x_, a * v.y_, a * v.z_);
215}
216
217/////////////OPERATOR *///////////////////////////////////
218// dot product
219//
221{
222 return (x_ * v.x_) + (y_ * v.y_) + (z_ * v.z_);
223}
224
225/////////////OPERATOR +///////////////////////////////////
226//
228{
229 return SpatialVector(x_ + v.x_, y_ + v.y_, z_ + v.z_);
230}
231
232/////////////OPERATOR -///////////////////////////////////
233//
235{
236 return SpatialVector(x_ - v.x_, y_ - v.y_, z_ - v.z_);
237}
238
239/////////////OPERATOR ^///////////////////////////////////
240// cross product
241//
243{
244 return SpatialVector(y_ * v.z_ - v.y_ * z_, z_ * v.x_ - v.z_ * x_, x_ * v.y_ - v.x_ * y_);
245}
246
247/////////////OPERATOR ==//////////////////////////////////
248//
250{
251 return ((x_ == v.x_ && y_ == v.y_ && z_ == v.z_) ? 1 : 0);
252}
SpatialVector is a 3D vector usually living on the surface of the sphere.
void normalize()
Normalize vector length to 1.
float64 operator*(const SpatialVector &) const
dot product
float64 dec()
return dec - this norms the vector to 1 if not already done so
float64 y() const
return y
void get(float64 &x, float64 &y, float64 &z) const
Get x,y,z.
SpatialVector operator-(const SpatialVector &) const
subtraction
float64 length() const
return length of vector
float64 z() const
return z
int operator==(const SpatialVector &) const
Comparison.
float64 ra()
return ra - this norms the vector to 1 if not already done so
SpatialVector operator^(const SpatialVector &) const
cross product
SpatialVector operator+(const SpatialVector &) const
addition
void set(const float64 &x, const float64 &y, const float64 &z)
Set member function: set values - always normed to 1.
float64 x() const
return x (only as rvalue)
SpatialVector()
constructs (1,0,0), ra=0, dec=0.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:04:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.