Commit faa69e5e authored by Peter Lundin's avatar Peter Lundin
Browse files

Initial commit

parents
/*
* 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 Coordinate{
public final String direction;
public final static double MAX_DIFF = 0.000000001;
public final double lat;
public final double lon;
public double value;
public int x;
public int y;
public final int r;
public final int c;
public Coordinate(String direction, double lat, double lon, int r, int c, double value){
this.direction = direction;
this.lat = lat;
this.lon = lon;
this.value = value;
this.x = (int)Math.round(lat);
this.y = (int)Math.round(lon);
this.r=r;
this.c=c;
}
public void fitToFrame(int offsetX, int offsetY, double scaleX, double scaleY){
this.x = (int)Math.round(lat * scaleX + offsetX);
this.y = (int)Math.round(lon * scaleY + offsetY);
}
public boolean equalValue(Coordinate coord){
if(Math.abs(value - coord.value)>MAX_DIFF)
return false;
return true;
}
public void setValue(double value){
this.value = value;
}
@Override
public String toString(){
return "[" + lat + ", " + lon + "] => " + value;
}
}
/*
* 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 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 hasNext(Coordinate currentCoordinate){
return true;
}
}
/*
* 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;
import java.util.ArrayList;
/**
*
* @author a001188
*/
public class Coordinates extends ArrayList<Coordinate>{
private int rows = 1;
private int cols = 0;
private Coordinate maxCoordinate;
private Coordinate minCoordinate;
public void fitToFrame(int offsetX, int offsetY, double scaleX, double scaleY){
this.forEach((coord) -> {
coord.fitToFrame(offsetX, offsetY, scaleX, scaleY);
});
}
public Dimension getDimension(){
return new Dimension(rows, cols);
}
public int getRows(){
return rows;
}
public int getCols(){
return cols;
}
public Coordinate getCoordinate(int row, int col){
return get(getIndexFromCoord(row, col));
}
public double getValue(int row, int col){
return get(getIndexFromCoord(row, col)).value;
}
public 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 + getValue(r,c);
return sum/n;
}
public void setValue(int row, int col, double value){
get(getIndexFromCoord(row, col)).setValue(value);
}
@Override
public boolean add(Coordinate coord){
boolean ok;
ok = super.add(coord);
if(size()>1){
if(get(size()-1).lat < get(size()-2).lat){
if(rows == 1)
cols = size()-1;
rows++;
}
}
return ok;
}
public double getMax(){
double max = getCoordinate(0,0).value;
for(int r = 0; r<getRows() ;r++)
for(int c = 0; c<getCols(); c++)
if(getCoordinate(r,c).value>max){
max=getCoordinate(r,c).value;
maxCoordinate = getCoordinate(r,c);
}
return max;
}
public double getMin(){
double min = getCoordinate(0,0).value;
for(int r = 0; r<getRows() ;r++)
for(int c = 0; c<getCols(); c++)
if(getCoordinate(r,c).value<min){
min=getCoordinate(r,c).value;
minCoordinate = getCoordinate(r,c);
}
return min;
}
public double average(){
double sum = 0;
for(int r = 0; r<getRows() ;r++)
for(int c = 0; c<getCols(); c++)
sum=sum+getCoordinate(r,c).value;
return sum/(getRows()*getCols());
}
public double getMinA(int r, int c){
if(getCoordinate(r,c).value < getCoordinate(r+1,c+1).value)
return getCoordinate(r,c).value;
else
return getCoordinate(r+1,c+1).value;
}
public double getMaxA(int r, int c){
if(getCoordinate(r,c).value > getCoordinate(r+1,c+1).value)
return getCoordinate(r,c).value;
else
return getCoordinate(r+1,c+1).value;
}
public double getMinB(int r, int c){
if(getCoordinate(r+1,c).value < getCoordinate(r,c+1).value)
return getCoordinate(r+1,c).value;
else
return getCoordinate(r,c+1).value;
}
public double getMaxB(int r, int c){
if(getCoordinate(r+1,c).value > getCoordinate(r,c+1).value)
return getCoordinate(r+1,c).value;
else
return getCoordinate(r,c+1).value;
}
public int getMinRow(){
if(minCoordinate==null)
getMin();
return minCoordinate.y;
}
public int getMinCol(){
if(minCoordinate==null)
getMin();
return minCoordinate.x;
}
public int getMaxCol(){
if(minCoordinate==null)
getMax();
return minCoordinate.x;
}
public int getMaxRow(){
if(minCoordinate==null)
getMax();
return minCoordinate.y;
}
public IsoErrorlist getIsoErrorList(){
IsoErrorlist isoErrorlist = new IsoErrorlist();
isoErrorlist.add(new IsoErrorElement(getMinRow(), getMinCol(), getMin(), 0));
isoErrorlist.add(new IsoErrorElement(getMaxRow(), getMaxCol(), getMax(), 0));
for(int r=0; r<rows-1;r++)
for(int c=0; c<cols-1;c++){
if(getMinA(r,c) > getMaxB(r,c)){
isoErrorlist.add(new IsoErrorElement(r, c, getMinA(r, c),-1));
isoErrorlist.add(new IsoErrorElement(r, c, getMaxB(r, c), 1));
}
if(getMaxA(r,c) < getMinB(r,c)){
isoErrorlist.add(new IsoErrorElement(r, c, getMinB(r, c),-1));
isoErrorlist.add(new IsoErrorElement(r, c, getMaxA(r, c), 1));
}
}
return isoErrorlist;
}
private int getIndexFromCoord(int row, int col){
int index;
index = row * getCols() + col;
return index;
}
}
/*
* 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 peter
*/
public class Direction {
public final String name;
public final int r1;
public final int c1;
public final int r2;
public final int c2;
public Direction(String name, int r1, int c1, int r2, int c2){
this.name = name;
this.r1=r1;
this.c1=c1;
this.r2=r2;
this.c2=c2;
}
}
/*
* 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;
import java.util.ArrayList;
/**
*
* @author peter
*/
public class Directions extends ArrayList<Direction>{
public static String NORTH = "North";
public static String EAST = "East";
public static String SOUTH = "South";
public static String WEST = "West";
public Directions(Dimension dimension){
int r=dimension.height;
int c=dimension.width;
add(new Direction(NORTH, r + 0, c + 0, r + 0, c + 1));
add(new Direction(EAST, r + 0, c + 1, r + 1, c + 1));
add(new Direction(SOUTH, r + 1, c + 0, r + 1, c + 1));
add(new Direction(WEST, r + 0, c + 0, r + 1, c + 0));
}
}
/*
* 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.awt.Graphics;
import javax.swing.JFrame;
/**
*
* @author peter
*/
public class GUI extends JFrame{
private final Polygons polygons = new Polygons();
private final Coordinates coords;
int offsetX = 1000;
int offsetY = 3000;//-2000;
double scaleX = -25;
double scaleY = -40;
public GUI(Coordinates coords){
this.coords = coords;
}
public void addPolygon(Polygon polygon){
polygons.add(polygon);
}
public void clearPolygons(){
polygons.clear();
}
@Override
public void paint(Graphics g){
g.clearRect(0,0, this.getSize().width, this.getSize().height);
drawGrid(g, coords);
for(int p = 0; p < polygons.size(); p++)
drawIso(g, polygons.get(p));
}
private void drawGrid(Graphics g, Coordinates coords){
coords.fitToFrame(offsetX, offsetY, scaleX, scaleY);
g.setColor(Color.LIGHT_GRAY);
int s = 15;
for(int r = 0; r<coords.getRows()-s; r=r+s)
for(int c = 0; c<coords.getCols()-s; c=c+s){
int x11 = coords.getCoordinate(r, c).x;
int x12 = coords.getCoordinate(r, c+s).x;
int x21 = coords.getCoordinate(r+s, c).x;
int y11 = coords.getCoordinate(r, c).y;
int y12 = coords.getCoordinate(r, c+s).y;
int y21 = coords.getCoordinate(r+s, c).y;
g.drawLine(x11, y11, x12, y12);
g.drawLine(x11, y11, x21, y21);
}
int cc=coords.getCols()-1; // last row
for(int r = 0; r<coords.getRows()-s; r=r+s){
int x1 = coords.getCoordinate(r, cc).x;
int x2 = coords.getCoordinate(r+s, cc).x;
int y1 = coords.getCoordinate(r, cc).y;
int y2 = coords.getCoordinate(r+s, cc).y;
g.drawLine(x1, y1, x2, y2);
}
int rr=coords.getRows()-1; // last col
for(int c = 0; c<coords.getCols()-s; c=c+s){
int x1 = coords.getCoordinate(rr, c).x;
int x2 = coords.getCoordinate(rr, c+s).x;
int y1 = coords.getCoordinate(rr, c).y;
int y2 = coords.getCoordinate(rr, c+s).y;
g.drawLine(x1, y1, x2, y2);
}
g.setColor(Color.BLUE); //Corner
drawDot(g, coords.getCoordinate(0, 0));
drawDot(g, coords.getCoordinate(coords.getRows()-1, 0));
drawDot(g, coords.getCoordinate(0, coords.getCols()-1));
drawDot(g, coords.getCoordinate(coords.getRows()-1, coords.getCols()-1));
}
private void drawIso(Graphics g, Polygon polygon){
polygon.fitToFrame(offsetX, offsetY, scaleX, scaleY);
g.setColor(polygon.getColor());
for(int i = 0; i<polygon.size(); i++){
Coordinate coord1 = polygon.get(i).coord1;
Coordinate coord2 = polygon.get(i).coord2;
int x1=coord1.x;
int y1=coord1.y;
int x2=coord2.x;
int y2=coord2.y;
g.drawLine(x1, y1, x2, y2);
}
}
private void drawDot(Graphics g, Coordinate coord){
int dotDiameter = 4;
g.drawOval(coord.x - dotDiameter/2, coord.y - dotDiameter/2, dotDiameter, dotDiameter);
}
}
/*
* 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 IsoErrorElement implements Comparable<IsoErrorElement>{
public final int offset;
public final double isoLevel;
public final int r;
public final int c;
public IsoErrorElement(int r, int c, double isoLevel, int offset){
this.isoLevel=isoLevel;
this.offset=offset;
this.r = r;
this.c = c;
}
@Override
public int compareTo(IsoErrorElement isoErrorElement) {
if(isoErrorElement.isoLevel > this.isoLevel)
return -1;
else
return 1;
}
}
/*
* 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;
import java.util.Collections;
import java.util.Comparator;
/**
*
* @author a001188
*/
public class IsoErrorlist extends ArrayList<IsoErrorElement>{
public void sort(){
Collections.sort(this, new Comparator<IsoErrorElement>() {
@Override
public int compare(IsoErrorElement isoErrorElement1, IsoErrorElement isoErrorElement2){
return isoErrorElement1.compareTo(isoErrorElement2);
}
});
}
public int max(){
int level = 0;
int max = 0;
for(int i = 0; i<size(); i++){
level = level + get(i).offset;
if(level>max)
max=level;
}
return max;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
for(int i =0;i < size();i++){
sb.append("Value (");
sb.append(get(i).r);
sb.append(", ");
sb.append(get(i).c);
sb.append(") = ");
sb.append(get(i).isoLevel);
sb.append(" (");
sb.append(get(i).offset);
sb.append(")\r\n");
}
return sb.toString();
}
}
/*
* 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;
import java.awt.Color;
/**
*
* @author a001188
*/
public class IsoSpline {
private final Coordinates coords;
private final ArrayList<Double> isoLevels = new ArrayList();
private final GUI gui;
private final double min;
private final double max;
private final double avg;
public IsoSpline(GUI gui, Coordinates coords){
this.coords=coords;
this.gui=gui;
this.avg = coords.average();
this.min = coords.getMin();
this.max = coords.getMax();
for(int i=0;i<Main.ISO_LEVELS;i++)
isoLevels.add(i * (max-min)/(Main.ISO_LEVELS-1) + min);
}
public void createIso(Coordinates coords, String name, Color color){
Polygon polygon = null;
for(int n=0; n<isoLevels.size(); n++){
polygon = new Polygon(isoLevels.get(n), name, color);
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;
polygon.add(new CoordinatePair(coord1,coord2, direction1, direction2));
}
gui.addPolygon(polygon);
gui.repaint();
}
}
public void filtering(){
int filterWidth = 2;
int filtrations = 10;
Coordinates tmp = (Coordinates)coords.clone();
for(int i = 0; i<filtrations; i++){
tmp = (Coordinates)coords.clone();