1
0
mirror of https://gitlab.com/MisterBiggs/stl-process.git synced 2025-08-05 13:01:23 +00:00

added function to find volume

This commit is contained in:
2022-03-30 21:30:04 -07:00
parent a6fcb39140
commit d137eb4050
2 changed files with 61 additions and 11 deletions

View File

@@ -16,15 +16,52 @@ struct Properties
end
end
function get_mass_properties(triangles)
function get_volume(triangles; scale=1)
"""
Reference:
https://people.eecs.berkeley.edu/~wkahan/VtetLang.pdf
"""
volume = 0.0
for tri in triangles
a, b, c = tri .* scale
W = sqrt(sum((a .- b) .^ 2))
V = sqrt(sum((b .- c) .^ 2))
U = sqrt(sum((c .- a) .^ 2))
v = sqrt(sum(a .^ 2))
u = sqrt(sum(b .^ 2))
w = sqrt(sum(c .^ 2))
X = (w - U + v) * (U + v + w)
Y = (u - V + w) * (V + w + u)
Z = (v - W + u) * (W + u + v)
x = (U - v + w) * (v - w + U)
y = (V - w + u) * (w - u + V)
z = (W - u + v) * (u - v + W)
ξ = sqrt(x * Y * Z)
η = sqrt(y * Z * X)
ζ = sqrt(z * X * Y)
λ = sqrt(x * y * z)
volume += sqrt((ξ + η + ζ - λ) * (λ + ξ + η - ζ) * (η + ζ + λ - ξ) * (ζ + λ + ξ - η)) / (192 * u * v * w)
end
return volume
end
function get_mass_properties(triangles; scale=1)
"""
Reference:
https://github.com/WoLpH/numpy-stl/blob/42b6c67324e13b8712af6730f77e5e9544ef63b0/stl/base.py#L362
https://www.geometrictools.com/Documentation/PolyhedralMassProperties.pdf
"""
x = reduce(hcat, [[v[1] for v in tri] for tri in triangles])'
y = reduce(hcat, [[v[2] for v in tri] for tri in triangles])'
z = reduce(hcat, [[v[3] for v in tri] for tri in triangles])'
x = reduce(hcat, [[v[1] .* scale for v in tri] for tri in triangles])'
y = reduce(hcat, [[v[2] .* scale for v in tri] for tri in triangles])'
z = reduce(hcat, [[v[3] .* scale for v in tri] for tri in triangles])'
function subexpression(x)
w0, w1, w2 = x[:, 1], x[:, 2], x[:, 3]
@@ -76,6 +113,6 @@ function get_mass_properties(triangles)
return Properties(volume, center_of_gravity, inertia)
end
export get_mass_properties
export get_mass_properties, get_volume
end # module