2017-08-30 00:00:16 +00:00
//////////////////////////////////////////////////////////////////////
2019-03-23 04:13:18 +00:00
// LibFile: paths.scad
2020-11-17 01:50:08 +00:00
// Support for polygons and paths.
2021-01-05 09:20:01 +00:00
// Includes:
2019-04-19 07:25:10 +00:00
// include <BOSL2/std.scad>
2017-08-30 00:00:16 +00:00
//////////////////////////////////////////////////////////////////////
2020-03-29 03:50:04 +00:00
include < triangulation.scad >
2019-05-03 19:19:51 +00:00
2017-08-30 00:00:16 +00:00
2019-03-23 04:13:18 +00:00
// Section: Functions
2020-01-30 22:00:10 +00:00
// Function: is_path()
// Usage:
2020-03-02 21:47:43 +00:00
// is_path(list, [dim], [fast])
2020-01-30 22:00:10 +00:00
// Description:
2020-03-02 21:47:43 +00:00
// Returns true if `list` is a path. A path is a list of two or more numeric vectors (AKA points).
// All vectors must of the same size, and may only contain numbers that are not inf or nan.
2020-03-03 02:39:57 +00:00
// By default the vectors in a path must be 2d or 3d. Set the `dim` parameter to specify a list
// of allowed dimensions, or set it to `undef` to allow any dimension.
2020-03-02 21:47:43 +00:00
// Examples:
// is_path([[3,4],[5,6]]); // Returns true
// is_path([[3,4]]); // Returns false
// is_path([[3,4],[4,5]],2); // Returns true
// is_path([[3,4,3],[5,4,5]],2); // Returns false
// is_path([[3,4,3],[5,4,5]],2); // Returns false
// is_path([[3,4,5],undef,[4,5,6]]); // Returns false
// is_path([[3,5],[undef,undef],[4,5]]); // Returns false
// is_path([[3,4],[5,6],[5,3]]); // Returns true
// is_path([3,4,5,6,7,8]); // Returns false
// is_path([[3,4],[5,6]], dim=[2,3]);// Returns true
// is_path([[3,4],[5,6]], dim=[1,3]);// Returns false
// is_path([[3,4],"hello"], fast=true); // Returns true
// is_path([[3,4],[3,4,5]]); // Returns false
// is_path([[1,2,3,4],[2,3,4,5]]); // Returns false
2020-03-03 02:39:57 +00:00
// is_path([[1,2,3,4],[2,3,4,5]],undef);// Returns true
2020-03-02 21:47:43 +00:00
// Arguments:
// list = list to check
// dim = list of allowed dimensions of the vectors in the path. Default: [2,3]
// fast = set to true for fast check that only looks at first entry. Default: false
function is_path ( list , dim = [ 2 , 3 ] , fast = false ) =
2020-08-11 14:15:49 +00:00
fast
? is_list ( list ) && is_vector ( list [ 0 ] )
: is_matrix ( list )
&& len ( list ) > 1
&& len ( list [ 0 ] ) > 0
&& ( is_undef ( dim ) || in_list ( len ( list [ 0 ] ) , force_list ( dim ) ) ) ;
2020-01-30 22:00:10 +00:00
// Function: is_closed_path()
// Usage:
// is_closed_path(path, [eps]);
2019-03-23 04:13:18 +00:00
// Description:
2020-01-30 22:00:10 +00:00
// Returns true if the first and last points in the given path are coincident.
function is_closed_path ( path , eps = EPSILON ) = approx ( path [ 0 ] , path [ len ( path ) - 1 ] , eps = eps ) ;
// Function: close_path()
2019-03-23 04:13:18 +00:00
// Usage:
2020-01-30 22:00:10 +00:00
// close_path(path);
// Description:
// If a path's last point does not coincide with its first point, closes the path so it does.
function close_path ( path , eps = EPSILON ) = is_closed_path ( path , eps = eps ) ? path : concat ( path , [ path [ 0 ] ] ) ;
// Function: cleanup_path()
// Usage:
// cleanup_path(path);
// Description:
// If a path's last point coincides with its first point, deletes the last point in the path.
function cleanup_path ( path , eps = EPSILON ) = is_closed_path ( path , eps = eps ) ? select ( path , 0 , - 2 ) : path ;
// Function: path_subselect()
// Usage:
// path_subselect(path,s1,u1,s2,u2,[closed]):
// Description:
// Returns a portion of a path, from between the `u1` part of segment `s1`, to the `u2` part of
// segment `s2`. Both `u1` and `u2` are values between 0.0 and 1.0, inclusive, where 0 is the start
// of the segment, and 1 is the end. Both `s1` and `s2` are integers, where 0 is the first segment.
// Arguments:
// path = The path to get a section of.
// s1 = The number of the starting segment.
// u1 = The proportion along the starting segment, between 0.0 and 1.0, inclusive.
// s2 = The number of the ending segment.
// u2 = The proportion along the ending segment, between 0.0 and 1.0, inclusive.
// closed = If true, treat path as a closed polygon.
function path_subselect ( path , s1 , u1 , s2 , u2 , closed = false ) =
2020-05-30 02:04:34 +00:00
let (
lp = len ( path ) ,
l = lp - ( closed ? 0 : 1 ) ,
u1 = s1 < 0 ? 0 : s1 > l ? 1 : u1 ,
u2 = s2 < 0 ? 0 : s2 > l ? 1 : u2 ,
s1 = constrain ( s1 , 0 , l ) ,
s2 = constrain ( s2 , 0 , l ) ,
pathout = concat (
( s1 < l && u1 < 1 ) ? [ lerp ( path [ s1 ] , path [ ( s1 + 1 ) % lp ] , u1 ) ] : [ ] ,
[ for ( i = [ s1 + 1 : 1 : s2 ] ) path [ i ] ] ,
( s2 < l && u2 > 0 ) ? [ lerp ( path [ s2 ] , path [ ( s2 + 1 ) % lp ] , u2 ) ] : [ ]
)
) pathout ;
2020-01-30 22:00:10 +00:00
// Function: simplify_path()
// Description:
2020-08-11 14:15:49 +00:00
// Takes a path and removes unnecessary subsequent collinear points.
2020-01-30 22:00:10 +00:00
// Usage:
// simplify_path(path, [eps])
2019-03-23 04:13:18 +00:00
// Arguments:
2020-08-11 14:15:49 +00:00
// path = A list of path points of any dimension.
2020-01-30 22:00:10 +00:00
// eps = Largest positional variance allowed. Default: `EPSILON` (1-e9)
function simplify_path ( path , eps = EPSILON ) =
2020-08-11 14:15:49 +00:00
assert ( is_path ( path ) , "Invalid path." )
assert ( is_undef ( eps ) || ( is_finite ( eps ) && ( eps >= 0 ) ) , "Invalid tolerance." )
2021-03-06 10:26:39 +00:00
len ( path ) < = 2 ? path :
let (
indices = [
0 ,
for ( i = [ 1 : 1 : len ( path ) - 2 ] )
if ( ! collinear ( path [ i - 1 ] , path [ i ] , path [ i + 1 ] , eps = eps ) ) i ,
len ( path ) - 1
]
) [ for ( i = indices ) path [ i ] ] ;
2019-03-23 04:13:18 +00:00
2020-01-30 22:00:10 +00:00
// Function: simplify_path_indexed()
2019-03-23 04:13:18 +00:00
// Description:
2020-08-11 14:15:49 +00:00
// Takes a list of points, and a list of indices into `points`,
// and removes from the list all indices of subsequent indexed points that are unecessarily collinear.
// Returns the list of the remained indices.
2019-03-23 04:13:18 +00:00
// Usage:
2020-08-11 14:15:49 +00:00
// simplify_path_indexed(points,indices, eps)
2019-03-23 04:13:18 +00:00
// Arguments:
2020-01-30 22:00:10 +00:00
// points = A list of points.
2020-08-11 14:15:49 +00:00
// indices = A list of indices into `points` that forms a path.
2020-01-30 22:00:10 +00:00
// eps = Largest angle variance allowed. Default: EPSILON (1-e9) degrees.
2020-08-11 14:15:49 +00:00
function simplify_path_indexed ( points , indices , eps = EPSILON ) =
2021-03-06 10:26:39 +00:00
len ( indices ) < = 2 ? indices :
let (
indices = concat (
indices [ 0 ] ,
[
for ( i = [ 1 : 1 : len ( indices ) - 2 ] ) let (
i1 = indices [ i - 1 ] ,
i2 = indices [ i ] ,
i3 = indices [ i + 1 ]
) if ( ! collinear ( points [ i1 ] , points [ i2 ] , points [ i3 ] , eps = eps ) )
indices [ i ]
] ,
indices [ len ( indices ) - 1 ]
)
) indices ;
2019-03-23 04:13:18 +00:00
2019-03-27 06:22:38 +00:00
// Function: path_length()
// Usage:
2019-07-01 23:25:00 +00:00
// path_length(path,[closed])
2019-03-27 06:22:38 +00:00
// Description:
// Returns the length of the path.
// Arguments:
// path = The list of points of the path to measure.
2019-08-09 20:07:18 +00:00
// closed = true if the path is closed. Default: false
2019-03-27 06:22:38 +00:00
// Example:
// path = [[0,0], [5,35], [60,-25], [80,0]];
// echo(path_length(path));
2019-07-01 23:25:00 +00:00
function path_length ( path , closed = false ) =
2020-05-30 02:04:34 +00:00
len ( path ) < 2 ? 0 :
sum ( [ for ( i = [ 0 : 1 : len ( path ) - 2 ] ) norm ( path [ i + 1 ] - path [ i ] ) ] ) + ( closed ? norm ( path [ len ( path ) - 1 ] - path [ 0 ] ) : 0 ) ;
2019-03-27 06:22:38 +00:00
2020-06-14 02:35:22 +00:00
// Function: path_segment_lengths()
// Usage:
// path_segment_lengths(path,[closed])
// Description:
// Returns list of the length of each segment in a path
// Arguments:
// path = path to measure
// closed = true if the path is closed. Default: false
function path_segment_lengths ( path , closed = false ) =
[
2021-03-06 10:26:39 +00:00
for ( i = [ 0 : 1 : len ( path ) - 2 ] ) norm ( path [ i + 1 ] - path [ i ] ) ,
if ( closed ) norm ( path [ 0 ] - select ( path , - 1 ) )
] ;
2020-06-14 02:35:22 +00:00
2020-01-29 03:13:56 +00:00
// Function: path_pos_from_start()
// Usage:
// pos = path_pos_from_start(path,length,[closed]);
// Description:
// Finds the segment and relative position along that segment that is `length` distance from the
// front of the given `path`. Returned as [SEGNUM, U] where SEGNUM is the segment number, and U is
// the relative distance along that segment, a number from 0 to 1. If the path is shorter than the
// asked for length, this returns `undef`.
// Arguments:
// path = The path to find the position on.
// length = The length from the start of the path to find the segment and position of.
// Example(2D):
// path = circle(d=50,$fn=18);
// pos = path_pos_from_start(path,20,closed=false);
// stroke(path,width=1,endcaps=false);
// pt = lerp(path[pos[0]], path[(pos[0]+1)%len(path)], pos[1]);
// color("red") translate(pt) circle(d=2,$fn=12);
function path_pos_from_start ( path , length , closed = false , _d = 0 , _i = 0 ) =
2020-05-30 02:04:34 +00:00
let ( lp = len ( path ) )
_i >= lp - ( closed ? 0 : 1 ) ? undef :
let ( l = norm ( path [ ( _i + 1 ) % lp ] - path [ _i ] ) )
_d + l < = length ? path_pos_from_start ( path , length , closed , _d + l , _i + 1 ) :
[ _i , ( length - _d ) / l ] ;
2020-01-29 03:13:56 +00:00
// Function: path_pos_from_end()
// Usage:
// pos = path_pos_from_end(path,length,[closed]);
// Description:
// Finds the segment and relative position along that segment that is `length` distance from the
// end of the given `path`. Returned as [SEGNUM, U] where SEGNUM is the segment number, and U is
// the relative distance along that segment, a number from 0 to 1. If the path is shorter than the
// asked for length, this returns `undef`.
// Arguments:
// path = The path to find the position on.
// length = The length from the end of the path to find the segment and position of.
// Example(2D):
// path = circle(d=50,$fn=18);
// pos = path_pos_from_end(path,20,closed=false);
// stroke(path,width=1,endcaps=false);
// pt = lerp(path[pos[0]], path[(pos[0]+1)%len(path)], pos[1]);
// color("red") translate(pt) circle(d=2,$fn=12);
function path_pos_from_end ( path , length , closed = false , _d = 0 , _i = undef ) =
2020-05-30 02:04:34 +00:00
let (
lp = len ( path ) ,
_i = _i ! = undef ? _i : lp - ( closed ? 1 : 2 )
)
_i < 0 ? undef :
let ( l = norm ( path [ ( _i + 1 ) % lp ] - path [ _i ] ) )
_d + l < = length ? path_pos_from_end ( path , length , closed , _d + l , _i - 1 ) :
[ _i , 1 - ( length - _d ) / l ] ;
2020-01-29 03:13:56 +00:00
// Function: path_trim_start()
// Usage:
// path_trim_start(path,trim);
// Description:
// Returns the `path`, with the start shortened by the length `trim`.
// Arguments:
// path = The path to trim.
// trim = The length to trim from the start.
// Example(2D):
// path = circle(d=50,$fn=18);
// path2 = path_trim_start(path,5);
// path3 = path_trim_start(path,20);
// color("blue") stroke(path3,width=5,endcaps=false);
// color("cyan") stroke(path2,width=3,endcaps=false);
// color("red") stroke(path,width=1,endcaps=false);
function path_trim_start ( path , trim , _d = 0 , _i = 0 ) =
2020-05-30 02:04:34 +00:00
_i >= len ( path ) - 1 ? [ ] :
let ( l = norm ( path [ _i + 1 ] - path [ _i ] ) )
_d + l < = trim ? path_trim_start ( path , trim , _d + l , _i + 1 ) :
let ( v = unit ( path [ _i + 1 ] - path [ _i ] ) )
concat (
[ path [ _i + 1 ] - v * ( l - ( trim - _d ) ) ] ,
[ for ( i = [ _i + 1 : 1 : len ( path ) - 1 ] ) path [ i ] ]
) ;
2020-01-29 03:13:56 +00:00
// Function: path_trim_end()
// Usage:
// path_trim_end(path,trim);
// Description:
// Returns the `path`, with the end shortened by the length `trim`.
// Arguments:
// path = The path to trim.
// trim = The length to trim from the end.
// Example(2D):
// path = circle(d=50,$fn=18);
// path2 = path_trim_end(path,5);
// path3 = path_trim_end(path,20);
// color("blue") stroke(path3,width=5,endcaps=false);
// color("cyan") stroke(path2,width=3,endcaps=false);
// color("red") stroke(path,width=1,endcaps=false);
function path_trim_end ( path , trim , _d = 0 , _i = undef ) =
2020-05-30 02:04:34 +00:00
let ( _i = _i ! = undef ? _i : len ( path ) - 1 )
_i < = 0 ? [ ] :
let ( l = norm ( path [ _i ] - path [ _i - 1 ] ) )
_d + l < = trim ? path_trim_end ( path , trim , _d + l , _i - 1 ) :
let ( v = unit ( path [ _i ] - path [ _i - 1 ] ) )
concat (
[ for ( i = [ 0 : 1 : _i - 1 ] ) path [ i ] ] ,
[ path [ _i - 1 ] + v * ( l - ( trim - _d ) ) ]
) ;
2020-01-29 03:13:56 +00:00
2019-08-17 04:22:41 +00:00
// Function: path_closest_point()
// Usage:
// path_closest_point(path, pt);
// Description:
// Finds the closest path segment, and point on that segment to the given point.
// Returns `[SEGNUM, POINT]`
// Arguments:
// path = The path to find the closest point on.
// pt = the point to find the closest point to.
// Example(2D):
// path = circle(d=100,$fn=6);
// pt = [20,10];
// closest = path_closest_point(path, pt);
// stroke(path, closed=true);
// color("blue") translate(pt) circle(d=3, $fn=12);
// color("red") translate(closest[1]) circle(d=3, $fn=12);
function path_closest_point ( path , pt ) =
2020-05-30 02:04:34 +00:00
let (
pts = [ for ( seg = idx ( path ) ) segment_closest_point ( select ( path , seg , seg + 1 ) , pt ) ] ,
dists = [ for ( p = pts ) norm ( p - pt ) ] ,
min_seg = min_index ( dists )
) [ min_seg , pts [ min_seg ] ] ;
2019-08-17 04:22:41 +00:00
2020-03-02 00:12:51 +00:00
// Function: path_tangents()
2020-10-04 03:29:35 +00:00
// Usage:
// tangs = path_tangents(path, <closed>, <uniform>);
2020-03-02 00:12:51 +00:00
// Description:
// Compute the tangent vector to the input path. The derivative approximation is described in deriv().
2020-06-14 02:35:22 +00:00
// The returns vectors will be normalized to length 1. If any derivatives are zero then
// the function fails with an error. If you set `uniform` to false then the sampling is
// assumed to be non-uniform and the derivative is computed with adjustments to produce corrected
// values.
// Arguments:
// path = path to find the tagent vectors for
// closed = set to true of the path is closed. Default: false
// uniform = set to false to correct for non-uniform sampling. Default: true
// Example: A shape with non-uniform sampling gives distorted derivatives that may be undesirable
// rect = square([10,3]);
// tangents = path_tangents(rect,closed=true);
// stroke(rect,closed=true, width=0.1);
// color("purple")
// for(i=[0:len(tangents)-1])
// stroke([rect[i]-tangents[i], rect[i]+tangents[i]],width=.1, endcap2="arrow2");
// Example: A shape with non-uniform sampling gives distorted derivatives that may be undesirable
// rect = square([10,3]);
// tangents = path_tangents(rect,closed=true,uniform=false);
// stroke(rect,closed=true, width=0.1);
// color("purple")
// for(i=[0:len(tangents)-1])
// stroke([rect[i]-tangents[i], rect[i]+tangents[i]],width=.1, endcap2="arrow2");
function path_tangents ( path , closed = false , uniform = true ) =
2020-05-30 02:04:34 +00:00
assert ( is_path ( path ) )
2020-06-14 02:35:22 +00:00
! uniform ? [ for ( t = deriv ( path , closed = closed , h = path_segment_lengths ( path , closed ) ) ) unit ( t ) ]
: [ for ( t = deriv ( path , closed = closed ) ) unit ( t ) ] ;
2020-03-02 00:12:51 +00:00
// Function: path_normals()
2020-10-04 03:29:35 +00:00
// Usage:
// norms = path_normals(path, <tangents>, <closed>);
2020-03-02 00:12:51 +00:00
// Description:
// Compute the normal vector to the input path. This vector is perpendicular to the
2021-02-24 21:56:21 +00:00
// path tangent and lies in the plane of the curve. For 3d paths we define the plane of the curve
// at path point i to be the plane defined by point i and its two neighbors. At the endpoints of open paths
// we use the three end points. The computed normal is the one lying in this plane and pointing to the
// right of the direction of the path. If points are collinear then the path does not define a unique plane
// and hence the (right pointing) normal is not uniquely defined. In this case the function issues an error.
// For 2d paths the plane is always defined so the normal fails to exist only
// when the derivative is zero (in the case of repeated points).
2020-03-02 00:12:51 +00:00
function path_normals ( path , tangents , closed = false ) =
2021-02-24 21:56:21 +00:00
assert ( is_path ( path , [ 2 , 3 ] ) )
2020-05-30 02:04:34 +00:00
assert ( is_bool ( closed ) )
2021-02-24 21:56:21 +00:00
let (
tangents = default ( tangents , path_tangents ( path , closed ) ) ,
dim = len ( path [ 0 ] )
)
assert ( is_path ( tangents ) && len ( tangents [ 0 ] ) = = dim , "Dimensions of path and tangents must match" )
2020-05-30 02:04:34 +00:00
[
2021-02-24 21:56:21 +00:00
for ( i = idx ( path ) )
let (
pts = i = = 0 ? ( closed ? select ( path , - 1 , 1 ) : select ( path , 0 , 2 ) )
: i = = len ( path ) - 1 ? ( closed ? select ( path , i - 1 , i + 1 ) : select ( path , i - 2 , i ) )
: select ( path , i - 1 , i + 1 )
)
dim = = 2 ? [ tangents [ i ] . y , - tangents [ i ] . x ]
: let ( v = cross ( cross ( pts [ 1 ] - pts [ 0 ] , pts [ 2 ] - pts [ 0 ] ) , tangents [ i ] ) )
assert ( norm ( v ) > EPSILON , "3D path contains collinear points" )
v
2020-05-30 02:04:34 +00:00
] ;
2020-03-02 00:12:51 +00:00
// Function: path_curvature()
2020-10-04 03:29:35 +00:00
// Usage:
// curvs = path_curvature(path, <closed>);
2020-03-02 00:12:51 +00:00
// Description:
// Numerically estimate the curvature of the path (in any dimension).
function path_curvature ( path , closed = false ) =
2020-05-30 02:04:34 +00:00
let (
d1 = deriv ( path , closed = closed ) ,
d2 = deriv2 ( path , closed = closed )
) [
for ( i = idx ( path ) )
sqrt (
sqr ( norm ( d1 [ i ] ) * norm ( d2 [ i ] ) ) -
sqr ( d1 [ i ] * d2 [ i ] )
) / pow ( norm ( d1 [ i ] ) , 3 )
] ;
2020-03-02 00:12:51 +00:00
// Function: path_torsion()
2020-10-04 03:29:35 +00:00
// Usage:
// tortions = path_torsion(path, <closed>);
2020-03-02 00:12:51 +00:00
// Description:
// Numerically estimate the torsion of a 3d path.
function path_torsion ( path , closed = false ) =
2020-05-30 02:04:34 +00:00
let (
d1 = deriv ( path , closed = closed ) ,
d2 = deriv2 ( path , closed = closed ) ,
d3 = deriv3 ( path , closed = closed )
) [
for ( i = idx ( path ) ) let (
crossterm = cross ( d1 [ i ] , d2 [ i ] )
) crossterm * d3 [ i ] / sqr ( norm ( crossterm ) )
] ;
2020-03-02 00:12:51 +00:00
2019-08-17 04:22:41 +00:00
2020-12-30 08:34:25 +00:00
// Function: path_chamfer_and_rounding()
// Usage:
// path2 = path_chamfer_and_rounding(path, [closed], [chamfer], [rounding]);
// Description:
// Rounds or chamfers corners in the given path.
// Arguments:
// path = The path to chamfer and/or round.
// closed = If true, treat path like a closed polygon. Default: true
// chamfer = The length of the chamfer faces at the corners. If given as a list of numbers, gives individual chamfers for each corner, from first to last. Default: 0 (no chamfer)
// rounding = The rounding radius for the corners. If given as a list of numbers, gives individual radii for each corner, from first to last. Default: 0 (no rounding)
// Example(2D): Chamfering a Path
// path = star(5, step=2, d=100);
// path2 = path_chamfer_and_rounding(path, closed=true, chamfer=5);
// stroke(path2, closed=true);
// Example(2D): Per-Corner Chamfering
// path = star(5, step=2, d=100);
// chamfs = [for (i=[0:1:4]) each 3*[i,i]];
// path2 = path_chamfer_and_rounding(path, closed=true, chamfer=chamfs);
// stroke(path2, closed=true);
// Example(2D): Rounding a Path
// path = star(5, step=2, d=100);
// path2 = path_chamfer_and_rounding(path, closed=true, rounding=5);
// stroke(path2, closed=true);
// Example(2D): Per-Corner Chamfering
// path = star(5, step=2, d=100);
2020-12-30 08:38:24 +00:00
// rs = [for (i=[0:1:4]) each 2*[i,i]];
2020-12-30 08:34:25 +00:00
// path2 = path_chamfer_and_rounding(path, closed=true, rounding=rs);
// stroke(path2, closed=true);
// Example(2D): Mixing Chamfers and Roundings
// path = star(5, step=2, d=100);
// chamfs = [for (i=[0:4]) each [5,0]];
// rs = [for (i=[0:4]) each [0,10]];
// path2 = path_chamfer_and_rounding(path, closed=true, chamfer=chamfs, rounding=rs);
// stroke(path2, closed=true);
function path_chamfer_and_rounding ( path , closed , chamfer , rounding ) =
let (
path = deduplicate ( path , closed = true ) ,
lp = len ( path ) ,
chamfer = is_undef ( chamfer ) ? repeat ( 0 , lp ) :
is_vector ( chamfer ) ? list_pad ( chamfer , lp , 0 ) :
is_num ( chamfer ) ? repeat ( chamfer , lp ) :
assert ( false , "Bad chamfer value." ) ,
rounding = is_undef ( rounding ) ? repeat ( 0 , lp ) :
is_vector ( rounding ) ? list_pad ( rounding , lp , 0 ) :
is_num ( rounding ) ? repeat ( rounding , lp ) :
assert ( false , "Bad rounding value." ) ,
corner_paths = [
for ( i = ( closed ? [ 0 : 1 : lp - 1 ] : [ 1 : 1 : lp - 2 ] ) ) let (
p1 = select ( path , i - 1 ) ,
p2 = select ( path , i ) ,
p3 = select ( path , i + 1 )
)
chamfer [ i ] > 0 ? _corner_chamfer_path ( p1 , p2 , p3 , side = chamfer [ i ] ) :
rounding [ i ] > 0 ? _corner_roundover_path ( p1 , p2 , p3 , r = rounding [ i ] ) :
[ p2 ]
] ,
out = [
if ( ! closed ) path [ 0 ] ,
for ( i = ( closed ? [ 0 : 1 : lp - 1 ] : [ 1 : 1 : lp - 2 ] ) ) let (
p1 = select ( path , i - 1 ) ,
p2 = select ( path , i ) ,
crn1 = select ( corner_paths , i - 1 ) ,
crn2 = corner_paths [ i ] ,
l1 = norm ( select ( crn1 , - 1 ) - p1 ) ,
l2 = norm ( crn2 [ 0 ] - p2 ) ,
needed = l1 + l2 ,
seglen = norm ( p2 - p1 ) ,
check = assert ( seglen >= needed , str ( "Path segment " , i , " is too short to fulfill rounding/chamfering for the adjacent corners." ) )
) each crn2 ,
if ( ! closed ) select ( path , - 1 )
]
) deduplicate ( out ) ;
function _corner_chamfer_path ( p1 , p2 , p3 , dist1 , dist2 , side , angle ) =
let (
v1 = unit ( p1 - p2 ) ,
v2 = unit ( p3 - p2 ) ,
n = vector_axis ( v1 , v2 ) ,
ang = vector_angle ( v1 , v2 ) ,
path = ( is_num ( dist1 ) && is_undef ( dist2 ) && is_undef ( side ) ) ? (
// dist1 & optional angle
assert ( dist1 > 0 )
let ( angle = default ( angle , ( 180 - ang ) / 2 ) )
assert ( is_num ( angle ) )
assert ( angle > 0 && angle < 180 )
let (
pta = p2 + dist1 * v1 ,
a3 = 180 - angle - ang
) assert ( a3 > 0 , "Angle too extreme." )
let (
side = sin ( angle ) * dist1 / sin ( a3 ) ,
ptb = p2 + side * v2
) [ pta , ptb ]
) : ( is_undef ( dist1 ) && is_num ( dist2 ) && is_undef ( side ) ) ? (
// dist2 & optional angle
assert ( dist2 > 0 )
let ( angle = default ( angle , ( 180 - ang ) / 2 ) )
assert ( is_num ( angle ) )
assert ( angle > 0 && angle < 180 )
let (
ptb = p2 + dist2 * v2 ,
a3 = 180 - angle - ang
) assert ( a3 > 0 , "Angle too extreme." )
let (
side = sin ( angle ) * dist2 / sin ( a3 ) ,
pta = p2 + side * v1
) [ pta , ptb ]
) : ( is_undef ( dist1 ) && is_undef ( dist2 ) && is_num ( side ) ) ? (
// side & optional angle
assert ( side > 0 )
let ( angle = default ( angle , ( 180 - ang ) / 2 ) )
assert ( is_num ( angle ) )
assert ( angle > 0 && angle < 180 )
let (
a3 = 180 - angle - ang
) assert ( a3 > 0 , "Angle too extreme." )
let (
dist1 = sin ( a3 ) * side / sin ( ang ) ,
dist2 = sin ( angle ) * side / sin ( ang ) ,
pta = p2 + dist1 * v1 ,
ptb = p2 + dist2 * v2
) [ pta , ptb ]
) : ( is_num ( dist1 ) && is_num ( dist2 ) && is_undef ( side ) && is_undef ( side ) ) ? (
// dist1 & dist2
assert ( dist1 > 0 )
assert ( dist2 > 0 )
let (
pta = p2 + dist1 * v1 ,
ptb = p2 + dist2 * v2
) [ pta , ptb ]
) : (
assert ( false , "Bad arguments." )
)
) path ;
function _corner_roundover_path ( p1 , p2 , p3 , r , d ) =
let (
r = get_radius ( r = r , d = d , dflt = undef ) ,
res = circle_2tangents ( p1 , p2 , p3 , r = r , tangents = true ) ,
cp = res [ 0 ] ,
n = res [ 1 ] ,
tp1 = res [ 2 ] ,
ang = res [ 4 ] + res [ 5 ] ,
steps = floor ( segs ( r ) * ang / 360 + 0.5 ) ,
step = ang / steps ,
path = [ for ( i = [ 0 : 1 : steps ] ) move ( cp , p = rot ( a = - i * step , v = n , p = tp1 - cp ) ) ]
) path ;
2019-03-23 04:13:18 +00:00
// Function: path3d_spiral()
// Description:
// Returns a 3D spiral path.
// Usage:
// path3d_spiral(turns, h, n, r|d, [cp], [scale]);
// Arguments:
// h = Height of spiral.
// turns = Number of turns in spiral.
// n = Number of spiral sides.
// r = Radius of spiral.
// d = Radius of spiral.
// cp = Centerpoint of spiral. Default: `[0,0]`
// scale = [X,Y] scaling factors for each axis. Default: `[1,1]`
// Example(3D):
2020-11-17 01:50:08 +00:00
// trace_path(path3d_spiral(turns=2.5, h=100, n=24, r=50), N=1, showpts=true);
2020-08-27 03:39:45 +00:00
function path3d_spiral ( turns = 3 , h = 100 , n = 12 , r , d , cp = [ 0 , 0 ] , scale = [ 1 , 1 ] ) = let (
2020-05-30 02:04:34 +00:00
rr = get_radius ( r = r , d = d , dflt = 100 ) ,
cnt = floor ( turns * n ) ,
dz = h / cnt
) [
for ( i = [ 0 : 1 : cnt ] ) [
rr * cos ( i * 360 / n ) * scale . x + cp . x ,
rr * sin ( i * 360 / n ) * scale . y + cp . y ,
i * dz
]
] ;
2019-03-23 04:13:18 +00:00
2020-01-30 22:00:10 +00:00
// Function: path_self_intersections()
// Usage:
// isects = path_self_intersections(path, [eps]);
// Description:
// Locates all self intersections of the given path. Returns a list of intersections, where
// each intersection is a list like [POINT, SEGNUM1, PROPORTION1, SEGNUM2, PROPORTION2] where
// POINT is the coordinates of the intersection point, SEGNUMs are the integer indices of the
// intersecting segments along the path, and the PROPORTIONS are the 0.0 to 1.0 proportions
// of how far along those segments they intersect at. A proportion of 0.0 indicates the start
// of the segment, and a proportion of 1.0 indicates the end of the segment.
// Arguments:
// path = The path to find self intersections of.
// closed = If true, treat path like a closed polygon. Default: true
// eps = The epsilon error value to determine whether two points coincide. Default: `EPSILON` (1e-9)
// Example(2D):
// path = [
// [-100,100], [0,-50], [100,100], [100,-100], [0,50], [-100,-100]
// ];
// isects = path_self_intersections(path, closed=true);
// // isects == [[[-33.3333, 0], 0, 0.666667, 4, 0.333333], [[33.3333, 0], 1, 0.333333, 3, 0.666667]]
// stroke(path, closed=true, width=1);
// for (isect=isects) translate(isect[0]) color("blue") sphere(d=10);
function path_self_intersections ( path , closed = true , eps = EPSILON ) =
2020-05-30 02:04:34 +00:00
let (
path = cleanup_path ( path , eps = eps ) ,
plen = len ( path )
) [
for ( i = [ 0 : 1 : plen - ( closed ? 2 : 3 ) ] , j = [ i + 1 : 1 : plen - ( closed ? 1 : 2 ) ] ) let (
a1 = path [ i ] ,
a2 = path [ ( i + 1 ) % plen ] ,
b1 = path [ j ] ,
b2 = path [ ( j + 1 ) % plen ] ,
isect =
( max ( a1 . x , a2 . x ) < min ( b1 . x , b2 . x ) ) ? undef :
( min ( a1 . x , a2 . x ) > max ( b1 . x , b2 . x ) ) ? undef :
( max ( a1 . y , a2 . y ) < min ( b1 . y , b2 . y ) ) ? undef :
( min ( a1 . y , a2 . y ) > max ( b1 . y , b2 . y ) ) ? undef :
let (
c = a1 - a2 ,
d = b1 - b2 ,
denom = ( c . x * d . y ) - ( c . y * d . x )
) abs ( denom ) < eps ? undef : let (
e = a1 - b1 ,
t = ( ( e . x * d . y ) - ( e . y * d . x ) ) / denom ,
u = ( ( e . x * c . y ) - ( e . y * c . x ) ) / denom
) [ a1 + t * ( a2 - a1 ) , t , u ]
) if (
isect ! = undef &&
isect [ 1 ] > eps && isect [ 1 ] < = 1 + eps &&
isect [ 2 ] > eps && isect [ 2 ] < = 1 + eps
) [ isect [ 0 ] , i , isect [ 1 ] , j , isect [ 2 ] ]
] ;
2020-01-30 22:00:10 +00:00
2020-02-01 00:37:27 +00:00
// Function: split_path_at_self_crossings()
// Usage:
2020-11-17 01:50:08 +00:00
// paths = split_path_at_self_crossings(path, [closed], [eps]);
2020-02-01 00:37:27 +00:00
// Description:
2020-11-17 01:50:08 +00:00
// Splits a path into sub-paths wherever the original path crosses itself.
2020-02-01 00:37:27 +00:00
// Splits may occur mid-segment, so new vertices will be created at the intersection points.
// Arguments:
// path = The path to split up.
// closed = If true, treat path as a closed polygon. Default: true
// eps = Acceptable variance. Default: `EPSILON` (1e-9)
// Example(2D):
// path = [ [-100,100], [0,-50], [100,100], [100,-100], [0,50], [-100,-100] ];
2020-11-17 01:50:08 +00:00
// paths = split_path_at_self_crossings(path);
// rainbow(paths) stroke($item, closed=false, width=2);
2020-02-01 00:37:27 +00:00
function split_path_at_self_crossings ( path , closed = true , eps = EPSILON ) =
2020-05-30 02:04:34 +00:00
let (
path = cleanup_path ( path , eps = eps ) ,
isects = deduplicate (
eps = eps ,
concat (
[ [ 0 , 0 ] ] ,
sort ( [
for (
a = path_self_intersections ( path , closed = closed , eps = eps ) ,
ss = [ [ a [ 1 ] , a [ 2 ] ] , [ a [ 3 ] , a [ 4 ] ] ]
) if ( ss [ 0 ] ! = undef ) ss
] ) ,
[ [ len ( path ) - ( closed ? 1 : 2 ) , 1 ] ]
)
)
) [
for ( p = pair ( isects ) )
let (
s1 = p [ 0 ] [ 0 ] ,
u1 = p [ 0 ] [ 1 ] ,
s2 = p [ 1 ] [ 0 ] ,
u2 = p [ 1 ] [ 1 ] ,
section = path_subselect ( path , s1 , u1 , s2 , u2 , closed = closed ) ,
outpath = deduplicate ( eps = eps , section )
)
outpath
] ;
2020-02-01 00:37:27 +00:00
2020-01-30 22:00:10 +00:00
function _tag_self_crossing_subpaths ( path , closed = true , eps = EPSILON ) =
2020-05-30 02:04:34 +00:00
let (
subpaths = split_path_at_self_crossings (
path , closed = closed , eps = eps
)
) [
for ( subpath = subpaths ) let (
seg = select ( subpath , 0 , 1 ) ,
mp = mean ( seg ) ,
n = line_normal ( seg ) / 2048 ,
p1 = mp + n ,
p2 = mp - n ,
p1in = point_in_polygon ( p1 , path ) >= 0 ,
p2in = point_in_polygon ( p2 , path ) >= 0 ,
tag = ( p1in && p2in ) ? "I" : "O"
) [ tag , subpath ]
] ;
2020-01-30 22:00:10 +00:00
// Function: decompose_path()
// Usage:
// splitpaths = decompose_path(path, [closed], [eps]);
// Description:
// Given a possibly self-crossing path, decompose it into non-crossing paths that are on the perimeter
// of the areas bounded by that path.
// Arguments:
// path = The path to split up.
// closed = If true, treat path like a closed polygon. Default: true
// eps = The epsilon error value to determine whether two points coincide. Default: `EPSILON` (1e-9)
// Example(2D):
// path = [
// [-100,100], [0,-50], [100,100], [100,-100], [0,50], [-100,-100]
// ];
// splitpaths = decompose_path(path, closed=true);
// rainbow(splitpaths) stroke($item, closed=true, width=3);
function decompose_path ( path , closed = true , eps = EPSILON ) =
2020-05-30 02:04:34 +00:00
let (
path = cleanup_path ( path , eps = eps ) ,
tagged = _tag_self_crossing_subpaths ( path , closed = closed , eps = eps ) ,
kept = [ for ( sub = tagged ) if ( sub [ 0 ] = = "O" ) sub [ 1 ] ] ,
completed = [ for ( frag = kept ) if ( is_closed_path ( frag ) ) frag ] ,
incomplete = [ for ( frag = kept ) if ( ! is_closed_path ( frag ) ) frag ] ,
defrag = _path_fast_defragment ( incomplete , eps = eps ) ,
completed2 = assemble_path_fragments ( defrag , eps = eps )
) concat ( completed2 , completed ) ;
2020-04-01 08:36:48 +00:00
function _path_fast_defragment ( fragments , eps = EPSILON , _done = [ ] ) =
2020-05-30 02:04:34 +00:00
len ( fragments ) = = 0 ? _done :
let (
path = fragments [ 0 ] ,
endpt = select ( path , - 1 ) ,
extenders = [
for ( i = [ 1 : 1 : len ( fragments ) - 1 ] ) let (
test1 = approx ( endpt , fragments [ i ] [ 0 ] , eps = eps ) ,
test2 = approx ( endpt , select ( fragments [ i ] , - 1 ) , eps = eps )
) if ( test1 || test2 ) ( test1 ? i : - 1 )
]
) len ( extenders ) = = 1 && extenders [ 0 ] >= 0 ? _path_fast_defragment (
fragments = [
concat ( select ( path , 0 , - 2 ) , fragments [ extenders [ 0 ] ] ) ,
for ( i = [ 1 : 1 : len ( fragments ) - 1 ] )
if ( i ! = extenders [ 0 ] ) fragments [ i ]
] ,
eps = eps ,
_done = _done
) : _path_fast_defragment (
fragments = [ for ( i = [ 1 : 1 : len ( fragments ) - 1 ] ) fragments [ i ] ] ,
eps = eps ,
_done = concat ( _done , [ deduplicate ( path , closed = true , eps = eps ) ] )
) ;
2020-01-30 22:00:10 +00:00
function _extreme_angle_fragment ( seg , fragments , rightmost = true , eps = EPSILON ) =
2020-05-30 02:04:34 +00:00
! fragments ? [ undef , [ ] ] :
let (
delta = seg [ 1 ] - seg [ 0 ] ,
segang = atan2 ( delta . y , delta . x ) ,
frags = [
for ( i = idx ( fragments ) ) let (
fragment = fragments [ i ] ,
fwdmatch = approx ( seg [ 1 ] , fragment [ 0 ] , eps = eps ) ,
bakmatch = approx ( seg [ 1 ] , select ( fragment , - 1 ) , eps = eps )
) [
fwdmatch ,
bakmatch ,
bakmatch ? reverse ( fragment ) : fragment
]
] ,
angs = [
for ( frag = frags )
( frag [ 0 ] || frag [ 1 ] ) ? let (
delta2 = frag [ 2 ] [ 1 ] - frag [ 2 ] [ 0 ] ,
segang2 = atan2 ( delta2 . y , delta2 . x )
) modang ( segang2 - segang ) : (
rightmost ? 999 : - 999
)
] ,
fi = rightmost ? min_index ( angs ) : max_index ( angs )
) abs ( angs [ fi ] ) > 360 ? [ undef , fragments ] : let (
remainder = [ for ( i = idx ( fragments ) ) if ( i ! = fi ) fragments [ i ] ] ,
frag = frags [ fi ] ,
foundfrag = frag [ 2 ]
) [ foundfrag , remainder ] ;
2020-01-30 22:00:10 +00:00
// Function: assemble_a_path_from_fragments()
// Usage:
// assemble_a_path_from_fragments(subpaths);
// Description:
2020-11-17 01:50:08 +00:00
// Given a list of paths, assembles them together into one complete closed polygon path, and
2020-01-30 22:00:10 +00:00
// remainder fragments. Returns [PATH, FRAGMENTS] where FRAGMENTS is the list of remaining
2020-11-17 01:50:08 +00:00
// unused path fragments.
2020-01-30 22:00:10 +00:00
// Arguments:
2020-11-17 01:50:08 +00:00
// fragments = List of paths to be assembled into complete polygons.
2020-01-30 22:00:10 +00:00
// rightmost = If true, assemble paths using rightmost turns. Leftmost if false.
2020-04-01 08:36:48 +00:00
// startfrag = The fragment to start with. Default: 0
2020-01-30 22:00:10 +00:00
// eps = The epsilon error value to determine whether two points coincide. Default: `EPSILON` (1e-9)
2020-04-01 08:36:48 +00:00
function assemble_a_path_from_fragments ( fragments , rightmost = true , startfrag = 0 , eps = EPSILON ) =
2020-05-30 02:04:34 +00:00
len ( fragments ) = = 0 ? _finished :
let (
path = fragments [ startfrag ] ,
newfrags = [ for ( i = idx ( fragments ) ) if ( i ! = startfrag ) fragments [ i ] ]
) is_closed_path ( path , eps = eps ) ? (
// starting fragment is already closed
[ path , newfrags ]
) : let (
// Find rightmost/leftmost continuation fragment
seg = select ( path , - 2 , - 1 ) ,
extrema = _extreme_angle_fragment ( seg = seg , fragments = newfrags , rightmost = rightmost , eps = eps ) ,
foundfrag = extrema [ 0 ] ,
remainder = extrema [ 1 ]
) is_undef ( foundfrag ) ? (
// No remaining fragments connect! INCOMPLETE PATH!
// Treat it as complete.
[ path , remainder ]
) : is_closed_path ( foundfrag , eps = eps ) ? (
// Found fragment is already closed
[ foundfrag , concat ( [ path ] , remainder ) ]
) : let (
fragend = select ( foundfrag , - 1 ) ,
2021-01-25 07:26:39 +00:00
hits = [ for ( i = idx ( path , e = - 2 ) ) if ( approx ( path [ i ] , fragend , eps = eps ) ) i ]
2020-05-30 02:04:34 +00:00
) hits ? (
let (
// Found fragment intersects with initial path
hitidx = select ( hits , - 1 ) ,
newpath = slice ( path , 0 , hitidx + 1 ) ,
newfrags = concat ( len ( newpath ) > 1 ? [ newpath ] : [ ] , remainder ) ,
outpath = concat ( slice ( path , hitidx , - 2 ) , foundfrag )
)
[ outpath , newfrags ]
) : let (
// Path still incomplete. Continue building it.
newpath = concat ( path , slice ( foundfrag , 1 , - 1 ) ) ,
newfrags = concat ( [ newpath ] , remainder )
)
assemble_a_path_from_fragments (
fragments = newfrags ,
rightmost = rightmost ,
eps = eps
) ;
2020-01-30 22:00:10 +00:00
// Function: assemble_path_fragments()
// Usage:
// assemble_path_fragments(subpaths);
// Description:
2020-11-17 01:50:08 +00:00
// Given a list of paths, assembles them together into complete closed polygon paths if it can.
2020-01-30 22:00:10 +00:00
// Arguments:
2020-11-17 01:50:08 +00:00
// fragments = List of paths to be assembled into complete polygons.
2020-01-30 22:00:10 +00:00
// eps = The epsilon error value to determine whether two points coincide. Default: `EPSILON` (1e-9)
2020-03-12 05:14:16 +00:00
function assemble_path_fragments ( fragments , eps = EPSILON , _finished = [ ] ) =
2020-05-30 02:04:34 +00:00
len ( fragments ) = = 0 ? _finished :
let (
minxidx = min_index ( [
for ( frag = fragments ) min ( subindex ( frag , 0 ) )
] ) ,
result_l = assemble_a_path_from_fragments (
fragments = fragments ,
startfrag = minxidx ,
rightmost = false ,
eps = eps
) ,
result_r = assemble_a_path_from_fragments (
fragments = fragments ,
startfrag = minxidx ,
rightmost = true ,
eps = eps
) ,
l_area = abs ( polygon_area ( result_l [ 0 ] ) ) ,
r_area = abs ( polygon_area ( result_r [ 0 ] ) ) ,
result = l_area < r_area ? result_l : result_r ,
newpath = cleanup_path ( result [ 0 ] ) ,
remainder = result [ 1 ] ,
finished = concat ( _finished , [ newpath ] )
) assemble_path_fragments (
fragments = remainder ,
eps = eps ,
_finished = finished
) ;
2020-01-30 22:00:10 +00:00
2019-03-23 04:13:18 +00:00
// Section: 2D Modules
// Module: modulated_circle()
2020-08-27 03:39:45 +00:00
// Usage:
// modulated_circle(r|d, sines);
2019-03-23 04:13:18 +00:00
// Description:
// Creates a 2D polygon circle, modulated by one or more superimposed sine waves.
// Arguments:
2020-08-27 03:39:45 +00:00
// r = Radius of the base circle. Default: 40
// d = Diameter of the base circle.
2020-12-01 22:56:57 +00:00
// sines = array of [amplitude, frequency] pairs or [amplitude, frequency, phase] triples, where the frequency is the number of times the cycle repeats around the circle.
2019-03-23 04:13:18 +00:00
// Example(2D):
2017-08-30 00:00:16 +00:00
// modulated_circle(r=40, sines=[[3, 11], [1, 31]], $fn=6);
2020-12-01 22:56:57 +00:00
module modulated_circle ( r , sines = [ [ 1 , 1 ] ] , d )
2017-08-30 00:00:16 +00:00
{
2020-08-27 03:39:45 +00:00
r = get_radius ( r = r , d = d , dflt = 40 ) ;
2020-12-01 22:56:57 +00:00
assert ( is_list ( sines )
&& all ( [ for ( s = sines ) is_vector ( s , 2 ) || is_vector ( s , 3 ) ] ) ,
"sines must be given as a list of pairs or triples" ) ;
sines_ = [ for ( s = sines ) [ s [ 0 ] , s [ 1 ] , len ( s ) = = 2 ? 0 : s [ 2 ] ] ] ;
freqs = len ( sines_ ) > 0 ? [ for ( i = sines_ ) i [ 1 ] ] : [ 5 ] ;
2020-05-30 02:04:34 +00:00
points = [
for ( a = [ 0 : ( 360 / segs ( r ) / max ( freqs ) ) : 360 ] )
2020-12-01 22:56:57 +00:00
let ( nr = r + sum_of_sines ( a , sines_ ) ) [ nr * cos ( a ) , nr * sin ( a ) ]
2020-05-30 02:04:34 +00:00
] ;
polygon ( points ) ;
2017-08-30 00:00:16 +00:00
}
2019-03-23 04:13:18 +00:00
// Section: 3D Modules
// Module: extrude_from_to()
// Description:
// Extrudes a 2D shape between the points pt1 and pt2. Takes as children a set of 2D shapes to extrude.
// Arguments:
2019-02-03 08:12:37 +00:00
// pt1 = starting point of extrusion.
// pt2 = ending point of extrusion.
// convexity = max number of times a line could intersect a wall of the 2D shape being extruded.
// twist = number of degrees to twist the 2D shape over the entire extrusion length.
// scale = scale multiplier for end of extrusion compared the start.
// slices = Number of slices along the extrusion to break the extrusion into. Useful for refining `twist` extrusions.
2021-02-20 03:56:43 +00:00
// Example(FlatSpin,VPD=200,VPT=[0,0,15]):
2019-02-03 08:12:37 +00:00
// extrude_from_to([0,0,0], [10,20,30], convexity=4, twist=360, scale=3.0, slices=40) {
2020-03-25 02:11:05 +00:00
// xcopies(3) circle(3, $fn=32);
2019-02-03 08:12:37 +00:00
// }
2020-11-30 04:23:03 +00:00
module extrude_from_to ( pt1 , pt2 , convexity , twist , scale , slices ) {
2020-05-30 02:04:34 +00:00
rtp = xyz_to_spherical ( pt2 - pt1 ) ;
translate ( pt1 ) {
rotate ( [ 0 , rtp [ 2 ] , rtp [ 1 ] ] ) {
2020-11-30 04:23:03 +00:00
if ( rtp [ 0 ] > 0 ) {
linear_extrude ( height = rtp [ 0 ] , convexity = convexity , center = false , slices = slices , twist = twist , scale = scale ) {
children ( ) ;
}
2020-05-30 02:04:34 +00:00
}
}
}
2019-02-03 08:12:37 +00:00
}
2019-06-24 07:32:13 +00:00
// Module: spiral_sweep()
2019-03-23 04:13:18 +00:00
// Description:
2021-01-31 23:43:50 +00:00
// Takes a closed 2D polygon path, centered on the XY plane, and sweeps/extrudes it along a 3D spiral path.
2020-11-17 01:50:08 +00:00
// of a given radius, height and twist.
2019-03-23 04:13:18 +00:00
// Arguments:
2021-01-31 23:43:50 +00:00
// poly = Array of points of a polygon path, to be extruded.
2017-08-30 00:00:16 +00:00
// h = height of the spiral to extrude along.
2020-08-27 03:39:45 +00:00
// r = Radius of the spiral to extrude along. Default: 50
2017-08-30 00:00:16 +00:00
// twist = number of degrees of rotation to spiral up along height.
2021-01-31 23:43:50 +00:00
// ---
// d = Diameter of the spiral to extrude along.
// higbee = Length to taper thread ends over.
2019-05-26 19:47:50 +00:00
// anchor = Translate so anchor point is at origin (0,0,0). See [anchor](attachments.scad#anchor). Default: `CENTER`
// spin = Rotate this many degrees around the Z axis after anchor. See [spin](attachments.scad#spin). Default: `0`
// orient = Vector to rotate top towards, after spin. See [orient](attachments.scad#orient). Default: `UP`
2019-05-03 18:32:40 +00:00
// center = If given, overrides `anchor`. A true value sets `anchor=CENTER`, false sets `anchor=BOTTOM`.
2017-08-30 00:00:16 +00:00
// Example:
// poly = [[-10,0], [-3,-5], [3,-5], [10,0], [0,-30]];
2019-06-24 07:32:13 +00:00
// spiral_sweep(poly, h=200, r=50, twist=1080, $fn=36);
2021-01-31 23:43:50 +00:00
module spiral_sweep ( poly , h , r , twist = 360 , higbee , center , r1 , r2 , d , d1 , d2 , higbee1 , higbee2 , anchor , spin = 0 , orient = UP ) {
2020-11-17 01:50:08 +00:00
poly = path3d ( poly ) ;
2020-05-30 02:04:34 +00:00
anchor = get_anchor ( anchor , center , BOT , BOT ) ;
2021-01-31 23:43:50 +00:00
r1 = get_radius ( r1 = r1 , r = r , d1 = d1 , d = d , dflt = 50 ) ;
r2 = get_radius ( r1 = r2 , r = r , d1 = d2 , d = d , dflt = 50 ) ;
sides = segs ( max ( r1 , r2 ) ) ;
steps = ceil ( sides * ( twist / 360 ) ) ;
higbee1 = first_defined ( [ higbee1 , higbee , 0 ] ) ;
higbee2 = first_defined ( [ higbee2 , higbee , 0 ] ) ;
higang1 = 360 * higbee1 / ( 2 * r1 * PI ) ;
higang2 = 360 * higbee2 / ( 2 * r2 * PI ) ;
higsteps1 = ceil ( higang1 / 360 * sides ) ;
higsteps2 = ceil ( higang2 / 360 * sides ) ;
assert ( higang1 < twist / 2 ) ;
assert ( higang2 < twist / 2 ) ;
function higsize ( a ) = lookup ( a , [
[ - 0.001 , 0 ] ,
for ( x = [ 0.125 : 0.125 : 1 ] ) [ x * higang1 , pow ( x , 1 / 2 ) ] ,
for ( x = [ 0.125 : 0.125 : 1 ] ) [ twist - x * higang2 , pow ( x , 1 / 2 ) ] ,
[ twist + 0.001 , 0 ]
] ) ;
us = [
for ( i = [ 0 : higsteps1 / 10 : higsteps1 ] ) i ,
for ( i = [ higsteps1 + 1 : 1 : steps - higsteps2 - 1 ] ) i ,
for ( i = [ steps - higsteps2 : higsteps2 / 10 : steps ] ) i ,
] ;
zang = atan2 ( r2 - r1 , h ) ;
points = [
for ( p = us ) let (
u = p / steps ,
a = twist * u ,
hsc = higsize ( a ) ,
r = lerp ( r1 , r2 , u ) ,
mat = affine3d_zrot ( a ) *
affine3d_translate ( [ r , 0 , h * ( u - 0.5 ) ] ) *
affine3d_xrot ( 90 ) *
affine3d_skew_xz ( xa = zang ) *
affine3d_scale ( [ hsc , lerp ( hsc , 1 , 0.25 ) , 1 ] ) ,
2021-01-20 21:36:41 +00:00
pts = apply ( mat , poly )
2021-01-31 23:43:50 +00:00
) pts
2020-05-30 02:04:34 +00:00
] ;
2021-01-31 23:43:50 +00:00
vnf = vnf_vertex_array (
points , col_wrap = true , caps = true ,
style = ( abs ( higbee1 ) + abs ( higbee2 ) ) > 0 ? "quincunx" : "alt"
2020-05-30 02:04:34 +00:00
) ;
2021-01-31 23:43:50 +00:00
attachable ( anchor , spin , orient , r1 = r1 , r2 = r2 , l = h ) {
vnf_polyhedron ( vnf , convexity = 2 * twist / 360 ) ;
2020-05-30 02:04:34 +00:00
children ( ) ;
}
2017-08-30 00:00:16 +00:00
}
2019-06-24 07:32:13 +00:00
// Module: path_extrude()
2019-03-23 04:13:18 +00:00
// Description:
2020-11-17 01:50:08 +00:00
// Extrudes 2D children along a 3D path. This may be slow.
2019-03-23 04:13:18 +00:00
// Arguments:
2018-11-24 09:37:56 +00:00
// path = array of points for the bezier path to extrude along.
// convexity = maximum number of walls a ran can pass through.
// clipsize = increase if artifacts are left. Default: 1000
2021-02-20 03:56:43 +00:00
// Example(FlatSpin,VPD=600,VPT=[75,16,20]):
2019-03-23 04:13:18 +00:00
// path = [ [0, 0, 0], [33, 33, 33], [66, 33, 40], [100, 0, 0], [150,0,0] ];
2019-06-24 07:32:13 +00:00
// path_extrude(path) circle(r=10, $fn=6);
module path_extrude ( path , convexity = 10 , clipsize = 100 ) {
2020-05-30 02:04:34 +00:00
function polyquats ( path , q = Q_Ident ( ) , v = [ 0 , 0 , 1 ] , i = 0 ) = let (
v2 = path [ i + 1 ] - path [ i ] ,
ang = vector_angle ( v , v2 ) ,
axis = ang > 0.001 ? unit ( cross ( v , v2 ) ) : [ 0 , 0 , 1 ] ,
newq = Q_Mul ( Quat ( axis , ang ) , q ) ,
dist = norm ( v2 )
) i < ( len ( path ) - 2 ) ?
concat ( [ [ dist , newq , ang ] ] , polyquats ( path , newq , v2 , i + 1 ) ) :
[ [ dist , newq , ang ] ] ;
epsilon = 0.0001 ; // Make segments ever so slightly too long so they overlap.
ptcount = len ( path ) ;
pquats = polyquats ( path ) ;
for ( i = [ 0 : 1 : ptcount - 2 ] ) {
pt1 = path [ i ] ;
pt2 = path [ i + 1 ] ;
dist = pquats [ i ] [ 0 ] ;
q = pquats [ i ] [ 1 ] ;
difference ( ) {
translate ( pt1 ) {
Qrot ( q ) {
down ( clipsize / 2 / 2 ) {
2020-11-30 04:23:03 +00:00
if ( ( dist + clipsize / 2 ) > 0 ) {
linear_extrude ( height = dist + clipsize / 2 , convexity = convexity ) {
children ( ) ;
}
2020-05-30 02:04:34 +00:00
}
}
}
}
translate ( pt1 ) {
hq = ( i > 0 ) ? Q_Slerp ( q , pquats [ i - 1 ] [ 1 ] , 0.5 ) : q ;
Qrot ( hq ) down ( clipsize / 2 + epsilon ) cube ( clipsize , center = true ) ;
}
translate ( pt2 ) {
hq = ( i < ptcount - 2 ) ? Q_Slerp ( q , pquats [ i + 1 ] [ 1 ] , 0.5 ) : q ;
Qrot ( hq ) up ( clipsize / 2 + epsilon ) cube ( clipsize , center = true ) ;
}
}
}
2018-11-24 09:37:56 +00:00
}
2019-07-01 23:25:00 +00:00
// Module: path_spread()
//
// Description:
// Uniformly spreads out copies of children along a path. Copies are located based on path length. If you specify `n` but not spacing then `n` copies will be placed
// with one at path[0] of `closed` is true, or spanning the entire path from start to end if `closed` is false.
// If you specify `spacing` but not `n` then copies will spread out starting from one at path[0] for `closed=true` or at the path center for open paths.
2019-08-09 20:07:18 +00:00
// If you specify `sp` then the copies will start at `sp`.
2019-07-01 23:25:00 +00:00
//
// Usage:
// path_spread(path), [n], [spacing], [sp], [rotate_children], [closed]) ...
//
// Arguments:
// path = the path where children are placed
// n = number of copies
// spacing = space between copies
// sp = if given, copies will start distance sp from the path start and spread beyond that point
//
// Side Effects:
// `$pos` is set to the center of each copy
2019-08-09 20:07:18 +00:00
// `$idx` is set to the index number of each copy. In the case of closed paths the first copy is at `path[0]` unless you give `sp`.
2019-07-01 23:25:00 +00:00
// `$dir` is set to the direction vector of the path at the point where the copy is placed.
// `$normal` is set to the direction of the normal vector to the path direction that is coplanar with the path at this point
2019-08-09 20:07:18 +00:00
//
2019-07-01 23:25:00 +00:00
// Example(2D):
// spiral = [for(theta=[0:360*8]) theta * [cos(theta), sin(theta)]]/100;
// stroke(spiral,width=.25);
// color("red") path_spread(spiral, n=100) circle(r=1);
// Example(2D):
// circle = regular_ngon(n=64, or=10);
2019-07-12 20:11:13 +00:00
// stroke(circle,width=1,closed=true);
2020-04-26 08:16:20 +00:00
// color("green") path_spread(circle, n=7, closed=true) circle(r=1+$idx/3);
2019-08-09 20:07:18 +00:00
// Example(2D):
2019-07-01 23:25:00 +00:00
// heptagon = regular_ngon(n=7, or=10);
2019-07-12 20:11:13 +00:00
// stroke(heptagon, width=1, closed=true);
2020-04-26 08:16:20 +00:00
// color("purple") path_spread(heptagon, n=9, closed=true) rect([0.5,3],anchor=FRONT);
2019-07-01 23:25:00 +00:00
// Example(2D): Direction at the corners is the average of the two adjacent edges
// heptagon = regular_ngon(n=7, or=10);
2019-07-12 20:11:13 +00:00
// stroke(heptagon, width=1, closed=true);
2020-04-26 08:16:20 +00:00
// color("purple") path_spread(heptagon, n=7, closed=true) rect([0.5,3],anchor=FRONT);
2019-07-01 23:25:00 +00:00
// Example(2D): Don't rotate the children
// heptagon = regular_ngon(n=7, or=10);
2019-07-12 20:11:13 +00:00
// stroke(heptagon, width=1, closed=true);
2020-04-26 08:16:20 +00:00
// color("red") path_spread(heptagon, n=9, closed=true, rotate_children=false) rect([0.5,3],anchor=FRONT);
2019-07-01 23:25:00 +00:00
// Example(2D): Open path, specify `n`
// sinwav = [for(theta=[0:360]) 5*[theta/180, sin(theta)]];
// stroke(sinwav,width=.1);
2020-04-26 08:16:20 +00:00
// color("red") path_spread(sinwav, n=5) rect([.2,1.5],anchor=FRONT);
2020-07-27 22:15:34 +00:00
// Example(2D): Open path, specify `n` and `spacing`
2019-07-01 23:25:00 +00:00
// sinwav = [for(theta=[0:360]) 5*[theta/180, sin(theta)]];
// stroke(sinwav,width=.1);
2020-04-26 08:16:20 +00:00
// color("red") path_spread(sinwav, n=5, spacing=1) rect([.2,1.5],anchor=FRONT);
2020-07-27 22:15:34 +00:00
// Example(2D): Closed path, specify `n` and `spacing`, copies centered around circle[0]
2019-07-01 23:25:00 +00:00
// circle = regular_ngon(n=64,or=10);
2019-07-12 20:11:13 +00:00
// stroke(circle,width=.1,closed=true);
2020-04-26 08:16:20 +00:00
// color("red") path_spread(circle, n=10, spacing=1, closed=true) rect([.2,1.5],anchor=FRONT);
2019-07-01 23:25:00 +00:00
// Example(2D): Open path, specify `spacing`
// sinwav = [for(theta=[0:360]) 5*[theta/180, sin(theta)]];
// stroke(sinwav,width=.1);
2020-04-26 08:16:20 +00:00
// color("red") path_spread(sinwav, spacing=5) rect([.2,1.5],anchor=FRONT);
2019-07-01 23:25:00 +00:00
// Example(2D): Open path, specify `sp`
// sinwav = [for(theta=[0:360]) 5*[theta/180, sin(theta)]];
// stroke(sinwav,width=.1);
2020-04-26 08:16:20 +00:00
// color("red") path_spread(sinwav, n=5, sp=18) rect([.2,1.5],anchor=FRONT);
2019-08-09 20:07:18 +00:00
// Example(2D):
2019-07-01 23:25:00 +00:00
// wedge = arc(angle=[0,100], r=10, $fn=64);
// difference(){
// polygon(concat([[0,0]],wedge));
2020-04-26 08:16:20 +00:00
// path_spread(wedge,n=5,spacing=3) fwd(.1) rect([1,4],anchor=FRONT);
2019-07-01 23:25:00 +00:00
// }
2021-02-20 03:56:43 +00:00
// Example(Spin,VPD=115): 3d example, with children rotated into the plane of the path
2019-07-01 23:25:00 +00:00
// tilted_circle = lift_plane(regular_ngon(n=64, or=12), [0,0,0], [5,0,5], [0,2,3]);
// path_sweep(regular_ngon(n=16,or=.1),tilted_circle);
// path_spread(tilted_circle, n=15,closed=true) {
2020-04-26 08:16:20 +00:00
// color("blue") cyl(h=3,r=.2, anchor=BOTTOM); // z-aligned cylinder
// color("red") xcyl(h=10,r=.2, anchor=FRONT+LEFT); // x-aligned cylinder
2019-07-01 23:25:00 +00:00
// }
2021-02-20 03:56:43 +00:00
// Example(Spin,VPD=115): 3d example, with rotate_children set to false
2019-07-01 23:25:00 +00:00
// tilted_circle = lift_plane(regular_ngon(n=64, or=12), [0,0,0], [5,0,5], [0,2,3]);
// path_sweep(regular_ngon(n=16,or=.1),tilted_circle);
// path_spread(tilted_circle, n=25,rotate_children=false,closed=true) {
2020-04-26 08:16:20 +00:00
// color("blue") cyl(h=3,r=.2, anchor=BOTTOM); // z-aligned cylinder
// color("red") xcyl(h=10,r=.2, anchor=FRONT+LEFT); // x-aligned cylinder
2019-07-01 23:25:00 +00:00
// }
module path_spread ( path , n , spacing , sp = undef , rotate_children = true , closed = false )
{
2020-05-30 02:04:34 +00:00
length = path_length ( path , closed ) ;
distances = is_def ( sp ) ? (
is_def ( n ) && is_def ( spacing ) ? list_range ( s = sp , step = spacing , n = n ) :
is_def ( n ) ? list_range ( s = sp , e = length , n = n ) :
list_range ( s = sp , step = spacing , e = length )
) : is_def ( n ) && is_undef ( spacing ) ? (
closed ?
let ( range = list_range ( s = 0 , e = length , n = n + 1 ) ) slice ( range , 0 , - 2 ) :
list_range ( s = 0 , e = length , n = n )
) : (
let (
n = is_def ( n ) ? n : floor ( length / spacing ) + ( closed ? 0 : 1 ) ,
ptlist = list_range ( s = 0 , step = spacing , n = n ) ,
listcenter = mean ( ptlist )
) closed ?
sort ( [ for ( entry = ptlist ) posmod ( entry - listcenter , length ) ] ) :
[ for ( entry = ptlist ) entry + length / 2 - listcenter ]
) ;
2021-01-21 00:33:24 +00:00
distOK = is_def ( n ) || ( min ( distances ) >= 0 && max ( distances ) < = length ) ;
2020-05-30 02:04:34 +00:00
assert ( distOK , "Cannot fit all of the copies" ) ;
2021-03-06 10:26:39 +00:00
cutlist = path_cut_points ( path , distances , closed , direction = true ) ;
2020-05-30 02:04:34 +00:00
planar = len ( path [ 0 ] ) = = 2 ;
if ( true ) for ( i = [ 0 : 1 : len ( cutlist ) - 1 ] ) {
$ pos = cutlist [ i ] [ 0 ] ;
$ idx = i ;
$ dir = rotate_children ? ( planar ? [ 1 , 0 ] : [ 1 , 0 , 0 ] ) : cutlist [ i ] [ 2 ] ;
$ normal = rotate_children ? ( planar ? [ 0 , 1 ] : [ 0 , 0 , 1 ] ) : cutlist [ i ] [ 3 ] ;
translate ( $ pos ) {
if ( rotate_children ) {
if ( planar ) {
rot ( from = [ 0 , 1 ] , to = cutlist [ i ] [ 3 ] ) children ( ) ;
} else {
2021-01-21 00:30:09 +00:00
multmatrix ( affine3d_frame_map ( x = cutlist [ i ] [ 2 ] , z = cutlist [ i ] [ 3 ] ) )
2020-05-30 02:04:34 +00:00
children ( ) ;
}
} else {
children ( ) ;
}
}
}
2019-08-09 20:07:18 +00:00
}
2019-07-01 23:25:00 +00:00
2021-03-06 10:26:39 +00:00
// Function: path_cut_points()
2019-07-01 23:25:00 +00:00
//
2020-07-27 22:15:34 +00:00
// Usage:
2021-03-06 10:54:03 +00:00
// cuts = path_cut_points(path, dists, <closed=>, <direction=>);
2019-07-01 23:25:00 +00:00
//
// Description:
2019-08-09 20:07:18 +00:00
// Cuts a path at a list of distances from the first point in the path. Returns a list of the cut
// points and indices of the next point in the path after that point. So for example, a return
// value entry of [[2,3], 5] means that the cut point was [2,3] and the next point on the path after
2021-03-06 10:26:39 +00:00
// this point is path[5]. If the path is too short then path_cut_points returns undef. If you set
// `direction` to true then `path_cut_points` will also return the tangent vector to the path and a normal
2019-08-09 20:07:18 +00:00
// vector to the path. It tries to find a normal vector that is coplanar to the path near the cut
// point. If this fails it will return a normal vector parallel to the xy plane. The output with
// direction vectors will be `[point, next_index, tangent, normal]`.
2021-03-05 21:35:41 +00:00
// .
// If you give the very last point of the path as a cut point then the returned index will be
// one larger than the last index (so it will not be a valid index). If you use the closed
// option then the returned index will be equal to the path length for cuts along the closing
// path segment, and if you give a point equal to the path length you will get an
// index of len(path)+1 for the index.
2019-07-01 23:25:00 +00:00
//
// Arguments:
// path = path to cut
2019-08-09 20:07:18 +00:00
// dists = distances where the path should be cut (a list) or a scalar single distance
2021-03-06 10:54:03 +00:00
// ---
2019-07-01 23:25:00 +00:00
// closed = set to true if the curve is closed. Default: false
2019-08-09 20:07:18 +00:00
// direction = set to true to return direction vectors. Default: false
2019-07-12 20:21:23 +00:00
//
2019-07-01 23:25:00 +00:00
// Example(NORENDER):
// square=[[0,0],[1,0],[1,1],[0,1]];
2021-03-06 10:26:39 +00:00
// path_cut_points(square, [.5,1.5,2.5]); // Returns [[[0.5, 0], 1], [[1, 0.5], 2], [[0.5, 1], 3]]
// path_cut_points(square, [0,1,2,3]); // Returns [[[0, 0], 1], [[1, 0], 2], [[1, 1], 3], [[0, 1], 4]]
// path_cut_points(square, [0,0.8,1.6,2.4,3.2], closed=true); // Returns [[[0, 0], 1], [[0.8, 0], 1], [[1, 0.6], 2], [[0.6, 1], 3], [[0, 0.8], 4]]
// path_cut_points(square, [0,0.8,1.6,2.4,3.2]); // Returns [[[0, 0], 1], [[0.8, 0], 1], [[1, 0.6], 2], [[0.6, 1], 3], undef]
function path_cut_points ( path , dists , closed = false , direction = false ) =
2020-05-30 02:04:34 +00:00
let ( long_enough = len ( path ) >= ( closed ? 3 : 2 ) )
assert ( long_enough , len ( path ) < 2 ? "Two points needed to define a path" : "Closed path must include three points" )
2021-03-06 10:37:00 +00:00
is_num ( dists ) ? path_cut_points ( path , [ dists ] , closed , direction ) [ 0 ] :
2021-03-05 21:35:41 +00:00
assert ( is_vector ( dists ) )
assert ( list_increasing ( dists ) , "Cut distances must be an increasing list" )
2021-03-06 10:37:00 +00:00
let ( cuts = _path_cut_points ( path , dists , closed ) )
2021-01-24 15:29:34 +00:00
! direction
? cuts
: let (
dir = _path_cuts_dir ( path , cuts , closed ) ,
normals = _path_cuts_normals ( path , cuts , dir , closed )
)
hstack ( cuts , array_group ( dir , 1 ) , array_group ( normals , 1 ) ) ;
2019-07-01 23:25:00 +00:00
// Main recursive path cut function
2021-03-06 10:26:39 +00:00
function _path_cut_points ( path , dists , closed = false , pind = 0 , dtotal = 0 , dind = 0 , result = [ ] ) =
2020-05-30 02:04:34 +00:00
dind = = len ( dists ) ? result :
let (
2021-03-05 21:35:41 +00:00
lastpt = len ( result ) = = 0 ? [ ] : select ( result , - 1 ) [ 0 ] , // location of last cut point
dpartial = len ( result ) = = 0 ? 0 : norm ( lastpt - select ( path , pind ) ) , // remaining length in segment
nextpoint = dists [ dind ] < = dpartial + dtotal // Do we have enough length left on the current segment?
? [ lerp ( lastpt , select ( path , pind ) , ( dists [ dind ] - dtotal ) / dpartial ) , pind ]
: _path_cut_single ( path , dists [ dind ] - dtotal - dpartial , closed , pind )
)
2021-03-06 10:37:00 +00:00
_path_cut_points ( path , dists , closed , nextpoint [ 1 ] , dists [ dind ] , dind + 1 , concat ( result , [ nextpoint ] ) ) ;
2021-03-05 21:35:41 +00:00
2019-07-01 23:25:00 +00:00
// Search for a single cut point in the path
2019-08-09 20:07:18 +00:00
function _path_cut_single ( path , dist , closed = false , ind = 0 , eps = 1e-7 ) =
2021-03-05 21:35:41 +00:00
// If we get to the very end of the path (ind is last point or wraparound for closed case) then
// check if we are within epsilon of the final path point. If not we're out of path, so we fail
ind = = len ( path ) - ( closed ? 0 : 1 ) ?
assert ( dist < eps , "Path is too short for specified cut distance" )
[ select ( path , ind ) , ind + 1 ]
: let ( d = norm ( path [ ind ] - select ( path , ind + 1 ) ) ) d > dist ?
2020-05-30 02:04:34 +00:00
[ lerp ( path [ ind ] , select ( path , ind + 1 ) , dist / d ) , ind + 1 ] :
_path_cut_single ( path , dist - d , closed , ind + 1 , eps ) ;
2019-07-01 23:25:00 +00:00
// Find normal directions to the path, coplanar to local part of the path
// Or return a vector parallel to the x-y plane if the above fails
function _path_cuts_normals ( path , cuts , dirs , closed = false ) =
2020-05-30 02:04:34 +00:00
[ for ( i = [ 0 : len ( cuts ) - 1 ] )
len ( path [ 0 ] ) = = 2 ? [ - dirs [ i ] . y , dirs [ i ] . x ] : (
let (
plane = len ( path ) < 3 ? undef :
let ( start = max ( min ( cuts [ i ] [ 1 ] , len ( path ) - 1 ) , 2 ) ) _path_plane ( path , start , start - 2 )
)
plane = = undef ?
unit ( [ - dirs [ i ] . y , dirs [ i ] . x , 0 ] ) :
unit ( cross ( dirs [ i ] , cross ( plane [ 0 ] , plane [ 1 ] ) ) )
)
] ;
2019-07-01 23:25:00 +00:00
// Scan from the specified point (ind) to find a noncoplanar triple to use
2019-08-09 20:07:18 +00:00
// to define the plane of the path.
2019-07-01 23:25:00 +00:00
function _path_plane ( path , ind , i , closed ) =
2020-05-30 02:04:34 +00:00
i < ( closed ? - 1 : 0 ) ? undef :
! collinear ( path [ ind ] , path [ ind - 1 ] , select ( path , i ) ) ?
[ select ( path , i ) - path [ ind - 1 ] , path [ ind ] - path [ ind - 1 ] ] :
_path_plane ( path , ind , i - 1 ) ;
2019-07-01 23:25:00 +00:00
// Find the direction of the path at the cut points
function _path_cuts_dir ( path , cuts , closed = false , eps = 1e-2 ) =
2020-05-30 02:04:34 +00:00
[ for ( ind = [ 0 : len ( cuts ) - 1 ] )
let (
2020-09-24 02:00:17 +00:00
zeros = path [ 0 ] * 0 ,
2020-05-30 02:04:34 +00:00
nextind = cuts [ ind ] [ 1 ] ,
2020-09-24 02:00:17 +00:00
nextpath = unit ( select ( path , nextind + 1 ) - select ( path , nextind ) , zeros ) ,
2021-03-05 21:35:41 +00:00
thispath = unit ( select ( path , nextind ) - select ( path , nextind - 1 ) , zeros ) ,
lastpath = unit ( select ( path , nextind - 1 ) - select ( path , nextind - 2 ) , zeros ) ,
2020-05-30 02:04:34 +00:00
nextdir =
nextind = = len ( path ) && ! closed ? lastpath :
2021-03-05 21:35:41 +00:00
( nextind < = len ( path ) - 2 || closed ) && approx ( cuts [ ind ] [ 0 ] , path [ nextind ] , eps )
? unit ( nextpath + thispath )
: ( nextind > 1 || closed ) && approx ( cuts [ ind ] [ 0 ] , select ( path , nextind - 1 ) , eps )
? unit ( thispath + lastpath )
: thispath
2020-05-30 02:04:34 +00:00
) nextdir
] ;
2019-03-23 04:13:18 +00:00
2021-03-05 21:35:41 +00:00
2021-03-06 16:10:31 +00:00
// Function: path_cut()
2021-03-06 10:54:03 +00:00
// Topics: Paths
// See Also: path_cut_points()
2021-03-05 21:35:41 +00:00
// Usage:
2021-03-06 16:10:31 +00:00
// path_list = path_cut(path, cutdist, <closed=>);
2021-03-05 21:35:41 +00:00
// Description:
// Given a list of distances in `cutdist`, cut the path into
// subpaths at those lengths, returning a list of paths.
// If the input path is closed then the final path will include the
// original starting point. The list of cut distances must be
// in ascending order. If you repeat a distance you will get an
// empty list in that position in the output.
// Arguments:
2021-03-06 10:54:03 +00:00
// path = The original path to split.
// cutdist = Distance or list of distances where path is cut
// closed = If true, treat the path as a closed polygon.
// Example(2D):
// path = circle(d=100);
2021-03-06 16:10:31 +00:00
// segs = path_cut(path, [50, 200], closed=true);
2021-03-06 10:54:03 +00:00
// rainbow(segs) stroke($item);
2021-03-06 16:10:31 +00:00
function path_cut ( path , cutdist , closed ) =
is_num ( cutdist ) ? path_cut ( path , [ cutdist ] , closed ) :
2021-03-05 21:35:41 +00:00
assert ( is_vector ( cutdist ) )
assert ( select ( cutdist , - 1 ) < path_length ( path , closed = closed ) , "Cut distances must be smaller than the path length" )
assert ( cutdist [ 0 ] > 0 , "Cut distances must be strictly positive" )
let (
2021-03-06 16:10:31 +00:00
cutlist = path_cut_points ( path , cutdist , closed = closed ) ,
2021-03-05 21:35:41 +00:00
cuts = len ( cutlist )
)
[
[ each slice ( path , 0 , cutlist [ 0 ] [ 1 ] ) ,
if ( ! approx ( cutlist [ 0 ] [ 0 ] , path [ cutlist [ 0 ] [ 1 ] - 1 ] ) ) cutlist [ 0 ] [ 0 ]
] ,
for ( i = [ 0 : 1 : cuts - 2 ] )
cutlist [ i ] [ 0 ] = = cutlist [ i + 1 ] [ 0 ] ? [ ]
:
[ if ( ! approx ( cutlist [ i ] [ 0 ] , select ( path , cutlist [ i ] [ 1 ] ) ) ) cutlist [ i ] [ 0 ] ,
each slice ( path , cutlist [ i ] [ 1 ] , cutlist [ i + 1 ] [ 1 ] ) ,
if ( ! approx ( cutlist [ i + 1 ] [ 0 ] , select ( path , cutlist [ i + 1 ] [ 1 ] - 1 ) ) ) cutlist [ i + 1 ] [ 0 ] ,
] ,
[
if ( ! approx ( cutlist [ cuts - 1 ] [ 0 ] , select ( path , cutlist [ cuts - 1 ] [ 1 ] ) ) ) cutlist [ cuts - 1 ] [ 0 ] ,
each select ( path , cutlist [ cuts - 1 ] [ 1 ] , closed ? 0 : - 1 )
]
] ;
2019-11-19 22:42:11 +00:00
// Input `data` is a list that sums to an integer.
// Returns rounded version of input data so that every
// entry is rounded to an integer and the sum is the same as
// that of the input. Works by rounding an entry in the list
// and passing the rounding error forward to the next entry.
// This will generally distribute the error in a uniform manner.
function _sum_preserving_round ( data , index = 0 ) =
2020-05-30 02:04:34 +00:00
index = = len ( data ) - 1 ? list_set ( data , len ( data ) - 1 , round ( data [ len ( data ) - 1 ] ) ) :
let (
newval = round ( data [ index ] ) ,
error = newval - data [ index ]
) _sum_preserving_round (
list_set ( data , [ index , index + 1 ] , [ newval , data [ index + 1 ] - error ] ) ,
index + 1
) ;
2019-11-19 22:42:11 +00:00
2019-11-20 02:03:47 +00:00
// Function: subdivide_path()
// Usage:
2020-06-25 06:32:59 +00:00
// newpath = subdivide_path(path, [N|refine], method);
2019-11-19 22:42:11 +00:00
// Description:
// Takes a path as input (closed or open) and subdivides the path to produce a more
// finely sampled path. The new points can be distributed proportional to length
// (`method="length"`) or they can be divided up evenly among all the path segments
// (`method="segment"`). If the extra points don't fit evenly on the path then the
// algorithm attempts to distribute them uniformly. The `exact` option requires that
// the final length is exactly as requested. If you set it to `false` then the
// algorithm will favor uniformity and the output path may have a different number of
// points due to rounding error.
2020-07-27 22:15:34 +00:00
// .
2019-11-19 22:42:11 +00:00
// With the `"segment"` method you can also specify a vector of lengths. This vector,
// `N` specfies the desired point count on each segment: with vector input, `subdivide_path`
// attempts to place `N[i]-1` points on segment `i`. The reason for the -1 is to avoid
// double counting the endpoints, which are shared by pairs of segments, so that for
// a closed polygon the total number of points will be sum(N). Note that with an open
// path there is an extra point at the end, so the number of points will be sum(N)+1.
// Arguments:
2019-11-20 02:03:47 +00:00
// path = path to subdivide
// N = scalar total number of points desired or with `method="segment"` can be a vector requesting `N[i]-1` points on segment i.
2020-10-04 02:50:29 +00:00
// refine = number of points to add each segment.
2019-11-20 02:03:47 +00:00
// closed = set to false if the path is open. Default: True
// exact = if true return exactly the requested number of points, possibly sacrificing uniformity. If false, return uniform point sample that may not match the number of points requested. Default: True
2019-12-07 03:16:31 +00:00
// method = One of `"length"` or `"segment"`. If `"length"`, adds vertices evenly along the total path length. If `"segment"`, adds points evenly among the segments. Default: `"length"`
2019-11-19 22:42:11 +00:00
// Example(2D):
// mypath = subdivide_path(square([2,2],center=true), 12);
2020-03-25 01:31:22 +00:00
// move_copies(mypath)circle(r=.1,$fn=32);
2019-11-19 22:42:11 +00:00
// Example(2D):
// mypath = subdivide_path(square([8,2],center=true), 12);
2020-03-25 01:31:22 +00:00
// move_copies(mypath)circle(r=.2,$fn=32);
2019-11-19 22:42:11 +00:00
// Example(2D):
// mypath = subdivide_path(square([8,2],center=true), 12, method="segment");
2020-03-25 01:31:22 +00:00
// move_copies(mypath)circle(r=.2,$fn=32);
2019-11-19 22:42:11 +00:00
// Example(2D):
2019-11-20 02:03:47 +00:00
// mypath = subdivide_path(square([2,2],center=true), 17, closed=false);
2020-03-25 01:31:22 +00:00
// move_copies(mypath)circle(r=.1,$fn=32);
2019-11-19 22:42:11 +00:00
// Example(2D): Specifying different numbers of points on each segment
// mypath = subdivide_path(hexagon(side=2), [2,3,4,5,6,7], method="segment");
2020-03-25 01:31:22 +00:00
// move_copies(mypath)circle(r=.1,$fn=32);
2019-11-19 22:42:11 +00:00
// Example(2D): Requested point total is 14 but 15 points output due to extra end point
// mypath = subdivide_path(pentagon(side=2), [3,4,3,4], method="segment", closed=false);
2020-03-25 01:31:22 +00:00
// move_copies(mypath)circle(r=.1,$fn=32);
2019-11-19 22:42:11 +00:00
// Example(2D): Since 17 is not divisible by 5, a completely uniform distribution is not possible.
// mypath = subdivide_path(pentagon(side=2), 17);
2020-03-25 01:31:22 +00:00
// move_copies(mypath)circle(r=.1,$fn=32);
2019-11-19 22:42:11 +00:00
// Example(2D): With `exact=false` a uniform distribution, but only 15 points
// mypath = subdivide_path(pentagon(side=2), 17, exact=false);
2020-03-25 01:31:22 +00:00
// move_copies(mypath)circle(r=.1,$fn=32);
2019-11-19 22:42:11 +00:00
// Example(2D): With `exact=false` you can also get extra points, here 20 instead of requested 18
// mypath = subdivide_path(pentagon(side=2), 18, exact=false);
2020-03-25 01:31:22 +00:00
// move_copies(mypath)circle(r=.1,$fn=32);
2021-02-20 03:56:43 +00:00
// Example(FlatSpin,VPD=15,VPT=[0,0,1.5]): Three-dimensional paths also work
2019-11-19 22:42:11 +00:00
// mypath = subdivide_path([[0,0,0],[2,0,1],[2,3,2]], 12);
2020-03-25 01:31:22 +00:00
// move_copies(mypath)sphere(r=.1,$fn=32);
2020-06-20 16:49:13 +00:00
function subdivide_path ( path , N , refine , closed = true , exact = true , method = "length" ) =
2020-05-30 02:04:34 +00:00
assert ( is_path ( path ) )
assert ( method = = "length" || method = = "segment" )
2020-06-20 16:49:13 +00:00
assert ( num_defined ( [ N , refine ] ) , "Must give exactly one of N and refine" )
2020-10-04 02:50:29 +00:00
let (
N = ! is_undef ( N ) ? N :
! is_undef ( refine ) ? len ( path ) * refine :
undef
)
2020-05-30 02:04:34 +00:00
assert ( ( is_num ( N ) && N > 0 ) || is_vector ( N ) , "Parameter N to subdivide_path must be postive number or vector" )
let (
count = len ( path ) - ( closed ? 0 : 1 ) ,
add_guess = method = = "segment" ? (
is_list ( N ) ? (
assert ( len ( N ) = = count , "Vector parameter N to subdivide_path has the wrong length" )
add_scalar ( N , - 1 )
) : repeat ( ( N - len ( path ) ) / count , count )
) : // method=="length"
assert ( is_num ( N ) , "Parameter N to subdivide path must be a number when method=\"length\"" )
let (
path_lens = concat (
[ for ( i = [ 0 : 1 : len ( path ) - 2 ] ) norm ( path [ i + 1 ] - path [ i ] ) ] ,
closed ? [ norm ( path [ len ( path ) - 1 ] - path [ 0 ] ) ] : [ ]
) ,
add_density = ( N - len ( path ) ) / sum ( path_lens )
)
path_lens * add_density ,
add = exact ? _sum_preserving_round ( add_guess ) :
[ for ( val = add_guess ) round ( val ) ]
) concat (
[
for ( i = [ 0 : 1 : count ] ) each [
for ( j = [ 0 : 1 : add [ i ] ] )
lerp ( path [ i ] , select ( path , i + 1 ) , j / ( add [ i ] + 1 ) )
]
] ,
closed ? [ ] : [ select ( path , - 1 ) ]
) ;
2019-11-19 22:42:11 +00:00
2020-02-27 22:32:03 +00:00
// Function: path_length_fractions()
2020-10-04 03:29:35 +00:00
// Usage:
// fracs = path_length_fractions(path, <closed>);
2020-02-27 22:32:03 +00:00
// Description:
// Returns the distance fraction of each point in the path along the path, so the first
// point is zero and the final point is 1. If the path is closed the length of the output
// will have one extra point because of the final connecting segment that connects the last
// point of the path to the first point.
function path_length_fractions ( path , closed = false ) =
2020-05-30 02:04:34 +00:00
assert ( is_path ( path ) )
assert ( is_bool ( closed ) )
let (
lengths = [
0 ,
for ( i = [ 0 : 1 : len ( path ) - ( closed ? 1 : 2 ) ] )
norm ( select ( path , i + 1 ) - path [ i ] )
] ,
partial_len = cumsum ( lengths ) ,
total_len = select ( partial_len , - 1 )
) partial_len / total_len ;
2020-02-27 22:32:03 +00:00
2020-05-15 21:28:54 +00:00
// Function: resample_path()
2020-10-04 03:29:35 +00:00
// Usage:
// newpath = resample_path(path, N|spacing, <closed>);
2020-05-15 21:28:54 +00:00
// Description:
// Compute a uniform resampling of the input path. If you specify `N` then the output path will have N
// points spaced uniformly (by linear interpolation along the input path segments). The only points of the
// input path that are guaranteed to appear in the output path are the starting and ending points.
// If you specify `spacing` then the length you give will be rounded to the nearest spacing that gives
// a uniform sampling of the path and the resulting uniformly sampled path is returned.
// Note that because this function operates on a discrete input path the quality of the output depends on
// the sampling of the input. If you want very accurate output, use a lot of points for the input.
// Arguments:
// path = path to resample
// N = Number of points in output
// spacing = Approximate spacing desired
// closed = set to true if path is closed. Default: false
function resample_path ( path , N , spacing , closed = false ) =
assert ( is_path ( path ) )
assert ( num_defined ( [ N , spacing ] ) = = 1 , "Must define exactly one of N and spacing" )
assert ( is_bool ( closed ) )
let (
2020-05-30 02:04:34 +00:00
length = path_length ( path , closed ) ,
N = is_def ( N ) ? N : round ( length / spacing ) + ( closed ? 0 : 1 ) ,
2020-05-15 21:28:54 +00:00
spacing = length / ( closed ? N : N - 1 ) , // Note: worried about round-off error, so don't include
distlist = list_range ( closed ? N : N - 1 , step = spacing ) , // last point when closed=false
2021-03-06 10:26:39 +00:00
cuts = path_cut_points ( path , distlist , closed = closed )
2020-05-15 21:28:54 +00:00
)
concat ( subindex ( cuts , 0 ) , closed ? [ ] : [ select ( path , - 1 ) ] ) ; // Then add last point here
2017-08-30 00:00:16 +00:00
2020-05-30 02:04:34 +00:00
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap