-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcoord2.hpp
429 lines (416 loc) · 9.77 KB
/
coord2.hpp
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/************************************************************************************************
* [Coordinate System]
* by Guojun Pan
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* The coordinate system class is separately encapsulated by me for
* simplifying coordinate transformation and deriving many algorithms,
* which can solve some problems related to coordinate system transformation.
* The operation of the coordinate system is similar to Lie group.
* The coordinate system consists of three parts: C = M (position) + S (scaling) * R (rotation).
*
* * * * * * * * * * * * Detailed Explanation * * * * * * * * * * * * * *
* The coordinate system transformation is divided into three steps:
* projection (/), translation (^), and restoration (*).
*
* The symbol of the coordinate system itself is C. The transformation between coordinate systems
* can be written as G = C2 / C1 - I, where G means gradient.
* oper(/) = C1 * C2^-1
* oper(\) = C1^-1 * C2
*
* Specifically:
* Define an intrinsic coordinate system (assuming it is a flat space, and the vector can move freely
* without changing) under V. Observing V in a curved coordinate system, V is different at different points.
* Therefore, the coordinate system is related to the position.
* Take vectors V1 and V2 at adjacent points (1) and (2) respectively,
* corresponding to coordinate systems C1 and C2. Then:
* V = V1 * C1 = V2 * C2 =>
* V2 = V1 * C1 / C2, let R12 = C1 / C2 =>
* V2 = V1 * R12
*
* The coordinate system can be used to calculate spatial curvature. In the u,v coordinate system,
* the Riemann curvature tensor is:
* Ruv = Gu*Gv - Gv*Gu - G[u,v]
* where: Gu = C2 / C1 - I
* Connection vector: W = [U, V] (Lie bracket operation)
* G[u,v] = Gu*Wu + Gv*Wv
*/
//#define NON_UNIFORM_SCALE // 非均匀缩放
// *******************************************************************
// |_
// UC 2d Rotation Coordinate System
// *******************************************************************
struct ucoord2
{
static const ucoord2 ZERO;
static const ucoord2 ONE;
vec2 ux = vec2::UX; // basis 单位化基向量
vec2 uy = vec2::UY;
ucoord2() {}
ucoord2(crvec2 _ux, crvec2 _uy)
{
ux = _ux;
uy = _uy;
}
ucoord2(crvec2 _ux)
{
ux = _ux;
uy = ux.rotcopy(PI / 2);
}
ucoord2(real ang)
{
ux.rot(ang);
uy.rot(ang);
}
bool is_same_dirs(const ucoord2& c) const
{
return ux == c.ux && uy == c.uy;
}
// 在坐标系下定义一个向量
friend vec2 operator * (crvec2 p, const ucoord2& c)
{
return c.ux * (p.x) + c.uy * (p.y);
}
ucoord2 operator * (const ucoord2& c) const
{
ucoord2 rc;
rc.ux = ux.x * c.ux + ux.y * c.uy;
rc.uy = uy.x * c.ux + uy.y * c.uy;
return rc;
}
// 向量向坐标系投影
friend vec2 operator / (crvec2 v, const ucoord2& c)
{
return vec2(v.dot(c.ux), v.dot(c.uy));
}
// oper(/) = C1 * C2^-1
ucoord2 operator / (const ucoord2& c) const
{
ucoord2 rc;
rc.ux = vec2(ux.dot(c.ux), ux.dot(c.uy));
rc.uy = vec2(uy.dot(c.ux), uy.dot(c.uy));
return rc;
}
// oper(//) = C1^-1 * C2
ucoord2 operator % (const ucoord2& c) const
{
return (*this).reversed() * c;
}
// 倒置
void reverse()
{
(*this) = ONE / (*this);
}
ucoord2 reversed() const
{
return ONE / (*this);
}
// 梯度坐标系
static ucoord2 grad(const ucoord2& c1, const ucoord2& c2)
{
return c1.reversed() * c2;
}
real dot(crvec2 v) const
{
return v.dot(ux) + v.dot(uy);
}
// 角度
real angle() const
{
return ux.angle();
}
// 旋转
void rot(real angle)
{
vec2 z = vector2::ang_len(angle, 1);
ux = complex_mul(ux, z);
uy = complex_mul(uy, z);
}
ucoord2 rotcopy(real angle) const
{
ucoord2 c = (*this);
vec2 z = vector2::ang_len(angle, 1);
c.ux = complex_mul(c.ux, z);
c.uy = complex_mul(c.uy, z);
return c;
}
void rot2dir(crvec2 _dir)
{
ux = _dir; uy = _dir.rotcopy(PI / 2);
}
void dump(const std::string& name = "") const
{
PRINT("----" << name << "---");
PRINTVEC2(ux);
PRINTVEC2(uy);
}
};
#if defined(PMDLL) || !defined(PM_IMPLEMENTED)
const ucoord2 ucoord2::ZERO = { 0 };
const ucoord2 ucoord2::ONE = ucoord2();
#endif
// *******************************************************************
// |_
// C 2d Coordinate System
// *******************************************************************
struct coord2 : ucoord2
{
static const coord2 ZERO;
static const coord2 ONE;
vec2 s = vec2::ONE; // 缩放
vec2 o; // 原点
coord2() {}
coord2(const ucoord2& c) : ucoord2(c)
{
}
coord2(const ucoord2& c, crvec2 _s) : ucoord2(c)
{
s = _s;
}
coord2(const coord2& c) : ucoord2(c.ux, c.uy)
{
s = c.s;
o = c.o;
}
coord2(crvec2 _ux, crvec2 _uy) : ucoord2(_ux, _uy)
{
}
coord2(crvec2 p)
{
o = p;
}
coord2(real ang)
{
vec2 z = vector2::ang_len(ang, 1);
ux = complex_mul(ux, z);
uy = complex_mul(uy, z);
}
coord2(real ang, real _r)
{
vec2 z = vector2::ang_len(ang, 1);
ux = complex_mul(ux, z);
uy = complex_mul(uy, z);
s *= _r;
}
coord2(crvec2 p, real ang)
{
o = p;
vec2 z = vector2::ang_len(ang, 1);
ux = complex_mul(ux, z);
uy = complex_mul(uy, z);
}
coord2(crvec2 p, crvec2 _s, real ang)
{
o = p;
s = _s;
vec2 z = vector2::ang_len(ang, 1);
ux = complex_mul(ux, z);
uy = complex_mul(uy, z);
}
operator vec2 () const
{
return o;
}
vec2 VX() const { return ux * s.x; }
vec2 VY() const { return uy * s.y; }
coord2 VC() const
{
return { VX(), VY() };
}
ucoord2 UC() const
{
return {ux, uy};
}
void UC(const ucoord2& uc)
{
ux = uc.ux;
uy = uc.uy;
}
bool equal_dirs(const coord2& c) const
{
return ux == c.ux && uy == c.uy && o == c.o && s == c.s;
}
bool operator == (const coord2& c) const
{
return o == c.o && s == c.s && equal_dirs(c);
}
coord2 operator + (const coord2& c) const
{
coord2 rc;
rc.ux = VX() + c.VX();
rc.uy = VY() + c.VY();
rc.norm();
rc.o = o + c.o;
return rc;
}
void operator += (const coord2& c)
{
*this = *this + c;
}
coord2 operator + (const vec2& v) const
{
coord2 c = (*this);
c.o += v;
return c;
}
void operator += (const vec2& v)
{
*this = *this + v;
}
coord2 operator - (const coord2& c) const
{
coord2 rc;
rc.ux = VX() - c.VX();
rc.uy = VY() - c.VY();
rc.norm();
rc.o = o - c.o;
return rc;
}
void operator -= (const coord2& c)
{
*this = *this - c;
}
coord2 operator - (const vec2& v) const
{
coord2 c = (*this);
c.o -= v;
return c;
}
void operator -= (const vec2& v)
{
*this = *this - v;
}
// 在坐标系下定义一个向量
friend vec2 operator * (crvec2 p, const coord2& c)
{
return c.ux * (c.s.x * p.x) + c.uy * (c.s.y * p.y) + c.o;
}
coord2 operator * (crvec2 v) const
{
return (*this) * coord2(vec2::UX * v.x, vec2::UY * v.y);
}
void operator *= (crvec2 v)
{
(*this) *= coord2(vec2::UX * v.x, vec2::UY * v.y);
}
coord2 operator * (const coord2& c) const
{
coord2 rc;
#ifdef NON_UNIFORM_SCALE
rc.ux = (ux.x * s.x) * (c.ux * c.s.x) + (ux.y * s.x) * (c.uy * c.s.y);
rc.uy = (uy.x * s.y) * (c.ux * c.s.x) + (uy.y * s.y) * (c.uy * c.s.y);
rc.norm();
#else
rc = ucoord2::operator*(c);
rc.s = s * c.s;
#endif
rc.o = c.o + o.x * c.s.x * c.ux + o.y * c.s.y * c.uy;
return rc;
}
void operator *= (const coord2& c)
{
(*this) = (*this) * c;
}
coord2 operator * (real s) const
{
coord2 c = *this;
//{// C*S 缩放乘法
// c.s.x *= s; c.s.y *= s;
//}
{// C*S 移动乘法
c.o *= s;
}
return c;
}
void operator *= (real s)
{
*this = (*this) * s;
}
// 向量向坐标系投影
friend vec2 operator / (crvec2 p, const coord2& c)
{
vec2 v = p - c.o;
v /= c.s;
return vec2(v.dot(c.ux), v.dot(c.uy));
}
// oper(/) = C1 * C2^-1
coord2 operator / (const coord2& c) const
{
coord2 rc;
#ifdef NON_UNIFORM_SCALE
vec2 vx = VX();
vec2 vy = VY();
vec2 cvx = c.ux / c.s.x;
vec2 cvy = c.uy / c.s.y;
rc.ux = vec2(vx.dot(cvx), vx.dot(cvy));
rc.uy = vec2(vy.dot(cvx), vy.dot(cvy));
rc.norm();
#else
rc = ucoord2::operator/(c);
rc.s = s / c.s;
#endif
rc.o = o - c.o;
rc.o = vec2(rc.o.dot(c.ux) / c.s.x, rc.o.dot(c.uy) / c.s.y);
return rc;
}
coord2 operator / (crvec2 v) const
{
return (*this) / coord2(ux * v.x, uy * v.y);
}
// oper(//) = C1^-1 * C2
coord2 operator % (const coord2& c) const
{
return (*this).reversed() * c;
}
coord2 operator ^ (real f) const
{
real ang = ux.angle() * f;
real rad = ::exp(::log(ux.len()) * f);
return coord2(ang, rad);
}
void norm(bool bscl = true)
{
s.x = ux.len(); if (!ISZERO(s.x)) ux /= s.x;
s.y = uy.len(); if (!ISZERO(s.y)) uy /= s.y;
if (!bscl)
s = vec2::ONE;
}
// 倒置
void reverse()
{
(*this) = ONE / (*this);
}
coord2 reversed() const
{
return ONE / (*this);
}
real dot(crvec2 v) const
{
return v.dot(ux) * s.x + v.dot(uy) * s.y;
}
// 梯度
static coord2 grad(const coord2& c1, const coord2& c2)
{
return c1.reversed() * c2 - ONE;
}
// 位置
vec2 pos() const
{
return o;
}
string serialise() const
{
return o.serialise() + "," + std::to_string(angle());
}
void dump(const string& name = "") const
{
PRINT("----" << name << "---");
PRINTVEC2(ux);
PRINTVEC2(uy);
PRINTVEC2(s);
PRINTVEC2(o);
}
};
#ifndef(PM_IMPLEMENTED)
const coord2 coord2::ZERO = { ucoord2::ZERO, vec2::ZERO };
const coord2 coord2::ONE = coord2();
#endif