@@ -7,20 +7,71 @@ package md3
77import (
88 "fmt"
99 "math"
10+ "math/rand"
1011 "testing"
12+
13+ "github.com/soypat/geometry/internal"
1114)
1215
1316func TestRotation (t * testing.T ) {
1417 const tol = 1e-7
1518 v := Vec {X : 1 }
16- y90 := RotatingMat4 (math .Pi / 2 , Vec {Y : 1 })
19+ y90 := RotationMat4 (math .Pi / 2 , Vec {Y : 1 })
1720 got := y90 .MulPosition (v )
1821 want := Vec {Z : - 1 }
1922 if ! EqualElem (got , want , tol ) {
2023 t .Errorf ("want %v, got %v" , want , got )
2124 }
2225}
2326
27+ func TestRotationConversions (t * testing.T ) {
28+ const tol = internal .Smallfloat64 / 10
29+ rotations := []Quat {
30+ Rotation (1 , Vec {X : 1 }),
31+ Rotation (1 , Vec {X : 1 , Y : 1 }),
32+ Rotation (2 , Vec {X : 1 , Y : 1 , Z : 1 }),
33+ Rotation (math .Pi , Vec {X : 1 }),
34+ Rotation (math .Pi , Vec {X : 1 , Y : 1 }),
35+ Rotation (math .Pi , Vec {X : 1 , Y : 1 , Z : 1 }),
36+ Rotation (2 * math .Pi , Vec {X : 1 }),
37+ Rotation (0 , Vec {X : 1 }),
38+ }
39+ rng := newRNG (1 )
40+ for _ , rot := range rotations {
41+ angle , axis := rot .Rotation ()
42+ gotrot := Rotation (angle , axis )
43+ if ! EqualQuat (rot , gotrot , tol ) {
44+ t .Errorf ("want %v, got %v" , rot , gotrot )
45+ }
46+ if ! rot .EqualOrientation (gotrot , tol ) {
47+ t .Errorf ("not equal orientations %v, got %v" , rot , gotrot )
48+ }
49+ m3 := rot .RotationMat3 ()
50+ for i := 0 ; i < 20 ; i ++ {
51+ v := rng .Vec ()
52+ vgot := MulMatVec (m3 , v )
53+ vwant := rot .Rotate (v )
54+ if ! EqualElem (vgot , vwant , tol ) {
55+ t .Errorf ("want %v, got %v" , vwant , vgot )
56+ }
57+ }
58+ }
59+ }
60+
61+ func TestRotationBetweenVecs (t * testing.T ) {
62+ const tol = internal .Smallfloat64 / 10
63+ rng := newRNG (1 )
64+ for i := 0 ; i < 80 ; i ++ {
65+ start := rng .Vec ()
66+ dest := rng .Vec ()
67+ rot := RotationBetweenVecs (start , dest )
68+ gotDest := rot .Rotate (start )
69+ if ! EqualElem (Unit (gotDest ), Unit (dest ), tol ) { // test direction, so use Unit.
70+ t .Errorf ("want %v, got %v" , dest , gotDest )
71+ }
72+ }
73+ }
74+
2475func TestSVD (t * testing.T ) {
2576 const tol = 1e-6
2677 a := mat3 (- 0.558253 , - 0.0461681 , - 0.505735 , - 0.411397 , 0.0365854 , 0.199707 , 0.285389 , - 0.313789 , 0.200189 )
@@ -59,3 +110,27 @@ func printMat(a Mat3) {
59110 fmt .Printf ("%f %f %f \n " , a .x10 , a .x11 , a .x12 )
60111 fmt .Printf ("%f %f %f \n " , a .x20 , a .x21 , a .x22 )
61112}
113+
114+ func newRNG (src int ) * rngGen {
115+ return & rngGen {rng : * rand .New (rand .NewSource (int64 (src )))}
116+ }
117+
118+ type rngGen struct {
119+ rng rand.Rand
120+ }
121+
122+ func (rng * rngGen ) Vec () Vec {
123+ return Vec {X : rng .Float (), Y : rng .Float (), Z : rng .Float ()}
124+ }
125+
126+ func (rng * rngGen ) Float () float64 {
127+ return float64 (rng .rng .Float64 ())
128+ }
129+
130+ func (rng * rngGen ) FloatRange (start , end float64 ) float64 {
131+ return float64 (rng .rng .Float64 ())* (end - start ) + start
132+ }
133+
134+ func (rng * rngGen ) VecRange (start , end float64 ) Vec {
135+ return Vec {X : rng .FloatRange (start , end ), Y : rng .FloatRange (start , end ), Z : rng .FloatRange (start , end )}
136+ }
0 commit comments