diff --git a/src/stlProcess.jl b/src/stlProcess.jl index b286539..1d036c3 100644 --- a/src/stlProcess.jl +++ b/src/stlProcess.jl @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index 17eed18..e9b9518 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 + @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 "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 - end - end - @testset "Find Scale" begin - @test fast_volume(stl; scale=find_scale(stl)) == 1.0 - end -end +end \ No newline at end of file diff --git a/test/test_assets/2_4_8_cuboid.stl b/test/test_assets/2_4_8_cuboid.stl new file mode 100644 index 0000000..f07d1da Binary files /dev/null and b/test/test_assets/2_4_8_cuboid.stl differ diff --git a/test/test_assets/README.md b/test/test_assets/README.md new file mode 100644 index 0000000..eaee96a --- /dev/null +++ b/test/test_assets/README.md @@ -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. diff --git a/test/test_assets/cubeblender.stl b/test/test_assets/cube.stl similarity index 100% rename from test/test_assets/cubeblender.stl rename to test/test_assets/cube.stl diff --git a/test/test_assets/cylinder.stl b/test/test_assets/cylinder.stl new file mode 100644 index 0000000..7a532b6 Binary files /dev/null and b/test/test_assets/cylinder.stl differ diff --git a/test/test_assets/slender_y.stl b/test/test_assets/slender_y.stl new file mode 100644 index 0000000..a755235 Binary files /dev/null and b/test/test_assets/slender_y.stl differ diff --git a/test/test_assets/sphere.stl b/test/test_assets/sphere.stl new file mode 100644 index 0000000..6af6f2b Binary files /dev/null and b/test/test_assets/sphere.stl differ