Skip to content

Commit

Permalink
perf: Add some code paths to reduce transform calls.
Browse files Browse the repository at this point in the history
This reduces functions calls for webgl polygons and lines.
  • Loading branch information
manthey committed May 18, 2022
1 parent 819c5c8 commit 7b38197
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 7 additions & 3 deletions src/webgl/lineFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
19 changes: 11 additions & 8 deletions src/webgl/polygonFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand All @@ -134,20 +135,22 @@ 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;
}
});
}

// 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
Expand Down

0 comments on commit 7b38197

Please sign in to comment.