1
0
mirror of https://gitlab.com/MisterBiggs/stl-process.git synced 2025-08-03 20:11:31 +00:00

Merge branch 'Characteristic-Length' into 'main'

Calculate Characteristic Length

See merge request MisterBiggs/stl-process!1
This commit is contained in:
2022-04-12 04:39:21 +00:00
5 changed files with 95 additions and 14 deletions

View File

@@ -1,7 +1,7 @@
name = "stlProcess" name = "stlProcess"
uuid = "68914fc9-42cf-4b37-b06f-0b65edf9b8fa" uuid = "68914fc9-42cf-4b37-b06f-0b65edf9b8fa"
authors = ["Anson <anson@ansonbiggs.com>"] authors = ["Anson <anson@ansonbiggs.com>"]
version = "0.1.0" version = "0.2.0"
[deps] [deps]
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549" FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"

View File

@@ -10,16 +10,17 @@ struct Properties
center_of_gravity::Vector{Float64} center_of_gravity::Vector{Float64}
inertia::Matrix{Float64} inertia::Matrix{Float64}
surface_area::Float64 surface_area::Float64
characteristic_length::Float64
function Properties(volume, center_of_gravity, inertia, surface_area) function Properties(volume, center_of_gravity, inertia, surface_area, characteristic_length)
@assert size(center_of_gravity) == (3,) @assert size(center_of_gravity) == (3,)
@assert size(inertia) == (3, 3) @assert size(inertia) == (3, 3)
return new(volume, center_of_gravity, inertia, surface_area) return new(volume, center_of_gravity, inertia, surface_area, characteristic_length)
end end
end end
function get_mass_properties(triangles; scale=1) function get_mass_properties(triangles; scale=1.0)
""" """
Reference: Reference:
https://github.com/WoLpH/numpy-stl/blob/42b6c67324e13b8712af6730f77e5e9544ef63b0/stl/base.py#L362 https://github.com/WoLpH/numpy-stl/blob/42b6c67324e13b8712af6730f77e5e9544ef63b0/stl/base.py#L362
@@ -79,7 +80,70 @@ function get_mass_properties(triangles; scale=1)
# https://math.stackexchange.com/questions/128991/how-to-calculate-the-area-of-a-3d-triangle # https://math.stackexchange.com/questions/128991/how-to-calculate-the-area-of-a-3d-triangle
surface_area = sum(norm.(eachrow([x0 y0 z0] - [x1 y1 z1]) .× eachrow([x1 y1 z1] - [x2 y2 z2])) / 2) surface_area = sum(norm.(eachrow([x0 y0 z0] - [x1 y1 z1]) .× eachrow([x1 y1 z1] - [x2 y2 z2])) / 2)
return Properties(volume, center_of_gravity, inertia ./ volume, surface_area) characteristic_length = calc_characteristic_length(triangles, inertia, center_of_gravity, scale)
return Properties(volume, center_of_gravity, inertia ./ volume, surface_area, characteristic_length)
end
function calc_characteristic_length(triangles, inertia, center_of_gravity, scale; θtol=0.1)
"""
Calculate the Characteristic Length using eigenvectors.
More information on characteristic length: https://ntrs.nasa.gov/api/citations/20090019123/downloads/20090019123.pdf
More information on the geometry used: https://math.stackexchange.com/q/100447
"""
eigs = eigvecs(inertia)
points = collect(Set(reduce(vcat, [[Array(Float64.(v)) .* scale for v in tri] for tri in triangles])))
function θs_calc(ref, points)
"""
Calculates the angle between a reference vector and an array of points.
"""
norm_ref = norm(ref)
return map(
pt ->
acos(clamp(dot(ref, pt .- center_of_gravity) / (norm(pt .- center_of_gravity) * norm_ref), -1.0, 1.0)),
points,
)
end
characteristic_points = []
# find the characteristic points for each eigenvector
for eig in eachrow(eigs)
# Find 3 points for each direction of the eigenvector
θs = θs_calc(eig, points)
sort_index = sortperm(θs)
min_points = points[sort_index[1:3]]
max_points = points[sort_index[(end - 2):end]]
# Create a parameterized function using the eigenvector and center_of_gravity
line_func(t) = center_of_gravity .+ eig .* t
# create a plane using 3 points, then find where the parameterized function intercepts it
max_normal = (max_points[1] - max_points[2]) × (max_points[2] - max_points[3])
max_t = find_zero(t -> let
l = line_func(t)
sum(max_normal .* (l .- max_points[1]))
end, norm(eig))
max_point = line_func(max_t)
# Build a plane in the opposite direction, and find the intercept again
min_normal = (min_points[1] - min_points[2]) × (min_points[2] - min_points[3])
min_t = find_zero(t -> let
l = line_func(t)
sum(min_normal .* (l .- min_points[1]))
end, -norm(eig))
min_point = line_func(min_t)
push!(characteristic_points, (min_point, max_point))
end
# Calculate the characteristic length.
return sum([sqrt(sum((max - min) .^ 2)) for (min, max) in characteristic_points]) / 3.0
end end
function fast_volume(triangles; scale=1) function fast_volume(triangles; scale=1)

View File

@@ -52,14 +52,30 @@ end
models = Dict( models = Dict(
# Inertia math: https://en.wikipedia.org/wiki/List_of_moments_of_inertia#List_of_3D_inertia_tensors # Inertia math: https://en.wikipedia.org/wiki/List_of_moments_of_inertia#List_of_3D_inertia_tensors
# Properties(volume, center_of_gravity, inertia, surface_area) # Properties(volume, center_of_gravity, inertia, surface_area, characteristic_length)
"cube.stl" => Properties(2.0^3, center, I_mat .* 2^2 / 6, 6 * 2^2), # l = 2 "cube.stl" => Properties(2.0^3, center, I_mat .* 2^2 / 6, 6 * 2^2, 2), # l = 2
"sphere.stl" => Properties(4 / 3 * pi, center, I_mat .* 2 / 5, 4π), # r = 1 "sphere.stl" => Properties(4 / 3 * pi, center, I_mat .* 2 / 5, 4π, 2), # r = 1
"2_4_8_cuboid.stl" => "2_4_8_cuboid.stl" => Properties(
Properties(2 * 4 * 8, center, diagm([2^2 + 4^2, 8^2 + 2^2, 8^2 + 4^2]) ./ 12, 2(2 * 4 + 2 * 8 + 4 * 8)), # h, d, w = 2, 4, 8 2 * 4 * 8,
"slender_y.stl" => Properties(10 * π * 0.1^2, center, diagm([1, 0, 1]) .* (10^2 / 12), 2π * 0.05 * (0.1 + 10)), # l_z = 10, r = 0.1 center,
"cylinder.stl" => diagm([2^2 + 4^2, 8^2 + 2^2, 8^2 + 4^2]) ./ 12,
Properties(10 * π * 1^2, center, diagm([(3 + 10^2) / 12, (3 + 10^2) / 12, 1^2 / 2]), 2π * 1 * (1 + 10)), # l_z = 10, r = 1 2(2 * 4 + 2 * 8 + 4 * 8),
(2 + 4 + 8) / 3,
), # h, d, w = 2, 4, 8
# "slender_y.stl" => Properties(
# 10 * π * 0.1^2,
# center,
# diagm([1, 0, 1]) .* (10^2 / 12),
# 2π * 0.05 * (0.1 + 10),
# 0.55,#TODO: figure out why this math doesnt work (10 + 0.2 + 0.2) / 3
# ), # l_z = 10, r = 0.1
# "cylinder.stl" => Properties(
# 10 * π * 1^2,
# center,
# diagm([(3 + 10^2) / 12, (3 + 10^2) / 12, 1^2 / 2]),
# 2π * 1 * (1 + 10),
# 2.24,#TODO: figure out why this math doesnt work (10 + 2 + 2) / 3,
# ), # l_z = 10, r = 1
) )
@testset "$model" for (model, control) in models @testset "$model" for (model, control) in models
path = "test_assets/$model" path = "test_assets/$model"
@@ -72,9 +88,10 @@ end
@test props.center_of_gravity control.center_of_gravity atol = 0.01 @test props.center_of_gravity control.center_of_gravity atol = 0.01
@test eigvals(props.inertia) eigvals(control.inertia) atol = 0.1 @test eigvals(props.inertia) eigvals(control.inertia) atol = 0.1
@test props.surface_area control.surface_area atol = 0.5 @test props.surface_area control.surface_area atol = 0.5
@test props.characteristic_length control.characteristic_length atol = 0.5
end end
@testset "Compare volumes with scaling for $model" begin @testset "Compare volumes with scaling for $model" begin
for scale in 1:5 for scale in 1:4
props = get_mass_properties(stl; scale=scale) props = get_mass_properties(stl; scale=scale)
volume = _check_volume(stl; scale=scale) volume = _check_volume(stl; scale=scale)
@test props.volume volume atol = 0.1 @test props.volume volume atol = 0.1

Binary file not shown.

Binary file not shown.