File: SpatialVector.cpp

package info (click to toggle)
siril 1.4.0~rc2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,352 kB
  • sloc: ansic: 174,082; cpp: 28,254; python: 7,891; makefile: 974; xml: 777; sh: 271
file content (252 lines) | stat: -rw-r--r-- 5,797 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

//#     Filename:       SpatialVector.cpp
//#
//#     The SpatialVector class is defined here.
//#
//#     Author:         Peter Z. Kunszt based on A. Szalay's code
//#
//#     Date:           October 15, 1998
//#
//#	    SPDX-FileCopyrightText: 2000 Peter Z. Kunszt Alex S. Szalay, Aniruddha R. Thakar
//#                     The Johns Hopkins University
//#     Modification History:
//#
//#     Oct 18, 2001 : Dennis C. Dinge -- Replaced ValVec with std::vector
//#

#include "SpatialVector.h"
#include "SpatialException.h"

//==============================================================
//
// This 3D vector lives on the surface of the sphere.
// Its length is always 1.
//
//==============================================================

/////////////CONSTRUCTOR//////////////////////////////////
//
SpatialVector::SpatialVector() : x_(1), y_(0), z_(0), ra_(0), dec_(0), okRaDec_(true)
{
}

SpatialVector::SpatialVector(float64 x, float64 y, float64 z) : x_(x), y_(y), z_(z), okRaDec_(false)
{
}

/////////////CONSTRUCTOR//////////////////////////////////
//
SpatialVector::SpatialVector(float64 ra, float64 dec) : ra_(ra), dec_(dec), okRaDec_(true)
{
    updateXYZ();
    updateRaDec();
}

/////////////SET//////////////////////////////////////////
//
void SpatialVector::set(const float64 &x, const float64 &y, const float64 &z)
{
    x_ = x;
    y_ = y;
    z_ = z;
    normalize();
    updateRaDec();
}
/////////////SET//////////////////////////////////////////
//
void SpatialVector::set(const float64 &ra, const float64 &dec)
{
    ra_  = ra;
    dec_ = dec;
    updateXYZ();
}

/////////////GET//////////////////////////////////////////
//
void SpatialVector::get(float64 &x, float64 &y, float64 &z) const
{
    x = x_;
    y = y_;
    z = z_;
}

/////////////GET//////////////////////////////////////////
//
void SpatialVector::get(float64 &ra, float64 &dec)
{
    if (!okRaDec_)
    {
        normalize();
        updateRaDec();
    }
    ra  = ra_;
    dec = dec_;
}

float64 SpatialVector::ra()
{
    if (!okRaDec_)
    {
        normalize();
        updateRaDec();
    }
    return ra_;
}

float64 SpatialVector::dec()
{
    if (!okRaDec_)
    {
        normalize();
        updateRaDec();
    }
    return dec_;
}

/////////////NORMALIZE////////////////////////////////////
//
void SpatialVector::normalize()
{
    float64 sum = x_ * x_ + y_ * y_ + z_ * z_;

    sum = sqrt(sum);
    if (sum == 0)
    {
        x_ = 0;
        y_ = 0;
        z_ = 0;
        return;
    }

    x_ /= sum;
    y_ /= sum;
    z_ /= sum;
}

/////////////LENGTH///////////////////////////////////////
//
float64 SpatialVector::length() const
{
    float64 sum;
    sum = x_ * x_ + y_ * y_ + z_ * z_;
    return sum > gEpsilon ? sqrt(sum) : 0.0;
}

/////////////UPDATERADEC//////////////////////////////////
//
void SpatialVector::updateRaDec()
{
    dec_       = asin(z_) / gPr; // easy.
    float64 cd = cos(dec_ * gPr);
    if (cd > gEpsilon || cd < -gEpsilon)
        if (y_ > gEpsilon || y_ < -gEpsilon)
            if (y_ < 0.0)
                ra_ = 360 - acos(x_ / cd) / gPr;
            else
                ra_ = acos(x_ / cd) / gPr;
        else
            ra_ = (x_ < 0.0 ? 180.0 : 0.0);
    else
        ra_ = 0.0;
    okRaDec_ = true;
}

/////////////UPDATEXYZ////////////////////////////////////
//
void SpatialVector::updateXYZ()
{
    float64 cd = cos(dec_ * gPr);
    x_         = cos(ra_ * gPr) * cd;
    y_         = sin(ra_ * gPr) * cd;
    z_         = sin(dec_ * gPr);
}
/////////////OPERATOR *=//////////////////////////////////
//
SpatialVector &SpatialVector::operator*=(float64 a)
{
    x_       = a * x_;
    y_       = a * y_;
    z_       = a * z_;
    okRaDec_ = false;
    return *this;
}

/////////////OPERATOR *=//////////////////////////////////
//
SpatialVector &SpatialVector::operator*=(int a)
{
    x_       = a * x_;
    y_       = a * y_;
    z_       = a * z_;
    okRaDec_ = false;
    return *this;
}

/////////////OPERATOR *///////////////////////////////////
// Multiply with a number
//
SpatialVector operator*(float64 a, const SpatialVector &v)
{
    return SpatialVector(a * v.x_, a * v.y_, a * v.z_);
}

/////////////OPERATOR *///////////////////////////////////
// Multiply with a number
//
SpatialVector operator*(const SpatialVector &v, float64 a)
{
    return SpatialVector(a * v.x_, a * v.y_, a * v.z_);
}

/////////////OPERATOR *///////////////////////////////////
// Multiply with a number
//
SpatialVector operator*(int a, const SpatialVector &v)
{
    return SpatialVector(a * v.x_, a * v.y_, a * v.z_);
}

/////////////OPERATOR *///////////////////////////////////
// Multiply with a number
//
SpatialVector operator*(const SpatialVector &v, int a)
{
    return SpatialVector(a * v.x_, a * v.y_, a * v.z_);
}

/////////////OPERATOR *///////////////////////////////////
// dot product
//
float64 SpatialVector::operator*(const SpatialVector &v) const
{
    return (x_ * v.x_) + (y_ * v.y_) + (z_ * v.z_);
}

/////////////OPERATOR +///////////////////////////////////
//
SpatialVector SpatialVector::operator+(const SpatialVector &v) const
{
    return SpatialVector(x_ + v.x_, y_ + v.y_, z_ + v.z_);
}

/////////////OPERATOR -///////////////////////////////////
//
SpatialVector SpatialVector::operator-(const SpatialVector &v) const
{
    return SpatialVector(x_ - v.x_, y_ - v.y_, z_ - v.z_);
}

/////////////OPERATOR ^///////////////////////////////////
// cross product
//
SpatialVector SpatialVector::operator^(const SpatialVector &v) const
{
    return SpatialVector(y_ * v.z_ - v.y_ * z_, z_ * v.x_ - v.z_ * x_, x_ * v.y_ - v.x_ * y_);
}

/////////////OPERATOR ==//////////////////////////////////
//
int SpatialVector::operator==(const SpatialVector &v) const
{
    return ((x_ == v.x_ && y_ == v.y_ && z_ == v.z_) ? 1 : 0);
}