API Docs for:
Show:

File: utils\graph.js

  1. /*
  2. * BGPlay.js #9660
  3. * A web-based service for the visualization of the Internet routing
  4. *
  5. * Copyright (c) 2012 Roma Tre University and RIPE NCC
  6. *
  7. * See the file LICENSE.txt for copying permission.
  8. */
  9.  
  10. var net = net || {};
  11. net.webrobotics = net.webrobotics || {};
  12.  
  13. net.webrobotics.GraphUtils = function () {
  14. /**
  15. * Returns the distance between two points.
  16. *
  17. * @method pointDistance
  18. * @param {Number,Number} x and y of the first point
  19. * @param {Number,Number} x and y of the second point
  20. * @return {Number} the distance between the two points
  21. */
  22. this.pointDistance = function (p1, p2) {
  23. var diffX, diffY;
  24. diffX = p2.x - p1.x;
  25. diffY = p2.y - p1.y;
  26. return Math.sqrt((diffX*diffX)+(diffY*diffY));
  27. };
  28.  
  29. this.vectorModule = function (vector) {
  30. return Math.abs(this.pointDistance({x:0, y:0}, vector));
  31. };
  32.  
  33. this.isNumeric = function (n) {
  34. return !isNaN(parseFloat(n)) && isFinite(n)
  35. };
  36.  
  37. this.unitVector = function (p1, p2) {
  38. var distance = this.pointDistance(p1, p2);
  39. return (distance == 0) ? {x:(p2.x - p1.x), y:(p2.y - p1.y)} : {x:(p2.x - p1.x) / distance, y:(p2.y - p1.y) / distance};
  40. };
  41.  
  42. this.cathetus = function (hypotenuse, otherLeg) {
  43. return Math.sqrt(Math.pow(hypotenuse, 2) - Math.pow(otherLeg, 2));
  44. };
  45.  
  46. this.inverseVector = function (vector) {
  47. return {x:-vector.x, y:-vector.y};
  48. };
  49.  
  50. this.leftUnitVector = function (unitVector) {
  51. return {x:-unitVector.y, y:unitVector.x};
  52. };
  53.  
  54. this.radiusOfTheInscribedCircle = function (width, height) {
  55. return Math.min(width, height) / 2;
  56. };
  57.  
  58. this.angle = function (vector) {
  59. var module = this.vectorModule(vector);
  60. var x = Math.abs(vector.x);
  61. var y = Math.abs(vector.y);
  62. return Math.acos((y*y) - (x*x) - (module*module)) / (-2 * module * x);
  63. };
  64.  
  65. this.vectorProjection = function (vector, axis) {
  66. return (axis == "x") ? {x:vector.x, y:0} : {x:0, y:vector.y};
  67. };
  68.  
  69. this.sumBetweenVectors = function (v1, v2) {
  70. return {x:v1.x + v2.x, y:v1.y + v2.y};
  71. };
  72.  
  73. this.mulVectorForValue = function (vector, value) {
  74. return {x:vector.x * value, y:vector.y * value};
  75. };
  76.  
  77. this.pointAtDistance = function (distance, unitVector, point) {
  78. return this.sumBetweenVectors(this.mulVectorForValue(unitVector, distance), point);
  79. };
  80.  
  81. this.translatePoint = function (point, vector) {
  82. var newPoint = this.sumBetweenVectors(vector, point);
  83. point.x = newPoint.x;
  84. point.y = newPoint.y;
  85. return point;
  86. };
  87.  
  88. this.transformPointToPoint = function (from, to) {
  89. from.x = to.x;
  90. from.y = to.y;
  91. };
  92.  
  93. this.hook = function (node1, node2, optimalDistance, limit) {
  94. var newPoint1, newPoint2, difference, unitVector, halfDifference, distance,force, kSpringFactor;
  95.  
  96. kSpringFactor=0.2; //0.3
  97. distance = this.pointDistance(node1, node2);
  98. difference = distance - (optimalDistance * Math.abs(node1.orbit - node2.orbit));
  99. //difference = distance - (optimalDistance);
  100. if (difference != 0) {
  101. unitVector = this.unitVector(node1, node2);
  102. difference = (limit != null && difference > limit) ? limit : difference;
  103. force=kSpringFactor*difference;
  104. //halfDifference = difference / 2;
  105. //newPoint1 = this.mulVectorForValue(unitVector, halfDifference);
  106. //newPoint2 = this.mulVectorForValue(this.inverseVector(unitVector), halfDifference);
  107. newPoint1 = this.mulVectorForValue(unitVector, force);
  108. newPoint2 = this.mulVectorForValue(this.inverseVector(unitVector), force);
  109.  
  110. node1.vectors.push(newPoint1);
  111. node2.vectors.push(newPoint2);
  112. }
  113.  
  114.  
  115. };
  116.  
  117. this.coulomb = function (node1, node2, factor1, factor2, limit, maxDistance) {
  118. var newPoint1, newPoint2, unitVector, distance, repulsionFactor, force1, force2, factorSum, factorPercentage;
  119. repulsionFactor = 80;//350
  120. distance = this.pointDistance(node1, node2);
  121. //distance=(distance==0)?0.1:distance;
  122. if (distance > maxDistance) return;
  123.  
  124. factorSum = factor1 + factor2;
  125. factorPercentage = (100/factorSum);
  126. factor1 = factorPercentage*factor1;
  127. factor2 = factorPercentage*factor2;
  128.  
  129. force1 = (repulsionFactor * (1/distance))/100 * factor2;
  130. force2 = (repulsionFactor * (1/distance))/100 * factor1;
  131.  
  132. //force=repulsionFactor*Math.sqrt(distance);
  133. force1 = (force1 > limit) ? limit : force1;
  134. force2 = (force2 > limit) ? limit : force2;
  135.  
  136. unitVector = this.unitVector(node1, node2);
  137. newPoint1 = this.mulVectorForValue(this.inverseVector(unitVector), force1);
  138. newPoint2 = this.mulVectorForValue(unitVector, force2);
  139. node1.vectors.push(newPoint1);
  140. node2.vectors.push(newPoint2);
  141. };
  142.  
  143. this.straighten = function (node1, node2, node3) {
  144. var unitVector1 = this.unitVector(node1, node2);
  145. var unitVector2 = this.unitVector(node2, node3);
  146. var distance = this.pointDistance(node2, node3);
  147. var resultVector1 = this.unitVector(unitVector2, unitVector1);
  148. var resultVector2 = this.unitVector(unitVector1, unitVector2);
  149. node2.vectors.push(this.mulVectorForValue(resultVector1, 2));
  150. node3.vectors.push(this.mulVectorForValue(resultVector2, 2));
  151. };
  152.  
  153.  
  154. this.computeFinalPosition = function (node) {
  155. var element;
  156. for (var n = 0; n < node.vectors.length; n++) {
  157. element = node.vectors[n];
  158. node.x += element.x;
  159. node.y += element.y;
  160. }
  161.  
  162. node.vectors = new Array();
  163. };
  164.  
  165. this.absOrientation = function (node1, node2) {
  166. return (parseInt(node1.view.graphId) <= parseInt(node2.view.graphId)) ? [node1, node2] : [node2, node1];
  167. };
  168.  
  169. this.edgeNodeRepulsion = function(edge, node, limit, maxDistance,n){
  170. var angolarCoefficient, perpendicolarCoefficient, q1, q2, px, py, tmpPoint, newPoint1, unitVector, distance, repulsionFactor, force;
  171. if (edge[1].id != node.id && edge[0].id != node.id){
  172. //&& Math.min(this.pointDistance(edge[0],node),this.pointDistance(edge[1],node))<maxDistance){
  173.  
  174. angolarCoefficient = (edge[1].y - edge[0].y)/(edge[1].x - edge[0].x);
  175. q1 = edge[1].y - (angolarCoefficient*edge[1].x);
  176. perpendicolarCoefficient = -angolarCoefficient;
  177. q2 = node.y/(node.x*perpendicolarCoefficient);
  178. px = (q2-q1)/(angolarCoefficient-perpendicolarCoefficient);
  179. py = (angolarCoefficient*edge[0].x) + q1;
  180. tmpPoint = {x:px,y:py, vectors:[]};
  181. if (Math.max(edge[0].x, edge[1].x)>= px && Math.min(edge[0].x, edge[1].x)<= px &&
  182. Math.max(edge[0].y, edge[1].y)>= py && Math.min(edge[0].y, edge[1].y)<= py &&
  183. this.pointDistance(tmpPoint,node) < maxDistance){
  184. //this.coulomb({x:px,y:py, vectors:[]}, node, 1000, 1, limit, maxDistance);
  185.  
  186. repulsionFactor = 20;//350
  187. distance = this.pointDistance(tmpPoint, node);
  188. if (distance > maxDistance) return;
  189.  
  190. //force = Math.pow(repulsionFactor,(1/n));
  191. force = repulsionFactor/n;
  192. //force=repulsionFactor*Math.sqrt(distance);
  193. force = (force > limit) ? limit : force;
  194.  
  195. unitVector = this.unitVector(tmpPoint, node);
  196. newPoint1 = this.mulVectorForValue(unitVector, force);
  197. node.vectors.push(newPoint1);
  198.  
  199. }
  200. }
  201. }
  202.  
  203. this.roundedPath = function (curveCoefficient, p1, p2, p3) {
  204. var points = [];
  205. var vector1 = this.unitVector(p2, p1);
  206. var vector2 = this.unitVector(p2, p3);
  207.  
  208. var borderPoint1 = this.pointAtDistance(curveCoefficient, vector1, p2);
  209. var borderPoint2 = this.pointAtDistance(curveCoefficient, vector2, p2);
  210.  
  211. var middlePoint1 = this.pointAtDistance(curveCoefficient / 2, vector1, p2);
  212. var middlePoint2 = this.pointAtDistance(curveCoefficient / 2, vector2, p2);
  213.  
  214. var vector3 = this.unitVector(middlePoint2, middlePoint1);
  215. var centralPoint = this.pointAtDistance(this.pointDistance(middlePoint2, middlePoint1) / 2, vector2, middlePoint2);
  216.  
  217. points.push(borderPoint1);
  218. points.push(middlePoint1);
  219. points.push(centralPoint);
  220. points.push(middlePoint2);
  221. points.push(borderPoint2);
  222. return points;
  223. }
  224. };
  225.  
  226.  
  227.  
  228.