Skip to content

Commit

Permalink
measureLenBetween consider altitude (#2406)
Browse files Browse the repository at this point in the history
* measureLenBetween  consider altitude

* spec
  • Loading branch information
deyihu authored Aug 22, 2024
1 parent d9926d6 commit cdf38b9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 13 deletions.
15 changes: 12 additions & 3 deletions src/geo/measurer/Identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ const identity = {
* @param c1
* @param c2
*/
measureLenBetween: function (c1: Coordinate | CoordinateJson, c2: Coordinate | CoordinateJson): number {
measureLenBetween: function (c1: Coordinate | CoordinateJson, c2: Coordinate | CoordinateJson, ignoreAltitude = false): number {
if (!c1 || !c2) {
return 0;
}
try {
return Math.sqrt(Math.pow(c1.x - c2.x, 2) + Math.pow(c1.y - c2.y, 2));
const distance = Math.sqrt(Math.pow(c1.x - c2.x, 2) + Math.pow(c1.y - c2.y, 2));
if (ignoreAltitude) {
return distance;
}
const z1 = c1.z || 0, z2 = c2.z || 0;
const dz = z1 - z2;
if (dz === 0) {
return distance;
}
return Math.sqrt(distance * distance + dz * dz);
} catch (err) {
return 0;
}
Expand Down Expand Up @@ -63,7 +72,7 @@ const identity = {
* @param yDist
* @param out
*/
locate : function (c: Coordinate | CoordinateJson, xDist: number, yDist: number, out?: Coordinate) {
locate: function (c: Coordinate | CoordinateJson, xDist: number, yDist: number, out?: Coordinate) {
out = out || new Coordinate(0, 0);
out.set(c.x, c.y);
return this._locate(out, xDist, yDist);
Expand Down
15 changes: 12 additions & 3 deletions src/geo/measurer/Sphere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Sphere {
* @param c1
* @param c2
*/
measureLenBetween(c1: CoordsLike, c2: CoordsLike): number {
measureLenBetween(c1: CoordsLike, c2: CoordsLike, ignoreAltitude = false): number {
if (!c1 || !c2) {
return 0;
}
Expand All @@ -42,7 +42,16 @@ class Sphere {
f = toRadian(c1.x) - toRadian(c2.x);
b = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(e / 2), 2) + Math.cos(b) * Math.cos(d) * Math.pow(Math.sin(f / 2), 2)));
b *= this.radius;
return b;
const distance = b;
if (ignoreAltitude) {
return distance;
}
const z1 = c1.z || 0, z2 = c2.z || 0;
const dz = z1 - z2;
if (dz === 0) {
return distance;
}
return Math.sqrt(distance * distance + dz * dz);
}

/**
Expand Down Expand Up @@ -159,7 +168,7 @@ class Sphere {
_rotate(c: Coordinate, pivot: Coordinate, angle: number) {
const initialAngle = rhumbBearing(pivot, c);
const finalAngle = initialAngle - angle;
const distance = this.measureLenBetween(pivot, c);
const distance = this.measureLenBetween(pivot, c, true);
c.x = pivot.x;
c.y = pivot.y;
return calculateRhumbDestination(c, distance, finalAngle, this.radius);
Expand Down
8 changes: 6 additions & 2 deletions src/layer/ImageLayer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { extend } from '../core/util';
import { extend, isNumber } from '../core/util';
import { getDepthFunc } from '../core/util/gl';
import Browser from '../core/Browser';
import Point from '../geo/Point';
Expand Down Expand Up @@ -212,7 +212,11 @@ export class ImageLayerCanvasRenderer extends CanvasRenderer {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this._painted = true;
this._drawImage(image, extent, imgData[i].opacity || 1);
let opacity = imgData[i].opacity;
if (!isNumber(opacity)) {
opacity = 1;
}
this._drawImage(image, extent, opacity);
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/geometry/GeometryAltitudeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,18 @@ describe('Geometry.Altitude', function () {
}, 100);
});

it('#1970 geometry measure length consider altitude ', function (done) {
map.addLayer(layer);
const altitude = 100;
const c1 = map.getCenter();
const c2 = c1.copy();
c2.z = altitude;
const line = new maptalks.LineString([c1, c2]).addTo(layer);
setTimeout(() => {
expect(line.getLength()).to.be.equal(altitude);
expect(map.computeGeometryLength(line)).to.be.equal(altitude);
done();
}, 100);
});

});
13 changes: 8 additions & 5 deletions test/geometry/SectorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,25 @@ describe('#Sector', function () {
});

it('getShell with altitude', function () {
var sector = new maptalks.Sector({ x: 0, y: 0, z: 100 }, 1000, 0, 90);
const radius = 1000, altitude = 100;
const len = Math.sqrt(radius * radius + altitude * altitude);
var sector = new maptalks.Sector({ x: 0, y: 0, z: altitude }, radius, 0, 90);
var shell = sector.getShell();

expect(shell).to.have.length(sector.options.numberOfShellPoints);
var num = sector.options.numberOfShellPoints;
expect(shell).to.have.length(num);
expect(map.computeLength(shell[1], [0, 0,])).to.be.approx(sector.getRadius(), 1E-5);

expect(map.computeLength(shell[1], [0, 0,])).to.be.approx(len, 1E-5);
expect(shell[1].x).to.be.above(0);
expect(shell[1].y).to.be.eql(0);

expect(map.computeLength(shell[shell.length - 2], [0, 0])).to.be.approx(sector.getRadius(), 1E-5);
expect(map.computeLength(shell[shell.length - 2], [0, 0])).to.be.approx(len, 1E-5);
expect(shell[shell.length - 2].y).to.be.above(0);
expect(shell[shell.length - 2].x).to.be.approx(0, 1E-3);

expect(shell[shell.length - 2].z).to.be.eql(100);
expect(shell[1].z).to.be.eql(100);
expect(shell[shell.length - 2].z).to.be.eql(altitude);
expect(shell[1].z).to.be.eql(altitude);
});

describe('geometry fires events', function () {
Expand Down

0 comments on commit cdf38b9

Please sign in to comment.