00001 package commandline.Encoder;
00002
00003 import java.util.ArrayList;
00004 import java.util.HashSet;
00005
00006 import commandline.ImageStuff.Loc;
00007 import commandline.ImageStuff.GeometricObjects.GenericEdge;
00008 import commandline.ImageStuff.GeometricObjects.GeometricObject;
00009 import commandline.ImageStuff.GeometricObjects.Triangle;
00010 import commandline.debuggingCode.Debug;
00011
00012
00013
00014 public class Splitter {
00015
00022 public static ArrayList<GeometricObject> split(Triangle t,boolean orderDependent){
00023 ArrayList<GeometricObject> parts=new ArrayList<GeometricObject>();
00024 for(GenericEdge e:t.getEdges()){
00025 if((!aNeighboringTriangleAlreadyHasThisAsAChild(e,t)&& orderDependent)&& shouldEdgeMakePartOfFurtherDecom(e,t) && (!hasWorthyTriangle(e,t) || !orderDependent)){
00026
00027 parts.add(e);}
00028 }
00029
00030
00031 return parts;
00032 }
00033
00034 private static boolean aNeighboringTriangleAlreadyHasThisAsAChild(GenericEdge edge, Triangle t){
00035 Triangle other=null;
00036 if(edge.getLeftTriangle()!=null && edge.getLeftTriangle()!=t) other=edge.getLeftTriangle();
00037 if(edge.getRightTriangle()!=null && edge.getRightTriangle()!=t) other=edge.getRightTriangle();
00038
00039 if(other==null){
00040 return false;
00041 }else{
00042 if(other.getChildren().contains(edge)){return true;}
00043 else{return false;}
00044 }
00045
00046 }
00047
00048
00049 private static boolean shouldEdgeMakePartOfFurtherDecom(GenericEdge e, Triangle t){
00050
00051 if(e.getBegin().isOn(e.getEind())) {
00052 if(Debug.DEBUG && Debug.DEBUG_SPLITTER){Debug.print("both end points of an edge ly on the same location");}
00053 return false;}
00054
00055
00056
00057 for(GenericEdge other: t.getEdges()){
00058 if(other!=e){
00059 if(e.getBegin().isOn(other.getBegin()) && e.getEind().isOn(other.getEind())){
00060
00061 if(!hasWorthyTriangle(other,t) && (e.getBegin().getZ()>other.getBegin().getZ() || e.getEind().getZ()>other.getEind().getZ())){
00062 if(Debug.DEBUG && Debug.DEBUG_SPLITTER){Debug.print("one of the edges above the other");}
00063 return false;
00064 }
00065 }
00066 if(e.getBegin().isOn(other.getEind()) && e.getEind().isOn(other.getBegin())){
00067
00068 if(!hasWorthyTriangle(other,t) && (e.getBegin().getZ()>other.getEind().getZ() || e.getEind().getZ()>other.getBegin().getZ())){
00069 if(Debug.DEBUG && Debug.DEBUG_SPLITTER){Debug.print("one of the edges above the other");}
00070 return false;
00071 }
00072
00073 }
00074
00075 }
00076
00077 }
00078
00079
00080 HashSet<Loc> locationsOfotherEdges=new HashSet<Loc>();
00081 boolean onewillleave=false;
00082 for(GenericEdge other:t.getEdges()){
00083 if(other!=e){
00084 if(hasWorthyTriangle(other,t)){onewillleave=true;}
00085 locationsOfotherEdges.addAll(other.getLocs());
00086 }
00087 }
00088 if(e.getLocs().containsAll(locationsOfotherEdges)&& !onewillleave){
00089 if(Debug.DEBUG && Debug.DEBUG_SPLITTER){Debug.print("one of the edges contains the other ones");}
00090 return false;
00091 }
00092
00093 return true;
00094 }
00095
00096
00097 private static boolean hasWorthyTriangle(GenericEdge edge, GeometricObject nextGeomObj){
00098 boolean hasWorthy=false;
00099 if(edge.getLeftTriangle()!=null && edge.getLeftTriangle()!=nextGeomObj) {
00100
00101 if(edge.getLeftTriangle().hasToBeConsidered()) {
00102
00103 hasWorthy=true;
00104 }else{
00105
00106
00107
00108 }
00109 }
00110 else{if(edge.getRightTriangle()!=null && edge.getRightTriangle()!=nextGeomObj){
00111
00112 if(edge.getRightTriangle().hasToBeConsidered()) {
00113
00114 hasWorthy=true;
00115 }else{
00116
00117
00118
00119 }
00120 }
00121 }
00122 return hasWorthy;
00123 }
00124
00125
00126
00127 }