/
SimpleGeometry.js
348 lines (323 loc) · 9.01 KB
/
SimpleGeometry.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* @module ol/geom/SimpleGeometry
*/
import Geometry from './Geometry.js';
import {abstract} from '../util.js';
import {createOrUpdateFromFlatCoordinates, getCenter} from '../extent.js';
import {rotate, scale, transform2D, translate} from './flat/transform.js';
/**
* @classdesc
* Abstract base class; only used for creating subclasses; do not instantiate
* in apps, as cannot be rendered.
*
* @abstract
* @api
*/
class SimpleGeometry extends Geometry {
constructor() {
super();
/**
* @protected
* @type {import("./Geometry.js").GeometryLayout}
*/
this.layout = 'XY';
/**
* @protected
* @type {number}
*/
this.stride = 2;
/**
* @protected
* @type {Array<number>}
*/
this.flatCoordinates;
}
/**
* @param {import("../extent.js").Extent} extent Extent.
* @protected
* @return {import("../extent.js").Extent} extent Extent.
*/
computeExtent(extent) {
return createOrUpdateFromFlatCoordinates(
this.flatCoordinates,
0,
this.flatCoordinates.length,
this.stride,
extent,
);
}
/**
* @abstract
* @return {Array<*> | null} Coordinates.
*/
getCoordinates() {
return abstract();
}
/**
* Return the first coordinate of the geometry.
* @return {import("../coordinate.js").Coordinate} First coordinate.
* @api
*/
getFirstCoordinate() {
return this.flatCoordinates.slice(0, this.stride);
}
/**
* @return {Array<number>} Flat coordinates.
*/
getFlatCoordinates() {
return this.flatCoordinates;
}
/**
* Return the last coordinate of the geometry.
* @return {import("../coordinate.js").Coordinate} Last point.
* @api
*/
getLastCoordinate() {
return this.flatCoordinates.slice(
this.flatCoordinates.length - this.stride,
);
}
/**
* Return the {@link import("./Geometry.js").GeometryLayout layout} of the geometry.
* @return {import("./Geometry.js").GeometryLayout} Layout.
* @api
*/
getLayout() {
return this.layout;
}
/**
* Create a simplified version of this geometry using the Douglas Peucker algorithm.
* @param {number} squaredTolerance Squared tolerance.
* @return {SimpleGeometry} Simplified geometry.
*/
getSimplifiedGeometry(squaredTolerance) {
if (this.simplifiedGeometryRevision !== this.getRevision()) {
this.simplifiedGeometryMaxMinSquaredTolerance = 0;
this.simplifiedGeometryRevision = this.getRevision();
}
// If squaredTolerance is negative or if we know that simplification will not
// have any effect then just return this.
if (
squaredTolerance < 0 ||
(this.simplifiedGeometryMaxMinSquaredTolerance !== 0 &&
squaredTolerance <= this.simplifiedGeometryMaxMinSquaredTolerance)
) {
return this;
}
const simplifiedGeometry =
this.getSimplifiedGeometryInternal(squaredTolerance);
const simplifiedFlatCoordinates = simplifiedGeometry.getFlatCoordinates();
if (simplifiedFlatCoordinates.length < this.flatCoordinates.length) {
return simplifiedGeometry;
}
// Simplification did not actually remove any coordinates. We now know
// that any calls to getSimplifiedGeometry with a squaredTolerance less
// than or equal to the current squaredTolerance will also not have any
// effect. This allows us to short circuit simplification (saving CPU
// cycles) and prevents the cache of simplified geometries from filling
// up with useless identical copies of this geometry (saving memory).
this.simplifiedGeometryMaxMinSquaredTolerance = squaredTolerance;
return this;
}
/**
* @param {number} squaredTolerance Squared tolerance.
* @return {SimpleGeometry} Simplified geometry.
* @protected
*/
getSimplifiedGeometryInternal(squaredTolerance) {
return this;
}
/**
* @return {number} Stride.
*/
getStride() {
return this.stride;
}
/**
* @param {import("./Geometry.js").GeometryLayout} layout Layout.
* @param {Array<number>} flatCoordinates Flat coordinates.
*/
setFlatCoordinates(layout, flatCoordinates) {
this.stride = getStrideForLayout(layout);
this.layout = layout;
this.flatCoordinates = flatCoordinates;
}
/**
* @abstract
* @param {!Array<*>} coordinates Coordinates.
* @param {import("./Geometry.js").GeometryLayout} [layout] Layout.
*/
setCoordinates(coordinates, layout) {
abstract();
}
/**
* @param {import("./Geometry.js").GeometryLayout|undefined} layout Layout.
* @param {Array<*>} coordinates Coordinates.
* @param {number} nesting Nesting.
* @protected
*/
setLayout(layout, coordinates, nesting) {
let stride;
if (layout) {
stride = getStrideForLayout(layout);
} else {
for (let i = 0; i < nesting; ++i) {
if (coordinates.length === 0) {
this.layout = 'XY';
this.stride = 2;
return;
}
coordinates = /** @type {Array<unknown>} */ (coordinates[0]);
}
stride = coordinates.length;
layout = getLayoutForStride(stride);
}
this.layout = layout;
this.stride = stride;
}
/**
* Apply a transform function to the coordinates of the geometry.
* The geometry is modified in place.
* If you do not want the geometry modified in place, first `clone()` it and
* then use this function on the clone.
* @param {import("../proj.js").TransformFunction} transformFn Transform function.
* Called with a flat array of geometry coordinates.
* @api
*/
applyTransform(transformFn) {
if (this.flatCoordinates) {
transformFn(this.flatCoordinates, this.flatCoordinates, this.stride);
this.changed();
}
}
/**
* Rotate the geometry around a given coordinate. This modifies the geometry
* coordinates in place.
* @param {number} angle Rotation angle in counter-clockwise radians.
* @param {import("../coordinate.js").Coordinate} anchor The rotation center.
* @api
*/
rotate(angle, anchor) {
const flatCoordinates = this.getFlatCoordinates();
if (flatCoordinates) {
const stride = this.getStride();
rotate(
flatCoordinates,
0,
flatCoordinates.length,
stride,
angle,
anchor,
flatCoordinates,
);
this.changed();
}
}
/**
* Scale the geometry (with an optional origin). This modifies the geometry
* coordinates in place.
* @param {number} sx The scaling factor in the x-direction.
* @param {number} [sy] The scaling factor in the y-direction (defaults to sx).
* @param {import("../coordinate.js").Coordinate} [anchor] The scale origin (defaults to the center
* of the geometry extent).
* @api
*/
scale(sx, sy, anchor) {
if (sy === undefined) {
sy = sx;
}
if (!anchor) {
anchor = getCenter(this.getExtent());
}
const flatCoordinates = this.getFlatCoordinates();
if (flatCoordinates) {
const stride = this.getStride();
scale(
flatCoordinates,
0,
flatCoordinates.length,
stride,
sx,
sy,
anchor,
flatCoordinates,
);
this.changed();
}
}
/**
* Translate the geometry. This modifies the geometry coordinates in place. If
* instead you want a new geometry, first `clone()` this geometry.
* @param {number} deltaX Delta X.
* @param {number} deltaY Delta Y.
* @api
*/
translate(deltaX, deltaY) {
const flatCoordinates = this.getFlatCoordinates();
if (flatCoordinates) {
const stride = this.getStride();
translate(
flatCoordinates,
0,
flatCoordinates.length,
stride,
deltaX,
deltaY,
flatCoordinates,
);
this.changed();
}
}
}
/**
* @param {number} stride Stride.
* @return {import("./Geometry.js").GeometryLayout} layout Layout.
*/
export function getLayoutForStride(stride) {
let layout;
if (stride == 2) {
layout = 'XY';
} else if (stride == 3) {
layout = 'XYZ';
} else if (stride == 4) {
layout = 'XYZM';
}
return /** @type {import("./Geometry.js").GeometryLayout} */ (layout);
}
/**
* @param {import("./Geometry.js").GeometryLayout} layout Layout.
* @return {number} Stride.
*/
export function getStrideForLayout(layout) {
let stride;
if (layout == 'XY') {
stride = 2;
} else if (layout == 'XYZ' || layout == 'XYM') {
stride = 3;
} else if (layout == 'XYZM') {
stride = 4;
}
return /** @type {number} */ (stride);
}
/**
* @param {SimpleGeometry} simpleGeometry Simple geometry.
* @param {import("../transform.js").Transform} transform Transform.
* @param {Array<number>} [dest] Destination.
* @return {Array<number>} Transformed flat coordinates.
*/
export function transformGeom2D(simpleGeometry, transform, dest) {
const flatCoordinates = simpleGeometry.getFlatCoordinates();
if (!flatCoordinates) {
return null;
}
const stride = simpleGeometry.getStride();
return transform2D(
flatCoordinates,
0,
flatCoordinates.length,
stride,
transform,
dest,
);
}
export default SimpleGeometry;