From d52a0b7ab467adb5378fb1397a3a32335da96175 Mon Sep 17 00:00:00 2001 From: Revar Desmera Date: Mon, 4 Feb 2019 03:40:38 -0800 Subject: [PATCH] Clarified triangulation code and added docs. --- triangulation.scad | 85 ++++++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 30 deletions(-) diff --git a/triangulation.scad b/triangulation.scad index 05765c2..0ef2cb3 100644 --- a/triangulation.scad +++ b/triangulation.scad @@ -1,33 +1,44 @@ use -function winding_dir(points, face) = +// Given an array of vertices (`points`), and a list of indexes into the +// vertex array (`face`), returns the normal vector of the face. +// points = Array of vertices for the polyhedron. +// face = The face, given as a list of indices into the vertex array `points`. +function face_normal(points, face) = let(count=len(face)) - sum( - [ - for(i=[0:count-1]) cross( - points[face[(i+1)%count]]-points[face[0]], - points[face[(i+2)%count]]-points[face[(i+1)%count]] - ) - ], - 0 + normalize( + sum( + [ + for(i=[0:count-1]) cross( + points[face[(i+1)%count]]-points[face[0]], + points[face[(i+2)%count]]-points[face[(i+1)%count]] + ) + ] + ) ) ; -function find_convex_vertex(dir, points, face, i=0) = +// Returns the index of a convex point on the given face. +// points = Array of vertices for the polyhedron. +// face = The face, given as a list of indices into the vertex array `points`. +// facenorm = The normal vector of the face. +function find_convex_vertex(points, face, facenorm, i=0) = let(count=len(face), p0=points[face[i]], p1=points[face[(i+1)%count]], p2=points[face[(i+2)%count]] ) (len(face)>i)? - (cross(p1-p0, p2-p1)*dir>0)? (i+1)%count : find_convex_vertex(dir, points, face, i+1) + (cross(p1-p0, p2-p1)*facenorm>0)? (i+1)%count : find_convex_vertex(points, face, facenorm, i+1) : //This should never happen since there is at least 1 convex vertex. undef ; +// points = Array of vertices for the polyhedron. +// face = The face, given as a list of indices into the vertex array `points`. function point_in_ear(points, face, tests, i=0) = (i