grid-apps-cmms/src/geo/bounds.js

140 lines
3.4 KiB
JavaScript
Raw Normal View History

2019-01-21 10:57:11 -05:00
/** Copyright 2014-2019 Stewart Allen -- All Rights Reserved */
2017-04-18 22:22:29 -04:00
2017-04-18 17:09:33 -04:00
"use strict";
// dep: geo.base
2017-04-18 17:09:33 -04:00
(function() {
2022-01-31 10:07:06 -05:00
let base = self.base;
if (base.Bounds) return;
2017-04-18 17:09:33 -04:00
2022-01-31 10:07:06 -05:00
let { config, util } = base;
class Bounds {
constructor() {
2017-04-18 17:09:33 -04:00
this.minx = 10e7;
this.miny = 10e7;
this.maxx = -10e7;
this.maxy = -10e7;
}
2022-01-31 10:07:06 -05:00
set(minx, maxx, miny, maxy) {
this.minx = minx;
this.miny = miny;
this.maxx = maxx;
this.maxy = maxy;
return this;
}
2022-01-31 10:07:06 -05:00
clone() {
2020-06-03 22:39:30 -04:00
let b = new Bounds();
2017-04-18 17:09:33 -04:00
b.minx = this.minx;
b.miny = this.miny;
b.maxx = this.maxx;
b.maxy = this.maxy;
b.maxy = this.maxy;
2017-04-18 17:09:33 -04:00
return b;
2022-01-31 10:07:06 -05:00
}
2017-04-18 17:09:33 -04:00
2022-01-31 10:07:06 -05:00
equals(bounds, margin) {
if (!margin) margin = config.precision_offset;
return util.isCloseTo(this.minx, bounds.minx, margin) &&
util.isCloseTo(this.miny, bounds.miny, margin) &&
util.isCloseTo(this.maxx, bounds.maxx, margin) &&
util.isCloseTo(this.maxy, bounds.maxy, margin);
}
2017-04-18 17:09:33 -04:00
2021-07-10 21:19:56 -04:00
/**
* @returns {Number} absolute delta in x,y coordinate space
*/
2022-01-31 10:07:06 -05:00
delta(bounds) {
return 0 +
2021-07-10 21:19:56 -04:00
Math.abs(this.minx - bounds.minx) +
Math.abs(this.miny - bounds.miny) +
Math.abs(this.maxx - bounds.maxx) +
Math.abs(this.maxy - bounds.maxy);
2022-01-31 10:07:06 -05:00
}
2021-07-10 21:19:56 -04:00
2017-04-18 17:09:33 -04:00
/**
* @param {Bounds} b
*/
2022-01-31 10:07:06 -05:00
merge(b) {
2020-06-03 22:39:30 -04:00
this.minx = Math.min(this.minx, b.minx);
this.maxx = Math.max(this.maxx, b.maxx);
this.miny = Math.min(this.miny, b.miny);
this.maxy = Math.max(this.maxy, b.maxy);
return this;
2022-01-31 10:07:06 -05:00
}
2017-04-18 17:09:33 -04:00
/**
* @param {Point} p
*/
2022-01-31 10:07:06 -05:00
update(p) {
2020-06-03 22:39:30 -04:00
this.minx = Math.min(this.minx, p.x);
this.maxx = Math.max(this.maxx, p.x);
this.miny = Math.min(this.miny, p.y);
this.maxy = Math.max(this.maxy, p.y);
return this;
2022-01-31 10:07:06 -05:00
}
2017-04-18 17:09:33 -04:00
2022-01-31 10:07:06 -05:00
contains(bounds) {
2017-04-18 17:09:33 -04:00
return bounds.isNested(this);
2022-01-31 10:07:06 -05:00
}
2017-04-18 17:09:33 -04:00
2022-01-31 10:07:06 -05:00
containsXY(x, y) {
2017-04-18 17:09:33 -04:00
return x >= this.minx && x <= this.maxx && y >= this.miny && y <= this.maxy;
2022-01-31 10:07:06 -05:00
}
2017-04-18 17:09:33 -04:00
2022-01-31 10:07:06 -05:00
containsOffsetXY(x, y, offset) {
return x >= this.minx - offset && x <= this.maxx + offset && y >= this.miny - offset && y <= this.maxy + offset;
}
2017-04-18 17:09:33 -04:00
/**
* @param {Bounds} parent
* @returns {boolean} true if fully inside parent bounds
*/
2022-01-31 10:07:06 -05:00
isNested(parent, precision = config.precision_bounds) {
2017-04-18 17:09:33 -04:00
return (
2022-01-31 10:07:06 -05:00
this.minx >= parent.minx - precision && // min-x
this.maxx <= parent.maxx + precision && // max-x
this.miny >= parent.miny - precision && // min-y
this.maxy <= parent.maxy + precision // max-y
2017-04-18 17:09:33 -04:00
);
2022-01-31 10:07:06 -05:00
}
2017-04-18 17:09:33 -04:00
/**
* @param {Bounds} b
* @param {number} precision
* @returns {boolean}
*/
2022-01-31 10:07:06 -05:00
overlaps(b, precision = config.precision_bounds) {
2017-04-18 17:09:33 -04:00
return (
2020-06-03 22:39:30 -04:00
Math.abs(this.centerx() - b.centerx()) * 2 - precision < this.width() + b.width() &&
Math.abs(this.centery() - b.centery()) * 2 - precision < this.height() + b.height()
2017-04-18 17:09:33 -04:00
);
2022-01-31 10:07:06 -05:00
}
2017-04-18 17:09:33 -04:00
2022-01-31 10:07:06 -05:00
width() {
2017-04-18 17:09:33 -04:00
return this.maxx - this.minx;
2022-01-31 10:07:06 -05:00
}
2017-04-18 17:09:33 -04:00
2022-01-31 10:07:06 -05:00
height() {
2017-04-18 17:09:33 -04:00
return this.maxy - this.miny;
2022-01-31 10:07:06 -05:00
}
2017-04-18 17:09:33 -04:00
2022-01-31 10:07:06 -05:00
center(z) {
return base.newPoint(this.centerx(), this.centery(), z);
}
2022-01-31 10:07:06 -05:00
centerx() {
2017-04-18 17:09:33 -04:00
return this.minx + this.width() / 2;
2022-01-31 10:07:06 -05:00
}
2017-04-18 17:09:33 -04:00
2022-01-31 10:07:06 -05:00
centery() {
2017-04-18 17:09:33 -04:00
return this.miny + this.height() / 2;
2022-01-31 10:07:06 -05:00
}}
2017-04-18 17:09:33 -04:00
2022-01-31 10:07:06 -05:00
base.Bounds = Bounds;
base.newBounds = function() { return new Bounds() };
2017-04-18 17:09:33 -04:00
})();