link.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. * @author Zhenyu Zhang <jump2cn@gmail.com>
  6. * @author Gianluca Tomasello <giagitom@gmail.com>
  7. */
  8. import { Vec3 } from '../../../../mol-math/linear-algebra';
  9. import { ParamDefinition as PD } from '../../../../mol-util/param-definition';
  10. import { Mesh } from '../../../../mol-geo/geometry/mesh/mesh';
  11. import { MeshBuilder } from '../../../../mol-geo/geometry/mesh/mesh-builder';
  12. import { CylinderProps } from '../../../../mol-geo/primitive/cylinder';
  13. import { addFixedCountDashedCylinder, addCylinder, addDoubleCylinder } from '../../../../mol-geo/geometry/mesh/builder/cylinder';
  14. import { VisualContext } from '../../../visual';
  15. import { BaseGeometry } from '../../../../mol-geo/geometry/base';
  16. import { Lines } from '../../../../mol-geo/geometry/lines/lines';
  17. import { LinesBuilder } from '../../../../mol-geo/geometry/lines/lines-builder';
  18. import { Cylinders } from '../../../../mol-geo/geometry/cylinders/cylinders';
  19. import { CylindersBuilder } from '../../../../mol-geo/geometry/cylinders/cylinders-builder';
  20. import { Sphere3D } from '../../../../mol-math/geometry/primitives/sphere3d';
  21. export const LinkCylinderParams = {
  22. linkScale: PD.Numeric(0.45, { min: 0, max: 1, step: 0.01 }),
  23. linkSpacing: PD.Numeric(1, { min: 0, max: 2, step: 0.01 }),
  24. linkCap: PD.Boolean(false),
  25. aromaticScale: PD.Numeric(0.3, { min: 0, max: 1, step: 0.01 }),
  26. aromaticSpacing: PD.Numeric(1.5, { min: 0, max: 3, step: 0.01 }),
  27. aromaticDashCount: PD.Numeric(2, { min: 1, max: 6, step: 1 }),
  28. dashCount: PD.Numeric(4, { min: 0, max: 10, step: 2 }),
  29. dashScale: PD.Numeric(0.8, { min: 0, max: 2, step: 0.1 }),
  30. dashCap: PD.Boolean(true),
  31. stubCap: PD.Boolean(true),
  32. radialSegments: PD.Numeric(16, { min: 2, max: 56, step: 2 }, BaseGeometry.CustomQualityParamInfo),
  33. };
  34. export const DefaultLinkCylinderProps = PD.getDefaultValues(LinkCylinderParams);
  35. export type LinkCylinderProps = typeof DefaultLinkCylinderProps
  36. export const LinkLineParams = {
  37. linkScale: PD.Numeric(0.5, { min: 0, max: 1, step: 0.1 }),
  38. linkSpacing: PD.Numeric(0.1, { min: 0, max: 2, step: 0.01 }),
  39. aromaticDashCount: PD.Numeric(2, { min: 2, max: 6, step: 2 }),
  40. dashCount: PD.Numeric(4, { min: 0, max: 10, step: 2 }),
  41. };
  42. export const DefaultLinkLineProps = PD.getDefaultValues(LinkLineParams);
  43. export type LinkLineProps = typeof DefaultLinkLineProps
  44. const tmpV12 = Vec3();
  45. const tmpShiftV12 = Vec3();
  46. const tmpShiftV13 = Vec3();
  47. const up = Vec3.create(0, 1, 0);
  48. /** Calculate 'shift' direction that is perpendiculat to v1 - v2 and goes through v3 */
  49. export function calculateShiftDir(out: Vec3, v1: Vec3, v2: Vec3, v3: Vec3 | null) {
  50. Vec3.normalize(tmpShiftV12, Vec3.sub(tmpShiftV12, v1, v2));
  51. if (v3 !== null) {
  52. Vec3.sub(tmpShiftV13, v1, v3);
  53. } else {
  54. Vec3.copy(tmpShiftV13, v1); // no reference point, use v1
  55. }
  56. Vec3.normalize(tmpShiftV13, tmpShiftV13);
  57. // ensure v13 and v12 are not colinear
  58. let dp = Vec3.dot(tmpShiftV12, tmpShiftV13);
  59. if (1 - Math.abs(dp) < 1e-5) {
  60. Vec3.set(tmpShiftV13, 1, 0, 0);
  61. dp = Vec3.dot(tmpShiftV12, tmpShiftV13);
  62. if (1 - Math.abs(dp) < 1e-5) {
  63. Vec3.set(tmpShiftV13, 0, 1, 0);
  64. dp = Vec3.dot(tmpShiftV12, tmpShiftV13);
  65. }
  66. }
  67. Vec3.setMagnitude(tmpShiftV12, tmpShiftV12, dp);
  68. Vec3.sub(tmpShiftV13, tmpShiftV13, tmpShiftV12);
  69. return Vec3.normalize(out, tmpShiftV13);
  70. }
  71. export interface LinkBuilderProps {
  72. linkCount: number
  73. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => void
  74. radius: (edgeIndex: number) => number,
  75. referencePosition?: (edgeIndex: number) => Vec3 | null
  76. style?: (edgeIndex: number) => LinkStyle
  77. ignore?: (edgeIndex: number) => boolean
  78. stub?: (edgeIndex: number) => boolean
  79. }
  80. export const enum LinkStyle {
  81. Solid = 0,
  82. Dashed = 1,
  83. Double = 2,
  84. OffsetDouble = 3,
  85. Triple = 4,
  86. OffsetTriple = 5,
  87. Disk = 6,
  88. Aromatic = 7,
  89. MirroredAromatic = 8,
  90. }
  91. // avoiding namespace lookup improved performance in Chrome (Aug 2020)
  92. const v3scale = Vec3.scale;
  93. const v3add = Vec3.add;
  94. const v3sub = Vec3.sub;
  95. const v3setMagnitude = Vec3.setMagnitude;
  96. const v3dot = Vec3.dot;
  97. /**
  98. * Each edge is included twice to allow for coloring/picking
  99. * the half closer to the first vertex, i.e. vertex a.
  100. */
  101. export function createLinkCylinderMesh(ctx: VisualContext, linkBuilder: LinkBuilderProps, props: LinkCylinderProps, mesh?: Mesh): { mesh: Mesh, boundingSphere?: Sphere3D } {
  102. const { linkCount, referencePosition, position, style, radius, ignore, stub } = linkBuilder;
  103. if (!linkCount) return { mesh: Mesh.createEmpty(mesh) };
  104. const { linkScale, linkSpacing, radialSegments, linkCap, aromaticScale, aromaticSpacing, aromaticDashCount, dashCount, dashScale, dashCap, stubCap } = props;
  105. const vertexCountEstimate = radialSegments * 2 * linkCount * 2;
  106. const builderState = MeshBuilder.createState(vertexCountEstimate, vertexCountEstimate / 4, mesh);
  107. const va = Vec3();
  108. const vb = Vec3();
  109. const vShift = Vec3();
  110. const center = Vec3();
  111. let count = 0;
  112. const cylinderProps: CylinderProps = {
  113. radiusTop: 1,
  114. radiusBottom: 1,
  115. radialSegments,
  116. topCap: linkCap,
  117. bottomCap: linkCap
  118. };
  119. const segmentCount = dashCount + 1;
  120. for (let edgeIndex = 0, _eI = linkCount; edgeIndex < _eI; ++edgeIndex) {
  121. if (ignore && ignore(edgeIndex)) continue;
  122. position(va, vb, edgeIndex);
  123. v3add(center, center, va);
  124. v3add(center, center, vb);
  125. count += 2;
  126. v3sub(tmpV12, vb, va);
  127. const dirFlag = v3dot(tmpV12, up) > 0;
  128. const linkRadius = radius(edgeIndex);
  129. const linkStyle = style ? style(edgeIndex) : LinkStyle.Solid;
  130. const linkStub = stubCap && (stub ? stub(edgeIndex) : false);
  131. const [topCap, bottomCap] = dirFlag ? [linkStub, linkCap] : [linkCap, linkStub];
  132. builderState.currentGroup = edgeIndex;
  133. const aromaticSegmentCount = aromaticDashCount + 1;
  134. if (linkStyle === LinkStyle.Solid) {
  135. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius;
  136. cylinderProps.topCap = topCap;
  137. cylinderProps.bottomCap = bottomCap;
  138. addCylinder(builderState, va, vb, 0.5, cylinderProps);
  139. } else if (linkStyle === LinkStyle.Dashed) {
  140. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius * dashScale;
  141. cylinderProps.topCap = cylinderProps.bottomCap = dashCap;
  142. if (segmentCount > 1) {
  143. addFixedCountDashedCylinder(builderState, va, vb, 0.5, segmentCount, cylinderProps);
  144. } else {
  145. addCylinder(builderState, va, vb, 0.5, cylinderProps);
  146. }
  147. } else if (linkStyle === LinkStyle.Double || linkStyle === LinkStyle.OffsetDouble || linkStyle === LinkStyle.Triple || linkStyle === LinkStyle.OffsetTriple || linkStyle === LinkStyle.Aromatic || linkStyle === LinkStyle.MirroredAromatic) {
  148. const order = (linkStyle === LinkStyle.Double || linkStyle === LinkStyle.OffsetDouble) ? 2 :
  149. (linkStyle === LinkStyle.Triple || linkStyle === LinkStyle.OffsetTriple) ? 3 : 1.5;
  150. const multiRadius = linkRadius * (linkScale / (0.5 * order));
  151. const absOffset = (linkRadius - multiRadius) * linkSpacing;
  152. calculateShiftDir(vShift, va, vb, referencePosition ? referencePosition(edgeIndex) : null);
  153. cylinderProps.topCap = topCap;
  154. cylinderProps.bottomCap = bottomCap;
  155. if (linkStyle === LinkStyle.Aromatic || linkStyle === LinkStyle.MirroredAromatic) {
  156. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius;
  157. addCylinder(builderState, va, vb, 0.5, cylinderProps);
  158. const aromaticOffset = linkRadius + aromaticScale * linkRadius + aromaticScale * linkRadius * aromaticSpacing;
  159. v3setMagnitude(tmpV12, v3sub(tmpV12, vb, va), linkRadius * 0.5);
  160. v3add(va, va, tmpV12);
  161. v3sub(vb, vb, tmpV12);
  162. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius * aromaticScale;
  163. cylinderProps.topCap = cylinderProps.bottomCap = dashCap;
  164. v3setMagnitude(vShift, vShift, aromaticOffset);
  165. v3sub(va, va, vShift);
  166. v3sub(vb, vb, vShift);
  167. addFixedCountDashedCylinder(builderState, va, vb, 0.5, aromaticSegmentCount, cylinderProps);
  168. if (linkStyle === LinkStyle.MirroredAromatic) {
  169. v3setMagnitude(vShift, vShift, aromaticOffset * 2);
  170. v3add(va, va, vShift);
  171. v3add(vb, vb, vShift);
  172. addFixedCountDashedCylinder(builderState, va, vb, 0.5, aromaticSegmentCount, cylinderProps);
  173. }
  174. } else if (linkStyle === LinkStyle.OffsetDouble || linkStyle === LinkStyle.OffsetTriple) {
  175. const multipleOffset = linkRadius + multiRadius + linkScale * linkRadius * linkSpacing;
  176. v3setMagnitude(vShift, vShift, multipleOffset);
  177. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius;
  178. addCylinder(builderState, va, vb, 0.5, cylinderProps);
  179. v3scale(tmpV12, tmpV12, linkSpacing * linkScale * 0.2);
  180. v3add(va, va, tmpV12);
  181. v3sub(vb, vb, tmpV12);
  182. cylinderProps.radiusTop = cylinderProps.radiusBottom = multiRadius;
  183. cylinderProps.topCap = dirFlag ? linkStub : dashCap;
  184. cylinderProps.bottomCap = dirFlag ? dashCap : linkStub;
  185. v3setMagnitude(vShift, vShift, multipleOffset);
  186. v3sub(va, va, vShift);
  187. v3sub(vb, vb, vShift);
  188. addCylinder(builderState, va, vb, 0.5, cylinderProps);
  189. if (order === 3) {
  190. v3setMagnitude(vShift, vShift, multipleOffset * 2);
  191. v3add(va, va, vShift);
  192. v3add(vb, vb, vShift);
  193. addCylinder(builderState, va, vb, 0.5, cylinderProps);
  194. }
  195. } else {
  196. v3setMagnitude(vShift, vShift, absOffset);
  197. cylinderProps.radiusTop = cylinderProps.radiusBottom = multiRadius;
  198. if (order === 3) addCylinder(builderState, va, vb, 0.5, cylinderProps);
  199. addDoubleCylinder(builderState, va, vb, 0.5, vShift, cylinderProps);
  200. }
  201. } else if (linkStyle === LinkStyle.Disk) {
  202. v3scale(tmpV12, tmpV12, 0.475);
  203. v3add(va, va, tmpV12);
  204. v3sub(vb, vb, tmpV12);
  205. cylinderProps.radiusTop = cylinderProps.radiusBottom = linkRadius;
  206. cylinderProps.topCap = topCap;
  207. cylinderProps.bottomCap = bottomCap;
  208. addCylinder(builderState, va, vb, 0.5, cylinderProps);
  209. }
  210. }
  211. const oldBoundingSphere = mesh ? Sphere3D.clone(mesh.boundingSphere) : undefined;
  212. const m = MeshBuilder.getMesh(builderState);
  213. if (count === 0) return { mesh: m };
  214. // re-use boundingSphere if it has not changed much
  215. Vec3.scale(center, center, 1 / count);
  216. if (oldBoundingSphere && Vec3.distance(center, oldBoundingSphere.center) / oldBoundingSphere.radius < 1.0) {
  217. return { mesh: m, boundingSphere: oldBoundingSphere };
  218. } else {
  219. return { mesh: m };
  220. }
  221. }
  222. /**
  223. * Each edge is included twice to allow for coloring/picking
  224. * the half closer to the first vertex, i.e. vertex a.
  225. */
  226. export function createLinkCylinderImpostors(ctx: VisualContext, linkBuilder: LinkBuilderProps, props: LinkCylinderProps, cylinders?: Cylinders): { cylinders: Cylinders, boundingSphere?: Sphere3D } {
  227. const { linkCount, referencePosition, position, style, radius, ignore, stub } = linkBuilder;
  228. if (!linkCount) return { cylinders: Cylinders.createEmpty(cylinders) };
  229. const { linkScale, linkSpacing, linkCap, aromaticScale, aromaticSpacing, aromaticDashCount, dashCount, dashScale, dashCap, stubCap } = props;
  230. const cylindersCountEstimate = linkCount * 2;
  231. const builder = CylindersBuilder.create(cylindersCountEstimate, cylindersCountEstimate / 4, cylinders);
  232. const va = Vec3();
  233. const vb = Vec3();
  234. const vm = Vec3();
  235. const vShift = Vec3();
  236. const center = Vec3();
  237. let count = 0;
  238. // automatically adjust length for evenly spaced dashed cylinders
  239. const segmentCount = dashCount % 2 === 1 ? dashCount : dashCount + 1;
  240. for (let edgeIndex = 0, _eI = linkCount; edgeIndex < _eI; ++edgeIndex) {
  241. if (ignore && ignore(edgeIndex)) continue;
  242. position(va, vb, edgeIndex);
  243. v3add(center, center, va);
  244. v3add(center, center, vb);
  245. count += 2;
  246. const linkRadius = radius(edgeIndex);
  247. const linkStyle = style ? style(edgeIndex) : LinkStyle.Solid;
  248. const linkStub = stubCap && (stub ? stub(edgeIndex) : false);
  249. if (linkStyle === LinkStyle.Solid) {
  250. v3scale(vm, v3add(vm, va, vb), 0.5);
  251. builder.add(va[0], va[1], va[2], vm[0], vm[1], vm[2], 1, linkCap, linkStub, edgeIndex);
  252. } else if (linkStyle === LinkStyle.Dashed) {
  253. v3scale(vm, v3add(vm, va, vb), 0.5);
  254. if (segmentCount > 1) {
  255. builder.addFixedCountDashes(va, vm, segmentCount, dashScale, dashCap, dashCap, edgeIndex);
  256. } else {
  257. builder.add(va[0], va[1], va[2], vm[0], vm[1], vm[2], dashScale, dashCap, dashCap, edgeIndex);
  258. }
  259. } else if (linkStyle === LinkStyle.Double || linkStyle === LinkStyle.OffsetDouble || linkStyle === LinkStyle.Triple || linkStyle === LinkStyle.OffsetTriple || linkStyle === LinkStyle.Aromatic || linkStyle === LinkStyle.MirroredAromatic) {
  260. const order = (linkStyle === LinkStyle.Double || linkStyle === LinkStyle.OffsetDouble) ? 2 :
  261. (linkStyle === LinkStyle.Triple || linkStyle === LinkStyle.OffsetTriple) ? 3 : 1.5;
  262. const multiScale = linkScale / (0.5 * order);
  263. const absOffset = (linkRadius - multiScale * linkRadius) * linkSpacing;
  264. v3scale(vm, v3add(vm, va, vb), 0.5);
  265. calculateShiftDir(vShift, va, vb, referencePosition ? referencePosition(edgeIndex) : null);
  266. if (linkStyle === LinkStyle.Aromatic || linkStyle === LinkStyle.MirroredAromatic) {
  267. builder.add(va[0], va[1], va[2], vm[0], vm[1], vm[2], 1, linkCap, linkStub, edgeIndex);
  268. const aromaticOffset = linkRadius + aromaticScale * linkRadius + aromaticScale * linkRadius * aromaticSpacing;
  269. v3setMagnitude(tmpV12, v3sub(tmpV12, vm, va), linkRadius * 0.5);
  270. v3add(va, va, tmpV12);
  271. v3setMagnitude(vShift, vShift, aromaticOffset);
  272. v3sub(va, va, vShift);
  273. v3sub(vm, vm, vShift);
  274. builder.addFixedCountDashes(va, vm, aromaticDashCount, aromaticScale, dashCap, dashCap, edgeIndex);
  275. if (linkStyle === LinkStyle.MirroredAromatic) {
  276. v3setMagnitude(vShift, vShift, aromaticOffset * 2);
  277. v3add(va, va, vShift);
  278. v3add(vm, vm, vShift);
  279. builder.addFixedCountDashes(va, vm, aromaticDashCount, aromaticScale, dashCap, dashCap, edgeIndex);
  280. }
  281. } else if (linkStyle === LinkStyle.OffsetDouble || linkStyle === LinkStyle.OffsetTriple) {
  282. const multipleOffset = linkRadius + multiScale * linkRadius + linkScale * linkRadius * linkSpacing;
  283. v3setMagnitude(vShift, vShift, multipleOffset);
  284. builder.add(va[0], va[1], va[2], vm[0], vm[1], vm[2], 1, linkCap, linkStub, edgeIndex);
  285. v3setMagnitude(tmpV12, v3sub(tmpV12, va, vm), linkRadius / 1.5);
  286. v3sub(va, va, tmpV12);
  287. if (order === 3) 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);
  288. 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, dashCap, linkStub, edgeIndex);
  289. } else {
  290. v3setMagnitude(vShift, vShift, absOffset);
  291. if (order === 3) builder.add(va[0], va[1], va[2], vm[0], vm[1], vm[2], multiScale, linkCap, linkStub, edgeIndex);
  292. 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);
  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], multiScale, linkCap, linkStub, edgeIndex);
  294. }
  295. } else if (linkStyle === LinkStyle.Disk) {
  296. v3scale(tmpV12, v3sub(tmpV12, vm, va), 0.475);
  297. v3add(va, va, tmpV12);
  298. v3sub(vm, vm, tmpV12);
  299. builder.add(va[0], va[1], va[2], vm[0], vm[1], vm[2], 1, linkCap, linkStub, edgeIndex);
  300. }
  301. }
  302. const oldBoundingSphere = cylinders ? Sphere3D.clone(cylinders.boundingSphere) : undefined;
  303. const c = builder.getCylinders();
  304. if (count === 0) return { cylinders: c };
  305. // re-use boundingSphere if it has not changed much
  306. Vec3.scale(center, center, 1 / count);
  307. if (oldBoundingSphere && Vec3.distance(center, oldBoundingSphere.center) / oldBoundingSphere.radius < 1.0) {
  308. return { cylinders: c, boundingSphere: oldBoundingSphere };
  309. } else {
  310. return { cylinders: c };
  311. }
  312. }
  313. /**
  314. * Each edge is included twice to allow for coloring/picking
  315. * the half closer to the first vertex, i.e. vertex a.
  316. */
  317. export function createLinkLines(ctx: VisualContext, linkBuilder: LinkBuilderProps, props: LinkLineProps, lines?: Lines): { lines: Lines, boundingSphere?: Sphere3D } {
  318. const { linkCount, referencePosition, position, style, ignore } = linkBuilder;
  319. if (!linkCount) return { lines: Lines.createEmpty(lines) };
  320. const { linkScale, linkSpacing, aromaticDashCount, dashCount } = props;
  321. const linesCountEstimate = linkCount * 2;
  322. const builder = LinesBuilder.create(linesCountEstimate, linesCountEstimate / 4, lines);
  323. const va = Vec3();
  324. const vb = Vec3();
  325. const vm = Vec3();
  326. const vShift = Vec3();
  327. const center = Vec3();
  328. let count = 0;
  329. // automatically adjust length for evenly spaced dashed lines
  330. const segmentCount = dashCount % 2 === 1 ? dashCount : dashCount + 1;
  331. const lengthScale = 0.5 - (0.5 / 2 / segmentCount);
  332. const aromaticSegmentCount = aromaticDashCount + 1;
  333. const aromaticLengthScale = 0.5 - (0.5 / 2 / aromaticSegmentCount);
  334. const aromaticOffsetFactor = 4.5;
  335. const multipleOffsetFactor = 3;
  336. for (let edgeIndex = 0, _eI = linkCount; edgeIndex < _eI; ++edgeIndex) {
  337. if (ignore && ignore(edgeIndex)) continue;
  338. position(va, vb, edgeIndex);
  339. v3add(center, center, va);
  340. v3add(center, center, vb);
  341. count += 2;
  342. const linkStyle = style ? style(edgeIndex) : LinkStyle.Solid;
  343. if (linkStyle === LinkStyle.Solid) {
  344. v3scale(vm, v3add(vm, va, vb), 0.5);
  345. builder.add(va[0], va[1], va[2], vm[0], vm[1], vm[2], edgeIndex);
  346. } else if (linkStyle === LinkStyle.Dashed) {
  347. if (segmentCount > 1) {
  348. v3scale(tmpV12, v3sub(tmpV12, vb, va), lengthScale);
  349. v3sub(vb, vb, tmpV12);
  350. builder.addFixedCountDashes(va, vb, segmentCount, edgeIndex);
  351. } else {
  352. v3scale(vm, v3add(vm, va, vb), 0.5);
  353. builder.add(va[0], va[1], va[2], vm[0], vm[1], vm[2], edgeIndex);
  354. }
  355. } else if (linkStyle === LinkStyle.Double || linkStyle === LinkStyle.OffsetDouble || linkStyle === LinkStyle.Triple || linkStyle === LinkStyle.OffsetTriple || linkStyle === LinkStyle.Aromatic || linkStyle === LinkStyle.MirroredAromatic) {
  356. const order = linkStyle === LinkStyle.Double || linkStyle === LinkStyle.OffsetDouble ? 2 :
  357. linkStyle === LinkStyle.Triple || linkStyle === LinkStyle.OffsetTriple ? 3 : 1.5;
  358. const multiRadius = 1 * (linkScale / (0.5 * order));
  359. const absOffset = (1 - multiRadius) * linkSpacing;
  360. v3scale(vm, v3add(vm, va, vb), 0.5);
  361. calculateShiftDir(vShift, va, vb, referencePosition ? referencePosition(edgeIndex) : null);
  362. if (linkStyle === LinkStyle.Aromatic || linkStyle === LinkStyle.MirroredAromatic) {
  363. builder.add(va[0], va[1], va[2], vm[0], vm[1], vm[2], edgeIndex);
  364. v3scale(tmpV12, v3sub(tmpV12, vb, va), aromaticLengthScale);
  365. v3sub(vb, vb, tmpV12);
  366. v3setMagnitude(vShift, vShift, absOffset * aromaticOffsetFactor);
  367. v3sub(va, va, vShift);
  368. v3sub(vb, vb, vShift);
  369. builder.addFixedCountDashes(va, vb, aromaticSegmentCount, edgeIndex);
  370. if (linkStyle === LinkStyle.MirroredAromatic) {
  371. v3setMagnitude(vShift, vShift, absOffset * aromaticOffsetFactor * 2);
  372. v3add(va, va, vShift);
  373. v3add(vb, vb, vShift);
  374. builder.addFixedCountDashes(va, vb, aromaticSegmentCount, edgeIndex);
  375. }
  376. } else if (linkStyle === LinkStyle.OffsetDouble || linkStyle === LinkStyle.OffsetTriple) {
  377. v3setMagnitude(vShift, vShift, absOffset * multipleOffsetFactor);
  378. builder.add(va[0], va[1], va[2], vm[0], vm[1], vm[2], edgeIndex);
  379. v3scale(tmpV12, v3sub(tmpV12, va, vb), linkSpacing * linkScale);
  380. v3sub(va, va, tmpV12);
  381. if (order === 3) 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);
  382. 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);
  383. } else {
  384. v3setMagnitude(vShift, vShift, absOffset * 1.5);
  385. if (order === 3) builder.add(va[0], va[1], va[2], vm[0], vm[1], vm[2], edgeIndex);
  386. 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);
  387. 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);
  388. }
  389. } else if (linkStyle === LinkStyle.Disk) {
  390. v3scale(tmpV12, v3sub(tmpV12, vb, va), 0.475);
  391. v3add(va, va, tmpV12);
  392. v3sub(vb, vb, tmpV12);
  393. // TODO what to do here? Line as disk doesn't work well.
  394. builder.add(va[0], va[1], va[2], vb[0], vb[1], vb[2], edgeIndex);
  395. }
  396. }
  397. const oldBoundingSphere = lines ? Sphere3D.clone(lines.boundingSphere) : undefined;
  398. const l = builder.getLines();
  399. if (count === 0) return { lines: l };
  400. // re-use boundingSphere if it has not changed much
  401. Vec3.scale(center, center, 1 / count);
  402. if (oldBoundingSphere && Vec3.distance(center, oldBoundingSphere.center) / oldBoundingSphere.radius < 1.0) {
  403. return { lines: l, boundingSphere: oldBoundingSphere };
  404. } else {
  405. return { lines: l };
  406. }
  407. }