@@ -15,6 +15,24 @@ struct VertexOut {
1515 int primIndex;
1616};
1717
18+ float udBezierApprox (float2 p, float2 A, float2 B, float2 C) {
19+
20+ // Compute barycentric coordinates of p.
21+ // p = s * A + t * B + (1-s-t) * C
22+ float2 v0 = B - A, v1 = C - A, v2 = p - A;
23+ float det = v0.x * v1.y - v1.x * v0.y ;
24+ float s = (v2.x * v1.y - v1.x * v2.y ) / det;
25+ float t = (v0.x * v2.y - v2.x * v0.y ) / det;
26+
27+ // Transform to canonical coordinte space.
28+ float u = s * .5 + t;
29+ float v = t;
30+
31+ float g = u*u - v;
32+
33+ return abs (g / length (float2 (dfdx (g), dfdy (g))));
34+ }
35+
1836float sdPrim (const DEVICE vgerPrim& prim, const DEVICE float2* cvs, float2 p, float filterWidth = 0 ) {
1937 float d = FLT_MAX;
2038 float s = 1 ;
@@ -60,25 +78,11 @@ float sdPrim(const DEVICE vgerPrim& prim, const DEVICE float2* cvs, float2 p, fl
6078 auto b = cvs[j+1 ];
6179 auto c = cvs[j+2 ];
6280
63- bool close = true ;
64- auto xmax = p.x + filterWidth;
65- auto xmin = p.x - filterWidth;
66-
67- // If the hull is far enough away, don't bother with
68- // a sdf.
69- if (a.x > xmax and b.x > xmax and c.x > xmax) {
70- close = false ;
71- } else if (a.x < xmin and b.x < xmin and c.x < xmin) {
72- close = false ;
73- }
81+ d = min (d, udBezierApprox (p, a, b, c));
7482
75- if (close) {
76- d = min (d, udBezier (p, a, b, c));
77-
78- // Flip if inside area between curve and line.
79- if (bezierTest (p, a, b, c)) {
80- s = -s;
81- }
83+ // Flip if inside area between curve and line.
84+ if (bezierTest (p, a, b, c)) {
85+ s = -s;
8286 }
8387
8488 if (lineTest (p, a, c)) {
0 commit comments