Examples
We'll use a few of the functions used for testing the package to demonstrate its usage. These are
simplices_sharing_vertices
. Generate a set of simplices which intersect in some arbitrary way, but sharing at least one vertex.nontrivially_intersecting_simplices
. Generate a set of non-trivially intersecting simplices (i.e. intersections are not only along boundaries or vertices).childsimplex(parentsimplex)
. Generate a simplex completely contained withinparentsimplex
.
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.068928 0.586851 0.205428 0.437983; 0.579088 0.192285 0.475006 0.113635; 0.194057 0.924657 0.382683 0.168654], [0.068928 0.271162 0.441062 0.306014; 0.579088 0.358447 0.839441 0.34442; 0.194057 0.279263 0.551103 0.36304])
julia> simplexintersection(s₁, s₂)
1.7569113600575637e-6
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.668376 0.980081 0.973242 0.900612; 0.843098 0.49178 0.103666 0.763074; 0.0277847 0.638673 0.185377 0.220356], [0.851447 1.535 1.54906 1.56646; 0.545218 1.3887 1.44313 1.28597; 0.219763 0.286418 0.337318 0.22876])
julia> simplexintersection(s₁, s₂)
2.7458435693596947e-6
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.0164994 0.0164994
0.000472644 0.000472644
2.58162e-5 2.58162e-5
3.48014e-8 3.48014e-8
1.11668e-7 1.11668e-7
5.00842e-9 5.00842e-9
0.0 4.13054e-11
0.0 1.08864e-12
0.0 3.45155e-12
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