Skip to content

Commit

Permalink
refacto(TileMesh): refactorize bbox update method
Browse files Browse the repository at this point in the history
  • Loading branch information
mgermerie committed Dec 17, 2021
1 parent b104fd5 commit 18196b6
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/Converter/convertToTile.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default {
setTileFromTiledLayer(tile, layer);

if (parent) {
tile.setBBoxZ(parent.obb.z.min, parent.obb.z.max);
tile.setBBoxZ({ min: parent.obb.z.min, max: parent.obb.z.max });
}

return tile;
Expand Down
2 changes: 1 addition & 1 deletion src/Core/3DTiles/C3DTBoundingVolume.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class C3DTBoundingVolume {
THREE.MathUtils.radToDeg(region[3]));
const regionBox = new OBB();
regionBox.setFromExtent(extent);
regionBox.updateZ(region[4], region[5]);
regionBox.updateZ({ min: region[4], max: region[5] });
// at this point box.matrix = box.epsg4978_from_local, so
// we transform it in parent_from_local by using parent's
// epsg4978_from_local which from our point of view is
Expand Down
24 changes: 9 additions & 15 deletions src/Core/TileMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,17 @@ class TileMesh extends THREE.Mesh {
* If specified, update the min and max elevation of the OBB
* and updates accordingly the bounding sphere and the geometric error
*
* @param {?number} min
* @param {?number} max
* @param {?number} scale
* @param {Object} elevation
* @param {number} [elevation.min]
* @param {number} [elevation.max]
* @param {number} [elevation.scale]
*/
setBBoxZ(min, max, scale) {
if (min == null && max == null) {
return;
}
// update bbox if min or max have changed by at least one decimal
// or if scale changed
if (min.toFixed(1) !== this.obb.z.min.toFixed(1) || max.toFixed(1) !== this.obb.z.max.toFixed(1) || scale != this.obb.z.scale) {
this.obb.updateZ(min, max, scale);
if (this.horizonCullingPointElevationScaled) {
this.horizonCullingPointElevationScaled.setLength(this.obb.z.delta + this.horizonCullingPoint.length());
}
this.obb.box3D.getBoundingSphere(this.boundingSphere);
setBBoxZ(elevation) {
this.obb.updateZ(elevation);
if (this.horizonCullingPointElevationScaled) {
this.horizonCullingPointElevationScaled.setLength(this.obb.z.delta + this.horizonCullingPoint.length());
}
this.obb.box3D.getBoundingSphere(this.boundingSphere);
}

getExtentsByProjection(crs) {
Expand Down
4 changes: 3 additions & 1 deletion src/Layer/ElevationLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ class ElevationLayer extends RasterLayer {
node.material.addLayer(rasterElevationNode);
node.material.setSequenceElevation(this.id);
// bounding box initialisation
const updateBBox = () => node.setBBoxZ(rasterElevationNode.min, rasterElevationNode.max, this.scale);
const updateBBox = () => node.setBBoxZ({
min: rasterElevationNode.min, max: rasterElevationNode.max, scale: this.scale,
});
updateBBox();

// listen elevation updating
Expand Down
2 changes: 1 addition & 1 deletion src/Process/LayeredMaterialNodeProcessing.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export function removeLayeredMaterialNodeLayer(layerId) {
if (node.material && node.material.removeLayer) {
node.material.removeLayer(layerId);
if (node.material.elevationLayerIds[0] == layerId) {
node.setBBoxZ(0, 0);
node.setBBoxZ({ min: 0, max: 0 });
}
}
if (node.layerUpdateState && node.layerUpdateState[layerId]) {
Expand Down
28 changes: 14 additions & 14 deletions src/Renderer/OBB.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,22 @@ class OBB extends THREE.Object3D {
}

/**
* Update z min and z max of oriented bounding box
* Update z min, z max and z scale of oriented bounding box
*
* @param {number} min The minimum of oriented bounding box
* @param {number} max The maximum of oriented bounding box
* @param {number} scale
* @param {Object} [elevation={}]
* @param {number} [elevation.min] The minimum of oriented bounding box
* @param {number} [elevation.max] The maximum of oriented bounding box
* @param {number} [elevation.scale] The scale of oriented bounding box Z axis
*/
updateZ(min, max, scale = this.z.scale) {
this.z = { min, max, scale, delta: Math.abs(max - min) * scale };
this.box3D.min.z = this.natBox.min.z + min * scale;
this.box3D.max.z = this.natBox.max.z + max * scale;
}
updateZ(elevation = {}) {
this.z.min = elevation.min !== undefined && elevation.min !== null ? elevation.min : this.z.min;
this.z.max = elevation.max !== undefined && elevation.max !== null ? elevation.max : this.z.max;

updateScaleZ(scale) {
if (scale > 0) {
this.updateZ(this.z.min, this.z.max, scale);
}
this.z.scale = elevation.scale > 0 ? elevation.scale : this.z.scale;
this.z.delta = Math.abs(this.z.max - this.z.min) * this.z.scale;

this.box3D.min.z = this.natBox.min.z + this.z.min * this.z.scale;
this.box3D.max.z = this.natBox.max.z + this.z.max * this.z.scale;
}

/**
Expand Down Expand Up @@ -126,7 +126,7 @@ class OBB extends THREE.Object3D {
obb.natBox.copy(geometry.boundingBox);
this.copy(obb);

this.updateZ(minHeight, maxHeight);
this.updateZ({ min: minHeight, max: maxHeight });
this.position.copy(position);
this.quaternion.copy(quaternion);
this.updateMatrixWorld(true);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/obb.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('OBB', function () {
const o1 = new OBB(min, max);
o1.z.min = -3;
o1.z.max = 5;
o1.updateScaleZ(2);
o1.updateZ({ scale: 2 });
assert.equal(o1.z.min, -3);
assert.equal(o1.z.max, 5);
assert.equal(o1.z.scale, 2);
Expand Down

0 comments on commit 18196b6

Please sign in to comment.