Yet another Bezier library for OpenSCAD by arpruss 3d model
3dmdb logo
Thingiverse
Yet another Bezier library for OpenSCAD by arpruss

Yet another Bezier library for OpenSCAD by arpruss

by Thingiverse
Last crawled date: 3 years ago
This is yet another Bezier library, which takes a list of nodes and control points and generates a list of points that you can then feed into polygon() or some other function. The Bezier curves can be either 2D or 3D, but I've only been using it for 2D.
The library is designed so one can lay out 2D Bezier curves basically in one's head. But if you prefer to lay them out graphically, you can use Inkscape plus this plugin to design Bezier curves for this library.
One of the things I wanted was the ability to have an easy way to include smooth control points--control points that ensure that curve through the node has no cusp.
To use, first do use ; and then call the function Bezier(pointList) to generate output points. There is an optional precision=x parameter. With that parameter, about 1/x points will be generated per segment (excepting 2D segments that are a straight line--these are optimized to two points). Default is precision=0.05.
The pointList is a list of points arranged as follows:[node,control,control,node,control,control,node,...]
I recommend that in your code you flag the control points with /*C*/ so you don't get lost in a long list of points.
Each segment of the Bezier curve consists of two nodes, with two control points in between them. You can also have these special control points:

SYMMETRIC(): This makes the control point be on the same line and at the same distance to the node as the control point on the other side


SMOOTH_REL(x): This is like SYMMETRIC() except that the distance of the control point to the node is x times the distance of the other control point to the node (can be negative for special effects); SYMMETRIC() is in fact shorthand for SMOOTH_REL(1)


SMOOTH_ABS(x): This is like SYMMETRIC() except that the distance of the control point to the node is exactly x (can be negative for special effects)


OFFSET(v): This makes the control point be at the neighboring node plus the vector v


POLAR(r,angle): Like OFFSET(v) but specified in polar coordinates; equivalent to OFFSET(r*[cos(angle),sin(angle)]); only works in 2D

SHARP(): Puts the control point at the neighboring node; equivalent to OFFSET([0,0]) (or OFFSET([0,0,0]))
You can't have SMOOTH_*() and/or SYMMETRIC() control points on both sides of a node--the library then wouldn't know which line to place the control points on.
Finally, at the end of your list of points (and only there) you can add one or more REPEAT_MIRRORED(normalVector) points each of which stitches at the end of the path what came before but reflects it in the direction of the normalVector. For instance, if you want to draw a rounded plus sign, you can just include the first quarter, and then two REPEAT_MIRRORED() items.
There is also a utility function DecodeSpecialBezierPoints(p) which takes a node/control point list p and decodes all the special points (SMOOTH_*(), etc.) into ordinary 2D vectors. This is useful if, say, you want to take a Bezier curve and transform it in some way.
See the sample polygons in the source code for examples.
If you just want to use Bezier splines to smooth out the corner of a path, you can use:PathToBezier(path,offset=2,tension=0.551915024494,closed=false)
This returns a Bezier path which rounds off each corner in the path at offset distance from the corner. The tension parameter specifies what fraction of the offset the control nodes are to be added at: the default is designed to make right angles be very close to circular arcs. You will still need to feed the resulting path into the Bezier() function to render it.
Note that REPEAT_MIRRORED() can be used (multiple times if need be) at the end of the path fed into PathToBezier: this allows you to quickly build up more complex shapes. For instance, the rounded plus board for a solitaire game can be generated with: PathToBezier([[1,0],[1,1.5/3.5],[1.5/3.5,1.5/3.5],REPEAT_MIRRORED([-1,1]),REPEAT_MIRRORED([1,0]), REPEAT_MIRRORED([0,1])],offset=0.1)
A useful included module for debugging Bezier curves is BezierVisualize(p).
Updates:
January 8, 2020: Remove warning in newer OpenSCAD versions.
December 12, 2019: PathToBezier() added.
December 1, 2019: SMOOTH_ABS() and other functions work together with LINE() on the other side of the node.

Tags