EdgeSelectionHelper.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package de.tu_darmstadt.informatik.tk.scopviz.main;
  2. import java.util.Iterator;
  3. import org.graphstream.algorithm.Toolkit;
  4. import org.graphstream.ui.geom.Point3;
  5. import org.graphstream.ui.view.Camera;
  6. import de.tu_darmstadt.informatik.tk.scopviz.graphs.GraphManager;
  7. import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyEdge;
  8. import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyNode;
  9. /**
  10. * This class contains helpful static functions.
  11. *
  12. * @author Matthias Wilhelm
  13. * @version 1.0
  14. */
  15. public final class EdgeSelectionHelper {
  16. /**
  17. * Private Constructor to prevent instantiation. private
  18. * EdgeSelectionHelper(){}
  19. *
  20. * /** Width in pixels for which the edge selection triggers.
  21. */
  22. private static final int EDGE_SELECTION_WIDTH = 5;
  23. /**
  24. * Precalculates pi / 2.
  25. */
  26. private static final double HALF_PI = Math.PI / 2;
  27. private EdgeSelectionHelper() {
  28. }
  29. // TODO optional: only update if view has changed
  30. /**
  31. * Returns the closest Edge in the current Graph to a given position. It
  32. * allows for an inaccuracy of around {@value #EDGE_SELECTION_WIDTH}px
  33. *
  34. * @param pos
  35. * The position. Expects a <b>Point3</b>, but only uses the x and
  36. * y coordinates
  37. * @return the closest Edge if a valid Edge exists, returns <b>null</b>
  38. * otherwise
  39. */
  40. public static MyEdge getClosestEdge(Point3 pos) {
  41. Camera cam = Main.getInstance().getGraphManager().getView().getCamera();
  42. // gets to points within a fixed distance of each other and calculates
  43. // the gu of those points
  44. Point3 min = cam.transformPxToGu(0, 0);
  45. Point3 max = cam.transformPxToGu(EDGE_SELECTION_WIDTH, EDGE_SELECTION_WIDTH);
  46. // calculates the approximate distance by taken the maximum of the
  47. // absolute Distances of the x's or y's
  48. double dist = Math.max(Math.abs(max.x - min.x), Math.abs(max.y - min.y));
  49. // calls the actual calculation
  50. return getClosestEdge(pos, dist);
  51. }
  52. /**
  53. * Returns the closest Edge in the current Graph to a given position within
  54. * the given maxDistance.
  55. *
  56. * @param pos
  57. * The position. Expects a <b>Point3</b>, but only uses the x and
  58. * y coordinates
  59. * @param maxDistance
  60. * the maximum distance (in gu) the edge may be away from the
  61. * position to be considered as a valid edge
  62. * @return the closest Edge if a valid Edge exists, returns <b>null</b>
  63. * otherwise
  64. */
  65. public static MyEdge getClosestEdge(Point3 pos, double maxDistance) {
  66. double x0 = pos.x;
  67. double y0 = pos.y;
  68. // keeps track of the current smallest distance to an edge;
  69. // initializes to the given maxDistance
  70. double dist = maxDistance;
  71. // keeps track of the current closest edge
  72. MyEdge result = null;
  73. GraphManager gm = Main.getInstance().getGraphManager();
  74. // Iterates over every edge, calculates the distance and updates the
  75. // best edge and corresponding distance
  76. for (Iterator<MyEdge> iterator = gm.getGraph().getEdgeIterator(); iterator.hasNext();) {
  77. MyEdge edge = (MyEdge) iterator.next();
  78. // Get the positions of the nodes of the currently selected edge.
  79. double[] n1 = Toolkit.nodePosition(edge.getNode0());
  80. double[] n2 = Toolkit.nodePosition(edge.getNode1());
  81. // Extract the x and y values of the positions of the nodes
  82. double x1 = n1[0];
  83. double y1 = n1[1];
  84. double x2 = n2[0];
  85. double y2 = n2[1];
  86. // Calculate the distance between the nodes
  87. // Naming convention is view node 0 as A, node 1 as B and the point
  88. // as C in a triangle
  89. double c = distance(x1, y1, x2, y2); // Distance Node0 Node1
  90. // Calculate the distance between the edge and the point
  91. double cdist = Math.abs((y2 - y1) * x0 - (x2 - x1) * y0 + x2 * y1 - y2 * x1) / c;
  92. // Check if the edge is closer than the currently stored one
  93. if (cdist < dist) {
  94. // Calculate the distances from each node to the point
  95. double a = distance(x0, y0, x2, y2); // Distance Point Node0
  96. double b = distance(x0, y0, x1, y1); // Distance Point Node1
  97. // Precalculate the squares of the distances for later use
  98. double b2 = b * b;
  99. double a2 = a * a;
  100. double c2 = c * c;
  101. // Calculates the inner angles off the triangle
  102. double alpha = Math.acos((b2 + c2 - a2) / (2 * b * c));
  103. double beta = Math.acos((a2 + c2 - b2) / (2 * a * c));
  104. // Check if the point is actually visually next to the edge by
  105. // checking if both inner angles are less than 90°
  106. if (alpha <= HALF_PI && beta <= HALF_PI) {
  107. dist = cdist;
  108. result = edge;
  109. }
  110. }
  111. }
  112. return result;
  113. }
  114. /**
  115. * Calculates the distance between two given Nodes.
  116. *
  117. * @param a
  118. * Node 1
  119. * @param b
  120. * Node 2
  121. * @return the distance between the two Nodes as a double
  122. */
  123. public static double distance(MyNode a, MyNode b) {
  124. double[] n1 = Toolkit.nodePosition(a);
  125. double[] n2 = Toolkit.nodePosition(b);
  126. return distance(n1[0], n1[1], n2[0], n2[1]);
  127. }
  128. /**
  129. * Calculates the distance between a x,y position and a Node.
  130. *
  131. * @param x0
  132. * x cord of the position
  133. * @param y0
  134. * y cord of the position
  135. * @param a
  136. * the Node
  137. * @return the distance between the position and the Node as a double
  138. */
  139. public static double distance(double x0, double y0, MyNode a) {
  140. double[] n1 = Toolkit.nodePosition(a);
  141. return distance(x0, y0, n1[0], n1[1]);
  142. }
  143. /**
  144. * Calculates the distance between two x,y positions.
  145. *
  146. * @param x0
  147. * x cord of the first position
  148. * @param y0
  149. * y cord of the first position
  150. * @param x1
  151. * x cord of the second position
  152. * @param y1
  153. * y cord of the second position
  154. * @return the distance between the two positions as a double
  155. */
  156. public static double distance(double x0, double y0, double x1, double y1) {
  157. return Math.sqrt(Math.pow(y0 - y1, 2) + Math.pow(x0 - x1, 2));
  158. }
  159. }