-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlorentz_coord.hpp
332 lines (307 loc) · 8.02 KB
/
lorentz_coord.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
/**
* [Lorentz Coordinate System]
*
* The Lorentz Coordinate System is a coordinate system designed to describe space-time transformations and handle the time dimension.
* It is an extension of the complex coordinate system, where the rotation of the coordinate system can be represented by a quaternion composed of a vector and a complex angle.
* The Lorentz Coordinate System consists of three vectors, ux, uy, and uz, which represent the x, y, and z-axis directions respectively.
* Additionally, it includes a complex number power, which represents the power in the time dimension.
* The Lorentz Coordinate System allows for operations such as multiplication, division, cross product, transpose, reverse, flip, rotation, and conversion to Euler angles.
* These operations enable the transformation of vectors between different coordinate systems and the calculation of relativistic effects.
* The design philosophy of the Lorentz Coordinate System is to provide a flexible and intuitive way to handle space-time transformations and relativistic effects.
* By incorporating complex numbers and quaternions, it allows for a more comprehensive representation of rotations and transformations in 3D space.
*/
struct lorentz_coord
{
static const lorentz_coord ZERO;
static const lorentz_coord ONE;
vec3 ux = vec3::UX; // x-axis direction
vec3 uy = vec3::UY; // y-axis direction
vec3 uz = vec3::UZ; // z-axis direction
complex power; // power in the time dimension
lorentz_coord() {}
/**
* Constructor
* @param c - lorentz_coord object
*/
lorentz_coord(const lorentz_coord& c)
{
ux = c.ux; uy = c.uy; uz = c.uz;
power = c.power;
}
/**
* Constructor
* @param _ux - x-axis direction
* @param _uy - y-axis direction
* @param _uz - z-axis direction
* @param _power - power in the time dimension
*/
lorentz_coord(const vec3& _ux, const vec3& _uy, const vec3& _uz, const complex& _power)
{
ux = _ux; uy = _uy; uz = _uz;
power = _power;
}
/**
* Constructor
* @param _ux - x-axis direction
* @param _uy - y-axis direction
* @param _power - power in the time dimension
*/
lorentz_coord(const vec3& _ux, const vec3& _uy, const complex& _power)
{
ux = _ux; uy = _uy; uz = ux.cross(uy);
power = _power;
}
/**
* Constructor
* @param ang - angle
* @param ax - axis
* @param _power - power in the time dimension
*/
lorentz_coord(real ang, const vec3& ax, const complex& _power)
{
ux.rot(ang, ax);
uy.rot(ang, ax);
uz.rot(ang, ax);
power = _power;
}
/**
* Constructor
* @param pit - pitch
* @param yaw - yaw
* @param rol - roll
* @param _power - power in the time dimension
*/
lorentz_coord(real pit, real yaw, real rol, const complex& _power)
{
ux.rot(pit, vec3::UX);
uy.rot(yaw, vec3::UY);
uz.rot(rol, vec3::UZ);
power = _power;
}
/**
* Constructor
* @param q - quaternion
* @param _power - power in the time dimension
*/
lorentz_coord(const quaternion& q, const complex& _power)
{
ux = q * vec3::UX;
uy = q * vec3::UY;
uz = q * vec3::UZ;
power = _power;
}
/**
* Multiplication: define a vector in the coordinate system or restore a vector to the parent space
* @param p - vector
* @param c - lorentz_coord object
* @return result of the multiplication
*/
friend vec3 operator * (const vec3& p, const lorentz_coord& c)
{
return (c.ux * p.x + c.uy * p.y + c.uz * p.z) * cos(c.power);
}
friend vec4 operator * (const vec4& p, const lorentz_coord& c)
{
return vec4((c.ux * p.x + c.uy * p.y + c.uz * p.z) * cos(c.power), p.w + c.power);
}
/**
* Multiplication: Cchild * Cparent * ...
* @param c - lorentz_coord object
* @return result of the multiplication
*/
lorentz_coord operator * (const lorentz_coord& c) const
{
lorentz_coord rc;
rc.ux = ux.x * c.ux + ux.y * c.uy + ux.z * c.uz;
rc.uy = uy.x * c.ux + uy.y * c.uy + uy.z * c.uz;
rc.uz = uz.x * c.ux + uz.y * c.uy + uz.z * c.uz;
rc.power = power + c.power;
return rc;
}
/**
* Multiplication: q * C
* @param q - quaternion
* @param c - lorentz_coord object
* @return result of the multiplication
*/
friend quaternion operator * (const quaternion& q, const lorentz_coord& c)
{
return q * c.to_quaternion();
}
/**
* Multiplication: C * q
* @param q - quaternion
* @return result of the multiplication
*/
lorentz_coord operator * (const quaternion& q) const
{
lorentz_coord rc;
rc.ux = q * ux;
rc.uy = q * uy;
rc.uz = q * uz;
rc.power = power + q.angle();
return rc;
}
/**
* Division: vector projection onto the coordinate system
* @param v - vector
* @param c - lorentz_coord object
* @return result of the division
*/
friend vec3 operator / (const vec3& v, const lorentz_coord& c)
{
return vec3(v.dot(c.ux), v.dot(c.uy), v.dot(c.uz)) * cos(-c.power);
}
/**
* Division: C1 * C2^-1
* @param c - lorentz_coord object
* @return result of the division
*/
lorentz_coord operator / (const lorentz_coord& c) const
{
lorentz_coord rc;
rc.ux = vec3(ux.dot(c.ux), ux.dot(c.uy), ux.dot(c.uz));
rc.uy = vec3(uy.dot(c.ux), uy.dot(c.uy), uy.dot(c.uz));
rc.uz = vec3(uz.dot(c.ux), uz.dot(c.uy), uz.dot(c.uz));
rc.power = power - c.power;
return rc;
}
/**
* Division: q / C
* @param q - quaternion
* @param c - lorentz_coord object
* @return result of the division
*/
friend quaternion operator / (const quaternion& q, const lorentz_coord& c)
{
return q * c.to_quaternion().conjugate();
}
/**
* Division: C / q
* @param q - quaternion
* @return result of the division
*/
lorentz_coord operator / (const quaternion& q) const
{
return (*this) * q.conjugate();
}
/**
* Cross product: C1 x C2
* @param c - lorentz_coord object
* @return result of the cross product
*/
lorentz_coord cross(const lorentz_coord& c) const
{
return lorentz_coord(
ux.cross(c.uz) - ux.cross(c.uy),
uy.cross(c.ux) - uy.cross(c.uz),
uz.cross(c.uy) - uz.cross(c.ux),
power + c.power
);
}
/**
* Cross product: C x v
* @param v - vector
* @return result of the cross product
*/
lorentz_coord cross(const vec3& v) const
{
return lorentz_coord(
ux.cross(v),
uy.cross(v),
uz.cross(v),
power
);
}
/**
* Reverse
*/
void reverse()
{
(*this) = ONE / (*this);
}
/**
* Reverse
* @return reversed lorentz_coord object
*/
lorentz_coord reversed() const
{
return ONE / (*this);
}
/**
* Rotation
* @param ang - angle
* @param ax - axis
*/
void rotate(real ang, const vec3& ax)
{
ux.rotate(ang, ax);
uy.rotate(ang, ax);
uz.rotate(ang, ax);
}
/**
* Convert coordinate system to Euler angles
* @return Euler angles
*/
vec3 coord_to_eulers() const
{
const lorentz_coord& rm = *this;
float sy = sqrt(rm.ux.x * rm.ux.x + rm.uy.x * rm.uy.x);
bool singular = sy < 1e-6;
float x, y, z;
if (!singular)
{
x = atan2(rm.uz.y, rm.uz.z);
y = atan2(-rm.uz.x, sy);
z = atan2(rm.uy.x, rm.ux.x);
}
else
{
x = atan2(-rm.uy.z, rm.uy.y);
y = atan2(-rm.uz.x, sy);
z = 0;
}
return vec3(x, y, z);
}
/**
* Dot product with a vector
* @param v - vector
* @return dot product
*/
real dot(const vec3& v) const
{
return v.dot(ux) + v.dot(uy) + v.dot(uz);
}
/**
* Dot product with another coordinate system
* @param c - lorentz_coord object
* @return dot product
*/
real dot(const lorentz_coord& c) const
{
return c.ux.dot(ux) + c.uy.dot(uy) + c.uz.dot(uz);
}
/**
* Gradient coordinate system = Gradient X Tangent space
* @param c1 - initial lorentz_coord object
* @param c2 - transformed lorentz_coord object
* @return gradient lorentz_coord object
*/
lorentz_coord gradient(const lorentz_coord& c1, const lorentz_coord& c2)
{
return c1.reversed() * c2;
}
/**
* Convert to quaternion
* @return quaternion
*/
quaternion to_quaternion() const
{
vec3 pyr = coord_to_eulers();
quaternion q;
q.from_euler(pyr.x, pyr.y, pyr.z);
return q;
}
};
const lorentz_coord lorentz_coord::ZERO = lorentz_coord(vec3::ZERO, vec3::ZERO, vec3::ZERO, complex::ZERO);
const lorentz_coord lorentz_coord::ONE = lorentz_coord(vec3::UX, vec3::UY, vec3::UZ, complex::ONE);