-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
normalize_attribute_transformer.go
83 lines (64 loc) · 2.12 KB
/
normalize_attribute_transformer.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
package meshops
import (
"math"
"github.com/EliCDavis/polyform/modeling"
"github.com/EliCDavis/vector/vector2"
"github.com/EliCDavis/vector/vector3"
)
// Finds the vector with the longest length and scales all vectors within the
// mesh attribute by (1 / longestLength)
type NormalizeAttribute3DTransformer struct {
Attribute string
}
func (st NormalizeAttribute3DTransformer) attribute() string {
return st.Attribute
}
func (st NormalizeAttribute3DTransformer) Transform(m modeling.Mesh) (results modeling.Mesh, err error) {
attribute := getAttribute(st, modeling.PositionAttribute)
if err = RequireV3Attribute(m, attribute); err != nil {
return
}
return NormalizeAttribute3D(m, attribute), nil
}
func NormalizeAttribute3D(m modeling.Mesh, attribute string) modeling.Mesh {
if err := RequireV3Attribute(m, attribute); err != nil {
panic(err)
}
oldData := m.Float3Attribute(attribute)
maxLength := -math.MaxFloat64
for i := 0; i < oldData.Len(); i++ {
maxLength = math.Max(maxLength, oldData.At(i).Length())
}
scaledData := make([]vector3.Float64, oldData.Len())
for i := 0; i < oldData.Len(); i++ {
scaledData[i] = oldData.At(i).DivByConstant(maxLength)
}
return m.SetFloat3Attribute(attribute, scaledData)
}
type NormalizeAttribute2DTransformer struct {
Attribute string
}
func (st NormalizeAttribute2DTransformer) attribute() string {
return st.Attribute
}
func (st NormalizeAttribute2DTransformer) Transform(m modeling.Mesh) (results modeling.Mesh, err error) {
if err = RequireV2Attribute(m, st.Attribute); err != nil {
return
}
return NormalizeAttribute2D(m, st.Attribute), nil
}
func NormalizeAttribute2D(m modeling.Mesh, attribute string) modeling.Mesh {
if err := RequireV2Attribute(m, attribute); err != nil {
panic(err)
}
oldData := m.Float2Attribute(attribute)
maxLength := -math.MaxFloat64
for i := 0; i < oldData.Len(); i++ {
maxLength = math.Max(maxLength, oldData.At(i).Length())
}
scaledData := make([]vector2.Float64, oldData.Len())
for i := 0; i < oldData.Len(); i++ {
scaledData[i] = oldData.At(i).DivByConstant(maxLength)
}
return m.SetFloat2Attribute(attribute, scaledData)
}