rev: 21c5e6d2f665d0e8b4ff917a51b664c895dab2ed tukan/tukan/rotation.sc -rw-r--r-- 8.0 KiB
21c5e6d2f665 — Leonard Ritter * renamed project from Liminal to Tukan 2 years ago
`                                                                                `
```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```
```using import glm

let pi = 3.141592653589793

#-------------------------------------------------------------------------------

x * (pi / 180.0)

fn degrees (x)
x * (180.0 / pi)

#-------------------------------------------------------------------------------

# anglevectors are identical with the first column of a 2x2 rotation matrix
the second column can be obtained by taking the orthogonal of the first
column
therefore the full matrix is
v.x -v.y
v.y  v.x
# vec2 <- f32
fn anglevector (a)
vec2
cos a
sin a

# obtain the angle described by a vector, regardless of scaling (same as anglevectors)
# f32 <- vec2
fn vectorangle (v)
atan2 v.y v.x

# multiply a point or anglevector by another anglevector and return the
rotated point / anglevector
this is the same operation as multiplying two rotation matrices with each other
and returning the first column.
when multiplying anglevectors, angles add up, while scales multiply
normalizing the result is equivalent to setting the scaling factor to 1,
and maintains the angle
# vec2 <- (vec2 vec2)
fn anglevector-rotate (q v)
vec2
v.x * q.x - v.y * q.y
v.y * q.x + v.x * q.y

#-------------------------------------------------------------------------------

# versors: unit quaternions

# versor from axis / angle
fn... versor
(w : vec3, a : f32)
let a = (a * 0.5)
vec4
w * (sin a)
cos a
(aa : vec4)
versor aa.xyz aa.w

# vec4 <- (vec3)
fn versor-lookat (n)
let v =
vec3 0 0 1
versor
normalize
cross v n
acos
dot v n

# rotation matrix constructor
# mat3 <- vec4
fn rotation (q)
let n = (dot q q)
let qs =
? (n == 0.0)
vec4 0.0
q * (2.0 / n)
let w = (* qs.w q.xyz)
let x = (* qs.x q.xyz)
let y = (* qs.y q.xyz)
let zz = (* qs.z q.z)
mat3
\ (1.0 - (y.y + zz)) (x.y + w.z)        (x.z - w.y)
\ (x.y - w.z)        (1.0 - (x.x + zz)) (y.z + w.x)
\ (x.z + w.y)        (y.z - w.x)        (1.0 - (x.x + y.y))

# axis / angle from versor
# vec4 <- vec4
fn axisangle (q)
vec4
q.xyz / (sqrt (1.0 - q.w * q.w))
2.0 * (acos q.w)

# vec4 <- vec4
fn versor-conjugate (q)
q * (vec4 -1.0 -1.0 -1.0 1.0)

fn... versor-rotate
# rotate a versor by another
(qA : vec4, qB : vec4)
vec4
+ (cross qA.xyz qB.xyz)
qB.xyz * qA.w
qA.xyz * qB.w
(qA.w * qB.w) - (dot qA.xyz qB.xyz)
# rotate a point by a versor
q (t) * V * q (t) ^-1
(q : vec4, v : vec3)
let t =
(cross q.xyz v) * 2.0
v + q.w * t + (cross q.xyz t)

#-------------------------------------------------------------------------------

# using rodrigues' rotation formula to transform a vector by axis/angle
https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula
v cos a + (k x v) sin a + k (k . v) (1 - cos a)
OR: v + (1 - cos a) k x (k x v) + sin a k x v
fn... axisangle-rotate
# takes anglevector instead of angle
(k : vec3, a : vec2, v : vec3)
+ (v * a.x)
a.y * (cross k v)
k * ((dot k v) * (1.0 - a.x))
(k : vec3, a : f32, v : vec3)
axisangle-rotate k (anglevector a) v

#-------------------------------------------------------------------------------

fn... rotation-bounds
# compute the half-length of a bounding square that tightly wraps a
unit square rotated by the given (unskewed) anglevector
(q : vec2)
(abs q.x) + (abs q.y)
# compute the half-length of a bounding cube that tightly wraps a
unit cube rotated by the given rotation matrix
(m : mat3)
max
unpack
+
abs (m @ 0)
abs (m @ 1)
abs (m @ 2)
# compute the half-length of a bounding cube that tightly wraps a
unit cube rotated by the given versor (unit quaternion)
(q : vec4)
let w = (* q.w q.xyz)
let x = (* q.x q.xyz)
let y = (* q.y q.xyz)
let zz = (* q.z q.z)
* 2.0
max
+ (abs (0.5 - (y.y + zz))) (abs (x.y - w.z)) (abs (x.z + w.y))
+ (abs (x.y + w.z)) (abs (0.5 - (x.x + zz))) (abs (y.z - w.x))
+ (abs (x.z - w.y)) (abs (y.z + w.x)) (abs (0.5 - (x.x + y.y)))

#-------------------------------------------------------------------------------

# fixpoint angle arithmetic
advantage of this type is that a - b
always yields the shortest difference
between two angles

let Angle = i32

# f32 <- Angle
fn angletof (x)
* pi (ldexp x -31)

# Angle <- f32
fn ftoangle (x)
u32
ldexp (x / pi) 31

#-------------------------------------------------------------------------------

#if main-module?
var testgui (require "liminal.testgui")
using "liminal.oui"

import-from "LiminalCore" nvgStroke nvgStrokeColor nvgBeginPath nvgRGBf nvgCircle
\ nvgStrokeWidth nvgRestore nvgSave nvgTranslate nvgRGBAf nvgRotate
\ nvgFill nvgFillColor nvgMoveTo nvgLineTo NVGcolor nvgClosePath nvgScale
\ nvgCurrentTransform

testgui.main
function ()
static angle1 : float 0
static angle2 : float 0
do
static draw-origin (v c) :
void <- (vec2 vec3)
nvgBeginPath vg
nvgStrokeColor vg
nvgRGBf c.r c.g c.b
nvgBeginPath vg
nvgCircle vg v.x v.y 0.01
nvgStroke vg

static draw-anglevector (v c) :
void <- (vec2 vec3)
nvgBeginPath vg
nvgStrokeColor vg
nvgRGBf c.r c.g c.b
nvgBeginPath vg
nvgMoveTo vg 0 0
nvgLineTo vg v.x v.y
nvgStroke vg

static on-frame (s size) :
void <- (float ivec2)

var v1 : vec2
var v2 : vec2
var v3 : vec2
var p1 : vec2
var p2 : vec2

p1 =
vec2 -0.25 0.75
v1 =
anglevector angle1
v2 =
anglevector angle2
v3 =
anglevector-rotate v1 v2
p2 =
anglevector-rotate v3 p1

var s
(min size.x size.y) * 0.5
nvgSave vg
nvgTranslate vg
size.x * 0.5
size.y * 0.5
nvgScale vg s -s
nvgRotate vg 0
nvgTranslate vg 0 0

nvgStrokeWidth vg ((float 1) / s)
nvgBeginPath vg
nvgStrokeColor vg
nvgRGBf 0.25 0.25 0.25
nvgBeginPath vg
nvgCircle vg 0 0 1
nvgStroke vg

draw-anglevector v1
vec3 1 0 0
draw-anglevector v2
vec3 0 1 0
draw-anglevector v3
vec3 1 1 0
draw-origin p1
vec3 1 1 1
draw-origin p2
vec3 0 0 1

nvgRestore vg

static on-ui (s size) :
void <- (float ivec2)

slider "Angle 1" &angle1 -math.pi math.pi
slider "Angle 2" &angle2 -math.pi math.pi
newline ;
labelboxf "vectorangle: %f\n"
vectorangle
anglevector angle1

(tuple)

(locals)
module-path
.= (\$)
sidepanel true

locals;
```