adjustEdge.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import * as curveTool from 'zrender/lib/core/curve.js';
  41. import * as vec2 from 'zrender/lib/core/vector.js';
  42. import { getSymbolSize } from './graphHelper.js';
  43. var v1 = [];
  44. var v2 = [];
  45. var v3 = [];
  46. var quadraticAt = curveTool.quadraticAt;
  47. var v2DistSquare = vec2.distSquare;
  48. var mathAbs = Math.abs;
  49. function intersectCurveCircle(curvePoints, center, radius) {
  50. var p0 = curvePoints[0];
  51. var p1 = curvePoints[1];
  52. var p2 = curvePoints[2];
  53. var d = Infinity;
  54. var t;
  55. var radiusSquare = radius * radius;
  56. var interval = 0.1;
  57. for (var _t = 0.1; _t <= 0.9; _t += 0.1) {
  58. v1[0] = quadraticAt(p0[0], p1[0], p2[0], _t);
  59. v1[1] = quadraticAt(p0[1], p1[1], p2[1], _t);
  60. var diff = mathAbs(v2DistSquare(v1, center) - radiusSquare);
  61. if (diff < d) {
  62. d = diff;
  63. t = _t;
  64. }
  65. } // Assume the segment is monotone,Find root through Bisection method
  66. // At most 32 iteration
  67. for (var i = 0; i < 32; i++) {
  68. // let prev = t - interval;
  69. var next = t + interval; // v1[0] = quadraticAt(p0[0], p1[0], p2[0], prev);
  70. // v1[1] = quadraticAt(p0[1], p1[1], p2[1], prev);
  71. v2[0] = quadraticAt(p0[0], p1[0], p2[0], t);
  72. v2[1] = quadraticAt(p0[1], p1[1], p2[1], t);
  73. v3[0] = quadraticAt(p0[0], p1[0], p2[0], next);
  74. v3[1] = quadraticAt(p0[1], p1[1], p2[1], next);
  75. var diff = v2DistSquare(v2, center) - radiusSquare;
  76. if (mathAbs(diff) < 1e-2) {
  77. break;
  78. } // let prevDiff = v2DistSquare(v1, center) - radiusSquare;
  79. var nextDiff = v2DistSquare(v3, center) - radiusSquare;
  80. interval /= 2;
  81. if (diff < 0) {
  82. if (nextDiff >= 0) {
  83. t = t + interval;
  84. } else {
  85. t = t - interval;
  86. }
  87. } else {
  88. if (nextDiff >= 0) {
  89. t = t - interval;
  90. } else {
  91. t = t + interval;
  92. }
  93. }
  94. }
  95. return t;
  96. } // Adjust edge to avoid
  97. export default function adjustEdge(graph, scale) {
  98. var tmp0 = [];
  99. var quadraticSubdivide = curveTool.quadraticSubdivide;
  100. var pts = [[], [], []];
  101. var pts2 = [[], []];
  102. var v = [];
  103. scale /= 2;
  104. graph.eachEdge(function (edge, idx) {
  105. var linePoints = edge.getLayout();
  106. var fromSymbol = edge.getVisual('fromSymbol');
  107. var toSymbol = edge.getVisual('toSymbol');
  108. if (!linePoints.__original) {
  109. linePoints.__original = [vec2.clone(linePoints[0]), vec2.clone(linePoints[1])];
  110. if (linePoints[2]) {
  111. linePoints.__original.push(vec2.clone(linePoints[2]));
  112. }
  113. }
  114. var originalPoints = linePoints.__original; // Quadratic curve
  115. if (linePoints[2] != null) {
  116. vec2.copy(pts[0], originalPoints[0]);
  117. vec2.copy(pts[1], originalPoints[2]);
  118. vec2.copy(pts[2], originalPoints[1]);
  119. if (fromSymbol && fromSymbol !== 'none') {
  120. var symbolSize = getSymbolSize(edge.node1);
  121. var t = intersectCurveCircle(pts, originalPoints[0], symbolSize * scale); // Subdivide and get the second
  122. quadraticSubdivide(pts[0][0], pts[1][0], pts[2][0], t, tmp0);
  123. pts[0][0] = tmp0[3];
  124. pts[1][0] = tmp0[4];
  125. quadraticSubdivide(pts[0][1], pts[1][1], pts[2][1], t, tmp0);
  126. pts[0][1] = tmp0[3];
  127. pts[1][1] = tmp0[4];
  128. }
  129. if (toSymbol && toSymbol !== 'none') {
  130. var symbolSize = getSymbolSize(edge.node2);
  131. var t = intersectCurveCircle(pts, originalPoints[1], symbolSize * scale); // Subdivide and get the first
  132. quadraticSubdivide(pts[0][0], pts[1][0], pts[2][0], t, tmp0);
  133. pts[1][0] = tmp0[1];
  134. pts[2][0] = tmp0[2];
  135. quadraticSubdivide(pts[0][1], pts[1][1], pts[2][1], t, tmp0);
  136. pts[1][1] = tmp0[1];
  137. pts[2][1] = tmp0[2];
  138. } // Copy back to layout
  139. vec2.copy(linePoints[0], pts[0]);
  140. vec2.copy(linePoints[1], pts[2]);
  141. vec2.copy(linePoints[2], pts[1]);
  142. } // Line
  143. else {
  144. vec2.copy(pts2[0], originalPoints[0]);
  145. vec2.copy(pts2[1], originalPoints[1]);
  146. vec2.sub(v, pts2[1], pts2[0]);
  147. vec2.normalize(v, v);
  148. if (fromSymbol && fromSymbol !== 'none') {
  149. var symbolSize = getSymbolSize(edge.node1);
  150. vec2.scaleAndAdd(pts2[0], pts2[0], v, symbolSize * scale);
  151. }
  152. if (toSymbol && toSymbol !== 'none') {
  153. var symbolSize = getSymbolSize(edge.node2);
  154. vec2.scaleAndAdd(pts2[1], pts2[1], v, -symbolSize * scale);
  155. }
  156. vec2.copy(linePoints[0], pts2[0]);
  157. vec2.copy(linePoints[1], pts2[1]);
  158. }
  159. });
  160. }