AuxilFunctions.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package de.tu_darmstadt.informatik.tk.scopviz.main;
  2. import java.util.Iterator;
  3. import org.graphstream.algorithm.Toolkit;
  4. import org.graphstream.graph.Edge;
  5. import org.graphstream.graph.Node;
  6. import org.graphstream.ui.geom.Point3;
  7. import org.graphstream.ui.view.Camera;
  8. /**
  9. * This class contains helpful static functions.
  10. *
  11. * @author Matthias Wilhelm
  12. *
  13. */
  14. public class AuxilFunctions {
  15. /**
  16. * Width in pixel for which the edge selection triggers
  17. */
  18. private static final int EDGE_SELECTION_WIDTH = 5;
  19. /**
  20. * Recalculates pi / 2
  21. */
  22. private static final double HALF_PI = Math.PI / 2;
  23. /**
  24. * Returns the closest Edge in the current Graph to a given position. It
  25. * allows for an inaccuracy of around {@value #EDGE_SELECTION_WIDTH}px
  26. *
  27. * @param pos
  28. * The position. Expects a <b>Point3</b>, but only uses the x and
  29. * y coordinates
  30. * @return the closest Edge if a valid Edge exists, returns <b>null</b>
  31. * otherwise
  32. */
  33. public static Edge getClosestEdge(Point3 pos) {
  34. Camera cam = Main.getInstance().getGraphManager().getView().getCamera();
  35. Point3 min = cam.transformPxToGu(0, 0);
  36. Point3 max = cam.transformPxToGu(EDGE_SELECTION_WIDTH, EDGE_SELECTION_WIDTH);
  37. double dist = Math.max(Math.abs(max.x - min.x), Math.abs(max.y - min.y));
  38. return getClosestEdge(pos, dist);
  39. }
  40. /**
  41. * Returns the closest Edge in the current Graph to a given position within
  42. * the given maxDistance
  43. *
  44. * @param pos
  45. * The position. Expects a <b>Point3</b>, but only uses the x and
  46. * y coordinates
  47. * @param maxDistance
  48. * the maximum distance (in gu) the edge may be away from the
  49. * position to be considered as a valid edge
  50. * @return the closest Edge if a valid Edge exists, returns <b>null</b>
  51. * otherwise
  52. */
  53. public static Edge getClosestEdge(Point3 pos, double maxDistance) {
  54. double x0 = pos.x;
  55. double y0 = pos.y;
  56. double dist = maxDistance;
  57. Edge result = null;
  58. GraphManager gm = Main.getInstance().getGraphManager();
  59. for (Iterator<Edge> iterator = gm.getGraph().getEdgeIterator(); iterator.hasNext();) {
  60. Edge edge = (Edge) iterator.next();
  61. double[] n1 = Toolkit.nodePosition(edge.getNode0());
  62. double[] n2 = Toolkit.nodePosition(edge.getNode1());
  63. double x1 = n1[0];
  64. double y1 = n1[1];
  65. double x2 = n2[0];
  66. double y2 = n2[1];
  67. double a = distance(x0, y0, x2, y2);
  68. double b = distance(x0, y0, x1, y1);
  69. double c = distance(x1, y1, x2, y2);
  70. double b2 = b * b;
  71. double a2 = a * a;
  72. double c2 = c * c;
  73. double cdist = Math.abs((y2 - y1) * x0 - (x2 - x1) * y0 + x2 * y1 - y2 * x1) / c;
  74. double alpha = Math.acos((b2 + c2 - a2) / (2 * b * c));
  75. double beta = Math.acos((a2 + c2 - b2) / (2 * a * c));
  76. if (cdist < dist && alpha <= HALF_PI && beta <= HALF_PI) {
  77. dist = cdist;
  78. result = edge;
  79. }
  80. }
  81. return result;
  82. }
  83. /**
  84. * Calculates the distance between two given Nodes
  85. *
  86. * @param a
  87. * Node 1
  88. * @param b
  89. * Node 2
  90. * @return the distance between the two Nodes as a double
  91. */
  92. public static double distance(Node a, Node b) {
  93. double[] n1 = Toolkit.nodePosition(a);
  94. double[] n2 = Toolkit.nodePosition(b);
  95. return distance(n1[0], n1[1], n2[0], n2[1]);
  96. }
  97. /**
  98. * Calculates the distance between a x,y position and a Node
  99. *
  100. * @param x0
  101. * x cord of the position
  102. * @param y0
  103. * y cord of the position
  104. * @param a
  105. * the Node
  106. * @return the distance between the position and the Node as a double
  107. */
  108. public static double distance(double x0, double y0, Node a) {
  109. double[] n1 = Toolkit.nodePosition(a);
  110. return distance(x0, y0, n1[0], n1[1]);
  111. }
  112. /**
  113. * Calculates the distance between two x,y positions
  114. *
  115. * @param x0
  116. * x cord of the first position
  117. * @param y0
  118. * y cord of the first position
  119. * @param x1
  120. * x cord of the second position
  121. * @param y1
  122. * y cord of the second position
  123. * @return the distance between the two positions as a double
  124. */
  125. public static double distance(double x0, double y0, double x1, double y1) {
  126. return Math.sqrt(Math.pow(y0 - y1, 2) + Math.pow(x0 - x1, 2));
  127. }
  128. }