-
Notifications
You must be signed in to change notification settings - Fork 2
/
quaternion.rkt
78 lines (66 loc) · 2.29 KB
/
quaternion.rkt
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
#lang typed/racket
(require "quaternion-local.rkt"
math/flonum
"flvector3.rkt"
"matrix3.rkt")
(provide (all-defined-out)
(struct-out quaternion))
(: quaternion-identity (-> quaternion))
(define (quaternion-identity)
(quaternion 1.0 0.0 0.0 0.0))
(: quaternion-vector (quaternion -> FlVector))
(define (quaternion-vector q)
(flvector (i q)
(j q)
(k q)))
(: axis-angle->quaternion (FlVector Float -> quaternion))
(define (axis-angle->quaternion v a)
(fl->vector->quaternion
(flcos (fl* 0.5 a))
(flvector3-scale (flsin (fl* 0.5 a)) (flvector3-normal v))))
(: quaternion-length-square (quaternion -> Float))
(define (quaternion-length-square q)
(define square (λ ([a : Float])
(* a a)))
(quaternion-sum (quaternion-map square q)))
(: quaternion-length (quaternion -> Float))
(define (quaternion-length q)
(flsqrt (quaternion-length-square q)))
(: quaternion-inverse (quaternion -> quaternion))
(define (quaternion-inverse q)
(quaternion-scale (quaternion-conjugate q)
(/ (quaternion-length-square q))))
(: quaternion-normal (quaternion -> quaternion))
(define (quaternion-normal q)
(if (zero? (quaternion-length q))
q
(quaternion-scale q (/ (quaternion-length q)))))
(: quaternion-product (quaternion * -> quaternion))
(define (quaternion-product . quats)
(quaternion-normal
(foldl quaternion-single-product
(quaternion-identity) quats)))
(: quaternion-vector-product (quaternion FlVector -> FlVector))
(define (quaternion-vector-product q v)
(quaternion-vector
(quaternion-product q (vector->quaternion v) (quaternion-conjugate q))))
(: quaternion->matrix3 (quaternion -> FlVector))
(define (quaternion->matrix3 q)
(let* ([a (a q)]
[b (i q)]
[c (j q)]
[d (k q)]
[*2 (λ ([a : Float]
[b : Float])
(* 2.0 (* a b)))]
[*-2 (λ ([a : Float]
[b : Float])
(- (*2 a b)))])
(matrix3-sum
(matrix3-identity)
(flvector (*-2 c c) (*2 b c) (*2 b d)
(*2 b c) (*-2 b b) (*2 c d)
(*2 b d) (*2 c d) (*-2 b b))
(flvector (*-2 d d) (*-2 a d) (*2 a c)
(*2 a d) (*-2 d d) (*-2 a b)
(*-2 a c) (*2 a b) (*-2 c c)))))