Skip to content

Instantly share code, notes, and snippets.

@connormanning
Created December 6, 2016 16:58
Show Gist options
  • Save connormanning/a0364287180d60ffc0560f3f86b15fa4 to your computer and use it in GitHub Desktop.
Save connormanning/a0364287180d60ffc0560f3f86b15fa4 to your computer and use it in GitHub Desktop.
Greyhound bounds scale/offset
var PointCloudBounds = function(serverInfo) {
var bounds = serverInfo.bounds;
var preScale = serverInfo.scale || [1, 1, 1];
var preOffset = serverInfo.offset || [0, 0, 0];
console.log('From:', serverInfo);
if (!preScale.length) preScale = [preScale, preScale, preScale];
bounds = bounds.map((v, i) => v * preScale[i % 3] + preOffset[i % 3]);
// Default to scale of 0.01, or the highest-resolution scale available in
// the resource.
var minScale = Math.min(preScale[0], preScale[1], preScale[2], 0.01);
var effectiveScale = [minScale, minScale, minScale];
var effectiveOffset = [
bounds[0] + (bounds[3] - bounds[0]) / 2,
bounds[1] + (bounds[4] - bounds[1]) / 2,
bounds[2] + (bounds[5] - bounds[2]) / 2
];
bounds = bounds.map((v, i) => (v - effectiveOffset[i % 3]) / effectiveScale[i % 3]);
var scale = preScale.map((v, i) => 1 / v * effectiveScale[i]);
var offset = preOffset.map((v, i) => -v + effectiveOffset[i]);
this.effectiveScale = effectiveScale;
this.effectiveOffset = effectiveOffset;
this.bounds = bounds;
this.scale = scale;
this.offset = offset;
console.log('Our bounds', bounds);
console.log('To-native scale', effectiveScale);
console.log('To-native offset', effectiveOffset);
console.log('Query param scale', scale);
console.log('Query param offset', offset);
console.log('Native:', this.nativeBounds()); // This will be equal to serverInfo.nativeBounds
console.log();
};
PointCloudBounds.prototype.nativeBounds = function(b) {
if (!b) b = this.bounds;
return b.map((v, i) => v * this.effectiveScale[i % 3] + this.effectiveOffset[i % 3]);
};
var a = new PointCloudBounds({
bounds: [50, 50, 50, 150, 150, 150]
});
var b = new PointCloudBounds({
bounds: [-50, -50, -50, 50, 50, 50],
offset: [100, 100, 100]
});
var c = new PointCloudBounds({
bounds: [-5000, -5000, -5000, 5000, 5000, 5000],
offset: [100, 100, 100],
scale: 0.01
});
var d = new PointCloudBounds({
bounds: [-5000, -5000, -50000, 5000, 5000, 50000],
offset: [100, 100, 100],
scale: [0.01, 0.01, 0.001]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment