-
Notifications
You must be signed in to change notification settings - Fork 1
/
matstack4f.go
90 lines (75 loc) · 2.03 KB
/
matstack4f.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package vmath
import (
"errors"
)
// MatStack4f represents a stack of 4x4 matrices.
type MatStack4f struct {
stack []Mat4f
}
// NewMatStack4f creates a new matrix stack containing only the identity matrix.
func NewMatStack4f() *MatStack4f {
mStack := &MatStack4f{
stack: make([]Mat4f, 1),
}
mStack.stack[0] = Ident4f()
return mStack
}
// Size returns the current size of the matrix stack
func (m MatStack4f) Size() int {
return len(m.stack)
}
// Push stores the current top on the stack by duplicating it.
func (m *MatStack4f) Push() {
m.stack = append(m.stack, m.Top())
}
// Pop removes the current top element from the stack.
// Returns an error if the stack contains only one element.
func (m *MatStack4f) Pop() error {
if len(m.stack) == 1 {
return errors.New("cannot pop last element from matrix stack")
}
m.stack = m.stack[:len(m.stack)-1]
return nil
}
// Top returns the current top element without modifying the stack.
func (m MatStack4f) Top() Mat4f {
return m.stack[len(m.stack)-1]
}
// Set overwrites the top element with a new matrix.
func (m *MatStack4f) Set(mat Mat4f) {
m.stack[len(m.stack)-1] = mat
}
// PushSet is equivalent to Push(), Set().
func (m *MatStack4f) PushSet(mat Mat4f) {
m.Push()
m.Set(mat)
}
// SetIdent overwrites the top element with the identity matrix.
func (m *MatStack4f) SetIdent() {
m.Set(Ident4f())
}
// PushIdent is equivalent to Push(), SetIdent().
func (m *MatStack4f) PushIdent() {
m.Push()
m.SetIdent()
}
// MulRight multiplies the top element with the given matrix.
func (m *MatStack4f) MulRight(mat Mat4f) {
top := m.Top()
m.Set(top.Mul(mat))
}
// PushMulRight is equivalent to Push(), MulRight().
func (m *MatStack4f) PushMulRight(mat Mat4f) {
m.Push()
m.MulRight(mat)
}
// MulLeft multiplies the given matrix with the top element and overwrites the top element with the result.
func (m *MatStack4f) MulLeft(mat Mat4f) {
top := m.Top()
m.Set(mat.Mul(top))
}
// PushMulLeft is equivalent to Push(), MulLeft().
func (m *MatStack4f) PushMulLeft(mat Mat4f) {
m.Push()
m.MulLeft(mat)
}