From e635755528dc7642e9b166b95e2367942ed31dcd Mon Sep 17 00:00:00 2001 From: Revar Desmera Date: Fri, 20 Mar 2020 22:15:41 -0700 Subject: [PATCH] Added vsqr(), vnf_centroid(), and vnf_volume() --- vectors.scad | 8 ++++++++ version.scad | 2 +- vnf.scad | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/vectors.scad b/vectors.scad index ed8a848..9509b6c 100644 --- a/vectors.scad +++ b/vectors.scad @@ -95,6 +95,14 @@ function vdiv(v1, v2) = [for (i = [0:1:len(v1)-1]) v1[i]/v2[i]]; function vabs(v) = [for (x=v) abs(x)]; +// Function: vsqr() +// Usage: +// x = vsqr(v); +// Description: +// Returns a vector where each value in the original given vector is squared. +function vsqr(v) = [for(i=v) i*i]; + + // Function: unit() // Description: // Returns unit length normalized version of vector v. diff --git a/version.scad b/version.scad index b1ce138..1ed74dc 100644 --- a/version.scad +++ b/version.scad @@ -8,7 +8,7 @@ ////////////////////////////////////////////////////////////////////// -BOSL_VERSION = [2,0,208]; +BOSL_VERSION = [2,0,209]; // Section: BOSL Library Version Functions diff --git a/vnf.scad b/vnf.scad index 568ec7d..e1ea665 100644 --- a/vnf.scad +++ b/vnf.scad @@ -336,6 +336,47 @@ module vnf_polyhedron(vnf, convexity=2) { } +// Function: vnf_volume() +// Usage: +// vol = vnf_volume(vnf); +// Description: +// Returns the volume enclosed by the given manifold VNF. May return a negative value if faces are reversed. +function vnf_volume(vnf) = + let(vnf = vnf_triangulate(vnf)) + sum([ + for(face_index=vnf[1]) let( + face = select(vnf[0], face_index), + n = cross(face[2]-face[0],face[1]-face[0]) + ) face[0] * n + ])/6; + + +// Function: vnf_centroid() +// Usage: +// vol = vnf_centroid(vnf); +// Description: +// Returns the centroid of the given manifold VNF. +function vnf_centroid(vnf) = + let( + vnf = vnf_triangulate(vnf), + val=sum([ + for(face_index=vnf[1]) + let( + face = select(vnf[0], face_index), + n = cross(face[2]-face[0],face[1]-face[0]) + ) [ + face[0] * n, + vmul(n, + vsqr(face[0] + face[1]) + + vsqr(face[0] + face[2]) + + vsqr(face[1] + face[2]) + ) + ] + ]) + ) val[1]/val[0]/8; + + + // Function&Module: vnf_validate() // Usage: As Function // fails = vnf_validate(vnf);