Intersection
Intersections are implemented for various geometries and domains with the ∩ (\cap) operator:
Base.intersect — Method
g₁ ∩ g₂Return the intersection of g₁ and g₂.
poly₁ = Pgon([
[(8, 0), (4, 8), (2, 8), (-2, 0), (0, 0), (1, 2), (5, 2), (6, 0)],
[(4, 4), (2, 4), (3, 6)]
])
poly₂ = Pgon((0, 1), (6, 1), (6, 7), (4, 7), (4, 3), (2, 3), (2, 7), (0, 7))
poly = poly₁ ∩ poly₂
viz(poly₁)
viz!(poly₂, color = "black", alpha = 0.2)
viz!(boundary(poly), color = "red", segmentsize = 3)
Mke.current_figure()
seg₁ = Segment((0.0, 0.0), (1.0, 0.0))
seg₂ = Segment((0.5, 0.0), (2.0, 0.0))
seg₁ ∩ seg₂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(seg₁, seg₂)Intersection{Segment{𝔼{2}, CoordRefSystems.Cartesian2D{CoordRefSystems.NoDatum, Unitful.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 = 6get(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(seg₁, seg₂) do I
if type(I) == Crossing
return 1
elseif type(I) == Overlapping
return 2
else
return 3
end
end2In summary, the following functions and types are related to the ∩ operator:
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 type(I) == CrossingLines
# do something
else
# do nothing
end
endNotes
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.
Meshes.Intersection — Type
Intersection{G}An intersection between geometries holding a geometry of type G.
Meshes.IntersectionType — Type
IntersectionTypeThe different types of intersection that may occur between geometries. Type IntersectionType in a Julia session to see the full list.