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 5e3feee
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 8 deletions.
5 changes: 4 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
name = "Inti"
uuid = "fb74042b-437e-4c5b-88cf-d4e2beb394d5"
authors = ["Luiz M. Faria"]
version = "0.1.0"

[deps]
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat]
StaticArrays = "1"
julia = "1.6"
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)

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

View check run for this annotation

Codecov / codecov/patch

src/Geometry/referenceshapes.jl#L20

Added line #L20 was not covered by tests
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#L22

Added line #L22 was 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)

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

View check run for this annotation

Codecov / codecov/patch

src/Geometry/referenceshapes.jl#L33

Added line #L33 was not covered by tests
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#L35

Added line #L35 was 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)

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

View check run for this annotation

Codecov / codecov/patch

src/Geometry/referenceshapes.jl#L49

Added line #L49 was not covered by tests
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#L51

Added line #L51 was 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}
8 changes: 7 additions & 1 deletion src/Inti.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module Inti

# Write your package code here.
const PROJECT_ROOT = pkgdir(Inti)

using StaticArrays

include("utils.jl")
include("Geometry/Geometry.jl")


end
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
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[deps]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
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
13 changes: 7 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Inti
using Test
using SafeTestsets
using Aqua

@testset "Inti.jl" begin
@testset "Code quality (Aqua.jl)" begin
Aqua.test_all(Inti)
end
# Write your tests here.
@safetestset "Code quality" begin
include("aqua_test.jl")
end

@safetestset "Geometry" begin
@safetestset "Reference shapes" include("Geometry/referenceshapes_test.jl")
end

0 comments on commit 5e3feee

Please sign in to comment.