Commit 67f31c85 authored by peter's avatar peter
Browse files

Not complete solution - buggy

parent ea87ceeb
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package isospline;
import java.util.ArrayList;
/**
*
* @author peter
*/
public class Directions extends ArrayList<String>{
public static String NORTH = "North";
public static String EAST = "East";
public static String SOUTH = "South";
public static String WEST = "West";
public static String NO_DIRECTION = "No direction";
public Directions(){
add(SOUTH);
add(EAST);
add(NORTH);
add(WEST);
}
public String getOppositeDirection(String direction){
if(direction.equals(NORTH))
return SOUTH;
if(direction.equals(SOUTH))
return NORTH;
if(direction.equals(EAST))
return WEST;
if(direction.equals(WEST))
return EAST;
return null;
}
}
......@@ -40,126 +40,127 @@ public class IsoSpline {
}
public Polygons createIsoPolygon(IsoApi matrix, String name, Color color, double isoLevel){
public Polygons createIsoPolygons(IsoApi matrix, String name, Color color, double isoLevel){
for(int r=0; r<matrix.getRows()-1; r++)
for(int c=0; c<matrix.getCols()-1; c++){
if(iscs.getIsoSplineContainer(r,c).canCreateIsoSplineElement(isoLevel))
iscs.getIsoSplineContainer(r, c).createSplineElement(isoLevel);
}
return createPolygons(isoLevel, color);
return sortIsoPolygons(isoLevel, color);
}
private Polygons createPolygons(double isoLevel, Color color){
private Polygons sortIsoPolygons(double isoLevel, Color color){
Polygons polygons = new Polygons();
String direction;
IsoSplineElement currentIses;
IsoSplineElements ises;
IsoSplineContainer startContainer;
IsoSplineContainer finishContainer;
IsoSplineContainer currentContainer;
for(int r=0;r<iscs.rows;r++)
for(int r=0;r<iscs.rows;r++){
for(int c=0;c<iscs.cols;c++){
startContainer = iscs.getIsoSplineContainer(r, c);
Polygon polygon = new Polygon();
polygon.color=color;
if(startContainer.hasIsoSplineElement()){
ises = new IsoSplineElements("IsoLevel: " + isoLevel, color, startContainer.ref);
int sortIndex=0;
currentIses = startContainer.getIsoSplineElement(sortIndex);
ises.add(currentIses);
//System.out.println("new polygon " + isoLevel);
//direction = start.getPrimaryDirection();
direction = currentIses.get(0).direction;
currentContainer=startContainer;
currentContainer.nextDirection = direction;
//System.out.println("rc: > (" + current.ref.r + ", "+ current.ref.c + ") direction " + direction + " (next direction " + current.nextDirection + ")");
while(currentContainer.hasNotUsedNeighbourInDirection()){
currentContainer = step(currentContainer, ises, sortIndex);
sortIndex++;
}
finishContainer=currentContainer;
if(!currentContainer.equals(startContainer)){
currentContainer=startContainer;
direction = startContainer.coords.get(1).direction;
currentContainer.nextDirection = direction;
//System.out.println("rc: < (" + current.ref.r + ", "+ current.ref.c + ") direction " + direction + " (next direction " + current.nextDirection + ")");
sortIndex=-1;
while(currentContainer.hasNotUsedNeighbourInDirection()){
currentContainer = step(currentContainer, ises, sortIndex);
sortIndex--;
}
if(!currentContainer.equals(finishContainer))
polygon.closed = true;
}
else
polygon.closed = true;
polygons.add(trace(startContainer, color, isoLevel));
}
}
}
return polygons;
}
if(ises.size()>0){
ises.sort();
IsoSplineElement ise0 = ises.get(0);
//System.out.println(ise0.toString());
if(ises.size()>1){
IsoSplineElement ise1 = ises.get(1);
//System.out.println(ise1.toString());
int m;
int n;
boolean foundEquals=false;
for(m = 0; m<ise0.size();m++){
for(n = 0; n<ise0.size();n++){
if(ise0.get(m).equals(ise1.get(n))){
foundEquals=true;
break;
}
}
if(foundEquals){
break;
}
}
private Polygon trace(IsoSplineContainer startContainer, Color color, double isoLevel){
String currentDirection;
IsoSplineContainer finishContainer;
IsoSplineContainer currentContainer;
IsoSplineElement currentIse;
IsoSplineElements ises;
ises = new IsoSplineElements("IsoLevel: " + isoLevel, color, startContainer.ref);
int sortIndex=0;
Polygon polygon = new Polygon();
polygon.color=color;
currentIse = startContainer.getFirstIsoSplineElement();
currentIse.sortIndex=sortIndex;
ises.add(currentIse);
currentDirection = currentIse.get(0).direction;
currentContainer=startContainer;
while(currentContainer.hasIsoSplineContainerInDirection(currentDirection, true)){
currentContainer = currentContainer.getIsoSplineContainerInDirection(currentDirection);
currentDirection = step(currentContainer, currentDirection, ises, sortIndex);
sortIndex++;
}
if(currentContainer.hasIsoSplineContainerInDirection(currentDirection, false))
if(currentContainer.getIsoSplineContainerInDirection(currentDirection).equals(startContainer))
polygon.closed = true;
if(foundEquals){
polygon.add(ise0.get(1-m));
polygon.add(ise0.get(m));
Coordinate coord = ise0.get(m);
for(int i = 1;i<ises.size(); i++){
Coordinate oppositeCoord = ises.get(i).getOpposite(coord);
if(oppositeCoord == null)
break;
polygon.add(oppositeCoord);
coord = oppositeCoord;
}
}
else{
//System.out.println("There is a break in the polygon chain...");
}
}
else{ // only two coordinates
for(int i = 0; i<ise0.size();i++)
polygon.add(ise0.get(i));
if(!polygon.closed){
finishContainer=currentContainer;
currentContainer=startContainer;
currentDirection = startContainer.coords.get(1).direction;
sortIndex=-1;
while(currentContainer.hasIsoSplineElementWithDirection(currentDirection)){
currentContainer = currentContainer.getIsoSplineContainerInDirection(currentDirection);
currentDirection = step(currentContainer, currentDirection, ises, sortIndex);
sortIndex--;
}
}
return createPolygon(ises, polygon);
}
private Polygon createPolygon(IsoSplineElements ises, Polygon polygon){
if(ises.size()>0){
ises.sort();
IsoSplineElement ise0 = ises.get(0);
//System.out.println(ise0.toString());
if(ises.size()>1){
IsoSplineElement ise1 = ises.get(1);
//System.out.println(ise1.toString());
int m;
int n;
boolean foundEquals=false;
for(m = 0; m<ise0.size();m++){
for(n = 0; n<ise0.size();n++){
if(ise0.get(m).equals(ise1.get(n))){
foundEquals=true;
break;
}
}
polygons.add(polygon);
System.out.println(polygon.toString());
if(foundEquals){
break;
}
}
if(foundEquals){
polygon.add(ise0.get(1-m));
polygon.add(ise0.get(m));
Coordinate coord = ise0.get(m);
for(int i = 1;i<ises.size(); i++){
Coordinate oppositeCoord = ises.get(i).get(1); /// TODO
if(oppositeCoord == null)
break;
polygon.add(oppositeCoord);
coord = oppositeCoord;
}
}
else{
//System.out.println("There is a break in the polygon chain...");
}
}
return polygons;
else{ // only two coordinates
for(int i = 0; i<ise0.size();i++)
polygon.add(ise0.get(i));
}
}
System.out.println(polygon);
return polygon;
}
private IsoSplineContainer step(IsoSplineContainer current, IsoSplineElements polygon, int sortIndex){
String searchdirection = ">";
if(sortIndex<0)
searchdirection = "<";
String oppositeDirection;
IsoSplineContainer next;
next = current.getNext();
private String step(IsoSplineContainer current, String direction, IsoSplineElements polygon, int sortIndex){
IsoSplineElement ise;
ise = current.getIsoSplineElementWithDirection(direction);
ise.sortIndex=sortIndex;
String nextDirection = ise.getOppositeDirection(direction);
//System.out.println("rc: " + searchdirection + " (" + next.ref.r + ", "+ next.ref.c + ") direction " + current.nextDirection + " (next direction " + next.nextDirection + ")");
polygon.add(next.getIsoSplineElement(sortIndex));
oppositeDirection = current.getOppositeDirection(current.nextDirection);
next.setNextDirection(oppositeDirection);
return next;
polygon.add(ise);
return nextDirection;
}
}
......@@ -12,17 +12,12 @@ import java.util.ArrayList;
* @author a001188
*/
public class IsoSplineContainer {
public static String NORTH = "North";
public static String EAST = "East";
public static String SOUTH = "South";
public static String WEST = "West";
public static String NO_DIRECTION = "No direction";
private final IsoSplineContainers iscs;
public final ArrayList<Coordinate> coords = new ArrayList();
private IsoSplineElements ises;
public final Coordinate ref;
private final ArrayList<String> compass = new ArrayList();
public String nextDirection = NO_DIRECTION;
private final ArrayList<String> directions = new Directions();
//public String nextDirection = NO_DIRECTION;
public int currentIsoSplineElementIndex = 0;
public IsoSplineContainer(IsoSplineContainers iscs, Coordinate coord0, Coordinate coord1, Coordinate coord2, Coordinate coord3){
......@@ -32,78 +27,100 @@ public class IsoSplineContainer {
coords.add(coord2);
coords.add(coord3);
this.ref=coord0;
compass.add(SOUTH);
compass.add(EAST);
compass.add(NORTH);
compass.add(WEST);
}
public boolean hasDirection(String direction){
return compass.contains(direction);
}
///////////////////////////////////////////////////////////////////////////////
// IsoSplineContainer
///////////////////////////////////////////////////////////////////////////////
public boolean equals(IsoSplineContainer isc){
return coords.equals(isc.coords);
}
public String getPrimaryDirection(){
return ises.get(currentIsoSplineElementIndex).get(0).direction;
public IsoSplineContainer getIsoSplineContainerInDirection(String direction){
if(direction.equals(Directions.SOUTH)){
return iscs.getIsoSplineContainer(ref.r-1, ref.c);
}
if(direction.equals(Directions.EAST)){
return iscs.getIsoSplineContainer(ref.r, ref.c+1);
}
if(direction.equals(Directions.WEST)){
return iscs.getIsoSplineContainer(ref.r, ref.c-1);
}
if(direction.equals(Directions.NORTH))
return iscs.getIsoSplineContainer(ref.r+1, ref.c);
return null;
}
public String getSecondaryDirection(){
return ises.get(currentIsoSplineElementIndex).get(1).direction;
}
public boolean hasIsoSplineContainerInDirection(String direction, boolean hasIsoSpline){
if(direction.equals(Directions.SOUTH)){
if(ref.r-1>=0)
if(hasIsoSpline)
return this.hasIsoSplineElementWithDirection(Directions.NORTH);
else
return true;
}
else if(direction.equals(Directions.EAST)){
if(ref.c+1 < iscs.cols)
if(hasIsoSpline)
return this.hasIsoSplineElementWithDirection(Directions.WEST);
else
return true;
}
else if(direction.equals(Directions.NORTH)){
if(ref.r+1 < iscs.rows)
if(hasIsoSpline)
return this.hasIsoSplineElementWithDirection(Directions.SOUTH);
else
return true;
}
else if(direction.equals(Directions.WEST)){
if(ref.c-1>=0)
if(hasIsoSpline)
return this.hasIsoSplineElementWithDirection(Directions.EAST);
else
return true;
}
return false;
}
public String getOppositeDirection(String direction){
if(direction.equals(NORTH))
return SOUTH;
if(direction.equals(SOUTH))
return NORTH;
if(direction.equals(EAST))
return WEST;
if(direction.equals(WEST))
return EAST;
return null;
}
public void setNextDirection(String direction){
for(int i = 0; i<ises.size(); i++)
for(int j = 0; j<ises.size(); j++)
if(ises.get(i).get(j).direction.equals(direction)){
nextDirection = ises.get(i).get(1-j).direction;
currentIsoSplineElementIndex = i;
break;
}
///////////////////////////////////////////////////////////////////////////////
// IsoSplineElement
///////////////////////////////////////////////////////////////////////////////
public boolean canCreateIsoSplineElement(double isoLevel){
double min02 = Math.min(coords.get(0).value,coords.get(2).value);
double max13 = Math.max(coords.get(1).value,coords.get(3).value);
double min13 = Math.min(coords.get(1).value,coords.get(3).value);
double max02 = Math.max(coords.get(0).value,coords.get(2).value);
double min = Math.min(min02, min13);
double max = Math.max(max02, max13);
// // Undefined level
// if(min02 >= max13)
// if(isoLevel >= max13 && isoLevel <= min02)
// return false;
// // Undefined level
// if(min13 >= max02)
// if(isoLevel >= max02 && isoLevel <= min13)
// return false;
// Iso out of range
if(isoLevel <= min || max <= isoLevel)
return false;
nextDirection = NO_DIRECTION;
}
public boolean hasNotUsedNeighbourInDirection(){
if(ref.r>0 && nextDirection.equals(SOUTH))
if(getNext()!=null)
return getNext().hasIsoSplineElement();
else
return false;
if(ref.c>0 && nextDirection.equals(WEST))
if(getNext()!=null)
return getNext().hasIsoSplineElement();
else
return false;
if(ref.r<iscs.rows && nextDirection.equals(NORTH))
if(getNext()!=null)
return getNext().hasIsoSplineElement();
else
return false;
if(ref.c<iscs.cols && nextDirection.equals(EAST))
if(getNext()!=null)
return getNext().hasIsoSplineElement();
else
return false;
return false;
// if(Math.abs(min - max) < Coordinate.MAX_DIFF)
// return false;
return true;
}
public void createSplineElement(double isoLevel){
ArrayList<Coordinate> splineCoords = new ArrayList();
......@@ -114,7 +131,7 @@ public class IsoSplineContainer {
j=0;
else
j=i+1;
// Find out in whitch direction a spline point exist
Coordinate coord1 = coords.get(i);
Coordinate coord2 = coords.get(j);
if(Math.max(coord1.value, coord2.value)>=isoLevel)
......@@ -122,11 +139,12 @@ public class IsoSplineContainer {
double range = (isoLevel - coord1.value)/(coord2.value - coord1.value);
double lat = range * (coord2.lat-coord1.lat) + coord1.lat;
double lon = range * (coord2.lon-coord1.lon) + coord1.lon;
splineCoords.add(new Coordinate(lon, lat, coord1.r, coord1.c, isoLevel, compass.get(i)));
splineCoords.add(new Coordinate(lon, lat, coord1.r, coord1.c, isoLevel, directions.get(i)));
}
}
ises = new IsoSplineElements(ref);
// Create splines
switch (splineCoords.size()) {
case 2:
ises.add(new IsoSplineElement(splineCoords.get(0),splineCoords.get(1)));
......@@ -142,61 +160,59 @@ public class IsoSplineContainer {
System.out.println("Wrong number of iso points: " + coords.toString());
break;
}
}
public IsoSplineContainer getNext(){
if(nextDirection.equals(SOUTH))
return iscs.getIsoSplineContainer(ref.r-1, ref.c);
if(nextDirection.equals(EAST))
return iscs.getIsoSplineContainer(ref.r, ref.c+1);
if(nextDirection.equals(WEST))
return iscs.getIsoSplineContainer(ref.r, ref.c-1);
if(nextDirection.equals(NORTH))
return iscs.getIsoSplineContainer(ref.r+1, ref.c);
return null;
public boolean hasIsoSplineElement(){
if(ises!=null)
return ises.size()>0;
return false;
}
public IsoSplineElement getIsoSplineElement(int sortIndex){
IsoSplineElement ise = ises.get(currentIsoSplineElementIndex);
ises.remove(currentIsoSplineElementIndex);
currentIsoSplineElementIndex=0;
ise.sortIndex=sortIndex;
public IsoSplineElement getFirstIsoSplineElement(){
IsoSplineElement ise = ises.get(0);
ises.remove(0);
return ise;
}
public boolean hasIsoSplineElement(){
return ises.size()>0;
public boolean hasIsoSplineElementWithDirection(String direction){
if(ises!=null && direction!=null){
for(int i = 0; i<ises.size(); i++)
if(ises.get(i)!=null)
for(int j = 0; j<ises.get(i).size(); j++)
if(direction.equals(ises.get(i).get(j).direction))
return true;
}
else
return false;
return false;
}
public boolean canCreateIsoSplineElement(double isoLevel){
double min02 = Math.min(coords.get(0).value,coords.get(2).value);
double max13 = Math.max(coords.get(1).value,coords.get(3).value);
double min13 = Math.min(coords.get(1).value,coords.get(3).value);
double max02 = Math.max(coords.get(0).value,coords.get(2).value);
double min = Math.min(min02, min13);
double max = Math.max(max02, max13);
// // Undefined level
// if(min02 >= max13)
// if(isoLevel >= max13 && isoLevel <= min02)
// return false;
// // Undefined level
// if(min13 >= max02)
// if(isoLevel >= max02 && isoLevel <= min13)
// return false;
// Iso out of range
if(isoLevel <= min || max <= isoLevel)
return false;
// if(Math.abs(min - max) < Coordinate.MAX_DIFF)
// return false;
return true;
public IsoSplineElement getIsoSplineElementWithDirection(String direction){
for(int i = 0; i<ises.size(); i++)
for(int j = 0; j<ises.get(i).size(); j++)
if(direction.equals(ises.get(i).get(j).direction)){
ises.remove(i);
return ises.get(i);
}
return null;
}
public String getPrimaryDirection(){
return ises.get(currentIsoSplineElementIndex).get(0).direction;
}
public String getSecondaryDirection(){
return ises.get(currentIsoSplineElementIndex).get(1).direction;
}
@Override
public String toString(){
......
......@@ -32,14 +32,12 @@ public class IsoSplineElement extends ArrayList<Coordinate> implements Comparab
return false;
}
public Coordinate getOpposite(Coordinate coord){
if(coord==null)
return null;
for(int i = 0;i<size();i++)
if(coord.equals(get(i)))
return get(1-i);
return null;
public String getOppositeDirection(String direction){
if (get(0).direction.equals(direction))
return get(1).direction;
else
return get(0).direction;
}
@Override
......
......@@ -73,7 +73,7 @@ public class Main {
Filter filter = new Filter(clone);
filter.filtering(filterWidth, filterIter, filterRange);
}
Polygons polygons = iso.createIsoPolygon(clone, "test" + i + "isoLevel: " + isoLevels.get(i), colors.get(i), isoLevels.get(i));