adjustEdge.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. }
  66. // Assume the segment is monotone,Find root through Bisection method
  67. // At most 32 iteration
  68. for (var i = 0; i < 32; i++) {
  69. // let prev = t - interval;
  70. var next = t + interval;
  71. // v1[0] = quadraticAt(p0[0], p1[0], p2[0], prev);
  72. // v1[1] = quadraticAt(p0[1], p1[1], p2[1], prev);
  73. v2[0] = quadraticAt(p0[0], p1[0], p2[0], t);
  74. v2[1] = quadraticAt(p0[1], p1[1], p2[1], t);
  75. v3[0] = quadraticAt(p0[0], p1[0], p2[0], next);
  76. v3[1] = quadraticAt(p0[1], p1[1], p2[1], next);
  77. var diff = v2DistSquare(v2, center) - radiusSquare;
  78. if (mathAbs(diff) < 1e-2) {
  79. break;
  80. }
  81. // let prevDiff = v2DistSquare(v1, center) - radiusSquare;
  82. var nextDiff = v2DistSquare(v3, center) - radiusSquare;
  83. interval /= 2;
  84. if (diff < 0) {
  85. if (nextDiff >= 0) {
  86. t = t + interval;
  87. } else {
  88. t = t - interval;
  89. }
  90. } else {
  91. if (nextDiff >= 0) {
  92. t = t - interval;
  93. } else {
  94. t = t + interval;
  95. }
  96. }
  97. }
  98. return t;
  99. }
  100. // Adjust edge to avoid
  101. export default function adjustEdge(graph, scale) {
  102. var tmp0 = [];
  103. var quadraticSubdivide = curveTool.quadraticSubdivide;
  104. var pts = [[], [], []];
  105. var pts2 = [[], []];
  106. var v = [];
  107. scale /= 2;
  108. graph.eachEdge(function (edge, idx) {
  109. var linePoints = edge.getLayout();
  110. var fromSymbol = edge.getVisual('fromSymbol');
  111. var toSymbol = edge.getVisual('toSymbol');
  112. if (!linePoints.__original) {
  113. linePoints.__original = [vec2.clone(linePoints[0]), vec2.clone(linePoints[1])];
  114. if (linePoints[2]) {
  115. linePoints.__original.push(vec2.clone(linePoints[2]));
  116. }
  117. }
  118. var originalPoints = linePoints.__original;
  119. // Quadratic curve
  120. if (linePoints[2] != null) {
  121. vec2.copy(pts[0], originalPoints[0]);
  122. vec2.copy(pts[1], originalPoints[2]);
  123. vec2.copy(pts[2], originalPoints[1]);
  124. if (fromSymbol && fromSymbol !== 'none') {
  125. var symbolSize = getSymbolSize(edge.node1);
  126. var t = intersectCurveCircle(pts, originalPoints[0], symbolSize * scale);
  127. // Subdivide and get the second
  128. quadraticSubdivide(pts[0][0], pts[1][0], pts[2][0], t, tmp0);
  129. pts[0][0] = tmp0[3];
  130. pts[1][0] = tmp0[4];
  131. quadraticSubdivide(pts[0][1], pts[1][1], pts[2][1], t, tmp0);
  132. pts[0][1] = tmp0[3];
  133. pts[1][1] = tmp0[4];
  134. }
  135. if (toSymbol && toSymbol !== 'none') {
  136. var symbolSize = getSymbolSize(edge.node2);
  137. var t = intersectCurveCircle(pts, originalPoints[1], symbolSize * scale);
  138. // Subdivide and get the first
  139. quadraticSubdivide(pts[0][0], pts[1][0], pts[2][0], t, tmp0);
  140. pts[1][0] = tmp0[1];
  141. pts[2][0] = tmp0[2];
  142. quadraticSubdivide(pts[0][1], pts[1][1], pts[2][1], t, tmp0);
  143. pts[1][1] = tmp0[1];
  144. pts[2][1] = tmp0[2];
  145. }
  146. // Copy back to layout
  147. vec2.copy(linePoints[0], pts[0]);
  148. vec2.copy(linePoints[1], pts[2]);
  149. vec2.copy(linePoints[2], pts[1]);
  150. }
  151. // Line
  152. else {
  153. vec2.copy(pts2[0], originalPoints[0]);
  154. vec2.copy(pts2[1], originalPoints[1]);
  155. vec2.sub(v, pts2[1], pts2[0]);
  156. vec2.normalize(v, v);
  157. if (fromSymbol && fromSymbol !== 'none') {
  158. var symbolSize = getSymbolSize(edge.node1);
  159. vec2.scaleAndAdd(pts2[0], pts2[0], v, symbolSize * scale);
  160. }
  161. if (toSymbol && toSymbol !== 'none') {
  162. var symbolSize = getSymbolSize(edge.node2);
  163. vec2.scaleAndAdd(pts2[1], pts2[1], v, -symbolSize * scale);
  164. }
  165. vec2.copy(linePoints[0], pts2[0]);
  166. vec2.copy(linePoints[1], pts2[1]);
  167. }
  168. });
  169. }