symmetry-operator.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { Vec3, Mat4, Mat3 } from '../linear-algebra/3d'
  7. interface SymmetryOperator {
  8. readonly name: string,
  9. readonly hkl: Vec3,
  10. readonly matrix: Mat4,
  11. // cache the inverse of the transform
  12. readonly inverse: Mat4,
  13. // optimize the identity case
  14. readonly isIdentity: boolean
  15. }
  16. namespace SymmetryOperator {
  17. export const DefaultName = '1_555'
  18. export const Default: SymmetryOperator = create(DefaultName, Mat4.identity());
  19. const RotationEpsilon = 0.0001;
  20. export function create(name: string, matrix: Mat4, hkl?: Vec3): SymmetryOperator {
  21. const _hkl = hkl ? Vec3.clone(hkl) : Vec3.zero();
  22. if (Mat4.isIdentity(matrix)) return { name, matrix, inverse: Mat4.identity(), isIdentity: true, hkl: _hkl };
  23. if (!Mat4.isRotationAndTranslation(matrix, RotationEpsilon)) throw new Error(`Symmetry operator (${name}) must be a composition of rotation and translation.`);
  24. return { name, matrix, inverse: Mat4.invert(Mat4.zero(), matrix), isIdentity: false, hkl: _hkl };
  25. }
  26. export function ofRotationAndOffset(name: string, rot: Mat3, offset: Vec3) {
  27. const t = Mat4.identity();
  28. for (let i = 0; i < 3; i++) {
  29. for (let j = 0; j < 3; j++) {
  30. Mat4.setValue(t, i, j, Mat3.getValue(rot, i, j));
  31. }
  32. }
  33. Mat4.setTranslation(t, offset);
  34. return create(name, t);
  35. }
  36. // Apply the 1st and then 2nd operator. ( = second.matrix * first.matrix)
  37. export function compose(first: SymmetryOperator, second: SymmetryOperator) {
  38. const matrix = Mat4.mul(Mat4.zero(), second.matrix, first.matrix);
  39. return create(second.name, matrix, second.hkl);
  40. }
  41. export interface CoordinateMapper { (index: number, slot: Vec3): Vec3 }
  42. export interface ArrayMapping {
  43. readonly operator: SymmetryOperator,
  44. readonly invariantPosition: CoordinateMapper,
  45. readonly position: CoordinateMapper,
  46. x(index: number): number,
  47. y(index: number): number,
  48. z(index: number): number
  49. }
  50. export interface Coordinates { x: ArrayLike<number>, y: ArrayLike<number>, z: ArrayLike<number> }
  51. export function createMapping(operator: SymmetryOperator, coords: Coordinates): ArrayMapping {
  52. const invariantPosition = SymmetryOperator.createCoordinateMapper(SymmetryOperator.Default, coords);
  53. const position = operator.isIdentity ? invariantPosition : SymmetryOperator.createCoordinateMapper(operator, coords);
  54. const { x, y, z } = createProjections(operator, coords);
  55. return { operator, invariantPosition, position, x, y, z };
  56. }
  57. export function createCoordinateMapper(t: SymmetryOperator, coords: Coordinates): CoordinateMapper {
  58. if (t.isIdentity) return identityPosition(coords);
  59. return generalPosition(t, coords);
  60. }
  61. }
  62. export { SymmetryOperator }
  63. interface Projections { x(index: number): number, y(index: number): number, z(index: number): number }
  64. function createProjections(t: SymmetryOperator, coords: SymmetryOperator.Coordinates): Projections {
  65. if (t.isIdentity) return { x: projectCoord(coords.x), y: projectCoord(coords.y), z: projectCoord(coords.z) };
  66. return { x: projectX(t, coords), y: projectY(t, coords), z: projectZ(t, coords) };
  67. }
  68. function projectCoord(xs: ArrayLike<number>) {
  69. return (i: number) => xs[i];
  70. }
  71. function isW1(m: Mat4) {
  72. return m[3] === 0 && m[7] === 0 && m[11] === 0 && m[15] === 1;
  73. }
  74. function projectX({ matrix: m }: SymmetryOperator, { x: xs, y: ys, z: zs}: SymmetryOperator.Coordinates) {
  75. const xx = m[0], yy = m[4], zz = m[8], tx = m[12];
  76. if (isW1(m)) {
  77. // this should always be the case.
  78. return (i: number) => xx * xs[i] + yy * ys[i] + zz * zs[i] + tx;
  79. }
  80. return (i: number) => {
  81. const x = xs[i], y = ys[i], z = zs[i], w = (m[3] * x + m[7] * y + m[11] * z + m[15]) || 1.0;
  82. return (xx * x + yy * y + zz * z + tx) / w;
  83. }
  84. }
  85. function projectY({ matrix: m }: SymmetryOperator, { x: xs, y: ys, z: zs}: SymmetryOperator.Coordinates) {
  86. const xx = m[1], yy = m[5], zz = m[9], ty = m[13];
  87. if (isW1(m)) {
  88. // this should always be the case.
  89. return (i: number) => xx * xs[i] + yy * ys[i] + zz * zs[i] + ty;
  90. }
  91. return (i: number) => {
  92. const x = xs[i], y = ys[i], z = zs[i], w = (m[3] * x + m[7] * y + m[11] * z + m[15]) || 1.0;
  93. return (xx * x + yy * y + zz * z + ty) / w;
  94. }
  95. }
  96. function projectZ({ matrix: m }: SymmetryOperator, { x: xs, y: ys, z: zs}: SymmetryOperator.Coordinates) {
  97. const xx = m[2], yy = m[6], zz = m[10], tz = m[14];
  98. if (isW1(m)) {
  99. // this should always be the case.
  100. return (i: number) => xx * xs[i] + yy * ys[i] + zz * zs[i] + tz;
  101. }
  102. return (i: number) => {
  103. const x = xs[i], y = ys[i], z = zs[i], w = (m[3] * x + m[7] * y + m[11] * z + m[15]) || 1.0;
  104. return (xx * x + yy * y + zz * z + tz) / w;
  105. }
  106. }
  107. function identityPosition({ x, y, z }: SymmetryOperator.Coordinates): SymmetryOperator.CoordinateMapper {
  108. return (i, s) => {
  109. s[0] = x[i];
  110. s[1] = y[i];
  111. s[2] = z[i];
  112. return s;
  113. }
  114. }
  115. function generalPosition({ matrix: m }: SymmetryOperator, { x: xs, y: ys, z: zs }: SymmetryOperator.Coordinates) {
  116. if (isW1(m)) {
  117. // this should always be the case.
  118. return (i: number, r: Vec3): Vec3 => {
  119. const x = xs[i], y = ys[i], z = zs[i];
  120. r[0] = m[0] * x + m[4] * y + m[8] * z + m[12];
  121. r[1] = m[1] * x + m[5] * y + m[9] * z + m[13];
  122. r[2] = m[2] * x + m[6] * y + m[10] * z + m[14];
  123. return r;
  124. }
  125. }
  126. return (i: number, r: Vec3): Vec3 => {
  127. r[0] = xs[i];
  128. r[1] = ys[i];
  129. r[2] = zs[i];
  130. Vec3.transformMat4(r, r, m);
  131. return r;
  132. }
  133. }