Switch to Maven project.

This commit is contained in:
Mikaël Capelle 2020-02-23 16:30:06 +01:00
parent fd503d148e
commit 5bb454a3b2
139 changed files with 2487 additions and 2198 deletions

View File

@ -1,69 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="libs/kxml2-2.3.0.jar"/>
<classpathentry kind="lib" path="libs/mapsforge-core-0.9.1.jar">
<attributes>
<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-reloaded/libs/mapsforge-core-0.9.1-javadoc.jar!/"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="libs/mapsforge-map-0.9.1.jar">
<attributes>
<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-reloaded/libs/mapsforge-map-0.9.1-javadoc.jar!/"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="libs/mapsforge-map-awt-0.9.1.jar">
<attributes>
<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-reloaded/libs/mapsforge-map-awt-0.9.1-javadoc.jar!/"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="libs/mapsforge-map-reader-0.9.1.jar">
<attributes>
<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-reloaded/libs/mapsforge-map-reader-0.9.1-javadoc.jar!/"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="libs/mapsforge-poi-0.9.1.jar">
<attributes>
<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-reloaded/libs/mapsforge-poi-0.9.1-javadoc.jar!/"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="libs/mapsforge-poi-awt-0.9.1.jar">
<attributes>
<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-reloaded/libs/mapsforge-poi-awt-0.9.1-javadoc.jar!/"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="libs/mapsforge-themes-0.9.1.jar">
<attributes>
<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-reloaded/libs/mapsforge-themes-0.9.1-javadoc.jar!/"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="libs/svg-salamander-1.0.jar">
<attributes>
<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-reloaded/libs/svg-salamander-1.0-javadoc.jar!/"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="res"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

4
.gitignore vendored
View File

@ -6,12 +6,14 @@ bin
target
doc
*.jar
.settings
.classpath
# Editor specific files and folders
*~
.project
# Project speicific files and folders
# Project specific files and folders
*.mapfg
*.mapgr
*.path

View File

@ -1,5 +0,0 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8

View File

@ -1,4 +0,0 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

25
be-graphes-algos/pom.xml Normal file
View File

@ -0,0 +1,25 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.insa.graphs</groupId>
<artifactId>be-graphes-all</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>be-graphes-algos</artifactId>
<name>be-graphes-algos</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.insa.graphs</groupId>
<artifactId>be-graphes-model</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -1,4 +1,4 @@
package org.insa.algo;
package org.insa.graphs.algorithm;
import java.time.Duration;
import java.time.Instant;

View File

@ -1,8 +1,8 @@
package org.insa.algo;
package org.insa.graphs.algorithm;
import org.insa.graph.Arc;
import org.insa.graph.Graph;
import org.insa.graph.GraphStatistics;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.GraphStatistics;
/**
* Base class for algorithm input data classes. This class contains the basic

View File

@ -1,4 +1,4 @@
package org.insa.algo;
package org.insa.graphs.algorithm;
import java.time.Duration;
@ -38,6 +38,11 @@ public abstract class AbstractSolution {
this.status = Status.UNKNOWN;
}
/**
*
* @param data
* @param status
*/
protected AbstractSolution(AbstractInputData data, Status status) {
this.data = data;
this.status = status;

View File

@ -1,4 +1,4 @@
package org.insa.algo;
package org.insa.graphs.algorithm;
import java.lang.reflect.Constructor;
import java.util.IdentityHashMap;
@ -7,11 +7,11 @@ import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.insa.algo.shortestpath.AStarAlgorithm;
import org.insa.algo.shortestpath.BellmanFordAlgorithm;
import org.insa.algo.shortestpath.DijkstraAlgorithm;
import org.insa.algo.shortestpath.ShortestPathAlgorithm;
import org.insa.algo.weakconnectivity.WeaklyConnectedComponentsAlgorithm;
import org.insa.graphs.algorithm.shortestpath.AStarAlgorithm;
import org.insa.graphs.algorithm.shortestpath.BellmanFordAlgorithm;
import org.insa.graphs.algorithm.shortestpath.DijkstraAlgorithm;
import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm;
import org.insa.graphs.algorithm.weakconnectivity.WeaklyConnectedComponentsAlgorithm;
/**
* Factory class used to register and retrieve algorithms based on their common
@ -43,10 +43,10 @@ public class AlgorithmFactory {
* the given base algorithm.
*
* @param baseAlgorithm Base algorithm class that corresponds to the newly
* registered algorithm class (e.g., generic algorithm class for the
* problem).
* @param name Name for the registered algorithm class.
* @param algoClass Algorithm class to register.
* registered algorithm class (e.g., generic algorithm
* class for the problem).
* @param name Name for the registered algorithm class.
* @param algoClass Algorithm class to register.
*/
public static void registerAlgorithm(Class<? extends AbstractAlgorithm<?>> baseAlgorithm,
String name, Class<? extends AbstractAlgorithm<?>> algoClass) {
@ -62,14 +62,14 @@ public class AlgorithmFactory {
* an object equivalent to `new Algorithm(data)`.
*
* @param algorithm Class of the algorithm to create.
* @param data Input data for the algorithm.
* @param data Input data for the algorithm.
*
* @return A new instance of the given algorithm class using the given data.
*
* @throws Exception if something wrong happens when constructing the object,
* i.e. the given input data does not correspond to the given algorithm
* and/or no constructor that takes a single parameter of type
* (data.getClass()) exists.
* i.e. the given input data does not correspond to the given
* algorithm and/or no constructor that takes a single
* parameter of type (data.getClass()) exists.
*/
public static AbstractAlgorithm<?> createAlgorithm(
Class<? extends AbstractAlgorithm<?>> algorithm, AbstractInputData data)
@ -96,7 +96,7 @@ public class AlgorithmFactory {
* registerAlgorithm.
*
* @param baseAlgorithm Base algorithm class for the algorithm to retrieve.
* @param name Name of the algorithm to retrieve.
* @param name Name of the algorithm to retrieve.
*
* @return Class corresponding to the given name.
*
@ -112,7 +112,7 @@ public class AlgorithmFactory {
* for the given base algorithm class.
*
* @param baseAlgorithm Base algorithm class for the algorithm class names to
* retrieve.
* retrieve.
*
* @return Names of the currently registered algorithms.
*

View File

@ -1,8 +1,8 @@
package org.insa.algo;
package org.insa.graphs.algorithm;
import org.insa.algo.AbstractInputData.Mode;
import org.insa.graph.Arc;
import org.insa.graph.GraphStatistics;
import org.insa.graphs.algorithm.AbstractInputData.Mode;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.GraphStatistics;
/**
* This class can be used to indicate to an algorithm which arcs can be used and

View File

@ -1,14 +1,14 @@
package org.insa.algo;
package org.insa.graphs.algorithm;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import org.insa.algo.AbstractInputData.Mode;
import org.insa.graph.AccessRestrictions.AccessMode;
import org.insa.graph.AccessRestrictions.AccessRestriction;
import org.insa.graph.Arc;
import org.insa.graph.GraphStatistics;
import org.insa.graphs.algorithm.AbstractInputData.Mode;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.GraphStatistics;
import org.insa.graphs.model.AccessRestrictions.AccessMode;
import org.insa.graphs.model.AccessRestrictions.AccessRestriction;
public class ArcInspectorFactory {

View File

@ -1,6 +1,6 @@
package org.insa.algo.carpooling;
package org.insa.graphs.algorithm.carpooling;
import org.insa.algo.AbstractAlgorithm;
import org.insa.graphs.algorithm.AbstractAlgorithm;
public abstract class CarPoolingAlgorithm extends AbstractAlgorithm<CarPoolingObserver> {

View File

@ -0,0 +1,13 @@
package org.insa.graphs.algorithm.carpooling;
import org.insa.graphs.algorithm.AbstractInputData;
import org.insa.graphs.algorithm.ArcInspector;
import org.insa.graphs.model.Graph;
public class CarPoolingData extends AbstractInputData {
protected CarPoolingData(Graph graph, ArcInspector arcFilter) {
super(graph, arcFilter);
}
}

View File

@ -1,4 +1,4 @@
package org.insa.algo.carpooling;
package org.insa.graphs.algorithm.carpooling;
public class CarPoolingGraphicObserver implements CarPoolingObserver {

View File

@ -0,0 +1,5 @@
package org.insa.graphs.algorithm.carpooling;
public interface CarPoolingObserver {
}

View File

@ -1,6 +1,6 @@
package org.insa.algo.carpooling;
package org.insa.graphs.algorithm.carpooling;
import org.insa.algo.AbstractSolution;
import org.insa.graphs.algorithm.AbstractSolution;
public class CarPoolingSolution extends AbstractSolution {

View File

@ -1,4 +1,4 @@
package org.insa.algo.carpooling;
package org.insa.graphs.algorithm.carpooling;
public class CarPoolingTextObserver implements CarPoolingObserver {

View File

@ -1,6 +1,6 @@
package org.insa.algo.packageswitch;
package org.insa.graphs.algorithm.packageswitch;
import org.insa.algo.AbstractAlgorithm;
import org.insa.graphs.algorithm.AbstractAlgorithm;
public abstract class PackageSwitchAlgorithm extends AbstractAlgorithm<PackageSwitchObserver> {

View File

@ -0,0 +1,13 @@
package org.insa.graphs.algorithm.packageswitch;
import org.insa.graphs.algorithm.AbstractInputData;
import org.insa.graphs.algorithm.ArcInspector;
import org.insa.graphs.model.Graph;
public class PackageSwitchData extends AbstractInputData {
protected PackageSwitchData(Graph graph, ArcInspector arcFilter) {
super(graph, arcFilter);
}
}

View File

@ -1,4 +1,4 @@
package org.insa.algo.packageswitch;
package org.insa.graphs.algorithm.packageswitch;
public class PackageSwitchGraphicObserver implements PackageSwitchObserver {

View File

@ -0,0 +1,5 @@
package org.insa.graphs.algorithm.packageswitch;
public interface PackageSwitchObserver {
}

View File

@ -1,6 +1,6 @@
package org.insa.algo.packageswitch;
package org.insa.graphs.algorithm.packageswitch;
import org.insa.algo.AbstractSolution;
import org.insa.graphs.algorithm.AbstractSolution;
public class PackageSwitchSolution extends AbstractSolution {

View File

@ -1,4 +1,4 @@
package org.insa.algo.packageswitch;
package org.insa.graphs.algorithm.packageswitch;
public class PackageSwitchTextObserver implements PackageSwitchObserver {

View File

@ -1,4 +1,4 @@
package org.insa.algo.shortestpath;
package org.insa.graphs.algorithm.shortestpath;
public class AStarAlgorithm extends DijkstraAlgorithm {

View File

@ -1,14 +1,14 @@
package org.insa.algo.shortestpath;
package org.insa.graphs.algorithm.shortestpath;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import org.insa.algo.AbstractSolution.Status;
import org.insa.graph.Arc;
import org.insa.graph.Graph;
import org.insa.graph.Node;
import org.insa.graph.Path;
import org.insa.graphs.algorithm.AbstractSolution.Status;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Path;
public class BellmanFordAlgorithm extends ShortestPathAlgorithm {

View File

@ -1,4 +1,4 @@
package org.insa.algo.shortestpath;
package org.insa.graphs.algorithm.shortestpath;
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
@ -8,7 +8,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
@Override
protected ShortestPathSolution doRun() {
ShortestPathData data = getInputData();
final ShortestPathData data = getInputData();
ShortestPathSolution solution = null;
// TODO:
return solution;

View File

@ -1,7 +1,7 @@
package org.insa.algo.shortestpath;
package org.insa.graphs.algorithm.shortestpath;
import org.insa.algo.AbstractAlgorithm;
import org.insa.graph.Node;
import org.insa.graphs.algorithm.AbstractAlgorithm;
import org.insa.graphs.model.Node;
public abstract class ShortestPathAlgorithm extends AbstractAlgorithm<ShortestPathObserver> {

View File

@ -1,9 +1,9 @@
package org.insa.algo.shortestpath;
package org.insa.graphs.algorithm.shortestpath;
import org.insa.algo.AbstractInputData;
import org.insa.algo.ArcInspector;
import org.insa.graph.Graph;
import org.insa.graph.Node;
import org.insa.graphs.algorithm.AbstractInputData;
import org.insa.graphs.algorithm.ArcInspector;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node;
public class ShortestPathData extends AbstractInputData {

View File

@ -1,6 +1,6 @@
package org.insa.algo.shortestpath;
package org.insa.graphs.algorithm.shortestpath;
import org.insa.graph.Node;
import org.insa.graphs.model.Node;
public interface ShortestPathObserver {

View File

@ -1,21 +1,14 @@
package org.insa.algo.shortestpath;
package org.insa.graphs.algorithm.shortestpath;
import org.insa.algo.AbstractInputData.Mode;
import org.insa.algo.AbstractSolution;
import org.insa.graph.Arc;
import org.insa.graph.Path;
import org.insa.graphs.algorithm.AbstractInputData.Mode;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Path;
import org.insa.graphs.algorithm.AbstractSolution;
public class ShortestPathSolution extends AbstractSolution {
// Optimal solution.
private Path path;
/**
* {@inheritDoc}
*/
public ShortestPathSolution(ShortestPathData data) {
super(data);
}
private final Path path;
/**
* Create a new infeasible shortest-path solution for the given input and
@ -26,6 +19,7 @@ public class ShortestPathSolution extends AbstractSolution {
*/
public ShortestPathSolution(ShortestPathData data, Status status) {
super(data, status);
this.path = null;
}
/**

View File

@ -1,8 +1,8 @@
package org.insa.algo.shortestpath;
package org.insa.graphs.algorithm.shortestpath;
import java.io.PrintStream;
import org.insa.graph.Node;
import org.insa.graphs.model.Node;
public class ShortestPathTextObserver implements ShortestPathObserver {

View File

@ -0,0 +1,205 @@
package org.insa.graphs.algorithm.utils;
import java.util.ArrayList;
/**
* Implements a binary heap containing elements of type E.
*
* Note that all comparisons are based on the compareTo method, hence E must
* implement Comparable
*
* @author Mark Allen Weiss
* @author DLB
*/
public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
// Number of elements in heap.
private int currentSize;
// The heap array.
protected final ArrayList<E> array;
/**
* Construct a new empty binary heap.
*/
public BinaryHeap() {
this.currentSize = 0;
this.array = new ArrayList<E>();
}
/**
* Construct a copy of the given heap.
*
* @param heap Binary heap to copy.
*/
public BinaryHeap(BinaryHeap<E> heap) {
this.currentSize = heap.currentSize;
this.array = new ArrayList<E>(heap.array);
}
/**
* Set an element at the given index.
*
* @param index Index at which the element should be set.
* @param value Element to set.
*/
private void arraySet(int index, E value) {
if (index == this.array.size()) {
this.array.add(value);
}
else {
this.array.set(index, value);
}
}
/**
* @return Index of the parent of the given index.
*/
protected int indexParent(int index) {
return (index - 1) / 2;
}
/**
* @return Index of the left child of the given index.
*/
protected int indexLeft(int index) {
return index * 2 + 1;
}
/**
* Internal method to percolate up in the heap.
*
* @param index Index at which the percolate begins.
*/
private void percolateUp(int index) {
E x = this.array.get(index);
for (; index > 0
&& x.compareTo(this.array.get(indexParent(index))) < 0; index = indexParent(
index)) {
E moving_val = this.array.get(indexParent(index));
this.arraySet(index, moving_val);
}
this.arraySet(index, x);
}
/**
* Internal method to percolate down in the heap.
*
* @param index Index at which the percolate begins.
*/
private void percolateDown(int index) {
int ileft = indexLeft(index);
int iright = ileft + 1;
if (ileft < this.currentSize) {
E current = this.array.get(index);
E left = this.array.get(ileft);
boolean hasRight = iright < this.currentSize;
E right = (hasRight) ? this.array.get(iright) : null;
if (!hasRight || left.compareTo(right) < 0) {
// Left is smaller
if (left.compareTo(current) < 0) {
this.arraySet(index, left);
this.arraySet(ileft, current);
this.percolateDown(ileft);
}
}
else {
// Right is smaller
if (right.compareTo(current) < 0) {
this.arraySet(index, right);
this.arraySet(iright, current);
this.percolateDown(iright);
}
}
}
}
@Override
public boolean isEmpty() {
return this.currentSize == 0;
}
@Override
public int size() {
return this.currentSize;
}
@Override
public void insert(E x) {
int index = this.currentSize++;
this.arraySet(index, x);
this.percolateUp(index);
}
@Override
public void remove(E x) throws ElementNotFoundException {
// TODO:
}
@Override
public E findMin() throws EmptyPriorityQueueException {
if (isEmpty())
throw new EmptyPriorityQueueException();
return this.array.get(0);
}
@Override
public E deleteMin() throws EmptyPriorityQueueException {
E minItem = findMin();
E lastItem = this.array.get(--this.currentSize);
this.arraySet(0, lastItem);
this.percolateDown(0);
return minItem;
}
/**
* Creates a multi-lines string representing a sorted view of this binary heap.
*
* @return a string containing a sorted view this binary heap.
*/
public String toStringSorted() {
return BinaryHeapFormatter.toStringSorted(this, -1);
}
/**
* Creates a multi-lines string representing a sorted view of this binary heap.
*
* @param maxElement Maximum number of elements to display. or {@code -1} to
* display all the elements.
*
* @return a string containing a sorted view this binary heap.
*/
public String toStringSorted(int maxElement) {
return BinaryHeapFormatter.toStringSorted(this, maxElement);
}
/**
* Creates a multi-lines string representing a tree view of this binary heap.
*
* @return a string containing a tree view of this binary heap.
*/
public String toStringTree() {
return BinaryHeapFormatter.toStringTree(this, Integer.MAX_VALUE);
}
/**
* Creates a multi-lines string representing a tree view of this binary heap.
*
* @param maxDepth Maximum depth of the tree to display.
*
* @return a string containing a tree view of this binary heap.
*/
public String toStringTree(int maxDepth) {
return BinaryHeapFormatter.toStringTree(this, maxDepth);
}
@Override
public String toString() {
return BinaryHeapFormatter.toStringTree(this, 8);
}
}

View File

@ -0,0 +1,198 @@
package org.insa.graphs.algorithm.utils;
import java.util.ArrayList;
public class BinaryHeapFormatter {
/**
* This class is used by {@link #toStringTree}, and simply contains three string
* accumulating. This is an immutable class.
*
*/
private static class Context {
// Output text:
public final String acu;
// Margin to get back exactly under the current position:
public final String margin;
// Last margin used for the last child of a node. The markers are different:
public final String lastmargin;
/**
* Creaet a new {@code Context}.
*
* @param acu The accumulated string.
* @param margin The current margin.
* @param lastMargin The last margin used.
*/
public Context(String acu, String margin, String lastMargin) {
this.acu = acu;
this.margin = margin;
this.lastmargin = lastMargin;
}
/**
* Creates a new context by appending newlines to this context.
*
* @param n Number of newlines to append.
*
* @return a new context with {@code n} newlines appended.
*/
public Context appendNewlines(int n) {
if (n <= 0) {
return this;
}
else {
return (new Context(this.acu + "\n" + this.margin, this.margin, this.lastmargin)
.appendNewlines(n - 1));
}
}
/**
* Creates a new context by appending the given string to this context.
*
* @param count Number of spaces to add to the margin, or {@code null} to use
* the length of the string.
* @param text String to append.
*
* @return a new context with {@code text} appended.
*/
public Context appendText(Integer count, String text) {
int cnt = (count == null) ? text.length() : count;
final String spaces = new String(new char[cnt]).replace('\0', ' ');
return new Context(this.acu + text, this.margin + spaces, this.lastmargin + spaces);
}
/**
* Creates a new context by appending a branch to this context.
*
* @param n Number of spaces to add to the margin, or {@code null} to use
* the length of the string.
* @param label Name of the branch.
*
* @return a new context with the branch appended.
*/
public Context appendBranch(Integer count, String label) {
final Context ctxt = this.appendText(count, label);
if (count == null) {
return new Context(ctxt.acu + "_", ctxt.margin + "|", ctxt.margin + " ");
}
else {
return new Context(ctxt.acu, ctxt.margin + "|", ctxt.margin + " ")
.appendNewlines(1);
}
}
}
/*
* Input : ready to write the current node at the current context position.
* Output : the last character of acu is the last character of the current node.
*/
protected static <E extends Comparable<E>> Context toStringLoop(BinaryHeap<E> heap,
Context ctxt, int node, int max_depth) {
if (max_depth < 0) {
return ctxt.appendText(null, "...");
}
else {
E nodeval = heap.array.get(node);
String nodevals = nodeval.toString();
ArrayList<Integer> childs = new ArrayList<Integer>();
// Add childs
int index_left = heap.indexLeft(node);
int index_right = index_left + 1;
if (index_left < heap.size()) {
childs.add(index_left);
}
if (index_right < heap.size()) {
childs.add(index_right);
}
Context ctxt2 = childs.isEmpty() ? ctxt.appendText(null, nodevals)
: ctxt.appendBranch(1, nodevals);
for (int ch = 0; ch < childs.size(); ch++) {
boolean is_last = (ch == childs.size() - 1);
int child = childs.get(ch);
if (is_last) {
Context ctxt3 = new Context(ctxt2.acu, ctxt2.lastmargin, ctxt2.lastmargin);
ctxt2 = new Context(toStringLoop(heap, ctxt3.appendText(null, "___"), child,
max_depth - 1).acu, ctxt2.margin, ctxt2.lastmargin);
}
else {
ctxt2 = new Context(toStringLoop(heap, ctxt2.appendText(null, "___"), child,
max_depth - 1).acu, ctxt2.margin, ctxt2.lastmargin).appendNewlines(2);
}
}
return ctxt2;
}
}
/**
* Creates a multi-lines string representing a tree view of the given binary
* heap.
*
* @param heap The binary heap to display.
* @param maxDepth Maximum depth of the tree to display.
*
* @return a string containing a tree view of the given binary heap.
*/
public static <E extends Comparable<E>> String toStringTree(BinaryHeap<E> heap, int maxDepth) {
final Context init_context = new Context(" ", " ", " ");
final Context result = toStringLoop(heap, init_context, 0, maxDepth);
return result.acu;
}
/**
* Creates a multi-lines string representing a sorted view of the given binary
* heap.
*
* @param heap The binary heap to display.
* @param maxElement Maximum number of elements to display. or {@code -1} to
* display all the elements.
*
* @return a string containing a sorted view the given binary heap.
*/
public static <E extends Comparable<E>> String toStringSorted(BinaryHeap<E> heap,
int max_elements) {
String result = "";
final BinaryHeap<E> copy = new BinaryHeap<E>(heap);
final String truncate;
if (max_elements < 0 || max_elements >= heap.size()) {
truncate = "";
}
else {
truncate = ", only " + max_elements + " elements are shown";
}
result += "======== Sorted HEAP (size = " + heap.size() + truncate + ") ========\n\n";
while (!copy.isEmpty() && max_elements-- != 0) {
result += copy.deleteMin() + "\n";
}
result += "\n-------- End of heap --------";
return result;
}
public static void main(String[] args) {
final BinaryHeap<Integer> heap = new BinaryHeap<Integer>();
for (int i = 0; i < 12; i++) {
heap.insert(i);
}
System.out.println(heap.toStringSorted(-1));
System.out.println(heap.toStringTree(6));
}
}

View File

@ -1,4 +1,4 @@
package org.insa.algo.utils;
package org.insa.graphs.algorithm.utils;
import java.util.SortedSet;
import java.util.TreeSet;

View File

@ -1,4 +1,4 @@
package org.insa.algo.utils;
package org.insa.graphs.algorithm.utils;
public class ElementNotFoundException extends RuntimeException {

View File

@ -1,4 +1,4 @@
package org.insa.algo.utils;
package org.insa.graphs.algorithm.utils;
public class EmptyPriorityQueueException extends RuntimeException {

View File

@ -1,4 +1,4 @@
package org.insa.algo.utils;
package org.insa.graphs.algorithm.utils;
/**
* Interface representing a basic priority queue.

View File

@ -1,8 +1,8 @@
package org.insa.algo.weakconnectivity;
package org.insa.graphs.algorithm.weakconnectivity;
import java.util.ArrayList;
import org.insa.graph.Node;
import org.insa.graphs.model.Node;
public interface WeaklyConnectedComponentObserver {

View File

@ -1,9 +1,9 @@
package org.insa.algo.weakconnectivity;
package org.insa.graphs.algorithm.weakconnectivity;
import java.io.PrintStream;
import java.util.ArrayList;
import org.insa.graph.Node;
import org.insa.graphs.model.Node;
public class WeaklyConnectedComponentTextObserver implements WeaklyConnectedComponentObserver {

View File

@ -1,4 +1,4 @@
package org.insa.algo.weakconnectivity;
package org.insa.graphs.algorithm.weakconnectivity;
import java.util.ArrayList;
import java.util.Arrays;
@ -6,11 +6,11 @@ import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import org.insa.algo.AbstractAlgorithm;
import org.insa.algo.AbstractSolution.Status;
import org.insa.graph.Arc;
import org.insa.graph.Graph;
import org.insa.graph.Node;
import org.insa.graphs.algorithm.AbstractAlgorithm;
import org.insa.graphs.algorithm.AbstractSolution.Status;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node;
public class WeaklyConnectedComponentsAlgorithm
extends AbstractAlgorithm<WeaklyConnectedComponentObserver> {

View File

@ -1,7 +1,7 @@
package org.insa.algo.weakconnectivity;
package org.insa.graphs.algorithm.weakconnectivity;
import org.insa.algo.AbstractInputData;
import org.insa.graph.Graph;
import org.insa.graphs.algorithm.AbstractInputData;
import org.insa.graphs.model.Graph;
public class WeaklyConnectedComponentsData extends AbstractInputData {

View File

@ -1,9 +1,9 @@
package org.insa.algo.weakconnectivity;
package org.insa.graphs.algorithm.weakconnectivity;
import java.util.ArrayList;
import org.insa.algo.AbstractSolution;
import org.insa.graph.Node;
import org.insa.graphs.algorithm.AbstractSolution;
import org.insa.graphs.model.Node;
public class WeaklyConnectedComponentsSolution extends AbstractSolution {

View File

@ -1,4 +1,4 @@
package org.insa.algo.utils;
package org.insa.graphs.algorithm.utils;
public class BinaryHeapTest extends PriorityQueueTest {

View File

@ -1,4 +1,4 @@
package org.insa.algo.utils;
package org.insa.graphs.algorithm.utils;
public class BinarySearchTreeTest extends PriorityQueueTest {

View File

@ -1,4 +1,4 @@
package org.insa.algo.utils;
package org.insa.graphs.algorithm.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

84
be-graphes-gui/pom.xml Normal file
View File

@ -0,0 +1,84 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.insa.graphs</groupId>
<artifactId>be-graphes-all</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<mapsforge.version>0.13.0</mapsforge.version>
</properties>
<artifactId>be-graphes-gui</artifactId>
<name>be-graphes-gui</name>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.insa.graphs</groupId>
<artifactId>be-graphes-model</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.insa.graphs</groupId>
<artifactId>be-graphes-algos</artifactId>
<version>${project.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mapsforge/mapsforge-core -->
<dependency>
<groupId>net.sf.kxml</groupId>
<artifactId>kxml2</artifactId>
<version>2.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mapsforge/mapsforge-map-reader -->
<dependency>
<groupId>org.mapsforge</groupId>
<artifactId>mapsforge-themes</artifactId>
<version>${mapsforge.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mapsforge/mapsforge-map -->
<dependency>
<groupId>org.mapsforge</groupId>
<artifactId>mapsforge-map</artifactId>
<version>${mapsforge.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mapsforge/mapsforge-map-awt -->
<dependency>
<groupId>org.mapsforge</groupId>
<artifactId>mapsforge-map-awt</artifactId>
<version>${mapsforge.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mapsforge/mapsforge-map-reader -->
<dependency>
<groupId>org.mapsforge</groupId>
<artifactId>mapsforge-themes</artifactId>
<version>${mapsforge.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mapsforge/mapsforge-map-reader -->
<dependency>
<groupId>org.mapsforge</groupId>
<artifactId>mapsforge-map-reader</artifactId>
<version>${mapsforge.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -1,4 +1,4 @@
package org.insa.graphics;
package org.insa.graphs.gui;
import java.awt.Color;
import java.awt.Component;
@ -22,15 +22,14 @@ import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import org.insa.algo.AbstractAlgorithm;
import org.insa.algo.AlgorithmFactory;
import org.insa.algo.ArcInspector;
import org.insa.algo.ArcInspectorFactory;
import org.insa.graph.Node;
import org.insa.graphics.NodesInputPanel.InputChangedEvent;
import org.insa.graphics.drawing.Drawing;
import org.insa.graphics.drawing.components.MapViewDrawing;
import org.insa.graphics.utils.ColorUtils;
import org.insa.graphs.algorithm.AbstractAlgorithm;
import org.insa.graphs.algorithm.AlgorithmFactory;
import org.insa.graphs.algorithm.ArcInspector;
import org.insa.graphs.algorithm.ArcInspectorFactory;
import org.insa.graphs.gui.NodesInputPanel.InputChangedEvent;
import org.insa.graphs.gui.drawing.Drawing;
import org.insa.graphs.gui.utils.ColorUtils;
import org.insa.graphs.model.Node;
public class AlgorithmPanel extends JPanel implements DrawingChangeListener {
@ -118,9 +117,6 @@ public class AlgorithmPanel extends JPanel implements DrawingChangeListener {
// Graphic / Text checkbox observer
private final JCheckBox graphicObserverCheckbox, textualObserverCheckbox;
// Drawing
private Drawing drawing = null;
private JButton startAlgoButton;
// Start listeners
@ -360,7 +356,7 @@ public class AlgorithmPanel extends JPanel implements DrawingChangeListener {
for (JComponent component: components) {
component.setEnabled(enabled);
}
graphicObserverCheckbox.setEnabled(enabled && !(drawing instanceof MapViewDrawing));
graphicObserverCheckbox.setEnabled(enabled);
enabled = enabled && allNotNull(this.nodesInputPanel.getNodeForInputs());
startAlgoButton.setEnabled(enabled);
}
@ -376,15 +372,6 @@ public class AlgorithmPanel extends JPanel implements DrawingChangeListener {
@Override
public void onDrawingLoaded(Drawing oldDrawing, Drawing newDrawing) {
if (newDrawing instanceof MapViewDrawing) {
graphicObserverCheckbox.setSelected(false);
graphicObserverCheckbox.setEnabled(false);
}
else {
graphicObserverCheckbox.setSelected(true);
graphicObserverCheckbox.setEnabled(true);
}
this.drawing = newDrawing;
}
@Override

View File

@ -1,4 +1,4 @@
package org.insa.graphics;
package org.insa.graphs.gui;
import java.awt.Component;
import java.awt.event.ActionEvent;

View File

@ -1,6 +1,6 @@
package org.insa.graphics;
package org.insa.graphs.gui;
import org.insa.graphics.drawing.Drawing;
import org.insa.graphs.gui.drawing.Drawing;
public interface DrawingChangeListener {

View File

@ -1,6 +1,6 @@
package org.insa.graphics;
package org.insa.graphs.gui;
import org.insa.graph.Graph;
import org.insa.graphs.model.Graph;
public interface GraphChangeListener {

View File

@ -1,4 +1,4 @@
package org.insa.graphics;
package org.insa.graphs.gui;
import java.awt.Component;
@ -11,10 +11,10 @@ import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.border.EmptyBorder;
import org.insa.graph.Arc;
import org.insa.graph.Node;
import org.insa.graph.RoadInformation;
import org.insa.graph.io.GraphReaderObserver;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.RoadInformation;
import org.insa.graphs.model.io.GraphReaderObserver;
/**
* One-time use GraphReaderObserver that display progress in three different

View File

@ -1,4 +1,4 @@
package org.insa.graphics;
package org.insa.graphs.gui;
import java.awt.Color;
import java.awt.Font;
@ -21,13 +21,13 @@ import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.insa.graph.Graph;
import org.insa.graph.Node;
import org.insa.graph.Point;
import org.insa.graphics.drawing.Drawing;
import org.insa.graphics.drawing.Drawing.AlphaMode;
import org.insa.graphics.drawing.DrawingClickListener;
import org.insa.graphics.drawing.overlays.MarkerOverlay;
import org.insa.graphs.gui.drawing.Drawing;
import org.insa.graphs.gui.drawing.Drawing.AlphaMode;
import org.insa.graphs.gui.drawing.DrawingClickListener;
import org.insa.graphs.gui.drawing.overlays.MarkerOverlay;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Point;
public class NodesInputPanel extends JPanel
implements DrawingClickListener, DrawingChangeListener, GraphChangeListener {

View File

@ -1,4 +1,4 @@
package org.insa.graphics;
package org.insa.graphs.gui;
import java.awt.Color;
import java.awt.Component;
@ -32,14 +32,14 @@ import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.insa.graph.Graph;
import org.insa.graph.Path;
import org.insa.graph.io.BinaryPathWriter;
import org.insa.graphics.drawing.Drawing;
import org.insa.graphics.drawing.overlays.PathOverlay;
import org.insa.graphics.utils.ColorUtils;
import org.insa.graphics.utils.FileUtils;
import org.insa.graphics.utils.FileUtils.FolderType;
import org.insa.graphs.gui.drawing.Drawing;
import org.insa.graphs.gui.drawing.overlays.PathOverlay;
import org.insa.graphs.gui.utils.ColorUtils;
import org.insa.graphs.gui.utils.FileUtils;
import org.insa.graphs.gui.utils.FileUtils.FolderType;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Path;
import org.insa.graphs.model.io.BinaryPathWriter;
public class PathsPanel extends JPanel implements DrawingChangeListener, GraphChangeListener {
@ -232,9 +232,8 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh
if (chooser
.showSaveDialog(getTopLevelAncestor()) == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
BinaryPathWriter writer = new BinaryPathWriter(new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(file))));
try (BinaryPathWriter writer = new BinaryPathWriter(new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(file))))) {
writer.writePath(path);
}
catch (IOException e1) {

View File

@ -1,4 +1,4 @@
package org.insa.graphics;
package org.insa.graphs.gui;
import java.time.Duration;
import java.time.Instant;

View File

@ -1,4 +1,4 @@
package org.insa.graphics;
package org.insa.graphs.gui;
import java.awt.Color;
import java.awt.Component;
@ -19,12 +19,12 @@ import javax.swing.JPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import org.insa.algo.AbstractInputData;
import org.insa.algo.AbstractSolution;
import org.insa.graph.Graph;
import org.insa.graph.Path;
import org.insa.graphics.drawing.Drawing;
import org.insa.graphics.drawing.overlays.PathOverlay;
import org.insa.graphs.algorithm.AbstractInputData;
import org.insa.graphs.algorithm.AbstractSolution;
import org.insa.graphs.gui.drawing.Drawing;
import org.insa.graphs.gui.drawing.overlays.PathOverlay;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Path;
public class SolutionPanel extends JPanel implements DrawingChangeListener, GraphChangeListener {
@ -122,6 +122,7 @@ public class SolutionPanel extends JPanel implements DrawingChangeListener, Grap
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
public String toString() {

View File

@ -1,4 +1,4 @@
package org.insa.graphics;
package org.insa.graphs.gui;
import java.io.IOException;
import java.io.OutputStream;

View File

@ -1,4 +1,4 @@
package org.insa.graphics;
package org.insa.graphs.gui;
import java.time.Duration;
import java.time.Instant;

View File

@ -1,9 +1,9 @@
package org.insa.graphics.drawing;
package org.insa.graphs.gui.drawing;
import java.awt.Color;
import org.insa.graph.Arc;
import org.insa.graph.RoadInformation.RoadType;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.RoadInformation.RoadType;
public class BasicGraphPalette implements GraphPalette {

View File

@ -1,8 +1,8 @@
package org.insa.graphics.drawing;
package org.insa.graphs.gui.drawing;
import java.awt.Color;
import org.insa.graph.Arc;
import org.insa.graphs.model.Arc;
public class BlackAndWhiteGraphPalette extends BasicGraphPalette {

View File

@ -1,13 +1,13 @@
package org.insa.graphics.drawing;
package org.insa.graphs.gui.drawing;
import java.awt.Color;
import org.insa.graph.Graph;
import org.insa.graph.Path;
import org.insa.graph.Point;
import org.insa.graphics.drawing.overlays.MarkerOverlay;
import org.insa.graphics.drawing.overlays.PathOverlay;
import org.insa.graphics.drawing.overlays.PointSetOverlay;
import org.insa.graphs.gui.drawing.overlays.MarkerOverlay;
import org.insa.graphs.gui.drawing.overlays.PathOverlay;
import org.insa.graphs.gui.drawing.overlays.PointSetOverlay;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Path;
import org.insa.graphs.model.Point;
public interface Drawing {

View File

@ -1,14 +1,14 @@
package org.insa.graphics.drawing;
import org.insa.graph.Point;
public interface DrawingClickListener {
/**
* Event triggered when a click is made on the map.
*
* @param point Position (on the map) of the mouse click.
*/
public void mouseClicked(Point point);
}
package org.insa.graphs.gui.drawing;
import org.insa.graphs.model.Point;
public interface DrawingClickListener {
/**
* Event triggered when a click is made on the map.
*
* @param point Position (on the map) of the mouse click.
*/
public void mouseClicked(Point point);
}

View File

@ -1,8 +1,8 @@
package org.insa.graphics.drawing;
package org.insa.graphs.gui.drawing;
import java.awt.Color;
import org.insa.graph.Arc;
import org.insa.graphs.model.Arc;
public interface GraphPalette {

View File

@ -1,8 +1,8 @@
package org.insa.graphics.drawing;
package org.insa.graphs.gui.drawing;
import java.awt.Dimension;
import org.insa.graph.GraphStatistics.BoundingBox;
import org.insa.graphs.model.GraphStatistics.BoundingBox;
public class MercatorProjection implements Projection {

View File

@ -1,6 +1,6 @@
package org.insa.graphics.drawing;
package org.insa.graphs.gui.drawing;
import org.insa.graph.GraphStatistics.BoundingBox;
import org.insa.graphs.model.GraphStatistics.BoundingBox;
public class PlateCarreProjection implements Projection {

View File

@ -1,4 +1,4 @@
package org.insa.graphics.drawing;
package org.insa.graphs.gui.drawing;
public interface Projection {

View File

@ -1,4 +1,4 @@
package org.insa.graphics.drawing.components;
package org.insa.graphs.gui.drawing.components;
import java.awt.BasicStroke;
import java.awt.Color;
@ -20,24 +20,24 @@ import java.util.List;
import javax.swing.JPanel;
import org.insa.graph.Arc;
import org.insa.graph.Graph;
import org.insa.graph.GraphStatistics.BoundingBox;
import org.insa.graph.Node;
import org.insa.graph.Path;
import org.insa.graph.Point;
import org.insa.graphics.drawing.BasicGraphPalette;
import org.insa.graphics.drawing.Drawing;
import org.insa.graphics.drawing.DrawingClickListener;
import org.insa.graphics.drawing.GraphPalette;
import org.insa.graphics.drawing.MercatorProjection;
import org.insa.graphics.drawing.PlateCarreProjection;
import org.insa.graphics.drawing.Projection;
import org.insa.graphics.drawing.overlays.MarkerOverlay;
import org.insa.graphics.drawing.overlays.MarkerUtils;
import org.insa.graphics.drawing.overlays.Overlay;
import org.insa.graphics.drawing.overlays.PathOverlay;
import org.insa.graphics.drawing.overlays.PointSetOverlay;
import org.insa.graphs.gui.drawing.BasicGraphPalette;
import org.insa.graphs.gui.drawing.Drawing;
import org.insa.graphs.gui.drawing.DrawingClickListener;
import org.insa.graphs.gui.drawing.GraphPalette;
import org.insa.graphs.gui.drawing.MercatorProjection;
import org.insa.graphs.gui.drawing.PlateCarreProjection;
import org.insa.graphs.gui.drawing.Projection;
import org.insa.graphs.gui.drawing.overlays.MarkerOverlay;
import org.insa.graphs.gui.drawing.overlays.MarkerUtils;
import org.insa.graphs.gui.drawing.overlays.Overlay;
import org.insa.graphs.gui.drawing.overlays.PathOverlay;
import org.insa.graphs.gui.drawing.overlays.PointSetOverlay;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Path;
import org.insa.graphs.model.Point;
import org.insa.graphs.model.GraphStatistics.BoundingBox;
/**
*

View File

@ -1,465 +1,521 @@
package org.insa.graphics.drawing.components;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.UUID;
import org.insa.graph.Arc;
import org.insa.graph.Graph;
import org.insa.graph.Path;
import org.insa.graph.Point;
import org.insa.graphics.drawing.Drawing;
import org.insa.graphics.drawing.DrawingClickListener;
import org.insa.graphics.drawing.GraphPalette;
import org.insa.graphics.drawing.overlays.MarkerAutoScaling;
import org.insa.graphics.drawing.overlays.MarkerOverlay;
import org.insa.graphics.drawing.overlays.MarkerUtils;
import org.insa.graphics.drawing.overlays.Overlay;
import org.insa.graphics.drawing.overlays.PathOverlay;
import org.insa.graphics.drawing.overlays.PointSetOverlay;
import org.insa.graphics.drawing.overlays.PolylineAutoScaling;
import org.mapsforge.core.graphics.GraphicFactory;
import org.mapsforge.core.model.BoundingBox;
import org.mapsforge.core.model.LatLong;
import org.mapsforge.core.model.MapPosition;
import org.mapsforge.core.util.LatLongUtils;
import org.mapsforge.core.util.Parameters;
import org.mapsforge.map.awt.graphics.AwtGraphicFactory;
import org.mapsforge.map.awt.util.AwtUtil;
import org.mapsforge.map.awt.view.MapView;
import org.mapsforge.map.datastore.MapDataStore;
import org.mapsforge.map.layer.Layer;
import org.mapsforge.map.layer.Layers;
import org.mapsforge.map.layer.cache.TileCache;
import org.mapsforge.map.layer.hills.HillsRenderConfig;
import org.mapsforge.map.layer.overlay.Marker;
import org.mapsforge.map.layer.renderer.TileRendererLayer;
import org.mapsforge.map.model.DisplayModel;
import org.mapsforge.map.model.MapViewPosition;
import org.mapsforge.map.model.Model;
import org.mapsforge.map.reader.MapFile;
import org.mapsforge.map.rendertheme.InternalRenderTheme;
/**
*
*/
public class MapViewDrawing extends MapView implements Drawing {
/**
*
*/
private static final long serialVersionUID = 8606967833704938092L;
/**
* Base Overlay for MapViewDrawing overlays.
*
*/
private abstract class MapViewOverlay implements Overlay {
// Marker associated.
protected Layer[] layers;
// Current color
protected Color color;
public MapViewOverlay(Layer[] layers, Color color) {
this.layers = layers;
for (Layer layer: this.layers) {
MapViewDrawing.this.getLayerManager().getLayers().add(layer);
}
this.color = color;
}
@Override
public void setColor(Color color) {
this.color = color;
}
@Override
public Color getColor() {
return this.color;
}
@Override
public void setVisible(boolean visible) {
for (Layer layer: layers) {
layer.setVisible(visible);
}
}
@Override
public boolean isVisible() {
if (this.layers.length == 0) {
return true;
}
return this.layers[0].isVisible();
}
@Override
public void delete() {
Layers mlayers = MapViewDrawing.this.getLayerManager().getLayers();
for (Layer layer: layers) {
mlayers.remove(layer);
}
}
@Override
public void redraw() {
MapViewDrawing.this.getLayerManager().redrawLayers();
}
};
/**
* MarkerOverlay for MapViewDrawing.
*
*/
private class MapViewMarkerOverlay extends MapViewOverlay implements MarkerOverlay {
private final AlphaMode alphaMode;
private Color innerColor;
public MapViewMarkerOverlay(Marker marker, Color outer, Color innerColor,
AlphaMode alphaMode) {
super(new Layer[] { marker }, outer);
this.innerColor = innerColor;
this.alphaMode = alphaMode;
}
@Override
public Point getPoint() {
Marker marker = (Marker) super.layers[0];
return new Point((float) marker.getLatLong().getLongitude(),
(float) marker.getLatLong().getLatitude());
}
@Override
public void setColor(Color outer) {
this.innerColor = this.innerColor.equals(this.color) ? outer : this.innerColor;
super.setColor(color);
MarkerAutoScaling marker = (MarkerAutoScaling) super.layers[0];
marker.setImage(MarkerUtils.getMarkerForColor(color, this.innerColor, this.alphaMode));
}
@Override
public void moveTo(Point point) {
MarkerAutoScaling marker = (MarkerAutoScaling) this.layers[0];
this.delete();
marker = new MarkerAutoScaling(convertPoint(point), marker.getImage());
this.layers[0] = marker;
MapViewDrawing.this.getLayerManager().getLayers().add(marker);
}
};
/**
* PathOverlay for MapViewDrawing.
*
*/
private class MapViewPathOverlay extends MapViewOverlay implements PathOverlay {
public MapViewPathOverlay(PolylineAutoScaling path, MarkerAutoScaling origin,
MarkerAutoScaling destination) {
super(new Layer[] { path, origin, destination }, path.getColor());
}
public MapViewPathOverlay(PolylineAutoScaling path) {
super(new Layer[] { path }, path.getColor());
}
@Override
public void setColor(Color color) {
super.setColor(color);
((PolylineAutoScaling) this.layers[0]).setColor(color);
((MarkerAutoScaling) this.layers[1])
.setImage(MarkerUtils.getMarkerForColor(color, color, AlphaMode.TRANSPARENT));
((MarkerAutoScaling) this.layers[2])
.setImage(MarkerUtils.getMarkerForColor(color, color, AlphaMode.TRANSPARENT));
}
}
/**
* PointSetOverlay for MapViewDrawing - Not currently implemented.
*
*/
private class MapViewPointSetOverlay extends MapViewOverlay implements PointSetOverlay {
public MapViewPointSetOverlay() {
super(new Layer[0], Color.BLACK);
}
@Override
public void setWidth(int width) {
}
@Override
public void setWidthAndColor(int width, Color color) {
setWidth(width);
setColor(color);
}
@Override
public void addPoint(Point point) {
}
@Override
public void addPoint(Point point, int width) {
setWidth(width);
addPoint(point);
}
@Override
public void addPoint(Point point, Color color) {
setColor(color);
addPoint(point);
}
@Override
public void addPoint(Point point, int width, Color color) {
setWidth(width);
setColor(color);
addPoint(point);
}
};
// Default path color.
public static final Color DEFAULT_PATH_COLOR = new Color(66, 134, 244);
// Graphic factory.
private static final GraphicFactory GRAPHIC_FACTORY = AwtGraphicFactory.INSTANCE;
// Default tile size.
private static final int DEFAULT_TILE_SIZE = 512;
// List of listeners.
private ArrayList<DrawingClickListener> drawingClickListeners = new ArrayList<>();
// Tile size
private int tileSize;
// Zoom controls
private MapZoomControls zoomControls;
public MapViewDrawing() {
super();
Parameters.NUMBER_OF_THREADS = 2;
Parameters.SQUARE_FRAME_BUFFER = false;
getMapScaleBar().setVisible(true);
DisplayModel model = getModel().displayModel;
this.tileSize = DEFAULT_TILE_SIZE;
model.setFixedTileSize(this.tileSize);
this.setZoomLevelMin((byte) 0);
this.setZoomLevelMax((byte) 20);
// Try...
try {
this.zoomControls = new MapZoomControls(this, 0, 0, 20);
this.zoomControls.addZoomInListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getModel().mapViewPosition.zoomIn();
}
});
this.zoomControls.addZoomOutListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getModel().mapViewPosition.zoomOut();
}
});
}
catch (IOException e) {
e.printStackTrace();
}
}
/*
* (non-Javadoc)
*
* @see org.mapsforge.map.awt.view.MapView#paint(java.awt.Graphics)
*/
@Override
public void paint(Graphics graphics) {
super.paint(graphics);
if (this.zoomControls != null) {
this.zoomControls.setZoomLevel(this.getModel().mapViewPosition.getZoomLevel());
this.zoomControls.draw((Graphics2D) graphics,
getWidth() - this.zoomControls.getWidth() - 20,
this.getHeight() - this.zoomControls.getHeight() - 10, this);
}
}
/*
* (non-Javadoc)
*
* @see org.insa.graphics.drawing.Drawing#clear()
*/
@Override
public void clear() {
getLayerManager().getLayers().clear();
repaint();
}
/*
* (non-Javadoc)
*
* @see org.insa.graphics.drawing.Drawing#clearOverlays()
*/
@Override
public void clearOverlays() {
Layers layers = getLayerManager().getLayers();
for (Layer layer: layers) {
if (layer instanceof PolylineAutoScaling || layer instanceof MarkerAutoScaling) {
getLayerManager().getLayers().remove(layer, false);
}
}
repaint();
}
protected LatLong convertPoint(Point point) {
return new LatLong(point.getLatitude(), point.getLongitude());
}
private TileRendererLayer createTileRendererLayer(TileCache tileCache,
MapDataStore mapDataStore, MapViewPosition mapViewPosition,
HillsRenderConfig hillsRenderConfig) {
TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore,
mapViewPosition, false, true, false, GRAPHIC_FACTORY, hillsRenderConfig) {
@Override
public boolean onTap(LatLong tapLatLong, org.mapsforge.core.model.Point layerXY,
org.mapsforge.core.model.Point tapXY) {
if (zoomControls.contains(new java.awt.Point((int) tapXY.x, (int) tapXY.y))) {
return false;
}
Point pt = new Point((float) tapLatLong.getLongitude(),
(float) tapLatLong.getLatitude());
for (DrawingClickListener listener: MapViewDrawing.this.drawingClickListeners) {
listener.mouseClicked(pt);
}
return true;
}
};
tileRendererLayer.setXmlRenderTheme(InternalRenderTheme.DEFAULT);
return tileRendererLayer;
}
@Override
public void addDrawingClickListener(DrawingClickListener listener) {
this.drawingClickListeners.add(listener);
}
@Override
public void removeDrawingClickListener(DrawingClickListener listener) {
this.drawingClickListeners.remove(listener);
}
protected MarkerAutoScaling createMarker(Point point, Color outer, Color inner,
AlphaMode mode) {
Image image = MarkerUtils.getMarkerForColor(outer, inner, mode);
return new MarkerAutoScaling(convertPoint(point), image);
}
@Override
public MarkerOverlay drawMarker(Point point, Color outer, Color inner, AlphaMode mode) {
return new MapViewMarkerOverlay(createMarker(point, outer, inner, mode), outer, inner,
mode);
}
@Override
public PointSetOverlay createPointSetOverlay() {
return new MapViewPointSetOverlay();
}
@Override
public PointSetOverlay createPointSetOverlay(int width, Color color) {
PointSetOverlay ps = new MapViewPointSetOverlay();
ps.setWidthAndColor(width, color);
return ps;
}
public void drawGraph(File file) {
// Tile cache
TileCache tileCache = AwtUtil.createTileCache(tileSize,
getModel().frameBufferModel.getOverdrawFactor(), 1024,
new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()));
// Layers
Layers layers = getLayerManager().getLayers();
MapDataStore mapDataStore = new MapFile(file);
TileRendererLayer tileRendererLayer = createTileRendererLayer(tileCache, mapDataStore,
getModel().mapViewPosition, null);
layers.add(tileRendererLayer);
BoundingBox boundingBox = mapDataStore.boundingBox();
final Model model = getModel();
if (model.mapViewPosition.getZoomLevel() == 0
|| !boundingBox.contains(model.mapViewPosition.getCenter())) {
byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(),
boundingBox, model.displayModel.getTileSize());
model.mapViewPosition
.setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel));
zoomControls.setZoomLevel(zoomLevel);
}
}
@Override
public void drawGraph(Graph graph, GraphPalette palette) {
throw new RuntimeException("Not implemented, use drawGraph(File).");
}
@Override
public void drawGraph(Graph graph) {
throw new RuntimeException("Not implemented, use drawGraph(File).");
}
@Override
public PathOverlay drawPath(Path path, Color color, boolean markers) {
PolylineAutoScaling line = new PolylineAutoScaling(1, color);
ArrayList<Point> points = new ArrayList<>(path.getArcs().size() * 4);
for (Arc arc: path.getArcs()) {
points.addAll(arc.getPoints());
}
line.addAll(points);
PathOverlay overlay = null;
if (markers) {
MarkerAutoScaling origin = createMarker(path.getOrigin().getPoint(), color, color,
AlphaMode.TRANSPARENT),
destination = createMarker(path.getDestination().getPoint(), color, color,
AlphaMode.TRANSPARENT);
overlay = new MapViewPathOverlay(line, origin, destination);
}
else {
overlay = new MapViewPathOverlay(line);
}
return overlay;
}
@Override
public PathOverlay drawPath(Path path, Color color) {
return drawPath(path, color, true);
}
@Override
public PathOverlay drawPath(Path path) {
return drawPath(path, DEFAULT_PATH_COLOR, true);
}
@Override
public PathOverlay drawPath(Path path, boolean markers) {
return drawPath(path, DEFAULT_PATH_COLOR, markers);
}
}
package org.insa.graphs.gui.drawing.components;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import org.insa.graphs.gui.drawing.Drawing;
import org.insa.graphs.gui.drawing.DrawingClickListener;
import org.insa.graphs.gui.drawing.GraphPalette;
import org.insa.graphs.gui.drawing.overlays.MarkerAutoScaling;
import org.insa.graphs.gui.drawing.overlays.MarkerOverlay;
import org.insa.graphs.gui.drawing.overlays.MarkerUtils;
import org.insa.graphs.gui.drawing.overlays.Overlay;
import org.insa.graphs.gui.drawing.overlays.PathOverlay;
import org.insa.graphs.gui.drawing.overlays.PointSetOverlay;
import org.insa.graphs.gui.drawing.overlays.PolylineAutoScaling;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Path;
import org.insa.graphs.model.Point;
import org.mapsforge.core.graphics.GraphicFactory;
import org.mapsforge.core.model.BoundingBox;
import org.mapsforge.core.model.LatLong;
import org.mapsforge.core.model.MapPosition;
import org.mapsforge.core.util.LatLongUtils;
import org.mapsforge.core.util.Parameters;
import org.mapsforge.map.awt.graphics.AwtGraphicFactory;
import org.mapsforge.map.awt.util.AwtUtil;
import org.mapsforge.map.awt.view.MapView;
import org.mapsforge.map.datastore.MapDataStore;
import org.mapsforge.map.layer.Layer;
import org.mapsforge.map.layer.Layers;
import org.mapsforge.map.layer.cache.TileCache;
import org.mapsforge.map.layer.hills.HillsRenderConfig;
import org.mapsforge.map.layer.overlay.Marker;
import org.mapsforge.map.layer.overlay.Polygon;
import org.mapsforge.map.layer.renderer.TileRendererLayer;
import org.mapsforge.map.model.DisplayModel;
import org.mapsforge.map.model.IMapViewPosition;
import org.mapsforge.map.model.Model;
import org.mapsforge.map.reader.MapFile;
import org.mapsforge.map.rendertheme.InternalRenderTheme;
/**
*
*/
public class MapViewDrawing extends MapView implements Drawing {
/**
*
*/
private static final long serialVersionUID = 8606967833704938092L;
/**
* Base Overlay for MapViewDrawing overlays.
*
*/
private abstract class MapViewOverlay implements Overlay {
// Marker associated.
protected Layer[] layers;
// Current color
protected Color color;
public MapViewOverlay(Layer[] layers, Color color) {
this.layers = layers;
for (Layer layer: this.layers) {
MapViewDrawing.this.getLayerManager().getLayers().add(layer);
}
this.color = color;
}
@Override
public void setColor(Color color) {
this.color = color;
}
@Override
public Color getColor() {
return this.color;
}
@Override
public void setVisible(boolean visible) {
for (Layer layer: layers) {
layer.setVisible(visible);
}
}
@Override
public boolean isVisible() {
if (this.layers.length == 0) {
return true;
}
return this.layers[0].isVisible();
}
@Override
public void delete() {
Layers mlayers = MapViewDrawing.this.getLayerManager().getLayers();
for (Layer layer: layers) {
mlayers.remove(layer);
}
}
@Override
public void redraw() {
MapViewDrawing.this.getLayerManager().redrawLayers();
}
};
/**
* MarkerOverlay for MapViewDrawing.
*
*/
private class MapViewMarkerOverlay extends MapViewOverlay implements MarkerOverlay {
private final AlphaMode alphaMode;
private Color innerColor;
public MapViewMarkerOverlay(Marker marker, Color outer, Color innerColor,
AlphaMode alphaMode) {
super(new Layer[] { marker }, outer);
this.innerColor = innerColor;
this.alphaMode = alphaMode;
}
@Override
public Point getPoint() {
Marker marker = (Marker) super.layers[0];
return new Point((float) marker.getLatLong().getLongitude(),
(float) marker.getLatLong().getLatitude());
}
@Override
public void setColor(Color outer) {
this.innerColor = this.innerColor.equals(this.color) ? outer : this.innerColor;
super.setColor(color);
MarkerAutoScaling marker = (MarkerAutoScaling) super.layers[0];
marker.setImage(MarkerUtils.getMarkerForColor(color, this.innerColor, this.alphaMode));
}
@Override
public void moveTo(Point point) {
MarkerAutoScaling marker = (MarkerAutoScaling) this.layers[0];
this.delete();
marker = new MarkerAutoScaling(convertPoint(point), marker.getImage());
this.layers[0] = marker;
MapViewDrawing.this.getLayerManager().getLayers().add(marker);
}
};
/**
* PathOverlay for MapViewDrawing.
*
*/
private class MapViewPathOverlay extends MapViewOverlay implements PathOverlay {
public MapViewPathOverlay(PolylineAutoScaling path, MarkerAutoScaling origin,
MarkerAutoScaling destination) {
super(new Layer[] { path, origin, destination }, path.getColor());
}
public MapViewPathOverlay(PolylineAutoScaling path) {
super(new Layer[] { path }, path.getColor());
}
@Override
public void setColor(Color color) {
super.setColor(color);
((PolylineAutoScaling) this.layers[0]).setColor(color);
((MarkerAutoScaling) this.layers[1])
.setImage(MarkerUtils.getMarkerForColor(color, color, AlphaMode.TRANSPARENT));
((MarkerAutoScaling) this.layers[2])
.setImage(MarkerUtils.getMarkerForColor(color, color, AlphaMode.TRANSPARENT));
}
}
/**
* PointSetOverlay for MapViewDrawing - Not currently implemented.
*
*/
private class MapViewPointSetOverlay extends MapViewOverlay implements PointSetOverlay {
private List<Point> points = new ArrayList<>();
private final Polygon polygon;
private List<Point> convexHull(List<Point> p) {
if (p.isEmpty()) {
return new ArrayList<>();
}
p.sort((p1, p2) -> Float.compare(p1.getLongitude(), p2.getLongitude()));
List<Point> h = new ArrayList<>();
// lower hull
for (Point pt: p) {
while (h.size() >= 2 && !ccw(h.get(h.size() - 2), h.get(h.size() - 1), pt)) {
h.remove(h.size() - 1);
}
h.add(pt);
}
// upper hull
int t = h.size() + 1;
for (int i = p.size() - 1; i >= 0; i--) {
Point pt = p.get(i);
while (h.size() >= t && !ccw(h.get(h.size() - 2), h.get(h.size() - 1), pt)) {
h.remove(h.size() - 1);
}
h.add(pt);
}
h.remove(h.size() - 1);
return h;
}
// ccw returns true if the three points make a counter-clockwise turn
private boolean ccw(Point a, Point b, Point c) {
return ((b.getLongitude() - a.getLongitude())
* (c.getLatitude() - a.getLatitude())) > ((b.getLatitude() - a.getLatitude())
* (c.getLongitude() - a.getLongitude()));
}
public MapViewPointSetOverlay() {
super(new Layer[] { new Polygon(GRAPHIC_FACTORY.createPaint(), null, GRAPHIC_FACTORY) },
Color.BLACK);
polygon = (Polygon) this.layers[0];
}
@Override
public void setColor(Color color) {
super.setColor(color);
polygon.getPaintFill().setColor(GRAPHIC_FACTORY.createColor(100, color.getRed(),
color.getGreen(), color.getBlue()));
}
@Override
public void setWidth(int width) {
}
@Override
public void setWidthAndColor(int width, Color color) {
setWidth(width);
setColor(color);
}
@Override
public void addPoint(Point point) {
points.add(point);
this.points = convexHull(points);
polygon.setPoints(this.points.stream().map(MapViewDrawing.this::convertPoint)
.collect(Collectors.toList()));
polygon.requestRedraw();
}
@Override
public void addPoint(Point point, int width) {
setWidth(width);
addPoint(point);
}
@Override
public void addPoint(Point point, Color color) {
setColor(color);
addPoint(point);
}
@Override
public void addPoint(Point point, int width, Color color) {
setWidth(width);
setColor(color);
addPoint(point);
}
};
// Default path color.
public static final Color DEFAULT_PATH_COLOR = new Color(66, 134, 244);
// Graphic factory.
private static final GraphicFactory GRAPHIC_FACTORY = AwtGraphicFactory.INSTANCE;
// Default tile size.
private static final int DEFAULT_TILE_SIZE = 512;
// List of listeners.
private ArrayList<DrawingClickListener> drawingClickListeners = new ArrayList<>();
// Tile size
private int tileSize;
// Zoom controls
private MapZoomControls zoomControls;
public MapViewDrawing() {
super();
Parameters.NUMBER_OF_THREADS = 2;
Parameters.SQUARE_FRAME_BUFFER = false;
getMapScaleBar().setVisible(true);
DisplayModel model = getModel().displayModel;
this.tileSize = DEFAULT_TILE_SIZE;
model.setFixedTileSize(this.tileSize);
this.setZoomLevelMin((byte) 0);
this.setZoomLevelMax((byte) 20);
// Try...
try {
this.zoomControls = new MapZoomControls(this, 0, 0, 20);
this.zoomControls.addZoomInListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getModel().mapViewPosition.zoomIn();
}
});
this.zoomControls.addZoomOutListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getModel().mapViewPosition.zoomOut();
}
});
}
catch (IOException e) {
e.printStackTrace();
}
}
/*
* (non-Javadoc)
*
* @see org.mapsforge.map.awt.view.MapView#paint(java.awt.Graphics)
*/
@Override
public void paint(Graphics graphics) {
super.paint(graphics);
if (this.zoomControls != null) {
this.zoomControls.setZoomLevel(this.getModel().mapViewPosition.getZoomLevel());
this.zoomControls.draw((Graphics2D) graphics,
getWidth() - this.zoomControls.getWidth() - 20,
this.getHeight() - this.zoomControls.getHeight() - 10, this);
}
}
/*
* (non-Javadoc)
*
* @see org.insa.graphics.drawing.Drawing#clear()
*/
@Override
public void clear() {
getLayerManager().getLayers().clear();
repaint();
}
/*
* (non-Javadoc)
*
* @see org.insa.graphics.drawing.Drawing#clearOverlays()
*/
@Override
public void clearOverlays() {
Layers layers = getLayerManager().getLayers();
for (Layer layer: layers) {
if (layer instanceof PolylineAutoScaling || layer instanceof MarkerAutoScaling) {
getLayerManager().getLayers().remove(layer, false);
}
}
repaint();
}
protected LatLong convertPoint(Point point) {
return new LatLong(point.getLatitude(), point.getLongitude());
}
private TileRendererLayer createTileRendererLayer(TileCache tileCache,
MapDataStore mapDataStore, IMapViewPosition mapViewPosition,
HillsRenderConfig hillsRenderConfig) {
TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore,
mapViewPosition, false, true, false, GRAPHIC_FACTORY, hillsRenderConfig) {
@Override
public boolean onTap(LatLong tapLatLong, org.mapsforge.core.model.Point layerXY,
org.mapsforge.core.model.Point tapXY) {
if (zoomControls.contains(new java.awt.Point((int) tapXY.x, (int) tapXY.y))) {
return false;
}
Point pt = new Point((float) tapLatLong.getLongitude(),
(float) tapLatLong.getLatitude());
for (DrawingClickListener listener: MapViewDrawing.this.drawingClickListeners) {
listener.mouseClicked(pt);
}
return true;
}
};
tileRendererLayer.setXmlRenderTheme(InternalRenderTheme.DEFAULT);
return tileRendererLayer;
}
@Override
public void addDrawingClickListener(DrawingClickListener listener) {
this.drawingClickListeners.add(listener);
}
@Override
public void removeDrawingClickListener(DrawingClickListener listener) {
this.drawingClickListeners.remove(listener);
}
protected MarkerAutoScaling createMarker(Point point, Color outer, Color inner,
AlphaMode mode) {
Image image = MarkerUtils.getMarkerForColor(outer, inner, mode);
return new MarkerAutoScaling(convertPoint(point), image);
}
@Override
public MarkerOverlay drawMarker(Point point, Color outer, Color inner, AlphaMode mode) {
return new MapViewMarkerOverlay(createMarker(point, outer, inner, mode), outer, inner,
mode);
}
@Override
public PointSetOverlay createPointSetOverlay() {
return new MapViewPointSetOverlay();
}
@Override
public PointSetOverlay createPointSetOverlay(int width, Color color) {
PointSetOverlay ps = new MapViewPointSetOverlay();
ps.setWidthAndColor(width, color);
return ps;
}
public void drawGraph(File file) {
// Tile cache
TileCache tileCache = AwtUtil.createTileCache(tileSize,
getModel().frameBufferModel.getOverdrawFactor(), 1024,
new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()));
// Layers
Layers layers = getLayerManager().getLayers();
MapDataStore mapDataStore = new MapFile(file);
TileRendererLayer tileRendererLayer = createTileRendererLayer(tileCache, mapDataStore,
getModel().mapViewPosition, null);
layers.add(tileRendererLayer);
BoundingBox boundingBox = mapDataStore.boundingBox();
final Model model = getModel();
if (model.mapViewPosition.getZoomLevel() == 0
|| !boundingBox.contains(model.mapViewPosition.getCenter())) {
byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(),
boundingBox, model.displayModel.getTileSize());
model.mapViewPosition
.setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel));
zoomControls.setZoomLevel(zoomLevel);
}
}
@Override
public void drawGraph(Graph graph, GraphPalette palette) {
throw new RuntimeException("Not implemented, use drawGraph(File).");
}
@Override
public void drawGraph(Graph graph) {
throw new RuntimeException("Not implemented, use drawGraph(File).");
}
@Override
public PathOverlay drawPath(Path path, Color color, boolean markers) {
PolylineAutoScaling line = new PolylineAutoScaling(1, color);
ArrayList<Point> points = new ArrayList<>(path.getArcs().size() * 4);
for (Arc arc: path.getArcs()) {
points.addAll(arc.getPoints());
}
line.addAll(points);
PathOverlay overlay = null;
if (markers) {
MarkerAutoScaling origin = createMarker(path.getOrigin().getPoint(), color, color,
AlphaMode.TRANSPARENT),
destination = createMarker(path.getDestination().getPoint(), color, color,
AlphaMode.TRANSPARENT);
overlay = new MapViewPathOverlay(line, origin, destination);
}
else {
overlay = new MapViewPathOverlay(line);
}
return overlay;
}
@Override
public PathOverlay drawPath(Path path, Color color) {
return drawPath(path, color, true);
}
@Override
public PathOverlay drawPath(Path path) {
return drawPath(path, DEFAULT_PATH_COLOR, true);
}
@Override
public PathOverlay drawPath(Path path, boolean markers) {
return drawPath(path, DEFAULT_PATH_COLOR, markers);
}
}

View File

@ -1,4 +1,4 @@
package org.insa.graphics.drawing.components;
package org.insa.graphs.gui.drawing.components;
import java.awt.BasicStroke;
import java.awt.Color;

View File

@ -1,4 +1,4 @@
package org.insa.graphics.drawing.components;
package org.insa.graphs.gui.drawing.components;
import java.awt.Component;
import java.awt.Point;

View File

@ -1,4 +1,4 @@
package org.insa.graphics.drawing.overlays;
package org.insa.graphs.gui.drawing.overlays;
import java.awt.Graphics2D;
import java.awt.Image;

View File

@ -1,6 +1,6 @@
package org.insa.graphics.drawing.overlays;
package org.insa.graphs.gui.drawing.overlays;
import org.insa.graph.Point;
import org.insa.graphs.model.Point;
public interface MarkerOverlay extends Overlay {

View File

@ -1,97 +1,97 @@
package org.insa.graphics.drawing.overlays;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import org.insa.graphics.drawing.Drawing.AlphaMode;
public class MarkerUtils {
/**
* Create an image to represent a marker using the given color for the outer and
* inner part, and the given mode for the inner part.
*
* @param outer Outer color of the marker.
* @param inner Inner color of the marker.
* @param mode Mode to use to fill the inner part of the marker.
*
* @return An image representing a marker.
*/
public static Image getMarkerForColor(Color outer, Color inner, AlphaMode mode) {
// create image
int[][] mask = readMarkerMask();
BufferedImage image = new BufferedImage(mask[0].length, mask.length,
BufferedImage.TYPE_4BYTE_ABGR);
// Color[] map = getColorMapping(color);
int outerRGB = outer.getRGB() & 0x00ffffff;
for (int i = 0; i < image.getHeight(); ++i) {
for (int j = 0; j < image.getWidth(); ++j) {
// If we are in the "inner" part of the marker...
if (i >= MIN_Y_CENTER && i < MAX_Y_CENTER && j >= MIN_X_CENTER && j < MAX_X_CENTER
&& mask[i][j] != MAXIMUM_INNER_MASK_VALUE) {
// Don't ask... https://stackoverflow.com/a/29321264/2666289
// Basically, this compute a "middle" color between outer and inner depending on
// the current mask value.
double t = 1 - (mask[i][j] - MINIMUM_INNER_MASK_VALUE)
/ (double) (MAXIMUM_INNER_MASK_VALUE - MINIMUM_INNER_MASK_VALUE);
int r = (int) Math.sqrt((1 - t) * outer.getRed() * outer.getRed()
+ t * inner.getRed() * inner.getRed());
int g = (int) Math.sqrt((1 - t) * outer.getGreen() * outer.getGreen()
+ t * inner.getGreen() * inner.getGreen());
int b = (int) Math.sqrt((1 - t) * outer.getBlue() * outer.getBlue()
+ t * inner.getBlue() * inner.getBlue());
int a = mode == AlphaMode.OPAQUE ? MAXIMUM_INNER_MASK_VALUE : mask[i][j];
image.setRGB(j, i, (a << 24) | (r << 16) | (g << 8) | b);
}
// Otherwize, just fill with the outer color and set the alpha value properly.
else {
image.setRGB(j, i, outerRGB | (mask[i][j] << 24));
}
}
}
return image;
}
// Mask cache
private static int[][] MASK_CACHE = null;
// Hand-made... These defines the "center" of the marker, that can be filled
// with a different color.
private static final int MIN_X_CENTER = 40, MAX_X_CENTER = 101, MIN_Y_CENTER = 40,
MAX_Y_CENTER = 100;
private static final int MINIMUM_INNER_MASK_VALUE = 116, MAXIMUM_INNER_MASK_VALUE = 249;
/**
* @return Retrieve the mask from the mask file or from the cache.
*/
private static int[][] readMarkerMask() {
if (MASK_CACHE == null) {
try {
DataInputStream dis = new DataInputStream(
MarkerUtils.class.getResourceAsStream("/marker_mask.bin"));
int nrows = dis.readInt();
int ncols = dis.readInt();
MASK_CACHE = new int[nrows][ncols];
for (int i = 0; i < nrows; ++i) {
for (int j = 0; j < ncols; ++j) {
MASK_CACHE[i][j] = dis.readUnsignedByte();
}
}
dis.close();
}
catch (Exception e) {
e.printStackTrace();
MASK_CACHE = null;
}
}
return MASK_CACHE;
}
}
package org.insa.graphs.gui.drawing.overlays;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import org.insa.graphs.gui.drawing.Drawing.AlphaMode;
public class MarkerUtils {
/**
* Create an image to represent a marker using the given color for the outer and
* inner part, and the given mode for the inner part.
*
* @param outer Outer color of the marker.
* @param inner Inner color of the marker.
* @param mode Mode to use to fill the inner part of the marker.
*
* @return An image representing a marker.
*/
public static Image getMarkerForColor(Color outer, Color inner, AlphaMode mode) {
// create image
int[][] mask = readMarkerMask();
BufferedImage image = new BufferedImage(mask[0].length, mask.length,
BufferedImage.TYPE_4BYTE_ABGR);
// Color[] map = getColorMapping(color);
int outerRGB = outer.getRGB() & 0x00ffffff;
for (int i = 0; i < image.getHeight(); ++i) {
for (int j = 0; j < image.getWidth(); ++j) {
// If we are in the "inner" part of the marker...
if (i >= MIN_Y_CENTER && i < MAX_Y_CENTER && j >= MIN_X_CENTER && j < MAX_X_CENTER
&& mask[i][j] != MAXIMUM_INNER_MASK_VALUE) {
// Don't ask... https://stackoverflow.com/a/29321264/2666289
// Basically, this compute a "middle" color between outer and inner depending on
// the current mask value.
double t = 1 - (mask[i][j] - MINIMUM_INNER_MASK_VALUE)
/ (double) (MAXIMUM_INNER_MASK_VALUE - MINIMUM_INNER_MASK_VALUE);
int r = (int) Math.sqrt((1 - t) * outer.getRed() * outer.getRed()
+ t * inner.getRed() * inner.getRed());
int g = (int) Math.sqrt((1 - t) * outer.getGreen() * outer.getGreen()
+ t * inner.getGreen() * inner.getGreen());
int b = (int) Math.sqrt((1 - t) * outer.getBlue() * outer.getBlue()
+ t * inner.getBlue() * inner.getBlue());
int a = mode == AlphaMode.OPAQUE ? MAXIMUM_INNER_MASK_VALUE : mask[i][j];
image.setRGB(j, i, (a << 24) | (r << 16) | (g << 8) | b);
}
// Otherwize, just fill with the outer color and set the alpha value properly.
else {
image.setRGB(j, i, outerRGB | (mask[i][j] << 24));
}
}
}
return image;
}
// Mask cache
private static int[][] MASK_CACHE = null;
// Hand-made... These defines the "center" of the marker, that can be filled
// with a different color.
private static final int MIN_X_CENTER = 40, MAX_X_CENTER = 101, MIN_Y_CENTER = 40,
MAX_Y_CENTER = 100;
private static final int MINIMUM_INNER_MASK_VALUE = 116, MAXIMUM_INNER_MASK_VALUE = 249;
/**
* @return Retrieve the mask from the mask file or from the cache.
*/
private static int[][] readMarkerMask() {
if (MASK_CACHE == null) {
try {
DataInputStream dis = new DataInputStream(
MarkerUtils.class.getResourceAsStream("/marker_mask.bin"));
int nrows = dis.readInt();
int ncols = dis.readInt();
MASK_CACHE = new int[nrows][ncols];
for (int i = 0; i < nrows; ++i) {
for (int j = 0; j < ncols; ++j) {
MASK_CACHE[i][j] = dis.readUnsignedByte();
}
}
dis.close();
}
catch (Exception e) {
e.printStackTrace();
MASK_CACHE = null;
}
}
return MASK_CACHE;
}
}

View File

@ -1,4 +1,4 @@
package org.insa.graphics.drawing.overlays;
package org.insa.graphs.gui.drawing.overlays;
import java.awt.Color;

View File

@ -1,4 +1,4 @@
package org.insa.graphics.drawing.overlays;
package org.insa.graphs.gui.drawing.overlays;
import java.awt.Color;

View File

@ -1,4 +1,4 @@
package org.insa.graphics.drawing.overlays;
package org.insa.graphs.gui.drawing.overlays;
public interface PathOverlay extends Overlay {

View File

@ -1,8 +1,8 @@
package org.insa.graphics.drawing.overlays;
package org.insa.graphs.gui.drawing.overlays;
import java.awt.Color;
import org.insa.graph.Point;
import org.insa.graphs.model.Point;
public interface PointSetOverlay extends Overlay {

View File

@ -1,10 +1,10 @@
package org.insa.graphics.drawing.overlays;
package org.insa.graphs.gui.drawing.overlays;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Collection;
import org.insa.graph.Point;
import org.insa.graphs.model.Point;
import org.mapsforge.core.graphics.Canvas;
import org.mapsforge.core.graphics.GraphicFactory;
import org.mapsforge.core.graphics.Style;

View File

@ -1,10 +1,11 @@
package org.insa.algo.shortestpath;
package org.insa.graphs.gui.observers;
import java.awt.Color;
import org.insa.graph.Node;
import org.insa.graphics.drawing.Drawing;
import org.insa.graphics.drawing.overlays.PointSetOverlay;
import org.insa.graphs.algorithm.shortestpath.ShortestPathObserver;
import org.insa.graphs.gui.drawing.Drawing;
import org.insa.graphs.gui.drawing.overlays.PointSetOverlay;
import org.insa.graphs.model.Node;
public class ShortestPathGraphicObserver implements ShortestPathObserver {

View File

@ -1,15 +1,17 @@
package org.insa.algo.weakconnectivity;
package org.insa.graphs.gui.observers;
import java.awt.Color;
import java.util.ArrayList;
import org.insa.graph.Node;
import org.insa.graphics.drawing.Drawing;
import org.insa.graphics.drawing.overlays.PointSetOverlay;
import org.insa.graphs.algorithm.weakconnectivity.WeaklyConnectedComponentObserver;
import org.insa.graphs.gui.drawing.Drawing;
import org.insa.graphs.gui.drawing.overlays.PointSetOverlay;
import org.insa.graphs.model.Node;
public class WeaklyConnectedComponentGraphicObserver implements WeaklyConnectedComponentObserver {
private static final Color[] COLORS = { Color.BLUE, Color.ORANGE, Color.GREEN, Color.YELLOW, Color.RED };
private static final Color[] COLORS = { Color.BLUE, Color.ORANGE, Color.GREEN, Color.YELLOW,
Color.RED };
// Drawing + Graph drawing
private PointSetOverlay grPoints;

View File

@ -1,4 +1,4 @@
package org.insa.base;
package org.insa.graphs.gui.simple;
import java.awt.BorderLayout;
import java.awt.Dimension;
@ -9,13 +9,13 @@ import java.io.FileInputStream;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import org.insa.graph.Graph;
import org.insa.graph.Path;
import org.insa.graph.io.BinaryGraphReader;
import org.insa.graph.io.GraphReader;
import org.insa.graph.io.PathReader;
import org.insa.graphics.drawing.Drawing;
import org.insa.graphics.drawing.components.BasicDrawing;
import org.insa.graphs.gui.drawing.Drawing;
import org.insa.graphs.gui.drawing.components.BasicDrawing;
import org.insa.graphs.model.Graph;
import org.insa.graphs.model.Path;
import org.insa.graphs.model.io.BinaryGraphReader;
import org.insa.graphs.model.io.GraphReader;
import org.insa.graphs.model.io.PathReader;
public class Launch {
@ -46,29 +46,28 @@ public class Launch {
public static void main(String[] args) throws Exception {
// Visit these directory to see the list of available files on Commetud.
String mapName = "/home/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr";
String pathName = "/home/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31insa_rangueil_r2.path";
final String mapName = "/home/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps/insa.mapgr";
final String pathName = "/home/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Paths/path_fr31insa_rangueil_r2.path";
// Create a graph reader.
GraphReader reader = new BinaryGraphReader(
final GraphReader reader = new BinaryGraphReader(
new DataInputStream(new BufferedInputStream(new FileInputStream(mapName))));
// TODO: Read the graph.
Graph graph = null;
final Graph graph = null;
// Create the drawing:
Drawing drawing = createDrawing();
final Drawing drawing = createDrawing();
// TODO: Draw the graph on the drawing.
// TODO: Create a PathReader.
PathReader pathReader = null;
final PathReader pathReader = null;
// TODO: Read the path.
Path path = null;
final Path path = null;
// TODO: Draw the path.
}
}

View File

@ -1,4 +1,4 @@
package org.insa.graphics.utils;
package org.insa.graphs.gui.utils;
import java.awt.Color;

View File

@ -1,4 +1,4 @@
package org.insa.graphics.utils;
package org.insa.graphs.gui.utils;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

View File

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

Before

Width:  |  Height:  |  Size: 3.0 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

View File

Before

Width:  |  Height:  |  Size: 3.5 KiB

After

Width:  |  Height:  |  Size: 3.5 KiB

View File

Before

Width:  |  Height:  |  Size: 3.2 KiB

After

Width:  |  Height:  |  Size: 3.2 KiB

14
be-graphes-model/pom.xml Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.insa.graphs</groupId>
<artifactId>be-graphes-all</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>be-graphes-model</artifactId>
<name>be-graphes-model</name>
</project>

View File

@ -1,4 +1,4 @@
package org.insa.graph;
package org.insa.graphs.model;
import java.util.EnumMap;
import java.util.EnumSet;

View File

@ -1,4 +1,4 @@
package org.insa.graph;
package org.insa.graphs.model;
import java.util.List;

View File

@ -1,4 +1,4 @@
package org.insa.graph;
package org.insa.graphs.model;
import java.util.ArrayList;
import java.util.Collections;

View File

@ -1,4 +1,4 @@
package org.insa.graph;
package org.insa.graphs.model;
import java.util.Collections;
import java.util.List;

View File

@ -1,4 +1,4 @@
package org.insa.graph;
package org.insa.graphs.model;
import java.util.ArrayList;
import java.util.Collections;

View File

@ -1,4 +1,4 @@
package org.insa.graph;
package org.insa.graphs.model;
/**
* <p>

View File

@ -1,4 +1,4 @@
package org.insa.graph;
package org.insa.graphs.model;
import java.util.ArrayList;
import java.util.Collections;

View File

@ -1,4 +1,4 @@
package org.insa.graph;
package org.insa.graphs.model;
import java.util.ArrayList;
import java.util.Collections;

View File

@ -1,4 +1,4 @@
package org.insa.graph;
package org.insa.graphs.model;
/**
* Class representing a point (position) on Earth.

View File

@ -1,4 +1,4 @@
package org.insa.graph;
package org.insa.graphs.model;
/**
* <p>

View File

@ -1,4 +1,4 @@
package org.insa.graph.io;
package org.insa.graphs.model.io;
import java.io.IOException;
@ -21,4 +21,13 @@ public class BadFormatException extends IOException {
super();
}
/**
* Create a new format exception with the given message.
*
* @param message Message for the exception.
*/
public BadFormatException(String message) {
super(message);
}
}

Some files were not shown because too many files have changed in this diff Show More