From 60c3a99e90c22a19cc21c5eb7d9e670387037c9c Mon Sep 17 00:00:00 2001 From: Adrian Mariano Date: Tue, 28 Sep 2021 19:08:47 -0400 Subject: [PATCH] moved some functions around and renamed --- paths.scad | 50 ++++++++++++++++++++++++++++++++++ regions.scad | 77 ++++------------------------------------------------ skin.scad | 4 +-- vnf.scad | 4 ++- 4 files changed, 61 insertions(+), 74 deletions(-) diff --git a/paths.scad b/paths.scad index d80cee4..4026d3e 100644 --- a/paths.scad +++ b/paths.scad @@ -125,6 +125,56 @@ function path_merge_collinear(path, closed=false, eps=EPSILON) = ) [for (i=indices) path[i]]; +// Function: are_polygons_equal() +// Usage: +// b = are_polygons_equal(poly1, poly2, [eps]) +// Description: +// Returns true if poly1 and poly2 are the same polongs +// within given epsilon tolerance. +// Arguments: +// poly1 = first polygon +// poly2 = second polygon +// eps = tolerance for comparison +// Example(NORENDER): +// are_polygons_equal(pentagon(r=4), +// rot(360/5, p=pentagon(r=4))); // returns true +// are_polygons_equal(pentagon(r=4), +// rot(90, p=pentagon(r=4))); // returns false +function are_polygons_equal(poly1, poly2, eps=EPSILON) = + let( + poly1 = cleanup_path(poly1), + poly2 = cleanup_path(poly2), + l1 = len(poly1), + l2 = len(poly2) + ) l1 != l2 ? false : + let( maybes = find_first_match(poly1[0], poly2, eps=eps, all=true) ) + maybes == []? false : + [for (i=maybes) if (_are_polygons_equal(poly1, poly2, eps, i)) 1] != []; + +function _are_polygons_equal(poly1, poly2, eps, st) = + max([for(d=poly1-select(poly2,st,st-1)) d*d])= len(polys)? false : + are_polygons_equal(poly, polys[i])? true : + __is_polygon_in_list(poly, polys, i+1); + + + // Section: Path length calculation diff --git a/regions.scad b/regions.scad index 44ec3cc..83ad84a 100644 --- a/regions.scad +++ b/regions.scad @@ -170,74 +170,9 @@ function is_region_simple(region, eps=EPSILON) = ] ==[]; - -function approx_sign(x) = approx(x,0) ? 0 : sign(x); - - -function do_segments_intersect(s1,s2) = - let( - a1=cross(s1[1]-s1[0], s2[0]-s1[1]), - a2=cross(s1[1]-s1[0], s2[1]-s1[1]), - a3=cross(s2[1]-s2[0], s1[0]-s2[1]), - a4=cross(s2[1]-s2[0], s1[1]-s2[1]) - ) - approx_sign(a1)!=approx_sign(a2) && approx_sign(a3)!=approx_sign(a4); - -// note that parallel intersecting lines seem to have all the a's equal approx to zero - - -// Function: polygons_equal() +// Function: are_regions_equal() // Usage: -// b = polygons_equal(poly1, poly2, [eps]) -// Description: -// Returns true if poly1 and poly2 are the same polongs -// within given epsilon tolerance. -// Arguments: -// poly1 = first polygon -// poly2 = second polygon -// eps = tolerance for comparison -// Example(NORENDER): -// polygons_equal(pentagon(r=4), -// rot(360/5, p=pentagon(r=4))); // returns true -// polygons_equal(pentagon(r=4), -// rot(90, p=pentagon(r=4))); // returns false -function polygons_equal(poly1, poly2, eps=EPSILON) = - let( - poly1 = cleanup_path(poly1), - poly2 = cleanup_path(poly2), - l1 = len(poly1), - l2 = len(poly2) - ) l1 != l2 ? false : - let( maybes = find_first_match(poly1[0], poly2, eps=eps, all=true) ) - maybes == []? false : - [for (i=maybes) if (__polygons_equal(poly1, poly2, eps, i)) 1] != []; - -function __polygons_equal(poly1, poly2, eps, st) = - max([for(d=poly1-select(poly2,st,st-1)) d*d])= len(polys)? false : - polygons_equal(poly, polys[i])? true : - __is_polygon_in_list(poly, polys, i+1); - - -// Function: regions_equal() -// Usage: -// b = regions_equal(region1, region2, [eps]) +// b = are_regions_equal(region1, region2, [eps]) // Description: // Returns true if the components of region1 and region2 are the same polygons (in any order) // within given epsilon tolerance. @@ -245,15 +180,15 @@ function __is_polygon_in_list(poly, polys, i) = // region1 = first region // region2 = second region // eps = tolerance for comparison -function regions_equal(region1, region2) = +function are_regions_equal(region1, region2) = assert(is_region(region1) && is_region(region2)) len(region1) != len(region2)? false : - __regions_equal(region1, region2, 0); + __are_regions_equal(region1, region2, 0); -function __regions_equal(region1, region2, i) = +function __are_regions_equal(region1, region2, i) = i >= len(region1)? true : !is_polygon_in_list(region1[i], region2)? false : - __regions_equal(region1, region2, i+1); + __are_regions_equal(region1, region2, i+1); /// Internal Function: _path_region_intersections() diff --git a/skin.scad b/skin.scad index eda48a5..4f1326d 100644 --- a/skin.scad +++ b/skin.scad @@ -910,8 +910,8 @@ function path_sweep(shape, path, method="incremental", normal, closed=false, twi : let( rshape = is_path(shape) ? [path3d(shape)] : [for(s=shape) path3d(s)] ) - regions_equal(apply(transform_list[0], rshape), - apply(transform_list[L], rshape)), + are_regions_equal(apply(transform_list[0], rshape), + apply(transform_list[L], rshape)), dummy = ends_match ? 0 : echo("WARNING: ***** The points do not match when closing the model *****") ) transforms ? transform_list : sweep(is_path(shape)?clockwise_polygon(shape):shape, transform_list, closed=false, caps=fullcaps,style=style); diff --git a/vnf.scad b/vnf.scad index 3ea55d3..fd84840 100644 --- a/vnf.scad +++ b/vnf.scad @@ -10,13 +10,15 @@ // Section: Creating Polyhedrons with VNF Structures - // VNF stands for "Vertices'N'Faces". VNF structures are 2-item lists, `[VERTICES,FACES]` where the // first item is a list of vertex points, and the second is a list of face indices into the vertex // list. Each VNF is self contained, with face indices referring only to its own vertex list. // You can construct a `polyhedron()` in parts by describing each part in a self-contained VNF, then // merge the various VNFs to get the completed polyhedron vertex list and faces. +// Constant: EMPTY_VNF +// Description: +// The empty VNF data structure. Equal to `[[],[]]`. EMPTY_VNF = [[],[]]; // The standard empty VNF with no vertices or faces.