molstar
Version:
A comprehensive macromolecular library.
127 lines • 5.16 kB
JavaScript
/**
* Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { Vec3 } from '../../linear-algebra';
import { OrderedSet } from '../../../mol-data/int';
import { Sphere3D } from './sphere3d';
function Box3D() {
return Box3D.zero();
}
(function (Box3D) {
function create(min, max) { return { min: min, max: max }; }
Box3D.create = create;
function zero() { return { min: Vec3(), max: Vec3() }; }
Box3D.zero = zero;
function copy(out, a) {
Vec3.copy(out.min, a.min);
Vec3.copy(out.max, a.max);
return out;
}
Box3D.copy = copy;
function clone(a) {
return copy(zero(), a);
}
Box3D.clone = clone;
/** Get box from sphere, uses extrema if available */
function fromSphere3D(out, sphere) {
if (Sphere3D.hasExtrema(sphere))
return fromVec3Array(out, sphere.extrema);
var r = Vec3.create(sphere.radius, sphere.radius, sphere.radius);
Vec3.sub(out.min, sphere.center, r);
Vec3.add(out.max, sphere.center, r);
return out;
}
Box3D.fromSphere3D = fromSphere3D;
/** Get box from sphere, uses extrema if available */
function fromVec3Array(out, array) {
Box3D.setEmpty(out);
for (var i = 0, il = array.length; i < il; i++) {
Box3D.add(out, array[i]);
}
return out;
}
Box3D.fromVec3Array = fromVec3Array;
function computeBounding(data) {
var min = Vec3.create(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
var max = Vec3.create(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
var x = data.x, y = data.y, z = data.z, indices = data.indices;
for (var t = 0, _t = OrderedSet.size(indices); t < _t; t++) {
var i = OrderedSet.getAt(indices, t);
min[0] = Math.min(x[i], min[0]);
min[1] = Math.min(y[i], min[1]);
min[2] = Math.min(z[i], min[2]);
max[0] = Math.max(x[i], max[0]);
max[1] = Math.max(y[i], max[1]);
max[2] = Math.max(z[i], max[2]);
}
return { min: min, max: max };
}
Box3D.computeBounding = computeBounding;
/** Get size/extent of the box */
function size(size, box) {
return Vec3.sub(size, box.max, box.min);
}
Box3D.size = size;
var tmpSizeV = Vec3();
/** Get volume of the box */
function volume(box) {
size(tmpSizeV, box);
return tmpSizeV[0] * tmpSizeV[1] * tmpSizeV[2];
}
Box3D.volume = volume;
/** Sets min to Number.MAX_VALUE and max to -Number.MAX_VALUE */
function setEmpty(box) {
Vec3.set(box.min, Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
Vec3.set(box.max, -Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
return box;
}
Box3D.setEmpty = setEmpty;
/** Add point to box */
function add(box, point) {
Vec3.min(box.min, box.min, point);
Vec3.max(box.max, box.max, point);
return box;
}
Box3D.add = add;
/** Expand box by delta */
function expand(out, box, delta) {
Vec3.sub(out.min, box.min, delta);
Vec3.add(out.max, box.max, delta);
return out;
}
Box3D.expand = expand;
function scale(out, box, scale) {
Vec3.scale(out.min, box.min, scale);
Vec3.scale(out.max, box.max, scale);
return out;
}
Box3D.scale = scale;
var tmpTransformV = Vec3();
/** Transform box with a Mat4 */
function transform(out, box, m) {
var _a = box.min, minX = _a[0], minY = _a[1], minZ = _a[2];
var _b = box.max, maxX = _b[0], maxY = _b[1], maxZ = _b[2];
setEmpty(out);
add(out, Vec3.transformMat4(tmpTransformV, Vec3.set(tmpTransformV, minX, minY, minZ), m));
add(out, Vec3.transformMat4(tmpTransformV, Vec3.set(tmpTransformV, minX, minY, maxZ), m));
add(out, Vec3.transformMat4(tmpTransformV, Vec3.set(tmpTransformV, minX, maxY, minZ), m));
add(out, Vec3.transformMat4(tmpTransformV, Vec3.set(tmpTransformV, minX, maxY, maxZ), m));
add(out, Vec3.transformMat4(tmpTransformV, Vec3.set(tmpTransformV, maxX, minY, minZ), m));
add(out, Vec3.transformMat4(tmpTransformV, Vec3.set(tmpTransformV, maxX, minY, maxZ), m));
add(out, Vec3.transformMat4(tmpTransformV, Vec3.set(tmpTransformV, maxX, maxY, minZ), m));
add(out, Vec3.transformMat4(tmpTransformV, Vec3.set(tmpTransformV, maxX, maxY, maxZ), m));
return out;
}
Box3D.transform = transform;
function containsVec3(box, v) {
return (v[0] < box.min[0] || v[0] > box.max[0] ||
v[1] < box.min[1] || v[1] > box.max[1] ||
v[2] < box.min[2] || v[2] > box.max[2]) ? false : true;
}
Box3D.containsVec3 = containsVec3;
})(Box3D || (Box3D = {}));
export { Box3D };
//# sourceMappingURL=box3d.js.map