forceHelper.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. /*
  41. * A third-party license is embedded for some of the code in this file:
  42. * Some formulas were originally copied from "d3.js" with some
  43. * modifications made for this project.
  44. * (See more details in the comment of the method "step" below.)
  45. * The use of the source code of this file is also subject to the terms
  46. * and consitions of the license of "d3.js" (BSD-3Clause, see
  47. * </licenses/LICENSE-d3>).
  48. */
  49. import * as vec2 from 'zrender/lib/core/vector.js';
  50. var scaleAndAdd = vec2.scaleAndAdd;
  51. // function adjacentNode(n, e) {
  52. // return e.n1 === n ? e.n2 : e.n1;
  53. // }
  54. export function forceLayout(inNodes, inEdges, opts) {
  55. var nodes = inNodes;
  56. var edges = inEdges;
  57. var rect = opts.rect;
  58. var width = rect.width;
  59. var height = rect.height;
  60. var center = [rect.x + width / 2, rect.y + height / 2];
  61. // let scale = opts.scale || 1;
  62. var gravity = opts.gravity == null ? 0.1 : opts.gravity;
  63. // for (let i = 0; i < edges.length; i++) {
  64. // let e = edges[i];
  65. // let n1 = e.n1;
  66. // let n2 = e.n2;
  67. // n1.edges = n1.edges || [];
  68. // n2.edges = n2.edges || [];
  69. // n1.edges.push(e);
  70. // n2.edges.push(e);
  71. // }
  72. // Init position
  73. for (var i = 0; i < nodes.length; i++) {
  74. var n = nodes[i];
  75. if (!n.p) {
  76. n.p = vec2.create(width * (Math.random() - 0.5) + center[0], height * (Math.random() - 0.5) + center[1]);
  77. }
  78. n.pp = vec2.clone(n.p);
  79. n.edges = null;
  80. }
  81. // Formula in 'Graph Drawing by Force-directed Placement'
  82. // let k = scale * Math.sqrt(width * height / nodes.length);
  83. // let k2 = k * k;
  84. var initialFriction = opts.friction == null ? 0.6 : opts.friction;
  85. var friction = initialFriction;
  86. var beforeStepCallback;
  87. var afterStepCallback;
  88. return {
  89. warmUp: function () {
  90. friction = initialFriction * 0.8;
  91. },
  92. setFixed: function (idx) {
  93. nodes[idx].fixed = true;
  94. },
  95. setUnfixed: function (idx) {
  96. nodes[idx].fixed = false;
  97. },
  98. /**
  99. * Before step hook
  100. */
  101. beforeStep: function (cb) {
  102. beforeStepCallback = cb;
  103. },
  104. /**
  105. * After step hook
  106. */
  107. afterStep: function (cb) {
  108. afterStepCallback = cb;
  109. },
  110. /**
  111. * Some formulas were originally copied from "d3.js"
  112. * https://github.com/d3/d3/blob/b516d77fb8566b576088e73410437494717ada26/src/layout/force.js
  113. * with some modifications made for this project.
  114. * See the license statement at the head of this file.
  115. */
  116. step: function (cb) {
  117. beforeStepCallback && beforeStepCallback(nodes, edges);
  118. var v12 = [];
  119. var nLen = nodes.length;
  120. for (var i = 0; i < edges.length; i++) {
  121. var e = edges[i];
  122. if (e.ignoreForceLayout) {
  123. continue;
  124. }
  125. var n1 = e.n1;
  126. var n2 = e.n2;
  127. vec2.sub(v12, n2.p, n1.p);
  128. var d = vec2.len(v12) - e.d;
  129. var w = n2.w / (n1.w + n2.w);
  130. if (isNaN(w)) {
  131. w = 0;
  132. }
  133. vec2.normalize(v12, v12);
  134. !n1.fixed && scaleAndAdd(n1.p, n1.p, v12, w * d * friction);
  135. !n2.fixed && scaleAndAdd(n2.p, n2.p, v12, -(1 - w) * d * friction);
  136. }
  137. // Gravity
  138. for (var i = 0; i < nLen; i++) {
  139. var n = nodes[i];
  140. if (!n.fixed) {
  141. vec2.sub(v12, center, n.p);
  142. // let d = vec2.len(v12);
  143. // vec2.scale(v12, v12, 1 / d);
  144. // let gravityFactor = gravity;
  145. scaleAndAdd(n.p, n.p, v12, gravity * friction);
  146. }
  147. }
  148. // Repulsive
  149. // PENDING
  150. for (var i = 0; i < nLen; i++) {
  151. var n1 = nodes[i];
  152. for (var j = i + 1; j < nLen; j++) {
  153. var n2 = nodes[j];
  154. vec2.sub(v12, n2.p, n1.p);
  155. var d = vec2.len(v12);
  156. if (d === 0) {
  157. // Random repulse
  158. vec2.set(v12, Math.random() - 0.5, Math.random() - 0.5);
  159. d = 1;
  160. }
  161. var repFact = (n1.rep + n2.rep) / d / d;
  162. !n1.fixed && scaleAndAdd(n1.pp, n1.pp, v12, repFact);
  163. !n2.fixed && scaleAndAdd(n2.pp, n2.pp, v12, -repFact);
  164. }
  165. }
  166. var v = [];
  167. for (var i = 0; i < nLen; i++) {
  168. var n = nodes[i];
  169. if (!n.fixed) {
  170. vec2.sub(v, n.p, n.pp);
  171. scaleAndAdd(n.p, n.p, v, friction);
  172. vec2.copy(n.pp, n.p);
  173. }
  174. }
  175. friction = friction * 0.992;
  176. var finished = friction < 0.01;
  177. afterStepCallback && afterStepCallback(nodes, edges, finished);
  178. cb && cb(finished);
  179. }
  180. };
  181. }