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

Refactor - direction.step

parent 5091f4a9
/*
* 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 Coordinates extends ArrayList<Coordinate>{
}
/*
* 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 Direction {
public final int row;
public final int col;
public Direction(int row, int col){
this.row=row;
this.col=col;
}
}
......@@ -17,14 +17,27 @@ public class Directions {
public static final String EAST = "East";
public static final String SOUTH = "South";
public static final String WEST = "West";
public static final String ON_SPOT = "onSpot";
public static final String SOUTH_WEST = SOUTH + "_" + WEST + "_" + ON_SPOT;
public static final String SOUTH_EAST = SOUTH + "_" + EAST + "_" + ON_SPOT;
public static final String NORTH_WEST = NORTH + "_" + WEST + "_" + ON_SPOT;
public static final String NORTH_EAST = NORTH + "_" + EAST + "_" + ON_SPOT;
public static final String ON_GRID_POINT = "onGridPoint";
public static final String SOUTH_WEST = SOUTH + "_" + WEST + "_" + ON_GRID_POINT;
public static final String SOUTH_EAST = SOUTH + "_" + EAST + "_" + ON_GRID_POINT;
public static final String NORTH_WEST = NORTH + "_" + WEST + "_" + ON_GRID_POINT;
public static final String NORTH_EAST = NORTH + "_" + EAST + "_" + ON_GRID_POINT;
public static final String ON_GRID_BOARDER = "onGridBoarder";
public static final String NORTH_BOARDER = "North" + "_" + ON_GRID_BOARDER;
public static final String EAST_BOARDER = "East" + "_" + ON_GRID_BOARDER;
public static final String SOUTH_BOARDER = "South" + "_" + ON_GRID_BOARDER;
public static final String WEST_BOARDER = "West" + "_" + ON_GRID_BOARDER;
public static final String ON_GRID_CORNER = "onGridCorner";
public static final String NO_DIRECTION = "No direction";
public Directions(){
private final Coordinate ref;
public Directions(Coordinate ref){
this.ref=ref;
directions.add(SOUTH);
directions.add(EAST);
directions.add(NORTH);
......@@ -62,4 +75,33 @@ public class Directions {
return directions.get(direction+offset);
}
public Direction stepInDirection(String direction){
if(direction.equals(Directions.SOUTH)){
return new Direction(ref.r-1, ref.c);
}
if(direction.equals(Directions.EAST)){
return new Direction(ref.r, ref.c+1);
}
if(direction.equals(Directions.WEST)){
return new Direction(ref.r, ref.c-1);
}
if(direction.equals(Directions.NORTH)){
return new Direction(ref.r+1, ref.c);
}
if(direction.equals(Directions.NORTH_WEST)){
return new Direction(ref.r+1, ref.c-1);
}
if(direction.equals(Directions.NORTH_EAST)){
return new Direction(ref.r+1, ref.c+1);
}
if(direction.equals(Directions.SOUTH_WEST)){
return new Direction(ref.r-1, ref.c-1);
}
if(direction.equals(Directions.SOUTH_EAST)){
return new Direction(ref.r-1, ref.c+1);
}
return new Direction(ref.r, ref.c);
}
}
......@@ -32,11 +32,11 @@ public class IsoSpline {
public Polygons createIsoPolygons(IsoApi matrix, String name, Color color, double isoLevel, boolean closeAroundTheBorder){
// iscs.getIsoSplineContainer(3, 1).createSplineElement(isoLevel);
// iscs.getIsoSplineContainer(3, 1).createSplineElements(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);
iscs.getIsoSplineContainer(r, c).createSplineElements(isoLevel);
}
// if(closeAroundTheBorder){
// for(int r=0; r<matrix.getRows()-1; r++){
......
......@@ -5,19 +5,19 @@
*/
package isospline;
import java.util.ArrayList;
import java.awt.Dimension;
/**
*
* @author a001188
*/
public class IsoSplineContainer {
private final boolean CORNER = true;
private final boolean ON_GRID_POINT = true;
private final IsoSplineContainers iscs;
public final ArrayList<Coordinate> coords = new ArrayList();
public final Coordinates coords = new Coordinates();
private IsoSplineElements ises;
public final Coordinate ref;
private final Directions directions = new Directions();
private final Directions directions;
public int currentIsoSplineElementIndex = 0;
public double isoLevel = 0;
public IsoSplineContainer(IsoSplineContainers iscs, Coordinate coord0, Coordinate coord1, Coordinate coord2, Coordinate coord3){
......@@ -27,6 +27,7 @@ public class IsoSplineContainer {
coords.add(coord2);
coords.add(coord3);
this.ref=coord0;
directions = new Directions(ref);
}
///////////////////////////////////////////////////////////////////////////////
......@@ -41,91 +42,19 @@ public class IsoSplineContainer {
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);
}
if(direction.equals(Directions.NORTH_WEST)){
return iscs.getIsoSplineContainer(ref.r+1, ref.c-1);
}
if(direction.equals(Directions.NORTH_EAST)){
return iscs.getIsoSplineContainer(ref.r+1, ref.c+1);
}
if(direction.equals(Directions.SOUTH_WEST)){
return iscs.getIsoSplineContainer(ref.r-1, ref.c-1);
}
if(direction.equals(Directions.SOUTH_EAST)){
return iscs.getIsoSplineContainer(ref.r-1, ref.c+1);
}
return null;
Direction step = directions.stepInDirection(direction);
return iscs.getIsoSplineContainer(step.row, step.col);
}
public boolean hasIsoSplineContainerInDirection(String direction, boolean hasIsoSpline){
if(direction.equals(Directions.SOUTH)){
if(ref.r > 0)
if(hasIsoSpline)
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 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 getIsoSplineContainerInDirection(direction).hasIsoSplineElementWithDirection(Directions.getOppositeDirection(Directions.NORTH));
else
return true;
}
else if(direction.equals(Directions.WEST)){
if(ref.c > 0)
if(hasIsoSpline)
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)
Direction step = directions.stepInDirection(direction);
if(step.row>0 && step.row < iscs.rows)
if(step.col>0 && step.col < iscs.cols)
if(hasIsoSpline)
return getIsoSplineContainerInDirection(direction).hasIsoSplineElementWithDirection(Directions.SOUTH_EAST);
return getIsoSplineContainerInDirection(direction).hasIsoSplineElementWithDirection(Directions.getOppositeDirection(direction));
else
return true;
}
else if(direction.equals(Directions.NORTH_EAST)){
if(ref.r+1 < iscs.rows && ref.c+1 < iscs.cols)
if(hasIsoSpline)
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 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 getIsoSplineContainerInDirection(direction).hasIsoSplineElementWithDirection(Directions.NORTH_EAST);
else
return true;
}
return false;
}
......@@ -151,49 +80,22 @@ public class IsoSplineContainer {
}
public void createBorderSplineElement(double isooLevel) {
if(ref.r==0 && ref.c==0){
if(ises.isEmpty())
ises.add(new IsoSplineElement(coords.get(1),coords.get(3))); // Shortcut the corner
else{
}
}
else if(ref.r==0 && ref.c==iscs.cols){
if(ises.isEmpty())
ises.add(new IsoSplineElement(coords.get(0),coords.get(3))); // Shortcut the corner
else{
}
}
else if(ref.r==iscs.rows && ref.c==0){
if(ises.isEmpty())
ises.add(new IsoSplineElement(coords.get(0),coords.get(3))); // Shortcut the corner
else{
}
}
else if(ref.r==iscs.rows && ref.c==iscs.cols){
if(ises.isEmpty()){
}
else{
}
}
//To change body of generated methods, choose Tools | Templates.
public void createSplineElements(double isoLevel){
Coordinates splineCoords = createSplineElement(isoLevel);
createIsoSplineElements(ref, splineCoords);
}
public void createSplineElement(double isoLevel){
private Coordinates createSplineElement(double isoLevel){
this.isoLevel=isoLevel;
ArrayList<Coordinate> splineCoords = new ArrayList();
Coordinates splineCoords = new Coordinates();
//System.out.println(ref);
int j;
for(int i = 0; i<coords.size(); i++){ // walk around
if(i==coords.size()-1)
if(i == coords.size()-1)
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);
......@@ -206,19 +108,20 @@ public class IsoSplineContainer {
double range = (isoLevel - v1)/(v2 - v1);
double lat = range * (coord2.lat-coord1.lat) + coord1.lat;
double lon = range * (coord2.lon-coord1.lon) + coord1.lon;
splineCoord = new Coordinate(lon, lat, coord1.r, coord1.c, isoLevel, directions.get(i, !CORNER));
splineCoord = new Coordinate(lon, lat, coord1.r, coord1.c, isoLevel, directions.get(i, !ON_GRID_POINT));
splineCoords.add(splineCoord);
}
}
if(v1 == isoLevel){
splineCoord = new Coordinate(coord1.lon, coord1.lat, coord1.r, coord1.c, isoLevel, directions.get(i, CORNER));
splineCoord = new Coordinate(coord1.lon, coord1.lat, coord1.r, coord1.c, isoLevel, directions.get(i, ON_GRID_POINT));
splineCoords.add(splineCoord);
}
}
return splineCoords;
}
public void createIsoSplineElements(Coordinate ref, Coordinates splineCoords){
ises = new IsoSplineElements(ref);
//System.out.println(ref.toString() + " size: " + splineCoords.size() + " => " + splineCoords.toString());
......@@ -232,42 +135,42 @@ public class IsoSplineContainer {
ises.add(new IsoSplineElement(splineCoords.get(0),splineCoords.get(1)));
break;
case 3:
if(getOnSpot(splineCoords)>=0){
if(countIsoSplineCoordsOnGridPoints(splineCoords)>=0){
ises.add(new IsoSplineElement(splineCoords.get(0),splineCoords.get(1)));
ises.add(new IsoSplineElement(splineCoords.get(0),splineCoords.get(2)));
ises.add(new IsoSplineElement(splineCoords.get(1),splineCoords.get(2)));
break;
}
if(getOnSpot(splineCoords)==2){
if(getFirstNotOnSpot(splineCoords)==0){
if(countIsoSplineCoordsOnGridPoints(splineCoords)==2){
if(getFirstIsoSplineCoord(splineCoords, !ON_GRID_POINT)==0){
ises.add(new IsoSplineElement(splineCoords.get(0),splineCoords.get(1)));
ises.add(new IsoSplineElement(splineCoords.get(0),splineCoords.get(2)));
break;
}
if(getFirstNotOnSpot(splineCoords)==1){
if(getFirstIsoSplineCoord(splineCoords, !ON_GRID_POINT)==1){
ises.add(new IsoSplineElement(splineCoords.get(1),splineCoords.get(0)));
ises.add(new IsoSplineElement(splineCoords.get(1),splineCoords.get(2)));
break;
}
if(getFirstNotOnSpot(splineCoords)==2){
if(getFirstIsoSplineCoord(splineCoords, !ON_GRID_POINT)==2){
ises.add(new IsoSplineElement(splineCoords.get(2),splineCoords.get(0)));
ises.add(new IsoSplineElement(splineCoords.get(2),splineCoords.get(1)));
break;
}
break;
}
if(getOnSpot(splineCoords)==1){
if(getFirstOnSpot(splineCoords)==0){
if(countIsoSplineCoordsOnGridPoints(splineCoords)==1){
if(getFirstIsoSplineCoord(splineCoords, ON_GRID_POINT)==0){
ises.add(new IsoSplineElement(splineCoords.get(0),splineCoords.get(1)));
ises.add(new IsoSplineElement(splineCoords.get(0),splineCoords.get(2)));
break;
}
if(getFirstOnSpot(splineCoords)==1){
if(getFirstIsoSplineCoord(splineCoords, ON_GRID_POINT)==1){
ises.add(new IsoSplineElement(splineCoords.get(1),splineCoords.get(0)));
ises.add(new IsoSplineElement(splineCoords.get(1),splineCoords.get(2)));
break;
}
if(getFirstOnSpot(splineCoords)==2){
if(getFirstIsoSplineCoord(splineCoords, ON_GRID_POINT)==2){
ises.add(new IsoSplineElement(splineCoords.get(2),splineCoords.get(0)));
ises.add(new IsoSplineElement(splineCoords.get(2),splineCoords.get(1)));
break;
......@@ -287,32 +190,23 @@ public class IsoSplineContainer {
private int getOnSpot(ArrayList<Coordinate> splineCoords){
private int countIsoSplineCoordsOnGridPoints(Coordinates splineCoords){
int n=0;
for(int i = 0; i<splineCoords.size();i++)
if(splineCoords.get(i).direction.contains(Directions.ON_SPOT))
if(splineCoords.get(i).direction.contains(Directions.ON_GRID_POINT))
n++;
return n;
}
private int getFirstOnSpot(ArrayList<Coordinate> splineCoords){
private int getFirstIsoSplineCoord(Coordinates splineCoords, boolean onGridPoint){
for(int i = 0; i<splineCoords.size();i++)
if(splineCoords.get(i).direction.contains(Directions.ON_SPOT))
if(splineCoords.get(i).direction.contains(Directions.ON_GRID_POINT) == onGridPoint)
return i;
return -1;
}
private int getFirstNotOnSpot(ArrayList<Coordinate> splineCoords){
for(int i = 0; i<splineCoords.size();i++)
if(!splineCoords.get(i).direction.contains(Directions.ON_SPOT))
return i;
return -1;
}
public boolean hasIsoSplineElement(){
......@@ -355,21 +249,18 @@ public class IsoSplineContainer {
}
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(){
StringBuilder sb = new StringBuilder();
......@@ -393,25 +284,4 @@ public class IsoSplineContainer {
sb.append("\r\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;
}
*/
......@@ -6,13 +6,12 @@
package isospline;
import java.awt.Dimension;
import java.util.ArrayList;
/**
*
* @author peter
*/
public class IsoSplineElement extends ArrayList<Coordinate> implements Comparable<IsoSplineElement> {
public class IsoSplineElement extends Coordinates implements Comparable<IsoSplineElement> {
public int sortIndex;
public IsoSplineElement(Coordinate coord1, Coordinate coors2){
......
......@@ -12,7 +12,7 @@ import java.util.ArrayList;
*
* @author a001188
*/
public class Matrix extends ArrayList<Coordinate> implements IsoApi, Cloneable{
public class Matrix extends Coordinates implements IsoApi, Cloneable{
public static final String LONLAT = "lonlat";
public static final String RC = "RC";
private final int rows;
......
......@@ -12,7 +12,7 @@ import java.util.ArrayList;
*
* @author a001188
*/
public class MatrixCorners extends ArrayList<Coordinate>{
public class MatrixCorners extends Coordinates{
public MatrixCorners(Coordinate R0C0, Coordinate R0Cn, Coordinate RmC0, Coordinate RmCn){
add(R0C0);
......
......@@ -13,7 +13,7 @@ import java.awt.Color;
*
* @author a001188
*/
public class Polygon extends ArrayList<Coordinate>{
public class Polygon extends Coordinates{
public boolean closed=false;
public final Color color;
public final String name;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment