angle.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Loci } from '../../../mol-model/loci';
  7. import { RuntimeContext } from '../../../mol-task';
  8. import { stringToWords } from '../../../mol-util/string';
  9. import { Lines } from '../../../mol-geo/geometry/lines/lines';
  10. import { Text } from '../../../mol-geo/geometry/text/text';
  11. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  12. import { ColorNames } from '../../../mol-util/color/names';
  13. import { ShapeRepresentation } from '../representation';
  14. import { Representation, RepresentationParamsGetter, RepresentationContext } from '../../representation';
  15. import { Shape } from '../../../mol-model/shape';
  16. import { LinesBuilder } from '../../../mol-geo/geometry/lines/lines-builder';
  17. import { TextBuilder } from '../../../mol-geo/geometry/text/text-builder';
  18. import { Vec3, Mat4 } from '../../../mol-math/linear-algebra';
  19. import { Mesh } from '../../../mol-geo/geometry/mesh/mesh';
  20. import { MeshBuilder } from '../../../mol-geo/geometry/mesh/mesh-builder';
  21. import { radToDeg, arcLength } from '../../../mol-math/misc';
  22. import { Circle } from '../../../mol-geo/primitive/circle';
  23. import { transformPrimitive } from '../../../mol-geo/primitive/primitive';
  24. export interface AngleData {
  25. triples: Loci.Triple[]
  26. }
  27. const SharedParams = {
  28. color: PD.Color(ColorNames.lightgreen),
  29. arcScale: PD.Numeric(0.7, { min: 0.01, max: 1, step: 0.01 })
  30. }
  31. const LinesParams = {
  32. ...Lines.Params,
  33. ...SharedParams,
  34. lineSizeAttenuation: PD.Boolean(true),
  35. linesSize: PD.Numeric(0.04, { min: 0.01, max: 5, step: 0.01 }),
  36. dashLength: PD.Numeric(0.04, { min: 0.01, max: 0.2, step: 0.01 }),
  37. }
  38. const VectorsParams = {
  39. ...LinesParams
  40. }
  41. type VectorsParams = typeof VectorsParams
  42. const ArcParams = {
  43. ...LinesParams
  44. }
  45. type ArcParams = typeof ArcParams
  46. const SectorParams = {
  47. ...Mesh.Params,
  48. ...SharedParams,
  49. ignoreLight: PD.Boolean(true),
  50. sectorOpacity: PD.Numeric(0.75, { min: 0, max: 1, step: 0.01 }),
  51. }
  52. type SectorParams = typeof SectorParams
  53. const TextParams = {
  54. ...Text.Params,
  55. borderWidth: PD.Numeric(0.2, { min: 0, max: 0.5, step: 0.01 }),
  56. textColor: PD.Color(ColorNames.black),
  57. textSize: PD.Numeric(0.4, { min: 0.1, max: 5, step: 0.1 }),
  58. }
  59. type TextParams = typeof TextParams
  60. const AngleVisuals = {
  61. 'vectors': (ctx: RepresentationContext, getParams: RepresentationParamsGetter<AngleData, VectorsParams>) => ShapeRepresentation(getVectorsShape, Lines.Utils, { modifyState: s => ({ ...s, pickable: false }) }),
  62. 'arc': (ctx: RepresentationContext, getParams: RepresentationParamsGetter<AngleData, ArcParams>) => ShapeRepresentation(getArcShape, Lines.Utils, { modifyState: s => ({ ...s, pickable: false }) }),
  63. 'sector': (ctx: RepresentationContext, getParams: RepresentationParamsGetter<AngleData, SectorParams>) => ShapeRepresentation(getSectorShape, Mesh.Utils, { modifyProps: p => ({ ...p, alpha: p.sectorOpacity }) }),
  64. 'text': (ctx: RepresentationContext, getParams: RepresentationParamsGetter<AngleData, TextParams>) => ShapeRepresentation(getTextShape, Text.Utils, { modifyState: s => ({ ...s, pickable: false }) }),
  65. }
  66. type AngleVisualName = keyof typeof AngleVisuals
  67. const AngleVisualOptions = Object.keys(AngleVisuals).map(name => [name, stringToWords(name)] as [AngleVisualName, string])
  68. export const AngleParams = {
  69. ...VectorsParams,
  70. ...ArcParams,
  71. ...SectorParams,
  72. ...TextParams,
  73. visuals: PD.MultiSelect<AngleVisualName>(['vectors', 'sector', 'text'], AngleVisualOptions),
  74. }
  75. export type AngleParams = typeof AngleParams
  76. export type AngleProps = PD.Values<AngleParams>
  77. //
  78. function getAngleState() {
  79. return {
  80. pointA: Vec3(),
  81. pointB: Vec3(),
  82. pointC: Vec3(),
  83. arcDirA: Vec3(),
  84. arcDirC: Vec3(),
  85. arcNormal: Vec3(),
  86. radius: 0,
  87. angle: 0,
  88. }
  89. }
  90. type AngleState = ReturnType<typeof getAngleState>
  91. const tmpVec = Vec3()
  92. const tmpMat = Mat4()
  93. function setAngleState(triple: Loci.Triple, state: AngleState, arcScale: number) {
  94. const { pointA, pointB, pointC } = state
  95. const { arcDirA, arcDirC, arcNormal } = state
  96. const { lociA, lociB, lociC } = triple
  97. Loci.getCenter(lociA, pointA)
  98. Loci.getCenter(lociB, pointB)
  99. Loci.getCenter(lociC, pointC)
  100. Vec3.sub(arcDirA, pointA, pointB)
  101. Vec3.sub(arcDirC, pointC, pointB)
  102. Vec3.cross(arcNormal, arcDirA, arcDirC)
  103. const len = Math.min(Vec3.magnitude(arcDirA), Vec3.magnitude(arcDirC))
  104. const radius = len * arcScale
  105. state.radius = radius
  106. state.angle = Vec3.angle(arcDirA, arcDirC)
  107. return state
  108. }
  109. function getCircle(state: AngleState, segmentLength?: number) {
  110. const { radius, angle } = state
  111. const segments = segmentLength ? arcLength(angle, radius) / segmentLength : 32
  112. Mat4.targetTo(tmpMat, state.pointB, state.pointA, state.arcNormal)
  113. Mat4.setTranslation(tmpMat, state.pointB)
  114. Mat4.mul(tmpMat, tmpMat, Mat4.rotY180)
  115. const circle = Circle({ radius, thetaLength: angle, segments })
  116. return transformPrimitive(circle, tmpMat)
  117. }
  118. const tmpState = getAngleState()
  119. function angleLabel(triple: Loci.Triple, arcScale: number) {
  120. setAngleState(triple, tmpState, arcScale)
  121. const angle = radToDeg(tmpState.angle).toFixed(2)
  122. return `Angle ${angle}\u00B0`
  123. }
  124. //
  125. function buildVectorsLines(data: AngleData, props: AngleProps, lines?: Lines): Lines {
  126. const builder = LinesBuilder.create(128, 64, lines)
  127. for (let i = 0, il = data.triples.length; i < il; ++i) {
  128. setAngleState(data.triples[i], tmpState, props.arcScale)
  129. builder.addFixedLengthDashes(tmpState.pointB, tmpState.pointA, props.dashLength, i)
  130. builder.addFixedLengthDashes(tmpState.pointB, tmpState.pointC, props.dashLength, i)
  131. }
  132. return builder.getLines()
  133. }
  134. function getVectorsShape(ctx: RuntimeContext, data: AngleData, props: AngleProps, shape?: Shape<Lines>) {
  135. const lines = buildVectorsLines(data, props, shape && shape.geometry);
  136. const getLabel = function (groupId: number ) {
  137. return angleLabel(data.triples[groupId], props.arcScale)
  138. }
  139. return Shape.create('Angle Vectors', data, lines, () => props.color, () => props.linesSize, getLabel)
  140. }
  141. //
  142. function buildArcLines(data: AngleData, props: AngleProps, lines?: Lines): Lines {
  143. const builder = LinesBuilder.create(128, 64, lines)
  144. for (let i = 0, il = data.triples.length; i < il; ++i) {
  145. setAngleState(data.triples[i], tmpState, props.arcScale)
  146. const circle = getCircle(tmpState, props.dashLength)
  147. const { indices, vertices } = circle
  148. for (let j = 0, jl = indices.length; j < jl; j += 3) {
  149. if (j % 2 === 1) continue // draw every other segment to get dashes
  150. const start = indices[j] * 3
  151. const end = indices[j + 1] * 3
  152. const startX = vertices[start]
  153. const startY = vertices[start + 1]
  154. const startZ = vertices[start + 2]
  155. const endX = vertices[end]
  156. const endY = vertices[end + 1]
  157. const endZ = vertices[end + 2]
  158. builder.add(startX, startY, startZ, endX, endY, endZ, i)
  159. }
  160. }
  161. return builder.getLines()
  162. }
  163. function getArcShape(ctx: RuntimeContext, data: AngleData, props: AngleProps, shape?: Shape<Lines>) {
  164. const lines = buildArcLines(data, props, shape && shape.geometry);
  165. const getLabel = function (groupId: number ) {
  166. return angleLabel(data.triples[groupId], props.arcScale)
  167. }
  168. return Shape.create('Angle Arc', data, lines, () => props.color, () => props.linesSize, getLabel)
  169. }
  170. //
  171. function buildSectorMesh(data: AngleData, props: AngleProps, mesh?: Mesh): Mesh {
  172. const state = MeshBuilder.createState(128, 64, mesh)
  173. for (let i = 0, il = data.triples.length; i < il; ++i) {
  174. setAngleState(data.triples[i], tmpState, props.arcScale)
  175. const circle = getCircle(tmpState)
  176. MeshBuilder.addPrimitive(state, Mat4.id, circle)
  177. MeshBuilder.addPrimitiveFlipped(state, Mat4.id, circle)
  178. }
  179. return MeshBuilder.getMesh(state)
  180. }
  181. function getSectorShape(ctx: RuntimeContext, data: AngleData, props: AngleProps, shape?: Shape<Mesh>) {
  182. const mesh = buildSectorMesh(data, props, shape && shape.geometry);
  183. const getLabel = function (groupId: number ) {
  184. return angleLabel(data.triples[groupId], props.arcScale)
  185. }
  186. return Shape.create('Angle Sector', data, mesh, () => props.color, () => 1, getLabel)
  187. }
  188. //
  189. function buildText(data: AngleData, props: AngleProps, text?: Text): Text {
  190. const builder = TextBuilder.create(props, 128, 64, text)
  191. for (let i = 0, il = data.triples.length; i < il; ++i) {
  192. setAngleState(data.triples[i], tmpState, props.arcScale)
  193. Vec3.add(tmpVec, tmpState.arcDirA, tmpState.arcDirC)
  194. Vec3.setMagnitude(tmpVec, tmpVec, tmpState.radius)
  195. Vec3.add(tmpVec, tmpState.pointB, tmpVec)
  196. const angle = radToDeg(tmpState.angle).toFixed(2)
  197. const label = `${angle}\u00B0`
  198. builder.add(label, tmpVec[0], tmpVec[1], tmpVec[2], 0.1, i)
  199. }
  200. return builder.getText()
  201. }
  202. function getTextShape(ctx: RuntimeContext, data: AngleData, props: AngleProps, shape?: Shape<Text>) {
  203. const text = buildText(data, props, shape && shape.geometry);
  204. const getLabel = function (groupId: number ) {
  205. return angleLabel(data.triples[groupId], props.arcScale)
  206. }
  207. return Shape.create('Angle Text', data, text, () => props.textColor, () => props.textSize, getLabel)
  208. }
  209. //
  210. export type AngleRepresentation = Representation<AngleData, AngleParams>
  211. export function AngleRepresentation(ctx: RepresentationContext, getParams: RepresentationParamsGetter<AngleData, AngleParams>): AngleRepresentation {
  212. return Representation.createMulti('Angle', ctx, getParams, Representation.StateBuilder, AngleVisuals as unknown as Representation.Def<AngleData, AngleParams>)
  213. }