-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvec3.cpp
194 lines (172 loc) · 4.23 KB
/
vec3.cpp
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
//. vec3.cpp - 3D vector
// Copyright (C) 1997,1998 hkuno
// mailto:hkuno.kuno@nifty.ne.jp
#include "vec3.h"
#include "degree.h"
namespace util {
//------------------------------------------------------------------------
//.----- class Mat3x3 ----------------------------------------------------
//------------------------------------------------------------------------
//. Mat3x3::setE() - 単位行列設定
void
Mat3x3::setE()
{
m11 = m22 = m33 = 1;
m12 = m13 = m21 = m23 = m31 = m32 = 0;
}
//. Mat3x3::setRotate - 回転行列設定
void
Mat3x3::setRotate(const Degree& angle, int axis)
{
const double s = sin(angle);
const double c = cos(angle);
switch (axis) {
case 'X':
m11 = 1; m12 = 0; m13 = 0;
m21 = 0; m22 = c; m23 = s;
m31 = 0; m32 = -s; m33 = c;
break;
case 'Y':
m11 = c; m12 = 0; m13 = -s;
m21 = 0; m22 = 1; m23 = 0;
m31 = s; m32 = 0; m33 = c;
break;
case 'Z':
m11 = c; m12 = s; m13 = 0;
m21 = -s; m22 = c; m23 = 0;
m31 = 0; m32 = 0; m33 = 1;
break;
default:
throw "Mat3x3::setRotate(angle, axis) - bad axis";
}//endswitch axis
}
//. Mat3x3::operator+=() - 加算
Mat3x3&
Mat3x3::operator+=(const Mat3x3& m)
{
#define M(N) m##N += m.m##N
M(11); M(12); M(13);
M(21); M(22); M(23);
M(31); M(32); M(33);
#undef M
return *this;
}
//. Mat3x3::operator-=() - 減算
Mat3x3&
Mat3x3::operator-=(const Mat3x3& m)
{
#define M(N) m##N -= m.m##N
M(11); M(12); M(13);
M(21); M(22); M(23);
M(31); M(32); M(33);
#undef M
return *this;
}
//. Mat3x3::operator*=() - 係数乗算
Mat3x3&
Mat3x3::operator*=(double k)
{
#define M(N) m##N *= k
M(11); M(12); M(13);
M(21); M(22); M(23);
M(31); M(32); M(33);
#undef M
return *this;
}
//. Mat3x3::operator/=() - 係数除算
Mat3x3&
Mat3x3::operator/=(double k)
{
#define M(N) m##N /= k
M(11); M(12); M(13);
M(21); M(22); M(23);
M(31); M(32); M(33);
#undef M
return *this;
}
//. Mat3x3::operator*=() - 乗算
Mat3x3&
Mat3x3::operator*=(const Mat3x3& m)
{
#define M(L,C) \
m##L##C = a.m##L##1 * m.m1##C \
+ a.m##L##2 * m.m2##C \
+ a.m##L##3 * m.m3##C
Mat3x3 a(*this);
M(1,1); M(1,2); M(1,3);
M(2,1); M(2,2); M(2,3);
M(3,1); M(3,2); M(3,3);
#undef M
return *this;
}
//------------------------------------------------------------------------
//.----- class Vec3 : 1行3列のベクトル ---------------------------------
//------------------------------------------------------------------------
//. Vec3::setDirectionCosines - 方向余弦化
void
Vec3::setDirectionCosines()
{
const double r = radius();
if (r != 0)
*this /= r;
}
//. Vec3::radius - 動径、ベクトルの長さ
double
Vec3::radius() const
{
return ::sqrt(square());
}
//. Vec3::getPolar - 極座標を得る
// @param phi 天頂角φ (0~180°)
// @param theta 方位角θ (-180°~+180°)
// @return 動径r
double
Vec3::getPolar(Degree& phi, Degree& theta) const
{
// 方位角θを得る
theta.setArcTan2(y, x); // -180°~+180°
// 天頂角φを得る
phi.setArcTan2(::sqrt(x * x + y * y), z);
// 長さを計算する
return radius();
}
//. Vec3::getLtLg - 緯度経度を得る
// @param latitude 緯度
// @param longitude 経度(東経を正とする)
// @return 動径r
double
Vec3::getLtLg(Degree& latitude, Degree& longitude) const
{
// 経度(==方位角θ)を得る
longitude.setArcTan2(y, x); // -180°~+180°
// 緯度(==90°- 天頂角φ)を得る
latitude.setArcTan2(z, ::sqrt(x * x + y * y));
// 長さを計算する
return radius();
}
//. Vec3::setPolar - 極座標を設定する
// @param radius 動径r
// @param phi 天頂角φ
// @param theta 方位角θ
void
Vec3::setPolar(double radius, const Degree& phi, const Degree& theta)
{
const double sin_phi = sin(phi);
x = radius * sin_phi * cos(theta);
y = radius * sin_phi * sin(theta);
z = radius * cos(phi);
}
//. Vec3::setLtLg - 緯度経度を設定する
// @param radius 動径r
// @param latitude 緯度
// @param longitude 経度(東経を正とする)
void
Vec3::setLtLg(double radius, const Degree& latitude, const Degree& longitude)
{
const double cos_lt = cos(latitude);
x = radius * cos_lt * cos(longitude);
y = radius * cos_lt * sin(longitude);
z = radius * sin(latitude);
}
}//.endnamespace util
//. vec3.cpp - end