Remove simplified geometry cache

This commit is contained in:
Simon Seyock
2019-09-24 16:55:33 +02:00
parent acac7a9403
commit 6866f06375
3 changed files with 34 additions and 54 deletions

View File

@@ -45,12 +45,6 @@ class Geometry extends BaseObject {
*/ */
this.extentRevision_ = -1; this.extentRevision_ = -1;
/**
* @protected
* @type {Object<string, Geometry>}
*/
this.simplifiedGeometryCache = {};
/** /**
* @protected * @protected
* @type {number} * @type {number}

View File

@@ -6,7 +6,6 @@ import EventType from '../events/EventType.js';
import {createOrUpdateEmpty, closestSquaredDistanceXY, extend, getCenter} from '../extent.js'; import {createOrUpdateEmpty, closestSquaredDistanceXY, extend, getCenter} from '../extent.js';
import Geometry from './Geometry.js'; import Geometry from './Geometry.js';
import GeometryType from './GeometryType.js'; import GeometryType from './GeometryType.js';
import {clear} from '../obj.js';
/** /**
* @classdesc * @classdesc
@@ -131,8 +130,7 @@ class GeometryCollection extends Geometry {
* @inheritDoc * @inheritDoc
*/ */
getSimplifiedGeometry(squaredTolerance) { getSimplifiedGeometry(squaredTolerance) {
if (this.simplifiedGeometryRevision != this.getRevision()) { if (this.simplifiedGeometryRevision !== this.getRevision()) {
clear(this.simplifiedGeometryCache);
this.simplifiedGeometryMaxMinSquaredTolerance = 0; this.simplifiedGeometryMaxMinSquaredTolerance = 0;
this.simplifiedGeometryRevision = this.getRevision(); this.simplifiedGeometryRevision = this.getRevision();
} }
@@ -141,30 +139,25 @@ class GeometryCollection extends Geometry {
squaredTolerance < this.simplifiedGeometryMaxMinSquaredTolerance)) { squaredTolerance < this.simplifiedGeometryMaxMinSquaredTolerance)) {
return this; return this;
} }
const key = squaredTolerance.toString();
if (this.simplifiedGeometryCache.hasOwnProperty(key)) { const simplifiedGeometries = [];
return this.simplifiedGeometryCache[key]; const geometries = this.geometries_;
let simplified = false;
for (let i = 0, ii = geometries.length; i < ii; ++i) {
const geometry = geometries[i];
const simplifiedGeometry = geometry.getSimplifiedGeometry(squaredTolerance);
simplifiedGeometries.push(simplifiedGeometry);
if (simplifiedGeometry !== geometry) {
simplified = true;
}
}
if (simplified) {
const simplifiedGeometryCollection = new GeometryCollection(null);
simplifiedGeometryCollection.setGeometriesArray(simplifiedGeometries);
return simplifiedGeometryCollection;
} else { } else {
const simplifiedGeometries = []; this.simplifiedGeometryMaxMinSquaredTolerance = squaredTolerance;
const geometries = this.geometries_; return this;
let simplified = false;
for (let i = 0, ii = geometries.length; i < ii; ++i) {
const geometry = geometries[i];
const simplifiedGeometry = geometry.getSimplifiedGeometry(squaredTolerance);
simplifiedGeometries.push(simplifiedGeometry);
if (simplifiedGeometry !== geometry) {
simplified = true;
}
}
if (simplified) {
const simplifiedGeometryCollection = new GeometryCollection(null);
simplifiedGeometryCollection.setGeometriesArray(simplifiedGeometries);
this.simplifiedGeometryCache[key] = simplifiedGeometryCollection;
return simplifiedGeometryCollection;
} else {
this.simplifiedGeometryMaxMinSquaredTolerance = squaredTolerance;
return this;
}
} }
} }

View File

@@ -6,7 +6,6 @@ import {createOrUpdateFromFlatCoordinates, getCenter} from '../extent.js';
import Geometry from './Geometry.js'; import Geometry from './Geometry.js';
import GeometryLayout from './GeometryLayout.js'; import GeometryLayout from './GeometryLayout.js';
import {rotate, scale, translate, transform2D} from './flat/transform.js'; import {rotate, scale, translate, transform2D} from './flat/transform.js';
import {clear} from '../obj.js';
/** /**
* @classdesc * @classdesc
@@ -95,8 +94,7 @@ class SimpleGeometry extends Geometry {
* @inheritDoc * @inheritDoc
*/ */
getSimplifiedGeometry(squaredTolerance) { getSimplifiedGeometry(squaredTolerance) {
if (this.simplifiedGeometryRevision != this.getRevision()) { if (this.simplifiedGeometryRevision !== this.getRevision()) {
clear(this.simplifiedGeometryCache);
this.simplifiedGeometryMaxMinSquaredTolerance = 0; this.simplifiedGeometryMaxMinSquaredTolerance = 0;
this.simplifiedGeometryRevision = this.getRevision(); this.simplifiedGeometryRevision = this.getRevision();
} }
@@ -107,26 +105,21 @@ class SimpleGeometry extends Geometry {
squaredTolerance <= this.simplifiedGeometryMaxMinSquaredTolerance)) { squaredTolerance <= this.simplifiedGeometryMaxMinSquaredTolerance)) {
return this; return this;
} }
const key = squaredTolerance.toString();
if (this.simplifiedGeometryCache.hasOwnProperty(key)) { const simplifiedGeometry =
return this.simplifiedGeometryCache[key]; this.getSimplifiedGeometryInternal(squaredTolerance);
const simplifiedFlatCoordinates = simplifiedGeometry.getFlatCoordinates();
if (simplifiedFlatCoordinates.length < this.flatCoordinates.length) {
return simplifiedGeometry;
} else { } else {
const simplifiedGeometry = // Simplification did not actually remove any coordinates. We now know
this.getSimplifiedGeometryInternal(squaredTolerance); // that any calls to getSimplifiedGeometry with a squaredTolerance less
const simplifiedFlatCoordinates = simplifiedGeometry.getFlatCoordinates(); // than or equal to the current squaredTolerance will also not have any
if (simplifiedFlatCoordinates.length < this.flatCoordinates.length) { // effect. This allows us to short circuit simplification (saving CPU
this.simplifiedGeometryCache[key] = simplifiedGeometry; // cycles) and prevents the cache of simplified geometries from filling
return simplifiedGeometry; // up with useless identical copies of this geometry (saving memory).
} else { this.simplifiedGeometryMaxMinSquaredTolerance = squaredTolerance;
// Simplification did not actually remove any coordinates. We now know return this;
// 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;
}
} }
} }