link.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.4, { min: 0, max: 1, step: 0.1 }),
  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. }
  80. // avoiding namespace lookup improved performance in Chrome (Aug 2020)
  81. const v3scale = Vec3.scale;
  82. const v3add = Vec3.add;
  83. const v3sub = Vec3.sub;
  84. const v3setMagnitude = Vec3.setMagnitude;
  85. const v3dot = Vec3.dot;
  86. /**
  87. * Each edge is included twice to allow for coloring/picking
  88. * the half closer to the first vertex, i.e. vertex a.
  89. */
  90. export function createLinkCylinderMesh(ctx: VisualContext, linkBuilder: LinkBuilderProps, props: LinkCylinderProps, mesh?: Mesh) {
  91. const { linkCount, referencePosition, position, style, radius, ignore, stub } = linkBuilder;
  92. if (!linkCount) return Mesh.createEmpty(mesh);
  93. const { linkScale, linkSpacing, radialSegments, linkCap, dashCount, dashScale, dashCap, stubCap } = props;
  94. const vertexCountEstimate = radialSegments * 2 * linkCount * 2;
  95. const builderState = MeshBuilder.createState(vertexCountEstimate, vertexCountEstimate / 4, mesh);
  96. const va = Vec3();
  97. const vb = Vec3();
  98. const vShift = Vec3();
  99. const cylinderProps: CylinderProps = {
  100. radiusTop: 1,
  101. radiusBottom: 1,
  102. radialSegments,
  103. topCap: linkCap,
  104. bottomCap: linkCap
  105. };
  106. const segmentCount = dashCount + 1;
  107. for (let edgeIndex = 0, _eI = linkCount; edgeIndex < _eI; ++edgeIndex) {
  108. if (ignore && ignore(edgeIndex)) continue;
  109. position(va, vb, edgeIndex);
  110. v3sub(tmpV12, vb, va);
  111. const linkRadius = radius(edgeIndex);
  112. const linkStyle = style ? style(edgeIndex) : LinkStyle.Solid;
  113. const linkStub = stubCap && (stub ? stub(edgeIndex) : false);
  114. const [topCap, bottomCap] = (v3dot(tmpV12, up) > 0) ? [linkStub, linkCap] : [linkCap, linkStub];
  115. builderState.currentGroup = edgeIndex;
  116. if (linkStyle === LinkStyle.Solid) {
  117. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius;
  118. cylinderProps.topCap = topCap;
  119. cylinderProps.bottomCap = bottomCap;
  120. addCylinder(builderState, va, vb, 0.5, cylinderProps);
  121. } else if (linkStyle === LinkStyle.Dashed) {
  122. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius * dashScale;
  123. cylinderProps.topCap = cylinderProps.bottomCap = dashCap;
  124. addFixedCountDashedCylinder(builderState, va, vb, 0.5, segmentCount, cylinderProps);
  125. } else if (linkStyle === LinkStyle.Double || linkStyle === LinkStyle.Triple) {
  126. const order = linkStyle === LinkStyle.Double ? 2 : 3;
  127. const multiRadius = linkRadius * (linkScale / (0.5 * order));
  128. const absOffset = (linkRadius - multiRadius) * linkSpacing;
  129. calculateShiftDir(vShift, va, vb, referencePosition ? referencePosition(edgeIndex) : null);
  130. v3setMagnitude(vShift, vShift, absOffset);
  131. cylinderProps.radiusTop = cylinderProps.radiusBottom = multiRadius;
  132. cylinderProps.topCap = topCap;
  133. cylinderProps.bottomCap = bottomCap;
  134. if (order === 3) addCylinder(builderState, va, vb, 0.5, cylinderProps);
  135. addDoubleCylinder(builderState, va, vb, 0.5, vShift, cylinderProps);
  136. } else if (linkStyle === LinkStyle.Disk) {
  137. v3scale(tmpV12, tmpV12, 0.475);
  138. v3add(va, va, tmpV12);
  139. v3sub(vb, vb, tmpV12);
  140. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius;
  141. cylinderProps.topCap = topCap;
  142. cylinderProps.bottomCap = bottomCap;
  143. addCylinder(builderState, va, vb, 0.5, cylinderProps);
  144. }
  145. }
  146. return MeshBuilder.getMesh(builderState);
  147. }
  148. /**
  149. * Each edge is included twice to allow for coloring/picking
  150. * the half closer to the first vertex, i.e. vertex a.
  151. */
  152. export function createLinkCylinderImpostors(ctx: VisualContext, linkBuilder: LinkBuilderProps, props: LinkCylinderProps, cylinders?: Cylinders) {
  153. const { linkCount, referencePosition, position, style, radius, ignore, stub } = linkBuilder;
  154. if (!linkCount) return Cylinders.createEmpty(cylinders);
  155. const { linkScale, linkSpacing, linkCap, dashCount, dashScale, dashCap, stubCap } = props;
  156. const cylindersCountEstimate = linkCount * 2;
  157. const builder = CylindersBuilder.create(cylindersCountEstimate, cylindersCountEstimate / 4, cylinders);
  158. const va = Vec3();
  159. const vb = Vec3();
  160. const vShift = Vec3();
  161. // automatically adjust length for evenly spaced dashed cylinders
  162. const segmentCount = dashCount % 2 === 1 ? dashCount : dashCount + 1;
  163. const lengthScale = 0.5 - (0.5 / 2 / segmentCount);
  164. for (let edgeIndex = 0, _eI = linkCount; edgeIndex < _eI; ++edgeIndex) {
  165. if (ignore && ignore(edgeIndex)) continue;
  166. position(va, vb, edgeIndex);
  167. const linkRadius = radius(edgeIndex);
  168. const linkStyle = style ? style(edgeIndex) : LinkStyle.Solid;
  169. const linkStub = stubCap && (stub ? stub(edgeIndex) : false);
  170. if (linkStyle === LinkStyle.Solid) {
  171. v3scale(vb, v3add(vb, va, vb), 0.5);
  172. builder.add(va[0], va[1], va[2], vb[0], vb[1], vb[2], 1, linkCap, linkStub, edgeIndex);
  173. } else if (linkStyle === LinkStyle.Dashed) {
  174. v3scale(tmpV12, v3sub(tmpV12, vb, va), lengthScale);
  175. v3sub(vb, vb, tmpV12);
  176. builder.addFixedCountDashes(va, vb, segmentCount, dashScale, dashCap, dashCap, edgeIndex);
  177. } else if (linkStyle === LinkStyle.Double || linkStyle === LinkStyle.Triple) {
  178. v3scale(vb, v3add(vb, va, vb), 0.5);
  179. const order = linkStyle === LinkStyle.Double ? 2 : 3;
  180. const multiScale = linkScale / (0.5 * order);
  181. const absOffset = (linkRadius - multiScale * linkRadius) * linkSpacing;
  182. calculateShiftDir(vShift, va, vb, referencePosition ? referencePosition(edgeIndex) : null);
  183. v3setMagnitude(vShift, vShift, absOffset);
  184. if (order === 3) builder.add(va[0], va[1], va[2], vb[0], vb[1], vb[2], multiScale, linkCap, false, edgeIndex);
  185. builder.add(va[0] + vShift[0], va[1] + vShift[1], va[2] + vShift[2], vb[0] + vShift[0], vb[1] + vShift[1], vb[2] + vShift[2], multiScale, linkCap, linkStub, edgeIndex);
  186. builder.add(va[0] - vShift[0], va[1] - vShift[1], va[2] - vShift[2], vb[0] - vShift[0], vb[1] - vShift[1], vb[2] - vShift[2], multiScale, linkCap, linkStub, edgeIndex);
  187. } else if (linkStyle === LinkStyle.Disk) {
  188. v3scale(tmpV12, v3sub(tmpV12, vb, va), 0.475);
  189. v3add(va, va, tmpV12);
  190. v3sub(vb, vb, tmpV12);
  191. builder.add(va[0], va[1], va[2], vb[0], vb[1], vb[2], 1, linkCap, linkStub, edgeIndex);
  192. }
  193. }
  194. return builder.getCylinders();
  195. }
  196. /**
  197. * Each edge is included twice to allow for coloring/picking
  198. * the half closer to the first vertex, i.e. vertex a.
  199. */
  200. export function createLinkLines(ctx: VisualContext, linkBuilder: LinkBuilderProps, props: LinkLineProps, lines?: Lines) {
  201. const { linkCount, referencePosition, position, style, ignore } = linkBuilder;
  202. if (!linkCount) return Lines.createEmpty(lines);
  203. const { linkScale, linkSpacing, dashCount } = props;
  204. const linesCountEstimate = linkCount * 2;
  205. const builder = LinesBuilder.create(linesCountEstimate, linesCountEstimate / 4, lines);
  206. const va = Vec3();
  207. const vb = Vec3();
  208. const vShift = Vec3();
  209. // automatically adjust length for evenly spaced dashed lines
  210. const segmentCount = dashCount % 2 === 1 ? dashCount : dashCount + 1;
  211. const lengthScale = 0.5 - (0.5 / 2 / segmentCount);
  212. for (let edgeIndex = 0, _eI = linkCount; edgeIndex < _eI; ++edgeIndex) {
  213. if (ignore && ignore(edgeIndex)) continue;
  214. position(va, vb, edgeIndex);
  215. const linkStyle = style ? style(edgeIndex) : LinkStyle.Solid;
  216. if (linkStyle === LinkStyle.Solid) {
  217. v3scale(vb, v3add(vb, va, vb), 0.5);
  218. builder.add(va[0], va[1], va[2], vb[0], vb[1], vb[2], edgeIndex);
  219. } else if (linkStyle === LinkStyle.Dashed) {
  220. v3scale(tmpV12, v3sub(tmpV12, vb, va), lengthScale);
  221. v3sub(vb, vb, tmpV12);
  222. builder.addFixedCountDashes(va, vb, segmentCount, edgeIndex);
  223. } else if (linkStyle === LinkStyle.Double || linkStyle === LinkStyle.Triple) {
  224. v3scale(vb, v3add(vb, va, vb), 0.5);
  225. const order = linkStyle === LinkStyle.Double ? 2 : 3;
  226. const multiRadius = 1 * (linkScale / (0.5 * order));
  227. const absOffset = (1 - multiRadius) * linkSpacing;
  228. calculateShiftDir(vShift, va, vb, referencePosition ? referencePosition(edgeIndex) : null);
  229. v3setMagnitude(vShift, vShift, absOffset);
  230. if (order === 3) builder.add(va[0], va[1], va[2], vb[0], vb[1], vb[2], edgeIndex);
  231. builder.add(va[0] + vShift[0], va[1] + vShift[1], va[2] + vShift[2], vb[0] + vShift[0], vb[1] + vShift[1], vb[2] + vShift[2], edgeIndex);
  232. builder.add(va[0] - vShift[0], va[1] - vShift[1], va[2] - vShift[2], vb[0] - vShift[0], vb[1] - vShift[1], vb[2] - vShift[2], edgeIndex);
  233. } else if (linkStyle === LinkStyle.Disk) {
  234. v3scale(tmpV12, v3sub(tmpV12, vb, va), 0.475);
  235. v3add(va, va, tmpV12);
  236. v3sub(vb, vb, tmpV12);
  237. // TODO what to do here? Line as disk doesn't work well.
  238. builder.add(va[0], va[1], va[2], vb[0], vb[1], vb[2], edgeIndex);
  239. }
  240. }
  241. return builder.getLines();
  242. }