-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
line.go
97 lines (82 loc) · 2.67 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package marching
import (
"math"
"github.com/EliCDavis/polyform/math/geometry"
"github.com/EliCDavis/polyform/math/sample"
"github.com/EliCDavis/polyform/math/sdf"
"github.com/EliCDavis/polyform/modeling"
"github.com/EliCDavis/polyform/trees"
"github.com/EliCDavis/vector/vector3"
)
type thickLinePrimitive struct {
start, end vector3.Float64
radius float64
}
func (l thickLinePrimitive) BoundingBox() geometry.AABB {
aabb := geometry.NewAABBFromPoints(l.start, l.end)
aabb.Expand(l.radius * math.Sqrt2 * 2)
return aabb
}
func (l thickLinePrimitive) ClosestPoint(point vector3.Float64) vector3.Float64 {
line3d := geometry.NewLine3D(l.start, l.end)
return line3d.ClosestPointOnLine(point)
}
func Line(start, end vector3.Float64, radius, strength float64) Field {
bounds := geometry.NewAABBFromPoints(start, end)
bounds.Expand(radius * 2)
return Field{
Domain: bounds,
Float1Functions: map[string]sample.Vec3ToFloat{
modeling.PositionAttribute: sdf.Line(start, end, radius).Scale(strength),
},
}
}
func MultiSegmentLine(linePoints []vector3.Float64, radius, strength float64) Field {
if len(linePoints) < 2 {
panic("can not create a line segment field with less than 2 points")
}
thickLines := make([]trees.Element, len(linePoints)-1)
for i := 1; i < len(linePoints); i++ {
thickLines[i-1] = &thickLinePrimitive{
start: linePoints[i-1],
end: linePoints[i],
radius: radius,
}
}
octree := trees.NewOctree(thickLines)
bounds := geometry.NewAABBFromPoints(linePoints...)
bounds.Expand(radius * math.Sqrt2 * 2)
return Field{
Domain: bounds,
Float1Functions: map[string]sample.Vec3ToFloat{
modeling.PositionAttribute: func(v vector3.Float64) float64 {
lineIndexes := octree.ElementsContainingPoint(v)
min := math.MaxFloat64
for _, l := range lineIndexes {
val := sdf.Line(linePoints[l], linePoints[l+1], radius).Scale(strength)(v)
min = math.Min(min, val)
}
return min
},
},
}
}
func VarryingThicknessLine(linePoints []sdf.LinePoint, strength float64) Field {
if len(linePoints) < 2 {
panic("can not create a line segment field with less than 2 points")
}
bounds := geometry.NewAABB(linePoints[0].Point, vector3.Zero[float64]())
for i := 1; i < len(linePoints); i++ {
start := linePoints[i-1]
end := linePoints[i]
boundsSize := vector3.Fill(math.Max(start.Radius, end.Radius) + strength)
bounds.EncapsulateBounds(geometry.NewAABB(start.Point, boundsSize))
bounds.EncapsulateBounds(geometry.NewAABB(end.Point, boundsSize))
}
return Field{
Domain: bounds,
Float1Functions: map[string]sample.Vec3ToFloat{
modeling.PositionAttribute: sdf.VarryingThicknessLine(linePoints).Scale(strength),
},
}
}