webgl2-sdf 0.0.2 → 0.0.4

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 (100) hide show
  1. package/browser/index.min.js +1 -1
  2. package/node/bezier/is-cubic-obtuse.js +2 -2
  3. package/node/bezier/is-cubic-obtuse.js.map +1 -1
  4. package/node/bezier/is-quad-obtuse.js +2 -2
  5. package/node/bezier/is-quad-obtuse.js.map +1 -1
  6. package/node/bezier/split-by-deviation-from-straight-line-quad.js +3 -3
  7. package/node/bezier/split-by-deviation-from-straight-line-quad.js.map +1 -1
  8. package/node/bezier/split-into-line-segments.js +3 -3
  9. package/node/bezier/split-into-line-segments.js.map +1 -1
  10. package/node/generate-sdf.d.ts +3 -2
  11. package/node/generate-sdf.js +5 -6
  12. package/node/generate-sdf.js.map +1 -1
  13. package/node/index.d.ts +2 -0
  14. package/node/index.js +2 -0
  15. package/node/index.js.map +1 -1
  16. package/node/main-program.js +0 -1
  17. package/node/main-program.js.map +1 -1
  18. package/node/prepare-buffers.js +4 -6
  19. package/node/prepare-buffers.js.map +1 -1
  20. package/node/shaders/main.fragment.js +1 -1
  21. package/node/shaders/main.fragment.js.map +1 -1
  22. package/node/shaders/main.vertex.d.ts +1 -1
  23. package/node/shaders/main.vertex.js +1 -2
  24. package/node/shaders/main.vertex.js.map +1 -1
  25. package/node/types/gl-context.d.ts +0 -1
  26. package/node/types/program.d.ts +4 -2
  27. package/node/utils/calc-circs.js +3 -3
  28. package/node/utils/calc-circs.js.map +1 -1
  29. package/node/utils/clip-line-segment-to-grid.d.ts +2 -2
  30. package/node/utils/clip-line-segment-to-grid.js +2 -5
  31. package/node/utils/clip-line-segment-to-grid.js.map +1 -1
  32. package/node/utils/clip-line-segment-to-strips.d.ts +2 -3
  33. package/node/utils/clip-line-segment-to-strips.js +6 -58
  34. package/node/utils/clip-line-segment-to-strips.js.map +1 -1
  35. package/node/utils/create-empty-grid.d.ts +1 -1
  36. package/node/utils/create-empty-grid.js +1 -1
  37. package/node/utils/create-empty-grid.js.map +1 -1
  38. package/node/utils/find-close-cells.d.ts +1 -1
  39. package/node/utils/find-close-cells.js +17 -73
  40. package/node/utils/find-close-cells.js.map +1 -1
  41. package/node/utils/find-crossing-cells.d.ts +1 -1
  42. package/node/utils/find-crossing-cells.js +1 -16
  43. package/node/utils/find-crossing-cells.js.map +1 -1
  44. package/node/utils/jump-idx.js +21 -19
  45. package/node/utils/jump-idx.js.map +1 -1
  46. package/node/webgl-utils/free-gl-context.d.ts +3 -0
  47. package/node/webgl-utils/free-gl-context.js +26 -0
  48. package/node/webgl-utils/free-gl-context.js.map +1 -0
  49. package/node/webgl-utils/get-gl-context.d.ts +3 -3
  50. package/node/webgl-utils/get-gl-context.js +6 -9
  51. package/node/webgl-utils/get-gl-context.js.map +1 -1
  52. package/node/webgl-utils/set-attribute.d.ts +3 -3
  53. package/node/webgl-utils/set-attribute.js +1 -1
  54. package/node/webgl-utils/set-attribute.js.map +1 -1
  55. package/node/webgl-utils/set-uniform-block.d.ts +1 -1
  56. package/node/webgl-utils/set-uniform-block.js +1 -3
  57. package/node/webgl-utils/set-uniform-block.js.map +1 -1
  58. package/node/webgl-utils/set-uniform.d.ts +2 -2
  59. package/node/webgl-utils/use-program.d.ts +2 -2
  60. package/node/webgl-utils/use-program.js +14 -4
  61. package/node/webgl-utils/use-program.js.map +1 -1
  62. package/package.json +1 -1
  63. package/src/bezier/is-cubic-obtuse.ts +2 -2
  64. package/src/bezier/is-quad-obtuse.ts +2 -2
  65. package/src/bezier/split-by-deviation-from-straight-line-quad.ts +3 -3
  66. package/src/bezier/split-into-line-segments.ts +3 -3
  67. package/src/debug-shaders.ts +2 -2
  68. package/src/generate-sdf.ts +7 -5
  69. package/src/index.ts +2 -0
  70. package/src/main-program.ts +0 -1
  71. package/src/prepare-buffers.ts +5 -6
  72. package/src/shaders/main.fragment.ts +1 -1
  73. package/src/shaders/main.vertex.ts +1 -2
  74. package/src/types/gl-context.ts +0 -1
  75. package/src/types/program.ts +4 -2
  76. package/src/utils/calc-circs.ts +3 -5
  77. package/src/utils/clip-line-segment-to-grid.ts +3 -8
  78. package/src/utils/clip-line-segment-to-strips.ts +7 -74
  79. package/src/utils/create-empty-grid.ts +2 -2
  80. package/src/utils/find-close-cells.ts +31 -91
  81. package/src/utils/find-crossing-cells.ts +2 -19
  82. package/src/utils/jump-idx.ts +26 -21
  83. package/src/webgl-utils/free-gl-context.ts +35 -0
  84. package/src/webgl-utils/get-gl-context.ts +9 -12
  85. package/src/webgl-utils/set-attribute.ts +4 -4
  86. package/src/webgl-utils/set-uniform-block.ts +2 -4
  87. package/src/webgl-utils/set-uniform.ts +2 -2
  88. package/src/webgl-utils/use-program.ts +19 -8
  89. package/node/types/strip.d.ts +0 -8
  90. package/node/types/strip.js +0 -2
  91. package/node/types/strip.js.map +0 -1
  92. package/node/utils/create-empty-strips.d.ts +0 -7
  93. package/node/utils/create-empty-strips.js +0 -16
  94. package/node/utils/create-empty-strips.js.map +0 -1
  95. package/node/utils/distance-seg-to-p.d.ts +0 -5
  96. package/node/utils/distance-seg-to-p.js +0 -35
  97. package/node/utils/distance-seg-to-p.js.map +0 -1
  98. package/src/types/strip.ts +0 -11
  99. package/src/utils/create-empty-strips.ts +0 -21
  100. package/src/utils/path.ts +0 -137
@@ -1 +1 @@
1
- {"version":3,"file":"use-program.js","sourceRoot":"","sources":["../../src/webgl-utils/use-program.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAGjD;;;;;;;;;;;;GAYG;AACH,SAAS,WAAW,CACR,SAAoB,EACpB,IAAY,EACZ,IAAY,EACZ,IAAY;IAEpB,MAAM,EAAE,EAAE,EAAE,QAAQ,EAAE,GAAG,SAAS,CAAC;IAEnC,IAAI,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QACjB,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;IAED,MAAM,UAAU,GAAkC,EAAE,CAAC;IACrD,MAAM,QAAQ,GAAoD,EAAE,CAAC;IACrE,MAAM,aAAa,GAAqC,EAAE,CAAC;IAE3D,MAAM,OAAO,GAAG,EAAE,CAAC,aAAa,EAAE,CAAC;IACnC,EAAE,CAAC,YAAY,CAAC,OAAO,EAAE,aAAa,CAAC,EAAE,EAAE,IAAI,EAAE,EAAE,CAAC,aAAa,CAAC,CAAC,CAAC;IACpE,EAAE,CAAC,YAAY,CAAC,OAAO,EAAE,aAAa,CAAC,EAAE,EAAE,IAAI,EAAE,EAAE,CAAC,eAAe,CAAC,CAAC,CAAC;IACtE,EAAE,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IAExB,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,EAAE,EAAE,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,aAAa,EAAE,CAAC;IAEtE,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC;AAGD,OAAO,EAAE,WAAW,EAAE,CAAA"}
1
+ {"version":3,"file":"use-program.js","sourceRoot":"","sources":["../../src/webgl-utils/use-program.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAGpD;;;;;;;;;;;;GAYG;AACH,SAAS,WAAW,CACR,SAAoB,EACpB,IAAY,EACZ,IAAY,EACZ,IAAY;IAEpB,MAAM,EAAE,EAAE,EAAE,QAAQ,EAAE,GAAG,SAAS,CAAC;IAEnC,IAAI,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QACjB,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;IAED,MAAM,UAAU,GAAkC,EAAE,CAAC;IACrD,MAAM,QAAQ,GAAoD,EAAE,CAAC;IACrE,MAAM,aAAa,GAAqC,EAAE,CAAC;IAE3D,MAAM,OAAO,GAAG,EAAE,CAAC,aAAa,EAAE,CAAC;IACnC,MAAM,YAAY,GAAG,aAAa,CAAC,EAAE,EAAE,IAAI,EAAE,EAAE,CAAC,aAAa,CAAC,CAAC;IAC/D,MAAM,cAAc,GAAG,aAAa,CAAC,EAAE,EAAE,IAAI,EAAE,EAAE,CAAC,eAAe,CAAC,CAAC;IAEnE,EAAE,CAAC,YAAY,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;IACvC,EAAE,CAAC,YAAY,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;IACzC,EAAE,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IAExB,QAAQ,CAAC,IAAI,CAAC,GAAG;QACb,EAAE;QACF,OAAO;QACP,UAAU;QACV,QAAQ;QACR,aAAa;QACb,YAAY;QACZ,cAAc;KACjB,CAAC;IAEF,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC;AAGD,OAAO,EAAE,WAAW,EAAE,CAAA"}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "webgl2-sdf",
3
3
  "sideEffects": false,
4
4
  "description": "Fast sdf generator from bezier curves or svg string.",
5
- "version": "0.0.2",
5
+ "version": "0.0.4",
6
6
  "author": {
7
7
  "name": "Floris Steenkamp"
8
8
  },
@@ -1,5 +1,5 @@
1
- import { fromToVec } from '../vector/from-to-vec';
2
- import { dot } from "../vector/dot";
1
+ import { fromToVec } from '../vector/from-to-vec.js';
2
+ import { dot } from "../vector/dot.js";
3
3
 
4
4
 
5
5
  /**
@@ -1,5 +1,5 @@
1
- import { fromToVec } from '../vector/from-to-vec';
2
- import { dot } from "../vector/dot";
1
+ import { fromToVec } from '../vector/from-to-vec.js';
2
+ import { dot } from "../vector/dot.js";
3
3
 
4
4
 
5
5
  /**
@@ -1,7 +1,7 @@
1
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';
2
+ import { evalDeCasteljau } from './eval-de-casteljau.js';
3
+ import { fromTo } from './from-to/from-to.js';
4
+ import { isQuadObtuse } from './is-quad-obtuse.js';
5
5
 
6
6
  const { abs } = Math;
7
7
 
@@ -1,7 +1,7 @@
1
- import { evalDeCasteljau } from './eval-de-casteljau';
2
- import { splitByDeviationFromStraighLine_Cubic_Crude } from './split-by-deviation-from-straight-line-cubic';
1
+ import { evalDeCasteljau } from './eval-de-casteljau.js';
2
+ import { splitByDeviationFromStraighLine_Cubic_Crude } from './split-by-deviation-from-straight-line-cubic.js';
3
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';
4
+ import { splitByDeviationFromStraighLine_Quad } from './split-by-deviation-from-straight-line-quad.js';
5
5
 
6
6
 
7
7
  /**
@@ -1,5 +1,5 @@
1
- import { getMainFragment } from "./shaders/main.fragment";
2
- import { main_Vertex } from "./shaders/main.vertex";
1
+ import { getMainFragment } from "./shaders/main.fragment.js";
2
+ import { main_Vertex } from "./shaders/main.vertex.js";
3
3
 
4
4
 
5
5
  /**
@@ -1,4 +1,4 @@
1
- import { getWebGLContext } from './webgl-utils/get-gl-context.js';
1
+ // import { getWebGLContext } from './webgl-utils/get-gl-context.js';
2
2
  import { main_Vertex } from './shaders/main.vertex.js';
3
3
  import { getMainFragment } from './shaders/main.fragment.js';
4
4
  import { initProgram } from './webgl-utils/use-program.js';
@@ -6,13 +6,14 @@ import { mainProgram } from './main-program.js';
6
6
  import { ROW_COUNT } from './row-count.js';
7
7
  import { getPathsFromStr } from './svg/get-paths-from-str.js';
8
8
  import { MAX_ASPECT_RATIO_BEFORE_STRETCH } from './max-aspect-ratio-before-stretch.js';
9
+ import { GlContext } from './types/gl-context.js';
9
10
  // import { debugShaders } from './debug-shaders.js';
10
11
 
11
12
  const { ceil, min, max } = Math;
12
13
 
13
14
 
14
15
  /**
15
- *
16
+ * TODO
16
17
  * @param gl
17
18
  * @param psss
18
19
  * @param width
@@ -28,7 +29,7 @@ const { ceil, min, max } = Math;
28
29
  * @param resolution
29
30
  */
30
31
  function generateIntoFramebuffer(
31
- gl: WebGL2RenderingContext,
32
+ glContext: GlContext,
32
33
  psss: (number[][])[][] | string,
33
34
  width: number,
34
35
  height: number,
@@ -37,7 +38,6 @@ function generateIntoFramebuffer(
37
38
  sdfExponent = 1,
38
39
  inclInside = true,
39
40
  inclOutside = true,
40
- // framebuffer: WebGLFramebuffer | null,
41
41
  x = 0, y = 0,
42
42
  channel = 0,
43
43
  resolution: 0.5|1 = 0.5) {
@@ -48,7 +48,7 @@ function generateIntoFramebuffer(
48
48
  ? getPathsFromStr(psss)
49
49
  : psss;
50
50
 
51
- const glContext = getWebGLContext(gl);
51
+ // const glContext = getWebGLContext(gl);
52
52
 
53
53
  const { onContextLoss } = glContext;
54
54
 
@@ -72,6 +72,8 @@ function generateIntoFramebuffer(
72
72
  main_Vertex, getMainFragment(colCount, padCount)
73
73
  );
74
74
 
75
+ const { gl } = glContext;
76
+
75
77
  gl.useProgram(programMain.program);
76
78
  mainProgram(
77
79
  glContext, programMain, resolution, psss_,
package/src/index.ts CHANGED
@@ -1,2 +1,4 @@
1
1
 
2
2
  export { generateIntoFramebuffer } from './generate-sdf.js';
3
+ export { freeGlContext } from './webgl-utils/free-gl-context.js';
4
+ export { getWebGlContext } from './webgl-utils/get-gl-context.js';
@@ -81,7 +81,6 @@ function mainProgram(
81
81
  setUniform_('1f', 'uMaxDistance', maxDistance);
82
82
  setUniform_('1f', 'uExponent', sdfExponent); // TODO
83
83
  setUniform_('1i', 'uIncl', (inclInside ? 1 : 0) + (inclOutside ? 2 : 0));
84
- setUniform_('1f', 'uStretch', stretch); // TODO
85
84
 
86
85
  setUniformBlock(programMain)('SegIdxRangePerCellBlock', 0, segIdxs_PerCell_Range_Arr);
87
86
  setUniformBlock(programMain)('SegIdxRangePerStripBlock', 1, segIdxs_PerStrip_Range_Arr);
@@ -5,10 +5,9 @@ import { createEmptyGrid } from './utils/create-empty-grid.js';
5
5
  import { findCrossingCells } from './utils/find-crossing-cells.js';
6
6
  import { TEX_WIDTH } from './tex-width.js';
7
7
  import { ROW_COUNT } from './row-count.js';
8
- import { createEmptyStrips } from './utils/create-empty-strips.js';
9
8
  import { clipLineSegmentToStrips } from './utils/clip-line-segment-to-strips.js';
10
9
  import { mapToViewbox } from './utils/map-to-viewbox.js';
11
- // import { sum } from './utils/sum.js';
10
+ // import { sum } from './utils/sum.js'; // testing
12
11
 
13
12
 
14
13
  function prepareBuffers(
@@ -28,11 +27,12 @@ function prepareBuffers(
28
27
 
29
28
  const lineSegs = bezierCurvesToLineSegs(psss_, resolution);
30
29
  const grid = createEmptyGrid(colCount, padCount);
31
- const strips = createEmptyStrips();
30
+ const strips = new Array(ROW_COUNT).fill(undefined).map(v => []);
31
+
32
32
  for (let i=0; i<lineSegs.length; i++) {
33
33
  const seg = lineSegs[i];
34
34
  // Split the line segment into multiple segments that fit within grid cells
35
- clipLineSegmentToGrid(grid, width, height, colCount, cellSize, seg, padCount); // add segments to grid
35
+ clipLineSegmentToGrid(grid, width, height, cellSize, seg, padCount); // add segments to grid
36
36
  clipLineSegmentToStrips(strips, height, seg); // add segments to strips
37
37
  }
38
38
 
@@ -96,8 +96,7 @@ function prepareBuffers(
96
96
  // Add line segs from strips
97
97
  const segIdxs_PerStrip_Range: [number,number][] = [];
98
98
  for (let i=0; i<ROW_COUNT; i++) {
99
- const strip = strips[i];
100
- const { lineSegs } = strip;
99
+ const lineSegs = strips[i];
101
100
 
102
101
  //////////
103
102
  const L = lineSegs.length;
@@ -1,4 +1,4 @@
1
- import { ROW_COUNT } from "../row-count"
1
+ import { ROW_COUNT } from "../row-count.js";
2
2
 
3
3
 
4
4
  const cache: { [padCount_times_colCount: number]: string } = {};
@@ -1,11 +1,10 @@
1
- import { ROW_COUNT } from "../row-count";
1
+ import { ROW_COUNT } from "../row-count.js";
2
2
 
3
3
  const main_Vertex = /*glsl*/`#version 300 es
4
4
 
5
5
  precision highp float;
6
6
 
7
7
  uniform vec2 uWidthHeight;
8
- uniform float uStretch;
9
8
  in vec2 aUV;
10
9
  in ivec2 aCloseCellIdxRangePerCell;
11
10
  in ivec2 aCrossIdxRangePerCell;
@@ -20,7 +20,6 @@ interface GlContext {
20
20
  readonly gl: WebGL2RenderingContext;
21
21
  readonly textures: { [index:string]: Texture };
22
22
  readonly programs: { [index:string]: Program };
23
- readonly framebufferStack: WebGLFramebuffer[];
24
23
  readonly onContextLoss: () => void;
25
24
  }
26
25
 
@@ -1,5 +1,5 @@
1
- import type { Attribute } from "./attribute";
2
- import type { UniformBlock } from "../webgl-utils/uniform-block";
1
+ import type { Attribute } from "./attribute.js";
2
+ import type { UniformBlock } from "../webgl-utils/uniform-block.js";
3
3
 
4
4
 
5
5
  interface Program {
@@ -8,6 +8,8 @@ interface Program {
8
8
  readonly attributes: { [index:string]: Attribute };
9
9
  readonly uniforms: { [index:string]: WebGLUniformLocation | null };
10
10
  readonly uniformBlocks: { [index:string]: UniformBlock };
11
+ readonly vertexShader: WebGLShader;
12
+ readonly fragmentShader: WebGLShader;
11
13
  }
12
14
 
13
15
 
@@ -1,6 +1,6 @@
1
- import { MAX_ASPECT_RATIO_BEFORE_STRETCH } from '../max-aspect-ratio-before-stretch';
2
- import { ROW_COUNT } from '../row-count';
3
- import { len } from '../vector/len';
1
+ import { MAX_ASPECT_RATIO_BEFORE_STRETCH } from '../max-aspect-ratio-before-stretch.js';
2
+ import { ROW_COUNT } from '../row-count.js';
3
+ import { len } from '../vector/len.js';
4
4
 
5
5
 
6
6
  /**
@@ -50,8 +50,6 @@ function calcCircs() {
50
50
  ranges.sort((a,b) => (a.from - b.from));
51
51
  }
52
52
 
53
-
54
-
55
53
  return ranges;
56
54
  }
57
55
 
@@ -1,6 +1,5 @@
1
- import type { Cell } from "../types/cell";
2
- import { ROW_COUNT } from "../row-count";
3
- import { segBoxX } from "./seg-box-x";
1
+ import type { Cell } from "../types/cell.js";
2
+ import { segBoxX } from "./seg-box-x.js";
4
3
 
5
4
 
6
5
  const { floor, ceil } = Math;
@@ -21,21 +20,17 @@ function clipLineSegmentToGrid(
21
20
  grid: Cell[][],
22
21
  width: number,
23
22
  height: number,
24
- colCount: number,
25
23
  cellSize: number,
26
24
  seg: number[][],
27
25
  padCount: number): void {
28
26
 
29
- /** `cellSize` *must* be a power of two */
30
- // const cellSize = width/ROW_COUNT;
31
-
32
27
  const padding = cellSize*padCount;
33
28
 
34
29
  const [[x0,y0],[x1,y1]] = seg;
35
30
 
36
31
  const paddedWidth = width + padding;
37
32
  const paddedHeight = height + padding;
38
- // TODO - investigate lines on edge and corners - ps could be length 4!!
33
+
39
34
  const ps = segBoxX(seg, [[-padding,-padding],[paddedWidth,paddedHeight]]);
40
35
 
41
36
  const p0IsInBox = x0 > -padding && x0 < paddedWidth && y0 > -padding && y0 < paddedHeight;
@@ -1,5 +1,4 @@
1
- import type { Strip } from "../types/strip";
2
- import { segStripX } from "./seg-strip-x";
1
+ import { segStripX } from "./seg-strip-x.js";
3
2
 
4
3
 
5
4
  const { floor, ceil } = Math;
@@ -11,12 +10,12 @@ const { floor, ceil } = Math;
11
10
  * * modifies strips by adding line segments to each strip
12
11
  * * size/count *must* be a power of 2
13
12
  *
14
- * @param count the number of strips
13
+ * @param strips array of strips - each contains an array line segments
15
14
  * @param height the height of a strip
16
15
  * @param seg the line segment (array of 2 points)
17
16
  */
18
17
  function clipLineSegmentToStrips(
19
- strips: Strip[],
18
+ strips: number[][][][],
20
19
  height: number,
21
20
  seg: number[][]): void {
22
21
 
@@ -93,7 +92,7 @@ function clipLineSegmentToStrips(
93
92
  // if we're past the line endpoint
94
93
  if ((tX > 1 || dX === 0) && tY > 1) {
95
94
  const v = floor(p1[1]/cellSize);
96
- strips[v]?.lineSegs.push([[x,y],p1]);
95
+ strips[v]?.push([[x,y],p1]);
97
96
  break;
98
97
  }
99
98
 
@@ -109,7 +108,7 @@ function clipLineSegmentToStrips(
109
108
  } else {
110
109
  const v = fY(y/cellSize) - (btt ? 0 : 1); // previous v
111
110
  const seg_ = [[x,y],ps[i]];
112
- strips[v]?.lineSegs.push(seg_);
111
+ strips[v]?.push(seg_);
113
112
  break;
114
113
  }
115
114
  }
@@ -117,7 +116,7 @@ function clipLineSegmentToStrips(
117
116
  // previous v
118
117
  const v = fY(y/cellSize) - (btt ? 0 : 1);
119
118
 
120
- strips[v]?.lineSegs.push([[x,y], [xX,xY]]);
119
+ strips[v]?.push([[x,y], [xX,xY]]);
121
120
 
122
121
  // update current position
123
122
  x = xX;
@@ -127,70 +126,4 @@ function clipLineSegmentToStrips(
127
126
 
128
127
 
129
128
  export { clipLineSegmentToStrips }
130
-
131
-
132
- // Quokka tests - https://www.desmos.com/calculator/uyqsdkviih
133
- // import { createEmptyStrips } from "./create-empty-strips";
134
-
135
- // {
136
- // const strips = createEmptyStrips(8);
137
- // const seg = [[0, 100], [0, 400]];
138
-
139
- // toDesmosStr(seg);
140
- // clipLineSegmentToStrips(strips, 512, seg);
141
- // testAllEmptyExcept(strips,[1,2,3,4,5,6]);
142
- // }
143
-
144
-
145
- // {
146
- // const strips = createEmptyStrips(8);
147
- // const seg = [[83, 166], [-90, 440]];
148
-
149
- // toDesmosStr(seg);
150
- // clipLineSegmentToStrips(strips, 512, seg);
151
- // testAllEmptyExcept(strips,[4,5,6]);
152
- // }
153
-
154
- // {
155
- // const strips = createEmptyStrips(8);
156
- // const seg = [[-570, 236], [-392, 546]];
157
-
158
- // toDesmosStr(seg);
159
- // clipLineSegmentToStrips(strips, 512, seg);
160
- // testAllEmptyExcept(strips,[3,4,5,6,7]);
161
- // }
162
-
163
- // {
164
- // const strips = createEmptyStrips(8);
165
- // const seg = [[-462, 632], [-152, 611]];
166
-
167
- // toDesmosStr(seg);
168
- // clipLineSegmentToStrips(strips, 512, seg);
169
- // testAllEmptyExcept(strips,[]);
170
- // }
171
-
172
-
173
- // function toDesmosStr(seg: number[][]) {
174
- // const [[x0,y0],[x1,y1]] = seg;
175
- // return `\\left(\\left(1-t\\right)\\cdot${x0.toFixed(2)}+t\\cdot\\left(${x1.toFixed(2)}\\right),\\left(1-t\\right)\\cdot${y0.toFixed(2)}+t\\cdot${y1.toFixed(2)}\\right)`;
176
- // }
177
- // function testAllEmptyExcept(
178
- // strips: Strip[],
179
- // exceptions: number[]) {
180
-
181
- // for (let i=0; i<strips.length; i++) {
182
- // const strip = strips[i];
183
- // const idx = exceptions.findIndex(v => v === i);
184
- // const exception = exceptions[idx];
185
- // const len = strip.lineSegs.length;
186
- // if (exception === undefined) {
187
- // if (len !== 0) {
188
- // throw new Error(`Strip ${i} must be empty`);
189
- // }
190
- // } else {
191
- // if (len !== 1) {
192
- // throw new Error(`Strip ${i} must contain 1 segments, found ${len}`);
193
- // }
194
- // }
195
- // }
196
- // }
129
+ var a = [[1,1], [1,1]]
@@ -1,5 +1,5 @@
1
- import { ROW_COUNT } from "../row-count";
2
- import { Cell } from "../types/cell";
1
+ import { ROW_COUNT } from "../row-count.js";
2
+ import { Cell } from "../types/cell.js";
3
3
 
4
4
 
5
5
  /**
@@ -1,11 +1,10 @@
1
- import type { Cell } from "../types/cell";
2
- import { ROW_COUNT } from "../row-count";
3
- import { circsCache } from "./calc-circs";
4
- import { distanceSegToP } from "./distance-seg-to-p";
5
- import { jumpIdx } from "./jump-idx";
1
+ import type { Cell } from "../types/cell.js";
2
+ import { ROW_COUNT } from "../row-count.js";
3
+ import { circsCache } from "./calc-circs.js";
4
+ import { jumpIdx } from "./jump-idx.js";
6
5
 
7
6
 
8
- const { min, SQRT2 } = Math;
7
+ const { min, max, SQRT2 } = Math;
9
8
 
10
9
 
11
10
  function findCloseCells(
@@ -44,57 +43,47 @@ function findCloseCellsFor(
44
43
  // reduce current circle search range since we moved one square
45
44
  let k = jumpIdx((_minD - SQRT2*cellSize) / cellSize);
46
45
  let minD = Number.POSITIVE_INFINITY;
47
- while (k < circsCache.length) {
48
- const from = circsCache[k].from;
46
+ while (true) {
47
+ if (k >= circsCache.length) {
48
+ minD = cellSize*(circsCache[circsCache.length - 1].from - SQRT2);
49
+ break;
50
+ }
51
+
52
+ const { u, v, from } = circsCache[k];
49
53
 
50
54
  // `SQRT2` to account for diagonal
51
55
  if (cellSize*from > maxDistance + SQRT2*cellSize) {
56
+ minD = cellSize*(from - 2*SQRT2);
52
57
  break;
53
58
  }
54
59
 
55
- // uncomment loop below to search all equidistant squares
56
- // while (circsCache?.[k].from === from) {
57
- const { u, v } = circsCache[k];
58
- const u_ = u + i;
59
- const v_ = v + j;
60
- if (u_ < 0 || u_ >= colCount + 2*padCount ||
61
- v_ < 0 || v_ >= ROW_COUNT + 2*padCount) {
62
-
63
- k++; continue;
64
- }
65
-
66
- const { lineSegs } = grid[u_][v_];
67
- for (let l=0; l<lineSegs.length; l++) {
68
- // center of square
69
- const c = [(i - padCount + 0.5)*cellSize, (j - padCount + 0.5)*cellSize];
70
- const d = distanceSegToP(lineSegs[l], c);
71
- if (d < minD) {
72
- minD = d;
73
- }
74
- }
75
-
76
- k++;
77
- // }
78
-
79
- if (minD !== Number.POSITIVE_INFINITY) {
60
+ const u_ = u + i;
61
+ const v_ = v + j;
62
+ if (u_ < 0 || u_ >= colCount + 2*padCount ||
63
+ v_ < 0 || v_ >= ROW_COUNT + 2*padCount) {
64
+
65
+ k++; continue;
66
+ }
67
+
68
+ const { lineSegs } = grid[u_][v_];
69
+
70
+ if (lineSegs.length > 0) {
71
+ minD = cellSize*(from - SQRT2);
80
72
  break;
81
73
  }
82
- }
83
74
 
84
- if (minD === Number.POSITIVE_INFINITY) {
85
- return min(
86
- maxDistance + SQRT2*cellSize,
87
- cellSize * circsCache[circsCache.length - 1].from
88
- );
75
+ k++;
89
76
  }
90
77
 
78
+
91
79
  const { closeCells } = grid[i][j];
92
- let l = jumpIdx((_minD - 2*SQRT2*cellSize) / cellSize);
80
+
81
+ let l = max(0, k - 1);
93
82
  while (l < circsCache.length) {
94
83
  const { from, u, v } = circsCache[l];
95
84
 
96
- // `SQRT2` to account for diagonal
97
- if (cellSize*from > min(minD, maxDistance) + SQRT2*cellSize) {
85
+ // `SQRT2` to account for diagonal (times 2 since we minused it off earlier)
86
+ if (cellSize*from > min(minD, maxDistance) + 2*SQRT2*cellSize) {
98
87
  break;
99
88
  }
100
89
 
@@ -120,52 +109,3 @@ function findCloseCellsFor(
120
109
 
121
110
 
122
111
  export { findCloseCells }
123
-
124
-
125
- // Quokka tests
126
- // const grid: Cell[][] = [
127
- // [
128
- // { u: 0, v: 0, lineSegs: [], closeCells: [], crossingCells: [] },
129
- // { u: 0, v: 1, lineSegs: [], closeCells: [], crossingCells: [] },
130
- // { u: 0, v: 2, lineSegs: [], closeCells: [], crossingCells: [] },
131
- // { u: 0, v: 3, lineSegs: [], closeCells: [], crossingCells: [] }
132
- // ],
133
- // [
134
- // { u: 1, v: 0, lineSegs: [], closeCells: [], crossingCells: [] },
135
- // {
136
- // u: 1, v: 1,
137
- // lineSegs: [[[175, 163], [175, 355]]],
138
- // closeCells: [], crossingCells: []
139
- // },
140
- // {
141
- // u: 1, v: 2,
142
- // lineSegs: [[[175, 355], [335, 355]]],
143
- // closeCells: [], crossingCells: []
144
- // },
145
- // { u: 1, v: 3, lineSegs: [], closeCells: [], crossingCells: [] }
146
- // ],
147
- // [
148
- // { u: 2, v: 0, lineSegs: [], closeCells: [], crossingCells: [] },
149
- // {
150
- // u: 2, v: 1,
151
- // lineSegs: [[[335, 163], [175, 163]]],
152
- // closeCells: [], crossingCells: []
153
- // },
154
- // {
155
- // u: 2, v: 2,
156
- // lineSegs: [[[335, 355], [335, 163]]],
157
- // closeCells: [], crossingCells: []
158
- // },
159
- // { u: 2, v: 3, lineSegs: [], closeCells: [], crossingCells: [] }
160
- // ],
161
- // [
162
- // { u: 3, v: 0, lineSegs: [], closeCells: [], crossingCells: [] },
163
- // { u: 3, v: 1, lineSegs: [], closeCells: [], crossingCells: [] },
164
- // { u: 3, v: 2, lineSegs: [], closeCells: [], crossingCells: [] },
165
- // { u: 3, v: 3, lineSegs: [], closeCells: [], crossingCells: [] }
166
- // ]
167
- // ];
168
-
169
- // findCloseCells(grid);
170
- // const v = grid.map(cells => cells.map(c => ({ u: c.u, v: c.v, closeCells: c.closeCells })));
171
- // v;//?
@@ -1,10 +1,7 @@
1
- import { ROW_COUNT } from "../row-count";
2
- import type { Cell } from "../types/cell";
1
+ import type { Cell } from "../types/cell.js";
2
+ import { ROW_COUNT } from "../row-count.js";
3
3
 
4
4
 
5
- // TODO - simplify?
6
- // TODO - could be made much faster by also checking intersections here
7
- // and passing the result to the next cell
8
5
  function findCrossingCells(
9
6
  grid: Cell[][],
10
7
  colCount: number,
@@ -24,17 +21,3 @@ function findCrossingCells(
24
21
 
25
22
 
26
23
  export { findCrossingCells }
27
-
28
-
29
- // Quokka tests
30
- // import { createEmptyGrid } from "./create-empty-grid";
31
- // // const g = createEmptyGrid(0);
32
- // const g = createEmptyGrid(0);
33
- // // g[0][0].lineSegs.push([[]]);
34
- // g[2][0].lineSegs.push([[]]);
35
- // g[2][2].lineSegs.push([[]]);
36
- // findCrossingCells(g);
37
- // let r = 2;
38
- // // g.map(r => r.map(c => c.crossingCells))[r].map(v => v);//?
39
- // g.map(r => r.map(c => c.crossingCells))[r].map(v => v.map(r => r[1]));//?
40
- // g;//?