react-three-nurbs
Version:
A React component library for NURBS (Non-Uniform Rational B-Spline) curves, surfaces, and solids in Three.js. Built with React Three Fiber, zero external NURBS dependencies — all math implemented from scratch. Boolean operations powered by OpenCASCADE WAS
97 lines (96 loc) • 3.67 kB
JavaScript
import { getOC as S } from "./index.js";
function M(e, n, a = 0.1) {
var f;
console.log("occtShapeToMesh: meshing with deflection", a);
const c = new e.BRepMesh_IncrementalMesh_2(n, a, !1, 0.5, !1);
console.log("occtShapeToMesh: mesher created, IsDone:", c.IsDone());
const o = [], r = [], s = [];
let t = 0, p = 0;
const l = new e.TopExp_Explorer_2(n, e.TopAbs_ShapeEnum.TopAbs_FACE, e.TopAbs_ShapeEnum.TopAbs_SHAPE);
for (; l.More(); ) {
p++;
const u = e.TopoDS.Face_1(l.Current()), b = new e.TopLoc_Location_1();
let i = null;
try {
i = e.BRep_Tool.Triangulation(u, b, 0);
} catch (g) {
console.log("occtShapeToMesh: Triangulation(3 args) failed:", g);
try {
i = e.BRep_Tool.Triangulation(u, b);
} catch (w) {
console.log("occtShapeToMesh: Triangulation(2 args) failed:", w), l.Next();
continue;
}
}
if (console.log("occtShapeToMesh: face", p, "triangulation:", i, "isNull:", (f = i == null ? void 0 : i.IsNull) == null ? void 0 : f.call(i)), i && !i.IsNull())
try {
const g = i.get(), w = g.NbNodes(), T = g.NbTriangles();
console.log("occtShapeToMesh: face", p, "nodes:", w, "triangles:", T);
for (let h = 1; h <= w; h++) {
const _ = g.Node(h);
o.push(_.X(), _.Y(), _.Z()), s.push(0, 0, 1);
}
for (let h = 1; h <= T; h++) {
const _ = g.Triangle(h), m = _.Value(1) - 1 + t, x = _.Value(2) - 1 + t, A = _.Value(3) - 1 + t;
r.push(m, x, A);
}
t += w;
} catch (g) {
console.error("occtShapeToMesh: extraction failed for face", p, g);
}
l.Next();
}
return {
vertices: new Float32Array(o),
indices: new Uint32Array(r),
normals: new Float32Array(s)
};
}
function d(e, n) {
switch (n.type) {
case "box": {
const [a, c, o] = n.origin ?? [0, 0, 0], r = new e.gp_Pnt_3(a, c, o), s = new e.BRepPrimAPI_MakeBox_3(r, n.dx, n.dy, n.dz).Shape();
return console.log("Built box shape:", s), s;
}
case "cylinder": {
const [a, c, o] = n.origin ?? [0, 0, 0], [r, s, t] = n.axis ?? [0, 0, 1], p = new e.gp_Pnt_3(a, c, o), l = new e.gp_Dir_4(r, s, t), f = new e.gp_Ax2_3(p, l), u = new e.BRepPrimAPI_MakeCylinder_3(f, n.radius, n.height).Shape();
return console.log("Built cylinder shape:", u), u;
}
case "sphere": {
const [a, c, o] = n.center ?? [0, 0, 0], r = new e.gp_Pnt_3(a, c, o), s = new e.BRepPrimAPI_MakeSphere_5(r, n.radius).Shape();
return console.log("Built sphere shape:", s), s;
}
}
}
async function y(e, n, a, c = 0.1) {
console.log("booleanOperation: loading OCCT...");
const o = await S();
console.log("booleanOperation: OCCT loaded, building shapes...");
const r = d(o, e), s = d(o, n);
console.log("booleanOperation: performing", a, "...");
let t;
switch (a) {
case "union":
t = new o.BRepAlgoAPI_Fuse_3(r, s, new o.Message_ProgressRange_1());
break;
case "difference":
t = new o.BRepAlgoAPI_Cut_3(r, s, new o.Message_ProgressRange_1());
break;
case "intersection":
t = new o.BRepAlgoAPI_Common_3(r, s, new o.Message_ProgressRange_1());
break;
}
if (t.Build(new o.Message_ProgressRange_1()), !t.IsDone())
throw new Error(`Boolean ${a} failed: IsDone() returned false`);
const p = t.Shape();
console.log("booleanOperation: boolean done, meshing result...");
const l = M(o, p, c);
return console.log("booleanOperation: mesh result:", {
vertices: l.vertices.length / 3,
indices: l.indices.length / 3,
normals: l.normals.length / 3
}), l;
}
export {
y as booleanOperation
};