Skip to content

Instantly share code, notes, and snippets.

@thw0rted

thw0rted/bounds.ts

Created Jan 4, 2021
Embed
What would you like to do?
Cesium entity bounds checking function
// 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