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 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. // Bounding box for this graph.
  76. private final BoundingBox boundingBox;
  77. // Number of roads
  78. private final int nbRoadOneWay, nbRoadTwoWays;
  79. // Maximum speed on this graph (in kmph).
  80. private final int maximumSpeed;
  81. // Maximum length of any arc on this graph.
  82. private final float maximumLength;
  83. /**
  84. * Create a new GraphStatistics instance with the given value.
  85. *
  86. * @param boundingBox Bounding-box for the graph.
  87. * @param nbRoadOneWay Number of one-way roads in the graph.
  88. * @param nbRoadTwoWays Number of two-ways roads in the graph.
  89. * @param maximumSpeed Maximum speed of any road of the graph. You can use
  90. * {@link #NO_MAXIMUM_SPEED} to indicate that the graph has no maximum
  91. * speed limit.
  92. * @param maximumLength Maximum length of any arc of the graph.
  93. */
  94. public GraphStatistics(BoundingBox boundingBox, int nbRoadOneWay, int nbRoadTwoWays,
  95. int maximumSpeed, float maximumLength) {
  96. this.boundingBox = boundingBox;
  97. this.nbRoadOneWay = nbRoadOneWay;
  98. this.nbRoadTwoWays = nbRoadTwoWays;
  99. this.maximumLength = maximumLength;
  100. this.maximumSpeed = maximumSpeed;
  101. }
  102. /**
  103. * @return The bounding box for this graph.
  104. */
  105. public BoundingBox getBoundingBox() {
  106. return this.boundingBox;
  107. }
  108. /**
  109. * @return Amount of one-way roads in this graph.
  110. */
  111. public int getOneWayRoadCount() {
  112. return this.nbRoadOneWay;
  113. }
  114. /**
  115. * @return Amount of two-ways roads in this graph.
  116. */
  117. public int getTwoWaysRoadCount() {
  118. return this.nbRoadTwoWays;
  119. }
  120. /**
  121. * @return Number of arcs in this graph.
  122. *
  123. * @see #getOneWayRoadCount()
  124. * @see #getTwoWaysRoadCount()
  125. */
  126. public int getArcCount() {
  127. return getOneWayRoadCount() + 2 * getTwoWaysRoadCount();
  128. }
  129. /**
  130. * @return true if this graph has a maximum speed limit, false otherwise.
  131. */
  132. public boolean hasMaximumSpeed() {
  133. return this.maximumLength != NO_MAXIMUM_SPEED;
  134. }
  135. /**
  136. * @return Maximum speed of any arc in the graph, or {@link #NO_MAXIMUM_SPEED}
  137. * if some roads have no speed limitation.
  138. */
  139. public int getMaximumSpeed() {
  140. return this.maximumSpeed;
  141. }
  142. /**
  143. * @return Maximum length of any arc in the graph.
  144. */
  145. public float getMaximumLength() {
  146. return this.maximumLength;
  147. }
  148. }