primitives-1d_bspline-curve.js

import oc from "../opencascade/initializer";
import {CONTINUITY_TYPES} from "../constants";
import {Edge} from "../modeling";
import {Plane, Vector} from "../math";

/**
 * Creates a BSpline curve passing through a set of points.
 * @memberof primitives-1D
 * @alias BSplineCurve
 * @param {Object} parameters - The parameters for the BSpline curve.
 * @param {Plane} [parameters.plane=Plane.XY] - The plane in which the curve is constructed.
 * @param {Vector[]} parameters.points - The points through which the curve passes.
 * @param {number} [parameters.minDegree=1] - The minimum degree of the BSpline curve.
 * @param {number} [parameters.maxDegree=6] - The maximum degree of the BSpline curve.
 * @param {('c0'|'c1'|'c2'|'c3'|'cn'|'g1'|'g2')} [parameters.continuityType="c2"] - The continuity type of the curve.
 * @returns {Edge} An `Edge` object representing the constructed `BSpline` curve.
 */
const BSplineCurve = ({plane = Plane.XY, points, minDegree = 1, maxDegree = 6, continuityType = "c2"}) => {
  const arrayOfPoints = new oc.TColgp_Array1OfPnt_2(1, points.length);
  points.forEach((point, index) => {
    const globalPoint = plane.toWorldCoordinates(new oc.gp_Pnt_3(point.x, point.y, point.z));
    arrayOfPoints.SetValue_1(index + 1, globalPoint);
  });
  const bspline = new oc.GeomAPI_PointsToBSpline_2(arrayOfPoints, minDegree, maxDegree, CONTINUITY_TYPES[continuityType], 1.0e-3)
    .Curve()
    .get();
  const handle = new oc.Handle_Geom_Curve_2(bspline);
  return new Edge(new oc.BRepBuilderAPI_MakeEdge_24(handle).Edge());
};

export {BSplineCurve};