-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvec3.h
271 lines (211 loc) · 7.74 KB
/
vec3.h
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//. Vec3.h - 3D vector
// Copyright (C) 1997,1998 hkuno
// mailto:[email protected]
#ifndef VEC3_H_
#define VEC3_H_
#include "defs.h"
namespace util {
using namespace std;
class Degree;
//------------------------------------------------------------------------
//.----- class Mat3x3 ----------------------------------------------------
//------------------------------------------------------------------------
// 1行3列のベクトルをアフィン変換する3行3列の行列である
// 加減乗算の他に、回転行列設定、単位行列設定ができる。
class Mat3x3 {
public:
//----- コンストラクタ -------------------------------------------
// 単位行列として生成する
Mat3x3() {
setE();
}
// 回転行列として生成する
// @param angle 回転角度
// @param axis 回転軸('X','Y','Z' のいずれかを指定する)
Mat3x3(const Degree& angle, int axis='Z') {
setRotate(angle, axis);
}
// 指定の3x3要素を持った行列として生成する
Mat3x3(double m11, double m12, double m13,
double m21, double m22, double m23,
double m31, double m32, double m33)
: m11(m11), m12(m12), m13(m13),
m21(m21), m22(m22), m23(m23),
m31(m31), m32(m32), m33(m33) {}
//----- 単位行列設定 ---------------------------------------------
void setE();
//----- 回転行列設定 ---------------------------------------------
// ベクトルの直交座標成分が、右手系(親指=x、人差し指=y、中指=z)のとき
// 回転行列は、ベクトルを次のように回転する。
// ・軸の+方向から原点を見て、軸の回りを反時計回り(左ネジ)
// ・原点から軸の+方向を見て、軸の回りを時計回り(右ネジ)
// ベクトルを固定して座標系の回転とみれば次のようになる。
// ・軸の+方向から原点を見て、軸の回りを時計回り(右ネジ)
// @param angle 回転角度
// @param axis 回転軸('X','Y','Z' のいずれかを指定する)
void setRotate(const Degree& angle, int axis='Z');
//----- 加減乗算 -------------------------------------------------
Mat3x3& operator+=(const Mat3x3&);
Mat3x3& operator-=(const Mat3x3&);
Mat3x3& operator*=(const Mat3x3&);
friend Mat3x3 operator+(const Mat3x3& a, const Mat3x3& b) {
Mat3x3 c(a); return c += b;
}
friend Mat3x3 operator-(const Mat3x3& a, const Mat3x3& b) {
Mat3x3 c(a); return c -= b;
}
friend Mat3x3 operator*(const Mat3x3& a, const Mat3x3& b) {
Mat3x3 c(a); return c *= b;
}
//----- 係数乗除算 -----------------------------------------------
Mat3x3& operator*=(double k);
Mat3x3& operator/=(double k);
friend Mat3x3 operator*(const Mat3x3& a, double k) {
Mat3x3 c(a); return c *= k;
}
friend Mat3x3 operator*(double k, const Mat3x3& a) {
Mat3x3 c(a); return c *= k;
}
friend Mat3x3 operator/(const Mat3x3& a, double k) {
Mat3x3 c(a); return c /= k;
}
//----- 行列データメンバ -----------------------------------------
// ※外部から直接変更されても困らないので、公開してしまう
double m11, m12, m13,
m21, m22, m23,
m31, m32, m33;
};//endclass Mat3x3
// コンストラクタに渡すダミー型。別の引数で渡す数値型の意味付けをする。
class asLtLg {};
//------------------------------------------------------------------------
//.----- class Vec3 : 1行3列のベクトル ---------------------------------
//------------------------------------------------------------------------
// 1行3列の実数ベクトル。3次元直交座標を保持する
class Vec3 {
public:
//----- コンストラクタ -------------------------------------------
// 全て0で生成する
Vec3()
: x(0), y(0), z(0) {}
// 直交座標値で生成する
Vec3(double x, double y, double z)
: x(x), y(y), z(z) {}
// 極座標値で生成する
// @param radius 動径r
// @param phi 天頂角φ
// @param theta 方位角θ
Vec3(double radius, const Degree& phi, const Degree& theta) {
setPolar(radius, phi, theta);
}
// 緯度経度値で生成する
// @param radius 動径r
// @param latitude 緯度
// @param longitude 経度(東経を正とする)
Vec3(double radius, const Degree& latitude, const Degree& longitude, asLtLg) {
setLtLg(radius, latitude, longitude);
}
// 直交座標値の配列から生成する
Vec3(const int* p) : x(p[0]), y(p[1]), z(p[2]) {}
Vec3(const short* p) : x(p[0]), y(p[1]), z(p[2]) {}
Vec3(const long* p) : x(p[0]), y(p[1]), z(p[2]) {}
Vec3(const double* p) : x(p[0]), y(p[1]), z(p[2]) {}
Vec3(const float* p) : x(p[0]), y(p[1]), z(p[2]) {}
//----- 加減算 ---------------------------------------------------
Vec3& operator+=(const Vec3& a) {
x += a.x;
y += a.y;
z += a.z;
return *this;
}
Vec3& operator-=(const Vec3& a) {
x -= a.x;
y -= a.y;
z -= a.z;
return *this;
}
friend Vec3 operator+(const Vec3& a, const Vec3& b) {
return Vec3(a.x + b.x, a.y + b.y, a.z + b.z);
}
friend Vec3 operator-(const Vec3& a, const Vec3& b) {
return Vec3(a.x - b.x, a.y - b.y, a.z - b.z);
}
//----- 係数乗除算 -----------------------------------------------
Vec3& operator*=(double k) {
x *= k;
y *= k;
z *= k;
return *this;
}
Vec3& operator/=(double k) {
x /= k;
y /= k;
z /= k;
return *this;
}
friend Vec3 operator*(const Vec3& a, double k) {
return Vec3(a.x * k, a.y * k, a.z * k);
}
friend Vec3 operator*(double k, const Vec3& a) {
return Vec3(a.x * k, a.y * k, a.z * k);
}
friend Vec3 operator/(const Vec3& a, double k) {
return Vec3(a.x / k, a.y / k, a.z / k);
}
//----- 行列演算 -------------------------------------------------
Vec3& operator*=(const Mat3x3& m) {
// | m11 m12 m13 |
// (x, y, z) * | m21 m22 m23 |
// | m31 m32 m33 |
double xx = x, yy = y, zz = z;
x = xx * m.m11 + yy * m.m21 + zz * m.m31;
y = xx * m.m12 + yy * m.m22 + zz * m.m32;
z = xx * m.m13 + yy * m.m23 + zz * m.m33;
return *this;
}
friend Vec3 operator*(const Vec3& a, const Mat3x3& b) {
return Vec3(
a.x * b.m11 + a.y * b.m21 + a.z * b.m31,
a.x * b.m12 + a.y * b.m22 + a.z * b.m32,
a.x * b.m13 + a.y * b.m23 + a.z * b.m33
);
}
//----- 内積 -----------------------------------------------------
// 内積値 = |a|*|b|*cosθ = a.x * b.x + a.y * b.y + a.z * b.z
double inner(const Vec3& a) const {
return x * a.x + y * a.y + z * a.z;
}
//----- 平方和 ---------------------------------------------------
double square() const {
return x * x + y * y + z * z;
}
//----- 方向余弦化 -----------------------------------------------
void setDirectionCosines();
//----- 極座標の設定・取得 ---------------------------------------
// 動径、ベクトルの長さ
double radius() const;
// 極座標を得る
// @param phi 天頂角φ
// @param theta 方位角θ
// @return 動径r
double getPolar(Degree& phi, Degree& theta) const;
// 極座標を設定する
// @param radius 動径r
// @param phi 天頂角φ
// @param theta 方位角θ
void setPolar(double radius, const Degree& phi, const Degree& theta);
// 緯度経度を得る
// @param latitude 緯度
// @param longitude 経度(東経を正とする)
// @return 動径r
double getLtLg(Degree& latitude, Degree& longitude) const;
// 緯度経度を設定する
// @param radius 動径r
// @param latitude 緯度
// @param longitude 経度(東経を正とする)
void setLtLg(double radius, const Degree& latitude, const Degree& longitude);
//----- 直交座標値データメンバ -----------------------------------
// ※外部から直接変更されても困らないので、公開してしまう
double x, y, z;
};//endclass Vec3
}//endnamespace util
#endif // VEC3_H_