mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2025-01-01 09:49:45 +00:00
further type checking fixes
This commit is contained in:
parent
35c2d7df42
commit
737baed34c
5 changed files with 32 additions and 45 deletions
49
common.scad
49
common.scad
|
@ -185,49 +185,40 @@ function valid_range(x) =
|
||||||
: ( x[1]<0 && x[0]>=x[2] ) );
|
: ( x[1]<0 && x[0]>=x[2] ) );
|
||||||
|
|
||||||
|
|
||||||
// Function: is_list_of()
|
|
||||||
// Usage:
|
|
||||||
// bool = is_list_of(list, pattern);
|
|
||||||
// Topics: Type Checking
|
|
||||||
// See Also: typeof(), is_type(), is_str(), is_def(), is_int(), is_range()
|
|
||||||
// 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,"a"],[4,true]], [1,undef]); // 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]]); // Returns true
|
|
||||||
// is_list_of([[1,[3,INF]], [4,[5,6]]], [1,[2,3]]); // Returns false
|
|
||||||
// is_list_of([], [1,[2,3]]); // Returns true
|
|
||||||
function is_list_of(list,pattern) =
|
|
||||||
let(pattern = 0*pattern)
|
|
||||||
is_list(list) &&
|
|
||||||
[]==[for(entry=0*list) if (entry != pattern) entry];
|
|
||||||
|
|
||||||
|
|
||||||
// Function: is_consistent()
|
// Function: is_consistent()
|
||||||
// Usage:
|
// Usage:
|
||||||
// bool = is_consistent(list);
|
// bool = is_consistent(list, <pattern>);
|
||||||
// Topics: Type Checking
|
// Topics: Type Checking
|
||||||
// See Also: typeof(), is_type(), is_str(), is_def(), is_int(), is_range(), is_homogeneous()
|
// See Also: typeof(), is_type(), is_str(), is_def(), is_int(), is_range(), is_homogeneous()
|
||||||
// Description:
|
// Description:
|
||||||
// Tests whether input is a list of entries which all have the same list structure
|
// Tests whether input is a list of entries which all have the same list structure
|
||||||
// and are filled with finite numerical data. It returns `true`for the empty list.
|
// and are filled with finite numerical data. You can optionally specify a required
|
||||||
|
// list structure with the pattern argument. It returns `true` for the empty list.
|
||||||
|
// Arguments:
|
||||||
|
// list = list to check
|
||||||
|
// pattern = optional pattern required to match
|
||||||
// Example:
|
// Example:
|
||||||
// is_consistent([3,4,5]); // Returns true
|
// is_consistent([3,4,5]); // Returns true
|
||||||
// is_consistent([[3,4],[4,5],[6,7]]); // Returns true
|
// is_consistent([[3,4],[4,5],[6,7]]); // Returns true
|
||||||
// is_consistent([[3,4,5],[3,4]]); // Returns false
|
// is_consistent([[3,4,5],[3,4]]); // Returns false
|
||||||
// is_consistent([[3,[3,4,[5]]], [5,[2,9,[9]]]]); // Returns true
|
// is_consistent([[3,[3,4,[5]]], [5,[2,9,[9]]]]); // Returns true
|
||||||
// is_consistent([[3,[3,4,[5]]], [5,[2,9,9]]]); // Returns false
|
// is_consistent([[3,[3,4,[5]]], [5,[2,9,9]]]); // Returns false
|
||||||
function is_consistent(list) =
|
// is_consistent([3,4,5], 0); // Returns true
|
||||||
/*is_list(list) &&*/ is_list_of(list, _list_pattern(list[0]));
|
// is_consistent([3,4,undef], 0); // Returns false
|
||||||
|
// is_consistent([[3,4],[4,5]], [1,1]); // Returns true
|
||||||
|
// is_consistent([[3,"a"],[4,true]], [1,undef]); // Returns true
|
||||||
|
// is_consistent([[3,4], 6, [4,5]], [1,1]); // Returns false
|
||||||
|
// is_consistent([[1,[3,4]], [4,[5,6]]], [1,[2,3]]); // Returns true
|
||||||
|
// is_consistent([[1,[3,INF]], [4,[5,6]]], [1,[2,3]]); // Returns false
|
||||||
|
// is_consistent([], [1,[2,3]]); // Returns true
|
||||||
|
function is_consistent(list, pattern) =
|
||||||
|
is_list(list)
|
||||||
|
&& (len(list)==0
|
||||||
|
|| (let(pattern = is_undef(pattern) ? _list_pattern(list[0]): _list_pattern(pattern) )
|
||||||
|
[]==[for(entry=0*list) if (entry != pattern) entry]));
|
||||||
|
|
||||||
//Internal function
|
//Internal function
|
||||||
//Creates a list with the same structure of `list` with each of its elements substituted by 0.
|
//Creates a list with the same structure of `list` with each of its elements replaced by 0.
|
||||||
function _list_pattern(list) =
|
function _list_pattern(list) =
|
||||||
is_list(list)
|
is_list(list)
|
||||||
? [for(entry=list) is_list(entry) ? _list_pattern(entry) : 0]
|
? [for(entry=list) is_list(entry) ? _list_pattern(entry) : 0]
|
||||||
|
|
|
@ -236,7 +236,7 @@
|
||||||
// interior = regular_ngon(n=len(base), d=60);
|
// interior = regular_ngon(n=len(base), d=60);
|
||||||
// right_half()
|
// right_half()
|
||||||
// skin([ sub_base, base, base, sub_base, interior], z=[0,2,height, height, 2], slices=0, refine=1, method="reindex");
|
// skin([ sub_base, base, base, sub_base, interior], z=[0,2,height, height, 2], slices=0, refine=1, method="reindex");
|
||||||
// Example: Connecting a pentagon and circle with the "tangent" method produces triangular faces.
|
// Example: Connecting a pentagon and circle with the "tangent" method produces large triangular faces and cone shaped corners.
|
||||||
// skin([pentagon(4), circle($fn=80,r=2)], z=[0,3], slices=10, method="tangent");
|
// skin([pentagon(4), circle($fn=80,r=2)], z=[0,3], slices=10, method="tangent");
|
||||||
// Example: rounding corners of a square. Note that `$fn` makes the number of points constant, and avoiding the `rounding=0` case keeps everything simple. In this case, the connections between profiles are linear, so there is no benefit to setting `slices` bigger than zero.
|
// Example: rounding corners of a square. Note that `$fn` makes the number of points constant, and avoiding the `rounding=0` case keeps everything simple. In this case, the connections between profiles are linear, so there is no benefit to setting `slices` bigger than zero.
|
||||||
// shapes = [for(i=[.01:.045:2])zrot(-i*180/2,cp=[-8,0,0],p=xrot(90,p=path3d(regular_ngon(n=4, side=4, rounding=i, $fn=64))))];
|
// shapes = [for(i=[.01:.045:2])zrot(-i*180/2,cp=[-8,0,0],p=xrot(90,p=path3d(regular_ngon(n=4, side=4, rounding=i, $fn=64))))];
|
||||||
|
@ -921,7 +921,7 @@ function associate_vertices(polygons, split, curpoly=0) =
|
||||||
// sweep(shape, concat(outside,inside));
|
// sweep(shape, concat(outside,inside));
|
||||||
|
|
||||||
function sweep(shape, transforms, closed=false, caps) =
|
function sweep(shape, transforms, closed=false, caps) =
|
||||||
assert(is_list_of(transforms, ident(4)), "Input transforms must be a list of numeric 4x4 matrices in sweep")
|
assert(is_consistent(transforms, ident(4)), "Input transforms must be a list of numeric 4x4 matrices in sweep")
|
||||||
assert(is_path(shape,2) || is_region(shape), "Input shape must be a 2d path or a region.")
|
assert(is_path(shape,2) || is_region(shape), "Input shape must be a 2d path or a region.")
|
||||||
let(
|
let(
|
||||||
caps = is_def(caps) ? caps :
|
caps = is_def(caps) ? caps :
|
||||||
|
|
|
@ -217,16 +217,6 @@ module test_valid_range() {
|
||||||
}
|
}
|
||||||
test_valid_range();
|
test_valid_range();
|
||||||
|
|
||||||
module test_is_list_of() {
|
|
||||||
assert(is_list_of([3,4,5], 0));
|
|
||||||
assert(!is_list_of([3,4,undef], 0));
|
|
||||||
assert(is_list_of([[3,4],[4,5]], [1,1]));
|
|
||||||
assert(!is_list_of([[3,4], 6, [4,5]], [1,1]));
|
|
||||||
assert(is_list_of([[1,[3,4]], [4,[5,6]]], [1,[2,3]]));
|
|
||||||
assert(!is_list_of([[1,[3,INF]], [4,[5,6]]], [1,[2,3]]));
|
|
||||||
}
|
|
||||||
test_is_list_of();
|
|
||||||
|
|
||||||
module test_is_consistent() {
|
module test_is_consistent() {
|
||||||
assert(is_consistent([]));
|
assert(is_consistent([]));
|
||||||
assert(is_consistent([[],[]]));
|
assert(is_consistent([[],[]]));
|
||||||
|
@ -238,6 +228,13 @@ module test_is_consistent() {
|
||||||
assert(!is_consistent([[3,4,5],[3,4]]));
|
assert(!is_consistent([[3,4,5],[3,4]]));
|
||||||
assert(is_consistent([[3,[3,4,[5]]], [5,[2,9,[9]]]]));
|
assert(is_consistent([[3,[3,4,[5]]], [5,[2,9,[9]]]]));
|
||||||
assert(!is_consistent([[3,[3,4,[5]]], [5,[2,9,9]]]));
|
assert(!is_consistent([[3,[3,4,[5]]], [5,[2,9,9]]]));
|
||||||
|
|
||||||
|
assert(is_consistent([3,4,5], 0));
|
||||||
|
assert(!is_consistent([3,4,undef], 0));
|
||||||
|
assert(is_consistent([[3,4],[4,5]], [1,1]));
|
||||||
|
assert(!is_consistent([[3,4], 6, [4,5]], [1,1]));
|
||||||
|
assert(is_consistent([[1,[3,4]], [4,[5,6]]], [1,[2,3]]));
|
||||||
|
assert(!is_consistent([[1,[3,INF]], [4,[5,6]]], [1,[2,3]]));
|
||||||
}
|
}
|
||||||
test_is_consistent();
|
test_is_consistent();
|
||||||
|
|
||||||
|
|
|
@ -421,8 +421,8 @@ turtle state: sequence of transformations ("path") so far
|
||||||
|
|
||||||
function _turtle3d_state_valid(state) =
|
function _turtle3d_state_valid(state) =
|
||||||
is_list(state)
|
is_list(state)
|
||||||
&& is_list_of(state[0],ident(4))
|
&& is_consistent(state[0],ident(4))
|
||||||
&& is_list_of(state[1],ident(4))
|
&& is_consistent(state[1],ident(4))
|
||||||
&& is_num(state[2])
|
&& is_num(state[2])
|
||||||
&& is_num(state[3])
|
&& is_num(state[3])
|
||||||
&& is_num(state[4]);
|
&& is_num(state[4]);
|
||||||
|
|
|
@ -36,8 +36,7 @@
|
||||||
// is_vector([1,1,1],all_nonzero=false); // Returns true
|
// is_vector([1,1,1],all_nonzero=false); // Returns true
|
||||||
// is_vector([],zero=false); // Returns false
|
// is_vector([],zero=false); // Returns false
|
||||||
function is_vector(v, length, zero, all_nonzero=false, eps=EPSILON) =
|
function is_vector(v, length, zero, all_nonzero=false, eps=EPSILON) =
|
||||||
is_list_of(v,1) // is_list(v) && is_num(v[0]) && is_num(0*(v*v))
|
is_list(v) && len(v)>0 && []==[for(vi=v) if(!is_num(vi)) 0]
|
||||||
&& len(v)>0
|
|
||||||
&& (is_undef(length) || len(v)==length)
|
&& (is_undef(length) || len(v)==length)
|
||||||
&& (is_undef(zero) || ((norm(v) >= eps) == !zero))
|
&& (is_undef(zero) || ((norm(v) >= eps) == !zero))
|
||||||
&& (!all_nonzero || all_nonzero(v)) ;
|
&& (!all_nonzero || all_nonzero(v)) ;
|
||||||
|
|
Loading…
Reference in a new issue