No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GraphStatistics.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package org.insa.graph;
  2. /**
  3. * Utility class that stores some statistics of graphs that are not easy to
  4. * access.
  5. *
  6. * This class is used to provide O(1) access to information in graph that do not
  7. * change, and that usually require O(n) to compute.
  8. *
  9. */
  10. public class GraphStatistics {
  11. /**
  12. * Special value used to indicate that the graph has no maximum speed limit
  13. * (some roads are not limited).
  14. */
  15. public static final int NO_MAXIMUM_SPEED = -1;
  16. /**
  17. * Class representing a bounding box for a graph (a rectangle that contains all
  18. * nodes in the graph).
  19. *
  20. */
  21. public static class BoundingBox {
  22. private final Point topLeft, bottomRight;
  23. /**
  24. * Create a new BoundingBox represented by the given top-left and bottom-right
  25. * points.
  26. *
  27. * @param topLeft Top left corner of the bounding box.
  28. * @param bottomRight Bottom right corner of the bounding box.
  29. */
  30. public BoundingBox(Point topLeft, Point bottomRight) {
  31. this.topLeft = topLeft;
  32. this.bottomRight = bottomRight;
  33. }
  34. /**
  35. * @return Bottom-right point of this boundin box.
  36. */
  37. public Point getBottomRightPoint() {
  38. return bottomRight;
  39. }
  40. /**
  41. * @return Top-left point of this bounding box.
  42. */
  43. public Point getTopLeftPoint() {
  44. return topLeft;
  45. }
  46. /**
  47. * Create a new bounding box by extending the current one according to the given
  48. * value for each side.
  49. *
  50. * @param left Extra size to add to the left of the box.
  51. * @param top Extra size to add to the top of the box.
  52. * @param right Extra size to add to the right of the box.
  53. * @param bottom Extra size to add to the bottom of the box.
  54. *
  55. * @return New bounding box corresponding to an extension of the current one.
  56. */
  57. public BoundingBox extend(float left, float top, float right, float bottom) {
  58. return new BoundingBox(
  59. new Point(this.topLeft.getLongitude() - left, this.topLeft.getLatitude() + top),
  60. new Point(this.bottomRight.getLongitude() + right,
  61. this.bottomRight.getLatitude() - bottom));
  62. }
  63. /**
  64. * Create a new bounding box by extending the current one according by the given
  65. * value on each side.
  66. *
  67. * @param size Extra size to add to each side of this box.
  68. *
  69. * @return New bounding box corresponding to an extension of the current one.
  70. */
  71. public BoundingBox extend(float size) {
  72. return this.extend(size, size, size, size);
  73. }
  74. /**
  75. * @param point Point to check
  76. *
  77. * @return true if this box contains the given point.
  78. */
  79. public boolean contains(Point point) {
  80. return this.bottomRight.getLatitude() <= point.getLatitude()
  81. && this.topLeft.getLatitude() >= point.getLatitude()
  82. && this.topLeft.getLongitude() <= point.getLongitude()
  83. && this.bottomRight.getLongitude() >= point.getLongitude();
  84. }
  85. /**
  86. * @return true if this box contains the given box.
  87. */
  88. public boolean contains(BoundingBox other) {
  89. return this.contains(other.bottomRight) && this.contains(other.topLeft);
  90. }
  91. @Override
  92. public String toString() {
  93. return "BoundingBox(topLeft=" + this.topLeft + ", bottomRight=" + this.bottomRight
  94. + ")";
  95. }
  96. }
  97. // Bounding box for this graph.
  98. private final BoundingBox boundingBox;
  99. // Number of roads
  100. private final int nbRoadOneWay, nbRoadTwoWays;
  101. // Maximum speed on this graph (in kmph).
  102. private final int maximumSpeed;
  103. // Maximum length of any arc on this graph.
  104. private final float maximumLength;
  105. /**
  106. * Create a new GraphStatistics instance with the given value.
  107. *
  108. * @param boundingBox Bounding-box for the graph.
  109. * @param nbRoadOneWay Number of one-way roads in the graph.
  110. * @param nbRoadTwoWays Number of two-ways roads in the graph.
  111. * @param maximumSpeed Maximum speed of any road of the graph. You can use
  112. * {@link #NO_MAXIMUM_SPEED} to indicate that the graph has no maximum
  113. * speed limit.
  114. * @param maximumLength Maximum length of any arc of the graph.
  115. */
  116. public GraphStatistics(BoundingBox boundingBox, int nbRoadOneWay, int nbRoadTwoWays,
  117. int maximumSpeed, float maximumLength) {
  118. this.boundingBox = boundingBox;
  119. this.nbRoadOneWay = nbRoadOneWay;
  120. this.nbRoadTwoWays = nbRoadTwoWays;
  121. this.maximumLength = maximumLength;
  122. this.maximumSpeed = maximumSpeed;
  123. }
  124. /**
  125. * @return The bounding box for this graph.
  126. */
  127. public BoundingBox getBoundingBox() {
  128. return this.boundingBox;
  129. }
  130. /**
  131. * @return Amount of one-way roads in this graph.
  132. */
  133. public int getOneWayRoadCount() {
  134. return this.nbRoadOneWay;
  135. }
  136. /**
  137. * @return Amount of two-ways roads in this graph.
  138. */
  139. public int getTwoWaysRoadCount() {
  140. return this.nbRoadTwoWays;
  141. }
  142. /**
  143. * @return Number of arcs in this graph.
  144. *
  145. * @see #getOneWayRoadCount()
  146. * @see #getTwoWaysRoadCount()
  147. */
  148. public int getArcCount() {
  149. return getOneWayRoadCount() + 2 * getTwoWaysRoadCount();
  150. }
  151. /**
  152. * @return true if this graph has a maximum speed limit, false otherwise.
  153. */
  154. public boolean hasMaximumSpeed() {
  155. return this.maximumLength != NO_MAXIMUM_SPEED;
  156. }
  157. /**
  158. * @return Maximum speed of any arc in the graph, or {@link #NO_MAXIMUM_SPEED}
  159. * if some roads have no speed limitation.
  160. */
  161. public int getMaximumSpeed() {
  162. return this.maximumSpeed;
  163. }
  164. /**
  165. * @return Maximum length of any arc in the graph.
  166. */
  167. public float getMaximumLength() {
  168. return this.maximumLength;
  169. }
  170. }