diff --git a/arrays.scad b/arrays.scad index a05d330..e29edef 100644 --- a/arrays.scad +++ b/arrays.scad @@ -396,16 +396,16 @@ function deduplicate_indexed(list, indices, closed=false, eps=EPSILON) = function repeat_entries(list, N, exact = true) = assert(is_list(list) && len(list)>0, "The list cannot be void.") assert((is_finite(N) && N>0) || is_vector(N,len(list)), - "Parameter N must be a number greater than zero or vector with the same length of `list`") + "Parameter N must be a number greater than zero or vector with the same length of `list`") let( length = len(list), reps_guess = is_list(N)? N : repeat(N/length,length), reps = exact ? - _sum_preserving_round(reps_guess) + _sum_preserving_round(reps_guess) : [for (val=reps_guess) round(val)] ) [for(i=[0:length-1]) each repeat(list[i],reps[i])]; - + //*** // 1. Complete asserts // 2. Format @@ -434,21 +434,21 @@ function list_set(list=[],indices,values,dflt=0,minlen=0) = assert(is_list(list)||is_string(list)) !is_list(indices)? ( (is_finite(indices) && indices length ? list_trim(array,length) - : list_pad(array,length,fill); + l==length ? array : + l> length ? list_trim(array,length) + : list_pad(array,length,fill); //*** // formating @@ -665,32 +665,32 @@ function shuffle(list) = left = [for (i=[0:len(list)-1]) if (rval[i]< 0.5) list[i]], right = [for (i=[0:len(list)-1]) if (rval[i]>=0.5) list[i]] ) - concat(shuffle(left), shuffle(right)); + concat(shuffle(left), shuffle(right)); // Sort a vector of scalar values function _sort_scalars(arr) = len(arr)<=1 ? arr : - let( + let( pivot = arr[floor(len(arr)/2)], lesser = [ for (y = arr) if (y < pivot) y ], equal = [ for (y = arr) if (y == pivot) y ], greater = [ for (y = arr) if (y > pivot) y ] ) - concat( _sort_scalars(lesser), equal, _sort_scalars(greater) ); + concat( _sort_scalars(lesser), equal, _sort_scalars(greater) ); // Sort a vector of vectors based on the first entry only of each vector function _sort_vectors1(arr) = len(arr)<=1 ? arr : !(len(arr)>0) ? [] : - let( + let( pivot = arr[floor(len(arr)/2)], lesser = [ for (y = arr) if (y[0] < pivot[0]) y ], equal = [ for (y = arr) if (y[0] == pivot[0]) y ], greater = [ for (y = arr) if (y[0] > pivot[0]) y ] ) - concat( _sort_vectors1(lesser), equal, _sort_vectors1(greater) ); + concat( _sort_vectors1(lesser), equal, _sort_vectors1(greater) ); // Sort a vector of vectors based on the first two entries of each vector @@ -698,19 +698,19 @@ function _sort_vectors1(arr) = function _sort_vectors2(arr) = len(arr)<=1 ? arr : !(len(arr)>0) ? [] : - let( + let( pivot = arr[floor(len(arr)/2)], lesser = [ for (y = arr) if (y[0] < pivot[0] || (y[0]==pivot[0] && y[1] pivot[0] || (y[0]==pivot[0] && y[1]>pivot[1])) y ] ) - concat( _sort_vectors2(lesser), equal, _sort_vectors2(greater) ); + concat( _sort_vectors2(lesser), equal, _sort_vectors2(greater) ); // Sort a vector of vectors based on the first three entries of each vector // Lexicographic order, remaining entries of vector ignored function _sort_vectors3(arr) = len(arr)<=1 ? arr : - let( + let( pivot = arr[floor(len(arr)/2)], lesser = [ for (y = arr) if ( @@ -742,14 +742,14 @@ function _sort_vectors3(arr) = ) y ] ) - concat( _sort_vectors3(lesser), equal, _sort_vectors3(greater) ); + concat( _sort_vectors3(lesser), equal, _sort_vectors3(greater) ); // Sort a vector of vectors based on the first four entries of each vector // Lexicographic order, remaining entries of vector ignored function _sort_vectors4(arr) = len(arr)<=1 ? arr : - let( + let( pivot = arr[floor(len(arr)/2)], lesser = [ for (y = arr) if ( @@ -792,18 +792,18 @@ function _sort_vectors4(arr) = ) y ] ) - concat( _sort_vectors4(lesser), equal, _sort_vectors4(greater) ); - + concat( _sort_vectors4(lesser), equal, _sort_vectors4(greater) ); + function _sort_general(arr, idx=undef) = (len(arr)<=1) ? arr : let( pivot = arr[floor(len(arr)/2)], pivotval = idx==undef? pivot : [for (i=idx) pivot[i]], - compare = - is_undef(idx) ? [for(entry=arr) compare_vals(entry, pivotval) ] : + compare = + is_undef(idx) ? [for(entry=arr) compare_vals(entry, pivotval) ] : [ for (entry = arr) - let( val = [for (i=idx) entry[i] ] ) + let( val = [for (i=idx) entry[i] ] ) compare_vals(val, pivotval) ] , lesser = [ for (i = [0:1:len(arr)-1]) if (compare[i] < 0) arr[i] ], equal = [ for (i = [0:1:len(arr)-1]) if (compare[i] ==0) arr[i] ], @@ -832,19 +832,19 @@ function _sort_general(arr, idx=undef) = // sorted = sort(l); // Returns [2,3,8,9,12,16,23,34,37,45,89] function sort(list, idx=undef) = !is_list(list) || len(list)<=1 ? list : - assert( is_undef(idx) || is_finite(idx) || is_vector(idx), "Invalid indices.") + assert( is_undef(idx) || is_finite(idx) || is_vector(idx), "Invalid indices.") is_def(idx) ? _sort_general(list,idx) : let(size = array_dim(list)) len(size)==1 ? _sort_scalars(list) : len(size)==2 && size[1] <=4 - ? ( + ? ( size[1]==0 ? list : size[1]==1 ? _sort_vectors1(list) : size[1]==2 ? _sort_vectors2(list) : size[1]==3 ? _sort_vectors3(list) /*size[1]==4*/ : _sort_vectors4(list) ) - : _sort_general(list); + : _sort_general(list); //*** // Formating and input check @@ -872,9 +872,9 @@ function sort(list, idx=undef) = // idxs3 = sortidx(lst, idx=[1,3]); // Returns: [3,0,2,1] function sortidx(list, idx=undef) = assert( is_list(list) || is_string(list) , "Invalid input." ) - assert( is_undef(idx) || is_finite(idx) || is_vector(idx), "Invalid indices.") + assert( is_undef(idx) || is_finite(idx) || is_vector(idx), "Invalid indices.") list==[] ? [] : - let( + let( size = array_dim(list), aug = is_undef(idx) && (len(size) == 1 || (len(size) == 2 && size[1]<=4)) ? zip(list, list_range(len(list))) @@ -882,14 +882,14 @@ function sortidx(list, idx=undef) = ) is_undef(idx) && len(size) == 1? subindex(_sort_vectors1(aug),1) : is_undef(idx) && len(size) == 2 && size[1] <=4 - ? ( + ? ( size[1]==0 ? list_range(len(arr)) : size[1]==1 ? subindex(_sort_vectors1(aug),1) : size[1]==2 ? subindex(_sort_vectors2(aug),2) : size[1]==3 ? subindex(_sort_vectors3(aug),3) /*size[1]==4*/ : subindex(_sort_vectors4(aug),4) ) - : // general case + : // general case subindex(_sort_general(aug, idx=list_range(s=1,n=len(aug)-1)), 0); //*** @@ -906,8 +906,8 @@ function sortidx(list, idx=undef) = function unique(arr) = assert(is_list(arr)||is_string(arr), "Invalid input." ) len(arr)<=1? arr : - let( sorted = sort(arr)) - [ for (i=[0:1:len(sorted)-1]) + let( sorted = sort(arr)) + [ for (i=[0:1:len(sorted)-1]) if (i==0 || (sorted[i] != sorted[i-1])) sorted[i] ]; @@ -1079,9 +1079,9 @@ function triplet_wrap(v) = // for (p=permute(regular_ngon(n=7,d=100))) stroke(p); function permute(l,n=2,_s=0) = assert(is_list(l), "Invalid list." ) - assert( is_finite(n) && n>=1 && n<=len(l), "Invalid number `n`." ) + assert( is_finite(n) && n>=1 && n<=len(l), "Invalid number `n`." ) n==1 - ? [for (i=[_s:1:len(l)-1]) [l[i]]] + ? [for (i=[_s:1:len(l)-1]) [l[i]]] : [for (i=[_s:1:len(l)-n], p=permute(l,n=n-1,_s=i+1)) concat([l[i]], p)]; @@ -1108,23 +1108,23 @@ function permute(l,n=2,_s=0) = // set_v = set_union(set_a, set_b, get_indices=true); // // set_v now equals [[5,0,1,2,6], [2,3,5,7,11,1,8]] function set_union(a, b, get_indices=false) = - assert( is_list(a) && is_list(b), "Invalid sets." ) + assert( is_list(a) && is_list(b), "Invalid sets." ) let( found1 = search(b, a), found2 = search(b, b), c = [ for (i=idx(b)) - if (found1[i] == [] && found2[i] == i) - b[i] - ], + if (found1[i] == [] && found2[i] == i) + b[i] + ], nset = concat(a, c) ) - ! get_indices ? nset : + ! get_indices ? nset : let( la = len(a), found3 = search(b, c), idxs = [ for (i=idx(b)) - (found1[i] != [])? found1[i] : la + found3[i] - ] + (found1[i] != [])? found1[i] : la + found3[i] + ] ) [idxs, nset]; //*** @@ -1145,7 +1145,7 @@ function set_union(a, b, get_indices=false) = // set_d = set_difference(set_a, set_b); // // set_d now equals [7,11] function set_difference(a, b) = - assert( is_list(a) && is_list(b), "Invalid sets." ) + assert( is_list(a) && is_list(b), "Invalid sets." ) let( found = search(a, b, num_returns_per_match=1) ) [ for (i=idx(a)) if(found[i]==[]) a[i] ]; @@ -1166,7 +1166,7 @@ function set_difference(a, b) = // set_i = set_intersection(set_a, set_b); // // set_i now equals [2,3,5] function set_intersection(a, b) = - assert( is_list(a) && is_list(b), "Invalid sets." ) + assert( is_list(a) && is_list(b), "Invalid sets." ) let( found = search(a, b, num_returns_per_match=1) ) [ for (i=idx(a)) if(found[i]!=[]) a[i] ]; @@ -1209,8 +1209,8 @@ function add_scalar(v,s) = // subindex(v,[2,1]); // Returns [[3, 2], [7, 6], [11, 10], [15, 14]] // subindex(v,[1:3]); // Returns [[2, 3, 4], [6, 7, 8], [10, 11, 12], [14, 15, 16]] function subindex(v, idx) = - [ for(val=v) - let( value=[for(i=idx) val[i]] ) + [ for(val=v) + let( value=[for(i=idx) val[i]] ) len(value)==1 ? value[0] : value ]; @@ -1249,7 +1249,7 @@ function zip(vecs, v2, v3, fit=false, fill=undef) = let( minlen = list_shortest(vecs), maxlen = list_longest(vecs) - ) + ) assert(fit!=false || minlen==maxlen, "Input vectors to zip must have the same length") (fit == "long") ? [for(i=[0:1:maxlen-1]) [for(v=vecs) for(x=(i len(dimlist))? 0 : dimlist[depth-1] ; @@ -1362,10 +1362,10 @@ function array_dim(v, depth=undef) = function transpose(arr) = let( a0 = arr[0] ) is_list(a0) - ? assert([for(a=arr) if(len(a)!=len(a0)) 1]==[], "The array is not a matrix." ) - [for (i=[0:1:len(a0)-1]) - [ for (j=[0:1:len(arr)-1]) arr[j][i] ] ] - : arr; + ? assert([for(a=arr) if(len(a)!=len(a0)) 1]==[], "The array is not a matrix." ) + [for (i=[0:1:len(a0)-1]) + [ for (j=[0:1:len(arr)-1]) arr[j][i] ] ] + : arr; //*** // Input data check and formating diff --git a/common.scad b/common.scad index 6cc910e..f3fabd5 100644 --- a/common.scad +++ b/common.scad @@ -320,7 +320,7 @@ function scalar_vec3(v, dflt=undef) = function segs(r) = $fn>0? ($fn>3? $fn : 3) : let( r = is_finite(r)? r: 0 ) - ceil(max(5, min(360/$fa, abs(r)*2*PI/$fs))) ; + ceil(max(5, min(360/$fa, abs(r)*2*PI/$fs))) ; //*** // avoids undef diff --git a/tests/test_arrays.scad b/tests/test_arrays.scad index d048afa..479f4bb 100644 --- a/tests/test_arrays.scad +++ b/tests/test_arrays.scad @@ -33,7 +33,7 @@ module test_in_list() { assert(in_list("bar", ["foo", "bar", "baz"])); assert(!in_list("bee", ["foo", "bar", "baz"])); assert(in_list("bar", [[2,"foo"], [4,"bar"], [3,"baz"]], idx=1)); - + assert(!in_list("bee", ["foo", "bar", ["bee"]])); assert(in_list(NAN, [NAN])==false); } diff --git a/vectors.scad b/vectors.scad index 92e4866..82535e0 100644 --- a/vectors.scad +++ b/vectors.scad @@ -217,8 +217,8 @@ function vector_axis(v1,v2=undef,v3=undef) = w1 = point3d(v1/norm(v1)), w2 = point3d(v2/norm(v2)), w3 = (norm(w1-w2) > eps && norm(w1+w2) > eps) ? w2 - : (norm(vabs(w2)-UP) > eps)? UP - : RIGHT + : (norm(vabs(w2)-UP) > eps)? UP + : RIGHT ) unit(cross(w1,w3));