Intersection
Intersections are implemented for various geometries and domains with the ∩ (\cap) operator:
using Meshes
s1 = Segment((0.0,0.0), (1.0,0.0))
s2 = Segment((0.5,0.0), (2.0,0.0))
s1 ∩ s2
Segment
├─ Point(x: 0.5 m, y: 0.0 m)
└─ Point(x: 1.0 m, y: 0.0 m)
First, the intersection function computes the Intersection object, which holds the IntersectionType besides the actual geometry:
I = intersection(s1, s2)
Intersection{Segment{𝔼{2}, Cartesian2D{NoDatum, Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}}}(Overlapping, Segment((x: 0.5 m, y: 0.0 m), (x: 1.0 m, y: 0.0 m)))
This object supports two methods type and get to retrieve the underlying information:
type(I)
Overlapping::IntersectionType = 6
get(I)
Segment
├─ Point(x: 0.5 m, y: 0.0 m)
└─ Point(x: 1.0 m, y: 0.0 m)
For performance-sensitive code, it is recommended to use the intersection method with three arguments, including a function to reduce the number of output types.
In the example below, we use the do syntax to restrict our attention to a subset of intersection types and to make the return type and Int value in all cases:
intersection(s1, s2) do I
if type(I) == Crossing
return 1
elseif type(I) == Overlapping
return 2
else
return 3
end
end
2
#
Meshes.IntersectionType — Type
IntersectionType
The different types of intersection that may occur between geometries. Type IntersectionType in a Julia session to see the full list.
#
Meshes.Intersection — Type
Intersection{G}
An intersection between geometries holding a geometry of type G.
#
Meshes.intersection — Function
intersection([f], g₁, g₂)
Compute the intersection of two geometries or domains g₁ and g₂ and apply function f to it. Default function is identity.
Examples
intersection(g₁, g₂) do I
if I isa CrossingLines
# do something
else
# do nothing
end
end
Notes
When a custom function f is used that reduces the number of return types, Julia is able to optimize the branches of the code and generate specialized code. This is not the case when f === identity.
#
Base.intersect — Method
g₁ ∩ g₂
Return the intersection of two geometries or domains g₁ and g₂ as a new (multi-)geometry.