Skip to content

Commit

Permalink
Rename IThreeQuanternion to IQuanternion
Browse files Browse the repository at this point in the history
  • Loading branch information
itarck committed Oct 9, 2021
1 parent 4374385 commit 9917989
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/mathree/quaternion.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[mathree.arithmetic :as arith]))


(defprotocol IThreeQuanternion
(defprotocol IQuanternion
(almost-equals [q1 q2])
(angle-to [q1 q2] "Returns the angle between this quaternion and quaternion q in radians.")
(clone' [q] "Creates a new Quaternion with identical x, y, z and w properties to this one.")
Expand Down Expand Up @@ -51,7 +51,7 @@
:w (.-w v)
not-found)))

IThreeQuanternion
IQuanternion

(almost-equals [q1 q2]
(every? (fn [n] (arith/almost-equal? n 0.0)) (map - (seq q1) (seq q2))))
Expand Down
70 changes: 44 additions & 26 deletions test/mathree/test_quaternion.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns mathree.test-quaternion
(:require
[cljs.test :refer-macros [deftest is testing run-tests]]
[mathree.quaternion :as tq :refer [quaternion quatn]]
[mathree.quaternion :as q :refer [quaternion quatn]]
[mathree.vector3 :as v3]
[mathree.euler :as e]
[mathree.arithmetic :as arith]
Expand All @@ -10,72 +10,90 @@

(def q1 (quaternion 1 2 3 5))
(def q2 (quatn [1 2 3 5]))
(def q3 (tq/identity-quaternion))
(def q3 (q/identity-quaternion))
(def q4 (quaternion 5 4 3 2))

(def nq1 (tq/normalize q1))
(def nq4 (tq/normalize q4))
(def nq1 (q/normalize q1))
(def nq4 (q/normalize q4))

(def nv3 (v3/normalize (v3/vector3 1 2 3)))

(def e1 (e/euler 1 2 3))

(def q5 (tq/from-euler e1))
(def q5 (q/from-euler e1))

(def q6 (tq/from-euler (e/euler 0 0 (/ Math/PI -8))))
(def q6 (q/from-euler (e/euler 0 0 (/ Math/PI -8))))


(deftest test-quatn
(let [oq1 (tq/clone' q1)
oq2 (tq/clone' q2)]
(let [oq1 (q/clone' q1)
oq2 (q/clone' q2)]

(is (tq/equals q1 q2))
(is (tq/equals (tq/conjugate q1) (tq/quaternion -1 -2 -3 5)))
(is (q/equals q1 q2))
(is (q/equals (q/conjugate q1) (q/quaternion -1 -2 -3 5)))

(is (= 1 (tq/length (tq/normalize q1))))
(is (= 1 (q/length (q/normalize q1))))

(is (tq/equals q3 (tq/multiply (tq/invert nq1) nq1)))
(is (tq/equals (tq/multiply q1 (quaternion 1 0 0 0)) (quaternion 5 3 -2 -1)))
(is (q/equals q3 (q/multiply (q/invert nq1) nq1)))
(is (q/equals (q/multiply q1 (quaternion 1 0 0 0)) (quaternion 5 3 -2 -1)))


(is (tq/equals (tq/slerp nq1 nq4 0) nq1))
(is (tq/equals (tq/slerp nq1 nq4 1) nq4))
(is (q/equals (q/slerp nq1 nq4 0) nq1))
(is (q/equals (q/slerp nq1 nq4 1) nq4))

(is (arith/almost-all-equal?
(seq (tq/from-unit-vectors (v3/vector3 1 0 0) (v3/vector3 0 1 0)))
(seq (tq/from-axis-angle (v3/vector3 0 0 1) (/ Math/PI 2)))))
(seq (q/from-unit-vectors (v3/vector3 1 0 0) (v3/vector3 0 1 0)))
(seq (q/from-axis-angle (v3/vector3 0 0 1) (/ Math/PI 2)))))

(is (tq/almost-equals q6
(tq/from-rotation-matrix
(is (q/almost-equals q6
(q/from-rotation-matrix
(m4/make-rotation-from-quaternion q6))))
;;
(is (tq/equals q1 oq1))
(is (tq/equals q2 oq2))))
(is (q/equals q1 oq1))
(is (q/equals q2 oq2))))


(run-tests)


(comment

(def q1 (tq/from-axis-angle (v3/vector3 1 0 0) (/ Math/PI 2)))
(def q1 (q/from-axis-angle (v3/vector3 1 0 0) (/ Math/PI 2)))
q1
;; => #object[Quaternion [0.7071067811865475 0 0 0.7071067811865476]]

(v3/apply-quaternion (v3/vector3 0 0 1) q1)

(def q2 (tq/from-axis-angle (v3/vector3 0 0 1) (/ Math/PI 2)))
(def q2 (q/from-axis-angle (v3/vector3 0 0 1) (/ Math/PI 2)))

(vec q1)
;; => [0.7071067811865475 0 0 0.7071067811865476]

(:x q1)
;; => 0.7071067811865475
(:y q1)
;; => 0
(second q1)
;; => 0


(->
(v3/vector3 0 0 1)
(v3/apply-quaternion q1)
(v3/apply-quaternion q2))
;; => #object[Vector3 [1 -2.220446049250313e-16 2.220446049250313e-16]]



(-> (v3/vector3 0 0 1)
(v3/apply-quaternion (tq/multiply q2 q1)))
(v3/apply-quaternion (q/multiply q2 q1)))
;; => #object[Vector3 [1 -2.220446049250313e-16 2.220446049250313e-16]]


(-> (v3/vector3 0 0 1)
(v3/apply-quaternion (tq/multiply q1 q2)))
(v3/apply-quaternion (q/multiply q1 q2)))
;; => #object[Vector3 [0 -1 2.220446049250313e-16]]



;;
)

0 comments on commit 9917989

Please sign in to comment.