forked from hschendel/stl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.go
66 lines (50 loc) · 1.3 KB
/
transform.go
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
package stl
// This file contains generic 3D transformation stuff
import (
"math"
)
// Calculates a 4x4 rotation matrix for a rotation of angle in radians
// around a rotation axis defined by a point on it (pos) and its direction (dir).
// The result is written into *rotationMatrix.
func RotationMatrix(pos Vec3, dir Vec3, angle float64, rotationMatrix *Mat4) {
dirN := dir.unitVec()
s := math.Sin(angle)
c := math.Cos(angle)
u := float64(dirN[0])
v := float64(dirN[1])
w := float64(dirN[2])
su := s * u
sv := s * v
sw := s * w
ic := 1 - c
ic_u := ic * u
ic_uu := ic_u * u
ic_uv := ic_u * v
ic_uw := ic_u * w
ic_v := ic * v
ic_vv := ic_v * v
ic_vw := ic_v * w
ic_w := ic * w
ic_ww := ic_w * w
mRotate := Mat4{
Vec4{ic_uu + c, ic_uv - sw, ic_uw + sv, 0},
Vec4{ic_uv + sw, ic_vv + c, ic_vw - su, 0},
Vec4{ic_uw - sv, ic_vw + su, ic_ww + c, 0},
Vec4{0, 0, 0, 1},
}
mTranslateForward := Mat4{
Vec4{1, 0, 0, float64(pos[0])},
Vec4{0, 1, 0, float64(pos[1])},
Vec4{0, 0, 1, float64(pos[2])},
Vec4{0, 0, 0, 1},
}
mTranslateBackward := Mat4{
Vec4{1, 0, 0, float64(-pos[0])},
Vec4{0, 1, 0, float64(-pos[1])},
Vec4{0, 0, 1, float64(-pos[2])},
Vec4{0, 0, 0, 1},
}
var mTmp Mat4
mRotate.MultMat4(&mTranslateForward, &mTmp)
mTranslateBackward.MultMat4(&mTmp, rotationMatrix)
}