mirror of
https://gitlab.com/MisterBiggs/stl-process.git
synced 2025-08-05 13:01:23 +00:00
Calculate Characteristic Length
This commit is contained in:
@@ -10,16 +10,17 @@ struct Properties
|
||||
center_of_gravity::Vector{Float64}
|
||||
inertia::Matrix{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(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
|
||||
|
||||
function get_mass_properties(triangles; scale=1)
|
||||
function get_mass_properties(triangles; scale=1.0)
|
||||
"""
|
||||
Reference:
|
||||
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
|
||||
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
|
||||
|
||||
function fast_volume(triangles; scale=1)
|
||||
|
Reference in New Issue
Block a user