Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions packages/fragments/src/Utils/shells/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,19 @@ export class GeomsFbUtils {
// It's 65535, but we leave some margin
static ushortMaxValue = 65000;

static round(value: number, precission: number) {
return Math.round(value * precission) / precission;
static round(value: number, precision: number) {
return Math.round(value * precision) / precision;
}

static getAABB(vertices: Float32Array | number[]) {
static floor(value: number, precision: number) {
return Math.floor(value * precision) / precision;
}

static ceil(value: number, precision: number) {
return Math.ceil(value * precision) / precision;
}

static getAABB(vertices: Float32Array | number[], precision?: number) {
let minX = Number.POSITIVE_INFINITY;
let minY = Number.POSITIVE_INFINITY;
let minZ = Number.POSITIVE_INFINITY;
Expand All @@ -90,10 +98,31 @@ export class GeomsFbUtils {
if (z > maxZ) maxZ = z;
}

return {
min: { x: minX, y: minY, z: minZ },
max: { x: maxX, y: maxY, z: maxZ },
};
return precision
? {
min: {
x: this.floor(minX, precision),
y: this.floor(minY, precision),
z: this.floor(minZ, precision),
},
max: {
x: this.ceil(maxX, precision),
y: this.ceil(maxY, precision),
z: this.ceil(maxZ, precision),
},
}
: {
min: {
x: minX,
y: minY,
z: minZ,
},
max: {
x: maxX,
y: maxY,
z: maxZ,
},
};
}

static transformFromMatrix(
Expand Down Expand Up @@ -250,14 +279,20 @@ export class GeomsFbUtils {
) {
const pointsMap = new Map<string, number[]>();
const profiles = new Map<number, number[]>();
const { precision } = settings;

const getPointIndex = (x: number, y: number, z: number) => {
const key = `${x},${y},${z}`;
if (pointsMap.has(key)) {
return pointsMap.get(key)![0];
}
const index = pointsMap.size;
pointsMap.set(key, [index, x, y, z]);
pointsMap.set(key, [
index,
this.round(x, precision),
this.round(y, precision),
this.round(z, precision),
]);
return index;
};

Expand Down Expand Up @@ -329,7 +364,7 @@ export class GeomsFbUtils {
const vertexCount = position.length / 3;
const tooBigToShell = vertexCount > threshold;

const bbox = this.getAABB(position);
const bbox = this.getAABB(position, precision);

if (
bbox.min.x === 0 &&
Expand Down
6 changes: 3 additions & 3 deletions packages/fragments/src/Utils/shells/plane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ export class Plane {

faces: number[] = [];

constructor(plane: THREE.Plane, precission: number, normalPrecision: number) {
// Normals are smaller, so increase precission to avoid problems with almost coplanar faces
constructor(plane: THREE.Plane, precision: number, normalPrecision: number) {
// Normals are smaller, so increase precision to avoid problems with almost coplanar faces
const nx = GeomsFbUtils.round(plane.normal.x, normalPrecision);
const ny = GeomsFbUtils.round(plane.normal.y, normalPrecision);
const nz = GeomsFbUtils.round(plane.normal.z, normalPrecision);
const c = GeomsFbUtils.round(plane.constant, precission);
const c = GeomsFbUtils.round(plane.constant, precision);

this.normal = new THREE.Vector3(nx, ny, nz);
this.constant = c;
Expand Down
8 changes: 4 additions & 4 deletions packages/fragments/src/Utils/shells/point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export class Point {
vertices: Float32Array,
index: number,
id: number,
precission: number,
precision: number,
) {
this.x = GeomsFbUtils.round(vertices[index * 3], precission);
this.y = GeomsFbUtils.round(vertices[index * 3 + 1], precission);
this.z = GeomsFbUtils.round(vertices[index * 3 + 2], precission);
this.x = GeomsFbUtils.round(vertices[index * 3], precision);
this.y = GeomsFbUtils.round(vertices[index * 3 + 1], precision);
this.z = GeomsFbUtils.round(vertices[index * 3 + 2], precision);
this.hash = `${this.x}/${this.y}/${this.z}`;
this.id = id;
}
Expand Down
10 changes: 5 additions & 5 deletions packages/fragments/src/Utils/shells/points.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ export class Points {
tempV2 = new THREE.Vector3();
tempV3 = new THREE.Vector3();

precission: number;
precision: number;

constructor(precission: number) {
this.precission = precission;
constructor(precision: number) {
this.precision = precision;
}

create(vertices: Float32Array, index: number) {
const point = new Point(vertices, index, this.list.size, this.precission);
const point = new Point(vertices, index, this.list.size, this.precision);
if (!this.list.has(point.hash)) {
this.list.set(point.hash, point);
}
Expand Down Expand Up @@ -50,7 +50,7 @@ export class Points {
position[index3 * 3 + 2],
);

const pointPrecision = (1 / this.precission) * 10;
const pointPrecision = (1 / this.precision) * 10;
const d1Valid = this.tempV1.distanceTo(this.tempV2) > pointPrecision;
const d2Valid = this.tempV1.distanceTo(this.tempV3) > pointPrecision;
const d3Valid = this.tempV2.distanceTo(this.tempV3) > pointPrecision;
Expand Down