GithubHelp home page GithubHelp logo

nurbs's Introduction

NURBS

Complete Javascript NURBS library

Features

  • Handle NURBS curves and surfaces
  • Basic primitives (points, vectors)
  • Utility functions for creating standard geometry pieces (lines, circles, ellipses, arcs)
  • Trimming NURBS curves and surfaces
  • Rational NURBS (weights for control points)
  • NURBS curve and surface derivatives
  • NURBS curve and surface refinement
  • NURBS curve and surface knot insertion and removal
  • NURBS curve and surface degree elevation
  • NURBS curve and surface intersection
  • NURBS curve and surface approximation

Usage

Creating a Point

const point = new Point(1, 2, 3);
console.log(point.getX()); // 1
console.log(point.getY()); // 2
console.log(point.getZ()); // 3

Creating a Vector

const vector1 = new Vector(1, 2, 3);
const vector2 = new Vector(4, 5, 6);
const result = vector1.add(vector2);
console.log(result); // Vector { x: 5, y: 7, z: 9 }

Creating a NURBS Curve

const controlPoints = [new Point(0, 0, 0), new Point(1, 1, 0), new Point(2, 0, 0)];
const degree = 2;
const knotVector = [0, 0, 0, 1, 1, 1];
const curve = new Curve(controlPoints, degree, knotVector);
const pointOnCurve = curve.evaluate(0.5);
console.log(pointOnCurve); // { x: 1, y: 0.5, z: 0 }

Creating a NURBS Surface

const controlPoints = [
  [new Point(0, 0, 0), new Point(1, 0, 0), new Point(2, 0, 0)],
  [new Point(0, 1, 0), new Point(1, 1, 0), new Point(2, 1, 0)],
  [new Point(0, 2, 0), new Point(1, 2, 0), new Point(2, 2, 0)]
];
const degrees = [2, 2];
const knotVectors = [
  [0, 0, 0, 1, 1, 1],
  [0, 0, 0, 1, 1, 1]
];
const surface = new Surface(controlPoints, degrees, knotVectors);
const pointOnSurface = surface.evaluate(0.5, 0.5);
console.log(pointOnSurface); // { x: 1, y: 1, z: 0 }

Creating a Line

import { createLine } from './src/NURBS/CurvePrimitives';

const startPoint = new Point(0, 0, 0);
const endPoint = new Point(1, 1, 1);
const line = createLine(startPoint, endPoint);
console.log(line);

Creating a Circle

import { createCircle } from './src/NURBS/CurvePrimitives';

const center = new Point(0, 0, 0);
const radius = 1;
const circle = createCircle(center, radius);
console.log(circle);

Creating an Ellipse

import { createEllipse } from './src/NURBS/CurvePrimitives';

const center = new Point(0, 0, 0);
const radiusX = 2;
const radiusY = 1;
const ellipse = createEllipse(center, radiusX, radiusY);
console.log(ellipse);

Creating an Arc

import { createArc } from './src/NURBS/CurvePrimitives';

const center = new Point(0, 0, 0);
const radius = 1;
const startAngle = 0;
const endAngle = Math.PI / 2;
const arc = createArc(center, radius, startAngle, endAngle);
console.log(arc);

Creating a Bezier Curve

import { createBezierCurve } from './src/NURBS/CurvePrimitives';

const controlPoints = [new Point(0, 0, 0), new Point(1, 2, 0), new Point(2, 0, 0)];
const bezierCurve = createBezierCurve(controlPoints);
console.log(bezierCurve);

Creating a Parabola

import { createParabola } from './src/NURBS/CurvePrimitives';

const vertex = new Point(0, 0, 0);
const focus = new Point(0, 1, 0);
const parabola = createParabola(vertex, focus);
console.log(parabola);

Creating a Hyperbola

import { createHyperbola } from './src/NURBS/CurvePrimitives';

const center = new Point(0, 0, 0);
const a = 1;
const b = 1;
const hyperbola = createHyperbola(center, a, b);
console.log(hyperbola);

Creating a Spline

import { createSpline } from './src/NURBS/CurvePrimitives';

const controlPoints = [new Point(0, 0, 0), new Point(1, 2, 0), new Point(2, 0, 0)];
const degree = 2;
const spline = createSpline(controlPoints, degree);
console.log(spline);

Creating a Rational NURBS Curve

import { createRationalNURBSCurve } from './src/NURBS/CurvePrimitives';

const controlPoints = [new Point(0, 0, 0), new Point(1, 1, 0), new Point(2, 0, 0)];
const weights = [1, 0.5, 1];
const degree = 2;
const rationalCurve = createRationalNURBSCurve(controlPoints, weights, degree);
console.log(rationalCurve);

Trimming a NURBS Curve

import { trimCurve } from './src/NURBS/CurvePrimitives';

const controlPoints = [new Point(0, 0, 0), new Point(1, 1, 0), new Point(2, 0, 0)];
const weights = [1, 0.5, 1];
const degree = 2;
const curve = createRationalNURBSCurve(controlPoints, weights, degree);
const trimmedCurve = trimCurve(curve, 0, 1);
console.log(trimmedCurve);

Creating a Plane

import { createPlane } from './src/NURBS/SurfacePrimitives';

const width = 2;
const height = 2;
const plane = createPlane(width, height);
console.log(plane);

Creating a Cylinder

import { createCylinder } from './src/NURBS/SurfacePrimitives';

const radius = 1;
const height = 2;
const cylinder = createCylinder(radius, height);
console.log(cylinder);

Creating a Cone

import { createCone } from './src/NURBS/SurfacePrimitives';

const radius = 1;
const height = 2;
const cone = createCone(radius, height);
console.log(cone);

Creating a Sphere

import { createSphere } from './src/NURBS/SurfacePrimitives';

const radius = 1;
const sphere = createSphere(radius);
console.log(sphere);

Creating a Rational NURBS Surface

import { createRationalNURBSSurface } from './src/NURBS/SurfacePrimitives';

const controlPoints = [
  [new Point(0, 0, 0), new Point(1, 0, 0), new Point(2, 0, 0)],
  [new Point(0, 1, 0), new Point(1, 1, 0), new Point(2, 1, 0)],
  [new Point(0, 2, 0), new Point(1, 2, 0), new Point(2, 2, 0)]
];
const weights = [
  [1, 0.5, 1],
  [0.5, 0.25, 0.5],
  [1, 0.5, 1]
];
const degrees = [2, 2];
const rationalSurface = createRationalNURBSSurface(controlPoints, weights, degrees);
console.log(rationalSurface);

Trimming a NURBS Surface

import { trimSurface } from './src/NURBS/SurfacePrimitives';

const controlPoints = [
  [new Point(0, 0, 0), new Point(1, 0, 0), new Point(2, 0, 0)],
  [new Point(0, 1, 0), new Point(1, 1, 0), new Point(2, 1, 0)],
  [new Point(0, 2, 0), new Point(1, 2, 0), new Point(2, 2, 0)]
];
const weights = [
  [1, 0.5, 1],
  [0.5, 0.25, 0.5],
  [1, 0.5, 1]
];
const degrees = [2, 2];
const surface = createRationalNURBSSurface(controlPoints, weights, degrees);
const trimmedSurface = trimSurface(surface, 0, 1, 0, 1);
console.log(trimmedSurface);

Evaluating NURBS Curve Derivatives

const controlPoints = [new Point(0, 0, 0), new Point(1, 1, 0), new Point(2, 0, 0)];
const degree = 2;
const knotVector = [0, 0, 0, 1, 1, 1];
const curve = new Curve(controlPoints, degree, knotVector);
const derivative = curve.derivative(0.5, 1);
console.log(derivative); // { x: 1, y: 0, z: 0 }

Evaluating NURBS Surface Derivatives

const controlPoints = [
  [new Point(0, 0, 0), new Point(1, 0, 0), new Point(2, 0, 0)],
  [new Point(0, 1, 0), new Point(1, 1, 0), new Point(2, 1, 0)],
  [new Point(0, 2, 0), new Point(1, 2, 0), new Point(2, 2, 0)]
];
const degrees = [2, 2];
const knotVectors = [
  [0, 0, 0, 1, 1, 1],
  [0, 0, 0, 1, 1, 1]
];
const surface = new Surface(controlPoints, degrees, knotVectors);
const derivative = surface.derivative(0.5, 0.5, 1, 1);
console.log(derivative); // { x: 1, y: 1, z: 0 }

Refining a NURBS Curve

const controlPoints = [new Point(0, 0, 0), new Point(1, 1, 0), new Point(2, 0, 0)];
const degree = 2;
const knotVector = [0, 0, 0, 1, 1, 1];
const curve = new Curve(controlPoints, degree, knotVector);
const refinedCurve = curve.refine([0.5]);
console.log(refinedCurve);

Refining a NURBS Surface

const controlPoints = [
  [new Point(0, 0, 0), new Point(1, 0, 0), new Point(2, 0, 0)],
  [new Point(0, 1, 0), new Point(1, 1, 0), new Point(2, 1, 0)],
  [new Point(0, 2, 0), new Point(1, 2, 0), new Point(2, 2, 0)]
];
const degrees = [2, 2];
const knotVectors = [
  [0, 0, 0, 1, 1, 1],
  [0, 0, 0, 1, 1, 1]
];
const surface = new Surface(controlPoints, degrees, knotVectors);
const refinedSurface = surface.refine([[0.5], [0.5]]);
console.log(refinedSurface);

Inserting a Knot into a NURBS Curve

const controlPoints = [new Point(0, 0, 0), new Point(1, 1, 0), new Point(2, 0, 0)];
const degree = 2;
const knotVector = [0, 0, 0, 1, 1, 1];
const curve = new Curve(controlPoints, degree, knotVector);
const newCurve = curve.insertKnot(0.5);
console.log(newCurve);

Inserting a Knot into a NURBS Surface

const controlPoints = [
  [new Point(0, 0, 0), new Point(1, 0, 0), new Point(2, 0, 0)],
  [new Point(0, 1, 0), new Point(1, 1, 0), new Point(2, 1, 0)],
  [new Point(0, 2, 0), new Point(1, 2, 0), new Point(2, 2, 0)]
];
const degrees = [2, 2];
const knotVectors = [
  [0, 0, 0, 1, 1, 1],
  [0, 0, 0, 1, 1, 1]
];
const surface = new Surface(controlPoints, degrees, knotVectors);
const newSurface = surface.insertKnot(0.5, 0.5);
console.log(newSurface);

Removing a Knot from a NURBS Curve

const controlPoints = [new Point(0, 0, 0), new Point(1, 1, 0), new Point(2, 0, 0)];
const degree = 2;
const knotVector = [0, 0, 0, 0.5, 1, 1, 1];
const curve = new Curve(controlPoints, degree, knotVector);
const newCurve = curve.removeKnot(0.5);
console.log(newCurve);

Removing a Knot from a NURBS Surface

const controlPoints = [
  [new Point(0, 0, 0), new Point(1, 0, 0), new Point(2, 0, 0)],
  [new Point(0, 1, 0), new Point(1, 1, 0), new Point(2, 1, 0)],
  [new Point(0, 2, 0), new Point(1, 2, 0), new Point(2, 2, 0)]
];
const degrees = [2, 2];
const knotVectors = [
  [0, 0, 0, 0.5, 1, 1, 1],
  [0, 0, 0, 0.5, 1, 1, 1]
];
const surface = new Surface(controlPoints, degrees, knotVectors);
const newSurface = surface.removeKnot(0.5, 0.5);
console.log(newSurface);

Elevating the Degree of a NURBS Curve

const controlPoints = [new Point(0, 0, 0), new Point(1, 1, 0), new Point(2, 0, 0)];
const degree = 2;
const knotVector = [0, 0, 0, 1, 1, 1];
const curve = new Curve(controlPoints, degree, knotVector);
const newCurve = curve.elevateDegree();
console.log(newCurve);

Elevating the Degree of a NURBS Surface

const controlPoints = [
  [new Point(0, 0, 0), new Point(1, 0, 0), new Point(2, 0, 0)],
  [new Point(0, 1, 0), new Point(1, 1, 0), new Point(2, 1, 0)],
  [new Point(0, 2, 0), new Point(1, 2, 0), new Point(2, 2, 0)]
];
const degrees = [2, 2];
const knotVectors = [
  [0, 0, 0, 1, 1, 1],
  [0, 0, 0, 1, 1, 1]
];
const surface = new Surface(controlPoints, degrees, knotVectors);
const newSurface = surface.elevateDegree();
console.log(newSurface);

Intersecting NURBS Curves

const controlPoints1 = [new Point(0, 0, 0), new Point(1, 1, 0), new Point(2, 0, 0)];
const degree1 = 2;
const knotVector1 = [0, 0, 0, 1, 1, 1];
const curve1 = new Curve(controlPoints1, degree1, knotVector1);

const controlPoints2 = [new Point(0, 1, 0), new Point(1, 0, 0), new Point(2, 1, 0)];
const degree2 = 2;
const knotVector2 = [0, 0, 0, 1, 1, 1];
const curve2 = new Curve(controlPoints2, degree2, knotVector2);

const intersections = curve1.intersect(curve2);
console.log(intersections);

Intersecting NURBS Surfaces

const controlPoints1 = [
  [new Point(0, 0, 0), new Point(1, 0, 0), new Point(2, 0, 0)],
  [new Point(0, 1, 0), new Point(1, 1, 0), new Point(2, 1, 0)],
  [new Point(0, 2, 0), new Point(1, 2, 0), new Point(2, 2, 0)]
];
const degrees1 = [2, 2];
const knotVectors1 = [
  [0, 0, 0, 1, 1, 1],
  [0, 0, 0, 1, 1, 1]
];
const surface1 = new Surface(controlPoints1, degrees1, knotVectors1);

const controlPoints2 = [
  [new Point(0, 0, 1), new Point(1, 0, 1), new Point(2, 0, 1)],
  [new Point(0, 1, 1), new Point(1, 1, 1), new Point(2, 1, 1)],
  [new Point(0, 2, 1), new Point(1, 2, 1), new Point(2, 2, 1)]
];
const degrees2 = [2, 2];
const knotVectors2 = [
  [0, 0, 0, 1, 1, 1],
  [0, 0, 0, 1, 1, 1]
];
const surface2 = new Surface(controlPoints2, degrees2, knotVectors2);

const intersections = surface1.intersect(surface2);
console.log(intersections);

Intersecting a NURBS Curve and Surface

const controlPointsCurve = [new Point(0, 0, 0), new Point(1, 1, 0), new Point(2, 0, 0)];
const degreeCurve = 2;
const knotVectorCurve = [0, 0, 0, 1, 1, 1];
const curve = new Curve(controlPointsCurve, degreeCurve, knotVectorCurve);

const controlPointsSurface = [
  [new Point(0, 0, 0), new Point(1, 0, 0), new Point(2, 0, 0)],
  [new Point(0, 1, 0), new Point(1, 1, 0), new Point(2, 1, 0)],
  [new Point(0, 2, 0), new Point(1, 2, 0), new Point(2, 2, 0)]
];
const degreesSurface = [2, 2];
const knotVectorsSurface = [
  [0, 0, 0, 1, 1, 1],
  [0, 0, 0, 1, 1, 1]
];
const surface = new Surface(controlPointsSurface, degreesSurface, knotVectorsSurface);

const intersections = curve.intersectSurface(surface);
console.log(intersections);

Approximating a NURBS Curve

const points = [new Point(0, 0, 0), new Point(1, 1, 0), new Point(2, 0, 0)];
const degree = 2;
const curve = Curve.approximate(points, degree);
console.log(curve);

Approximating a NURBS Surface

const points = [
  [new Point(0, 0, 0), new Point(1, 0, 0), new Point(2, 0, 0)],
  [new Point(0, 1, 0), new Point(1, 1, 0), new Point(2, 1, 0)],
  [new Point(0, 2, 0), new Point(1, 2, 0), new Point(2, 2, 0)]
];
const degrees = [2, 2];
const surface = Surface.approximate(points, degrees);
console.log(surface);

Creating a NURBS Face

import { createNURBSFace } from './src/NURBS/SurfacePrimitives';

const controlPoints = [
  [new Point(0, 0, 0), new Point(1, 0, 0), new Point(2, 0, 0)],
  [new Point(0, 1, 0), new Point(1, 1, 0), new Point(2, 1, 0)],
  [new Point(0, 2, 0), new Point(1, 2, 0), new Point(2, 2, 0)]
];
const degrees = [2, 2];
const knotVectors = [
  [0, 0, 0, 1, 1, 1],
  [0, 0, 0, 1, 1, 1]
];
const face = createNURBSFace(controlPoints, degrees, knotVectors);
console.log(face);

Creating a NURBS Edge

import { createNURBSEdge } from './src/NURBS/CurvePrimitives';

const controlPoints = [new Point(0, 0, 0), new Point(1, 1, 0), new Point(2, 0, 0)];
const degree = 2;
const knotVector = [0, 0, 0, 1, 1, 1];
const edge = createNURBSEdge(controlPoints, degree, knotVector);
console.log(edge);

Creating a BREP Taurus

import { BREP } from './src/BREP/BREP';

const brep = new BREP();
const taurus = brep.createBREPTaurus(5, 2);
console.log(taurus);

Creating a BREP Cylinder

import { BREP } from './src/BREP/BREP';

const brep = new BREP();
const cylinder = brep.createBREPCylinder(3, 7);
console.log(cylinder);

Creating a BREP Cone

import { BREP } from './src/BREP/BREP';

const brep = new BREP();
const cone = brep.createBREPCone(3, 7);
console.log(cone);

Creating a BREP Cube

import { BREP } from './src/BREP/BREP';

const brep = new BREP();
const cube = brep.createBREPCube(3, 3, 3);
console.log(cube);

Tracking IDs for Faces and Edges in BREP Data Structures

The BREP data structures now support tracking unique IDs for faces and edges. Each face and edge is assigned a unique ID when it is created. This allows for easier identification and management of faces and edges within the BREP data structures.

Creating a BREP Edge with ID

import { BREPEdge } from './src/BREP/BREPEdge';

const startVertex = new Point(0, 0, 0);
const endVertex = new Point(1, 1, 1);
const edge = new BREPEdge(startVertex, endVertex, 1);
console.log(edge.id); // 1

Creating a BREP Face with ID

import { BREPFace } from './src/BREP/BREPFace';

const surface = new Surface(controlPoints, degrees, knotVectors);
const face = new BREPFace(surface, 1);
console.log(face.id); // 1

Exporting a BREP Solid as an STL File

import { BREP } from './src/BREP/BREP';

const brep = new BREP();
const cube = brep.createBREPCube(3, 3, 3);
const stlContent = brep.exportToSTL();
console.log(stlContent);

Running Tests

To run the test suite using Jest, follow these steps:

  1. Install the dependencies:

    npm install
  2. Run the tests:

    npm test

The test files are located in the tests/test-suite.js file.

nurbs's People

Contributors

mmiscool avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.