Skip to content

Commit 0fa7f17

Browse files
committed
fix: improve measure fill generation logic
1 parent 5bb654e commit 0fa7f17

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

packages/front/src/measurement/AreaMeasurement/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ export class AreaMeasurement extends Measurement<Area, "area"> {
9797
this._temp.lines.clear();
9898
});
9999

100-
this.onStateChanged.add(state => {
100+
this.onStateChanged.add((state) => {
101101
if (state.includes("rounding")) {
102-
this._temp.area.rounding = this.rounding
102+
this._temp.area.rounding = this.rounding;
103103
}
104104

105105
if (state.includes("units")) {
106-
this._temp.area.units = this.units
106+
this._temp.area.units = this.units;
107107
}
108-
})
108+
});
109109
}
110110

111111
private computeLineElements = () => {

packages/front/src/utils/measure-fill.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as THREE from "three";
22
import * as OBC from "@thatopen/components";
3+
// eslint-disable-next-line import/no-extraneous-dependencies
4+
import earcut from "earcut";
35
import { Area } from "./area";
46
import { MeasureMark } from "./measure-mark";
57

@@ -116,23 +118,34 @@ export class MeasureFill {
116118
private updateMesh() {
117119
if (this.area.points.size < 3) return;
118120

119-
const points = [...this.area.points];
121+
const points2D = this.area.points2D;
122+
if (!points2D || points2D.length < 3) return;
120123

121-
const vertices = points.flatMap((point) => [point.x, point.y, point.z]);
124+
// Flatten 2D points for earcut triangulation
125+
const flatPoints = points2D.flatMap((point) => [point.x, point.y]);
122126

123-
// Generate indices for the triangles (assuming the points are coplanar and ordered)
124-
const indices: number[] = [];
125-
for (let i = 1; i < points.length - 1; i++) {
126-
indices.push(0, i, i + 1); // Create triangles from the first point and subsequent pairs
127+
// Triangulate using earcut
128+
const indices = earcut(flatPoints);
129+
130+
// Convert 2D points back to 3D for the geometry
131+
const vertices: number[] = [];
132+
for (const point2D of points2D) {
133+
const point3D = this.area.convertPointTo3D(point2D);
134+
if (point3D) {
135+
vertices.push(point3D.x, point3D.y, point3D.z);
136+
}
127137
}
128138

129-
this.three.geometry.dispose();
139+
// Dispose old geometry if it exists
140+
if (this.three.geometry) {
141+
this.three.geometry.dispose();
142+
}
130143

144+
// Create new geometry with triangulated indices
131145
const geo = new THREE.BufferGeometry();
132146
geo.setAttribute("position", new THREE.Float32BufferAttribute(vertices, 3));
133147
geo.setIndex(indices);
134148
this.three.geometry = geo;
135-
136149
this.three.material = this.material;
137150
}
138151

0 commit comments

Comments
 (0)