UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

92 lines (91 loc) 3.18 kB
"use strict"; import { CADSopNode } from "./_BaseCAD"; import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig"; import { step } from "../../../core/geometry/modules/cad/CadConstant"; import { CoreCadType } from "../../../core/geometry/modules/cad/CadCoreType"; import { CadGC, CadGeometryType, _createCadNumberHandle } from "../../../core/geometry/modules/cad/CadCommon"; import { cadVertexCreate } from "../../../core/geometry/modules/cad/toObject3D/CadVertex"; import { Vector3 } from "three"; import { SopType } from "../../poly/registers/nodes/types/Sop"; import { CadObject } from "../../../core/geometry/modules/cad/CadObject"; import { CadLoaderSync } from "../../../core/geometry/modules/cad/CadLoaderSync"; const v0 = _createCadNumberHandle(); const v1 = _createCadNumberHandle(); const tmpV3 = new Vector3(); class CADPointsFromCurveSopParamsConfig extends NodeParamsConfig { constructor() { super(...arguments); /** @param points count */ this.count = ParamConfig.INTEGER(1, { range: [0, 100], rangeLocked: [true, false], step }); /** @param min */ this.min = ParamConfig.FLOAT(0, { range: [0, 1], rangeLocked: [false, false], step }); /** @param max */ this.max = ParamConfig.FLOAT(1, { range: [0, 1], rangeLocked: [false, false], step }); } } const ParamsConfig = new CADPointsFromCurveSopParamsConfig(); export class CADPointsFromCurveSopNode extends CADSopNode { constructor() { super(...arguments); this.paramsConfig = ParamsConfig; } static type() { return SopType.CAD_POINTS_FROM_CURVE; } initializeNode() { this.io.inputs.setCount(1); } cook(inputCoreGroups) { const oc = CadLoaderSync.oc(); const inputCoreGroup = inputCoreGroups[0]; const inputObjects = inputCoreGroup.cadObjects(); const newObjects = []; const { min, max, count } = this.pv; const delta = max - min; if (inputObjects) { CadGC.withGC((r) => { for (const inputObject of inputObjects) { if (CoreCadType.isGeom2dCurve(inputObject)) { const curve = inputObject.cadGeometry(); for (let i = 0; i < count; i++) { const d0 = min + delta * (i / count); const pt = r(new oc.gp_Pnt2d_1()); curve.D0(d0, pt); newObjects.push(new CadObject(pt, CadGeometryType.POINT_2D)); } } else if (CoreCadType.isEdge(inputObject)) { const edge = inputObject.cadGeometry(); oc.BRep_Tool.Range_1(edge, v0, v1); const handle = oc.BRep_Tool.Curve_2(edge, v0.current, v1.current); const curve = handle.get(); const pt = r(new oc.gp_Pnt_1()); for (let i = 0; i < count; i++) { const d0 = min + delta * (i / count); curve.D0(d0, pt); tmpV3.set(pt.X(), pt.Y(), pt.Z()); const vertex = cadVertexCreate(oc, tmpV3); newObjects.push(new CadObject(vertex, CadGeometryType.VERTEX)); } } } }); } this.setCADObjects(newObjects); } }