Created
January 4, 2021 12:45
-
-
Save thw0rted/e79f07571134fe6b034099cb99772d43 to your computer and use it in GitHub Desktop.
Cesium entity bounds checking function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check some commonly-used shapes and compute a bounding rectangle that | |
// encloses all the various graphics the Entity might be showing. | |
// NB: Remember to update this function if we start using more graphics | |
export function entityBounds(ent: Entity, time: JulianDate): Cartesian3 | Rectangle | undefined { | |
// Start with all position-independent graphics types | |
const corr: Cartesian3[] = ent.corridor?.positions?.getValue(time) || []; | |
const line: Cartesian3[] = ent.polyline?.positions?.getValue(time) || []; | |
const lineVol: Cartesian3[] = ent.polylineVolume?.positions?.getValue(time) || []; | |
const poly: Cartesian3[] = ent.polygon?.hierarchy?.getValue(time)?.positions || []; | |
const wall: Cartesian3[] = ent.wall?.positions?.getValue(time) || []; | |
const all = [ | |
...corr, | |
...poly, | |
...line, | |
...lineVol, | |
...wall, | |
]; | |
// If there was a `position`, we need that | |
const pos: Cartesian3 | undefined = ent.position?.getValue(time); | |
if (pos) { all.push(pos); } | |
// The entity might also define a rectangle graphic | |
const rect: Rectangle | undefined = ent.rectangle?.coordinates?.getValue(time); | |
// This gets a little complex | |
if (rect) { | |
const first = all[0]; | |
if (all.length > 1) { | |
// If there's a rectangle graphic plus multiple points, the overall | |
// bound is the union the rectangle dimensions and a rectangle | |
// covering all points | |
const cover = Rectangle.fromCartesianArray(all); | |
return Rectangle.union(rect, cover); | |
} else if (first) { | |
// If there's exactly one point, expand the rectangle to include it | |
return Rectangle.expand(rect, Cartographic.fromCartesian(first)); | |
} else { | |
// If there were no points just use the rectangle | |
return rect; | |
} | |
} else { | |
if (all.length > 1) { | |
// If there were multiple points, return a covering rectangle | |
return Rectangle.fromCartesianArray(all); | |
} else { | |
// There was only one position (or none) and no rectangle, return that | |
return all[0]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment