representation.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 { ParamDefinition as PD } from 'molstar/lib/mol-util/param-definition';
  8. import { Vec3, Mat4 } from 'molstar/lib/mol-math/linear-algebra';
  9. import { Representation, RepresentationContext, RepresentationParamsGetter } from 'molstar/lib/mol-repr/representation';
  10. import { Structure } from 'molstar/lib/mol-model/structure';
  11. import { StructureRepresentationProvider, StructureRepresentation, StructureRepresentationStateBuilder } from 'molstar/lib/mol-repr/structure/representation';
  12. import { MembraneOrientation, MembraneOrientationProvider } from './prop';
  13. import { ThemeRegistryContext } from 'molstar/lib/mol-theme/theme';
  14. import { ShapeRepresentation } from 'molstar/lib/mol-repr/shape/representation';
  15. import { Shape } from 'molstar/lib/mol-model/shape';
  16. import { RuntimeContext } from 'molstar/lib/mol-task';
  17. import { Lines } from 'molstar/lib/mol-geo/geometry/lines/lines';
  18. import { Mesh } from 'molstar/lib/mol-geo/geometry/mesh/mesh';
  19. import { LinesBuilder } from 'molstar/lib/mol-geo/geometry/lines/lines-builder';
  20. import { Circle } from 'molstar/lib/mol-geo/primitive/circle';
  21. import { transformPrimitive } from 'molstar/lib/mol-geo/primitive/primitive';
  22. import { MeshBuilder } from 'molstar/lib/mol-geo/geometry/mesh/mesh-builder';
  23. import { MarkerActions } from 'molstar/lib/mol-util/marker-action';
  24. import { lociLabel } from 'molstar/lib/mol-theme/label';
  25. import { ColorNames } from 'molstar/lib/mol-util/color/names';
  26. import { CustomProperty } from 'molstar/lib/mol-model-props/common/custom-property';
  27. const SharedParams = {
  28. color: PD.Color(ColorNames.lightgrey),
  29. radiusFactor: PD.Numeric(1.2, { min: 0.1, max: 3.0, step: 0.01 }, { description: 'Scale the radius of the membrane layer' })
  30. };
  31. const BilayerPlanesParams = {
  32. ...Mesh.Params,
  33. ...SharedParams,
  34. sectorOpacity: PD.Numeric(0.5, { min: 0, max: 1, step: 0.01 }),
  35. };
  36. export type BilayerPlanesParams = typeof BilayerPlanesParams
  37. export type BilayerPlanesProps = PD.Values<BilayerPlanesParams>
  38. const BilayerRimsParams = {
  39. ...Lines.Params,
  40. ...SharedParams,
  41. lineSizeAttenuation: PD.Boolean(true),
  42. linesSize: PD.Numeric(0.3, { min: 0.01, max: 50, step: 0.01 }),
  43. dashedLines: PD.Boolean(true),
  44. };
  45. export type BilayerRimsParams = typeof BilayerRimsParams
  46. export type BilayerRimsProps = PD.Values<BilayerRimsParams>
  47. const MembraneOrientationVisuals = {
  48. 'bilayer-planes': (ctx: RepresentationContext, getParams: RepresentationParamsGetter<MembraneOrientation, BilayerPlanesParams>) => ShapeRepresentation(getBilayerPlanes, Mesh.Utils, { modifyState: s => ({ ...s, markerActions: MarkerActions.Highlighting }), modifyProps: p => ({ ...p, alpha: p.sectorOpacity, ignoreLight: true, doubleSided: false }) }),
  49. 'bilayer-rims': (ctx: RepresentationContext, getParams: RepresentationParamsGetter<MembraneOrientation, BilayerRimsParams>) => ShapeRepresentation(getBilayerRims, Lines.Utils, { modifyState: s => ({ ...s, markerActions: MarkerActions.Highlighting }) })
  50. };
  51. export const MembraneOrientationParams = {
  52. ...BilayerPlanesParams,
  53. ...BilayerRimsParams,
  54. visuals: PD.MultiSelect(['bilayer-planes', 'bilayer-rims'], PD.objectToOptions(MembraneOrientationVisuals)),
  55. };
  56. export type MembraneOrientationParams = typeof MembraneOrientationParams
  57. export type MembraneOrientationProps = PD.Values<MembraneOrientationParams>
  58. export function getMembraneOrientationParams(ctx: ThemeRegistryContext, structure: Structure) {
  59. return PD.clone(MembraneOrientationParams);
  60. }
  61. export type MembraneOrientationRepresentation = StructureRepresentation<MembraneOrientationParams>
  62. export function MembraneOrientationRepresentation(ctx: RepresentationContext, getParams: RepresentationParamsGetter<Structure, MembraneOrientationParams>): MembraneOrientationRepresentation {
  63. return Representation.createMulti('TMDET Membrane Orientation', ctx, getParams, StructureRepresentationStateBuilder, MembraneOrientationVisuals as unknown as Representation.Def<Structure, MembraneOrientationParams>);
  64. }
  65. export const MembraneOrientationRepresentationProvider = StructureRepresentationProvider({
  66. name: 'tmdet-membrane-orientation',
  67. label: 'TMDET Membrane Orientation',
  68. description: 'Displays a grid of points representing membrane layers.',
  69. factory: MembraneOrientationRepresentation,
  70. getParams: getMembraneOrientationParams,
  71. defaultValues: PD.getDefaultValues(MembraneOrientationParams),
  72. defaultColorTheme: { name: 'shape-group' },
  73. defaultSizeTheme: { name: 'shape-group' },
  74. isApplicable: (structure: Structure) => structure.elementCount > 0,
  75. ensureCustomProperties: {
  76. attach: (ctx: CustomProperty.Context, structure: Structure) => MembraneOrientationProvider.attach(ctx, structure, void 0, true),
  77. detach: (data) => MembraneOrientationProvider.ref(data, false)
  78. }
  79. });
  80. function membraneLabel(data: Structure) {
  81. return `${lociLabel(Structure.Loci(data))} | TMDET Membrane Orientation`;
  82. }
  83. function getBilayerRims(ctx: RuntimeContext, data: Structure, props: BilayerRimsProps, shape?: Shape<Lines>): Shape<Lines> {
  84. const { planePoint1: p1, planePoint2: p2, centroid, radius } = MembraneOrientationProvider.get(data).value!;
  85. const scaledRadius = props.radiusFactor * radius;
  86. const builder = LinesBuilder.create(128, 64, shape?.geometry);
  87. getLayerCircle(builder, p1, centroid, scaledRadius, props);
  88. getLayerCircle(builder, p2, centroid, scaledRadius, props);
  89. return Shape.create('Bilayer rims', data, builder.getLines(), () => props.color, () => props.linesSize, () => membraneLabel(data));
  90. }
  91. function getLayerCircle(builder: LinesBuilder, p: Vec3, centroid: Vec3, radius: number, props: BilayerRimsProps, shape?: Shape<Lines>) {
  92. const circle = getCircle(p, centroid, radius);
  93. const { indices, vertices } = circle;
  94. for (let j = 0, jl = indices.length; j < jl; j += 3) {
  95. if (props.dashedLines && j % 2 === 1) continue; // draw every other segment to get dashes
  96. const start = indices[j] * 3;
  97. const end = indices[j + 1] * 3;
  98. const startX = vertices[start];
  99. const startY = vertices[start + 1];
  100. const startZ = vertices[start + 2];
  101. const endX = vertices[end];
  102. const endY = vertices[end + 1];
  103. const endZ = vertices[end + 2];
  104. builder.add(startX, startY, startZ, endX, endY, endZ, 0);
  105. }
  106. }
  107. const tmpMat = Mat4();
  108. const tmpV = Vec3();
  109. function getCircle(p: Vec3, centroid: Vec3, radius: number) {
  110. if (Vec3.dot(Vec3.unitY, Vec3.sub(tmpV, p, centroid)) === 0) {
  111. Mat4.targetTo(tmpMat, p, centroid, Vec3.unitY);
  112. } else {
  113. Mat4.targetTo(tmpMat, p, centroid, Vec3.unitX);
  114. }
  115. Mat4.setTranslation(tmpMat, p);
  116. Mat4.mul(tmpMat, tmpMat, Mat4.rotX90);
  117. const circle = Circle({ radius, segments: 64 });
  118. return transformPrimitive(circle, tmpMat);
  119. }
  120. function getBilayerPlanes(ctx: RuntimeContext, data: Structure, props: BilayerPlanesProps, shape?: Shape<Mesh>): Shape<Mesh> {
  121. const { planePoint1: p1, planePoint2: p2, centroid, radius } = MembraneOrientationProvider.get(data).value!;
  122. const state = MeshBuilder.createState(128, 64, shape && shape.geometry);
  123. const scaledRadius = props.radiusFactor * radius;
  124. getLayerPlane(state, p1, centroid, scaledRadius);
  125. getLayerPlane(state, p2, centroid, scaledRadius);
  126. return Shape.create('Bilayer planes', data, MeshBuilder.getMesh(state), () => props.color, () => 1, () => membraneLabel(data));
  127. }
  128. function getLayerPlane(state: MeshBuilder.State, p: Vec3, centroid: Vec3, radius: number) {
  129. const circle = getCircle(p, centroid, radius);
  130. state.currentGroup = 0;
  131. MeshBuilder.addPrimitive(state, Mat4.id, circle);
  132. MeshBuilder.addPrimitiveFlipped(state, Mat4.id, circle);
  133. }