Added vnf.scad regressions and bugfixes.

This commit is contained in:
Revar Desmera 2019-11-06 16:17:33 -08:00
parent 59c48f37d6
commit 5addbe8184
3 changed files with 113 additions and 3 deletions

103
tests/test_vnf.scad Normal file
View file

@ -0,0 +1,103 @@
include <BOSL2/std.scad>
include <BOSL2/vnf.scad>
module test_is_vnf() {
assert(is_vnf([[],[]]));
assert(!is_vnf([]));
assert(is_vnf([[[-1,-1,-1],[1,-1,-1],[0,1,-1],[0,0,1]],[[0,1,2],[0,3,1],[1,3,2],[2,3,0]]]));
}
test_is_vnf();
module test_is_vnf_list() {
assert(is_vnf_list([]));
assert(!is_vnf_list([[],[]]));
assert(is_vnf_list([[[],[]]]));
assert(!is_vnf_list([[[-1,-1,-1],[1,-1,-1],[0,1,-1],[0,0,1]],[[0,1,2],[0,3,1],[1,3,2],[2,3,0]]]));
assert(is_vnf_list([[[[-1,-1,-1],[1,-1,-1],[0,1,-1],[0,0,1]],[[0,1,2],[0,3,1],[1,3,2],[2,3,0]]]]));
}
test_is_vnf_list();
module test_vnf_vertices() {
vnf = [[[-1,-1,-1],[1,-1,-1],[0,1,-1],[0,0,1]],[[0,1,2],[0,3,1],[1,3,2],[2,3,0]]];
assert(vnf_vertices(vnf) == vnf[0]);
}
test_vnf_vertices();
module test_vnf_faces() {
vnf = [[[-1,-1,-1],[1,-1,-1],[0,1,-1],[0,0,1]],[[0,1,2],[0,3,1],[1,3,2],[2,3,0]]];
assert(vnf_faces(vnf) == vnf[1]);
}
test_vnf_faces();
module test_vnf_get_vertex() {
vnf = [[[-1,-1,-1],[1,-1,-1],[0,1,-1],[0,0,1]],[[0,1,2],[0,3,1],[1,3,2],[2,3,0]]];
assert(vnf_get_vertex(vnf,[0,1,-1]) == [2,vnf]);
assert(vnf_get_vertex(vnf,[0,1,2]) == [4,[concat(vnf[0],[[0,1,2]]),vnf[1]]]);
}
test_vnf_get_vertex();
module test_vnf_add_face() {
verts = [[-1,-1,-1],[1,-1,-1],[0,1,-1],[0,0,1]];
faces = [[0,1,2],[0,3,1],[1,3,2],[2,3,0]];
vnf1 = vnf_add_face(pts=select(verts,faces[0]));
vnf2 = vnf_add_face(vnf1, pts=select(verts,faces[1]));
vnf3 = vnf_add_face(vnf2, pts=select(verts,faces[2]));
vnf4 = vnf_add_face(vnf3, pts=select(verts,faces[3]));
assert(vnf1 == [select(verts,0,2),select(faces,[0])]);
assert(vnf2 == [verts,select(faces,[0:1])]);
assert(vnf3 == [verts,select(faces,[0:2])]);
assert(vnf4 == [verts,faces]);
}
test_vnf_add_face();
module test_vnf_add_faces() {
verts = [[-1,-1,-1],[1,-1,-1],[0,1,-1],[0,0,1]];
faces = [[0,1,2],[0,3,1],[1,3,2],[2,3,0]];
assert(vnf_add_faces(faces=[for (face=faces) select(verts,face)]) == [verts,faces]);
}
test_vnf_add_faces();
module test_vnf_merge() {
vnf1 = vnf_add_face(pts=[[-1,-1,-1],[1,-1,-1],[0,1,-1]]);
vnf2 = vnf_add_face(pts=[[1,1,1],[-1,1,1],[0,1,-1]]);
assert(vnf_merge([vnf1,vnf2]) == [[[-1,-1,-1],[1,-1,-1],[0,1,-1],[1,1,1],[-1,1,1],[0,1,-1]],[[0,1,2],[3,4,5]]]);
}
test_vnf_merge();
module test_vnf_triangulate() {
vnf = [[[-1,-1,0],[1,-1,0],[1,1,0],[-1,1,0]],[[0,1,2,3]]];
assert(vnf_triangulate(vnf) == [[[-1,-1,0],[1,-1,0],[1,1,0],[-1,1,0]], [[0,1,2],[2,3,0]]]);
}
test_vnf_triangulate();
module test_vnf_vertex_array() {
vnf1 = vnf_vertex_array(
points=[for (h=[0:100:100]) [[100,-50,h],[-100,-50,h],[0,100,h]]],
col_wrap=true, caps=true
);
vnf2 = vnf_vertex_array(
points=[for (h=[0:100:100]) [[100,-50,h],[-100,-50,h],[0,100,h]]],
col_wrap=true, caps=true, style="alt"
);
vnf3 = vnf_vertex_array(
points=[for (h=[0:100:100]) [[100,-50,h],[-100,-50,h],[0,100,h]]],
col_wrap=true, caps=true, style="quincunx"
);
assert(vnf1 == [[[100,-50,0],[-100,-50,0],[0,100,0],[100,-50,100],[-100,-50,100],[0,100,100]],[[0,4,3],[0,1,4],[1,5,4],[1,2,5],[2,3,5],[2,0,3],[2,1,0],[3,4,5]]]);
assert(vnf2 == [[[100,-50,0],[-100,-50,0],[0,100,0],[100,-50,100],[-100,-50,100],[0,100,100]],[[0,1,3],[3,1,4],[1,2,4],[4,2,5],[2,0,5],[5,0,3],[2,1,0],[3,4,5]]]);
assert(vnf3 == [[[100,-50,0],[-100,-50,0],[0,100,0],[100,-50,100],[-100,-50,100],[0,100,100],[0,-50,50],[-50,25,50],[50,25,50]],[[0,6,3],[3,6,4],[4,6,1],[1,6,0],[1,7,4],[4,7,5],[5,7,2],[2,7,1],[2,8,5],[5,8,3],[3,8,0],[0,8,2],[2,1,0],[3,4,5]]]);
}
test_vnf_vertex_array();
// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap

View file

@ -8,7 +8,7 @@
//////////////////////////////////////////////////////////////////////
BOSL_VERSION = [2,0,8];
BOSL_VERSION = [2,0,9];
// Section: BOSL Library Version Functions

View file

@ -58,6 +58,8 @@ function vnf_faces(vnf) = vnf[1];
// vnf4 = vnf_get_vertex(vnf3, p=[[1,3,2],[3,2,1]]); // Returns: [[1,2], [[[3,5,8],[3,2,1],[1,3,2]],[]]]
function vnf_get_vertex(vnf=[[],[]], p) =
is_path(p)? _vnf_get_vertices(vnf, p) :
assert(is_vnf(vnf))
assert(is_vector(p))
let(
p = quant(p,1/1024), // OpenSCAD internally quantizes objects to 1/1024.
v = search([p], vnf[0])[0]
@ -89,6 +91,8 @@ function _vnf_get_vertices(vnf=[[],[]], pts, _i=0, _idxs=[]) =
// vnf = The VNF structure to add a face to.
// pts = The vertex points for the face.
function vnf_add_face(vnf=[[],[]], pts) =
assert(is_vnf(vnf))
assert(is_path(pts))
let(
vvnf = vnf_get_vertex(vnf, pts),
face = deduplicate(vvnf[0], closed=true),
@ -111,7 +115,8 @@ function vnf_add_face(vnf=[[],[]], pts) =
// vnf = The VNF structure to add a face to.
// faces = The list of faces, where each face is given as a list of vertex points.
function vnf_add_faces(vnf=[[],[]], faces, _i=0) =
_i<len(faces)? vnf_add_faces(vnf_add_face(vnf, faces[_i]), faces, _i=_i+1) : vnf;
(assert(is_vnf(vnf)) assert(is_list(faces)) _i>=len(faces))? vnf :
vnf_add_faces(vnf_add_face(vnf, faces[_i]), faces, _i=_i+1);
// Function: vnf_merge()
@ -119,7 +124,8 @@ function vnf_add_faces(vnf=[[],[]], faces, _i=0) =
// vnf = vnf_merge([VNF, VNF, VNF, ...]);
// Description:
// Given a list of VNF structures, merges them all into a single VNF structure.
function vnf_merge(vnfs=[],_i=0,_acc=[[],[]]) = _i>=len(vnfs)? _acc :
function vnf_merge(vnfs=[],_i=0,_acc=[[],[]]) =
(assert(is_vnf_list(vnfs)) _i>=len(vnfs))? _acc :
vnf_merge(
vnfs, _i=_i+1,
_acc = let(base=len(_acc[0])) [
@ -225,6 +231,7 @@ function vnf_vertex_array(
assert(in_list(style,["default","alt","quincunx"]))
let(
pts = flatten(points),
pcnt = len(pts),
rows = len(points),
cols = len(points[0]),
errchk = [for (row=points) assert(len(row)==cols, "All rows much have the same number of columns.") 0],