Skip to content
This repository was archived by the owner on Nov 22, 2023. It is now read-only.

add area and volume calculations #144

Merged
merged 9 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 additions & 3 deletions src/algorithms.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
"""
The unnormalized normal of three vertices.
"""
function orthogonal_vector( v1, v2, v3 )
a = v2 - v1
b = v3 - v1
cross(a, b)
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't call this normal, as the actual normal is the normalized version of this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too much of a pun, if I name it unnormal?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

edge_cross_product maybe? Personally I'd probably just keep it as a local function inside the other ones. Even though you'd have the same code twice then...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re: Silly names. What about normal_times_twice_the_area? 🤓

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe something like orthogonal_vector?

"""
```
normals{VT,FD,FT,FO}(vertices::Vector{Point{3, VT}},
Expand All @@ -16,9 +25,7 @@ function normals(
for face in faces
v = vertices[face]
# we can get away with two edges since faces are planar.
a = v[2] - v[1]
b = v[3] - v[1]
n = cross(a, b)
n = orthogonal_vector(v[1], v[2], v[3])
for i =1:length(F)
fi = face[i]
normals_result[fi] = normals_result[fi] + n
Expand All @@ -28,6 +35,48 @@ function normals(
normals_result
end

"""
Calculate the area of one triangle.
"""
function area(
vertices::AbstractVector{Point{3, VT}},
face::Face{3,FT}
) where {VT,FT}
v1, v2, v3 = vertices[face]
return 0.5norm( orthogonal_vector(v1, v2, v3) )
end

"""
Calculate the area of all triangles.
"""
function area(
vertices::AbstractVector{Point{3, VT}},
faces::AbstractVector{Face{3,FT}}
) where {VT,FT}
return sum( x->area( vertices, x ), faces )
end

"""
Calculate the signed volume of one tetrahedron. Be sure the orientation of your surface is right.
"""
function volume(
vertices::AbstractVector{Point{3, VT}},
face::Face{3,FT}
) where {VT,FT}
v1, v2, v3 = vertices[face]
sig = sign( orthogonal_vector( v1, v2, v3 ) ⋅ v1 )
return sig * abs( v1 ⋅ ( v2 × v3 ) ) / 6
end

"""
Calculate the signed volume of all tetrahedra. Be sure the orientation of your surface is right.
"""
function volume(
vertices::AbstractVector{Point{3, VT}},
faces::AbstractVector{Face{3,FT}}
) where {VT,FT}
return sum( x->volume( vertices, x), faces )
end

"""
Slice an AbstractMesh at the specified Z axis value.
Expand Down
16 changes: 16 additions & 0 deletions test/algorithms.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Test, GeometryTypes

@testset "algorithms.jl" begin
cube = HyperRectangle(Vec3f0(-0.5), Vec3f0(1))
cube_faces = decompose(Face{3,Int}, Face{4, Int}[
(1,3,4,2),
(2,4,8,6),
(4,3,7,8),
(1,5,7,3),
(1,2,6,5),
(5,6,8,7),
])
cube_vertices = decompose(Point{3,Float32}, cube) |> Array
@test area( cube_vertices, cube_faces ) == 6
@test volume( cube_vertices, cube_faces ) ≈ 1
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ using Test: @inferred
include("lines.jl")
include("polygons.jl")
include("cylinder.jl")
include("algorithms.jl")
end