forked from go-spatial/tegola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
region.go
178 lines (156 loc) · 4.26 KB
/
region.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package region
import (
"github.com/terranodo/tegola/container/singlelist/point/list"
"github.com/terranodo/tegola/maths"
)
/*
A region is made up of axises and a winding order. A region can hold other points along it's axises.
*/
type Region struct {
list.List
sentinelPoints [4]*list.Pt
winding maths.WindingOrder
// The direction of the Axis. true means it an Axis that goes from smaller to bigger, otherwise it goes from bigger point to smaller point.
aDownOrRight [4]bool
max, min maths.Pt
}
/*
Winding order:
Clockwise
1
1pt _____ 2pt
| |
0 | | 2
|_____|
0pt 3 3pt
Counter Clockwise
3
0pt _____ 3pt
| |
0 | | 2
|_____|
1pt 1 2pt
*/
// New creates a new region, initilization paramters as needed.
func New(winding maths.WindingOrder, Min, Max maths.Pt) *Region {
return new(Region).Init(winding, Min, Max)
}
// Init initilizes the region struct.
func (r *Region) Init(winding maths.WindingOrder, Min, Max maths.Pt) *Region {
//log.Println("Creating new clipping region ", Min, Max)
r.winding = winding
//r.List.Init()
r.max = Max
r.min = Min
var pts [4][2]float64
if winding == maths.Clockwise {
/*
Clockwise
MinX,MinY 1 MaxX,MinY
1pt _____ 2pt
| |
0 | | 2
|_____|
0pt 3 3pt
MinX,MaxY MaxX,MaxY
*/
pts = [4][2]float64{
[2]float64{Min.X, Max.Y},
[2]float64{Min.X, Min.Y},
[2]float64{Max.X, Min.Y},
[2]float64{Max.X, Max.Y},
}
r.aDownOrRight = [4]bool{false, true, true, false}
} else {
/*
Counter Clockwise
MinX,MinY 3 MaxX,MinY
0pt _____ 3pt
| |
0 | | 2
|_____|
1pt 1 2pt
MinX,MaxY MaxX,MaxY
*/
pts = [4][2]float64{[2]float64{Min.X, Min.Y}, [2]float64{Min.X, Max.Y}, [2]float64{Max.X, Max.Y}, [2]float64{Max.X, Min.Y}}
r.aDownOrRight = [4]bool{true, true, false, false}
}
for i, pt := range pts {
point := list.NewPoint(pt[0], pt[1])
r.sentinelPoints[i] = point
r.PushBack(point)
}
return r
}
func (r *Region) Axis(idx int) *Axis {
s, e := idx%4, (idx+1)%4
return &Axis{
region: r,
idx: s,
pt0: r.sentinelPoints[s],
pt1: r.sentinelPoints[e],
downOrRight: r.aDownOrRight[s],
winding: r.winding,
}
}
func (r *Region) FirstAxis() *Axis { return r.Axis(0) }
func (r *Region) LineString() []float64 {
return []float64{
r.sentinelPoints[0].Pt.X, r.sentinelPoints[0].Pt.Y,
r.sentinelPoints[1].Pt.X, r.sentinelPoints[1].Pt.Y,
r.sentinelPoints[2].Pt.X, r.sentinelPoints[2].Pt.Y,
r.sentinelPoints[3].Pt.X, r.sentinelPoints[3].Pt.Y,
}
}
func (r *Region) Max() maths.Pt { return r.max }
func (r *Region) Min() maths.Pt { return r.min }
func (r *Region) WindingOrder() maths.WindingOrder { return r.winding }
func (r *Region) Contains(pt maths.Pt) bool {
return r.max.X > pt.X && pt.X > r.min.X &&
r.max.Y > pt.Y && pt.Y > r.min.Y
}
func (r *Region) SentinalPoints() (pts []maths.Pt) {
for _, p := range r.sentinelPoints {
pts = append(pts, p.Point())
}
return pts
}
// Intersect holds the intersect point and the direction of the vector it's on. Into or out of the clipping region.
type Intersect struct {
// Pt is the intersect point.
Pt maths.Pt
// Is the vector this point is on heading into the region.
Inward bool
// Index of the Axis this point was found on.
Idx int
isNotZero bool
}
// Intersections returns zero to four intersections points.
// You should remove any duplicate and cancelling intersections points afterwards.
func (r *Region) Intersections(l maths.Line) (out []Intersect, Pt1Placement, Pt2Placement PlacementCode) {
pt1, pt2 := l[0], l[1]
if r.Contains(pt1) && r.Contains(pt2) {
return out, Pt1Placement, Pt2Placement
}
var ai [4]Intersect
for i := 0; i < len(ai); i++ {
a := r.Axis(i)
Pt1Placement |= a.Placement(pt1)
Pt2Placement |= a.Placement(pt2)
pt, doesIntersect := a.Intersect(l)
if !doesIntersect {
continue
}
inward, err := a.IsInward(l)
if err != nil {
continue
}
out = append(out, Intersect{
Pt: pt,
Inward: inward,
Idx: i,
isNotZero: true,
})
}
return out, Pt1Placement, Pt2Placement
}