data.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { SpacegroupCell, Box3D } from 'mol-math/geometry'
  7. import { Tensor, Mat4, Vec3 } from 'mol-math/linear-algebra'
  8. import { shallowEqual } from 'mol-util/object';
  9. /** The basic unit cell that contains the data. */
  10. interface VolumeData {
  11. readonly cell: SpacegroupCell,
  12. readonly fractionalBox: Box3D,
  13. readonly data: Tensor,
  14. readonly dataStats: Readonly<{
  15. min: number,
  16. max: number,
  17. mean: number,
  18. sigma: number
  19. }>
  20. }
  21. namespace VolumeData {
  22. export const One: VolumeData = {
  23. cell: SpacegroupCell.Zero,
  24. fractionalBox: Box3D.empty(),
  25. data: Tensor.create(Tensor.Space([1, 1, 1], [0, 1, 2]), Tensor.Data1([0])),
  26. dataStats: { min: 0, max: 0, mean: 0, sigma: 0 }
  27. }
  28. const _scale = Mat4.zero(), _translate = Mat4.zero();
  29. export function getGridToCartesianTransform(volume: VolumeData) {
  30. const { data: { space } } = volume;
  31. const scale = Mat4.fromScaling(_scale, Vec3.div(Vec3.zero(), Box3D.size(Vec3.zero(), volume.fractionalBox), Vec3.ofArray(space.dimensions)));
  32. const translate = Mat4.fromTranslation(_translate, volume.fractionalBox.min);
  33. return Mat4.mul3(Mat4.zero(), volume.cell.fromFractional, translate, scale);
  34. }
  35. export function areEquivalent(volA: VolumeData, volB: VolumeData) {
  36. return volA === volB
  37. }
  38. }
  39. type VolumeIsoValue = VolumeIsoValue.Absolute | VolumeIsoValue.Relative
  40. namespace VolumeIsoValue {
  41. export type Relative = Readonly<{ kind: 'relative', relativeValue: number }>
  42. export type Absolute = Readonly<{ kind: 'absolute', absoluteValue: number }>
  43. export function areSame(a: VolumeIsoValue, b: VolumeIsoValue) {
  44. // TODO: this should compare values converted to absolute value
  45. return shallowEqual(a, b);
  46. }
  47. export function absolute(value: number): Absolute { return { kind: 'absolute', absoluteValue: value }; }
  48. export function relative(value: number): Relative { return { kind: 'relative', relativeValue: value }; }
  49. export function calcAbsolute(stats: VolumeData['dataStats'], relativeValue: number): number {
  50. return relativeValue * stats.sigma + stats.mean
  51. }
  52. export function calcRelative(stats: VolumeData['dataStats'], absoluteValue: number): number {
  53. return stats.sigma === 0 ? 0 : ((absoluteValue - stats.mean) / stats.sigma)
  54. }
  55. export function toAbsolute(value: VolumeIsoValue, stats: VolumeData['dataStats']): Absolute {
  56. return value.kind === 'absolute' ? value : { kind: 'absolute', absoluteValue: VolumeIsoValue.calcAbsolute(stats, value.relativeValue) }
  57. }
  58. export function toRelative(value: VolumeIsoValue, stats: VolumeData['dataStats']): Relative {
  59. return value.kind === 'relative' ? value : { kind: 'relative', relativeValue: VolumeIsoValue.calcRelative(stats, value.absoluteValue) }
  60. }
  61. export function toString(value: VolumeIsoValue) {
  62. return value.kind === 'relative'
  63. ? `${value.relativeValue} σ`
  64. : `${value.absoluteValue}`
  65. }
  66. }
  67. export { VolumeData, VolumeIsoValue }