webgl2-sdf 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. package/README.md +100 -0
  2. package/node/bezier/bezier-curves-to-line-segs.d.ts +10 -0
  3. package/node/bezier/eval-de-casteljau.d.ts +17 -0
  4. package/node/bezier/from-to/from-to-2.d.ts +12 -0
  5. package/node/bezier/from-to/from-to-3.d.ts +12 -0
  6. package/node/bezier/from-to/from-to.d.ts +10 -0
  7. package/node/bezier/is-cubic-obtuse.d.ts +11 -0
  8. package/node/bezier/is-quad-obtuse.d.ts +11 -0
  9. package/node/bezier/is-really-point.d.ts +9 -0
  10. package/node/bezier/split-by-deviation-from-straight-line-cubic.d.ts +20 -0
  11. package/node/bezier/split-by-deviation-from-straight-line-quad.d.ts +11 -0
  12. package/node/bezier/split-into-line-segments.d.ts +10 -0
  13. package/node/debug-shaders.d.ts +9 -0
  14. package/node/generate-sdf.d.ts +18 -0
  15. package/node/helpers/calc-circs.d.ts +11 -0
  16. package/node/helpers/clip-line-segment-to-grid.d.ts +14 -0
  17. package/node/helpers/clip-line-segment-to-strips.d.ts +13 -0
  18. package/node/helpers/create-empty-grid.d.ts +8 -0
  19. package/node/helpers/create-empty-strips.d.ts +7 -0
  20. package/node/helpers/distance-seg-to-p.d.ts +5 -0
  21. package/node/helpers/find-close-cells.d.ts +3 -0
  22. package/node/helpers/find-crossing-cells.d.ts +3 -0
  23. package/node/helpers/get-distance-to-line-function.d.ts +10 -0
  24. package/node/helpers/jump-idx.d.ts +2 -0
  25. package/node/helpers/map-to-viewbox.d.ts +2 -0
  26. package/node/helpers/seg-box-x.d.ts +10 -0
  27. package/node/helpers/seg-strip-x.d.ts +9 -0
  28. package/node/index.d.ts +1 -0
  29. package/node/main-program.d.ts +4 -0
  30. package/node/max-aspect-ratio-before-stretch.d.ts +2 -0
  31. package/node/prepare-buffers.d.ts +10 -0
  32. package/node/row-count.d.ts +2 -0
  33. package/node/shaders/main.fragment.d.ts +2 -0
  34. package/node/shaders/main.vertex.d.ts +2 -0
  35. package/node/svg/get-beziers-from-raw-paths.d.ts +12 -0
  36. package/node/svg/get-paths-from-str.d.ts +9 -0
  37. package/node/svg/path-data-polyfill/parse-number.d.ts +10 -0
  38. package/node/svg/path-data-polyfill/parse-path-data-string.d.ts +9 -0
  39. package/node/svg/path-data-polyfill/source.d.ts +19 -0
  40. package/node/svg/path-segment/c.d.ts +19 -0
  41. package/node/svg/path-segment/h.d.ts +15 -0
  42. package/node/svg/path-segment/l.d.ts +16 -0
  43. package/node/svg/path-segment/q.d.ts +16 -0
  44. package/node/svg/path-segment/s.d.ts +20 -0
  45. package/node/svg/path-segment/t.d.ts +18 -0
  46. package/node/svg/path-segment/v.d.ts +15 -0
  47. package/node/svg/path-segment/z.d.ts +13 -0
  48. package/node/svg/path-state.d.ts +11 -0
  49. package/node/tex-width.d.ts +11 -0
  50. package/node/types/attribute.d.ts +6 -0
  51. package/node/types/cell.d.ts +18 -0
  52. package/node/types/gl-context.d.ts +28 -0
  53. package/node/types/gl-type.d.ts +2 -0
  54. package/node/types/gl-usage.d.ts +2 -0
  55. package/node/types/program.d.ts +16 -0
  56. package/node/types/strip.d.ts +8 -0
  57. package/node/types/texture.d.ts +4 -0
  58. package/node/utils/calc-circs.d.ts +11 -0
  59. package/node/utils/clip-line-segment-to-grid.d.ts +14 -0
  60. package/node/utils/clip-line-segment-to-strips.d.ts +13 -0
  61. package/node/utils/create-empty-grid.d.ts +8 -0
  62. package/node/utils/create-empty-strips.d.ts +7 -0
  63. package/node/utils/distance-seg-to-p.d.ts +5 -0
  64. package/node/utils/find-close-cells.d.ts +3 -0
  65. package/node/utils/find-crossing-cells.d.ts +3 -0
  66. package/node/utils/get-distance-to-line-function.d.ts +10 -0
  67. package/node/utils/jump-idx.d.ts +2 -0
  68. package/node/utils/map-to-viewbox.d.ts +2 -0
  69. package/node/utils/seg-box-x.d.ts +10 -0
  70. package/node/utils/seg-strip-x.d.ts +9 -0
  71. package/node/vector/dot.d.ts +8 -0
  72. package/node/vector/from-to-vec.d.ts +8 -0
  73. package/node/vector/len.d.ts +6 -0
  74. package/node/webgl-utils/compile-shader.d.ts +2 -0
  75. package/node/webgl-utils/get-gl-context.d.ts +10 -0
  76. package/node/webgl-utils/set-attribute.d.ts +15 -0
  77. package/node/webgl-utils/set-uniform-block.d.ts +9 -0
  78. package/node/webgl-utils/set-uniform.d.ts +4 -0
  79. package/node/webgl-utils/uniform-block.d.ts +6 -0
  80. package/node/webgl-utils/uniform-type.d.ts +2 -0
  81. package/node/webgl-utils/use-program.d.ts +17 -0
  82. package/node/webgl-utils/use-texture.d.ts +9 -0
  83. package/node/webgl2.d.ts +2 -0
  84. package/package.json +56 -0
  85. package/src/bezier/bezier-curves-to-line-segs.ts +39 -0
  86. package/src/bezier/eval-de-casteljau.ts +78 -0
  87. package/src/bezier/from-to/from-to-2.ts +159 -0
  88. package/src/bezier/from-to/from-to-3.ts +176 -0
  89. package/src/bezier/from-to/from-to.ts +30 -0
  90. package/src/bezier/is-cubic-obtuse.ts +31 -0
  91. package/src/bezier/is-quad-obtuse.ts +26 -0
  92. package/src/bezier/is-really-point.ts +25 -0
  93. package/src/bezier/split-by-deviation-from-straight-line-cubic.ts +109 -0
  94. package/src/bezier/split-by-deviation-from-straight-line-quad.ts +66 -0
  95. package/src/bezier/split-into-line-segments.ts +39 -0
  96. package/src/debug-shaders.ts +38 -0
  97. package/src/generate-sdf.ts +91 -0
  98. package/src/index.ts +2 -0
  99. package/src/main-program.ts +160 -0
  100. package/src/max-aspect-ratio-before-stretch.ts +5 -0
  101. package/src/prepare-buffers.ts +149 -0
  102. package/src/row-count.ts +6 -0
  103. package/src/shaders/main.fragment.ts +157 -0
  104. package/src/shaders/main.vertex.ts +55 -0
  105. package/src/svg/get-beziers-from-raw-paths.ts +112 -0
  106. package/src/svg/get-paths-from-str.ts +19 -0
  107. package/src/svg/path-data-polyfill/parse-number.ts +138 -0
  108. package/src/svg/path-data-polyfill/parse-path-data-string.ts +26 -0
  109. package/src/svg/path-data-polyfill/source.ts +176 -0
  110. package/src/svg/path-segment/c.ts +34 -0
  111. package/src/svg/path-segment/h.ts +28 -0
  112. package/src/svg/path-segment/l.ts +30 -0
  113. package/src/svg/path-segment/q.ts +30 -0
  114. package/src/svg/path-segment/s.ts +40 -0
  115. package/src/svg/path-segment/t.ts +35 -0
  116. package/src/svg/path-segment/v.ts +28 -0
  117. package/src/svg/path-segment/z.ts +27 -0
  118. package/src/svg/path-state.ts +15 -0
  119. package/src/tex-width.ts +14 -0
  120. package/src/types/attribute.ts +9 -0
  121. package/src/types/cell.ts +21 -0
  122. package/src/types/gl-context.ts +28 -0
  123. package/src/types/gl-type.ts +16 -0
  124. package/src/types/gl-usage.ts +14 -0
  125. package/src/types/program.ts +14 -0
  126. package/src/types/strip.ts +11 -0
  127. package/src/types/texture.ts +7 -0
  128. package/src/types/typed-array.ts +16 -0
  129. package/src/utils/calc-circs.ts +129 -0
  130. package/src/utils/clip-line-segment-to-grid.ts +133 -0
  131. package/src/utils/clip-line-segment-to-strips.ts +196 -0
  132. package/src/utils/create-empty-grid.ts +32 -0
  133. package/src/utils/create-empty-strips.ts +21 -0
  134. package/src/utils/distance-seg-to-p.ts +50 -0
  135. package/src/utils/find-close-cells.ts +171 -0
  136. package/src/utils/find-crossing-cells.ts +40 -0
  137. package/src/utils/get-distance-to-line-function.ts +59 -0
  138. package/src/utils/is-point-in-box.ts +16 -0
  139. package/src/utils/jump-idx.ts +107 -0
  140. package/src/utils/map-to-viewbox.ts +41 -0
  141. package/src/utils/path.ts +137 -0
  142. package/src/utils/seg-box-x.ts +84 -0
  143. package/src/utils/seg-strip-x.ts +72 -0
  144. package/src/utils/sum.ts +13 -0
  145. package/src/vector/dot.ts +13 -0
  146. package/src/vector/from-to-vec.ts +13 -0
  147. package/src/vector/len.ts +11 -0
  148. package/src/webgl-utils/compile-shader.ts +15 -0
  149. package/src/webgl-utils/get-gl-context.ts +61 -0
  150. package/src/webgl-utils/set-attribute.ts +74 -0
  151. package/src/webgl-utils/set-uniform-block.ts +45 -0
  152. package/src/webgl-utils/set-uniform.ts +24 -0
  153. package/src/webgl-utils/uniform-block.ts +9 -0
  154. package/src/webgl-utils/uniform-type.ts +10 -0
  155. package/src/webgl-utils/use-program.ts +48 -0
  156. package/src/webgl-utils/use-texture.ts +34 -0
@@ -0,0 +1,159 @@
1
+
2
+ /**
3
+ * Returns a bezier curve that starts and ends at the given `t` parameters.
4
+ *
5
+ * @param ps a quadratic bezier curve given as an ordered array of its
6
+ * control point coordinates, e.g. `[[0,0], [1,1], [2,1]]`
7
+ * @param tS the `t` parameter where the resultant bezier should start
8
+ * @param tE the `t` parameter where the resultant bezier should end
9
+ *
10
+ * @internal
11
+ */
12
+ function fromTo2(
13
+ ps: number[][],
14
+ tS: number,
15
+ tE: number): number[][] {
16
+
17
+ if (tS === 0) {
18
+ if (tE === 1) {
19
+ return ps;
20
+ }
21
+ return splitLeft2(ps, tE);
22
+ }
23
+
24
+ if (tE === 1) { return splitRight2(ps, tS); }
25
+
26
+ return splitAtBoth2(ps, tS, tE);
27
+ }
28
+
29
+
30
+ /**
31
+ * Returns a bezier curve that starts at the given t parameter and ends
32
+ * at `t === 1`.
33
+ *
34
+ * @param ps a quadratic bezier curve given as an ordered array of its
35
+ * control point coordinates, e.g. `[[0,0], [1,1], [2,1]]`
36
+ * @param t the `t` parameter where the resultant bezier should start
37
+ *
38
+ * @internal
39
+ */
40
+ function splitRight2(
41
+ ps: number[][],
42
+ t: number): number[][] {
43
+
44
+ // --------------------------------------------------------
45
+ // const [[x0, y0], [x1, y1], [x2, y2]] = ps;
46
+ const p0 = ps[0];
47
+ const p1 = ps[1];
48
+ const p2 = ps[2];
49
+ const x0 = p0[0]; const y0 = p0[1];
50
+ const x1 = p1[0]; const y1 = p1[1];
51
+ const x2 = p2[0]; const y2 = p2[1];
52
+ // --------------------------------------------------------
53
+
54
+ const tt = t*t;
55
+
56
+ const xA = x0 - x1;
57
+ const xB = x2 - x1;
58
+
59
+ const yA = y0 - y1;
60
+ const yB = y2 - y1;
61
+
62
+ return [
63
+ [tt*(xA + xB) - (2*t*xA - x0), // xx0, split point x
64
+ tt*(yA + yB) - (2*t*yA - y0)], // yy0, split point y
65
+ [t*xB + x1, // xx1
66
+ t*yB + y1], // yy1
67
+ p2
68
+ ];
69
+ }
70
+
71
+
72
+ /**
73
+ * Returns a bezier curve that starts at `t === 0` and ends at the given `t`
74
+ * parameter.
75
+ *
76
+ * @param ps a quadratic bezier curve given as an ordered array of its
77
+ * control point coordinates, e.g. `[[0,0], [1,1], [2,1]]`
78
+ * @param t the `t` parameter where the resultant bezier should end
79
+ *
80
+ * @internal
81
+ */
82
+ function splitLeft2(
83
+ ps: number[][],
84
+ t: number): number[][] {
85
+
86
+ // --------------------------------------------------------
87
+ // const [[x0, y0], [x1, y1], [x2, y2]] = ps;
88
+ const p0 = ps[0];
89
+ const p1 = ps[1];
90
+ const p2 = ps[2];
91
+ const x0 = p0[0]; const y0 = p0[1];
92
+ const x1 = p1[0]; const y1 = p1[1];
93
+ const x2 = p2[0]; const y2 = p2[1];
94
+ // --------------------------------------------------------
95
+
96
+ const tt = t*t;
97
+ const xA = x0 - x1;
98
+ const yA = y0 - y1;
99
+
100
+ return [
101
+ p0,
102
+ [-t*xA + x0, // xx1
103
+ -t*yA + y0], // yy1
104
+ [tt*(xA + (x2 - x1)) - (2*t*xA - x0), // xx2 - split point x
105
+ tt*(yA + (y2 - y1)) - (2*t*yA - y0)] // yy2 - split point y
106
+ ];
107
+ }
108
+
109
+
110
+ /**
111
+ * Returns a bezier curve that starts and ends at the given `t` parameters.
112
+ *
113
+ * @param ps a quadratic bezier curve given as an ordered array of its
114
+ * control point coordinates, e.g. `[[0,0], [1,1], [2,1]]`
115
+ * @param tS the `t` parameter where the resultant bezier should start
116
+ * @param tE the `t` parameter where the resultant bezier should end
117
+ *
118
+ * @internal
119
+ */
120
+ function splitAtBoth2(
121
+ ps: number[][],
122
+ tS: number,
123
+ tE: number): number[][] {
124
+
125
+ // --------------------------------------------------------
126
+ // const [[x0, y0], [x1, y1], [x2, y2]] = ps;
127
+ const p0 = ps[0];
128
+ const p1 = ps[1];
129
+ const p2 = ps[2];
130
+ const x0 = p0[0]; const y0 = p0[1];
131
+ const x1 = p1[0]; const y1 = p1[1];
132
+ const x2 = p2[0]; const y2 = p2[1];
133
+ // --------------------------------------------------------
134
+
135
+ const ttS = tS*tS;
136
+ const ttE = tE*tE;
137
+ const tStE = tS*tE;
138
+
139
+ const xA = x0 - x1;
140
+ const xB = x2 - x1;
141
+ const xC = xA + xB;
142
+
143
+ const yA = y0 - y1;
144
+ const yB = y2 - y1;
145
+ const yC = yA + yB;
146
+
147
+ const xx0 = ttS*xC - (2*tS*xA - x0);
148
+ const xx1 = tStE*xC - (xA*(tE + tS) - x0);
149
+ const xx2 = ttE*xC - (2*tE*xA - x0);
150
+
151
+ const yy0 = ttS*yC - (2*tS*yA - y0);
152
+ const yy1 = tStE*yC - (yA*(tE + tS) - y0);
153
+ const yy2 = ttE*yC - (2*tE*yA - y0);
154
+
155
+ return [[xx0, yy0], [xx1, yy1], [xx2, yy2]];
156
+ }
157
+
158
+
159
+ export { fromTo2 }
@@ -0,0 +1,176 @@
1
+
2
+ /**
3
+ * Returns a bezier curve that starts and ends at the given t parameters.
4
+ *
5
+ * @param ps a cubic bezier curve given as an ordered array of its
6
+ * control point coordinates, e.g. `[[0,0], [1,1], [2,1], [2,0]]`
7
+ * @param tS the `t` parameter where the resultant bezier should start
8
+ * @param tE the `t` parameter where the resultant bezier should end
9
+ *
10
+ * @internal
11
+ */
12
+ function fromTo3(
13
+ ps: number[][],
14
+ tS: number,
15
+ tE: number): number[][] {
16
+
17
+ if (tS === 0) {
18
+ if (tE === 1) {
19
+ return ps;
20
+ }
21
+ return splitLeft3(ps, tE);
22
+ }
23
+
24
+ if (tE === 1) { return splitRight3(ps, tS); }
25
+
26
+ return splitAtBoth3(ps, tS, tE);
27
+ }
28
+
29
+
30
+ /**
31
+ * Returns a bezier curve that starts at the given t parameter and ends
32
+ * at `t === 1`.
33
+ *
34
+ * @param ps a cubic bezier curve given as an ordered array of its
35
+ * control point coordinates, e.g. `[[0,0], [1,1], [2,1], [2,0]]`
36
+ * @param t the `t` parameter where the resultant bezier should start
37
+ *
38
+ * @internal
39
+ */
40
+ function splitRight3(
41
+ ps: number[][],
42
+ t: number): number[][] {
43
+
44
+ // --------------------------------------------------------
45
+ // const [[x0, y0], [x1, y1], [x2, y2], [x3, y3]] = ps;
46
+ const p0 = ps[0]; const p1 = ps[1];
47
+ const p2 = ps[2]; const p3 = ps[3];
48
+ const x00 = p0[0]; const y00 = p0[1];
49
+ const x10 = p1[0]; const y10 = p1[1];
50
+ const x20 = p2[0]; const y20 = p2[1];
51
+ const x30 = p3[0]; const y30 = p3[1];
52
+ // --------------------------------------------------------
53
+
54
+ const x01 = x00 - t*(x00 - x10);
55
+ const x11 = x10 - t*(x10 - x20);
56
+ const x21 = x20 - t*(x20 - x30);
57
+ const x02 = x01 - t*(x01 - x11);
58
+ const x12 = x11 - t*(x11 - x21);
59
+ const x03 = x02 - t*(x02 - x12);
60
+
61
+ const y01 = y00 - t*(y00 - y10);
62
+ const y11 = y10 - t*(y10 - y20);
63
+ const y21 = y20 - t*(y20 - y30);
64
+ const y02 = y01 - t*(y01 - y11);
65
+ const y12 = y11 - t*(y11 - y21);
66
+ const y03 = y02 - t*(y02 - y12);
67
+
68
+
69
+ return [[x03, y03], [x12, y12], [x21, y21], p3];
70
+ }
71
+
72
+
73
+ /**
74
+ * Returns a bezier curve that starts at `t === 0` and ends at the given t
75
+ * parameter.
76
+ *
77
+ * @param ps a cubic bezier curve given as an ordered array of its
78
+ * control point coordinates, e.g. `[[0,0], [1,1], [2,1], [2,0]]`
79
+ * @param t the `t` parameter where the resultant bezier should end
80
+ *
81
+ * @internal
82
+ */
83
+ function splitLeft3(
84
+ ps: number[][],
85
+ t: number): number[][] {
86
+
87
+ // --------------------------------------------------------
88
+ // const [[x0, y0], [x1, y1], [x2, y2], [x3, y3]] = ps;
89
+ const p0 = ps[0]; const p1 = ps[1];
90
+ const p2 = ps[2]; const p3 = ps[3];
91
+ const x00 = p0[0]; const y00 = p0[1];
92
+ const x10 = p1[0]; const y10 = p1[1];
93
+ const x20 = p2[0]; const y20 = p2[1];
94
+ const x30 = p3[0]; const y30 = p3[1];
95
+ // --------------------------------------------------------
96
+
97
+ const x01 = x00 - t*(x00 - x10);
98
+ const x11 = x10 - t*(x10 - x20);
99
+ const x21 = x20 - t*(x20 - x30);
100
+ const x02 = x01 - t*(x01 - x11);
101
+ const x12 = x11 - t*(x11 - x21);
102
+ const x03 = x02 - t*(x02 - x12);
103
+
104
+ const y01 = y00 - t*(y00 - y10);
105
+ const y11 = y10 - t*(y10 - y20);
106
+ const y21 = y20 - t*(y20 - y30);
107
+ const y02 = y01 - t*(y01 - y11);
108
+ const y12 = y11 - t*(y11 - y21);
109
+ const y03 = y02 - t*(y02 - y12);
110
+
111
+ return [p0, [x01, y01], [x02, y02], [x03, y03]];
112
+ }
113
+
114
+
115
+ /**
116
+ * Returns a bezier curve that starts and ends at the given `t` parameters.
117
+ *
118
+ * @param ps a cubic bezier curve given as an ordered array of its
119
+ * control point coordinates, e.g. `[[0,0], [1,1], [2,1], [2,0]]`
120
+ * @param tS the `t` parameter where the resultant bezier should start
121
+ * @param tE the `t` parameter where the resultant bezier should end
122
+ *
123
+ * @internal
124
+ */
125
+ function splitAtBoth3(
126
+ ps: number[][],
127
+ tS: number,
128
+ tE: number): number[][] {
129
+
130
+ // --------------------------------------------------------
131
+ // const [[x0, y0], [x1, y1], [x2, y2], [x3, y3]] = ps;
132
+ const p0 = ps[0]; const p1 = ps[1];
133
+ const p2 = ps[2]; const p3 = ps[3];
134
+ const x0 = p0[0]; const y0 = p0[1];
135
+ const x1 = p1[0]; const y1 = p1[1];
136
+ const x2 = p2[0]; const y2 = p2[1];
137
+ const x3 = p3[0]; const y3 = p3[1];
138
+ // --------------------------------------------------------
139
+
140
+ const ttS = tS*tS;
141
+ const tttS = tS*ttS;
142
+ const ttE = tE*tE;
143
+ const tttE = tE*ttE;
144
+ const tStE = tS*tE;
145
+
146
+ const xA = x0 - x1;
147
+ const xB = x2 - x1;
148
+ const xC = x3 - x0;
149
+ const xD = xA + xB;
150
+ const tSxA = tS*xA;
151
+ const tExA = tE*xA;
152
+ const xC3xB = xC - 3*xB;
153
+
154
+ const yA = y0 - y1;
155
+ const yB = y2 - y1;
156
+ const yC = y3 - y0;
157
+ const yD = yA + yB;
158
+ const tSyA = tS*yA;
159
+ const tEyA = tE*yA;
160
+ const yC3yB = yC - 3*yB;
161
+
162
+ const xx0 = tttS*xC3xB + (3*tS*(tS*xD - xA) + x0);
163
+ const xx1 = tStE*(tS*xC3xB + 2*xD) + ((ttS*xD + x0) - (tExA + 2*tSxA));
164
+ const xx2 = tStE*(tE*xC3xB + 2*xD) + ((ttE*xD + x0) - (2*tExA + tSxA));
165
+ const xx3 = tttE*xC3xB + (3*tE*(tE*xD - xA) + x0);
166
+
167
+ const yy0 = tttS*yC3yB + (3*tS*(tS*yD - yA) + y0);
168
+ const yy1 = tStE*(tS*yC3yB + 2*yD) + ((ttS*yD + y0) - (tEyA + 2*tSyA));
169
+ const yy2 = tStE*(tE*yC3yB + 2*yD) + ((ttE*yD + y0) - (2*tEyA + tSyA));
170
+ const yy3 = tttE*yC3yB + (3*tE*(tE*yD - yA) + y0);
171
+
172
+ return [[xx0, yy0], [xx1, yy1], [xx2, yy2], [xx3, yy3]];
173
+ }
174
+
175
+
176
+ export { fromTo3 }
@@ -0,0 +1,30 @@
1
+ import { fromTo2 } from './from-to-2.js';
2
+ import { fromTo3 } from './from-to-3.js';
3
+
4
+
5
+ /**
6
+ * Returns a bezier curve that starts and ends at the given `t` parameters.
7
+ *
8
+ * @param ps an order 2 or 3 bezier curve given as an ordered array of its
9
+ * control point coordinates, e.g. `[[0,0], [1,1], [2,1], [2,0]]`
10
+ * @param tS the `t` parameter where the resultant bezier should start
11
+ * @param tE the `t` parameter where the resultant bezier should end
12
+ */
13
+ function fromTo(
14
+ ps: number[][],
15
+ tS: number,
16
+ tE: number): number[][] {
17
+
18
+ if (ps.length === 4) {
19
+ return fromTo3(ps, tS, tE);
20
+ }
21
+
22
+ if (ps.length === 3) {
23
+ return fromTo2(ps, tS, tE);
24
+ }
25
+
26
+ throw new Error('The given bezier curve must be of order 2 or 3.');
27
+ }
28
+
29
+
30
+ export { fromTo }
@@ -0,0 +1,31 @@
1
+ import { fromToVec } from '../vector/from-to-vec';
2
+ import { dot } from "../vector/dot";
3
+
4
+
5
+ /**
6
+ * Returns `true` if the given cubic bezier is obtuse, `false` otherwise (i.e.
7
+ * `false` if acute).
8
+ *
9
+ * Obtuse here is defined as follows: let the cubic form triangles through its
10
+ * control points P0, P1, P3 where P0 and P3 are the endpoints. If both interior
11
+ * angles ∠P0 and ∠P2 are <= 90 degrees then the cubic is considered acute,
12
+ * otherwise it is considered obtuse. The same should be true for P0, P2, P3.
13
+ */
14
+ function isCubicObtuse(ps: number[][]) {
15
+ const v0 = fromToVec(ps[0], ps[1]);
16
+ const v1 = fromToVec(ps[1], ps[3]);
17
+ const v2 = fromToVec(ps[3], ps[0]);
18
+
19
+ const v3 = fromToVec(ps[0], ps[2]);
20
+ const v4 = fromToVec(ps[2], ps[3]);
21
+
22
+ return (
23
+ dot(v2, v0) > 0 ||
24
+ dot(v1, v2) > 0 ||
25
+ dot(v2, v3) > 0 ||
26
+ dot(v4, v2) > 0
27
+ );
28
+ }
29
+
30
+
31
+ export { isCubicObtuse }
@@ -0,0 +1,26 @@
1
+ import { fromToVec } from '../vector/from-to-vec';
2
+ import { dot } from "../vector/dot";
3
+
4
+
5
+ /**
6
+ * Returns `true` if the given quadratic bezier is obtuse, `false` otherwise (i.e.
7
+ * `false` if acute).
8
+ *
9
+ * Obtuse here is defined as follows: let the quad form a triangle through its
10
+ * control points P0, P1, P2 where P0 and P2 are the endpoints. If both interior
11
+ * angles ∠P0 and ∠P2 are <= 90 degrees then the quad is considered acute,
12
+ * otherwise it is considered obtuse.
13
+ */
14
+ function isQuadObtuse(ps: number[][]) {
15
+ const v0 = fromToVec(ps[0], ps[1]);
16
+ const v1 = fromToVec(ps[1], ps[2]);
17
+ const v2 = fromToVec(ps[2], ps[0]);
18
+
19
+ return (
20
+ dot(v2, v0) > 0 ||
21
+ dot(v1, v2) > 0
22
+ );
23
+ }
24
+
25
+
26
+ export { isQuadObtuse }
@@ -0,0 +1,25 @@
1
+
2
+ /**
3
+ * Returns `true` if the given bezier curve has all control points coincident,
4
+ * `false` otherwise.
5
+ *
6
+ * @param ps an order 0,1,2 or 3 bezier curve given as an array of its control
7
+ * points, e.g. `[[0,0],[1,1],[2,1],[2,0]]`
8
+ */
9
+ function isReallyPoint(
10
+ ps: number[][]): boolean {
11
+
12
+ const x = ps[0][0];
13
+ const y = ps[0][1];
14
+
15
+ for (let i=1; i<ps.length; i++) {
16
+ if (x !== ps[i][0] || y !== ps[i][1]) {
17
+ return false;
18
+ }
19
+ }
20
+
21
+ return true;
22
+ }
23
+
24
+
25
+ export { isReallyPoint }
@@ -0,0 +1,109 @@
1
+ import { getDistanceToLineFunction } from '../utils/get-distance-to-line-function.js';
2
+ import { fromTo } from './from-to/from-to.js';
3
+ import { isCubicObtuse } from './is-cubic-obtuse.js';
4
+
5
+ const { abs, max } = Math;
6
+
7
+
8
+ /**
9
+ * Split the given cubic bezier curve into pieces (given as an array of
10
+ * parameter `t` values) such that each piece is guaranteed to deviate less
11
+ * than `maxD` from a straigh line.
12
+ *
13
+ * * a crude method is employed (for efficiency) by noting that the hausdorff
14
+ * distance between a cubic and the line segment connecting its endpoints is at
15
+ * most 3/4 the distance of the max distance between any control point and the
16
+ * line segment if the control points are on the same side of the line segment
17
+ * and at most 4/9 if they are on opposite sides AND (in both cases) the cubic
18
+ * is not obtuse, i.e. the inner control points are not outside the strip formed
19
+ * by the two lines passing through the endpoint control points normal to the
20
+ * line segment connecting the endpoint control points.
21
+ *
22
+ * @param ps an order 2 bezier curve given as an ordered array of its
23
+ * control point coordinates, e.g. `[[0,0], [1,1], [2,1]]`
24
+ * @param maxD
25
+ */
26
+ function splitByDeviationFromStraighLine_Cubic_Crude(
27
+ ps: number[][],
28
+ maxD: number): number[] {
29
+
30
+ const tsS = [0];
31
+ const tsE = [1];
32
+ while (true) {
33
+ const tS = tsS[tsS.length - 1];
34
+ const tE = tsE[tsE.length - 1];
35
+ const ps_ = fromTo(ps, tS, tE);
36
+
37
+ if ((!isCubicObtuse(ps_) && getMaxD(ps_) <= maxD)) {
38
+ tsS.push(tsE.pop()!);
39
+ if (tE === 1) {
40
+ return tsS;
41
+ }
42
+ continue;
43
+ }
44
+
45
+ const t = (tS + tE)/2;
46
+
47
+ tsE.push(t);
48
+ }
49
+ }
50
+
51
+
52
+ // function splitByDeviationFromStraighLine_Cubic_Precise(
53
+ // ps: number[][],
54
+ // maxD: number): number[] {
55
+
56
+ // const tsS = [0];
57
+ // const tsE = [1];
58
+ // while (true) {
59
+ // const tS = tsS[tsS.length - 1];
60
+ // const tE = tsE[tsE.length - 1];
61
+ // const ps_ = fromTo(ps, tS, tE);
62
+
63
+ // if ((!isCubicObtuse(ps_) && getMaxDPrecise(ps_) <= maxD)) {
64
+ // tsS.push(tsE.pop()!);
65
+ // if (tE === 1) {
66
+ // return tsS;
67
+ // }
68
+ // continue;
69
+ // }
70
+
71
+ // const t = (tS + tE)/2;
72
+
73
+ // tsE.push(t);
74
+ // }
75
+ // }
76
+
77
+
78
+ function getMaxD(ps: number[][]) {
79
+ const dF = getDistanceToLineFunction(ps[0], ps[3]);
80
+ const d1 = dF(ps[1]);
81
+ const d2 = dF(ps[2]);
82
+
83
+ // The hausdorff distance between a cubic and the line segment connecting its
84
+ // endpoints is at most 3/4 the distance of the max distance between any control
85
+ // point and the line segment if the control points are on the same side of the line
86
+ // segment and at most 4/9 if they are on opposite sides AND (in both cases) the cubic
87
+ // is not obtuse, i.e. the inner control points are not outside the strip formed
88
+ // by the two lines passing through the endpoint control points normal to the
89
+ // line segment connecting the endpoint control points.
90
+
91
+ const C = d1*d2 <= 0 ? 4/9 : 3/4;
92
+
93
+ return C * max(abs(d1), abs(d2));
94
+ }
95
+
96
+
97
+ export {
98
+ splitByDeviationFromStraighLine_Cubic_Crude,
99
+ // splitByDeviationFromStraighLine_Cubic_Precise
100
+ }
101
+
102
+
103
+ // Quokka tests
104
+ // splitByDeviationFromStraighLine_Cubic_Crude([
105
+ // [43, 0],
106
+ // [23, 0],
107
+ // [33, 0],
108
+ // [53, 0]
109
+ // ], 0.5);//?
@@ -0,0 +1,66 @@
1
+ import { getDistanceToLineFunction } from '../utils/get-distance-to-line-function.js';
2
+ import { evalDeCasteljau } from './eval-de-casteljau';
3
+ import { fromTo } from './from-to/from-to';
4
+ import { isQuadObtuse } from './is-quad-obtuse';
5
+
6
+ const { abs } = Math;
7
+
8
+
9
+ /**
10
+ * Split the given quadratic bezier curve into pieces (given as an array of
11
+ * parameter `t` values) such that each piece is guaranteed to deviate less
12
+ * than `maxD` from a straigh line.
13
+ *
14
+ * @param ps an order 2 bezier curve given as an ordered array of its
15
+ * control point coordinates, e.g. `[[0,0], [1,1], [2,1]]`
16
+ * @param maxD
17
+ */
18
+ function splitByDeviationFromStraighLine_Quad(
19
+ ps: number[][],
20
+ maxD: number): number[] {
21
+
22
+ const tsS = [0];
23
+ const tsE = [1];
24
+ while (true) {
25
+ const tS = tsS[tsS.length - 1];
26
+ const tE = tsE[tsE.length - 1];
27
+ const ps_ = fromTo(ps, tS, tE);
28
+
29
+ if ((!isQuadObtuse(ps_) && getMaxD(ps_) <= maxD)) {
30
+ tsS.push(tsE.pop()!);
31
+ if (tE === 1) {
32
+ return tsS;
33
+ }
34
+ continue;
35
+ }
36
+
37
+ const t = (tS + tE)/2;
38
+
39
+ tsE.push(t);
40
+ }
41
+ }
42
+
43
+
44
+ function getMaxD(ps: number[][]) {
45
+ if (ps[0][0] === ps[1][0] && ps[0][1] === ps[1][1]) {
46
+ return 0;
47
+ }
48
+ const p = evalDeCasteljau(ps, 0.5); // peak is reached at t = 0.5
49
+ const dF = getDistanceToLineFunction(ps[0], ps[2]);
50
+
51
+ const d = abs(dF(p));
52
+
53
+ return d;
54
+ }
55
+
56
+
57
+ export { splitByDeviationFromStraighLine_Quad }
58
+
59
+
60
+ // Quokka tests
61
+ // splitByDeviationFromStraighLine_Quad([[0,0],[0,4],[-3,4],[20,0]], 0.25);//?
62
+ // splitByDeviationFromStraighLine_Quad([
63
+ // [128, 423],
64
+ // [128, 423],
65
+ // [128, 423]
66
+ // ]);//?
@@ -0,0 +1,39 @@
1
+ import { evalDeCasteljau } from './eval-de-casteljau';
2
+ import { splitByDeviationFromStraighLine_Cubic_Crude } from './split-by-deviation-from-straight-line-cubic';
3
+ // import { splitByDeviationFromStraighLine_Cubic_Precise } from './split-by-deviation-from-straight-line-cubic';
4
+ import { splitByDeviationFromStraighLine_Quad } from './split-by-deviation-from-straight-line-quad';
5
+
6
+
7
+ /**
8
+ * Returns the result of splitting the given bezier curve into straight line
9
+ * segments up to `maxDeviation`
10
+ *
11
+ *
12
+ * @param ps
13
+ * @param maxDeviation maximum deviation from straight line
14
+ */
15
+ function splitIntoLineSegments(
16
+ ps: number[][],
17
+ maxDeviation: number) {
18
+
19
+ const ts = ps.length === 3
20
+ ? splitByDeviationFromStraighLine_Quad(ps, maxDeviation)
21
+ // : splitByDeviationFromStraighLine_Cubic_Precise(ps, maxDeviation)
22
+ : splitByDeviationFromStraighLine_Cubic_Crude(ps, maxDeviation)
23
+
24
+ const segs: number[][][] = [];
25
+ for (let i=0; i<ts.length - 1; i++) {
26
+ const p0 = evalDeCasteljau(ps, ts[i]);
27
+ const p1 = evalDeCasteljau(ps, ts[i+1]);
28
+ segs.push([p0,p1]);
29
+ }
30
+
31
+ return segs;
32
+ }
33
+
34
+
35
+ export { splitIntoLineSegments }
36
+
37
+
38
+ // Quokka tests
39
+ // splitByDeviationFromStraighLine([[0,0],[0,4],[-3,4],[20,0]], 0.25);//?
@@ -0,0 +1,38 @@
1
+ import { getMainFragment } from "./shaders/main.fragment";
2
+ import { main_Vertex } from "./shaders/main.vertex";
3
+
4
+
5
+ /**
6
+ *
7
+ * @param gl
8
+ * @param type `gl.VERTEX_SHADER | gl.FRAGMENT_SHADER`
9
+ * @param shaderStr
10
+ */
11
+ function debugGlsl(
12
+ gl: WebGL2RenderingContext,
13
+ type: typeof gl.VERTEX_SHADER | typeof gl.FRAGMENT_SHADER,
14
+ shaderStr: string) {
15
+
16
+ const s = gl.createShader(type)!;
17
+ gl.shaderSource(s, shaderStr);
18
+ gl.compileShader(s);
19
+ if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) {
20
+ throw new Error(gl.getShaderInfoLog(s)!);
21
+ }
22
+ }
23
+
24
+
25
+ function debugShaders(
26
+ gl: WebGL2RenderingContext) {
27
+
28
+ try {
29
+ debugGlsl(gl, gl.VERTEX_SHADER, main_Vertex);
30
+ debugGlsl(gl, gl.FRAGMENT_SHADER, getMainFragment(32,4));
31
+ } catch (e) {
32
+ console.log(e);
33
+ throw e;
34
+ }
35
+ }
36
+
37
+
38
+ export { debugShaders, debugGlsl }