Added vsqr(), vnf_centroid(), and vnf_volume()

This commit is contained in:
Revar Desmera 2020-03-20 22:15:41 -07:00
parent 9f3cf44f91
commit e635755528
3 changed files with 50 additions and 1 deletions

View file

@ -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 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() // Function: unit()
// Description: // Description:
// Returns unit length normalized version of vector v. // Returns unit length normalized version of vector v.

View file

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

View file

@ -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() // Function&Module: vnf_validate()
// Usage: As Function // Usage: As Function
// fails = vnf_validate(vnf); // fails = vnf_validate(vnf);