mirror of
https://gitlab.com/MisterBiggs/stl-process.git
synced 2025-08-03 12:01:33 +00:00
made tests like way better
This commit is contained in:
@@ -74,7 +74,7 @@ function get_mass_properties(triangles; scale=1)
|
||||
inertia[2, 3] = inertia[3, 2] = -(intg[9] - volume .* center_of_gravity[2] .* center_of_gravity[3])
|
||||
inertia[1, 3] = inertia[3, 1] = -(intg[10] - volume .* center_of_gravity[3] .* center_of_gravity[1])
|
||||
|
||||
return Properties(volume, center_of_gravity, inertia)
|
||||
return Properties(volume, center_of_gravity, inertia ./ volume)
|
||||
end
|
||||
|
||||
function fast_volume(triangles; scale=1)
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using Test
|
||||
|
||||
using stlProcess
|
||||
import stlProcess.Properties
|
||||
|
||||
using FileIO
|
||||
using MeshIO
|
||||
@@ -45,37 +46,41 @@ function _check_volume(triangles; scale=1)
|
||||
return volume
|
||||
end
|
||||
|
||||
@testset "simple cube stl" begin
|
||||
"""
|
||||
inertia math:
|
||||
https://hepweb.ucsd.edu/ph110b/110b_notes/node26.html
|
||||
@testset "3D Models" begin
|
||||
I_mat = Matrix(I, 3, 3)
|
||||
center = [0, 0, 0]
|
||||
|
||||
Cube is a standard cube with a length of 2 for each side.
|
||||
"""
|
||||
cube_path = raw"test_assets\cubeblender.stl"
|
||||
stl = load(cube_path)
|
||||
models = Dict(
|
||||
# Inertia math: https://en.wikipedia.org/wiki/List_of_moments_of_inertia#List_of_3D_inertia_tensors
|
||||
# Properties(volume, center_of_gravity, inertia)
|
||||
"cube.stl" => Properties(2.0^3, center, I_mat .* 2^2 / 6), # l = 2
|
||||
"sphere.stl" => Properties(4 / 3 * pi, center, I_mat .* 2 / 5), # r = 1
|
||||
"2_4_8_cuboid.stl" => Properties(2 * 4 * 8, center, diagm([2^2 + 4^2, 8^2 + 2^2, 8^2 + 4^2]) ./ 12), # h, d, w = 2, 4, 8
|
||||
"slender_y.stl" => Properties(10 * π * 0.1^2, center, diagm([1, 0, 1]) .* (10^2 / 12)), # 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])), # l_z = 10, r = 1
|
||||
)
|
||||
@testset "$model" for (model, control) in models
|
||||
path = "test_assets\\$model"
|
||||
stl = load(path)
|
||||
|
||||
@testset "get_mass_properties function" begin
|
||||
props = get_mass_properties(stl)
|
||||
@testset "get_mass_properties" begin
|
||||
props = get_mass_properties(stl)
|
||||
|
||||
@test props.volume == 8.0
|
||||
@test all(props.center_of_gravity .== 0.0)
|
||||
@test all(eigvals(props.inertia) .≈ (5.0 + 1 / 3))
|
||||
|
||||
@testset "get_volume function" begin
|
||||
@test _check_volume(stl; scale=1) ≈ props.volume rtol = 0.01
|
||||
@test _check_volume(stl; scale=4) ≈ (4 * 2)^3 rtol = 0.01
|
||||
@test props.volume ≈ control.volume atol = 0.5
|
||||
@test props.center_of_gravity ≈ control.center_of_gravity atol = 0.01
|
||||
@test eigvals(props.inertia) ≈ eigvals(control.inertia) atol = 0.1
|
||||
end
|
||||
end
|
||||
@testset "Compare volumes with scaling" begin
|
||||
for scale in 1:5
|
||||
props = get_mass_properties(stl; scale=scale)
|
||||
volume = _check_volume(stl; scale=scale)
|
||||
|
||||
@test props.volume ≈ volume rtol = 0.01
|
||||
@testset "Compare volumes with scaling" begin
|
||||
for scale in 1:5
|
||||
props = get_mass_properties(stl; scale=scale)
|
||||
volume = _check_volume(stl; scale=scale)
|
||||
@test props.volume ≈ volume atol = 0.1
|
||||
end
|
||||
end
|
||||
@testset "find_volume" begin
|
||||
for scale in [0.5, 1, 1.0, 10, 2 / 3]
|
||||
@test fast_volume(stl; scale=find_scale(stl; desired_volume=scale)) ≈ scale
|
||||
end
|
||||
end
|
||||
end
|
||||
@testset "Find Scale" begin
|
||||
@test fast_volume(stl; scale=find_scale(stl)) == 1.0
|
||||
end
|
||||
end
|
BIN
test/test_assets/2_4_8_cuboid.stl
Normal file
BIN
test/test_assets/2_4_8_cuboid.stl
Normal file
Binary file not shown.
27
test/test_assets/README.md
Normal file
27
test/test_assets/README.md
Normal file
@@ -0,0 +1,27 @@
|
||||
# Test Assets
|
||||
|
||||
## cube.stl
|
||||
|
||||
Perfect cube with length of 2 for all sides and a center of gravity at origin.
|
||||
|
||||
## 2_4_8_cuboid.stl
|
||||
|
||||
Cube with dimensions:
|
||||
|
||||
- height = 2
|
||||
- depth = 4
|
||||
- width = 8
|
||||
|
||||
Center of gravity at origin.
|
||||
|
||||
## sphere.stl
|
||||
|
||||
Perfect sphere with radius of 1 and a center of gravity at origin.
|
||||
|
||||
## slender_rod.stl
|
||||
|
||||
Negligible thickness cylinder with length of 10, radius of 0.1, and a center of gravity at origin.
|
||||
|
||||
## cylinder.stl
|
||||
|
||||
Cylinder with length of 10, radius of 1, and a center of gravity at origin.
|
BIN
test/test_assets/cylinder.stl
Normal file
BIN
test/test_assets/cylinder.stl
Normal file
Binary file not shown.
BIN
test/test_assets/slender_y.stl
Normal file
BIN
test/test_assets/slender_y.stl
Normal file
Binary file not shown.
BIN
test/test_assets/sphere.stl
Normal file
BIN
test/test_assets/sphere.stl
Normal file
Binary file not shown.
Reference in New Issue
Block a user