00001 package ImageStuff;
00002
00003 import java.util.ArrayList;
00004 import java.util.PriorityQueue;
00005
00006
00007 import junit.framework.Assert;
00008
00009 import debuggingCode.Debug;
00010
00011 import Algorithms.ArrayHandler;
00012 import Algorithms.Bresenham;
00013 import Algorithms.RicoSingleton;
00014 import CodingUtils.CommitDataStructure;
00015 import CodingUtils.DataStructure;
00016 import CodingUtils.State;
00017 import ImageStuff.Topology.AbstractTesselation;
00018 import ImageStuff.Topology.ConnectieCriterium;
00019
00020 import ImageStuff.Topology.EdgeTesselation_Encoder;
00021
00022 import ImageStuff.util.myVector;
00023
00024 import NormalMeshAlgorithm.Configuration;
00025 import NormalMeshAlgorithm.SnijPixelStrategy;
00026 import TxTPackage.DecodedFileWithRealValues;
00027 import TxTPackage.EncodedFileWithRealValues;
00028
00029 public class GenericEdge extends GeometricObject {
00030 protected int[][] lijn;
00031 protected final Vertex begin,eind;
00032
00033
00034 protected Triangle leftTriangle, rightTriangle;
00035 protected GenericEdge beginChild, endChild;
00036 protected boolean isRegistered;
00037
00038
00039
00040
00041 private int normalOffset;
00042 private int verticalOffset;
00043 public boolean test_offset_set;
00044 protected State state;
00045
00046
00051 public GenericEdge(Vertex a, Vertex b, int [][] lijn,Configuration configuration) {
00052 super(configuration);
00053 PriorityQueue<Vertex> sortedVertices=new PriorityQueue<Vertex>(2,new VertexComparator());
00054 sortedVertices.add(a);
00055 sortedVertices.add(b);
00056 this.begin = sortedVertices.poll();
00057 this.eind=sortedVertices.poll();
00058 if(new Loc(lijn[0][0],lijn[0][1]).isOn(eind) &&new Loc(lijn[lijn.length-1][0],lijn[lijn.length-1][1]).isOn(begin) ){
00059 this.lijn=ArrayHandler.Flip(lijn);
00060 if(Debug.DEBUG){ ArrayHandler.printrij(lijn);
00061 Debug.print("HOW ZEKER!!!!!!!!");}
00062
00063
00064
00065 }
00066 else{
00067 this.lijn=lijn;
00068 }
00069 VulOp();
00070
00071
00072 if(Debug.DEBUG && Debug.DEBUG_IMAGESTUFF){
00073 if(this.begin.isOn(eind)){
00074 if(!(begin==eind)){
00075 Debug.print("GenericEdge krijgt twee verschillende vertices binnen op dezelfde locatie;als dit komt doordat twee piercingpunten van een tesselatie samenvallen (no prob), maar niet overschrijven!");
00076 }
00077 }
00078 }
00079
00080
00081 this.state=State.getInitialState(this,configuration);
00082
00083 }
00084
00085
00086
00087
00088 public GenericEdge(Vertex a, Vertex b,Configuration configuration){
00089 super(configuration);
00090 PriorityQueue<Vertex> sortedVertices=new PriorityQueue<Vertex>(2,new VertexComparator());
00091 sortedVertices.add(a);
00092 sortedVertices.add(b);
00093 this.begin = sortedVertices.poll();
00094 this.eind=sortedVertices.poll();
00095 this.lijn=Bresenham.lijn(begin.getX(),begin.getY(),eind.getX(),eind.getY());
00096 VulOp();
00097
00098
00099 if(Debug.DEBUG && Debug.DEBUG_IMAGESTUFF){
00100 if(this.begin.isOn(eind)){
00101 if(!(begin==eind)){
00102 Debug.print("GenericEdge krijgt twee verschillende vertices binnen op dezelfde locatie;als dit komt doordat twee piercingpunten van een tesselatie samenvallen (no prob), maar niet overschrijven!");
00103 }
00104 }
00105 }
00106
00107 this.state=State.getInitialState(this,configuration);
00108 }
00109
00110 public boolean containsBothVertices(Vertex a,Vertex b){
00111 return (a==this.begin && b==this.eind) || (a==this.eind && b==this.begin);
00112
00113 }
00114
00115 public boolean containtsVertex(Vertex v){
00116 return this.begin==v || this.eind==v;
00117 }
00118
00119 public boolean hasToBeConsidered(){
00120 return true;
00121 }
00122
00123 public boolean isATriangle(){
00124 return false;
00125 }
00126 public boolean isAnEdge(){
00127 return true;
00128 }
00129
00130
00131 protected void VulOp() {
00132 locs.clear();
00133 for (int i = 0; i != lijn.length; i++) {
00134 locs.add(Image.getInstance().getPuntOpEnty(lijn[i][0], lijn[i][1]));
00135 }
00136 }
00137
00138
00139
00140 public void commit(CommitDataStructure dataStructure){
00141
00142 this.register(dataStructure);
00143 this.registerTriangles(dataStructure);
00144 if(Debug.DEBUG && Debug.DEBUG_WRITE_DECODER_DATASTATUS){
00145 DecodedFileWithRealValues.writeStatusOfDataStructure(dataStructure);
00146 }
00147 }
00148
00149
00150
00151
00152 public Triangle getLeftTriangle(){
00153 return this.leftTriangle;
00154 }
00155
00156 public void setTriangle(Triangle t){
00157 this.setLeftTriangle(t);
00158 myVector v=new myVector(begin.getLoc(),eind.getLoc());
00159 myVector other=null;
00160 if(t.hasToBeConsidered()){
00161
00162 Vertex vertex=t.getVertexOtherThan(begin,eind);
00163 if(vertex==null){
00164 Debug.print("vertex niet gevonden");
00165 }
00166 other=new myVector(begin.getLoc(),vertex);
00167
00168 }
00169 else{
00170
00171
00172 return;
00173 }
00174
00175 if((v.vectorieelProduct(other)).getZ()>0){
00176 setLeftTriangle(t);
00177 }
00178 else{
00179 setRightTriangle(t);
00180 }
00181 }
00182
00183
00184 public Triangle getRightTriangle(){
00185 return this.rightTriangle;
00186 }
00187
00188 private void registerLeftTriangle(CommitDataStructure dataStructure){
00189 if(this.leftTriangle!=null) leftTriangle.register(dataStructure);
00190 }
00191
00192
00193 private void setRightTriangle(Triangle t){
00194 this.rightTriangle=t;
00195 }
00196
00197
00198
00199 private void registerRightTriangle(CommitDataStructure dataStructure){
00200 if(this.rightTriangle!=null) rightTriangle.register(dataStructure);
00201 }
00202
00203 private void registerTriangles(CommitDataStructure dataStructure){
00204 this.registerLeftTriangle(dataStructure);
00205 this.registerRightTriangle(dataStructure);
00206 }
00207
00208
00209
00210
00211
00215 private void register(CommitDataStructure dataStructure){
00216 if(!isRegistered()){
00217 dataStructure.addEdge(this);
00218 this.getVertex(0).register(dataStructure);
00219 this.getVertex(1).register(dataStructure);
00220 this.isRegistered=true;
00221 }
00222 }
00223
00224 private boolean isRegistered(){
00225 return isRegistered;
00226 }
00227
00228 private void setLeftTriangle(Triangle t){
00229 this.leftTriangle=t;
00230 }
00231
00232
00233
00234
00235
00236 public void setChildren(GenericEdge beginChild, GenericEdge endChild){
00237 if(beginChild==null || endChild==null){
00238 Object o=new Object();
00239 o=null;
00240 o.notify();
00241 }
00242 if(beginChild!=this)this.beginChild=beginChild;
00243 if(endChild!=this) this.endChild=endChild;
00244
00245 beginChild.setState(this.state.getNextState(beginChild));
00246 endChild.setState(this.state.getNextState(endChild));
00247 }
00248
00249 public boolean hasChildren(){
00250 return (beginChild!=null && endChild!=null);
00251 }
00252
00253 public GenericEdge getBeginChild(){
00254 return beginChild;
00255 }
00256
00257 public GenericEdge getEindChild(){
00258 return endChild;
00259
00260 }
00261
00262 public void setChildren(ArrayList<GeometricObject> children){
00263 if(!(children.size()==2)){
00264 System.out.print("kan hier niet!");
00265 }
00266 try{
00267 setChildren((GenericEdge)children.get(0),(GenericEdge)children.get(1));
00268 }catch(ClassCastException castException){
00269 Debug.print("Kan niet casten naar GenericEdge!!");
00270 }
00271 }
00272
00273 public ArrayList<GeometricObject> getChildren(){
00274 ArrayList<GeometricObject> children=new ArrayList<GeometricObject>(2);
00275 children.add(beginChild);
00276 children.add(endChild);
00277 return children;
00278 }
00279
00280
00281
00287 public AbstractTesselation getBestEncoderTesselation(SnijPixelStrategy snijPixelStrategy, ConnectieCriterium connectieCriterium){
00288 return new EdgeTesselation_Encoder(this,snijPixelStrategy,configuration);
00289 }
00290
00291 public AbstractTesselation getTesselation(SnijPixelStrategy snijPixelStrategy,String choise){
00292 Debug.print("should not be issued");
00293 Object o=null;
00294 o.notify();
00295 System.exit(-1);
00296 return null;
00297 }
00298
00299
00300
00301
00302 public Vertex doorsnede(GenericEdge andere)throws Exception{
00303 if(this.begin == andere.getBegin() || this.begin == andere.getEind()){
00304 return begin;
00305 }
00306 if(this.eind == andere.getBegin() || this.eind == andere.getEind()){
00307 return eind;
00308 }
00309 throw new Exception();
00310
00311 }
00312
00313
00314
00315
00316 public float getArea(){
00317 return 0;
00318 }
00319
00320 public float getHoekInDomain(int index){
00321 return 0;
00322 }
00323
00324 public Loc getLoc(int index) {
00325 return new Loc(lijn[index][0], lijn[index][1]);
00326 }
00327
00328
00329 public final Vertex getBegin() {
00330 return this.begin;
00331 }
00332
00333
00334 public final Vertex getEind() {
00335 return this.eind;
00336 }
00337
00338 public Vertex getVertex(int index){
00339 if (index == 0) {
00340 return getBegin();
00341 }
00342 return getEind();
00343
00344 }
00345
00346 public int[][] getLijn() {
00347 return lijn;
00348 }
00349
00350 public int getLengte() {
00351 if(begin.isOn(this.eind)) return 1;
00352
00353 return lijn.length;
00354 }
00355
00356 public float getEuclidischeLengte(){
00357 return this.getBegin().getLoc().distance(this.getEind().getLoc());
00358
00359 }
00360
00361 public float getEuclidischeLengtePerPixel(){
00362 return this.getEuclidischeLengte()/ (this.getLengte());
00363 }
00364
00365 public int getAantalPunten() {
00366 if(this.getEind().isOn(this.getBegin())){return 1;}
00367 return 2;
00368 }
00369
00370 public ArrayList<Vertex> getOrderedVertices(){
00371 ArrayList<Vertex> vertices=new ArrayList<Vertex>(2);
00372 vertices.add(this.getEind());
00373 if(this.getAantalPunten()>1){
00374 vertices.add(begin);
00375 }
00376 return vertices;
00377 }
00378
00379
00384 public int getHoogte() {
00385 return (int)Math.abs(this.begin.getZ() - this.eind.getZ());
00386 }
00387
00388 public int getMiddenIndex() {
00389 if(getLengte()% 2 ==0 && this.getLengte()!=0){
00390 return Math.round((float) (Math.floor(getLengte() / 2f)))-1;
00391 }
00392 else{
00393 return Math.round((float) (Math.floor(getLengte() / 2f)));
00394 }
00395 }
00396
00397 public Loc getMidLoc() {
00398 return this.getLoc(this.getMiddenIndex());
00399 }
00400
00401 public float getFundamentenWaardeOn(int index){
00402 float f1=begin.getZ();
00403 float f2=eind.getZ();
00404 float totale_afstand=begin.distance(eind);
00405 Loc l=this.getLoc(index);
00406 float afstand_begin_tot_index=begin.distance(l);
00407 float verhouding=afstand_begin_tot_index/totale_afstand;
00408
00409 float functiewaarde = (1-verhouding)*f1+verhouding*f2;
00410
00411 return functiewaarde;
00412 }
00413
00414 public float getFunctiewaardeOn(int index) {
00415 float f1=begin.getZ()+begin.getVerticalOffset();
00416 float f2=eind.getZ()+eind.getVerticalOffset();
00417 float totale_afstand=begin.distance(eind);
00418 Loc l=this.getLoc(index);
00419 float afstand_begin_tot_index=begin.distance(l);
00420 float verhouding=afstand_begin_tot_index/totale_afstand;
00421
00422 float functiewaarde = (1-verhouding)*f1+verhouding*f2;
00423
00424 return functiewaarde;
00425 }
00426
00427 public float getFunctiewaardeOn(Loc loc) {
00428
00429 if (this.getLocs().size() == 1) {
00430 Loc l = (Loc) (this.getLocs().iterator().next());
00431 return this.getBegin().getZ()+this.getBegin().getVerticalOffset();
00432 }
00433 for (int i = 0; i != this.lijn.length; i++) {
00434 if (loc.getX() == lijn[i][0] && loc.getY() == lijn[i][1]) {
00435 return getFunctiewaardeOn(i);
00436 }
00437
00438 }
00439
00440 if(Debug.DEBUG) Debug.print("fout in Edge");
00441 return 0;
00442 }
00443
00444 public float fundamentenwaardeOn(Loc loc){
00445 return this.getFunctiewaardeOn(loc);
00446 }
00447
00449 public void setOffsets(int normalOffset, int verticalOffset) {
00450 test_offset_set = true;
00451 this.normalOffset = normalOffset;
00452 if(Debug.DEBUG && Debug.DEBUG_WRITE_DECODER_DATASTATUS){
00453 DecodedFileWithRealValues.write("normalOffset= " + normalOffset +" verticalOffset= " + verticalOffset);
00454 }
00455 this.verticalOffset = verticalOffset;
00456
00457 }
00458
00463 public int getNormalOffset() {
00464 Assert.assertTrue(test_offset_set);
00465 return this.normalOffset;
00466 }
00467
00468 public float getNormalOffsetDistance() {
00469 RicoSingleton r = RicoSingleton.getInstance(this, false);
00470 float rico = r.getRicoEdge();
00471 float afstand;
00472 if (Float.isNaN(rico)) {
00473
00474 return 0;
00475 }
00476 if (Math.abs(rico) < 0.1) {
00477 afstand = 0;
00478 } else {
00479 double alfa = Math.atan(rico);
00480 afstand = (float) this.getNormalOffset() / (float) Math.sin(alfa);
00481
00482
00483
00484
00485
00486 }
00487 return afstand;
00488 }
00489
00494 public int getVerticalOffset() {
00495 Assert.assertTrue(test_offset_set);
00496 return this.verticalOffset;
00497 }
00498
00499
00504 public boolean hasCrossedContour(){
00505 Assert.assertTrue(test_offset_set);
00506 int hoogte= (int)Math.abs(this.getBegin().getZ()-this.getEind().getZ());
00507 if((this.getNormalOffset()-this.getMiddenIndex())< hoogte*hoogte/(2*this.getLengte())){
00508
00509 return true;
00510 }
00511 return false;
00512 }
00513
00514
00515
00516
00517
00518
00519 public void print() {
00520 ArrayHandler.printrij(this.lijn);
00521 }
00522
00523 public GenericEdge getEdge(int i) {
00524 return this;
00525 }
00526
00527 public int getNbrOfEdges(){
00528 return 1;
00529 }
00530
00531
00532
00533
00534 public State getState() {
00535 return state;
00536 }
00537
00538
00539
00540
00541 public void setState(State state) {
00542 this.state = state;
00543 }
00544
00545
00546
00547 }