@woosh/meep-engine 2.49.9 → 2.50.0

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 (141) hide show
  1. package/editor/actions/concrete/ArrayCopyAction.js +1 -1
  2. package/package.json +1 -1
  3. package/src/core/binary/BinaryBuffer.js +1 -1
  4. package/src/core/binary/BinaryBuffer.spec.js +128 -0
  5. package/src/core/binary/int32_to_binary_string.js +4 -1
  6. package/src/core/binary/int32_to_binary_string.spec.js +9 -0
  7. package/src/core/bvh2/BinaryNode.js +0 -30
  8. package/src/core/bvh2/binary/2/BinaryUint32BVH.js +1 -1
  9. package/src/core/bvh2/binary/IndexedBinaryBVH.js +1 -1
  10. package/src/core/bvh2/bvh3/ExplicitBinaryBoundingVolumeHierarchy.js +1 -1
  11. package/src/core/bvh2/bvh3/query/compute_tight_near_far_clipping_planes.js +1 -1
  12. package/src/core/cache/Cache.js +31 -29
  13. package/src/core/cache/Cache.spec.js +4 -1
  14. package/src/core/collection/HashMap.js +1 -1
  15. package/src/core/collection/array/{copyArray.js → array_copy.js} +1 -24
  16. package/src/core/collection/array/array_copy_entire.js +21 -0
  17. package/src/core/collection/array/typed/typed_array_copy.js +1 -1
  18. package/src/core/collection/queue/Deque.d.ts +4 -0
  19. package/src/core/collection/queue/Deque.js +5 -7
  20. package/src/core/collection/queue/Deque.spec.js +107 -0
  21. package/src/core/collection/table/RowFirstTable.js +1 -1
  22. package/src/core/geom/2d/aabb/AABB2.d.ts +14 -0
  23. package/src/core/geom/2d/aabb/AABB2.js +9 -7
  24. package/src/core/geom/2d/aabb/AABB2.spec.js +100 -0
  25. package/src/core/geom/2d/aabb/aabb2_compute_center_from_multiple.spec.js +11 -0
  26. package/src/core/geom/2d/aabb/aabb2_compute_overlap.spec.js +56 -0
  27. package/src/core/geom/2d/aabb/aabb2_contains.spec.js +40 -0
  28. package/src/core/geom/2d/bvh/Node2.js +1 -1
  29. package/src/core/geom/2d/convex-hull/fixed_convex_hull_humus.js +1 -1
  30. package/src/core/geom/2d/convex-hull/fixed_convex_hull_relaxation.js +1 -1
  31. package/src/core/geom/2d/line/line_segment_compute_line_segment_intersection_2d.js +35 -0
  32. package/src/core/geom/2d/line/line_segment_compute_line_segment_intersection_array_2d.js +51 -0
  33. package/src/core/geom/2d/line/line_segment_compute_line_segment_intersection_vectors_2d.js +15 -0
  34. package/src/core/geom/2d/line/line_segment_compute_line_segment_intersection_vectors_array_2d.js +30 -0
  35. package/src/core/geom/2d/line/line_segment_line_segment_intersection_exists_2d.js +29 -0
  36. package/src/core/geom/3d/aabb/AABB3.d.ts +4 -0
  37. package/src/core/geom/3d/aabb/AABB3.spec.js +30 -0
  38. package/src/core/geom/3d/matrix/m4_make_translation.js +1 -1
  39. package/src/core/geom/3d/sphere/harmonics/sh3_dering_optimize_positive.js +1 -1
  40. package/src/core/geom/3d/tetrahedra/TetrahedralMesh.js +1 -1
  41. package/src/core/geom/3d/tetrahedra/compute_circumsphere.js +1 -1
  42. package/src/core/geom/3d/tetrahedra/delaunay/fill_in_a_cavity.js +1 -1
  43. package/src/core/geom/3d/topology/bounds/computeTriangleClusterNormalBoundingCone.js +1 -1
  44. package/src/core/geom/3d/topology/expandConnectivityByLocality.js +1 -1
  45. package/src/core/geom/3d/topology/struct/binary/BinaryTopology.js +1 -1
  46. package/src/core/geom/Quaternion.d.ts +11 -0
  47. package/src/core/geom/Quaternion.js +36 -27
  48. package/src/core/geom/Quaternion.spec.js +141 -0
  49. package/src/core/geom/Vector2.d.ts +5 -1
  50. package/src/core/geom/Vector2.js +24 -0
  51. package/src/core/geom/Vector3.d.ts +4 -0
  52. package/src/core/geom/Vector3.spec.js +60 -0
  53. package/src/core/graph/GraphUtils.js +4 -2
  54. package/src/core/graph/layout/CircleLayout.js +4 -2
  55. package/src/core/math/vector_nd_dot.js +16 -0
  56. package/src/core/math/{normalizeArrayVector.js → vector_nd_normalize.js} +3 -3
  57. package/src/core/math/{normalizeArrayVector.spec.js → vector_nd_normalize.spec.js} +3 -3
  58. package/src/core/process/PromiseWatcher.spec.js +1 -1
  59. package/src/engine/animation/curve/compression/downsample_float_array_curve_by_error.js +1 -1
  60. package/src/engine/ecs/EntityManager.js +1 -205
  61. package/src/engine/ecs/fow/FogOfWar.js +1 -1
  62. package/src/engine/ecs/guid/GUID.js +1 -1
  63. package/src/engine/ecs/terrain/ecs/splat/SplatMapping.js +1 -1
  64. package/src/engine/ecs/terrain/overlay/TerrainOverlay.js +1 -1
  65. package/src/engine/ecs/terrain/tiles/TerrainTile.js +1 -1
  66. package/src/engine/ecs/transform/Transform.d.ts +2 -0
  67. package/src/engine/ecs/transform/Transform.spec.js +63 -0
  68. package/src/engine/ecs/transform-attachment/TransformAttachment.d.ts +17 -1
  69. package/src/engine/ecs/transform-attachment/TransformAttachment.js +12 -2
  70. package/src/engine/ecs/transform-attachment/TransformAttachment.spec.js +103 -0
  71. package/src/engine/graphics/ecs/path/tube/build/computeFrenetFrames.js +1 -1
  72. package/src/engine/graphics/geometry/MikkT/GenerateTSpaces.js +1 -1
  73. package/src/engine/graphics/geometry/MikkT/m_getNormal.js +1 -1
  74. package/src/engine/graphics/geometry/MikkT/m_getTexCoord.js +1 -1
  75. package/src/engine/graphics/geometry/bvh/buffered/BVHGeometryRaycaster.js +1 -1
  76. package/src/engine/graphics/geometry/instancing/InstancedMeshGroup.js +1 -1
  77. package/src/engine/graphics/geometry/optimization/merge/merge_geometry_hierarchy.js +1 -1
  78. package/src/engine/graphics/impostors/octahedral/util/build_cutout_from_atlas_by_alpha.js +1 -1
  79. package/src/engine/graphics/particles/particular/engine/utils/volume/AttributeValue.js +1 -1
  80. package/src/engine/graphics/render/Lines.js +1 -1
  81. package/src/engine/graphics/render/buffer/simple-fx/taa/TemporalSupersamplingRenderPlugin.js +1 -1
  82. package/src/engine/graphics/render/forward_plus/LightManager.js +1 -1
  83. package/src/engine/graphics/render/forward_plus/model/Decal.js +1 -1
  84. package/src/engine/graphics/render/forward_plus/prototype/prototypeLightManager.js +1 -1
  85. package/src/engine/graphics/render/gizmo/GizmoShapeRenderingInterface.js +1 -1
  86. package/src/engine/graphics/render/view/CameraView.js +1 -1
  87. package/src/engine/graphics/render/visibility/hiz/query/BatchOcclusionQuery.js +1 -1
  88. package/src/engine/graphics/render/webgpu/sample/MeshInstance.js +1 -1
  89. package/src/engine/graphics/sh3/LightProbeVolume.js +1 -1
  90. package/src/engine/graphics/sh3/path_tracer/PathTracedMesh.js +1 -1
  91. package/src/engine/graphics/sh3/path_tracer/PathTracer.js +1 -1
  92. package/src/engine/graphics/sh3/path_tracer/make_sky_hosek.js +1 -1
  93. package/src/engine/graphics/sh3/prototypeSH3Probe.js +1 -1
  94. package/src/engine/graphics/texture/3d/scs3d_read_2d_slice.js +1 -1
  95. package/src/engine/graphics/texture/CanvasClone.js +5 -1
  96. package/src/engine/graphics/texture/sampler/Sampler2D.js +14 -75
  97. package/src/engine/graphics/texture/sampler/bicubic.js +19 -19
  98. package/src/engine/graphics/texture/sampler/convertSampler2D2DataURL.spec.js +10 -0
  99. package/src/engine/graphics/texture/sampler/copy_Sampler2D_channel_data.spec.js +90 -0
  100. package/src/engine/graphics/texture/sampler/differenceSampler.js +13 -8
  101. package/src/engine/graphics/texture/sampler/distance/computeSignedDistanceField_Chamfer.js +140 -0
  102. package/src/engine/graphics/texture/sampler/distance/computeSignedDistanceField_NaiveFlood.js +130 -0
  103. package/src/engine/graphics/texture/sampler/distance/computeUnsignedDistanceField.js +10 -0
  104. package/src/engine/graphics/texture/sampler/distance/computeUnsignedDistanceField.spec.js +183 -0
  105. package/src/engine/graphics/texture/sampler/distance/computeUnsignedDistanceField_Chamfer.js +133 -0
  106. package/src/engine/graphics/texture/sampler/filter/mitchell.js +4 -0
  107. package/src/engine/graphics/texture/sampler/loadSampler2D.js +5 -2
  108. package/src/engine/graphics/texture/sampler/resize/sampler2d_downsample_mipmap.js +8 -3
  109. package/src/engine/graphics/texture/sampler/resize/sampler2d_downsample_mipmap.spec.js +13 -0
  110. package/src/engine/graphics/texture/sampler/sampler2d_channel_compute_min_indices.js +58 -0
  111. package/src/engine/graphics/trail/TemporalPath.js +0 -36
  112. package/src/engine/intelligence/behavior/composite/ParallelBehavior.spec.js +12 -12
  113. package/src/engine/intelligence/behavior/composite/SequenceBehavior.spec.js +17 -0
  114. package/src/engine/intelligence/behavior/primitive/SucceedingBehavior.js +2 -0
  115. package/src/engine/knowledge/database/StaticKnowledgeDataTable.d.ts +7 -1
  116. package/src/engine/knowledge/database/StaticKnowledgeDataTable.spec.js +21 -0
  117. package/src/engine/knowledge/database/StaticKnowledgeDataTableDescriptor.d.ts +2 -2
  118. package/src/engine/logging/ConsoleLoggerBackend.js +4 -0
  119. package/src/engine/logging/VoidLoggerBackend.js +12 -0
  120. package/src/engine/navigation/ecs/components/computeCatmullRomSpline.js +1 -1
  121. package/src/engine/navigation/ecs/components/computeCatmullRomSplineUniformDistance.js +1 -1
  122. package/src/engine/physics/cannon/CannonJSPhysicsSystem.js +1 -1
  123. package/src/engine/save/GameStateLoader.js +1 -1
  124. package/src/engine/scene/Scene.d.ts +2 -0
  125. package/src/engine/scene/Scene.js +2 -2
  126. package/src/engine/scene/Scene.spec.js +20 -0
  127. package/src/engine/scene/SceneManager.d.ts +4 -0
  128. package/src/engine/scene/SceneManager.js +46 -23
  129. package/src/engine/scene/SceneManager.spec.js +107 -0
  130. package/src/engine/sound/material/detector/terrain/TerrainSoundMaterialSurfaceDetector.js +2 -2
  131. package/src/engine/ui/GUIEngine.js +1 -1
  132. package/src/generation/grid/generation/util/buildUnsignedDistanceField.js +3 -1
  133. package/src/generation/markers/MarkerNode.js +2 -2
  134. package/src/generation/theme/AreaMask.js +3 -1
  135. package/src/view/elements/progress/RectangularPieProgressView.js +8 -6
  136. package/src/core/geom/2d/LineSegment2.js +0 -175
  137. package/src/core/geom/Matrix4.js +0 -275
  138. package/src/engine/graphics/texture/sampler/distanceField.js +0 -411
  139. package/src/engine/graphics/texture/sampler/distanceField.spec.js +0 -184
  140. package/src/engine/physics/cannon/cannon.min.js +0 -27
  141. /package/src/engine/physics/spring/{Spring.js → computeHookeForce.js} +0 -0
@@ -1,275 +0,0 @@
1
- import Vector3 from "./Vector3.js";
2
-
3
- const _z = new Vector3();
4
- const _x = new Vector3();
5
- const _y = new Vector3();
6
-
7
- /**
8
- * @deprecated use arrays instead or mat4 from gl-matrix
9
- */
10
- export class Matrix4 {
11
- /**
12
- *
13
- * @constructor
14
- * @class
15
- */
16
- constructor() {
17
- console.warn("deprecated, use plain arrays or Float32Array instead")
18
-
19
- this.a0 = 1;
20
- this.a1 = 0;
21
- this.a2 = 0;
22
- this.a3 = 0;
23
-
24
- this.b0 = 0;
25
- this.b1 = 1;
26
- this.b2 = 0;
27
- this.b3 = 0;
28
-
29
- this.c0 = 0;
30
- this.c1 = 0;
31
- this.c2 = 1;
32
- this.c3 = 0;
33
-
34
- this.d0 = 0;
35
- this.d1 = 0;
36
- this.d2 = 0;
37
- this.d3 = 1;
38
- }
39
-
40
- /**
41
- *
42
- * @param {Vector3} eye
43
- * @param {Vector3} target
44
- * @param {Vector3} up
45
- */
46
- lookAt(eye, target, up) {
47
- // compute direction vector
48
- _z.subVectors(eye, target);
49
-
50
- const delta_length_sqr = _z.lengthSqr();
51
-
52
- if (delta_length_sqr === 0) {
53
- // eye and target are at the same place
54
- _z.set(0, 0, 1);
55
- }
56
-
57
- _z.normalize();
58
- _x.crossVectors(up, _z);
59
-
60
- if (_x.lengthSqr() === 0) {
61
- // up and direction are parallel
62
-
63
- // jitter direction a little
64
- if (Math.abs(up.z) === 1) {
65
- _z.x += 0.0001;
66
- } else {
67
- _z.z += 0.0001;
68
- }
69
-
70
- // recompute normalized direction and cross product
71
- _z.normalize();
72
-
73
- _x.crossVectors(up, _z);
74
- }
75
-
76
- _x.normalize();
77
-
78
- _y.crossVectors(_z, _x);
79
-
80
- this.a0 = _x.x;
81
- this.a1 = _x.y;
82
- this.a2 = _x.z;
83
-
84
- this.b0 = _y.x;
85
- this.b1 = _y.y;
86
- this.b2 = _y.z;
87
-
88
- this.c0 = _z.x;
89
- this.c1 = _z.y;
90
- this.c2 = _z.z;
91
- }
92
-
93
- /**
94
- *
95
- * @returns {number}
96
- */
97
- determinant() {
98
-
99
- const n11 = this.a0;
100
- const n12 = this.b0;
101
- const n13 = this.c0;
102
- const n14 = this.d0;
103
-
104
- const n21 = this.a1;
105
- const n22 = this.b1;
106
- const n23 = this.c1;
107
- const n24 = this.d1;
108
-
109
- const n31 = this.a2;
110
- const n32 = this.b2;
111
- const n33 = this.c2;
112
- const n34 = this.d2;
113
-
114
- const n41 = this.a3;
115
- const n42 = this.b3;
116
- const n43 = this.c3;
117
- const n44 = this.d3;
118
-
119
- //TODO: make this more efficient
120
- //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
121
-
122
- return (
123
- n41 * (
124
- +n14 * n23 * n32
125
- - n13 * n24 * n32
126
- - n14 * n22 * n33
127
- + n12 * n24 * n33
128
- + n13 * n22 * n34
129
- - n12 * n23 * n34
130
- ) +
131
- n42 * (
132
- +n11 * n23 * n34
133
- - n11 * n24 * n33
134
- + n14 * n21 * n33
135
- - n13 * n21 * n34
136
- + n13 * n24 * n31
137
- - n14 * n23 * n31
138
- ) +
139
- n43 * (
140
- +n11 * n24 * n32
141
- - n11 * n22 * n34
142
- - n14 * n21 * n32
143
- + n12 * n21 * n34
144
- + n14 * n22 * n31
145
- - n12 * n24 * n31
146
- ) +
147
- n44 * (
148
- -n13 * n22 * n31
149
- - n11 * n23 * n32
150
- + n11 * n22 * n33
151
- + n13 * n21 * n32
152
- - n12 * n21 * n33
153
- + n12 * n23 * n31
154
- )
155
-
156
- );
157
-
158
- }
159
-
160
- /**
161
- *
162
- * @param {Vector3} position
163
- * @param {Quaternion} rotation
164
- * @param {Vector3} scale
165
- */
166
- compose(position, rotation, scale) {
167
- var x = rotation.x, y = rotation.y, z = rotation.z, w = rotation.w;
168
-
169
- var x2 = x + x, y2 = y + y, z2 = z + z;
170
- var xx = x * x2, xy = x * y2, xz = x * z2;
171
- var yy = y * y2, yz = y * z2, zz = z * z2;
172
- var wx = w * x2, wy = w * y2, wz = w * z2;
173
-
174
- var sx = scale.x, sy = scale.y, sz = scale.z;
175
-
176
- this.a0 = (1 - (yy + zz)) * sx;
177
- this.a1 = (xy + wz) * sx;
178
- this.a2 = (xz - wy) * sx;
179
- this.a3 = 0;
180
-
181
- this.b0 = (xy - wz) * sy;
182
- this.b1 = (1 - (xx + zz)) * sy;
183
- this.b2 = (yz + wx) * sy;
184
- this.b3 = 0;
185
-
186
- this.c0 = (xz + wy) * sz;
187
- this.c1 = (yz - wx) * sz;
188
- this.c2 = (1 - (xx + yy)) * sz;
189
- this.c3 = 0;
190
-
191
- this.d0 = position.x;
192
- this.d1 = position.y;
193
- this.d2 = position.z;
194
- this.d3 = 1;
195
- }
196
-
197
- decompose(position, rotation, scale) {
198
-
199
-
200
- let sx = Math.hypot(this.a0, this.a1, this.a2);
201
- const sy = Math.hypot(this.b0, this.b1, this.b2);
202
- const sz = Math.hypot(this.c0, this.c1, this.c2);
203
-
204
- // if determine is negative, we need to invert one scale
205
- const det = this.determinant();
206
- if (det < 0) sx = -sx;
207
-
208
- position.set(
209
- this.d0,
210
- this.d1,
211
- this.d2
212
- );
213
-
214
- // scale the rotation part
215
- const invSX = 1 / sx;
216
- const invSY = 1 / sy;
217
- const invSZ = 1 / sz;
218
-
219
- const i_a0 = this.a0 * invSX;
220
- const i_a1 = this.a1 * invSX;
221
- const i_a2 = this.a2 * invSX;
222
-
223
- const i_b0 = this.b0 * invSY;
224
- const i_b1 = this.b1 * invSY;
225
- const i_b2 = this.b2 * invSY;
226
-
227
- const i_c0 = this.c0 * invSZ;
228
- const i_c1 = this.c1 * invSZ;
229
- const i_c2 = this.c2 * invSZ;
230
-
231
- rotation.__setFromRotationMatrix(i_a0, i_b0, i_c0, i_a1, i_b1, i_c1, i_a2, i_b2, i_c2);
232
-
233
- scale.set(sx, sy, sz);
234
- }
235
-
236
- /**
237
- *
238
- * @param {Matrix4} a
239
- * @param {Matrix4} b
240
- */
241
- multiplyMatrices(a, b) {
242
-
243
- const a11 = a.a0, a12 = a.b0, a13 = a.c0, a14 = a.d0;
244
- const a21 = a.a1, a22 = a.b1, a23 = a.c1, a24 = a.d1;
245
- const a31 = a.a2, a32 = a.b2, a33 = a.c2, a34 = a.d2;
246
- const a41 = a.a3, a42 = a.b3, a43 = a.c3, a44 = a.d3;
247
-
248
- const b11 = b.a0, b12 = b.b0, b13 = b.c0, b14 = b.d0;
249
- const b21 = b.a1, b22 = b.b1, b23 = b.c1, b24 = b.d1;
250
- const b31 = b.a2, b32 = b.b2, b33 = b.c2, b34 = b.d2;
251
- const b41 = b.a3, b42 = b.b3, b43 = b.c3, b44 = b.d3;
252
-
253
- this.a0 = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
254
- this.b0 = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
255
- this.c0 = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
256
- this.d0 = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
257
-
258
- this.a1 = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
259
- this.b1 = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
260
- this.c1 = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
261
- this.d1 = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
262
-
263
- this.a2 = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
264
- this.b2 = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
265
- this.c2 = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
266
- this.d2 = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
267
-
268
- this.a3 = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
269
- this.b3 = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
270
- this.c3 = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
271
- this.d3 = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
272
-
273
- }
274
-
275
- }
@@ -1,411 +0,0 @@
1
- import { BitSet } from "../../../../core/binary/BitSet.js";
2
- import { assert } from "../../../../core/assert.js";
3
- import { clamp } from "../../../../core/math/clamp.js";
4
- import { max2 } from "../../../../core/math/max2.js";
5
- import { min2 } from "../../../../core/math/min2.js";
6
-
7
- /**
8
- * Naive flood implementation of a distance field computation algorithm
9
- * @author Alex Goldring Dec 2018
10
- * @param {Sampler2D} source
11
- * @param {Sampler2D} destination
12
- * @param {number} emptyValue
13
- */
14
- export function computeSignedDistanceField_(source, destination, emptyValue) {
15
- assert.equal(source.itemSize, 1, `unsupported source.itemSize, expected 1, got '${source.itemSize}'`);
16
- assert.typeOf(emptyValue, 'number', 'emptyValue');
17
-
18
- let i, j;
19
-
20
- const distanceData = destination.data;
21
-
22
- const visited = new BitSet();
23
-
24
- const openSet = new BitSet();
25
-
26
- //use "flood" algorithm
27
-
28
- //mark all visible tiles as visited
29
- const samplerData = source.data;
30
- const totalCellCount = samplerData.length;
31
-
32
- function traverseNeighbours(index, visitor) {
33
- let i = index - (width + 1);
34
-
35
- const top = index > width;
36
- const bottom = index < (totalCellCount - width);
37
-
38
- const x = index % width;
39
-
40
- const left = x > 0;
41
- const right = x < width - 1;
42
-
43
- if (top) {
44
- if (left) {
45
- visitor(i);
46
- }
47
-
48
- visitor(i + 1);
49
-
50
- if (right) {
51
- visitor(i + 2);
52
- }
53
- }
54
-
55
- i += width;
56
-
57
- if (left) {
58
- visitor(i);
59
- }
60
-
61
- if (right) {
62
- visitor(i + 2);
63
- }
64
-
65
- i += width;
66
-
67
- if (bottom) {
68
- if (left) {
69
- visitor(i);
70
- }
71
-
72
- visitor(i + 1);
73
-
74
- if (right) {
75
- visitor(i + 2);
76
- }
77
- }
78
-
79
- }
80
-
81
- const width = destination.width;
82
-
83
- for (i = 0; i < totalCellCount; i++) {
84
- const sampleValue = samplerData[i];
85
- if (sampleValue !== emptyValue) {
86
- visited.set(i, true);
87
- //write distance data
88
- distanceData[i] = 0;
89
- } else {
90
- distanceData[i] = 255;
91
- }
92
- }
93
-
94
- //populate initial open set
95
- for (i = visited.nextSetBit(0); i !== -1; i = visited.nextSetBit(i + 1)) {
96
- j = 0;
97
- traverseNeighbours(i, function (neighbourIndex) {
98
- if (!visited.get(neighbourIndex)) {
99
- //increment number of not visited
100
- j++;
101
- }
102
- });
103
-
104
- if (j === 0) {
105
- //all neighbours are visited, we can safely ignore this cell
106
- } else {
107
- openSet.set(i, true);
108
- }
109
- }
110
-
111
- for (i = openSet.nextSetBit(0); i !== -1; i = openSet.nextSetBit(0)) {
112
- //remove from open set
113
- openSet.set(i, false);
114
-
115
- const value = distanceData[i];
116
-
117
- traverseNeighbours(i, function (neighbourIndex) {
118
- const neighbourValue = value + 1;
119
-
120
- if (visited.get(neighbourIndex)) {
121
- if (distanceData[neighbourIndex] <= neighbourValue) {
122
- return;
123
- }
124
- } else {
125
- visited.set(neighbourIndex, true);
126
- }
127
-
128
- distanceData[neighbourIndex] = neighbourValue;
129
- //add neighbour to open set
130
- openSet.set(neighbourIndex, true);
131
- });
132
- }
133
- }
134
-
135
- /**
136
- * @param {Sampler2D} source
137
- * @param {Sampler2D} distanceField
138
- * @param {number} emptyValue
139
- */
140
- export function computeUnsignedDistanceField(source, distanceField, emptyValue) {
141
- computeUnsignedDistanceField_Chamfer(source, distanceField, emptyValue, 1, 1, 255);
142
- }
143
-
144
- /**
145
- * algorithm proposed by Borgefors, Chamfer distance [J. ACM 15 (1968) 600, Comput. Vis. Graph. Image Process. 34 (1986) 344], h
146
- * @param {Sampler2D} source
147
- * @param {Sampler2D} distanceField
148
- * @param {number} emptyValue
149
- * @param {number} d1 distance between two adjacent pixels in either x or y direction
150
- * @param {number} d2 distance between two diagonally adjacent pixels
151
- * @param {number} maxD highest value that distance field can hold
152
- */
153
- export function computeUnsignedDistanceField_Chamfer(source, distanceField, emptyValue, d1, d2, maxD) {
154
- const sourceData = source.data;
155
- const distanceFieldData = distanceField.data;
156
-
157
- const width = source.width;
158
- const height = source.height;
159
-
160
- const maxX = width - 1;
161
- const maxY = height - 1;
162
-
163
- let x, y, i, v;
164
-
165
- //initialize distance field
166
- const dataSize = height * width;
167
- for (i = 0; i < dataSize; i++) {
168
- if (sourceData[i] !== emptyValue) {
169
- distanceFieldData[i] = 0;
170
- } else {
171
- distanceFieldData[i] = maxD;
172
- }
173
- }
174
-
175
- //first pass (forward)
176
- for (y = 0; y < height; y++) {
177
-
178
- const y_m1 = max2(y - 1, 0);
179
-
180
- const y_w = y * width;
181
-
182
- const y_m1_w = y_m1 * width;
183
-
184
- for (x = 0; x < width; x++) {
185
-
186
- i = y_w + x;
187
-
188
- v = distanceFieldData[i];
189
-
190
- const x_m1 = max2(x - 1, 0);
191
- const x_p1 = min2(x + 1, maxX);
192
-
193
- const v0_i = x_m1 + y_m1_w;
194
- const v0 = distanceFieldData[v0_i] + d2;
195
-
196
- if (v0 < v) {
197
- distanceFieldData[i] = v0;
198
- v = v0;
199
- }
200
-
201
- const v1_i = x + y_m1_w;
202
- const v1 = distanceFieldData[v1_i] + d1;
203
-
204
- if (v1 < v) {
205
- distanceFieldData[i] = v1;
206
- v = v1;
207
- }
208
-
209
- const v2_i = x_p1 + y_m1_w;
210
- const v2 = distanceFieldData[v2_i] + d2;
211
-
212
- if (v2 < v) {
213
- distanceFieldData[i] = v2;
214
- v = v2;
215
- }
216
-
217
- const v3_i = x_m1 + y_w;
218
- const v3 = distanceFieldData[v3_i] + d1;
219
-
220
- if (v3 < v) {
221
- distanceFieldData[i] = v3;
222
- }
223
- }
224
- }
225
-
226
- //second pass (backward)
227
- for (y = maxY; y >= 0; y--) {
228
- const y_w = y * width;
229
-
230
- const y_p1 = min2(y + 1, maxY);
231
- const y_p1_w = y_p1 * width;
232
-
233
- for (x = maxX; x >= 0; x--) {
234
- const x_p1 = min2(x + 1, maxX);
235
- const x_m1 = max2(x - 1, 0);
236
-
237
- i = y_w + x;
238
-
239
- v = distanceFieldData[i];
240
-
241
- const v0_i = x_p1 + y_w;
242
- const v0 = distanceFieldData[v0_i] + d1;
243
-
244
- if (v0 < v) {
245
- distanceFieldData[i] = v0;
246
- v = v0;
247
- }
248
-
249
- const v1_i = x_m1 + y_p1_w;
250
- const v1 = distanceFieldData[v1_i] + d2;
251
-
252
- if (v1 < v) {
253
- distanceFieldData[i] = v1;
254
- v = v1;
255
- }
256
-
257
- const v2_i = x + y_p1_w;
258
- const v2 = distanceFieldData[v2_i] + d1;
259
-
260
- if (v2 < v) {
261
- distanceFieldData[i] = v2;
262
- v = v2;
263
- }
264
-
265
- const v3_i = x_p1 + y_p1_w;
266
- const v3 = distanceFieldData[v3_i] + d2;
267
-
268
- if (v3 < v) {
269
- distanceFieldData[i] = v3;
270
- }
271
- }
272
- }
273
- }
274
-
275
- /**
276
- * algorithm proposed by Borgefors, Chamfer distance [J. ACM 15 (1968) 600, Comput. Vis. Graph. Image Process. 34 (1986) 344], h
277
- * @param {Sampler2D} source
278
- * @param {Sampler2D} distanceField
279
- * @param {number} emptyValue
280
- * @param {number} d1 distance between two adjacent pixels in either x or y direction
281
- * @param {number} d2 distance between two diagonally adjacent pixels
282
- * @param maxD
283
- */
284
- export function computeSignedDistanceField_Chamfer(source, distanceField, emptyValue, d1, d2, maxD) {
285
- const sourceData = source.data;
286
- const distanceFieldData = distanceField.data;
287
-
288
- const width = source.width;
289
- const height = source.height;
290
-
291
- const maxX = width - 1;
292
- const maxY = height - 1;
293
-
294
- function getS(x, y) {
295
- x = clamp(x, 0, maxX);
296
- y = clamp(y, 0, maxY);
297
-
298
- const index = x + y * width;
299
-
300
- return sourceData[index];
301
- }
302
-
303
- function getD(x, y) {
304
- x = clamp(x, 0, maxX);
305
- y = clamp(y, 0, maxY);
306
-
307
- const index = x + y * width;
308
-
309
- return distanceFieldData[index];
310
- }
311
-
312
- function setD(x, y, v) {
313
- x = clamp(x, 0, maxX);
314
- y = clamp(y, 0, maxY);
315
-
316
- const index = x + y * width;
317
-
318
- distanceFieldData[index] = min2(v, maxD);
319
- }
320
-
321
- let x, y;
322
-
323
- //initialize distance field
324
- for (y = 0; y < height; y++) {
325
- for (x = 0; x < width; x++) {
326
- if (
327
- getS(x - 1, y) !== getS(x, y)
328
- || getS(x + 1, y) !== getS(x, y)
329
- || getS(x, y - 1) !== getS(x, y)
330
- || getS(x, y + 1) !== getS(x, y)
331
- ) {
332
- setD(x, y, 0);
333
- } else {
334
- setD(x, y, 255);
335
- }
336
- }
337
- }
338
-
339
- //first pass (forward)
340
- for (y = 0; y < height; y++) {
341
- for (x = 0; x < width; x++) {
342
-
343
- const v = getD(x, y);
344
-
345
- const v0 = getD(x - 1, y - 1) + d2;
346
-
347
- if (v0 < v) {
348
- setD(x, y, v0);
349
- }
350
-
351
- const v1 = getD(x, y - 1) + d1;
352
-
353
- if (v1 < v) {
354
- setD(x, y, v1);
355
- }
356
-
357
- const v2 = getD(x + 1, y - 1) + d2;
358
-
359
- if (v2 < v) {
360
- setD(x, y, v2);
361
- }
362
-
363
- const v3 = getD(x - 1, y) + d1;
364
-
365
- if (v3 < v) {
366
- setD(x, y, v3);
367
- }
368
- }
369
- }
370
-
371
- //second pass (backward)
372
- for (y = maxY; y >= 0; y--) {
373
- for (x = maxX; x >= 0; x--) {
374
- const v = getD(x, y);
375
-
376
- const v0 = getD(x + 1, y) + d1;
377
-
378
- if (v0 < v) {
379
- setD(x, y, v0);
380
- }
381
-
382
- const v1 = getD(x - 1, y + 1) + d2;
383
-
384
- if (v1 < v) {
385
- setD(x, y, v1);
386
- }
387
-
388
- const v2 = getD(x, y + 1) + d1;
389
-
390
- if (v2 < v) {
391
- setD(x, y, v2);
392
- }
393
-
394
- const v3 = getD(x + 1, y + 1) + d2;
395
-
396
- if (v3 < v) {
397
- setD(x, y, v3);
398
- }
399
- }
400
- }
401
-
402
- //indicate inside & outside
403
- for (y = 0; y < height; y++) {
404
- for (x = 0; x < width; x++) {
405
- if (getS(x, y) !== emptyValue) {
406
- //inside
407
- setD(x, y, -getD(x, y));
408
- }
409
- }
410
- }
411
- }