-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
rotation.cljc
158 lines (131 loc) · 4.16 KB
/
rotation.cljc
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
;
; Copyright © 2017 Colin Smith.
; This work is based on the Scmutils system of MIT/GNU Scheme:
; Copyright © 2002 Massachusetts Institute of Technology
;
; This is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 3 of the License, or (at
; your option) any later version.
;
; This software is distributed in the hope that it will be useful, but
; WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
; General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this code; if not, see <http://www.gnu.org/licenses/>.
;
(ns sicmutils.mechanics.rotation
(:refer-clojure :exclude [+ - * /])
(:require [sicmutils.generic :as g :refer [cos sin + - * /]]
[sicmutils.matrix :as matrix]
[sicmutils.structure :as s :refer [up down]]))
(defn- rotate-x-matrix-2 [c s]
(matrix/by-rows [1 0 0]
[0 c (- s)]
[0 s c]))
(defn rotate-x-matrix
"Produce the matrix of a rotation of α radians about the x axis."
[α]
(rotate-x-matrix-2 (cos α) (sin α)))
(def Rx-matrix rotate-x-matrix)
(defn- rotate-y-matrix-2 [c s]
(matrix/by-rows [c 0 s]
[0 1 0]
[(- s) 0 c]))
(defn rotate-y-matrix
"Produce the matrix of a rotation of α radians about the y axis."
[α]
(rotate-y-matrix-2 (cos α) (sin α)))
(def Ry-matrix rotate-y-matrix)
(defn- rotate-z-matrix-2
"Produce the matrix of a rotation of α radians about the z axis."
[c s]
(matrix/by-rows [c (- s) 0]
[s c 0]
[0 0 1]))
(defn rotate-z-matrix
"Produce the matrix of a rotation of α radians about the z axis."
[α]
(rotate-z-matrix-2 (cos α) (sin α)))
(def Rz-matrix rotate-z-matrix)
(defn angle-axis->rotation-matrix [theta [x y z]]
(let [colatitude (g/acos z)
longitude (g/atan y x)]
(* (rotate-z-matrix longitude)
(rotate-y-matrix colatitude)
(rotate-z-matrix theta)
(matrix/transpose (rotate-y-matrix colatitude))
(matrix/transpose (rotate-z-matrix longitude)))))
;; ## Rotation Tuples
(defn ^:no-doc rotate-x-tuple-2 [c s]
(matrix/m->s
(s/literal-down 'l 3)
(rotate-x-matrix-2 c s)
(s/literal-up 'r 3)))
(defn rotate-x-tuple [α]
(rotate-x-tuple-2 (cos α)
(sin α)))
(defn ^:no-doc rotate-y-tuple-2 [c s]
(matrix/m->s
(s/literal-down 'l 3)
(rotate-y-matrix-2 c s)
(s/literal-up 'r 3)))
(defn rotate-y-tuple [α]
(rotate-y-tuple-2 (cos α)
(sin α)))
(defn ^:no-doc rotate-z-tuple-2 [c s]
(matrix/m->s
(s/literal-down 'l 3)
(rotate-z-matrix-2 c s)
(s/literal-up 'r 3)))
(defn rotate-z-tuple [α]
(rotate-z-tuple-2 (cos α)
(sin α)))
;; ## Rotation procedures
;; XXX: R[xyz] should not return an up; they should return a struct
;; of the same shape they were given. But do rotations of covectors
;; work that way? Maybe we should assert up-ness here rather than
;; promise to be more general than we are.
(defn Rx
"Returns a function which rotates a vector α radians about the x axis."
[α]
(fn [[x y z]]
(let [c (cos α)
s (sin α)]
(up x
(- (* c y) (* s z))
(+ (* s y) (* c z))))))
(defn Ry
"Returns a function which rotates a vector α radians about the y axis."
[α]
(fn [[x y z]]
(let [c (cos α)
s (sin α)]
(up (+ (* c x) (* s z))
y
(- (* c z) (* s x))))))
(defn Rz
"Returns a function which rotates a vector α radians about the z axis."
[α]
(fn [[x y z]]
(let [c (cos α)
s (sin α)]
(up (- (* c x) (* s y))
(+ (* s x) (* c y))
z))))
;; Aliases to match scmutils.
(def rotate-x Rx)
(def rotate-y Ry)
(def rotate-z Rz)
(defn Euler->M
"Compute the rotation matrix from a set of Euler angles."
[[θ φ ψ]]
(* (rotate-z-matrix φ)
(rotate-x-matrix θ)
(rotate-z-matrix ψ)))
(defn wcross->w [A]
(up (get-in A [1 2])
(get-in A [2 0])
(get-in A [0 1])))