-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathintersections.jl
117 lines (94 loc) · 2.69 KB
/
intersections.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENSE in the project root.
# ------------------------------------------------------------------
"""
IntersectionType
The different types of intersection that may occur between geometries.
Type `IntersectionType` in a Julia session to see the full list.
"""
@enum IntersectionType begin
# crossing types
Crossing
CornerCrossing
EdgeCrossing
# touching types
Touching
CornerTouching
EdgeTouching
# overlapping types
Overlapping
PosOverlapping
NegOverlapping
# no much information
NotIntersecting
Intersecting
end
"""
Intersection{G}
An intersection between geometries holding a geometry of type `G`.
"""
struct Intersection{GeometryType}
type::IntersectionType
geom::GeometryType
end
Intersection(type, geom) = Intersection{typeof(geom)}(type, geom)
"""
type(intersection)
Return the type of intersection computed between geometries.
"""
type(I::Intersection) = I.type
"""
get(intersection)
Return the underlying geometry stored in the intersection object.
"""
Base.get(I::Intersection) = I.geom
# helper macro for developers in case we decide to
# change the internal representation of Intersection
macro IT(type, geom, func)
type = esc(type)
geom = esc(geom)
func = esc(func)
:(Intersection($type, $geom) |> $func)
end
"""
g₁ ∩ g₂
Return the intersection of two geometries or domains `g₁` and `g₂`
as a new (multi-)geometry.
"""
Base.intersect(g₁::GeometryOrDomain, g₂::GeometryOrDomain) = get(intersection(g₁, g₂))
"""
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
```julia
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`.
"""
intersection(f, g₁, g₂) = intersection(f, g₂, g₁)
intersection(g₁, g₂) = intersection(identity, g₁, g₂)
# ----------------
# IMPLEMENTATIONS
# ----------------
# order of geometries according to following convention:
# https://github.com/JuliaGeometry/Meshes.jl/issues/325
include("intersections/points.jl")
include("intersections/segments.jl")
include("intersections/rays.jl")
include("intersections/lines.jl")
include("intersections/chains.jl")
include("intersections/planes.jl")
include("intersections/boxes.jl")
include("intersections/polygons.jl")
include("intersections/domains.jl")