adjustEdge.js 5.9 KB

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