Debian Codegraph @

vendor/github.com/xyproto/gfx/signed_distance.go

raw
1 //go:build !tinygo
2 // +build !tinygo
3
4 package gfx
5
6 import "math"
7
8 // SignedDistance holds 2D signed distance functions based on
9 // https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
10 type SignedDistance struct {
11 Vec
12 }
13
14 // SignedDistanceFunc is a func that takes a SignedDistance and returns a float64.
15 type SignedDistanceFunc func(SignedDistance) float64
16
17 // CircleFunc creates a SignedDistanceFunc for a circle with the given radius.
18 func (SignedDistance) CircleFunc(r float64) SignedDistanceFunc {
19 return func(sd SignedDistance) float64 {
20 return sd.Circle(r)
21 }
22 }
23
24 // LineFunc cleates a SignedDistanceFunc for a line with the given start and end.
25 func (SignedDistance) LineFunc(a, b Vec) SignedDistanceFunc {
26 return func(sd SignedDistance) float64 {
27 return sd.Line(a, b)
28 }
29 }
30
31 // RectangleFunc creates a SignedDistanceFunc for a rectangle with the given size.
32 func (SignedDistance) RectangleFunc(b Vec) SignedDistanceFunc {
33 return func(sd SignedDistance) float64 {
34 return sd.Rectangle(b)
35 }
36 }
37
38 // RhombusFunc creates a SignedDistanceFunc for a rhombus with the given size.
39 func (SignedDistance) RhombusFunc(b Vec) SignedDistanceFunc {
40 return func(sd SignedDistance) float64 {
41 return sd.Rhombus(b)
42 }
43 }
44
45 // EquilateralTriangleFunc creates a SignedDistanceFunc for an equilateral triangle with the given size.
46 func (SignedDistance) EquilateralTriangleFunc(s float64) SignedDistanceFunc {
47 return func(sd SignedDistance) float64 {
48 return sd.EquilateralTriangle(s)
49 }
50 }
51
52 // IsoscelesTriangleFunc creates a SignedDistanceFunc for an isosceles triangle with the given size.
53 func (SignedDistance) IsoscelesTriangleFunc(q Vec) SignedDistanceFunc {
54 return func(sd SignedDistance) float64 {
55 return sd.IsoscelesTriangle(q)
56 }
57 }
58
59 // Circle primitive
60 func (sd SignedDistance) Circle(r float64) float64 {
61 return sd.Len() - r
62 }
63
64 // Line primitive
65 func (sd SignedDistance) Line(a, b Vec) float64 {
66 pa, ba := sd.Sub(a), b.Sub(a)
67 c := Clamp(pa.Dot(ba)/ba.Dot(ba), 0.0, 1.0)
68
69 return pa.Sub(ba.Scaled(c)).Len()
70 }
71
72 // Rectangle primitive
73 func (sd SignedDistance) Rectangle(b Vec) float64 {
74 d := sd.Abs().Sub(b)
75
76 return d.Max(ZV).Len() + math.Min(math.Max(d.X, d.Y), 0)
77 }
78
79 // Rhombus primitive
80 func (sd SignedDistance) Rhombus(b Vec) float64 {
81 q := sd.Abs()
82 x := (-2*q.Normal().Dot(b.Normal()) + b.Normal().Dot(b.Normal())) / b.Dot(b)
83 h := Clamp(x, -1.0, 1.0)
84 d := q.Sub(b.Scaled(0.5).ScaledXY(V(1.0-h, 1.0+h))).Len()
85
86 return d * Sign(q.X*b.Y+q.Y*b.X-b.X*b.Y)
87 }
88
89 // EquilateralTriangle primitive
90 func (sd SignedDistance) EquilateralTriangle(s float64) float64 {
91 k := math.Sqrt(3)
92
93 p := sd.Vec
94
95 p.X = math.Abs(p.X) - s
96 p.Y = p.Y + s/k
97
98 if p.X+k*p.Y > 0.0 {
99 p = V(p.X-k*p.Y, -k*p.X-p.Y).Scaled(0.5)
100 }
101
102 p.X -= Clamp(p.X, -2.0, 0.0)
103
104 return -p.Len() * Sign(p.Y)
105 }
106
107 // IsoscelesTriangle primitive
108 func (sd SignedDistance) IsoscelesTriangle(q Vec) float64 {
109 p := sd.Vec
110
111 p.X = math.Abs(p.X)
112
113 a := p.Sub(q.Scaled(Clamp(p.Dot(q)/q.Dot(q), 0.0, 1.0)))
114 b := p.Sub(q.ScaledXY(V(Clamp(p.X/q.X, 0.0, 1.0), 1.0)))
115
116 s := -Sign(q.Y)
117
118 d := V(a.Dot(a), s*(p.X*q.Y-p.Y*q.X)).Min(V(b.Dot(b), s*(p.Y-q.Y)))
119
120 return -math.Sqrt(d.X) * Sign(d.Y)
121 }
122
123 // Rounded signed distance function shape
124 func (sd SignedDistance) Rounded(v, r float64) float64 {
125 return v - r
126 }
127
128 // Annular signed distance function shape
129 func (sd SignedDistance) Annular(v, r float64) float64 {
130 return math.Abs(v) - r
131 }
132
133 // OpUnion basic boolean operation for union.
134 func (sd SignedDistance) OpUnion(x, y float64) float64 {
135 return math.Min(x, y)
136 }
137
138 // OpSubtraction basic boolean operation for subtraction.
139 func (sd SignedDistance) OpSubtraction(x, y float64) float64 {
140 return math.Max(-x, y)
141 }
142
143 // OpIntersection basic boolean operation for intersection.
144 func (sd SignedDistance) OpIntersection(x, y float64) float64 {
145 return math.Max(x, y)
146 }
147
148 // OpSmoothUnion smooth operation for union.
149 func (sd SignedDistance) OpSmoothUnion(x, y, k float64) float64 {
150 h := Clamp(0.5+0.5*(y-x)/k, 0.0, 1.0)
151
152 return Lerp(y, x, h) - k*h*(1.0-h)
153 }
154
155 // OpSmoothSubtraction smooth operation for subtraction.
156 func (sd SignedDistance) OpSmoothSubtraction(x, y, k float64) float64 {
157 h := Clamp(0.5-0.5*(y+x)/k, 0.0, 1.0)
158
159 return Lerp(y, -x, h) + k*h*(1.0-h)
160 }
161
162 // OpSmoothIntersection smooth operation for intersection.
163 func (sd SignedDistance) OpSmoothIntersection(x, y, k float64) float64 {
164 h := Clamp(0.5-0.5*(y-x)/k, 0.0, 1.0)
165
166 return Lerp(y, x, h) + k*h*(1.0-h)
167 }
168
169 // OpSymX symmetry operation for X.
170 func (sd SignedDistance) OpSymX(sdf SignedDistanceFunc) float64 {
171 sd.X = math.Abs(sd.X)
172
173 return sdf(sd)
174 }
175
176 // OpSymY symmetry operation for Y.
177 func (sd SignedDistance) OpSymY(sdf SignedDistanceFunc) float64 {
178 sd.Y = math.Abs(sd.Y)
179
180 return sdf(sd)
181 }
182
183 // OpSymXY symmetry operation for X and Y.
184 func (sd SignedDistance) OpSymXY(sdf SignedDistanceFunc) float64 {
185 sd.X = math.Abs(sd.X)
186 sd.Y = math.Abs(sd.Y)
187
188 return sdf(sd)
189 }
190
191 // OpRepeat repeats based on the given c vector.
192 func (sd SignedDistance) OpRepeat(c Vec, sdf SignedDistanceFunc) float64 {
193 q := sd.Mod(c).Sub(c.Scaled(0.5))
194
195 return sdf(SignedDistance{q})
196 }
197
198 // OpMoved moves result of sdf by the given delta.
199 // (Relative to the identity matrix)
200 func (sd SignedDistance) OpMoved(d Vec, sdf SignedDistanceFunc) float64 {
201 return sd.OpTx(IM.Moved(d), sdf)
202 }
203
204 // OpTx translates using the given matrix.
205 func (sd SignedDistance) OpTx(t Matrix, sdf SignedDistanceFunc) float64 {
206 return sdf(SignedDistance{t.Unproject(sd.Vec)})
207 }