Path

Path object is used to construct a path. Additional operations like evolve, extrude can be applied to a face or an edge created from this path.

Methods that draw a line segment will use either absolute values or relative to current point depending on if Path object is initialized to use relative values.

Many methods are chainable


Constructor
Path(isRelative)

Creates a new instance of a path.

Syntax

new cadlib.Path(relative);

Parameters

Name Type Description
isRelative boolean If true, successive path commands are treated as relative to the current end point. If false, they are treated as absolute.

Properties

startpoint

Returns the starting point (x,y,z) values of the path

Syntax

path.startpoint


endpoint

Returns the end point (x,y,z) of the path

Syntax

path.endpoint


Methods
start()

Chainable

Starts a new Path. Returns this path object.

Syntax

path.start(x, y, z);

Parameters

Parameters:

Name Type Description
x Number X coordinate of the start point
y Number Y coordinate of the start point
z Number Z coordinate of the start point

Returns

Path - Returns this path object

Example


let p = new cadlib.Path();

//start a path at the origin
p.start(0,0,0);

//add more points

//complete this path


close()

Cloes the path.

Syntax

path.start(x, y, z);

Parameters

Parameters:

Name Type Description
face Face A Face object of which wires are desired

Returns

Path - Returns this path object

Example


let p = new Path();

p.start(0,0,0);

//add more points

//complete this path


line(from, to)

Chainable

Draws a line

Syntax

path.line( new Point(0,0,0), new Point(10,10,10));

Parameters

Name Type Description
from Point From point
to Point To point

Returns

Path - Returns this path object


lineTo()

Chainable

Adds a line segment upto the specified point.

Syntax

path.lineTo(x, y, z);

Parameters

Parameters:

Name Type Description
x Number x component of the vertex/point
y Number y component of the vertex/point
z Number z component of the vertex/point

Returns

Path - Returns this path object


xLineTo()

Chainable

Draws line segment in X direction

Syntax

path.xlineTo(x);

Parameters

Name Type Description
x number change on the X axis

Returns

Path - Returns this path object


yLineTo()

Chainable

Draws line segment in Y direction

Syntax

path.ylineTo(y);

Parameters

Name Type Description
y number change on the Y axis

Returns

Path - Returns this path object


zLineTo()

Chainable

Draws line segment in X direction

Syntax

path.zlineTo(z);

Parameters

Name Type Description
z number change on the Z axis

Returns

Path - Returns this path object


arc3p()

Chainable

Creates an arc from 3 points

Syntax

path.arc3p(start, end, pointOnArc);

Parameters

Name Type Description
start Point Start point
end Point End point
pointOnArc Point Point on the arc

Returns

Path - Returns this path object


arc3pTo()

Chainable

Adds a 3 point arc to the existing path.

Syntax

path.arc3pTo(pointOnArc, endPoint);

Parameters

Name Type Description
pointOnArc Point point on the arc
endPoint Point end point on the arc

Returns

Path - Returns this path object


arc2p()

Chainable

Adds a two point arc.

Syntax

path.arc2p(center, axis, radius, startPoint, endPoint);

Parameters

Name Type Description
center Point center of the arc
axis Point axis of rotation
radius number radius of the arc
startPoint Point start point on the arc
endPoint Point end point on the arc

Returns

Path - Returns this path object


arc2pTo()

Chainable

Adds a two point arc. Useful when path is using relative mode.

Syntax

path.arc2pTo(center, axis, radius, endPoint);

Parameters

Name Type Description
center Point center of the arc
axis Point axis of rotation
radius number radius of the arc
endPoint Point end point on the arc

Returns

Path - Returns this path object


arcEllipse()

Creates an elliptical arc edge.

Syntax

path.arcEllipse(center, axis, majorRadius, minorRadius, p1, p2);

Parameters

Name Type Description
center Point Ceter of the ellipitical arc
axis Point axis of the ellipse
majorRadius number Major radius of the arc
minorRadius number Minor radius of the arc
p1 Point First point on the arc
p2 Point Second point on the arc

Returns

Path - Returns this path object


arcEllipseTo()

Creates an elliptical arc edge from current point (Useful when path is in relative mode)

Syntax

path.arcEllipseTo(center, axis, majorRadius, minorRadius, endPoint);

Parameters

Name Type Description
center Point Ceter of the ellipse
axis Point Normal to the plane of the ellipse
majorRadius number
minorRadius number
endPoint Point End point on the arc

Returns

Path - Returns this path object


bezier()

Creates an edge from a Bézier curve. A Bézier curve is defined by a set of control points or poles, P0 through Pn, where n is called its order (n = 1 for linear, 2 for quadratic, etc.). The first and last control points are always the end points of the curve; however, the intermediate control points (if any) generally do not lie on the curve. Quadratic and cubic Bézier curves are most common. Higher degree curves are more computationally expensive to evaluate. Please refer to following for additional details.

References https://en.wikipedia.org/wiki/B%C3%A9zier_curve

Syntax

path.bezierTo(poles, weights);

Parameters

Name Type Description
poles Array of Points Poles or control points of a bezier curve. Each pole is a point or {x, y, z} tuple
weights Array of numbers Weights of a bezier curve. Number of weights and poles must match else results are unpredictable.

Returns

Path - Returns this path object

Example

Following script creates a face brom a bezier curve and a line. Line is generated by connecting start and end points of a bezier curve.


var poles = [

{x: 5, y:0, z:0},

{x:0, y:0, z:5},

{x:-5, y:0, z:0},

{x:0, y:0, z:-10}

];



var weights = [ 1.0, 1.0, 1.0, 1.0];

var edge1 = cadlib.bezierCurve(poles, weights);

var edge2 = cadlib.line(poles[3], poles[0]);

var face = cadlib.face( cadlib.wire([edge1, edge2]));


bezierTo()

Adds bezier curve to a path.

Syntax

path.bezierTo(poles, weights);

Parameters

Name Type Description
poles Array of Points Poles or control points of a bezier curve. .Each point is expressed relative to prior end point of a path.
weights Array of numbers Weights of a bezier curve. Number of weights and poles must match else results are unpredictable.

Returns

Path - Returns this path object


splineTo()

Adds a B-spline curve. A B-Spline curve is a piecewise polynomial or rational curve. Please refer to additional literature below.

References

https://en.wikipedia.org/wiki/B-spline#NURBS

http://mathworld.wolfram.com/B-Spline.html

https://en.wikipedia.org/wiki/Non-uniform_rational_B-spline

Syntax

path.splineTo(poles, weights, knots, multiplicities, degree, rational);

Parameters

Name Type Description
poles Array of Points Control points. Each pole is a point or {x, y, z} tuple. The control points determine the shape of the curve.[8] Typically, each point of the curve is computed by taking a weighted sum of a number of control points. The weight of each point varies according to the governing parameter. For a curve of degree d, the weight of any control point is only nonzero in d+1 intervals of the parameter space. Within those intervals, the weight changes according to a polynomial function (basis functions) of degree

| weights | Array of numbers | Array of weights (numbers) | | knots | Array of numbers | The knot vector is a sequence of parameter values that determines where and how the control points affect the NURBS curve. The number of knots is always equal to the number of control points plus curve degree plus one (i.e. number of control points plus curve order). | | multiplicities | Array of numbers | Multiplicities define the repetition of the knots. Determins different types of bezier curves

+Uniform: if all multiplicities are equal to 1, except the first and the last knot which have a multiplicity of Degree + 1, where Degree is the degree of the BSpline curve

| | degree | number (Integer) | Degre of the B-spline curve | | rational | number | If true, a rational B-spline is created|

Returns

Path - Returns this path object


interpolateSpline()

Adds a spline curve by interpolating specified points

Syntax

path.interpolateSpline(options));

Parameters

options object

Name Type Description
points Array of Points Points to pass the interpolated spline through

| periodic | boolean | True, if curve is closed |

| tolerance | number | Tolerance used to pass the curve |

| startTangent | Vec3 | Optiona, Spline’s tangent at the start point |

| endTangent | Vec3 | Optiona, Spline’s tangent at the end point |

Returns

Path - Returns this path object

Examples

Create a spline passing through 3 points


var options = {

points : [ {x: 0, y:0, z:5 },{x: 5, y:0, z:0 },{x: 0, y:0,z:-5 }],

periodic : false,

tolerance : 0.0001

};

var edge = path.interpolateSpline(options);


wire()

Returns a wire that represents this path.

Syntax

path.wire();

Parameteres

None

Returns

Wire


face()

Creates a face with wire representing this path

Syntax

path.face();

Parameters

None

Returns

Face


reset()

Resets this path.

Syntax

path.reset();

Parameters

None

Returns

Path - Returns this path object


line()

Creates a line,

Parameters

Name Type descri[tion
from Point First Point
to Point Second Point
Returns

Path

Example


let path = new cadlib.Path();
let p1 = new Point(0, 0, 0);
let p2 = new Point(20, 20, 20)
path.line(p1, p2);



circle()

Creates a Circle

Syntax

path.circle(center, normal, radius);

Parameters

Name Type descri[tion
center Point Center
normal Vector Normal to the circle’s plane
radius Number Circle’s radius
Returns

Path - this path