@bitbybit-dev/manifold
Version:
Bit By Bit Developers Manifold based CAD Library to Program Geometry
76 lines (75 loc) • 3.02 kB
JavaScript
import { CrossSectionShapes } from "./cross-section-shapes";
import { CrossSectionOperations } from "./cross-section-operations";
import { CrossSectionTransforms } from "./cross-section-transforms";
import { CrossSectionBooleans } from "./cross-section-booleans";
import { CrossSectionEvaluate } from "./cross-section-evaluate";
export class CrossSection {
constructor(wasm, base) {
this.manifold = wasm;
this.base = base;
this.shapes = new CrossSectionShapes(wasm);
this.operations = new CrossSectionOperations(wasm);
this.transforms = new CrossSectionTransforms(wasm);
this.booleans = new CrossSectionBooleans(wasm);
this.evaluate = new CrossSectionEvaluate(wasm);
}
crossSectionFromPoints(inputs) {
let points = inputs.points;
// Remove consecutive duplicates if requested
if (inputs.removeDuplicates) {
points = this.base.point.removeConsecutiveDuplicates({
points,
checkFirstAndLast: true,
tolerance: inputs.tolerance || 1e-7
});
}
// Convert Base.Point3[] to SimplePolygon (array of 2D points [x, y])
const polygon = points.map(p => [p[0], p[1]]);
return this.manifold.CrossSection.ofPolygons([polygon], inputs.fillRule);
}
crossSectionFromPolygons(inputs) {
let polygonPoints = inputs.polygonPoints;
// Remove consecutive duplicates from each polygon if requested
if (inputs.removeDuplicates) {
polygonPoints = polygonPoints.map(polygon => this.base.point.removeConsecutiveDuplicates({
points: polygon,
checkFirstAndLast: true,
tolerance: inputs.tolerance || 1e-7
}));
}
// Convert Base.Point3[][] to SimplePolygon[] (array of 2D points [x, y])
const polygons = polygonPoints.map(polygon => polygon.map(p => [p[0], p[1]]));
return this.manifold.CrossSection.ofPolygons(polygons, inputs.fillRule);
}
crossSectionToPolygons(inputs) {
return inputs.crossSection.toPolygons();
}
crossSectionToPoints(inputs) {
const polygons = inputs.crossSection.toPolygons();
return polygons.map(polygon => polygon.map(point => [point[0], point[1], 0]));
}
crossSectionsToPolygons(inputs) {
return inputs.crossSections.map((crossSection) => {
return this.crossSectionToPolygons({
crossSection
});
});
}
crossSectionsToPoints(inputs) {
return inputs.crossSections.map((crossSection) => {
return this.crossSectionToPoints({
crossSection
});
});
}
deleteCrossSection(inputs) {
inputs.crossSection.delete();
}
deleteCrossSections(inputs) {
inputs.crossSections.forEach((crossSection) => {
return this.deleteCrossSection({
crossSection
});
});
}
}