Merge pull request #177 from adrianVmariano/master

Update vnf_volume and vnf_centroid to avoid vnf_triangulate.  Huge speed gain.
This commit is contained in:
Revar Desmera 2020-06-13 15:05:30 -07:00 committed by GitHub
commit 1da733fd32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -358,14 +358,16 @@ module vnf_polyhedron(vnf, convexity=2) {
// no holes; otherwise the results are undefined. Returns a positive volume if face direction is clockwise and a negative volume // no holes; otherwise the results are undefined. Returns a positive volume if face direction is clockwise and a negative volume
// if face direction is counter-clockwise. // if face direction is counter-clockwise.
function vnf_volume(vnf) = function vnf_volume(vnf) =
let(verts = vnf[0])
sum([
for(face=vnf[1], j=[1:1:len(face)-2])
let( let(
vnf = vnf_triangulate(vnf), v0 = verts[face[0]],
verts = vnf[0] v1 = verts[face[j]],
) sum([ v2 = verts[face[j+1]],
for(face_index=vnf[1]) let( n = cross(v2-v0,v1-v0)
face = select(verts, face_index), )
n = cross(face[2]-face[0],face[1]-face[0]) v0 * n
) face[0] * n
])/6; ])/6;
@ -379,19 +381,20 @@ function vnf_volume(vnf) =
// Algorithm from: https://wwwf.imperial.ac.uk/~rn/centroid.pdf // Algorithm from: https://wwwf.imperial.ac.uk/~rn/centroid.pdf
function vnf_centroid(vnf) = function vnf_centroid(vnf) =
let( let(
vnf = vnf_triangulate(vnf),
verts = vnf[0], verts = vnf[0],
val=sum([ val=sum([
for(face_index=vnf[1]) for(face=vnf[1], j=[1:1:len(face)-2])
let( let(
face = select(verts, face_index), v0 = verts[face[0]],
n = cross(face[2]-face[0],face[1]-face[0]) v1 = verts[face[j]],
v2 = verts[face[j+1]],
n = cross(v2-v0,v1-v0)
) [ ) [
face[0] * n, v0 * n,
vmul(n, vmul(n,
sqr(face[0] + face[1]) + sqr(v0 + v1) +
sqr(face[0] + face[2]) + sqr(v0 + v2) +
sqr(face[1] + face[2]) sqr(v1 + v2)
) )
] ]
]) ])