-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintersection.go
137 lines (122 loc) · 3.34 KB
/
intersection.go
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package geom
import "github.com/intdxdt/sset"
//Checks if pt intersection other geometry
func (pt Point) Intersection(other Geometry) []Point {
var res []Point
//checks for non-geometry types
if IsNullGeometry(other) {
return res
}
if other.Type().IsPoint() {
var p = CastAsPoint(other)
if pt.Equals2D(&p) {
res = append(res, pt)
}
} else if other.Type().IsLineString() {
res = pt.AsLineString().Intersection(other.(*LineString))
} else if other.Type().IsSegment() {
res = pt.AsLineString().Intersection(other.(*Segment))
} else if other.Type().IsPolygon() {
res = pt.AsLineString().Intersection(other.(*Polygon))
}
return res
}
//Segment intersection other geometry
func (self *Segment) Intersection(other Geometry) []Point {
return self.AsLineString().Intersection(other)
}
//Checks if pt intersection other geometry
func (self *LineString) Intersection(other Geometry) []Point {
var res []Point
//checks for non-geometry types
if IsNullGeometry(other) {
return res
}
if other.Type().IsPoint() {
var pt = CastAsPoint(other)
res = self.linearIntersection(pt.AsLineString())
} else if other.Type().IsSegment() {
res = self.linearIntersection(CastAsSegment(other).AsLineString())
} else if other.Type().IsLineString() {
res = self.linearIntersection(CastAsLineString(other))
} else if other.Type().IsPolygon() {
res = self.intersectionPolygonRings(CastAsPolygon(other).AsLinearRings())
}
return res
}
//Checks if pt intersection other geometry
func (self *Polygon) Intersection(other Geometry) []Point {
var res []Point
//checks for non-geometry types
if IsNullGeometry(other) {
return res
}
if other.Type().IsPoint() {
res = CastAsPoint(other).AsLineString().Intersection(self)
} else if other.Type().IsSegment() {
res = other.(*Segment).Intersection(self)
} else if other.Type().IsLineString() {
res = other.(*LineString).Intersection(self)
} else if other.Type().IsPolygon() {
var ptset = sset.NewSSet(ptCmp)
//other intersect self
var lns = other.(*Polygon).AsLinear()
for _, ln := range lns {
var pts = ln.Intersection(self)
for i := range pts {
ptset.Add(pts[i])
}
}
//self intersects other
lns = self.AsLinear()
for _, ln := range lns {
var pts = ln.Intersection(other)
for i := range pts {
ptset.Add(pts[i])
}
}
var pts = ptset.Values()
for i := range pts {
res = append(res, pts[i].(Point))
}
}
return res
}
//line intersect polygon rings
func (self *LineString) intersectionPolygonRings(rings []*LinearRing) []Point {
var res []Point
var shell = rings[0]
var ptset = sset.NewSSet(ptCmp)
var bln = self.bbox.MBR.Intersects(&shell.bbox.MBR)
if bln {
var spts = self.linearIntersection(shell.LineString)
for idx := range spts {
ptset.Add(spts[idx])
}
//inside shell, does it touch hole boundary ?
for _, hole := range rings[1:] {
var hpts = self.linearIntersection(hole.LineString)
for idx := range hpts {
ptset.Add(hpts[idx])
}
}
//check for all vertices
for idx := range self.Coordinates.Idxs {
var pt = self.Coordinates.Pt(idx)
if shell.containsPoint(pt) {
inhole := false
for i := 1; !inhole && i < len(rings); i++ {
inhole = rings[i].containsPoint(pt)
}
if !inhole {
ptset.Add(*pt)
}
}
}
vals := ptset.Values()
for _, pt := range vals {
res = append(res, pt.(Point))
}
}
return res
}