-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathline.go
73 lines (58 loc) · 1.64 KB
/
line.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
package repeat
import (
"github.com/EliCDavis/polyform/math/trs"
"github.com/EliCDavis/polyform/nodes"
"github.com/EliCDavis/vector/vector3"
)
type Line struct {
// Start of the line
Start vector3.Float64
// End of the line
End vector3.Float64
// How many TRS matrices to produce
Samples int
// If true, the start and end points are not included in the resulting
// array of TRS values
Exclusive bool
}
func (l Line) TRS() []trs.TRS {
if l.Samples == 0 {
return make([]trs.TRS, 0)
}
if l.Samples == 1 {
return []trs.TRS{trs.Position(vector3.Midpoint(l.Start, l.End))}
}
values := make([]trs.TRS, 0, l.Samples)
if !l.Exclusive {
values = append(values, trs.Position(l.Start))
}
inbetweenSamples := l.Samples
if !l.Exclusive {
inbetweenSamples -= 2
}
dir := l.End.Sub(l.Start)
inc := dir.DivByConstant(float64(inbetweenSamples + 1))
for i := 0; i < inbetweenSamples; i++ {
values = append(values, trs.Position(l.Start.Add(inc.Scale(float64(i+1)))))
}
if !l.Exclusive {
values = append(values, trs.Position(l.End))
}
return values
}
type LineNode = nodes.Struct[LineNodeData]
type LineNodeData struct {
Start nodes.Output[vector3.Float64]
End nodes.Output[vector3.Float64]
Samples nodes.Output[int]
Exclusive nodes.Output[bool]
}
func (r LineNodeData) Out() nodes.StructOutput[[]trs.TRS] {
line := Line{
Start: nodes.TryGetOutputValue(r.Start, vector3.Zero[float64]()),
End: nodes.TryGetOutputValue(r.End, vector3.Zero[float64]()),
Samples: max(nodes.TryGetOutputValue(r.Samples, 0), 0),
Exclusive: nodes.TryGetOutputValue(r.Exclusive, false),
}
return nodes.NewStructOutput(line.TRS())
}