Commit 7aa1404f authored by a001188's avatar a001188
Browse files

Initial commit

parent ff8fa1d8
/*
* 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 Colors extends ArrayList<Color>{
public Colors(){
add(Color.RED);
add(Color.BLUE);
add(Color.BLACK);
add(Color.GRAY);
}
@Override
public Color get(int i){
if(i>=size())
return super.get(size()-1);
return super.get(i);
}
}
/*
* 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;
/**
*
* @author a001188
*/
public class CoordinatePair {
public final Coordinate coord1;
public final Coordinate coord2;
public final String dírection1;
public final String dírection2;
public int polygonOrder=0;
public CoordinatePair(Coordinate coord1, Coordinate coord2, String direction1, String direction2){
this.coord1 = coord1;
this.coord2 = coord2;
this.dírection1=direction1;
this.dírection2=direction2;
}
public boolean hasEqual(Coordinate coord){
if(coord.equals(coord1) || coord.equals(coord2))
return true;
return false;
}
public Coordinate getOpposite(Coordinate coord){
if(coord.equals(coord1))
return coord2;
else
return coord1;
}
/*
public boolean hasNext(Coordinate coord){
if(coord.direction.equals(Directions.EAST))
if(coord.c+1 == coord1.c)
return true;
if(coord.direction.equals(Directions.WEST))
if(coord.c-1 == coord1.c)
return true;
if(coord.direction.equals(Directions.NORTH))
if(coord.r+1 == coord1.r)
return true;
if(coord.direction.equals(Directions.SOUTH))
if(coord.r-1 == coord1.r)
return true;
return false;
}
public Coordinate next(Coordinate coord){
if(coord.direction.equals(Directions.EAST))
if(coord.c+1 == coord1.c)
return coord2;
else
return coord1;
if(coord.direction.equals(Directions.WEST))
if(coord.c-1 == coord1.c)
return coord2;
else
return coord1;
if(coord.direction.equals(Directions.NORTH))
if(coord.r+1 == coord1.r)
return coord2;
else
return coord1;
if(coord.direction.equals(Directions.SOUTH))
if(coord.r-1 == coord1.r)
return coord2;
else
return coord1;
return null;
}
*/
}
/*
* 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;
/**
*
* @author a001188
*/
public class Filter {
private final IsoApi matrix;
public Filter(IsoApi matrix){
this.matrix=matrix;
}
public void filtering(int filterWidth, int filtrations){
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));
}
}
}
private double getFilteredValue(int row, int col, int width){
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;
}
}
\ No newline at end of file
/*
* 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.Dimension;
/**
*
* @author a001188
*/
public interface IsoApi {
public double getValue(int r, int c);
public double min();
public double max();
public double average();
public void setValue(int r, int c, double value);
public int getRows();
public int getCols();
public MatrixCoordinate getCoordinate(int r, int c);
public void fitToFrame(Dimension dimensionm, String type);
public MatrixCorners getGridCorners();
public IsoApi clone();
}
......@@ -5,6 +5,7 @@
*/
package isospline;
import isospline.ui.GUI;
import java.util.ArrayList;
import java.awt.Color;
......@@ -13,69 +14,48 @@ import java.awt.Color;
* @author a001188
*/
public class IsoSpline {
private final Matrix coords;
private final ArrayList<Double> isoLevels = new ArrayList();
private final GUI gui;
private final IsoSplineContainers isc;
private final IsoApi matrix;
private final double min;
private final double max;
private final double avg;
public IsoSpline(GUI gui, Matrix coords){
this.coords=coords;
this.gui=gui;
this.avg = coords.average();
this.min = coords.getMin();
this.max = coords.getMax();
public IsoSpline(IsoApi matrix){
this.matrix=matrix;
this.avg = matrix.average();
this.min = matrix.min();
this.max = matrix.max();
isc = new IsoSplineContainers(matrix.getRows()-1, matrix.getCols()-1);
isc.createIsoSplineContainers(matrix);
}
public ArrayList<Double> getIsoLevels(int amount){
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 void createIso(Matrix coords, String name, Color color){
Polygon coordinatPairs = null;
Polygons coordinatePairsList = new Polygons();
for(int n=0; n<isoLevels.size(); n++){
coordinatPairs = new Polygon(isoLevels.get(n), name, color, coords.get(0));
LinearSplineElements lse = new LinearSplineElements(coords, isoLevels.get(n), null);
for(int i = 0; i<lse.size(); i++){
Coordinate coord1 = lse.get(i).coord1;
Coordinate coord2 = lse.get(i).coord2;
String direction1 = lse.get(i).coord1.direction;
String direction2 = lse.get(i).coord2.direction;
coordinatPairs.add(new CoordinatePair(coord1,coord2, direction1, direction2));
public Polygon createIso(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(isc.getIsoSplineContainer(r,c).canCreateIsoSplineElement(isoLevel))
isc.getIsoSplineContainer(r, c).createSplineElement(isoLevel);
}
if(coordinatPairs.size()>0)
coordinatePairsList.add(coordinatPairs);
Polygon polygon = new Polygon(isoLevel, name, color, matrix.getCoordinate(0, 0));
for (IsoSplineContainer c : isc) {
if(c.hasIsoSplineElement())
polygon.add(c.getIsoSplineElement(false));
}
Polygons polygons = new Polygons();
if(coordinatPairs!=null)
for(int i = 0; i<coordinatePairsList.size();i++)
polygons = coordinatPairs.getSeparatePolygons(name, color);
for(int j=0; j<polygons.size();j++)
gui.addPolygon(polygons.get(j));
gui.repaint();
return polygon;
}
public void filtering(){
int filterWidth = 2;
int filtrations = 10;
Matrix tmp;
for(int i = 0; i<filtrations; i++){
tmp = (Matrix)coords.clone();
for(int r = filterWidth; r<coords.getRows()-filterWidth-1; r++)
for(int c = filterWidth; c<coords.getCols()-filterWidth-1; c++){
coords.setValue(r, c, tmp.getFilteredValue(r, c, filterWidth));
}
System.out.println("Filtered max: " + coords.getMax());
System.out.println("Filtered min: " + coords.getMin());
System.out.println("Filtered avg: " + coords.average());
}
createIso(coords, "Filtered", Color.RED);
}
}
/*
* 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 a001188
*/
public class IsoSplineContainer {
public final ArrayList<MatrixCoordinate> coords = new ArrayList();
public boolean includedInPolygon = false;
private IsoSplineElement ise = null;
private final MatrixCoordinate ref;
public IsoSplineContainer(MatrixCoordinate coord0, MatrixCoordinate coord1, MatrixCoordinate coord2, MatrixCoordinate coord3){
coords.add(coord0);
coords.add(coord1);
coords.add(coord2);
coords.add(coord3);
this.ref=coord0;
}
public IsoSplineElements getIsoSplineElements(){
return new IsoSplineElements();
}
public void createSplineElement(double isoLevel){
ise=null;
ArrayList<MatrixCoordinate> splineCoords = new ArrayList();
ise = null;
includedInPolygon = false;
//System.out.println(ref);
if(!canCreateIsoSplineElement(isoLevel))
return;
int j=0;
for(int i = 0; i<coords.size(); i++){
if(i==coords.size()-1)
j=0;
else
j=i+1;
MatrixCoordinate coord1 = coords.get(i);
MatrixCoordinate 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));
}
}
if(splineCoords.size()==2){
ise = new IsoSplineElement(splineCoords.get(0),splineCoords.get(1));
// System.out.println("Iso points: " + splineCoords.toString());
}
else
System.out.println("Wrong number of iso points: " + splineCoords.toString());
}
public IsoSplineElement getIsoSplineElement(boolean includedInPolygon){
this.includedInPolygon=includedInPolygon;
return ise;
}
public boolean hasIsoSplineElement(){
return ise!=null;
}
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) < 0.00001)
return false;
return true;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("Container (");
sb.append(coords.get(0).r);
sb.append(", ");
sb.append(coords.get(0).c);
sb.append("): ");
for (int i = 0; i < coords.size(); i++)
sb.append(coords.get(i).toString());
sb.append("\t\n");
return sb.toString();
}
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
public double getZ1(){
return coord1.value;
}
public double getZ2(){
return coord2.value;
}
public double getZprim(){
float zPrim =(float)((getZ2()-getZ1())/Math.pow(Math.pow((coord1.lat-coord2.lat),2)+Math.pow((coord2.lon-coord2.lon),2), 0.5));
return zPrim;
}
*/
/*
* 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 a001188
*/
public class IsoSplineContainers extends ArrayList<IsoSplineContainer>{
public final int rows;
public final int cols;
public IsoSplineContainers(int rows, int cols){
this.rows=rows;
this.cols=cols;
}
public void createIsoSplineContainers(IsoApi matrix){
for(int r = 0; r < matrix.getRows()-1; r++){
for(int c = 0; c < matrix.getCols()-1; c++)
add(new IsoSplineContainer(matrix.getCoordinate(r,c), matrix.getCoordinate(r,c+1), matrix.getCoordinate(r+1,c+1), matrix.getCoordinate(r+1,c)));
}
}
public IsoSplineContainer getIsoSplineContainer(int r, int c){
return get(getIndexFromCoord(r, c));
}
private int getIndexFromCoord(int row, int col){
int index;
index = row * cols + col ;
return index;
}
}
......@@ -5,38 +5,38 @@
*/
package isospline;
import java.awt.Dimension;
/**
*
* @author peter
*/
public class LinearSplineElement {
public final Coordinate coord1;
public final Coordinate coord2;
public final String dírection1;
public final String dírection2;
public class IsoSplineElement {
public final MatrixCoordinate coord1;
public final MatrixCoordinate coord2;
// public final String dírection1;
// public final String dírection2;
public LinearSplineElement(Coordinate coord1, Coordinate coors2, String direction1, String direction2){
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;
// this.dírection1=direction1;
// this.dírection2=direction2;
}
public int compareTo(LinearSplineElement lse) {
if(lse.getZprim()<getZprim())
return 1;
else
return -1;
public void fitToFrame(Dimension dimension, MatrixCorners gridCorners, String type){
coord1.fitToFrame(dimension, gridCorners, type);
coord2.fitToFrame(dimension, gridCorners, type);
}
public boolean hasEqual(Coordinate coord){
public boolean hasEqual(MatrixCoordinate coord){
if(coord.equals(coord1) || coord.equals(coord2))
return true;
return false;
}
public Coordinate getOpposite(Coordinate coord){
public MatrixCoordinate getOpposite(MatrixCoordinate coord){
if(coord.equals(coord1))
return coord2;
else
......@@ -44,27 +44,17 @@ public class LinearSplineElement {
}
public double getZ1(){
return coord1.value;
}
public double getZ2(){
return coord2.value;
}
public double getZprim(){
float zPrim =(float)((getZ2()-getZ1())/Math.pow(Math.pow((coord1.lat-coord2.lat),2)+Math.pow((coord2.lon-coord2.lon),2), 0.5));
return zPrim;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("Intersept 1 (");
sb.append("Coord 1 (");
sb.append(coord1.x);
sb.append(", ");
sb.append(coord1.y);
sb.append(", ");
sb.append(coord1.value);
sb.append(") - Intersept 2 (");
sb.append(") - Coord 2 (");
sb.append(coord2.x);
sb.append(", ");
sb.append(coord2.y);
......@@ -90,4 +80,13 @@ public class LinearSplineElement {
return sb.toString();
}
public int compareTo(IsoSplineElement ise) {
// if(ise.getZprim()<getZprim())
// return 1;
// else
return -1;
}
}
......@@ -11,6 +11,6 @@ import java.util.ArrayList;
*
* @author a001188
*/
public class IsoLinearSplineElementList extends ArrayList{
public class IsoSplineElements extends ArrayList<IsoSplineContainer>{
}
......@@ -5,7 +5,11 @@
*/
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;
......@@ -15,25 +19,33 @@ import org.json.JSONException;
* @author a001188
*/
public class Main {
public static final int ISO_LEVELS = 2;
public static final int ISO_LEVELS = 3;