diff --git a/tests/test_vectors.scad b/tests/test_vectors.scad index f5d3098..eede176 100644 --- a/tests/test_vectors.scad +++ b/tests/test_vectors.scad @@ -178,7 +178,7 @@ test_vector_search(); module test_vector_search_tree(){ points1 = [ [0,1,2], [1,2,3], [2,3,4] ]; tree1 = vector_search_tree(points1); - assert(tree1 == [ points1, [[0,1,2]] ]); + assert(tree1 == points1); points2 = [for(i=[0:9], j=[0:9], k=[1:5]) [i,j,k] ]; tree2 = vector_search_tree(points2); assert(tree2[0]==points2); diff --git a/vectors.scad b/vectors.scad index e9dcdca..e75216a 100644 --- a/vectors.scad +++ b/vectors.scad @@ -171,8 +171,8 @@ function pointlist_bounds(pts) = // v5 = unit([0,0,0],[1,2,3]); // Returns: [1,2,3] // v6 = unit([0,0,0]); // Asserts an error. function unit(v, error=[[["ASSERT"]]]) = - assert(is_vector(v), str("Expected a vector. Got: ",v)) - norm(v)=EPSILON,"Tried to normalize a zero vector") : error) : + assert(is_vector(v), "Invalid vector") + norm(v)=EPSILON,"Cannot normalize a zero vector") : error) : v/norm(v); @@ -313,8 +313,10 @@ function furthest_point(pt, points) = // When `target` is a large list of points, a search tree is constructed to // speed up the search with an order around O(log n) per query point. // For small point lists, a direct search is done dispensing a tree construction. -// Alternatively, `target` may be a search tree built with `vector_tree_search()`. +// Alternatively, `target` may be a search tree built with `vector_search_tree()`. // In that case, that tree is parsed looking for matches. +// An empty list of query points will return a empty output list. +// An empty list of target points will return a output list with an empty list for each query point. // Arguments: // query = list of points to find matches for. // r = the search radius. @@ -351,6 +353,8 @@ function furthest_point(pt, points) = // color("red") move_copies(select(points, search_2[i])) circle(r=.08); // } function vector_search(query, r, target) = + query==[] ? [] : + is_list(query) && target==[] ? is_vector(query) ? [] : [for(q=query) [] ] : assert( is_finite(r) && r>=0, "The query radius should be a positive number." ) let( @@ -365,13 +369,12 @@ function vector_search(query, r, target) = "The target should be a list of points or a search tree compatible with the query." ) let( dim = tgpts ? len(target[0]) : len(target[0][0]), - simple = is_vector(query, dim), - mult = !simple && is_matrix(query,undef,dim) - ) - assert( simple || mult, + simple = is_vector(query, dim) + ) + assert( simple || is_matrix(query,undef,dim), "The query points should be a list of points compatible with the target point list.") tgpts - ? len(target)<200 + ? len(target)<=400 ? simple ? [for(i=idx(target)) if(norm(target[i]-query)=1, "The tree leaf size should be an integer greater than zero.") + len(points)