Examples

Examples

We'll use a few of the functions used for testing the package to demonstrate its usage. These are

  1. simplices_sharing_vertices. Generate a set of simplices which intersect in some arbitrary way, but sharing at least one vertex.

  2. nontrivially_intersecting_simplices. Generate a set of non-trivially intersecting simplices (i.e. intersections are not only along boundaries or vertices).

  3. childsimplex(parentsimplex). Generate a simplex completely contained within parentsimplex.

Note that these functions take as inputs simplices of shape (dim + 1, dim). This will be fixed in a future release.

In the examples, some of the functions used to generate simplices return the simplex arrays with rows as columns. Therefore, we transpose the simplices before calling simplexintersection.

Simplices sharing vertices

Let's compute the intersection between a set of simplices that share at least one vertex.

julia> using Simplices

julia> s₁, s₂ = transpose.(simplices_sharing_vertices(3))
([0.0358361 0.778702 0.646206 0.202595; 0.96785 0.710035 0.247526 0.253073; 0.895915 0.933644 0.541463 0.501429], [0.778702 0.0358361 0.778702 0.528614; 0.710035 0.96785 0.710035 1.4648; 0.933644 0.895915 0.933644 1.59998])


julia> simplexintersection(s₁, s₂)
0

Nontrivially intersecting simplices

Simplices can also intersect in nontrivial ways, meaning that they have an intersection beyond a common boundary or vertex.

julia> using Simplices

julia> s₁, s₂ = transpose.(nontrivially_intersecting_simplices(3))
([0.687484 0.44752 0.574625 0.964968; 0.125207 0.0252662 0.645323 0.145588; 0.264476 0.487514 0.0796168 0.234394], [0.694489 1.40604 1.41439 1.34623; 0.282156 0.494101 0.269286 0.228796; 0.240769 0.450822 0.557408 0.606451])

julia> simplexintersection(s₁, s₂)
3.996549424093742e-8

One simplex fully contained within the other

We'll generate a random simplex s₁, then generate a simplex s₂ fully contained within that simplex. If s₂ is fully contained, the intersection volume should be the volume of s₂.

julia> using Simplices


julia> Ds =  2:10;

julia> intersection_vols = zeros(Float64, length(Ds));

julia> analytical_vols   = zeros(Float64, length(Ds));

julia> for i = 1:length(Ds)
           s₁ = rand(Ds[i] + 1, Ds[i])
           s₂ = childsimplex(s₁)
           intersection_vols[i] = simplexintersection(s₁.', s₂.')
           analytical_vols[i] = volume(s₂)
       end

julia> hcat(intersection_vols, analytical_vols)
9×2 Array{Float64,2}:
 0.00641567   0.00641567
 0.000243363  0.000243363
 2.3474e-5    2.3474e-5
 2.02239e-5   2.02239e-5
 5.00277e-9   5.00277e-9
 1.86901e-10  1.86901e-10
 1.06558e-9   1.06558e-9
 0.0          6.7276e-11
 0.0          1.85662e-13



julia> # Within numerical error, the results should be the same.
       
       all([isapprox(intersection_vols[i], analytical_vols[i];
           atol = 1e-9) for i = 1:length(Ds)])
true

Simplices are identical

If simplices are identical, the intersection volume should equal the volume of either simplex:

julia> using Simplices


julia> s₁ = rand(4, 3); s₂ = s₁;


julia> simplexintersection(s₁.', s₂.') .≈ volume(s₁) .≈ volume(s₂)
true