Skip to content

Commit

Permalink
first implementation of reference shapes + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maltezfaria committed Sep 29, 2023
1 parent bad3773 commit b04eda9
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Geometry/Geometry.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#=
Definition of basic geometrical concepts.
=#


"""
ambient_dimension(x)
Dimension of the ambient space where `x` lives. For geometrical objects this can
differ from its [`geometric_dimension`](@ref); for example a triangle in `ℝ³`
has ambient dimension `3` but geometric dimension `2`, while a curve in `ℝ³` has
ambient dimension 3 but geometric dimension 1.
"""
function ambient_dimension end

"""
geometric_dimension(x)
Number of independent coordinates needed to describe `x`. Lines have geometric
dimension 1, triangles have geometric dimension 2, etc.
"""
function geometric_dimension end

include("referenceshapes.jl")
72 changes: 72 additions & 0 deletions src/Geometry/referenceshapes.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""
abstract type AbstractReferenceShape
A fixed reference domain/shape. Used mostly for defining more complex shapes as
transformations mapping an `AbstractReferenceShape` to some region of `ℜᴹ`.
See e.g. [`ReferenceLine`](@ref) or [`ReferenceTriangle`](@ref) for some
examples of concrete subtypes.
"""
abstract type AbstractReferenceShape end

"""
struct ReferenceTriangle
Singleton type representing the triangle with vertices `(0,0),(1,0),(0,1)`
"""
struct ReferenceTriangle <: AbstractReferenceShape end
geometric_dimension(::ReferenceTriangle) = 2
ambient_dimension(::ReferenceTriangle) = 2
vertices(::ReferenceTriangle) = SVector(0, 0), SVector(1, 0), SVector(0, 1)
Base.in(x, ::ReferenceTriangle) = 0 x[1] 1 && 0 x[2] 1 - x[1]
center(::ReferenceTriangle) = svector(i -> 1 / 3, 3)

Check warning on line 22 in src/Geometry/referenceshapes.jl

View check run for this annotation

Codecov / codecov/patch

src/Geometry/referenceshapes.jl#L18-L22

Added lines #L18 - L22 were not covered by tests

"""
struct ReferenceTetrahedron
Singleton type representing the tetrahedron with vertices
`(0,0,0),(0,0,1),(0,1,0),(1,0,0)`
"""
struct ReferenceTetrahedron <: AbstractReferenceShape end
geometric_dimension(::ReferenceTetrahedron) = 3
ambient_dimension(::ReferenceTetrahedron) = 3
vertices(::ReferenceTetrahedron) = SVector(0, 0, 0), SVector(1, 0, 0), SVector(0, 1, 0), SVector(0, 0, 1)
Base.in(x,::ReferenceTetrahedron) = 0 x[1] 1 && 0 x[2] 1 - x[1] && 0 x[3] 1 - x[1] - x[2]
center(::ReferenceTetrahedron) = svector(i -> 1 / 4, 4)

Check warning on line 35 in src/Geometry/referenceshapes.jl

View check run for this annotation

Codecov / codecov/patch

src/Geometry/referenceshapes.jl#L31-L35

Added lines #L31 - L35 were not covered by tests


# TODO: generalize structs above to `ReferenceSimplex{N}` and

"""
struct ReferenceHyperCube{N} <: AbstractReferenceShape{N}
Singleton type representing the axis-aligned hypercube in `N` dimensions with
the lower corner at the origin and the upper corner at `(1,1,…,1)`.
"""
struct ReferenceHyperCube{N} <: AbstractReferenceShape end
geometric_dimension(::ReferenceHyperCube{N}) where {N} = N
ambient_dimension(::ReferenceHyperCube{N}) where {N} = N
vertices(::ReferenceHyperCube{N}) where {N} = ntuple(i -> SVector(ntuple(j -> (i >> j) & 1, N)), 2^N)
Base.in(x, ::ReferenceHyperCube{N}) where {N} = all(0 .≤ x .≤ 1)
center(::ReferenceHyperCube{N}) where {N} = svector(i -> 0.5, N)

Check warning on line 51 in src/Geometry/referenceshapes.jl

View check run for this annotation

Codecov / codecov/patch

src/Geometry/referenceshapes.jl#L47-L51

Added lines #L47 - L51 were not covered by tests

"""
const ReferenceLine = ReferenceHyperCube{1}
Singleton type representing the `[0,1]` segment.
"""
const ReferenceLine = ReferenceHyperCube{1}

"""
const ReferenceSquare = ReferenceHyperCube{2}
Singleton type representing the unit square `[0,1]²`.
"""
const ReferenceSquare = ReferenceHyperCube{2}

"""
const ReferenceCube = ReferenceHyperCube{3}
Singleton type representing the unit cube `[0,1]³`.
"""
const ReferenceCube = ReferenceHyperCube{3}
13 changes: 13 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#=
Utility functions that have nowhere obvious to go.
=#

"""
svector(f,n)
Create an `SVector` of length n, computing each element as f(i), where i is the
index of the element.
"""
svector(f,n)= ntuple(f,n) |> SVector

Check warning on line 13 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L13

Added line #L13 was not covered by tests
37 changes: 37 additions & 0 deletions test/Geometry/referenceshapes_test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Test
import Inti
using StaticArrays

@testset "Line" begin
l = Inti.ReferenceLine()
@test Inti.ambient_dimension(l) == 1
@test Inti.geometric_dimension(l) == 1
x = SVector(0.5)
@test x l
x = SVector(1.0)
@test x l
x = SVector(1.1)
@test !in(x, l)
end
@testset "Triangle" begin
t = Inti.ReferenceTriangle()
@test Inti.ambient_dimension(t) == 2
@test Inti.geometric_dimension(t) == 2
x = SVector(0.5, 0.5)
@test x t
x = SVector(1.0, 0.0)
@test x t
x = SVector(1.1, 0.0)
@test !in(x, t)
end
@testset "Tetrahedron" begin
t = Inti.ReferenceTetrahedron()
@test Inti.ambient_dimension(t) == 3
@test Inti.geometric_dimension(t) == 3
x = SVector(0.5, 0.5, 0.0)
@test x t
x = SVector(1.0, 0.0, 0.0) # point on edge
@test x t
x = SVector(1.1, 0.0, 0.0)
@test !in(x, t)
end
7 changes: 7 additions & 0 deletions test/aqua_test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Inti
using Test
using Aqua

@testset "Aqua" begin
Aqua.test_all(Inti)
end

0 comments on commit b04eda9

Please sign in to comment.