dx.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { DxFile } from '../../mol-io/reader/dx/parser';
  7. import { Mat4, Tensor } from '../../mol-math/linear-algebra';
  8. import { Volume } from '../../mol-model/volume';
  9. import { Task } from '../../mol-task';
  10. import { arrayMax, arrayMean, arrayMin, arrayRms } from '../../mol-util/array';
  11. import { ModelFormat } from '../format';
  12. import { CustomProperties } from '../../mol-model/custom-property';
  13. export function volumeFromDx(source: DxFile, params?: { label?: string, entryId?: string }): Task<Volume> {
  14. return Task.create<Volume>('Create Volume', async () => {
  15. const { header, values } = source;
  16. const space = Tensor.Space(header.dim, [0, 1, 2], Float64Array);
  17. const data = Tensor.create(space, Tensor.Data1(values));
  18. const matrix = Mat4.fromTranslation(Mat4(), header.min);
  19. const basis = Mat4.fromScaling(Mat4(), header.h);
  20. Mat4.mul(matrix, matrix, basis);
  21. return {
  22. label: params?.label,
  23. entryId: params?.entryId,
  24. grid: {
  25. transform: { kind: 'matrix', matrix },
  26. cells: data,
  27. stats: {
  28. min: arrayMin(values),
  29. max: arrayMax(values),
  30. mean: arrayMean(values),
  31. sigma: arrayRms(values)
  32. },
  33. },
  34. sourceData: DxFormat.create(source),
  35. customProperties: new CustomProperties(),
  36. _propertyData: Object.create(null),
  37. };
  38. });
  39. }
  40. //
  41. export { DxFormat };
  42. type DxFormat = ModelFormat<DxFile>
  43. namespace DxFormat {
  44. export function is(x?: ModelFormat): x is DxFormat {
  45. return x?.kind === 'dx';
  46. }
  47. export function create(dx: DxFile): DxFormat {
  48. return { kind: 'dx', name: dx.name, data: dx };
  49. }
  50. }