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

@ -356,16 +356,18 @@ module vnf_polyhedron(vnf, convexity=2) {
// Description: // Description:
// Returns the volume enclosed by the given manifold VNF. The VNF must describe a valid polyhedron with consistent face direction and // Returns the volume enclosed by the given manifold VNF. The VNF must describe a valid polyhedron with consistent face direction and
// 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( let(verts = vnf[0])
vnf = vnf_triangulate(vnf), sum([
verts = vnf[0] for(face=vnf[1], j=[1:1:len(face)-2])
) sum([ let(
for(face_index=vnf[1]) let( v0 = verts[face[0]],
face = select(verts, face_index), v1 = verts[face[j]],
n = cross(face[2]-face[0],face[1]-face[0]) v2 = verts[face[j+1]],
) face[0] * n n = cross(v2-v0,v1-v0)
)
v0 * 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)
) )
] ]
]) ])