boundary.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import { Box3D, Sphere3D } from '../../../../mol-math/geometry';
  8. import { Vec3 } from '../../../../mol-math/linear-algebra';
  9. import Structure from '../structure';
  10. import { BoundaryHelper } from '../../../../mol-math/geometry/boundary-helper';
  11. export type Boundary = { box: Box3D, sphere: Sphere3D }
  12. const tmpBox = Box3D();
  13. const tmpSphere = Sphere3D();
  14. const boundaryHelper = new BoundaryHelper('98');
  15. export function computeStructureBoundary(s: Structure): Boundary {
  16. const min = Vec3.create(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE)
  17. const max = Vec3.create(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE)
  18. const { units } = s;
  19. boundaryHelper.reset();
  20. for (let i = 0, _i = units.length; i < _i; i++) {
  21. const u = units[i];
  22. const invariantBoundary = u.lookup3d.boundary;
  23. const o = u.conformation.operator;
  24. if (o.isIdentity) {
  25. Vec3.min(min, min, invariantBoundary.box.min);
  26. Vec3.max(max, max, invariantBoundary.box.max);
  27. boundaryHelper.includeSphereStep(invariantBoundary.sphere.center, invariantBoundary.sphere.radius);
  28. } else {
  29. Box3D.transform(tmpBox, invariantBoundary.box, o.matrix);
  30. Vec3.min(min, min, tmpBox.min);
  31. Vec3.max(max, max, tmpBox.max);
  32. Sphere3D.transform(tmpSphere, invariantBoundary.sphere, o.matrix);
  33. boundaryHelper.includeSphereStep(tmpSphere.center, tmpSphere.radius);
  34. }
  35. }
  36. boundaryHelper.finishedIncludeStep();
  37. for (let i = 0, _i = units.length; i < _i; i++) {
  38. const u = units[i];
  39. const invariantBoundary = u.lookup3d.boundary;
  40. const o = u.conformation.operator;
  41. if (o.isIdentity) {
  42. boundaryHelper.radiusSphereStep(invariantBoundary.sphere.center, invariantBoundary.sphere.radius);
  43. } else {
  44. Sphere3D.transform(tmpSphere, invariantBoundary.sphere, o.matrix);
  45. boundaryHelper.radiusSphereStep(tmpSphere.center, tmpSphere.radius);
  46. }
  47. }
  48. return { box: { min, max }, sphere: boundaryHelper.getSphere() };
  49. }