Skip to content

Commit 0c042db

Browse files
committed
Improve checking for zero-length path segments and don't allocate geometry
1 parent 4467614 commit 0c042db

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

src/path.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,23 @@ export class Path {
108108
* @returns BufferGeometry representing the path
109109
*/
110110
geometry(opts: { extrusionWidthOverride?: number; lineHeightOverride?: number } = {}): BufferGeometry {
111-
if (this._vertices.length < 3) {
112-
return new BufferGeometry();
111+
if (this._vertices.length < 6) {
112+
// a path needs at least 2 points to be valid
113+
console.warn('Path has less than 6 points, returning empty geometry');
114+
return null;
115+
}
116+
117+
// check for zero length paths
118+
// do this check for each segment
119+
for (let i = 0; i < this._vertices.length - 3; i += 3) {
120+
const dx = this._vertices[i] - this._vertices[i + 3];
121+
const dy = this._vertices[i + 1] - this._vertices[i + 4];
122+
const dz = this._vertices[i + 2] - this._vertices[i + 5];
123+
const distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
124+
if (distance < 0.0001) {
125+
console.warn('Path has zero length, skipping');
126+
return null;
127+
}
113128
}
114129

115130
return new ExtrusionGeometry(

src/webgl-preview.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,9 @@ export class WebGLPreview {
867867
extrusionWidthOverride: this.extrusionWidth,
868868
lineHeightOverride: this.lineHeight
869869
});
870+
871+
if (!geometry) return;
872+
870873
this.disposables.push(geometry);
871874
geometries.push(geometry);
872875
});

0 commit comments

Comments
 (0)