NGL@1.0.0-beta.7 Home Manual Reference Source Gallery

src/representation/trajectory-representation.js

  1. /**
  2. * @file Trajectory Representation
  3. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  4. * @private
  5. */
  6.  
  7. import { Color } from '../../lib/three.es6.js'
  8.  
  9. import { defaults } from '../utils.js'
  10. import { uniformArray, uniformArray3 } from '../math/array-utils.js'
  11.  
  12. import StructureRepresentation from './structure-representation.js'
  13.  
  14. import SphereBuffer from '../buffer/sphere-buffer.js'
  15. import CylinderBuffer from '../buffer/cylinder-buffer.js'
  16. import PointBuffer from '../buffer/point-buffer.js'
  17. import LineBuffer from '../buffer/line-buffer.js'
  18.  
  19. /**
  20. * Trajectory representation parameter object.
  21. * @typedef {Object} TrajectoryRepresentationParameters - parameters
  22. *
  23. * @property {Boolean} drawLine - draw lines
  24. * @property {Boolean} drawCylinder - draw cylinders
  25. * @property {Boolean} drawPoint - draw points
  26. * @property {Boolean} drawSphere - draw sphere
  27. * @property {Integer} linewidth - line width
  28. * @property {Integer} pointSize - point size
  29. * @property {Boolean} sizeAttenuation - size attenuation
  30. * @property {Boolean} sort - sort flag for points
  31. */
  32.  
  33. /**
  34. * Trajectory representation
  35. */
  36. class TrajectoryRepresentation extends StructureRepresentation {
  37. /**
  38. * @param {Trajectory} trajectory - the trajectory
  39. * @param {Viewer} viewer - viewer object
  40. * @param {TrajectoryRepresentationParameters} params - parameters
  41. */
  42. constructor (trajectory, viewer, params) {
  43. super(trajectory.structure, viewer, params)
  44.  
  45. this.type = 'trajectory'
  46.  
  47. this.parameters = Object.assign({
  48. drawLine: {
  49. type: 'boolean', rebuild: true
  50. },
  51. drawCylinder: {
  52. type: 'boolean', rebuild: true
  53. },
  54. drawPoint: {
  55. type: 'boolean', rebuild: true
  56. },
  57. drawSphere: {
  58. type: 'boolean', rebuild: true
  59. },
  60.  
  61. linewidth: {
  62. type: 'integer', max: 20, min: 1, rebuild: true
  63. },
  64. pointSize: {
  65. type: 'integer', max: 20, min: 1, rebuild: true
  66. },
  67. sizeAttenuation: {
  68. type: 'boolean', rebuild: true
  69. },
  70. sort: {
  71. type: 'boolean', rebuild: true
  72. }
  73. }, this.parameters)
  74.  
  75. this.manualAttach = true
  76. this.trajectory = trajectory
  77.  
  78. this.init(params)
  79. }
  80.  
  81. init (params) {
  82. var p = params || {}
  83. p.colorScheme = defaults(p.colorScheme, 'uniform')
  84. p.colorValue = defaults(p.colorValue, 0xDDDDDD)
  85.  
  86. this.drawLine = defaults(p.drawLine, true)
  87. this.drawCylinder = defaults(p.drawCylinder, false)
  88. this.drawPoint = defaults(p.drawPoint, false)
  89. this.drawSphere = defaults(p.drawSphere, false)
  90.  
  91. this.pointSize = defaults(p.pointSize, 1)
  92. this.sizeAttenuation = defaults(p.sizeAttenuation, false)
  93. this.sort = defaults(p.sort, true)
  94.  
  95. super.init(p)
  96. }
  97.  
  98. attach () {
  99. this.bufferList.forEach(buffer => this.viewer.add(buffer))
  100. this.setVisibility(this.visible)
  101. this.tasks.decrement()
  102. }
  103.  
  104. // prepare (callback) {
  105. // // TODO
  106. // // - move loading of path here
  107. // // - get rid of manualAttach
  108. // callback()
  109. // }
  110.  
  111. create () {
  112. console.log('create', this.structureView.atomCount)
  113. if (this.structureView.atomCount === 0) return
  114.  
  115. var index = this.structureView.getAtomIndices()[ 0 ]
  116.  
  117. this.trajectory.getPath(index, path => {
  118. var n = path.length / 3
  119. var tc = new Color(this.colorValue)
  120.  
  121. if (this.drawSphere) {
  122. var sphereBuffer = new SphereBuffer(
  123. {
  124. position: path,
  125. color: uniformArray3(n, tc.r, tc.g, tc.b),
  126. radius: uniformArray(n, 0.2)
  127. },
  128. this.getBufferParams({
  129. sphereDetail: this.sphereDetail,
  130. dullInterior: true,
  131. disableImpostor: this.disableImpostor
  132. })
  133. )
  134.  
  135. this.bufferList.push(sphereBuffer)
  136. }
  137.  
  138. if (this.drawCylinder) {
  139. var cylinderBuffer = new CylinderBuffer(
  140. {
  141. position1: path.subarray(0, -3),
  142. position2: path.subarray(3),
  143. color: uniformArray3(n - 1, tc.r, tc.g, tc.b),
  144. color2: uniformArray3(n - 1, tc.r, tc.g, tc.b),
  145. radius: uniformArray(n, 0.05)
  146. },
  147. this.getBufferParams({
  148. openEnded: false,
  149. radialSegments: this.radialSegments,
  150. disableImpostor: this.disableImpostor,
  151. dullInterior: true
  152. })
  153. )
  154.  
  155. this.bufferList.push(cylinderBuffer)
  156. }
  157.  
  158. if (this.drawPoint) {
  159. var pointBuffer = new PointBuffer(
  160. {
  161. position: path,
  162. color: uniformArray3(n, tc.r, tc.g, tc.b)
  163. },
  164. this.getBufferParams({
  165. pointSize: this.pointSize,
  166. sizeAttenuation: this.sizeAttenuation,
  167. sort: this.sort
  168. })
  169. )
  170.  
  171. this.bufferList.push(pointBuffer)
  172. }
  173.  
  174. if (this.drawLine) {
  175. var lineBuffer = new LineBuffer(
  176. {
  177. position1: path.subarray(0, -3),
  178. position2: path.subarray(3),
  179. color: uniformArray3(n - 1, tc.r, tc.g, tc.b),
  180. color2: uniformArray3(n - 1, tc.r, tc.g, tc.b)
  181. },
  182. this.getBufferParams()
  183. )
  184.  
  185. this.bufferList.push(lineBuffer)
  186. }
  187.  
  188. this.attach()
  189. })
  190. }
  191. }
  192.  
  193. export default TrajectoryRepresentation