mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2025-01-28 23:39:36 +00:00
Added is_list_of(). Improved is_path() and is_vector() tests.
This commit is contained in:
parent
7ea33cc724
commit
af0e285781
4 changed files with 66 additions and 14 deletions
19
common.scad
19
common.scad
|
@ -81,6 +81,25 @@ function is_integer(n) = is_num(n) && n == round(n);
|
|||
function is_nan(x) = (x!=x);
|
||||
|
||||
|
||||
// Function: is_list_of()
|
||||
// Usage:
|
||||
// is_list_of(list, pattern)
|
||||
// Description:
|
||||
// Tests whether the input is a list whose entries are all numeric lists that have the same
|
||||
// list shape as the pattern.
|
||||
// Example:
|
||||
// is_list_of([3,4,5], 0); // Returns true
|
||||
// is_list_of([3,4,undef], 0); // Returns false
|
||||
// is_list_of([[3,4],[4,5]], [1,1]); // Returns true
|
||||
// is_list_of([[3,4], 6, [4,5]], [1,1]); // Returns false
|
||||
// is_list_of([[1,[3,4]], [4,[5,6]]], [1,[2,3]]); // Returne true
|
||||
// is_list_of([[1,[3,INF]], [4,[5,6]]], [1,[2,3]]); // Returne false
|
||||
function is_list_of(list,pattern) =
|
||||
is_list(list) &&
|
||||
let(pattern = 0*pattern)
|
||||
[]==[for(entry=list) if (entry*0 != pattern) entry];
|
||||
|
||||
|
||||
|
||||
// Section: Handling `undef`s.
|
||||
|
||||
|
|
31
paths.scad
31
paths.scad
|
@ -16,10 +16,35 @@ include <BOSL2/triangulation.scad>
|
|||
|
||||
// Function: is_path()
|
||||
// Usage:
|
||||
// is_path(x);
|
||||
// is_path(list, [dim], [fast])
|
||||
// Description:
|
||||
// Returns true if the given item looks like a path. A path is defined as a list of two or more points.
|
||||
function is_path(x) = is_list(x) && is_vector(x.x) && len(x)>1;
|
||||
// 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.
|
||||
// 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
|
||||
// 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) =
|
||||
fast? is_list(list) && is_vector(list[0],fast=true) :
|
||||
is_list(list) && is_list(list[0]) && len(list)>1 &&
|
||||
let( d = len(list[0]) )
|
||||
(is_undef(dim) || in_list(d, is_list(dim)?dim:[dim]) ) &&
|
||||
is_list_of(list, replist(0,d));
|
||||
|
||||
|
||||
// Function: is_closed_path()
|
||||
|
|
28
vectors.scad
28
vectors.scad
|
@ -13,18 +13,26 @@
|
|||
|
||||
// Function: is_vector()
|
||||
// Usage:
|
||||
// is_vector(v)
|
||||
// is_vector(v, [length], [fast]);
|
||||
// Description:
|
||||
// Returns true if the given value is a list, and at least the first item is a number.
|
||||
// Returns true if v is a list of finite numbers.
|
||||
// Arguments:
|
||||
// v = The value to test to see if it is a vector.
|
||||
// length = If given, make sure the vector is `length` items long.
|
||||
// fast = If true, do a shallow test that is faster.
|
||||
// Example:
|
||||
// is_vector([1,2,3]); // Returns: true
|
||||
// is_vector([[1,2,3]]); // Returns: false
|
||||
// is_vector(["foo"]); // Returns: false
|
||||
// is_vector([]); // Returns: false
|
||||
// is_vector(1); // Returns: false
|
||||
// is_vector("foo"); // Returns: false
|
||||
// is_vector(true); // Returns: false
|
||||
function is_vector(v) = is_list(v) && is_num(v[0]);
|
||||
// is_vector(4); // Returns false
|
||||
// is_vector([4,true,false]); // Returns false
|
||||
// is_vector([3,4,INF,5]); // Returns false
|
||||
// is_vector([3,4,5,6]); // Returns true
|
||||
// is_vector([3,4,undef,5]); // Returns false
|
||||
// is_vector([3,4,5],3); // Returns true
|
||||
// is_vector([3,4,5],4); // Returns true
|
||||
// is_vector([]); // Returns false
|
||||
// is_vector([3,undef,undef,true], fast=true); // Returns true
|
||||
function is_vector(v,length,fast=false) =
|
||||
(fast? (is_list(v) && is_num(v[0])) : is_list_of(v,0)) &&
|
||||
len(v) && (is_undef(length) || length==len(v));
|
||||
|
||||
|
||||
// Function: add_scalar()
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
BOSL_VERSION = [2,0,144];
|
||||
BOSL_VERSION = [2,0,145];
|
||||
|
||||
|
||||
// Section: BOSL Library Version Functions
|
||||
|
|
Loading…
Reference in a new issue