representation.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 { ShapeRepresentation } from '../../mol-repr/shape/representation';
  7. import { Shape } from '../../mol-model/shape';
  8. import { ColorNames } from '../../mol-util/color/names';
  9. import { RuntimeContext } from '../../mol-task';
  10. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  11. import { Mesh } from '../../mol-geo/geometry/mesh/mesh';
  12. import { MeshBuilder } from '../../mol-geo/geometry/mesh/mesh-builder';
  13. import { Polyhedron, DefaultPolyhedronProps } from '../../mol-geo/primitive/polyhedron';
  14. import { Icosahedron } from '../../mol-geo/primitive/icosahedron';
  15. import { Mat4, Vec3 } from '../../mol-math/linear-algebra';
  16. import { RepresentationParamsGetter, Representation, RepresentationContext } from '../../mol-repr/representation';
  17. interface MembraneSphereData {
  18. radius: number
  19. center: Vec3
  20. }
  21. const MembraneSphereParams = {
  22. ...Mesh.Params,
  23. cellColor: PD.Color(ColorNames.orange),
  24. cellScale: PD.Numeric(2, { min: 0.1, max: 5, step: 0.1 }),
  25. radius: PD.Numeric(2, { min: 0.1, max: 5, step: 0.1 }),
  26. center: PD.Vec3(Vec3.create(0, 0, 0)),
  27. quality: { ...Mesh.Params.quality, isEssential: false },
  28. };
  29. type MeshParams = typeof MembraneSphereParams
  30. const MembraneSphereVisuals = {
  31. 'mesh': (ctx: RepresentationContext, getParams: RepresentationParamsGetter<MembraneSphereData, MeshParams>) => ShapeRepresentation(getMBShape, Mesh.Utils),
  32. };
  33. export const MBParams = {
  34. ...MembraneSphereParams
  35. };
  36. export type MBParams = typeof MBParams
  37. export type UnitcellProps = PD.Values<MBParams>
  38. function getMBMesh(data: MembraneSphereData, props: UnitcellProps, mesh?: Mesh) {
  39. const state = MeshBuilder.createState(256, 128, mesh);
  40. const radius = props.radius;
  41. let p = DefaultPolyhedronProps;
  42. p.detail = 3;
  43. p.radius = radius;
  44. const { vertices, indices } = Icosahedron();
  45. const asphere = Polyhedron(vertices, indices, p);
  46. // const asphere = Sphere(3);
  47. let trans: Mat4 = Mat4.identity();
  48. // Mat4.fromScaling(trans, Vec3.create(radius,radius,radius));
  49. state.currentGroup = 1;
  50. MeshBuilder.addPrimitive(state, trans, asphere);
  51. const m = MeshBuilder.getMesh(state);
  52. return m;
  53. }
  54. function getMBShape(ctx: RuntimeContext, data: MembraneSphereData, props: UnitcellProps, shape?: Shape<Mesh>) {
  55. const geo = getMBMesh(data, props, shape && shape.geometry);
  56. const label = 'mb';
  57. return Shape.create(label, data, geo, () => props.cellColor, () => 1, () => label);
  58. }
  59. //
  60. /*
  61. export function getMBData(model: Model, symmetry: Symmetry, props: UnitcellProps) {
  62. const ref = Vec3();
  63. if (props.ref === 'model') {
  64. Vec3.transformMat4(ref, Model.getCenter(model), symmetry.spacegroup.cell.toFractional);
  65. }
  66. return { symmetry, ref };
  67. }
  68. */
  69. export type MBRepresentation = Representation<MembraneSphereData, MBParams>
  70. export function MBRepresentation(ctx: RepresentationContext, getParams: RepresentationParamsGetter<MembraneSphereData, MBParams>): MBRepresentation {
  71. return Representation.createMulti('MB', ctx, getParams, Representation.StateBuilder, MembraneSphereVisuals as unknown as Representation.Def<MembraneSphereData, MBParams>);
  72. }