Commit c4d09f7f authored by a001188's avatar a001188
Browse files

Initial commit

parent c55941c2
......@@ -11,7 +11,7 @@ import java.awt.Dimension;
*
* @author a001188
*/
public class MatrixCoordinate implements Cloneable{
public class Coordinate implements Cloneable{
public static double OFFSET_X = 0.1;
public static double OFFSET_Y = 0.1;
public static double WIDTH = 0.8;
......@@ -25,7 +25,7 @@ public class MatrixCoordinate implements Cloneable{
public final int r;
public final int c;
public MatrixCoordinate(double lon, double lat, int r, int c, double value){
public Coordinate(double lon, double lat, int r, int c, double value){
this.lat = lat;
this.lon = lon;
this.value = value;
......@@ -37,8 +37,8 @@ public class MatrixCoordinate implements Cloneable{
@Override
public MatrixCoordinate clone(){
return new MatrixCoordinate(lon, lat, r, c, value);
public Coordinate clone(){
return new Coordinate(lon, lat, r, c, value);
}
public void fitToFrame(Dimension dimension, MatrixCorners gridCorners, String type){
......@@ -60,7 +60,7 @@ public class MatrixCoordinate implements Cloneable{
}
public boolean equalValue(MatrixCoordinate coord){
public boolean equalValue(Coordinate coord){
return Math.abs(value - coord.value) <= MAX_DIFF;
}
......@@ -96,8 +96,9 @@ public class MatrixCoordinate implements Cloneable{
return sb.toString();
}
public boolean equals(MatrixCoordinate coord){
if(this.r == coord.r && this.c == coord.c)
public boolean equals(Coordinate coord){
double diff = 0.0000001;
if(Math.abs(lon - coord.lon)<diff && Math.abs(lat - coord.lat)<diff)
return true;
return false;
}
......
......@@ -18,24 +18,24 @@ public class Filter {
}
public void filtering(int filterWidth, int filtrations){
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));
matrix.setValue(r, c, getFilteredValue(r, c, filterWidth, range));
}
}
}
private double getFilteredValue(int row, int col, int width){
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 sum/n;
return (1-range)*matrix.getValue(row,col) + range*sum/n;
}
}
\ No newline at end of file
......@@ -19,7 +19,7 @@ public interface IsoApi {
public void setValue(int r, int c, double value);
public int getRows();
public int getCols();
public MatrixCoordinate getCoordinate(int r, int c);
public Coordinate getCoordinate(int r, int c);
public void fitToFrame(Dimension dimensionm, String type);
public MatrixCorners getGridCorners();
public IsoApi clone();
......
......@@ -5,7 +5,6 @@
*/
package isospline;
import isospline.ui.GUI;
import java.util.ArrayList;
import java.awt.Color;
......@@ -30,12 +29,13 @@ public class IsoSpline {
}
public ArrayList<Double> getIsoLevels(int amount){
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);
//System.out.println("Matrix min: " + min + ", Matrix max: " + max + " Matrix avg: " + avg);
//System.out.println("isoLevels (" + isoLevels.size() + "): " + isoLevels);
return isoLevels;
}
......@@ -46,62 +46,105 @@ public class IsoSpline {
if(iscs.getIsoSplineContainer(r,c).canCreateIsoSplineElement(isoLevel))
iscs.getIsoSplineContainer(r, c).createSplineElement(isoLevel);
}
// Polygon polygon = new Polygon(name, color, matrix.getCoordinate(0, 0));
// for(IsoSplineContainer c : iscs) {
// if(c.hasIsoSplineElement())
// polygon.add(c.getIsoSplineElement(false));
// }
// return polygons;
return createPolygons(isoLevel, color);
}
private Polygons createPolygons(double isoLevel, Color color){
Polygons polygons = new Polygons();
String direction;
Polygon polygon;
IsoSplineElements ises;
IsoSplineContainer start;
IsoSplineContainer finish;
IsoSplineContainer current;
IsoSplineContainer next;
for(int r=0;r<iscs.rows;r++)
for(int c=0;c<iscs.cols;c++){
start = iscs.getIsoSplineContainer(r, c);
Polygon polygon = new Polygon();
polygon.color=color;
if(start.hasNotUsedIsoSplineElement()){
polygon = new Polygon("IsoLevel: " + isoLevel, color, start.ref);
ises = new IsoSplineElements("IsoLevel: " + isoLevel, color, start.ref);
int sortIndex=0;
polygon.add(start.getIsoSplineElement(true, sortIndex));
System.out.println("new polygon " + isoLevel);
ises.add(start.getIsoSplineElement(true, sortIndex));
//System.out.println("new polygon " + isoLevel);
direction = start.getPrimaryDirection();
current=start;
current.nextDirection = direction;
System.out.println("rc: > (" + current.ref.r + ", "+ current.ref.c + ") direction " + direction + " (next direction " + current.nextDirection + ")");
//System.out.println("rc: > (" + current.ref.r + ", "+ current.ref.c + ") direction " + direction + " (next direction " + current.nextDirection + ")");
while(current.hasNotUsedNeighbourInDirection()){
current = step(current, polygon, sortIndex);
current = step(current, ises, sortIndex);
sortIndex++;
}
finish=current;
if(!current.equals(start)){
current=start;
direction = current.getSecondaryDirection();
current.nextDirection = direction;
System.out.println("rc: < (" + current.ref.r + ", "+ current.ref.c + ") direction " + direction + " (next direction " + current.nextDirection + ")");
//System.out.println("rc: < (" + current.ref.r + ", "+ current.ref.c + ") direction " + direction + " (next direction " + current.nextDirection + ")");
sortIndex=-1;
while(current.hasNotUsedNeighbourInDirection()){
current = step(current, polygon, sortIndex);
current = step(current, ises, sortIndex);
sortIndex--;
}
if(!current.equals(finish))
polygon.closed = true;
}
else
polygon.setClosed(true);
polygon.closed = true;
if(polygon.size()>0)
polygon.sort();
polygons.add(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;
}
}
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).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));
}
}
polygons.add(polygon);
System.out.println(polygon.toString());
}
}
return polygons;
}
private IsoSplineContainer step(IsoSplineContainer current, Polygon polygon, int sortIndex){
private IsoSplineContainer step(IsoSplineContainer current, IsoSplineElements polygon, int sortIndex){
String searchdirection = ">";
if(sortIndex<0)
searchdirection = "<";
......@@ -110,7 +153,7 @@ public class IsoSpline {
IsoSplineContainer next;
next = current.getNext();
System.out.println("rc: " + searchdirection + " (" + next.ref.r + ", "+ next.ref.c + ") direction " + current.nextDirection + " (next direction " + next.nextDirection + ")");
//System.out.println("rc: " + searchdirection + " (" + next.ref.r + ", "+ next.ref.c + ") direction " + current.nextDirection + " (next direction " + next.nextDirection + ")");
polygon.add(next.getIsoSplineElement(true, sortIndex));
oppositeDirection = current.getOppositeDirection(current.nextDirection);
next.setNextDirection(oppositeDirection);
......
......@@ -18,14 +18,14 @@ public class IsoSplineContainer {
public static String WEST = "West";
public static String NO_DIRECTION = "No direction";
private final IsoSplineContainers iscs;
public final ArrayList<MatrixCoordinate> coords = new ArrayList();
public final ArrayList<Coordinate> coords = new ArrayList();
public boolean used = false;
private IsoSplineElement ise = null;
public final MatrixCoordinate ref;
public final Coordinate ref;
private final ArrayList<String> directions = new ArrayList();
private final ArrayList<String> compass = new ArrayList();
public String nextDirection = NO_DIRECTION;
public IsoSplineContainer(IsoSplineContainers iscs, MatrixCoordinate coord0, MatrixCoordinate coord1, MatrixCoordinate coord2, MatrixCoordinate coord3){
public IsoSplineContainer(IsoSplineContainers iscs, Coordinate coord0, Coordinate coord1, Coordinate coord2, Coordinate coord3){
this.iscs=iscs;
coords.add(coord0);
coords.add(coord1);
......@@ -101,8 +101,7 @@ public class IsoSplineContainer {
public void createSplineElement(double isoLevel){
ise=null;
ArrayList<MatrixCoordinate> splineCoords = new ArrayList();
ArrayList<Coordinate> splineCoords = new ArrayList();
ise = null;
used = false;
//System.out.println(ref);
......@@ -113,14 +112,14 @@ public class IsoSplineContainer {
else
j=i+1;
MatrixCoordinate coord1 = coords.get(i);
MatrixCoordinate coord2 = coords.get(j);
Coordinate coord1 = coords.get(i);
Coordinate coord2 = coords.get(j);
if(Math.max(coord1.value, coord2.value)>=isoLevel)
if(Math.min(coord1.value, coord2.value)<=isoLevel){
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 MatrixCoordinate(lon, lat, coord1.r, coord1.c, isoLevel));
splineCoords.add(new Coordinate(lon, lat, coord1.r, coord1.c, isoLevel));
directions.add(compass.get(i));
}
}
......
......@@ -6,43 +6,40 @@
package isospline;
import java.awt.Dimension;
import java.util.ArrayList;
/**
*
* @author peter
*/
public class IsoSplineElement implements Comparable<IsoSplineElement> {
public final MatrixCoordinate coord1;
public final MatrixCoordinate coord2;
public class IsoSplineElement extends ArrayList<Coordinate> implements Comparable<IsoSplineElement> {
public int sortIndex;
// public final String dírection1;
// public final String dírection2;
public IsoSplineElement(MatrixCoordinate coord1, MatrixCoordinate coors2){
// public IsoSplineElement(MatrixCoordinate coord1, MatrixCoordinate coors2, String direction1, String direction2){
this.coord1=coord1;
this.coord2=coors2;
// this.dírection1=direction1;
// this.dírection2=direction2;
public IsoSplineElement(Coordinate coord1, Coordinate coors2){
add(coord1);
add(coors2);
}
public void fitToFrame(Dimension dimension, MatrixCorners gridCorners, String type){
coord1.fitToFrame(dimension, gridCorners, type);
coord2.fitToFrame(dimension, gridCorners, type);
for(int i = 0; i<size();i++)
get(i).fitToFrame(dimension, gridCorners, type);
}
public boolean hasEqual(MatrixCoordinate coord){
if(coord.equals(coord1) || coord.equals(coord2))
public boolean hasEqual(Coordinate coord){
if(coord.equals(get(0)) || coord.equals(get(1)))
return true;
return false;
}
public MatrixCoordinate getOpposite(MatrixCoordinate coord){
if(coord.equals(coord1))
return coord2;
else
return coord1;
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 int compareTo(IsoSplineElement ise) {
......@@ -55,35 +52,10 @@ public class IsoSplineElement implements Comparable<IsoSplineElement> {
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("Coord 1 (");
sb.append(coord1.x);
sb.append(", ");
sb.append(coord1.y);
sb.append(", ");
sb.append(coord1.value);
sb.append(") - Coord 2 (");
sb.append(coord2.x);
sb.append(", ");
sb.append(coord2.y);
sb.append(", ");
sb.append(coord2.value);
sb.append(") - > Point 1 (");
sb.append(coord1.lat);
sb.append(", ");
sb.append(coord2.lon);
sb.append(") - Point 2 (");
sb.append(coord2.lat);
sb.append(", ");
sb.append(coord2.lon);
sb.append(") - > (x1,y1) (");
sb.append(coord1.x);
sb.append(", ");
sb.append(coord1.y);
sb.append(") - > (x2,y2) (");
sb.append(coord2.x);
sb.append(", ");
sb.append(coord2.y);
sb.append(")");
sb.append("IsoSplineElement: (1) ");
sb.append(get(0).toString());
sb.append(" -+- (2) ");
sb.append(get(1).toString());
return sb.toString();
}
......
......@@ -5,12 +5,85 @@
*/
package isospline;
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/**
*
* @author a001188
*/
public class IsoSplineElements extends ArrayList<IsoSplineContainer>{
public class IsoSplineElements extends ArrayList<IsoSplineElement> {
public String name = "Not set";
private boolean closed = false;
private Color color;
private final Coordinate reference;
public IsoSplineElements(String name, Color color, Coordinate reference){
if(name!=null)
this.name = name;
this.color = color;
this.reference = reference;
}
public void sort(){
Collections.sort(this, new Comparator<IsoSplineElement>() {
@Override
public int compare(IsoSplineElement ise1, IsoSplineElement ise2){
return ise1.compareTo(ise2);
}
});
}
public Color getColor(){
return color;
}
public boolean isClosed(){
return closed;
}
public void setClosed(boolean closed){
this.closed = closed;
}
public void getColor(Color color){
this.color=color;
}
public void fitToFrame(Dimension dimension, MatrixCorners gridCorners, String type) {
for (IsoSplineElement ise : this) {
ise.fitToFrame(dimension, gridCorners, type);
}
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append(name);
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(", [");
sb.append(get(i).get(i).lon);
sb.append(", ");
sb.append(get(i).get(i).lat);
sb.append("]\r\n");
}
return sb.toString();
}
}
......@@ -22,6 +22,13 @@ public class Main {
public static final int ISO_LEVELS =3;
public static final boolean test = false;
public static int minPolygonSize = 0;
public static boolean filtering = true;
public static boolean original = false;
public static int filterWidth = 2;
public static int filterIter = 3;
public static double filterRange = 0.8;
/**
* @param args the command line arguments
*/
......@@ -37,19 +44,38 @@ public class Main {
colors.add(Color.black);
Matrix coords = JSONMatrix.read(test);
System.out.println("Coords size: " + coords.size());
//System.out.println("Coords size: " + coords.size());
IsoSpline iso = new IsoSpline(coords);
ArrayList<Double> isoLevels = iso.getIsoLevels(ISO_LEVELS);
ArrayList<Double> isoLevels = iso.getIsoLevels();
GUI gui = new GUI(coords, Matrix.LONLAT, minPolygonSize);
gui.setSize(1000,1000);
gui.setSize(1500,1500);
gui.setVisible(true);
for(int i = 0; i<isoLevels.size(); i++){
IsoApi clone = coords.clone();
iso = new IsoSpline(clone);
Filter filter = new Filter(clone);
filter.filtering(20, 100);
Polygons polygons = iso.createIsoPolygon(clone, "test" + i + "isoLevel: " + isoLevels.get(i), colors.get(i), isoLevels.get(i));
gui.addPolygons(polygons);
int f0=0;
int f1=0;
if(filtering){
f0=1;
f1=2;
}
if(original){
f0=0;
f1=1;
}
if(original && filtering){
f0=0;
f1=2;
}
for (int f = f0; f0<f1; f0++){
for(int i = 0; i<isoLevels.size(); i++){
IsoApi clone = coords.clone();
iso = new IsoSpline(clone);
if(f==1){
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));
gui.addPolygons(polygons);
}
}
gui.repaint();
......
......@@ -12,7 +12,7 @@ import java.util.ArrayList;
*
* @author a001188
*/
public class Matrix extends ArrayList<MatrixCoordinate> implements IsoApi, Cloneable{
public class Matrix extends ArrayList<Coordinate> implements IsoApi, Cloneable{
public static final String LONLAT = "lonlat";
public static final String RC = "RC";
private final int rows;
......@@ -58,7 +58,7 @@ public class Matrix extends ArrayList<MatrixCoordinate> implements IsoApi, Clone
}
@Override
public MatrixCoordinate getCoordinate(int row, int col){
public Coordinate getCoordinate(int row, int col){
return get(getIndexFromCoord(row, col));
}
......@@ -74,7 +74,7 @@ public class Matrix extends ArrayList<MatrixCoordinate> implements IsoApi, Clone
}
@Override
public boolean add(MatrixCoordinate coord){
public boolean add(Coordinate coord){
boolean ok;
ok = super.add(coord);
......
......@@ -12,9 +12,9 @@ import java.util.ArrayList;
*
* @author a001188
*/
public class MatrixCorners extends ArrayList<MatrixCoordinate>{
public class MatrixCorners extends ArrayList<Coordinate>{
public MatrixCorners(MatrixCoordinate R0C0, MatrixCoordinate R0Cn, MatrixCoordinate RmC0, MatrixCoordinate RmCn){
public MatrixCorners(Coordinate R0C0, Coordinate R0Cn, Coordinate RmC0, Coordinate RmCn){
add(R0C0);
add(R0Cn);
add(RmC0);
......
......@@ -5,173 +5,38 @@
*/
package isospline;
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.awt.Color;
/**
*