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

src/representation/cartoon-representation.js

  1. /**
  2. * @file Cartoon Representation
  3. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  4. * @private
  5. */
  6.  
  7. import { defaults } from '../utils.js'
  8. import { Debug, Log, RepresentationRegistry } from '../globals.js'
  9. import Spline from '../geometry/spline.js'
  10. import StructureRepresentation from './structure-representation.js'
  11. import TubeMeshBuffer from '../buffer/tubemesh-buffer.js'
  12.  
  13. /**
  14. * Cartoon representation. Show a thick ribbon that
  15. * smoothly connecting backbone atoms in polymers.
  16. *
  17. * __Name:__ _cartoon_
  18. *
  19. * @example
  20. * stage.loadFile( "rcsb://1crn" ).then( function( o ){
  21. * o.addRepresentation( "cartoon" );
  22. * o.autoView();
  23. * } );
  24. */
  25. class CartoonRepresentation extends StructureRepresentation {
  26. /**
  27. * Create Cartoon representation object
  28. * @param {Structure} structure - the structure to be represented
  29. * @param {Viewer} viewer - a viewer object
  30. * @param {StructureRepresentationParameters} params - representation parameters
  31. */
  32. constructor (structure, viewer, params) {
  33. super(structure, viewer, params)
  34.  
  35. this.type = 'cartoon'
  36.  
  37. this.parameters = Object.assign({
  38.  
  39. aspectRatio: {
  40. type: 'number', precision: 1, max: 10.0, min: 1.0
  41. },
  42. subdiv: {
  43. type: 'integer', max: 50, min: 1, rebuild: true
  44. },
  45. radialSegments: {
  46. type: 'integer', max: 50, min: 1, rebuild: true
  47. },
  48. tension: {
  49. type: 'number', precision: 1, max: 1.0, min: 0.1
  50. },
  51. capped: {
  52. type: 'boolean', rebuild: true
  53. },
  54. smoothSheet: {
  55. type: 'boolean', rebuild: true
  56. }
  57.  
  58. }, this.parameters)
  59.  
  60. this.init(params)
  61. }
  62.  
  63. init (params) {
  64. var p = params || {}
  65. p.colorScheme = defaults(p.colorScheme, 'chainname')
  66. p.colorScale = defaults(p.colorScale, 'RdYlBu')
  67. p.radius = defaults(p.radius, 'sstruc')
  68. p.scale = defaults(p.scale, 0.7)
  69.  
  70. this.aspectRatio = defaults(p.aspectRatio, 5.0)
  71. this.tension = defaults(p.tension, NaN)
  72. this.capped = defaults(p.capped, true)
  73. this.smoothSheet = defaults(p.smoothSheet, false)
  74.  
  75. if (p.quality === 'low') {
  76. this.subdiv = 3
  77. this.radialSegments = 6
  78. } else if (p.quality === 'medium') {
  79. this.subdiv = 6
  80. } else if (p.quality === 'high') {
  81. this.subdiv = 12
  82. } else {
  83. this.subdiv = defaults(p.subdiv, 6)
  84. }
  85.  
  86. super.init(p)
  87. }
  88.  
  89. getSplineParams (params) {
  90. return Object.assign({
  91. subdiv: this.subdiv,
  92. tension: this.tension,
  93. directional: this.aspectRatio !== 1.0,
  94. smoothSheet: this.smoothSheet
  95. }, params)
  96. }
  97.  
  98. getSpline (polymer) {
  99. return new Spline(polymer, this.getSplineParams())
  100. }
  101.  
  102. getScale (polymer) {
  103. return polymer.isCg() ? this.scale * this.aspectRatio : this.scale
  104. }
  105.  
  106. getAspectRatio (polymer) {
  107. return polymer.isCg() ? 1.0 : this.aspectRatio
  108. }
  109.  
  110. createData (sview) {
  111. var bufferList = []
  112. var polymerList = []
  113.  
  114. this.structure.eachPolymer(polymer => {
  115. if (polymer.residueCount < 4) return
  116. polymerList.push(polymer)
  117.  
  118. var spline = this.getSpline(polymer)
  119.  
  120. var subPos = spline.getSubdividedPosition()
  121. var subOri = spline.getSubdividedOrientation()
  122. var subCol = spline.getSubdividedColor(this.getColorParams())
  123. var subPick = spline.getSubdividedPicking()
  124. var subSize = spline.getSubdividedSize(this.radius, this.getScale(polymer))
  125.  
  126. bufferList.push(
  127. new TubeMeshBuffer(
  128. Object.assign({}, subPos, subOri, subCol, subPick, subSize),
  129. this.getBufferParams({
  130. radialSegments: this.radialSegments,
  131. aspectRatio: this.getAspectRatio(polymer),
  132. capped: this.capped,
  133. dullInterior: true
  134. })
  135. )
  136. )
  137. }, sview.getSelection())
  138.  
  139. return {
  140. bufferList: bufferList,
  141. polymerList: polymerList
  142. }
  143. }
  144.  
  145. updateData (what, data) {
  146. if (Debug) Log.time(this.type + ' repr update')
  147.  
  148. what = what || {}
  149.  
  150. for (var i = 0, il = data.polymerList.length; i < il; ++i) {
  151. var bufferData = {}
  152. var polymer = data.polymerList[ i ]
  153. var spline = this.getSpline(polymer)
  154.  
  155. data.bufferList[ i ].aspectRatio = this.getAspectRatio(polymer)
  156.  
  157. if (what.position || what.radius) {
  158. var subPos = spline.getSubdividedPosition()
  159. var subOri = spline.getSubdividedOrientation()
  160. var subSize = spline.getSubdividedSize(this.radius, this.getScale(polymer))
  161.  
  162. bufferData.position = subPos.position
  163. bufferData.normal = subOri.normal
  164. bufferData.binormal = subOri.binormal
  165. bufferData.tangent = subOri.tangent
  166. bufferData.size = subSize.size
  167. }
  168.  
  169. if (what.color) {
  170. var subCol = spline.getSubdividedColor(this.getColorParams())
  171. bufferData.color = subCol.color
  172. }
  173.  
  174. if (what.picking) {
  175. var subPick = spline.getSubdividedPicking()
  176. bufferData.picking = subPick.picking
  177. }
  178.  
  179. data.bufferList[ i ].setAttributes(bufferData)
  180. }
  181.  
  182. if (Debug) Log.timeEnd(this.type + ' repr update')
  183. }
  184.  
  185. setParameters (params) {
  186. var rebuild = false
  187. var what = {}
  188.  
  189. if (params && params.aspectRatio) {
  190. what.radius = true
  191. }
  192.  
  193. if (params && params.tension) {
  194. what.position = true
  195. }
  196.  
  197. super.setParameters(params, what, rebuild)
  198.  
  199. return this
  200. }
  201. }
  202.  
  203. RepresentationRegistry.add('cartoon', CartoonRepresentation)
  204.  
  205. export default CartoonRepresentation