From 7b3819727360882c693cd5655d2f8dc21fdcc375 Mon Sep 17 00:00:00 2001 From: David Manthey Date: Wed, 18 May 2022 09:24:39 -0400 Subject: [PATCH] perf: Add some code paths to reduce transform calls. This reduces functions calls for webgl polygons and lines. --- CHANGELOG.md | 6 ++++++ src/transform.js | 15 +++++++++++++++ src/webgl/lineFeature.js | 10 +++++++--- src/webgl/polygonFeature.js | 19 +++++++++++-------- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eedb91839b..6ae9089dfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # GeoJS Change Log +## Version 1.8.7 + +### Improvements + +- Add some code paths to reduce transform calls ([#1207](../../pull/1207)) + ## Version 1.8.6 ### Improvements diff --git a/src/transform.js b/src/transform.js index 8dd945f7c7..de91959751 100644 --- a/src/transform.js +++ b/src/transform.js @@ -767,6 +767,21 @@ transform.vincentyDistance = function (pt1, pt2, gcs, baseGcs, ellipsoid, maxIte }; }; +/** + * Return a boolean indicating if the projections only differ in their y + * coordinate. + * + * @param {string} srcPrj The source projection. + * @param {string} tgtPrj The destination projection. + * @returns {boolean} truthy if only the y coordinate is different between + * projections. + */ +transform.onlyInvertedY = function (srcPrj, tgtPrj) { + const smatch = srcPrj.match(axisPattern), + tmatch = tgtPrj.match(axisPattern); + return smatch && tmatch && smatch[1] === tmatch[1] && smatch[3] === tmatch[3]; +}; + /* Expose proj4 to make it easier to debug */ transform.proj4 = proj4; diff --git a/src/webgl/lineFeature.js b/src/webgl/lineFeature.js index bb0708eac9..e59e0c62f6 100644 --- a/src/webgl/lineFeature.js +++ b/src/webgl/lineFeature.js @@ -134,6 +134,9 @@ var webgl_lineFeature = function (arg) { strokeOffset: 0, posStrokeOffset: 0, negStrokeOffset: 0 }], v = vert[1], + target_gcs = m_this.gcs(), + map_gcs = m_this.layer().map().gcs(), + simpleInverse = transform.onlyInvertedY(target_gcs, map_gcs), pos, posIdx3, firstpos, firstPosIdx3, lineFunc = m_this.line(), strokeWidthFunc = m_this.style.get('strokeWidth'), strokeWidthVal, @@ -188,7 +191,7 @@ var webgl_lineFeature = function (arg) { for (j = 0; j < lineItem.length; j += 1) { pos = posFunc(lineItem[j], j, d, i); position.push(pos.x); - position.push(pos.y); + position.push(simpleInverse ? -pos.y : pos.y); position.push(pos.z || 0.0); if (!j) { firstpos = pos; @@ -208,8 +211,9 @@ var webgl_lineFeature = function (arg) { } } - position = transform.transformCoordinates( - m_this.gcs(), m_this.layer().map().gcs(), position, 3); + if (!simpleInverse) { + position = transform.transformCoordinates(target_gcs, map_gcs, position, 3); + } m_origin = new Float32Array(m_this.style.get('origin')(position)); if (m_origin[0] || m_origin[1] || m_origin[2]) { for (i = 0; i < position.length; i += 3) { diff --git a/src/webgl/polygonFeature.js b/src/webgl/polygonFeature.js index de2c6e0231..8ae07ea838 100644 --- a/src/webgl/polygonFeature.js +++ b/src/webgl/polygonFeature.js @@ -82,6 +82,7 @@ var webgl_polygonFeature = function (arg) { items = [], target_gcs = m_this.gcs(), map_gcs = m_this.layer().map().gcs(), + simpleInverse = transform.onlyInvertedY(target_gcs, map_gcs), numPts = 0, geom = m_mapper.geometryData(), color, opacity, fill, d, d3, vertices, i, j, k, n, @@ -117,7 +118,7 @@ var webgl_polygonFeature = function (arg) { for (i = d3 = 0; i < outer.length; i += 1, d3 += 3) { c = posFunc(outer[i], i, item, itemIndex); geometry[d3] = c.x; - geometry[d3 + 1] = c.y; + geometry[d3 + 1] = simpleInverse ? -c.y : c.y; // ignore the z values until we support them geometry[d3 + 2] = 0; // c.z || 0; } @@ -134,7 +135,7 @@ var webgl_polygonFeature = function (arg) { for (i = 0; i < hole.length; i += 1, d3 += 3) { c = posFunc(hole[i], i, item, itemIndex); geometry.vertices[d3] = c.x; - geometry.vertices[d3 + 1] = c.y; + geometry.vertices[d3 + 1] = simpleInverse ? -c.y : c.y; // ignore the z values until we support them geometry.vertices[d3 + 2] = 0; // c.z || 0; } @@ -142,12 +143,14 @@ var webgl_polygonFeature = function (arg) { } // transform to map gcs - geometry.vertices = transform.transformCoordinates( - target_gcs, - map_gcs, - geometry.vertices, - geometry.dimensions - ); + if (!simpleInverse) { + geometry.vertices = transform.transformCoordinates( + target_gcs, + map_gcs, + geometry.vertices, + geometry.dimensions + ); + } record = { // triangulate