link.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**
  2. * Copyright (c) 2018-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Vec3 } from '../../../../mol-math/linear-algebra';
  7. import { ParamDefinition as PD } from '../../../../mol-util/param-definition';
  8. import { Mesh } from '../../../../mol-geo/geometry/mesh/mesh';
  9. import { MeshBuilder } from '../../../../mol-geo/geometry/mesh/mesh-builder';
  10. import { CylinderProps } from '../../../../mol-geo/primitive/cylinder';
  11. import { addFixedCountDashedCylinder, addCylinder, addDoubleCylinder } from '../../../../mol-geo/geometry/mesh/builder/cylinder';
  12. import { VisualContext } from '../../../visual';
  13. import { BaseGeometry } from '../../../../mol-geo/geometry/base';
  14. import { Lines } from '../../../../mol-geo/geometry/lines/lines';
  15. import { LinesBuilder } from '../../../../mol-geo/geometry/lines/lines-builder';
  16. import { Cylinders } from '../../../../mol-geo/geometry/cylinders/cylinders';
  17. import { CylindersBuilder } from '../../../../mol-geo/geometry/cylinders/cylinders-builder';
  18. export const LinkCylinderParams = {
  19. linkScale: PD.Numeric(0.45, { min: 0, max: 1, step: 0.01 }),
  20. linkSpacing: PD.Numeric(1, { min: 0, max: 2, step: 0.01 }),
  21. linkCap: PD.Boolean(false),
  22. dashCount: PD.Numeric(4, { min: 2, max: 10, step: 2 }),
  23. dashScale: PD.Numeric(0.8, { min: 0, max: 2, step: 0.1 }),
  24. dashCap: PD.Boolean(true),
  25. stubCap: PD.Boolean(true),
  26. radialSegments: PD.Numeric(16, { min: 2, max: 56, step: 2 }, BaseGeometry.CustomQualityParamInfo),
  27. };
  28. export const DefaultLinkCylinderProps = PD.getDefaultValues(LinkCylinderParams);
  29. export type LinkCylinderProps = typeof DefaultLinkCylinderProps
  30. export const LinkLineParams = {
  31. linkScale: PD.Numeric(0.5, { min: 0, max: 1, step: 0.1 }),
  32. linkSpacing: PD.Numeric(0.1, { min: 0, max: 2, step: 0.01 }),
  33. dashCount: PD.Numeric(4, { min: 2, max: 10, step: 2 }),
  34. };
  35. export const DefaultLinkLineProps = PD.getDefaultValues(LinkLineParams);
  36. export type LinkLineProps = typeof DefaultLinkLineProps
  37. const tmpV12 = Vec3();
  38. const tmpShiftV12 = Vec3();
  39. const tmpShiftV13 = Vec3();
  40. const up = Vec3.create(0, 1, 0);
  41. /** Calculate 'shift' direction that is perpendiculat to v1 - v2 and goes through v3 */
  42. export function calculateShiftDir (out: Vec3, v1: Vec3, v2: Vec3, v3: Vec3 | null) {
  43. Vec3.normalize(tmpShiftV12, Vec3.sub(tmpShiftV12, v1, v2));
  44. if (v3 !== null) {
  45. Vec3.sub(tmpShiftV13, v1, v3);
  46. } else {
  47. Vec3.copy(tmpShiftV13, v1); // no reference point, use v1
  48. }
  49. Vec3.normalize(tmpShiftV13, tmpShiftV13);
  50. // ensure v13 and v12 are not colinear
  51. let dp = Vec3.dot(tmpShiftV12, tmpShiftV13);
  52. if (1 - Math.abs(dp) < 1e-5) {
  53. Vec3.set(tmpShiftV13, 1, 0, 0);
  54. dp = Vec3.dot(tmpShiftV12, tmpShiftV13);
  55. if (1 - Math.abs(dp) < 1e-5) {
  56. Vec3.set(tmpShiftV13, 0, 1, 0);
  57. dp = Vec3.dot(tmpShiftV12, tmpShiftV13);
  58. }
  59. }
  60. Vec3.setMagnitude(tmpShiftV12, tmpShiftV12, dp);
  61. Vec3.sub(tmpShiftV13, tmpShiftV13, tmpShiftV12);
  62. return Vec3.normalize(out, tmpShiftV13);
  63. }
  64. export interface LinkBuilderProps {
  65. linkCount: number
  66. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => void
  67. radius: (edgeIndex: number) => number,
  68. referencePosition?: (edgeIndex: number) => Vec3 | null
  69. style?: (edgeIndex: number) => LinkStyle
  70. ignore?: (edgeIndex: number) => boolean
  71. stub?: (edgeIndex: number) => boolean
  72. }
  73. export const enum LinkStyle {
  74. Solid = 0,
  75. Dashed = 1,
  76. Double = 2,
  77. Triple = 3,
  78. Disk = 4,
  79. Aromatic = 5,
  80. MirroredAromatic = 6,
  81. }
  82. // avoiding namespace lookup improved performance in Chrome (Aug 2020)
  83. const v3scale = Vec3.scale;
  84. const v3add = Vec3.add;
  85. const v3sub = Vec3.sub;
  86. const v3setMagnitude = Vec3.setMagnitude;
  87. const v3dot = Vec3.dot;
  88. /**
  89. * Each edge is included twice to allow for coloring/picking
  90. * the half closer to the first vertex, i.e. vertex a.
  91. */
  92. export function createLinkCylinderMesh(ctx: VisualContext, linkBuilder: LinkBuilderProps, props: LinkCylinderProps, mesh?: Mesh) {
  93. const { linkCount, referencePosition, position, style, radius, ignore, stub } = linkBuilder;
  94. if (!linkCount) return Mesh.createEmpty(mesh);
  95. const { linkScale, linkSpacing, radialSegments, linkCap, dashCount, dashScale, dashCap, stubCap } = props;
  96. const vertexCountEstimate = radialSegments * 2 * linkCount * 2;
  97. const builderState = MeshBuilder.createState(vertexCountEstimate, vertexCountEstimate / 4, mesh);
  98. const va = Vec3();
  99. const vb = Vec3();
  100. const vShift = Vec3();
  101. const cylinderProps: CylinderProps = {
  102. radiusTop: 1,
  103. radiusBottom: 1,
  104. radialSegments,
  105. topCap: linkCap,
  106. bottomCap: linkCap
  107. };
  108. const segmentCount = dashCount + 1;
  109. for (let edgeIndex = 0, _eI = linkCount; edgeIndex < _eI; ++edgeIndex) {
  110. if (ignore && ignore(edgeIndex)) continue;
  111. position(va, vb, edgeIndex);
  112. v3sub(tmpV12, vb, va);
  113. const linkRadius = radius(edgeIndex);
  114. const linkStyle = style ? style(edgeIndex) : LinkStyle.Solid;
  115. const linkStub = stubCap && (stub ? stub(edgeIndex) : false);
  116. const [topCap, bottomCap] = (v3dot(tmpV12, up) > 0) ? [linkStub, linkCap] : [linkCap, linkStub];
  117. builderState.currentGroup = edgeIndex;
  118. const aromaticOffsetFactor = 4.5;
  119. if (linkStyle === LinkStyle.Solid) {
  120. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius;
  121. cylinderProps.topCap = topCap;
  122. cylinderProps.bottomCap = bottomCap;
  123. addCylinder(builderState, va, vb, 0.5, cylinderProps);
  124. } else if (linkStyle === LinkStyle.Dashed) {
  125. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius * dashScale;
  126. cylinderProps.topCap = cylinderProps.bottomCap = dashCap;
  127. addFixedCountDashedCylinder(builderState, va, vb, 0.5, segmentCount, cylinderProps);
  128. } else if (linkStyle === LinkStyle.Double || linkStyle === LinkStyle.Triple || linkStyle === LinkStyle.Aromatic || linkStyle === LinkStyle.MirroredAromatic) {
  129. const order = linkStyle === LinkStyle.Double ? 2 :
  130. linkStyle === LinkStyle.Triple ? 3 : 1.5;
  131. const multiRadius = linkRadius * (linkScale / (0.5 * order));
  132. const absOffset = (linkRadius - multiRadius) * linkSpacing;
  133. calculateShiftDir(vShift, va, vb, referencePosition ? referencePosition(edgeIndex) : null);
  134. cylinderProps.topCap = topCap;
  135. cylinderProps.bottomCap = bottomCap;
  136. if (linkStyle === LinkStyle.Aromatic || linkStyle === LinkStyle.MirroredAromatic) {
  137. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius;
  138. addCylinder(builderState, va, vb, 0.5, cylinderProps);
  139. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius * linkScale;
  140. cylinderProps.topCap = cylinderProps.bottomCap = dashCap;
  141. v3setMagnitude(vShift, vShift, absOffset * aromaticOffsetFactor);
  142. v3sub(va, va, vShift);
  143. v3sub(vb, vb, vShift);
  144. addFixedCountDashedCylinder(builderState, va, vb, 0.5, 3, cylinderProps);
  145. if (linkStyle === LinkStyle.MirroredAromatic) {
  146. v3setMagnitude(vShift, vShift, absOffset * aromaticOffsetFactor * 2);
  147. v3add(va, va, vShift);
  148. v3add(vb, vb, vShift);
  149. addFixedCountDashedCylinder(builderState, va, vb, 0.5, 3, cylinderProps);
  150. }
  151. } else {
  152. v3setMagnitude(vShift, vShift, absOffset);
  153. cylinderProps.radiusTop = cylinderProps.radiusBottom = multiRadius;
  154. if (order === 3) addCylinder(builderState, va, vb, 0.5, cylinderProps);
  155. addDoubleCylinder(builderState, va, vb, 0.5, vShift, cylinderProps);
  156. }
  157. } else if (linkStyle === LinkStyle.Disk) {
  158. v3scale(tmpV12, tmpV12, 0.475);
  159. v3add(va, va, tmpV12);
  160. v3sub(vb, vb, tmpV12);
  161. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius;
  162. cylinderProps.topCap = topCap;
  163. cylinderProps.bottomCap = bottomCap;
  164. addCylinder(builderState, va, vb, 0.5, cylinderProps);
  165. }
  166. }
  167. return MeshBuilder.getMesh(builderState);
  168. }
  169. /**
  170. * Each edge is included twice to allow for coloring/picking
  171. * the half closer to the first vertex, i.e. vertex a.
  172. */
  173. export function createLinkCylinderImpostors(ctx: VisualContext, linkBuilder: LinkBuilderProps, props: LinkCylinderProps, cylinders?: Cylinders) {
  174. const { linkCount, referencePosition, position, style, radius, ignore, stub } = linkBuilder;
  175. if (!linkCount) return Cylinders.createEmpty(cylinders);
  176. const { linkScale, linkSpacing, linkCap, dashCount, dashScale, dashCap, stubCap } = props;
  177. const cylindersCountEstimate = linkCount * 2;
  178. const builder = CylindersBuilder.create(cylindersCountEstimate, cylindersCountEstimate / 4, cylinders);
  179. const va = Vec3();
  180. const vb = Vec3();
  181. const vm = Vec3();
  182. const vShift = Vec3();
  183. // automatically adjust length for evenly spaced dashed cylinders
  184. const segmentCount = dashCount % 2 === 1 ? dashCount : dashCount + 1;
  185. const lengthScale = 0.5 - (0.5 / 2 / segmentCount);
  186. const aromaticSegmentCount = 3;
  187. const aromaticLengthScale = 0.5 - (0.5 / 2 / aromaticSegmentCount);
  188. const aromaticOffsetFactor = 4.5;
  189. for (let edgeIndex = 0, _eI = linkCount; edgeIndex < _eI; ++edgeIndex) {
  190. if (ignore && ignore(edgeIndex)) continue;
  191. position(va, vb, edgeIndex);
  192. const linkRadius = radius(edgeIndex);
  193. const linkStyle = style ? style(edgeIndex) : LinkStyle.Solid;
  194. const linkStub = stubCap && (stub ? stub(edgeIndex) : false);
  195. if (linkStyle === LinkStyle.Solid) {
  196. v3scale(vm, v3add(vm, va, vb), 0.5);
  197. builder.add(va[0], va[1], va[2], vm[0], vm[1], vm[2], 1, linkCap, linkStub, edgeIndex);
  198. } else if (linkStyle === LinkStyle.Dashed) {
  199. v3scale(tmpV12, v3sub(tmpV12, vb, va), lengthScale);
  200. v3sub(vb, vb, tmpV12);
  201. builder.addFixedCountDashes(va, vb, segmentCount, dashScale, dashCap, dashCap, edgeIndex);
  202. } else if (linkStyle === LinkStyle.Double || linkStyle === LinkStyle.Triple || linkStyle === LinkStyle.Aromatic || linkStyle === LinkStyle.MirroredAromatic) {
  203. const order = linkStyle === LinkStyle.Double ? 2 :
  204. linkStyle === LinkStyle.Triple ? 3 : 1.5;
  205. const multiScale = linkScale / (0.5 * order);
  206. const absOffset = (linkRadius - multiScale * linkRadius) * linkSpacing;
  207. v3scale(vm, v3add(vm, va, vb), 0.5);
  208. calculateShiftDir(vShift, va, vb, referencePosition ? referencePosition(edgeIndex) : null);
  209. if (linkStyle === LinkStyle.Aromatic || linkStyle === LinkStyle.MirroredAromatic) {
  210. builder.add(va[0], va[1], va[2], vm[0], vm[1], vm[2], 1, linkCap, linkStub, edgeIndex);
  211. v3scale(tmpV12, v3sub(tmpV12, vb, va), aromaticLengthScale);
  212. v3sub(vb, vb, tmpV12);
  213. v3setMagnitude(vShift, vShift, absOffset * aromaticOffsetFactor);
  214. v3sub(va, va, vShift);
  215. v3sub(vb, vb, vShift);
  216. builder.addFixedCountDashes(va, vb, aromaticSegmentCount, linkScale, dashCap, dashCap, edgeIndex);
  217. if (linkStyle === LinkStyle.MirroredAromatic) {
  218. v3setMagnitude(vShift, vShift, absOffset * aromaticOffsetFactor * 2);
  219. v3add(va, va, vShift);
  220. v3add(vb, vb, vShift);
  221. builder.addFixedCountDashes(va, vb, aromaticSegmentCount, linkScale, dashCap, dashCap, edgeIndex);
  222. }
  223. } else {
  224. v3setMagnitude(vShift, vShift, absOffset);
  225. if (order === 3) builder.add(va[0], va[1], va[2], vm[0], vm[1], vm[2], multiScale, linkCap, false, edgeIndex);
  226. builder.add(va[0] + vShift[0], va[1] + vShift[1], va[2] + vShift[2], vm[0] + vShift[0], vm[1] + vShift[1], vm[2] + vShift[2], multiScale, linkCap, linkStub, edgeIndex);
  227. builder.add(va[0] - vShift[0], va[1] - vShift[1], va[2] - vShift[2], vm[0] - vShift[0], vm[1] - vShift[1], vm[2] - vShift[2], multiScale, linkCap, linkStub, edgeIndex);
  228. }
  229. } else if (linkStyle === LinkStyle.Disk) {
  230. v3scale(tmpV12, v3sub(tmpV12, vb, va), 0.475);
  231. v3add(va, va, tmpV12);
  232. v3sub(vb, vb, tmpV12);
  233. builder.add(va[0], va[1], va[2], vb[0], vb[1], vb[2], 1, linkCap, linkStub, edgeIndex);
  234. }
  235. }
  236. return builder.getCylinders();
  237. }
  238. /**
  239. * Each edge is included twice to allow for coloring/picking
  240. * the half closer to the first vertex, i.e. vertex a.
  241. */
  242. export function createLinkLines(ctx: VisualContext, linkBuilder: LinkBuilderProps, props: LinkLineProps, lines?: Lines) {
  243. const { linkCount, referencePosition, position, style, ignore } = linkBuilder;
  244. if (!linkCount) return Lines.createEmpty(lines);
  245. const { linkScale, linkSpacing, dashCount } = props;
  246. const linesCountEstimate = linkCount * 2;
  247. const builder = LinesBuilder.create(linesCountEstimate, linesCountEstimate / 4, lines);
  248. const va = Vec3();
  249. const vb = Vec3();
  250. const vm = Vec3();
  251. const vShift = Vec3();
  252. // automatically adjust length for evenly spaced dashed lines
  253. const segmentCount = dashCount % 2 === 1 ? dashCount : dashCount + 1;
  254. const lengthScale = 0.5 - (0.5 / 2 / segmentCount);
  255. const aromaticSegmentCount = 3;
  256. const aromaticLengthScale = 0.5 - (0.5 / 2 / aromaticSegmentCount);
  257. const aromaticOffsetFactor = 4.5;
  258. for (let edgeIndex = 0, _eI = linkCount; edgeIndex < _eI; ++edgeIndex) {
  259. if (ignore && ignore(edgeIndex)) continue;
  260. position(va, vb, edgeIndex);
  261. const linkStyle = style ? style(edgeIndex) : LinkStyle.Solid;
  262. if (linkStyle === LinkStyle.Solid) {
  263. v3scale(vm, v3add(vm, va, vb), 0.5);
  264. builder.add(va[0], va[1], va[2], vm[0], vm[1], vm[2], edgeIndex);
  265. } else if (linkStyle === LinkStyle.Dashed) {
  266. v3scale(tmpV12, v3sub(tmpV12, vb, va), lengthScale);
  267. v3sub(vb, vb, tmpV12);
  268. builder.addFixedCountDashes(va, vb, segmentCount, edgeIndex);
  269. } else if (linkStyle === LinkStyle.Double || linkStyle === LinkStyle.Triple || linkStyle === LinkStyle.Aromatic || linkStyle === LinkStyle.MirroredAromatic) {
  270. const order = linkStyle === LinkStyle.Double ? 2 :
  271. linkStyle === LinkStyle.Triple ? 3 : 1.5;
  272. const multiRadius = 1 * (linkScale / (0.5 * order));
  273. const absOffset = (1 - multiRadius) * linkSpacing;
  274. v3scale(vm, v3add(vm, va, vb), 0.5);
  275. calculateShiftDir(vShift, va, vb, referencePosition ? referencePosition(edgeIndex) : null);
  276. if (linkStyle === LinkStyle.Aromatic || linkStyle === LinkStyle.MirroredAromatic) {
  277. builder.add(va[0], va[1], va[2], vm[0], vm[1], vm[2], edgeIndex);
  278. v3scale(tmpV12, v3sub(tmpV12, vb, va), aromaticLengthScale);
  279. v3sub(vb, vb, tmpV12);
  280. v3setMagnitude(vShift, vShift, absOffset * aromaticOffsetFactor);
  281. v3sub(va, va, vShift);
  282. v3sub(vb, vb, vShift);
  283. builder.addFixedCountDashes(va, vb, aromaticSegmentCount, edgeIndex);
  284. if (linkStyle === LinkStyle.MirroredAromatic) {
  285. v3setMagnitude(vShift, vShift, absOffset * aromaticOffsetFactor * 2);
  286. v3add(va, va, vShift);
  287. v3add(vb, vb, vShift);
  288. builder.addFixedCountDashes(va, vb, aromaticSegmentCount, edgeIndex);
  289. }
  290. } else {
  291. v3setMagnitude(vShift, vShift, absOffset);
  292. if (order === 3) builder.add(va[0], va[1], va[2], vm[0], vm[1], vm[2], edgeIndex);
  293. builder.add(va[0] + vShift[0], va[1] + vShift[1], va[2] + vShift[2], vm[0] + vShift[0], vm[1] + vShift[1], vm[2] + vShift[2], edgeIndex);
  294. builder.add(va[0] - vShift[0], va[1] - vShift[1], va[2] - vShift[2], vm[0] - vShift[0], vm[1] - vShift[1], vm[2] - vShift[2], edgeIndex);
  295. }
  296. } else if (linkStyle === LinkStyle.Disk) {
  297. v3scale(tmpV12, v3sub(tmpV12, vb, va), 0.475);
  298. v3add(va, va, tmpV12);
  299. v3sub(vb, vb, tmpV12);
  300. // TODO what to do here? Line as disk doesn't work well.
  301. builder.add(va[0], va[1], va[2], vb[0], vb[1], vb[2], edgeIndex);
  302. }
  303. }
  304. return builder.getLines();
  305. }