Commit dd6bfc1b authored by a001188's avatar a001188
Browse files

Initial commit

parent a9134b35
......@@ -40,7 +40,7 @@ public class Coordinate implements Cloneable{
@Override
public Coordinate clone(){
return new Coordinate(lon, lat, r, c, value, null);
return new Coordinate(lon, lat, r, c, value, direction);
}
public void fitToFrame(Dimension dimension, MatrixCorners gridCorners, String type){
......
......@@ -35,6 +35,25 @@ public class Directions {
directions.add(NORTH_WEST);
}
public static 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;
if(direction.equals(NORTH_WEST))
return SOUTH_EAST;
if(direction.equals(SOUTH_EAST))
return NORTH_WEST;
if(direction.equals(NORTH_EAST))
return SOUTH_WEST;
if(direction.equals(SOUTH_WEST))
return NORTH_EAST;
return NO_DIRECTION;
}
public String get(int direction, boolean onSpot){
int offset = 0;
......
......@@ -18,21 +18,69 @@ public class Filter {
}
private double getFilteredValue(int row, int col, int width, double range){
double sum =0;
double n = (width*2+1)*(width*2+1);
for(int r=row-width; r<row+width+1; r++)
for(int c=col-width; c<col+width+1; c++)
sum = sum + matrix.getValue(r,c);
return (1-range)*matrix.getValue(row,col) + range*sum/n;
}
public void filtering(int filterWidth, int filtrations, double range){
for(int i = 0; i<filtrations; i++){
for(int r = filterWidth; r<matrix.getRows()-filterWidth-1; r++)
for(int c = filterWidth; c<matrix.getCols()-filterWidth-1; c++){
matrix.setValue(r, c, getFilteredValue(r, c, filterWidth, range));
matrix.setValue(r, c, getFilteredValue2(r, c, filterWidth, range));
}
}
}
public void filtering2(int filterWidth, int filtrations, double range){
for(int i = 0; i<filtrations; i++){
for(int r = 0; r<matrix.getRows()-1; r++)
for(int c = 0; c<matrix.getCols()-1; c++){
matrix.setValue(r, c, getFilteredValue2(r, c, filterWidth, range));
}
}
}
private double getFilteredValue(int row, int col, int width, double range){
private double getFilteredValue2(int row, int col, int width, double range){
double sum =0;
double n = (width*2+1)*(width*2+1);
for(int r=row-width; r<row+width+1; r++)
for(int c=col-width; c<col+width+1; c++)
int r0;
int r1;
int c0;
int c1;
if(row-width < 0)
r0=0;
else
r0=row-width;
if(col-width < 0)
c0=0;
else
c0=col-width;
if(row+width >= matrix.getRows()-1)
r1=matrix.getRows();
else
r1=row+width;
if(col+width >= matrix.getCols())
c1=matrix.getCols();
else
c1=col+width;
int n = (r1-r0)*(c1-c0);
for(int r=r0; r<r1; r++)
for(int c=c0; c<c1; c++)
sum = sum + matrix.getValue(r,c);
return (1-range)*matrix.getValue(row,col) + range*sum/n;
......
/*
* 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.awt.Color;
import java.util.ArrayList;
/**
*
* @author a001188
*/
public class IsoLevels {
ArrayList<Double> isoLevels = new ArrayList();
ArrayList<Color> colors = new ArrayList();
private final double minValue;
private final double maxValue;
public IsoLevels(int levels, double minValue, double maxValue){
this.minValue = minValue;
this.maxValue = maxValue;
for(int i=1;i<levels+1;i++){
double level = i * (maxValue-minValue)/(levels + 1) + minValue;
isoLevels.add(level);
}
for(int i= 0; i<levels;i++)
if(levels / 2 > i)
colors.add(Color.blue);
else if(levels/2 < i)
colors.add(Color.red);
else
colors.add(Color.black);
}
public int size(){
return isoLevels.size();
}
public Color getColor(int levelNo){
return colors.get(levelNo);
}
public double getIsoLevel(int levelNo){
return isoLevels.get(levelNo);
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("IsoLevels: ");
sb.append(isoLevels.size());
sb.append("\r\n");
sb.append("minValue: ");
sb.append(minValue);
sb.append("\r\n");
sb.append("maxValue: ");
sb.append(maxValue);
sb.append("\r\n");
for(int i = 0; i< size(); i++){
sb.append("IsoLevel no: ");
sb.append(i);
sb.append(", Level = ");
sb.append(isoLevels.get(i));
sb.append(", Color = ");
sb.append(colors.get(i));
sb.append("\r\n");
}
return sb.toString();
}
}
......@@ -29,15 +29,6 @@ public class IsoSpline {
}
public ArrayList<Double> getIsoLevels(){
ArrayList<Double> isoLevels = new ArrayList();
for(int i=1;i<Main.ISO_LEVELS+1;i++)
isoLevels.add(i * (max-min)/(Main.ISO_LEVELS+1) + min);
//System.out.println("Matrix min: " + min + ", Matrix max: " + max + " Matrix avg: " + avg);
//System.out.println("isoLevels (" + isoLevels.size() + "): " + isoLevels);
return isoLevels;
}
public Polygons createIsoPolygons(IsoApi matrix, String name, Color color, double isoLevel){
......@@ -47,7 +38,7 @@ public class IsoSpline {
if(iscs.getIsoSplineContainer(r,c).canCreateIsoSplineElement(isoLevel))
iscs.getIsoSplineContainer(r, c).createSplineElement(isoLevel);
}
return _sortIsoPolygons(isoLevel, color);
return sortIsoPolygons(isoLevel, color);
}
private Polygons _sortIsoPolygons(double isoLevel, Color color){
......@@ -55,13 +46,12 @@ public class IsoSpline {
for(int r=0;r<iscs.rows;r++){
for(int c=0;c<iscs.cols;c++)
while(iscs.getIsoSplineContainer(r, c).hasIsoSplineElement()){
Polygon polygon = new Polygon();
Polygon polygon = new Polygon("TEST", color, isoLevel);
IsoSplineElement ise = iscs.getIsoSplineContainer(r, c).getFirstIsoSplineElement();
polygon.add(ise.get(0));
polygon.add(ise.get(1));
polygons.add(polygon);
}
}
return polygons;
}
......@@ -75,7 +65,9 @@ public class IsoSpline {
for(int c=0;c<iscs.cols;c++){
startContainer = iscs.getIsoSplineContainer(r, c);
if(startContainer.hasIsoSplineElement()){
polygons.add(trace(startContainer, color, isoLevel));
Polygon polygon = trace(startContainer, color, isoLevel);
if(polygon.size()>Main.minPolygonSize)
polygons.add(polygon);
}
}
}
......@@ -90,30 +82,35 @@ public class IsoSpline {
IsoSplineElements ises;
ises = new IsoSplineElements("IsoLevel: " + isoLevel, color, startContainer.ref);
int sortIndex=0;
Polygon polygon = new Polygon();
polygon.color=color;
Polygon polygon = new Polygon(startContainer.ref.toString(), color, isoLevel);
//System.out.println(startContainer.toString());
currentIse = startContainer.getFirstIsoSplineElement();
currentIse.sortIndex=sortIndex;
ises.add(currentIse);
currentDirection = currentIse.get(0).direction;
currentDirection = currentIse.get(0).direction; // Start direction
currentContainer=startContainer;
while(currentContainer.hasIsoSplineContainerInDirection(currentDirection, true)){
currentContainer = currentContainer.getIsoSplineContainerInDirection(currentDirection);
currentDirection = step(currentContainer, currentDirection, ises, sortIndex);
//System.out.println(currentContainer.toString());
currentDirection = link(currentContainer, currentDirection, ises, sortIndex);
sortIndex++;
}
if(currentContainer.hasIsoSplineContainerInDirection(currentDirection, false))
if(currentContainer.getIsoSplineContainerInDirection(currentDirection).equals(startContainer))
if(currentContainer.hasIsoSplineContainerInDirection(currentDirection, false)){
IsoSplineContainer nextContainer = currentContainer.getIsoSplineContainerInDirection(currentDirection);
//System.out.println(nextContainer.toString());
if(nextContainer.equals(startContainer)){
polygon.closed = true;
}
}
if(!polygon.closed){
finishContainer=currentContainer;
currentContainer=startContainer;
currentDirection = startContainer.coords.get(1).direction;
currentDirection = currentIse.get(1).direction;
sortIndex=-1;
while(currentContainer.hasIsoSplineElementWithDirection(currentDirection)){
while(currentContainer.hasIsoSplineContainerInDirection(currentDirection, false)){
currentContainer = currentContainer.getIsoSplineContainerInDirection(currentDirection);
currentDirection = step(currentContainer, currentDirection, ises, sortIndex);
currentDirection = link(currentContainer, currentDirection, ises, sortIndex);
sortIndex--;
}
}
......@@ -121,20 +118,36 @@ public class IsoSpline {
}
private String link(IsoSplineContainer current, String direction, IsoSplineElements polygon, int sortIndex){
IsoSplineElement ise;
String movingDirection = Directions.getOppositeDirection(direction);
ise = current.getIsoSplineElementWithDirection(movingDirection);
if(ise==null)
return Directions.NO_DIRECTION;
ise.sortIndex=sortIndex;
String nextDirection = ise.getOppositeDirection(movingDirection);
//System.out.println("rc: " + searchdirection + " (" + next.ref.r + ", "+ next.ref.c + ") direction " + current.nextDirection + " (next direction " + next.nextDirection + ")");
polygon.add(ise);
return nextDirection;
}
private Polygon createPolygon(IsoSplineElements ises, Polygon polygon){
if(ises.size()>0){
ises.sort();
IsoSplineElement ise0 = ises.get(0);
//System.out.println(ise0.toString());
//System.out.println(ises.toString());
if(ises.size()>1){
IsoSplineElement ise1 = ises.get(1);
//System.out.println(ise1.toString());
int m;
int n;
boolean foundEquals=false;
boolean foundEquals=false; // Find start coordninate
for(m = 0; m<ise0.size();m++){
for(n = 0; n<ise0.size();n++){
for(n = 0; n<ise1.size();n++){
if(ise0.get(m).equals(ise1.get(n))){
foundEquals=true;
break;
......@@ -145,20 +158,23 @@ public class IsoSpline {
}
}
if(foundEquals){
if(foundEquals){ // Link chain
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;
Coordinate currentCoord = ise0.get(m);
String currentDirection = currentCoord.direction;
for(int i = 1; i<ises.size(); i++){
String oppositeDirectionToCurrentCoordDirection = Directions.getOppositeDirection(currentDirection);
currentDirection = ises.get(i).getOppositeDirection(oppositeDirectionToCurrentCoordDirection); // Get the opposite
Coordinate coord = ises.get(i).getCoordWithDirection(currentDirection);
if(coord != null)
polygon.add(coord);
currentCoord = coord;
}
}
else{
//System.out.println("There is a break in the polygon chain...");
System.out.println("There is a break in the polygon chain...");
}
}
else{ // only two coordinates
......@@ -166,21 +182,12 @@ public class IsoSpline {
polygon.add(ise0.get(i));
}
}
if(polygon.closed)
polygon.add(polygon.get(0));
//System.out.println(polygon);
return polygon;
}
private String step(IsoSplineContainer current, String direction, IsoSplineElements polygon, int sortIndex){
IsoSplineElement ise;
ise = current.getIsoSplineElementWithDirection(direction);
if(ise==null)
return Directions.NO_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(ise);
return nextDirection;
}
}
......@@ -18,7 +18,7 @@ public class IsoSplineContainer {
public final Coordinate ref;
private final Directions directions = new Directions();
public int currentIsoSplineElementIndex = 0;
public double isoLevel = 0;
public IsoSplineContainer(IsoSplineContainers iscs, Coordinate coord0, Coordinate coord1, Coordinate coord2, Coordinate coord3){
this.iscs=iscs;
coords.add(coord0);
......@@ -33,7 +33,9 @@ public class IsoSplineContainer {
///////////////////////////////////////////////////////////////////////////////
public boolean equals(IsoSplineContainer isc){
return coords.equals(isc.coords);
if(ref.r==isc.ref.r && ref.c==isc.ref.c)
return true;
return false;
}
......@@ -70,56 +72,56 @@ public class IsoSplineContainer {
if(direction.equals(Directions.SOUTH)){
if(ref.r > 0)
if(hasIsoSpline)
return this.hasIsoSplineElementWithDirection(Directions.NORTH);
return getIsoSplineContainerInDirection(direction).hasIsoSplineElementWithDirection(Directions.getOppositeDirection(Directions.SOUTH));
else
return true;
}
else if(direction.equals(Directions.EAST)){
if(ref.c+1 < iscs.cols)
if(hasIsoSpline)
return this.hasIsoSplineElementWithDirection(Directions.WEST);
return getIsoSplineContainerInDirection(direction).hasIsoSplineElementWithDirection(Directions.getOppositeDirection(Directions.EAST));
else
return true;
}
else if(direction.equals(Directions.NORTH)){
if(ref.r+1 < iscs.rows)
if(hasIsoSpline)
return this.hasIsoSplineElementWithDirection(Directions.SOUTH);
return getIsoSplineContainerInDirection(direction).hasIsoSplineElementWithDirection(Directions.getOppositeDirection(Directions.NORTH));
else
return true;
}
else if(direction.equals(Directions.WEST)){
if(ref.c > 0)
if(hasIsoSpline)
return this.hasIsoSplineElementWithDirection(Directions.EAST);
return getIsoSplineContainerInDirection(direction).hasIsoSplineElementWithDirection(Directions.getOppositeDirection(Directions.WEST));
else
return true;
}
else if(direction.equals(Directions.NORTH_WEST)){
if(ref.r+1<iscs.rows && ref.c > 0)
if(hasIsoSpline)
return this.hasIsoSplineElementWithDirection(Directions.SOUTH_EAST);
return getIsoSplineContainerInDirection(direction).hasIsoSplineElementWithDirection(Directions.SOUTH_EAST);
else
return true;
}
else if(direction.equals(Directions.NORTH_EAST)){
if(ref.r+1 < iscs.rows && ref.c+1 < iscs.cols)
if(hasIsoSpline)
return this.hasIsoSplineElementWithDirection(Directions.SOUTH_WEST);
return getIsoSplineContainerInDirection(direction).hasIsoSplineElementWithDirection(Directions.SOUTH_WEST);
else
return true;
}
else if(direction.equals(Directions.SOUTH_EAST)){
if(ref.r+1<iscs.rows && ref.c+1 < iscs.cols)
if(hasIsoSpline)
return this.hasIsoSplineElementWithDirection(Directions.NORTH_WEST);
return getIsoSplineContainerInDirection(direction).hasIsoSplineElementWithDirection(Directions.NORTH_WEST);
else
return true;
}
else if(direction.equals(Directions.SOUTH_WEST)){
if(ref.r+1<iscs.rows && ref.c > 0)
if(hasIsoSpline)
return this.hasIsoSplineElementWithDirection(Directions.NORTH_EAST);
return getIsoSplineContainerInDirection(direction).hasIsoSplineElementWithDirection(Directions.NORTH_EAST);
else
return true;
}
......@@ -132,6 +134,7 @@ public class IsoSplineContainer {
///////////////////////////////////////////////////////////////////////////////
public boolean canCreateIsoSplineElement(double isoLevel){
this.isoLevel=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);
......@@ -150,6 +153,7 @@ public class IsoSplineContainer {
public void createSplineElement(double isoLevel){
this.isoLevel=isoLevel;
ArrayList<Coordinate> splineCoords = new ArrayList();
//System.out.println(ref);
int j;
......@@ -308,12 +312,14 @@ public class IsoSplineContainer {
public IsoSplineElement getIsoSplineElementWithDirection(String direction){
IsoSplineElement ise;
if(ises!=null)
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)){
ise = ises.get(i);
ises.remove(i);
return ises.get(i);
return ise;
}
return null;
}
......@@ -344,6 +350,13 @@ public class IsoSplineContainer {
sb.append(coords.get(i).toString());
sb.append("\r\n");
}
sb.append("IsoSplineElements at isolevel=");
sb.append(isoLevel);
sb.append("\r\n");
for (int i = 0; i < ises.size(); i++){
sb.append(ises.get(i).toString());
sb.append("\r\n");
}
sb.append("\r\n");
return sb.toString();
......
......@@ -40,6 +40,15 @@ public class IsoSplineElement extends ArrayList<Coordinate> implements Comparab
}
public Coordinate getCoordWithDirection(String direction){
for(int i =0; i<size(); i++)
if(get(i).direction.equals(direction))
return get(i);
return null;
}
@Override
public int compareTo(IsoSplineElement ise) {
if(ise.sortIndex>sortIndex)
......@@ -51,11 +60,16 @@ public class IsoSplineElement extends ArrayList<Coordinate> implements Comparab
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("IsoSplineElement: (1) ");
sb.append(get(0).toString());
sb.append(" -+- (2) ");
sb.append(get(1).toString());
for(int i = 0; i<size();i++){
sb.append("[");
sb.append(get(i).lon);
sb.append(", ");
sb.append(get(i).lat);
sb.append("] => ");
sb.append(get(i).direction);
if(i==0)
sb.append(" -- ");
}
return sb.toString();
}
}
......@@ -75,21 +75,12 @@ public class IsoSplineElements extends ArrayList<IsoSplineElement> {
sb.append(" closed: ");
sb.append(closed);
sb.append("\r\n");
sb.append("i: ");
sb.append(0);
sb.append(", [");
sb.append(get(0).get(0).lon);
sb.append(", ");
sb.append(get(0).get(0).lat);
sb.append("]\r\n");
for(int i =0;i<size();i++){
sb.append("i: ");
sb.append("i=");
sb.append(i);
sb.append(", [");
sb.append(get(i).get(i).lon);
sb.append(", ");
sb.append(get(i).get(i).lat);
sb.append("]\r\n");
sb.append(": ");
sb.append(get(i).toString());
sb.append("\r\n");
}
return sb.toString();
}
......
......@@ -7,9 +7,7 @@ package isospline;
import isospline.ui.GUI;
import isospline.io.JSONMatrix;
import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
......@@ -20,13 +18,13 @@ import org.json.JSONException;
*/
public class Main {
public static final int ISO_LEVELS =1;
public static final boolean test = false;
public static int minPolygonSize = 0;
public static boolean filtering = true;
public static boolean original = false;
public static final boolean TEST = false;
public static int minPolygonSize = 150;
public static boolean filteredValues = false;
public static boolean originalValues = true;
public static int filterWidth = 2;
public static int filterIter = 3;
public static double filterRange = 1.0;
public static double<