Skip to content

Commit

Permalink
fix: Store coordinates in 'BoundingBox' as 'Cartographic' instead of …
Browse files Browse the repository at this point in the history
…'Cartesian3'
  • Loading branch information
njam committed Mar 2, 2024
1 parent 11c45ee commit e02e6ef
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 42 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions src/3dtiles/Batched3DModel.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ class Batched3DModel {
* @returns {Number[]}
*/
getRegion () {
let points = this.boundingBox.getPoints().map(point => {
return Cesium.Cartographic.fromCartesian(point)
})
let points = this.boundingBox.getPoints()
let longitudes = [points[0].longitude, points[1].longitude]
let latitudes = [points[0].latitude, points[1].latitude]
let heights = [points[0].height, points[1].height]
Expand Down
24 changes: 12 additions & 12 deletions src/Converter.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ describe('Converter', async function () {

it('should create the correct B3DM region', () => {
chai.assert.deepEqual(b3dm.getRegion(), [
0.14907335694208532,
0.8268099146975589,
0.14908126185163723,
0.8268101310946895,
372.8139593142252,
459.6138361611206,
0.14907155162642596,
0.826804434491562,
0.14908311706269073,
0.8268148017875332,
403.19936,
432.65393,
])
})

Expand Down Expand Up @@ -82,11 +82,11 @@ describe('Converter', async function () {
it('should create the correct B3DM region', () => {
chai.assert.deepEqual(b3dm.getRegion(), [
-0.8846020828104612,
1.5515922059263938,
1.5515922059263958,
-0.8845940828005763,
1.5515936762937024,
1.862645149230957e-9,
9.000000074429806,
1.551593676293702,
0,
9,
])
})

Expand Down Expand Up @@ -134,12 +134,12 @@ describe('Converter', async function () {

it('should create the correct B3DM region', () => {
chai.assert.deepEqual(b3dm.getRegion(), [
0.07601011853885128,
0.07601011853885127,
0.9075773939506161,
0.07648148496824216,
0.9078300429446181,
0,
100.00000000055493,
100,
])
})

Expand Down
29 changes: 23 additions & 6 deletions src/citygml/CityNode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ class CityNode {
}

/**
* @returns {Cesium.Cartesian3[]}
* @returns {Cesium.Cartographic[]}
*/
getTextAsCoordinates () {
getTextAsCoordinatesCartographic () {
let srs = this.getSRS()
let srsTranslator = this.document.getSRSTranslator()

Expand All @@ -140,23 +140,40 @@ class CityNode {
point = point.map(p => parseFloat(p))
point = point.map(p => isNaN(p) ? 0 : p)
point = srsTranslator.forward(point, srs, 'WGS84')
point = Cesium.Cartesian3.fromDegrees(point[0], point[1], point[2])
point = Cesium.Cartographic.fromDegrees(point[0], point[1], point[2])
points.push(point)
}
return points
}

/**
* @returns {Cesium.Cartesian3}
* @returns {Cesium.Cartesian3[]}
*/
getTextAsCoordinates1 () {
let coords = this.getTextAsCoordinates()
getTextAsCoordinatesCartesian() {
return this.getTextAsCoordinatesCartographic().map((point) => {
return Cesium.Cartographic.toCartesian(point)
})
}

/**
* @returns {Cesium.Cartographic}
*/
getTextAsCoordinates1Cartographic() {
let coords = this.getTextAsCoordinatesCartographic()
if (coords.length !== 1) {
throw new Error('Expected 1 coordinates, but found ' + coords.length)
}
return coords[0]
}

/**
* @returns {Cesium.Cartesian3}
*/
getTextAsCoordinates1Cartesian() {
let point = this.getTextAsCoordinates1Cartographic()
return Cesium.Cartographic.toCartesian(point)
}

/**
* @returns {String}
*/
Expand Down
2 changes: 1 addition & 1 deletion src/citygml/CityObject.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class CityObject {
if (posLists.length === 0) {
return null
}
let coordinates = posLists[0].getTextAsCoordinates()
let coordinates = posLists[0].getTextAsCoordinatesCartesian()
if (coordinates.length === 0) {
return null
}
Expand Down
4 changes: 2 additions & 2 deletions src/citygml/CityObject/Building.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class Building extends CityObject {
this.rings = this.cityNode.selectCityNodes('.//gml:Polygon//gml:LinearRing')
.map(ringNode => {
let pos = ringNode.selectCityNodes('./gml:pos')
let points = pos.map(n => n.getTextAsCoordinates1())
let points = pos.map(n => n.getTextAsCoordinates1Cartesian())
if (points.length === 0) {
points = ringNode.selectCityNode('./gml:posList').getTextAsCoordinates()
points = ringNode.selectCityNode('./gml:posList').getTextAsCoordinatesCartesian()
}

if (points.length < 4) {
Expand Down
4 changes: 2 additions & 2 deletions src/citygml/Envelope.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Envelope {
* @returns {BoundingBox}
*/
getBoundingBox () {
let lowerCorner = this.cityNode.selectCityNode('./gml:lowerCorner').getTextAsCoordinates1()
let upperCorner = this.cityNode.selectCityNode('./gml:upperCorner').getTextAsCoordinates1()
let lowerCorner = this.cityNode.selectCityNode('./gml:lowerCorner').getTextAsCoordinates1Cartographic()
let upperCorner = this.cityNode.selectCityNode('./gml:upperCorner').getTextAsCoordinates1Cartographic()
return new BoundingBox(lowerCorner, upperCorner)
}

Expand Down
28 changes: 14 additions & 14 deletions src/geometry/BoundingBox.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,53 @@ import Cesium from 'cesium'

class BoundingBox {
/**
* @param {Cesium.Cartesian3} min
* @param {Cesium.Cartesian3} max
* @param {Cesium.Cartographic} min
* @param {Cesium.Cartographic} max
*/
constructor (min, max) {
this.min = min
this.max = max
}

/**
* @returns {Cesium.Cartesian3[]}
* @returns {Cesium.Cartographic[]}
*/
getPoints () {
return [this.min, this.max]
}

/**
* @returns {Cesium.Cartesian3}
* @returns {Cesium.Cartographic}
*/
getMin () {
return this.min
}

/**
* @returns {Cesium.Cartesian3}
* @returns {Cesium.Cartographic}
*/
getMax () {
return this.max
}

/**
* @param {Cesium.Cartesian3[]} points
* @param {Cesium.Cartographic[]} points
* @returns {BoundingBox}
*/
static fromPoints (points) {
if (points.length < 1) {
throw new Error('Invalid number of points: ' + points.length)
}
let min = Cesium.Cartesian3.clone(points[0])
let max = Cesium.Cartesian3.clone(min)
let min = points[0].clone()
let max = min.clone()
points.forEach(point => {
min.x = Math.min(min.x, point.x)
min.y = Math.min(min.y, point.y)
min.z = Math.min(min.z, point.z)
min.longitude = Math.min(min.longitude, point.longitude)
min.latitude = Math.min(min.latitude, point.latitude)
min.height = Math.min(min.height, point.height)

max.x = Math.max(max.x, point.x)
max.y = Math.max(max.y, point.y)
max.z = Math.max(max.z, point.z)
max.longitude = Math.max(max.longitude, point.longitude)
max.latitude = Math.max(max.latitude, point.latitude)
max.height = Math.max(max.height, point.height)
})
return new BoundingBox(min, max)
}
Expand Down

0 comments on commit e02e6ef

Please sign in to comment.