unitcell.ts 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Model, Symmetry } from '../../../mol-model/structure';
  7. import { ShapeRepresentation } from '../representation';
  8. import { Shape } from '../../../mol-model/shape';
  9. import { ColorNames } from '../../../mol-util/color/names';
  10. import { RuntimeContext } from '../../../mol-task';
  11. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  12. import { Mesh } from '../../../mol-geo/geometry/mesh/mesh';
  13. import { MeshBuilder } from '../../../mol-geo/geometry/mesh/mesh-builder';
  14. import { BoxCage } from '../../../mol-geo/primitive/box';
  15. import { Mat4, Vec3 } from '../../../mol-math/linear-algebra';
  16. import { transformCage, cloneCage } from '../../../mol-geo/primitive/cage';
  17. import { Sphere3D } from '../../../mol-math/geometry';
  18. import { RepresentationParamsGetter, Representation, RepresentationContext } from '../../representation';
  19. const translate05 = Mat4.fromTranslation(Mat4(), Vec3.create(0.5, 0.5, 0.5));
  20. const unitCage = transformCage(cloneCage(BoxCage()), translate05);
  21. const tmpRef = Vec3();
  22. const tmpTranslate = Mat4();
  23. interface UnitcellData {
  24. symmetry: Symmetry
  25. ref: Vec3
  26. }
  27. const CellRef = {
  28. origin: 'Origin',
  29. model: 'Model'
  30. };
  31. const CellParams = {
  32. ...Mesh.Params,
  33. cellColor: PD.Color(ColorNames.orange),
  34. cellScale: PD.Numeric(2, { min: 0.1, max: 5, step: 0.1 }),
  35. ref: PD.Select('model', PD.objectToOptions(CellRef), { isEssential: true })
  36. };
  37. type MeshParams = typeof CellParams
  38. const UnitcellVisuals = {
  39. 'mesh': (ctx: RepresentationContext, getParams: RepresentationParamsGetter<UnitcellData, MeshParams>) => ShapeRepresentation(getUnitcellShape, Mesh.Utils),
  40. };
  41. export const UnitcellParams = {
  42. ...CellParams
  43. };
  44. export type UnitcellParams = typeof UnitcellParams
  45. export type UnitcellProps = PD.Values<UnitcellParams>
  46. function getUnitcellMesh(data: UnitcellData, props: UnitcellProps, mesh?: Mesh) {
  47. const state = MeshBuilder.createState(256, 128, mesh);
  48. const { fromFractional } = data.symmetry.spacegroup.cell;
  49. Vec3.floor(tmpRef, data.ref);
  50. Mat4.fromTranslation(tmpTranslate, tmpRef);
  51. const cellCage = transformCage(cloneCage(unitCage), tmpTranslate);
  52. const radius = (Math.cbrt(data.symmetry.spacegroup.cell.volume) / 300) * props.cellScale;
  53. state.currentGroup = 1;
  54. MeshBuilder.addCage(state, fromFractional, cellCage, radius, 2, 20);
  55. const sphere = Sphere3D.fromDimensionsAndTransform(Sphere3D(), Vec3.unit, fromFractional);
  56. Vec3.transformMat4(tmpRef, tmpRef, fromFractional);
  57. Sphere3D.translate(sphere, sphere, tmpRef);
  58. Sphere3D.expand(sphere, sphere, radius);
  59. const m = MeshBuilder.getMesh(state);
  60. m.setBoundingSphere(sphere);
  61. return m;
  62. }
  63. function getUnitcellShape(ctx: RuntimeContext, data: UnitcellData, props: UnitcellProps, shape?: Shape<Mesh>) {
  64. const geo = getUnitcellMesh(data, props, shape && shape.geometry);
  65. const label = Symmetry.getUnitcellLabel(data.symmetry);
  66. return Shape.create(label, data, geo, () => props.cellColor, () => 1, () => label);
  67. }
  68. //
  69. export function getUnitcellData(model: Model, symmetry: Symmetry, props: UnitcellProps) {
  70. const ref = Vec3();
  71. if (props.ref === 'model') {
  72. Vec3.transformMat4(ref, Model.getCenter(model), symmetry.spacegroup.cell.toFractional);
  73. }
  74. return { symmetry, ref };
  75. }
  76. export type UnitcellRepresentation = Representation<UnitcellData, UnitcellParams>
  77. export function UnitcellRepresentation(ctx: RepresentationContext, getParams: RepresentationParamsGetter<UnitcellData, UnitcellParams>): UnitcellRepresentation {
  78. return Representation.createMulti('Unit Cell', ctx, getParams, Representation.StateBuilder, UnitcellVisuals as unknown as Representation.Def<UnitcellData, UnitcellParams>);
  79. }