boundary.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 { PositionData } from './common';
  8. import { Vec3 } from '../linear-algebra';
  9. import { OrderedSet } from '../../mol-data/int';
  10. import { BoundaryHelper } from './boundary-helper';
  11. import { Box3D, Sphere3D } from '../geometry';
  12. const boundaryHelperCoarse = new BoundaryHelper('14');
  13. const boundaryHelperFine = new BoundaryHelper('98');
  14. function getBoundaryHelper(count: number) {
  15. return count > 100_000 ? boundaryHelperCoarse : boundaryHelperFine
  16. }
  17. export type Boundary = { readonly box: Box3D, readonly sphere: Sphere3D }
  18. export function getBoundary(data: PositionData): Boundary {
  19. const { x, y, z, radius, indices } = data;
  20. const p = Vec3();
  21. const boundaryHelper = getBoundaryHelper(OrderedSet.size(indices));
  22. boundaryHelper.reset();
  23. for (let t = 0, _t = OrderedSet.size(indices); t < _t; t++) {
  24. const i = OrderedSet.getAt(indices, t);
  25. Vec3.set(p, x[i], y[i], z[i]);
  26. boundaryHelper.includePositionRadius(p, (radius && radius[i]) || 0);
  27. }
  28. boundaryHelper.finishedIncludeStep();
  29. for (let t = 0, _t = OrderedSet.size(indices); t < _t; t++) {
  30. const i = OrderedSet.getAt(indices, t);
  31. Vec3.set(p, x[i], y[i], z[i]);
  32. boundaryHelper.radiusPositionRadius(p, (radius && radius[i]) || 0);
  33. }
  34. const sphere = boundaryHelper.getSphere()
  35. if (!radius && OrderedSet.size(indices) <= 98) {
  36. const extrema: Vec3[] = []
  37. for (let t = 0, _t = OrderedSet.size(indices); t < _t; t++) {
  38. const i = OrderedSet.getAt(indices, t);
  39. extrema.push(Vec3.create(x[i], y[i], z[i]));
  40. }
  41. Sphere3D.setExtrema(sphere, extrema)
  42. }
  43. return { box: boundaryHelper.getBox(), sphere };
  44. }