Enforce immutability via final and unmodifiableList.
This commit is contained in:
parent
7640fa34c7
commit
d00e012497
@ -2,9 +2,10 @@ package org.insa.algo.weakconnectivity;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.Queue;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
import org.insa.algo.AbstractAlgorithm;
|
import org.insa.algo.AbstractAlgorithm;
|
||||||
import org.insa.algo.AbstractSolution.Status;
|
import org.insa.algo.AbstractSolution.Status;
|
||||||
@ -12,146 +13,149 @@ import org.insa.graph.Arc;
|
|||||||
import org.insa.graph.Graph;
|
import org.insa.graph.Graph;
|
||||||
import org.insa.graph.Node;
|
import org.insa.graph.Node;
|
||||||
|
|
||||||
public class WeaklyConnectedComponentsAlgorithm extends AbstractAlgorithm<WeaklyConnectedComponentObserver>{
|
public class WeaklyConnectedComponentsAlgorithm extends AbstractAlgorithm<WeaklyConnectedComponentObserver> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param instance
|
* @param instance
|
||||||
* @param logOutput
|
* @param logOutput
|
||||||
*/
|
*/
|
||||||
public WeaklyConnectedComponentsAlgorithm(WeaklyConnectedComponentsData instance) {
|
public WeaklyConnectedComponentsAlgorithm(WeaklyConnectedComponentsData instance) {
|
||||||
super(instance);
|
super(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WeaklyConnectedComponentsSolution run() {
|
public WeaklyConnectedComponentsSolution run() {
|
||||||
return (WeaklyConnectedComponentsSolution)super.run();
|
return (WeaklyConnectedComponentsSolution) super.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WeaklyConnectedComponentsData getInstance() {
|
public WeaklyConnectedComponentsData getInstance() {
|
||||||
return (WeaklyConnectedComponentsData)super.getInstance();
|
return (WeaklyConnectedComponentsData) super.getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Notify all observers that the algorithm is entering a new component.
|
* Notify all observers that the algorithm is entering a new component.
|
||||||
*
|
*
|
||||||
* @param curNode Starting node for the component.
|
* @param curNode Starting node for the component.
|
||||||
*/
|
*/
|
||||||
protected void notifyStartComponent(Node curNode) {
|
protected void notifyStartComponent(Node curNode) {
|
||||||
for (WeaklyConnectedComponentObserver obs: getObservers()) {
|
for (WeaklyConnectedComponentObserver obs: getObservers()) {
|
||||||
obs.notifyStartComponent(curNode);
|
obs.notifyStartComponent(curNode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Notify all observers that a new node has been found for the current component.
|
* Notify all observers that a new node has been found for the current
|
||||||
*
|
* component.
|
||||||
* @param node New node found for the current component.
|
*
|
||||||
*/
|
* @param node New node found for the current component.
|
||||||
protected void notifyNewNodeInComponent(Node node) {
|
*/
|
||||||
for (WeaklyConnectedComponentObserver obs: getObservers()) {
|
protected void notifyNewNodeInComponent(Node node) {
|
||||||
obs.notifyNewNodeInComponent(node);
|
for (WeaklyConnectedComponentObserver obs: getObservers()) {
|
||||||
}
|
obs.notifyNewNodeInComponent(node);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Notify all observers that the algorithm has computed a new component.
|
* Notify all observers that the algorithm has computed a new component.
|
||||||
*
|
*
|
||||||
* @param nodes List of nodes in the component.
|
* @param nodes List of nodes in the component.
|
||||||
*/
|
*/
|
||||||
protected void notifyEndComponent(ArrayList<Node> nodes) {
|
protected void notifyEndComponent(ArrayList<Node> nodes) {
|
||||||
for (WeaklyConnectedComponentObserver obs: getObservers()) {
|
for (WeaklyConnectedComponentObserver obs: getObservers()) {
|
||||||
obs.notifyEndComponent(nodes);
|
obs.notifyEndComponent(nodes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return An adjacency list for the undirected graph equivalent to the stored graph.
|
* @return An adjacency list for the undirected graph equivalent to the stored
|
||||||
*/
|
* graph.
|
||||||
protected ArrayList<HashSet<Integer>> createUndirectedGraph() {
|
*/
|
||||||
int nNodes = getInstance().getGraph().getNodes().size();
|
protected ArrayList<HashSet<Integer>> createUndirectedGraph() {
|
||||||
ArrayList<HashSet<Integer>> res = new ArrayList<HashSet<Integer>>(nNodes);
|
int nNodes = getInstance().getGraph().getNodes().size();
|
||||||
for (int i = 0; i < nNodes; ++i) {
|
ArrayList<HashSet<Integer>> res = new ArrayList<HashSet<Integer>>(nNodes);
|
||||||
res.add(new HashSet<Integer>());
|
for (int i = 0; i < nNodes; ++i) {
|
||||||
}
|
res.add(new HashSet<Integer>());
|
||||||
|
}
|
||||||
|
|
||||||
for (Node node: getInstance().getGraph().getNodes()) {
|
for (Node node: getInstance().getGraph().getNodes()) {
|
||||||
for (Arc arc: node.getSuccessors()) {
|
for (Arc arc: node.getSuccessors()) {
|
||||||
res.get(node.getId()).add(arc.getDestination().getId());
|
res.get(node.getId()).add(arc.getDestination().getId());
|
||||||
if (arc.getInfo().isOneWay()) {
|
if (arc.getInfo().isOneWay()) {
|
||||||
res.get(arc.getDestination().getId()).add(node.getId());
|
res.get(arc.getDestination().getId()).add(node.getId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply a breadth first search algorithm on the given undirected graph (adjacency list),
|
* Apply a breadth first search algorithm on the given undirected graph
|
||||||
* starting at node cur, and marking nodes in marked.
|
* (adjacency list), starting at node cur, and marking nodes in marked.
|
||||||
*
|
*
|
||||||
* @param marked
|
* @param marked
|
||||||
* @param cur
|
* @param cur
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
protected ArrayList<Node> bfs(ArrayList<HashSet<Integer>> ugraph, boolean[] marked, int cur) {
|
protected ArrayList<Node> bfs(ArrayList<HashSet<Integer>> ugraph, boolean[] marked, int cur) {
|
||||||
ArrayList<Node> nodes = getInstance().getGraph().getNodes();
|
List<Node> nodes = getInstance().getGraph().getNodes();
|
||||||
ArrayList<Node> component = new ArrayList<Node>();
|
ArrayList<Node> component = new ArrayList<Node>();
|
||||||
|
|
||||||
// Using a queue because we are doing a BFS
|
// Using a queue because we are doing a BFS
|
||||||
Queue<Integer> queue = new LinkedList<Integer>();
|
Queue<Integer> queue = new LinkedList<Integer>();
|
||||||
|
|
||||||
// Notify observers about the current component.
|
// Notify observers about the current component.
|
||||||
notifyStartComponent(nodes.get(cur));
|
notifyStartComponent(nodes.get(cur));
|
||||||
|
|
||||||
// Add original node and loop until the queue is empty.
|
// Add original node and loop until the queue is empty.
|
||||||
queue.add(cur);
|
queue.add(cur);
|
||||||
marked[cur] = true;
|
marked[cur] = true;
|
||||||
while (!queue.isEmpty()) {
|
while (!queue.isEmpty()) {
|
||||||
Node node = nodes.get(queue.remove());
|
Node node = nodes.get(queue.remove());
|
||||||
component.add(node);
|
component.add(node);
|
||||||
|
|
||||||
// Notify observers
|
// Notify observers
|
||||||
notifyNewNodeInComponent(node);
|
notifyNewNodeInComponent(node);
|
||||||
|
|
||||||
for (Integer destId: ugraph.get(node.getId())) {
|
for (Integer destId: ugraph.get(node.getId())) {
|
||||||
Node dest = nodes.get(destId);
|
Node dest = nodes.get(destId);
|
||||||
if (!marked[dest.getId()]) {
|
if (!marked[dest.getId()]) {
|
||||||
queue.add(destId);
|
queue.add(destId);
|
||||||
marked[destId] = true;
|
marked[destId] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
notifyEndComponent(component);
|
notifyEndComponent(component);
|
||||||
|
|
||||||
return component;
|
return component;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected WeaklyConnectedComponentsSolution doRun() {
|
protected WeaklyConnectedComponentsSolution doRun() {
|
||||||
|
|
||||||
Graph graph = getInstance().getGraph();
|
Graph graph = getInstance().getGraph();
|
||||||
ArrayList<HashSet<Integer>> ugraph = createUndirectedGraph();
|
ArrayList<HashSet<Integer>> ugraph = createUndirectedGraph();
|
||||||
boolean[] marked = new boolean[graph.getNodes().size()];
|
boolean[] marked = new boolean[graph.getNodes().size()];
|
||||||
Arrays.fill(marked, false);
|
Arrays.fill(marked, false);
|
||||||
|
|
||||||
ArrayList<ArrayList<Node>> components = new ArrayList<ArrayList<Node>>();
|
ArrayList<ArrayList<Node>> components = new ArrayList<ArrayList<Node>>();
|
||||||
|
|
||||||
// perform algorithm
|
// perform algorithm
|
||||||
int cur = 0;
|
int cur = 0;
|
||||||
while (cur < marked.length) {
|
while (cur < marked.length) {
|
||||||
// Apply BFS
|
// Apply BFS
|
||||||
components.add(this.bfs(ugraph, marked, cur));
|
components.add(this.bfs(ugraph, marked, cur));
|
||||||
|
|
||||||
// Find next non-marked
|
// Find next non-marked
|
||||||
for (; cur < marked.length && marked[cur]; ++cur);
|
for (; cur < marked.length && marked[cur]; ++cur)
|
||||||
}
|
;
|
||||||
|
}
|
||||||
|
|
||||||
return new WeaklyConnectedComponentsSolution(getInstance(), Status.OPTIMAL, components);
|
return new WeaklyConnectedComponentsSolution(getInstance(), Status.OPTIMAL, components);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,92 +1,95 @@
|
|||||||
package org.insa.graph;
|
package org.insa.graph;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Arc {
|
public class Arc {
|
||||||
|
|
||||||
// Destination node.
|
// Destination node.
|
||||||
private Node origin, destination;
|
private final Node origin, destination;
|
||||||
|
|
||||||
// Length of the road (in meters).
|
// Length of the road (in meters).
|
||||||
private int length;
|
private final int length;
|
||||||
|
|
||||||
// Road information.
|
// Road information.
|
||||||
RoadInformation info;
|
private final RoadInformation info;
|
||||||
|
|
||||||
// Segments.
|
// Segments.
|
||||||
ArrayList<Point> points;
|
private final ArrayList<Point> points;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param dest
|
* @param dest
|
||||||
* @param length
|
* @param length
|
||||||
* @param roadInformation
|
* @param roadInformation
|
||||||
* @param points
|
* @param points
|
||||||
*/
|
*/
|
||||||
public Arc(Node origin, Node dest, int length, RoadInformation roadInformation) {
|
public Arc(Node origin, Node dest, int length, RoadInformation roadInformation) {
|
||||||
this.origin = origin;
|
this.origin = origin;
|
||||||
this.destination = dest;
|
this.destination = dest;
|
||||||
this.length = length;
|
this.length = length;
|
||||||
this.info = roadInformation;
|
this.info = roadInformation;
|
||||||
this.points = new ArrayList<Point>();
|
this.points = new ArrayList<Point>();
|
||||||
origin.addSuccessor(this);
|
origin.addSuccessor(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param dest
|
* @param dest
|
||||||
* @param length
|
* @param length
|
||||||
* @param roadInformation
|
* @param roadInformation
|
||||||
* @param points
|
* @param points
|
||||||
*/
|
*/
|
||||||
public Arc(Node origin, Node dest, int length, RoadInformation roadInformation, ArrayList<Point> points) {
|
public Arc(Node origin, Node dest, int length, RoadInformation roadInformation, ArrayList<Point> points) {
|
||||||
this.origin = origin;
|
this.origin = origin;
|
||||||
this.destination = dest;
|
this.destination = dest;
|
||||||
this.length = length;
|
this.length = length;
|
||||||
this.info = roadInformation;
|
this.info = roadInformation;
|
||||||
this.points = points;
|
this.points = points;
|
||||||
origin.addSuccessor(this);
|
origin.addSuccessor(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Origin node of this arc.
|
* @return Origin node of this arc.
|
||||||
*/
|
*/
|
||||||
public Node getOrigin() {
|
public Node getOrigin() {
|
||||||
return origin;
|
return origin;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Destination node of this arc.
|
* @return Destination node of this arc.
|
||||||
*/
|
*/
|
||||||
public Node getDestination() {
|
public Node getDestination() {
|
||||||
return destination;
|
return destination;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Length of this arc, in meters.
|
* @return Length of this arc, in meters.
|
||||||
*/
|
*/
|
||||||
public int getLength() {
|
public int getLength() {
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Minimum time required to travel this arc, in seconds.
|
* @return Minimum time required to travel this arc, in seconds.
|
||||||
*/
|
*/
|
||||||
public double getMinimumTravelTime() {
|
public double getMinimumTravelTime() {
|
||||||
return getLength() * 3600.0 / (info.getMaximumSpeed() * 1000.0);
|
return getLength() * 3600.0 / (info.getMaximumSpeed() * 1000.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Road information for this arc.
|
* @return Road information for this arc.
|
||||||
*/
|
*/
|
||||||
public RoadInformation getInfo() {
|
public RoadInformation getInfo() {
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Points representing segments of this arc. This function may return an empty
|
* @return Points representing segments of this arc. This function may return an
|
||||||
* ArrayList if the segments are stored in the reversed arc (for two-ways road).
|
* empty ArrayList if the segments are stored in the reversed arc (for
|
||||||
*/
|
* two-ways road).
|
||||||
public ArrayList<Point> getPoints() {
|
*/
|
||||||
return points;
|
public List<Point> getPoints() {
|
||||||
}
|
return Collections.unmodifiableList(points);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,60 +1,66 @@
|
|||||||
package org.insa.graph ;
|
package org.insa.graph;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Graph {
|
public class Graph {
|
||||||
|
|
||||||
// Map identifier.
|
// Map identifier.
|
||||||
private int mapId;
|
private final int mapId;
|
||||||
|
|
||||||
// Nodes of the graph.
|
// Nodes of the graph.
|
||||||
private ArrayList<Node> nodes;
|
private final ArrayList<Node> nodes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mapId
|
* @param mapId
|
||||||
* @param nodes
|
* @param nodes
|
||||||
*/
|
*/
|
||||||
public Graph(int mapId, ArrayList<Node> nodes) {
|
public Graph(int mapId, ArrayList<Node> nodes) {
|
||||||
this.mapId = mapId;
|
this.mapId = mapId;
|
||||||
this.nodes = nodes;
|
this.nodes = nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Nodes of this graph.
|
* @return Nodes of this graph.
|
||||||
*/
|
*/
|
||||||
public ArrayList<Node> getNodes() { return nodes; }
|
public List<Node> getNodes() {
|
||||||
|
return Collections.unmodifiableList(nodes);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find the closet node to the given point.
|
* Find the closet node to the given point.
|
||||||
*
|
*
|
||||||
* @param point
|
* @param point
|
||||||
*
|
*
|
||||||
* @return Closest node to the given point.
|
* @return Closest node to the given point.
|
||||||
*/
|
*/
|
||||||
public Node findClosestNode(Point point) {
|
public Node findClosestNode(Point point) {
|
||||||
Node node = null;
|
Node node = null;
|
||||||
double minDis = Double.POSITIVE_INFINITY;
|
double minDis = Double.POSITIVE_INFINITY;
|
||||||
for (int n = 0 ; n < nodes.size(); ++n) {
|
for (int n = 0; n < nodes.size(); ++n) {
|
||||||
double dis = point.distanceTo(nodes.get(n).getPoint());
|
double dis = point.distanceTo(nodes.get(n).getPoint());
|
||||||
if (dis < minDis) {
|
if (dis < minDis) {
|
||||||
node = nodes.get(n);
|
node = nodes.get(n);
|
||||||
minDis = dis;
|
minDis = dis;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Map ID of this graph.
|
* @return Map ID of this graph.
|
||||||
*/
|
*/
|
||||||
public int getMapId() { return mapId; }
|
public int getMapId() {
|
||||||
|
return mapId;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Return the transpose graph of this graph.
|
* @return Return the transpose graph of this graph.
|
||||||
*/
|
*/
|
||||||
public Graph transpose() {
|
public Graph transpose() {
|
||||||
// TODO:
|
// TODO:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,65 +1,73 @@
|
|||||||
package org.insa.graph;
|
package org.insa.graph;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Node implements Comparable<Node> {
|
public class Node implements Comparable<Node> {
|
||||||
|
|
||||||
// ID of the node.
|
// ID of the node.
|
||||||
private int id;
|
private final int id;
|
||||||
|
|
||||||
// Point of this graph.
|
// Point of this graph.
|
||||||
private Point point;
|
private final Point point;
|
||||||
|
|
||||||
// Successors.
|
// Successors.
|
||||||
private ArrayList<Arc> successors;
|
private final ArrayList<Arc> successors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new Node corresponding to the given Point with
|
* Create a new Node corresponding to the given Point with an empty list of
|
||||||
* an empty list of successors.
|
* successors.
|
||||||
*
|
*
|
||||||
* @param point
|
* @param point
|
||||||
*/
|
*/
|
||||||
public Node(int id, Point point) {
|
public Node(int id, Point point) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.point = point;
|
this.point = point;
|
||||||
this.successors = new ArrayList<Arc>();
|
this.successors = new ArrayList<Arc>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a successor to this node.
|
* Add a successor to this node.
|
||||||
*
|
*
|
||||||
* @param arc Arc to the successor.
|
* @param arc Arc to the successor.
|
||||||
*/
|
*/
|
||||||
protected void addSuccessor(Arc arc) {
|
protected void addSuccessor(Arc arc) {
|
||||||
successors.add(arc);
|
successors.add(arc);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return ID of this node.
|
* @return ID of this node.
|
||||||
*/
|
*/
|
||||||
public int getId() { return id; }
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return List of successors of this node.
|
* @return List of successors of this node.
|
||||||
*/
|
*/
|
||||||
public ArrayList<Arc> getSuccessors() { return successors; }
|
public List<Arc> getSuccessors() {
|
||||||
|
return Collections.unmodifiableList(successors);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Point of this node.
|
* @return Point of this node.
|
||||||
*/
|
*/
|
||||||
public Point getPoint() { return point; }
|
public Point getPoint() {
|
||||||
|
return point;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object other) {
|
public boolean equals(Object other) {
|
||||||
if (other instanceof Node) {
|
if (other instanceof Node) {
|
||||||
return getId() == ((Node) other).getId();
|
return getId() == ((Node) other).getId();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(Node other) {
|
public int compareTo(Node other) {
|
||||||
return Integer.compare(getId(), other.getId());
|
return Integer.compare(getId(), other.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,61 +6,65 @@ package org.insa.graph;
|
|||||||
*/
|
*/
|
||||||
public class Point {
|
public class Point {
|
||||||
|
|
||||||
// Earth radius, in meters;
|
// Earth radius, in meters;
|
||||||
private static final double EARTH_RADIUS = 6378137.0 ;
|
private static final double EARTH_RADIUS = 6378137.0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute the distance between the two given points.
|
* Compute the distance between the two given points.
|
||||||
*
|
*
|
||||||
* @param long1
|
* @param long1
|
||||||
* @param lat1
|
* @param lat1
|
||||||
* @param long2
|
* @param long2
|
||||||
* @param lat2
|
* @param lat2
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static double distance(Point p1, Point p2) {
|
public static double distance(Point p1, Point p2) {
|
||||||
double sinLat = Math.sin(Math.toRadians(p1.getLatitude()))*Math.sin(Math.toRadians(p2.getLatitude()));
|
double sinLat = Math.sin(Math.toRadians(p1.getLatitude())) * Math.sin(Math.toRadians(p2.getLatitude()));
|
||||||
double cosLat = Math.cos(Math.toRadians(p1.getLatitude()))*Math.cos(Math.toRadians(p2.getLatitude()));
|
double cosLat = Math.cos(Math.toRadians(p1.getLatitude())) * Math.cos(Math.toRadians(p2.getLatitude()));
|
||||||
double cosLong = Math.cos(Math.toRadians(p2.getLongitude() - p1.getLongitude()));
|
double cosLong = Math.cos(Math.toRadians(p2.getLongitude() - p1.getLongitude()));
|
||||||
return EARTH_RADIUS * Math.acos(sinLat+cosLat*cosLong);
|
return EARTH_RADIUS * Math.acos(sinLat + cosLat * cosLong);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Longitude and latitude of the point.
|
// Longitude and latitude of the point.
|
||||||
private double longitude, latitude;
|
private final double longitude, latitude;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param longitude Longitude of the point, in degrees.
|
* @param longitude Longitude of the point, in degrees.
|
||||||
* @param latitude Latitude of the point, in degrees.
|
* @param latitude Latitude of the point, in degrees.
|
||||||
*/
|
*/
|
||||||
public Point(double longitude, double latitude) {
|
public Point(double longitude, double latitude) {
|
||||||
this.longitude = longitude;
|
this.longitude = longitude;
|
||||||
this.latitude = latitude;
|
this.latitude = latitude;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Longitude of this point (in degrees).
|
* @return Longitude of this point (in degrees).
|
||||||
*/
|
*/
|
||||||
public double getLongitude() { return longitude; }
|
public double getLongitude() {
|
||||||
|
return longitude;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Latitude of this point (in degrees).
|
* @return Latitude of this point (in degrees).
|
||||||
*/
|
*/
|
||||||
public double getLatitude() { return latitude; }
|
public double getLatitude() {
|
||||||
|
return latitude;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute the distance from this point to the given point
|
* Compute the distance from this point to the given point
|
||||||
*
|
*
|
||||||
* @param target Target point.
|
* @param target Target point.
|
||||||
*
|
*
|
||||||
* @return Distane between this point and the target point, in meters.
|
* @return Distane between this point and the target point, in meters.
|
||||||
*/
|
*/
|
||||||
public double distanceTo(Point target) {
|
public double distanceTo(Point target) {
|
||||||
return distance(this, target);
|
return distance(this, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("Point(%f, %f)", getLongitude(), getLatitude());
|
return String.format("Point(%f, %f)", getLongitude(), getLatitude());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,85 +1,75 @@
|
|||||||
package org.insa.graph ;
|
package org.insa.graph;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class containing information for road that may be shared
|
* Class containing information for road that may be shared by multiple arcs.
|
||||||
* by multiple arcs.
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class RoadInformation {
|
public class RoadInformation {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Road type.
|
* Road type.
|
||||||
*/
|
*/
|
||||||
public enum RoadType {
|
public enum RoadType {
|
||||||
MOTORWAY,
|
MOTORWAY, TRUNK, PRIMARY, SECONDARY, MOTORWAY_LINK, TRUNK_LINK, PRIMARY_LINK, SECONDARY_LINK, TERTIARY, RESIDENTIAL, UNCLASSIFIED, ROAD, LIVING_STREET, SERVICE, ROUNDABOUT, COASTLINE
|
||||||
TRUNK,
|
}
|
||||||
PRIMARY,
|
|
||||||
SECONDARY,
|
|
||||||
MOTORWAY_LINK,
|
|
||||||
TRUNK_LINK,
|
|
||||||
PRIMARY_LINK,
|
|
||||||
SECONDARY_LINK,
|
|
||||||
TERTIARY,
|
|
||||||
RESIDENTIAL,
|
|
||||||
UNCLASSIFIED,
|
|
||||||
ROAD,
|
|
||||||
LIVING_STREET,
|
|
||||||
SERVICE,
|
|
||||||
ROUNDABOUT,
|
|
||||||
COASTLINE
|
|
||||||
}
|
|
||||||
|
|
||||||
// Type of the road (see above).
|
// Type of the road (see above).
|
||||||
private RoadType type;
|
private final RoadType type;
|
||||||
|
|
||||||
// One way road?
|
// One way road?
|
||||||
private boolean oneway;
|
private final boolean oneway;
|
||||||
|
|
||||||
// Max speed in kilometers per hour.
|
// Max speed in kilometers per hour.
|
||||||
private int maxSpeed;
|
private final int maxSpeed;
|
||||||
|
|
||||||
// Name of the road.
|
// Name of the road.
|
||||||
private String name;
|
private final String name;
|
||||||
|
|
||||||
public RoadInformation(RoadType roadType, boolean isOneWay, int maxSpeed, String name) {
|
public RoadInformation(RoadType roadType, boolean isOneWay, int maxSpeed, String name) {
|
||||||
this.type = roadType;
|
this.type = roadType;
|
||||||
this.oneway = isOneWay;
|
this.oneway = isOneWay;
|
||||||
this.maxSpeed = maxSpeed;
|
this.maxSpeed = maxSpeed;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Type of the road.
|
* @return Type of the road.
|
||||||
*/
|
*/
|
||||||
public RoadType getType() { return type; }
|
public RoadType getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true if this is a one-way road.
|
* @return true if this is a one-way road.
|
||||||
*/
|
*/
|
||||||
public boolean isOneWay() { return oneway; }
|
public boolean isOneWay() {
|
||||||
|
return oneway;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Maximum speed for this road (in km/h).
|
* @return Maximum speed for this road (in km/h).
|
||||||
*/
|
*/
|
||||||
public int getMaximumSpeed() { return maxSpeed; }
|
public int getMaximumSpeed() {
|
||||||
|
return maxSpeed;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Name of the road.
|
* @return Name of the road.
|
||||||
*/
|
*/
|
||||||
public String getName() { return name; }
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String typeAsString = "road";
|
String typeAsString = "road";
|
||||||
if (getType() == RoadType.COASTLINE) {
|
if (getType() == RoadType.COASTLINE) {
|
||||||
typeAsString = "coast";
|
typeAsString = "coast";
|
||||||
}
|
}
|
||||||
if (getType() == RoadType.MOTORWAY) {
|
if (getType() == RoadType.MOTORWAY) {
|
||||||
typeAsString = "highway";
|
typeAsString = "highway";
|
||||||
}
|
}
|
||||||
return typeAsString + " : " + getName()
|
return typeAsString + " : " + getName() + " " + (isOneWay() ? " (oneway) " : "") + maxSpeed + " km/h (max.)";
|
||||||
+ " " + (isOneWay() ? " (oneway) " : "")
|
}
|
||||||
+ maxSpeed + " km/h (max.)";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,9 @@ import java.awt.event.MouseListener;
|
|||||||
import java.awt.geom.NoninvertibleTransformException;
|
import java.awt.geom.NoninvertibleTransformException;
|
||||||
import java.awt.geom.Point2D;
|
import java.awt.geom.Point2D;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.IdentityHashMap;
|
import java.util.IdentityHashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
@ -238,14 +238,12 @@ public class BasicDrawing extends JPanel implements Drawing {
|
|||||||
/**
|
/**
|
||||||
* Draw the given arc.
|
* Draw the given arc.
|
||||||
*
|
*
|
||||||
* @param arc
|
* @param arc Arc to draw.
|
||||||
* Arc to draw.
|
* @param palette Palette to use to retrieve color and width for arc, or null to
|
||||||
* @param palette
|
* use current settings.
|
||||||
* Palette to use to retrieve color and width for arc, or null to use
|
|
||||||
* current settings.
|
|
||||||
*/
|
*/
|
||||||
public void drawArc(Arc arc, GraphPalette palette) {
|
public void drawArc(Arc arc, GraphPalette palette) {
|
||||||
ArrayList<Point> pts = arc.getPoints();
|
List<Point> pts = arc.getPoints();
|
||||||
if (!pts.isEmpty()) {
|
if (!pts.isEmpty()) {
|
||||||
if (palette != null) {
|
if (palette != null) {
|
||||||
setColor(palette.getColorForType(arc.getInfo().getType()));
|
setColor(palette.getColorForType(arc.getInfo().getType()));
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package org.insa.graphics.drawing.utils;
|
package org.insa.graphics.drawing.utils;
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.util.ArrayList;
|
import java.util.List;
|
||||||
|
|
||||||
import org.insa.graph.Point;
|
import org.insa.graph.Point;
|
||||||
import org.mapsforge.core.graphics.Canvas;
|
import org.mapsforge.core.graphics.Canvas;
|
||||||
@ -41,7 +41,7 @@ public class PolylineAutoScaling extends Polyline {
|
|||||||
/**
|
/**
|
||||||
* @param points
|
* @param points
|
||||||
*/
|
*/
|
||||||
public void add(ArrayList<Point> points) {
|
public void add(List<Point> points) {
|
||||||
for (Point point: points) {
|
for (Point point: points) {
|
||||||
add(point);
|
add(point);
|
||||||
}
|
}
|
||||||
|
@ -1,60 +1,53 @@
|
|||||||
package org.insa.graph.io;
|
package org.insa.graph.io;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.List;
|
||||||
|
|
||||||
import org.insa.graph.Graph;
|
import org.insa.graph.Graph;
|
||||||
import org.insa.graph.Node;
|
import org.insa.graph.Node;
|
||||||
import org.insa.graph.Point;
|
import org.insa.graph.Point;
|
||||||
import org.junit.jupiter.api.AfterAll;
|
|
||||||
import org.junit.jupiter.api.AfterEach;
|
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Disabled;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
|
||||||
import org.junit.jupiter.params.provider.MethodSource;
|
|
||||||
|
|
||||||
public class BinaryGraphReaderTest {
|
public class BinaryGraphReaderTest {
|
||||||
|
|
||||||
// Epsilon for latitude and longitude.
|
// Epsilon for latitude and longitude.
|
||||||
private static final double EPS = 1e-5;
|
private static final double EPS = 1e-5;
|
||||||
|
|
||||||
private static Graph midip;
|
private static Graph midip;
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
static void initAll() throws IOException {
|
static void initAll() throws IOException {
|
||||||
BinaryGraphReader reader = new BinaryGraphReader(Openfile.open("midip"));
|
BinaryGraphReader reader = new BinaryGraphReader(Openfile.open("midip"));
|
||||||
midip = reader.read();
|
midip = reader.read();
|
||||||
}
|
}
|
||||||
|
|
||||||
void assertPointAt(Point p1, double longitude, double latitude) {
|
void assertPointAt(Point p1, double longitude, double latitude) {
|
||||||
assertEquals(p1.getLongitude(), longitude, EPS);
|
assertEquals(p1.getLongitude(), longitude, EPS);
|
||||||
assertEquals(p1.getLatitude(), latitude, EPS);
|
assertEquals(p1.getLatitude(), latitude, EPS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testMidipNodes() {
|
void testMidipNodes() {
|
||||||
ArrayList<Node> nodes = midip.getNodes();
|
List<Node> nodes = midip.getNodes();
|
||||||
|
|
||||||
assertEquals(nodes.size(), 150827);
|
assertEquals(nodes.size(), 150827);
|
||||||
|
|
||||||
// Check the locations of some nodes.
|
// Check the locations of some nodes.
|
||||||
assertPointAt(nodes.get(58411).getPoint(), 1.799864, 43.92864);
|
assertPointAt(nodes.get(58411).getPoint(), 1.799864, 43.92864);
|
||||||
assertPointAt(nodes.get(133312).getPoint(), 0.539752, 43.317505);
|
assertPointAt(nodes.get(133312).getPoint(), 0.539752, 43.317505);
|
||||||
assertPointAt(nodes.get(113688).getPoint(), 1.682739, 44.799774);
|
assertPointAt(nodes.get(113688).getPoint(), 1.682739, 44.799774);
|
||||||
assertPointAt(nodes.get(118141).getPoint(), 0.274857, 43.47475);
|
assertPointAt(nodes.get(118141).getPoint(), 0.274857, 43.47475);
|
||||||
assertPointAt(nodes.get(146918).getPoint(), 0.116148, 43.811386);
|
assertPointAt(nodes.get(146918).getPoint(), 0.116148, 43.811386);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testMidipArcs() {
|
void testMidipArcs() {
|
||||||
// TODO: Check the number of edges.
|
// TODO: Check the number of edges.
|
||||||
// TODO: Check information for some edges.
|
// TODO: Check information for some edges.
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user