-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutils.js
169 lines (147 loc) · 4.41 KB
/
utils.js
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
import Coordinate from 'jsts/org/locationtech/jts/geom/Coordinate'
import GeometryFactory from 'jsts/org/locationtech/jts/geom/GeometryFactory'
import IsSimpleOp from 'jsts/org/locationtech/jts/operation/IsSimpleOp'
import RelateOp from 'jsts/org/locationtech/jts/operation/relate/RelateOp'
import DistanceOp from 'jsts/org/locationtech/jts/operation/distance/DistanceOp'
import Polygonizer from 'jsts/org/locationtech/jts/operation/polygonize/Polygonizer'
import BoundaryNodeRule from 'jsts/org/locationtech/jts/algorithm/BoundaryNodeRule'
import LengthIndexedLine from 'jsts/org/locationtech/jts/linearref/LengthIndexedLine'
const factory = new GeometryFactory()
const isSimpleOp = new IsSimpleOp()
function toLineString (coordinates) {
return factory.createLineString(coordinates.map(c => new Coordinate(c[0], c[1])))
}
function toCoordinate (c) {
return new Coordinate(c[0], c[1])
}
function toPoint (c) {
return factory.createPoint(new Coordinate(c[0], c[1]))
}
function polyToCoordss (poly) {
const cs = lineStringToCoords(poly.getExteriorRing())
return [cs]
}
function lineStringToCoords (ls) {
return ls.getCoordinates().map(c => [c.x, c.y])
}
export function isSimple (coordinates) {
const lineString = toLineString(coordinates)
return isSimpleOp.isSimpleLinearGeometry(lineString)
}
export function relate (cs1, cs2) {
const ls1 = toLineString(cs1)
const ls2 = toLineString(cs2)
return RelateOp.relate(ls1, ls2, BoundaryNodeRule.ENDPOINT_BOUNDARY_RULE)
}
export function intersects (cs1, cs2) {
const ls1 = toLineString(cs1)
const ls2 = toLineString(cs2)
return RelateOp.intersects(ls1, ls2)
}
export function equals (c1, c2) {
return c1[0] === c2[0] && c1[1] === c2[1]
}
export function signedArea (shell) {
if (shell.length < 3) {
return 0
}
let sum = 0
let x
let y1
let y2
let p1 = shell[0]
let p2 = shell[1]
const x0 = p1[0]
for (let i = 2; i < shell.length; i++) {
let p3 = shell[i]
x = p2[0] - x0
y1 = p3[1]
y2 = p1[1]
sum += x * (y2 - y1)
p1 = p2
p2 = p3
}
return sum / 2
}
export function azimuth (a, b) {
let d
if (a[0] === b[0]) {
if (a[1] < b[1]) d = 0.0
else if (a[1] > b[1]) d = Math.PI
else throw new Error('same coordinate')
return d
}
if (a[1] === b[1]) {
if (a[0] < b[0]) d = Math.PI / 2
else if (a[0] > b[0]) d = Math.PI + (Math.PI / 2)
else throw new Error('same coordinate')
return d
}
if (a[0] < b[0]) {
if (a[1] < b[1]) {
d = Math.atan(Math.abs(a[0] - b[0]) / Math.abs(a[1] - b[1]))
} else {
d = Math.atan(Math.abs(a[1] - b[1]) / Math.abs(a[0] - b[0])) + (Math.PI / 2)
}
} else {
if (a[1] > b[1]) {
d = Math.atan(Math.abs(a[0] - b[0]) / Math.abs(a[1] - b[1])) + Math.PI
} else {
d = Math.atan(Math.abs(a[1] - b[1]) / Math.abs(a[0] - b[0])) + (Math.PI + (Math.PI / 2))
}
}
return d
}
export function polygonize (css) {
const lss = css.map(toLineString)
const polygonizer = new Polygonizer()
lss.forEach(ls => polygonizer.add(ls))
const polys = polygonizer.getPolygons()
if (polys.size() === 0) {
throw new Error('Could not polygonize edges - invalid topology?')
}
return polyToCoordss(polys.get(0))
}
export function split (coordinates, coordinate) {
const ls = toLineString(coordinates)
const c = toCoordinate(coordinate)
const lil = new LengthIndexedLine(ls)
const splitIndex = lil.project(c)
const ls1 = lil.extractLine(lil.getStartIndex(), splitIndex)
const ls2 = lil.extractLine(splitIndex, lil.getEndIndex())
const cs1 = lineStringToCoords(ls1)
const cs2 = lineStringToCoords(ls2)
return [cs1, cs2]
}
function isLeft (c0, c1, c2) {
return ((c1[0] - c0[0]) * (c2[1] - c0[1])) - ((c2[0] - c0[0]) * (c1[1] - c0[1]))
}
export function distance (c, cs) {
const point = toPoint(c)
const lineString = toLineString(cs)
return DistanceOp.distance(point, lineString)
}
export function pointInPoly (c, shell) {
return calcWindingNumber(c, shell) !== 0
}
export function calcWindingNumber (c, shell) {
let wn = 0
for (let i = 0; i < shell.length - 1; i++) {
const va = shell[i]
const vb = shell[i + 1]
if (va[1] <= c[1]) {
if (vb[1] > c[1]) {
const l = isLeft(va, vb, c)
if (l > 0) wn++
else if (l === 0) return 0
}
} else {
if (vb[1] <= c[1]) {
const l = isLeft(va, vb, c)
if (l < 0) wn--
else if (l === 0) return 0
}
}
}
return wn
}