00001 package commandline.ImageStuff.GeometricObjects;
00002
00003 import java.util.ArrayList;
00004 import java.util.PriorityQueue;
00005
00006 import junit.framework.Assert;
00007
00008 import commandline.Algorithms.ArrayHandler;
00009 import commandline.Algorithms.Bresenham;
00010 import commandline.Algorithms.RicoSingleton;
00011 import commandline.CodingUtils.CommitDataStructure;
00012 import commandline.CodingUtils.State;
00013 import commandline.Encoder.dynamicProg.Tree;
00014 import commandline.ImageStuff.Image;
00015 import commandline.ImageStuff.Loc;
00016 import commandline.ImageStuff.VertexComparator;
00017 import commandline.ImageStuff.GeometricObjects.factories.EdgeConstructorTriangleMakerFactory;
00018 import commandline.ImageStuff.Topology.AbstractTesselation;
00019 import commandline.ImageStuff.Topology.ConnectieCriterium;
00020 import commandline.ImageStuff.Topology.EdgeTesselation_Encoder;
00021 import commandline.ImageStuff.util.myVector;
00022 import commandline.NormalMeshAlgorithm.Configuration;
00023 import commandline.NormalMeshAlgorithm.SnijPixelStrategy;
00024 import commandline.TxTPackage.DecodedFileWithRealValues;
00025 import commandline.debuggingCode.Debug;
00026
00027
00028
00029
00030
00031
00032 public class GenericEdge extends GeometricObject{
00033 protected int[][] lijn;
00034 protected final ParametrizedPiercingVertex begin,eind;
00035
00036
00037 protected Triangle leftTriangle, rightTriangle;
00038 protected GenericEdge beginChild, endChild;
00039 protected boolean isRegistered;
00040
00041
00042 private int maxr;
00043
00044
00045 private int normalOffset;
00046 private int verticalOffset;
00047 public boolean test_offset_set;
00048 protected State state;
00049
00050
00055 public GenericEdge(ParametrizedPiercingVertex a, ParametrizedPiercingVertex b, int [][] lijn,int maxr) {
00056 super();
00057 this.maxr=maxr;
00058 PriorityQueue<ParametrizedPiercingVertex> sortedVertices=new PriorityQueue<ParametrizedPiercingVertex>(2,new VertexComparator());
00059 sortedVertices.add(a);
00060 sortedVertices.add(b);
00061 this.begin = sortedVertices.poll();
00062 this.eind=sortedVertices.poll();
00063 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) ){
00064 this.lijn=ArrayHandler.Flip(lijn);
00065
00066
00067
00068
00069
00070 }
00071 else{
00072 this.lijn=lijn;
00073 }
00074 VulOp();
00075
00076
00077 if(Debug.DEBUG && Debug.DEBUG_IMAGESTUFF){
00078 if(this.begin.isOn(eind)){
00079 if(!(begin==eind)){
00080 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!");
00081 }
00082 }
00083 }
00084
00085
00086 this.state=State.getInitialState(this);
00087
00088 }
00089
00090
00091
00098 public GenericEdge(ParametrizedPiercingVertex a, ParametrizedPiercingVertex b,int maxr){
00099 super();
00100 this.maxr=maxr;
00101 PriorityQueue<ParametrizedPiercingVertex> sortedVertices=new PriorityQueue<ParametrizedPiercingVertex>(2,new VertexComparator());
00102 sortedVertices.add(a);
00103 sortedVertices.add(b);
00104 this.begin = sortedVertices.poll();
00105 this.eind=sortedVertices.poll();
00106 this.lijn=Bresenham.lijn(begin.getX(),begin.getY(),eind.getX(),eind.getY());
00107 VulOp();
00108
00109
00110 if(Debug.DEBUG && Debug.DEBUG_IMAGESTUFF){
00111 if(this.begin.isOn(eind)){
00112 if(!(begin==eind)){
00113 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!");
00114 }
00115 }
00116 }
00117
00118 this.state=State.getInitialState(this);
00119 }
00120
00121 public boolean containsBothVertices(Vertex a,Vertex b){
00122 return (a==this.begin && b==this.eind) || (a==this.eind && b==this.begin);
00123
00124 }
00125
00126 public boolean containtsVertex(Vertex v){
00127 return this.begin==v || this.eind==v;
00128 }
00129
00130 public boolean hasToBeConsidered(){
00131 return true;
00132 }
00133
00134 public boolean isATriangle(){
00135 return false;
00136 }
00137 public boolean isAnEdge(){
00138 return true;
00139 }
00140
00141 public boolean isFlat(){
00142 return this.getEuclidischeLengte()/this.getHoogte()>maxr;
00143 }
00144
00145
00146 protected void VulOp() {
00147 locs.clear();
00148 for (int i = 0; i != lijn.length; i++) {
00149 locs.add(Image.getInstance().getPuntOpEnty(lijn[i][0], lijn[i][1]));
00150 }
00151 }
00152
00153
00154
00155 public void commit(CommitDataStructure dataStructure){
00156
00157 this.register(dataStructure);
00158 this.registerTriangles(dataStructure);
00159 if(Debug.DEBUG && Debug.DEBUG_WRITE_DECODER_DATASTATUS){
00160 DecodedFileWithRealValues.writeStatusOfDataStructure(dataStructure);
00161 }
00162 }
00163
00164
00165
00166
00167 public Triangle getLeftTriangle(){
00168 return this.leftTriangle;
00169 }
00170
00171 public void setTriangle(Triangle t){
00172 this.setLeftTriangle(t);
00173 myVector v=new myVector(begin.getLoc(),eind.getLoc());
00174 myVector other=null;
00175 if(t.hasToBeConsidered()){
00176
00177 Vertex vertex=t.getVertexOtherThan(begin,eind);
00178 if(vertex==null){
00179 Debug.print("vertex niet gevonden");
00180 }
00181 other=new myVector(begin.getLoc(),vertex);
00182
00183 }
00184 else{
00185
00186
00187 return;
00188 }
00189
00190 if((v.vectorieelProduct(other)).getZ()>0){
00191 setLeftTriangle(t);
00192 }
00193 else{
00194 setRightTriangle(t);
00195 }
00196 }
00197
00198
00199 public Triangle getRightTriangle(){
00200 return this.rightTriangle;
00201 }
00202
00203 private void registerLeftTriangle(CommitDataStructure dataStructure){
00204 if(this.leftTriangle!=null) leftTriangle.register(dataStructure);
00205 }
00206
00207
00208 private void setRightTriangle(Triangle t){
00209 this.rightTriangle=t;
00210 }
00211
00212
00213
00214 private void registerRightTriangle(CommitDataStructure dataStructure){
00215 if(this.rightTriangle!=null) rightTriangle.register(dataStructure);
00216 }
00217
00218 private void registerTriangles(CommitDataStructure dataStructure){
00219 this.registerLeftTriangle(dataStructure);
00220 this.registerRightTriangle(dataStructure);
00221 }
00222
00223
00224
00225
00226
00230 private void register(CommitDataStructure dataStructure){
00231 if(!isRegistered()){
00232 dataStructure.addEdge(this);
00233 this.getVertex(0).register(dataStructure);
00234 this.getVertex(1).register(dataStructure);
00235 this.isRegistered=true;
00236 }
00237 }
00238
00239 private boolean isRegistered(){
00240 return isRegistered;
00241 }
00242
00243 private void setLeftTriangle(Triangle t){
00244 this.leftTriangle=t;
00245 }
00246
00247
00248
00249
00250
00251 public void setChildren(GenericEdge beginChild, GenericEdge endChild){
00252 if(beginChild==null || endChild==null){
00253 Object o=new Object();
00254 o=null;
00255 o.notify();
00256 }
00257
00258 if(Debug.ASSERT_STATEMENTS) Assert.assertTrue(beginChild!=this && endChild!=this);
00259 if(beginChild!=this)this.beginChild=beginChild;
00260 if(endChild!=this) this.endChild=endChild;
00261
00262 beginChild.setState(this.state.getNextState(beginChild));
00263 endChild.setState(this.state.getNextState(endChild));
00264 }
00265
00266 public boolean hasChildren(){
00267 return (beginChild!=null && endChild!=null);
00268 }
00269
00270 public GenericEdge getBeginChild(){
00271 return beginChild;
00272 }
00273
00274 public GenericEdge getEindChild(){
00275 return endChild;
00276
00277 }
00278
00283 public void prune(Tree tree){
00284 this.beginChild=null;
00285 this.endChild=null;
00286 }
00287
00288 public void setChildren(ArrayList<GeometricObject> children){
00289 if(!(children.size()==2)){
00290 System.out.print("kan hier niet!");
00291 }
00292 try{
00293 setChildren((GenericEdge)children.get(0),(GenericEdge)children.get(1));
00294 }catch(ClassCastException castException){
00295 Debug.print("Kan niet casten naar GenericEdge!!");
00296 }
00297 }
00298
00299 public ArrayList<GeometricObject> getChildren(){
00300 ArrayList<GeometricObject> children=new ArrayList<GeometricObject>(2);
00301 children.add(beginChild);
00302 children.add(endChild);
00303 return children;
00304 }
00305
00306
00307
00313 public AbstractTesselation getBestEncoderTesselation(SnijPixelStrategy snijPixelStrategy, ConnectieCriterium connectieCriterium,EdgeConstructorTriangleMakerFactory edgeConstructorTriangleMakerFactory,Configuration configuration){
00314 return new EdgeTesselation_Encoder(this,snijPixelStrategy,edgeConstructorTriangleMakerFactory.getEdgeConstructor(),edgeConstructorTriangleMakerFactory.getTriangleImplType(),configuration);
00315 }
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328 public ParametrizedPiercingVertex doorsnede(GenericEdge andere)throws Exception{
00329 if(this.begin == andere.getBegin() || this.begin == andere.getEind()){
00330 return begin;
00331 }
00332 if(this.eind == andere.getBegin() || this.eind == andere.getEind()){
00333 return eind;
00334 }
00335 throw new Exception();
00336
00337 }
00338
00339
00340
00341
00342 public float getArea(){
00343 return 0;
00344 }
00345
00346 public float getHoekInDomain(int index){
00347 return 0;
00348 }
00349
00350 public Loc getLoc(int index) {
00351 return new Loc(lijn[index][0], lijn[index][1]);
00352 }
00353
00357 public void disposeLocationSet(){
00358
00359 }
00360
00361 public ParametrizedPiercingVertex getBegin() {
00362 return this.begin;
00363 }
00364
00365
00366 public ParametrizedPiercingVertex getEind() {
00367 return this.eind;
00368 }
00369
00370 public ParametrizedPiercingVertex getVertex(int index){
00371 if (index == 0) {
00372 return getBegin();
00373 }
00374 return getEind();
00375
00376 }
00377
00378 public int[][] getLijn() {
00379 return lijn;
00380 }
00381
00382 public int getLengte() {
00383 if(begin.isOn(this.eind)) return 1;
00384
00385 return lijn.length;
00386 }
00387
00388 public float getEuclidischeLengte(){
00389 return this.getBegin().getLoc().distance(this.getEind().getLoc());
00390
00391 }
00392
00393 public float getEuclidischeLengtePerPixel(){
00394 return this.getEuclidischeLengte()/ (this.getLengte());
00395 }
00396
00402 public float getShortestDomainDistance(Loc l){
00403 float x_0,x_1,y_0,y_1,x,y;
00404 x_0=this.getBegin().getX();
00405 y_0=this.getBegin().getY();
00406 x_1=this.getEind().getX();
00407 y_1=this.getEind().getY();
00408 x=l.getX();
00409 y=l.getY();
00410 return Math.abs(((y_0-y_1)*x+(x_1-x_0)*y+(x_0*y_1-x_1*y_0))/begin.distance(eind));
00411 }
00412
00413
00414 public int getAantalPunten() {
00415 if(this.getEind().isOn(this.getBegin())){return 1;}
00416 return 2;
00417 }
00418
00419 public ArrayList<ParametrizedPiercingVertex> getOrderedVertices(){
00420 ArrayList<ParametrizedPiercingVertex> vertices=new ArrayList<ParametrizedPiercingVertex>(2);
00421 vertices.add(this.getEind());
00422 if(this.getAantalPunten()>1){
00423 vertices.add(begin);
00424 }
00425 return vertices;
00426 }
00427
00428
00433 public int getHoogte() {
00434 return (int)Math.abs(this.begin.getZ() - this.eind.getZ());
00435 }
00436
00437 public int getMiddenIndex() {
00438 if(getLengte()% 2 ==0 && this.getLengte()!=0){
00439 return Math.round((float) (Math.floor(getLengte() / 2f)))-1;
00440 }
00441 else{
00442 return Math.round((float) (Math.floor(getLengte() / 2f)));
00443 }
00444 }
00445
00446 public Loc getMidLoc() {
00447 return this.getLoc(this.getMiddenIndex());
00448 }
00449
00450 public float getFundamentenWaardeOn(int index){
00451 float f1=begin.getZ();
00452 float f2=eind.getZ();
00453 float totale_afstand=begin.distance(eind);
00454 Loc l=this.getLoc(index);
00455 float afstand_begin_tot_index=begin.distance(l);
00456 float verhouding=afstand_begin_tot_index/totale_afstand;
00457
00458 float functiewaarde = (1-verhouding)*f1+verhouding*f2;
00459
00460 return functiewaarde;
00461 }
00462
00469 protected float getFunctiewaardeOn(int index) {
00470 float f1=begin.getZ()+begin.getVerticalOffset();
00471
00472 float f2=eind.getZ()+eind.getVerticalOffset();
00473
00474 float totale_afstand=begin.distance(eind);
00475 Loc l=this.getLoc(index);
00476 float afstand_begin_tot_index=begin.distance(l);
00477 float verhouding=afstand_begin_tot_index/totale_afstand;
00478
00479 float functiewaarde = (1-verhouding)*f1+verhouding*f2;
00480
00481 return functiewaarde;
00482 }
00483
00484 public float getFunctiewaardeOn(Loc loc) {
00485
00486 if (this.getLocs().size() == 1) {
00487 return this.getBegin().getZ()+this.getBegin().getVerticalOffset();
00488 }
00489 for (int i = 0; i != this.lijn.length; i++) {
00490 if (loc.getX() == lijn[i][0] && loc.getY() == lijn[i][1]) {
00491 return getFunctiewaardeOn(i);
00492 }
00493
00494 }
00495
00496 if(Debug.DEBUG && Debug.VERBOSE) Debug.print("fout in Edge");
00497 return -1;
00498 }
00499
00500 public float fundamentenwaardeOn(Loc loc){
00501 return this.getFunctiewaardeOn(loc);
00502 }
00503
00505 public void setOffsets(int normalOffset, int verticalOffset) {
00506 test_offset_set = true;
00507 this.normalOffset = normalOffset;
00508 if(Debug.DEBUG && Debug.DEBUG_WRITE_DECODER_DATASTATUS){
00509 DecodedFileWithRealValues.write("normalOffset= " + normalOffset +" verticalOffset= " + verticalOffset);
00510 }
00511 this.verticalOffset = verticalOffset;
00512
00513 }
00514
00519 public int getNormalOffset() {
00520
00521 if(this.isFlat() && this.getLengte()>2) return this.getMiddenIndex();
00522 return this.normalOffset;
00523 }
00524
00525 public float getNormalOffsetDistance() {
00526 RicoSingleton r = RicoSingleton.getInstance(this, false);
00527 float rico = r.getRicoEdge();
00528 float afstand;
00529 if (Float.isNaN(rico)) {
00530
00531 return 0;
00532 }
00533 if (Math.abs(rico) < 0.1) {
00534 afstand = 0;
00535 } else {
00536 double alfa = Math.atan(rico);
00537 afstand = (float) this.getNormalOffset() / (float) Math.sin(alfa);
00538
00539
00540
00541
00542
00543 }
00544 return afstand;
00545 }
00546
00551 public int getVerticalOffset() {
00552
00553 return this.verticalOffset;
00554 }
00555
00556
00561 public boolean hasCrossedContour(){
00562 Assert.assertTrue(test_offset_set);
00563 int hoogte= (int)Math.abs(this.getBegin().getZ()-this.getEind().getZ());
00564 if((this.getNormalOffset()-this.getMiddenIndex())< hoogte*hoogte/(2*this.getLengte())){
00565
00566 return true;
00567 }
00568 return false;
00569 }
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00594 public float getNormalisationFactor(){
00595 float normalisatie=1;
00596 if(Debug.APPLE_TEST){
00597
00598
00599 Debug.print("APPLE_TEST: normalisatie= "+normalisatie);
00600 }
00601 return normalisatie;
00602 }
00603
00604
00605
00606
00607 public void print() {
00608 ArrayHandler.printrij(this.lijn);
00609 }
00610
00611 public GenericEdge getEdge(int i) {
00612 return this;
00613 }
00614
00615 public int getNbrOfEdges(){
00616 return 1;
00617 }
00618
00619 public State getState() {
00620 return state;
00621 }
00622
00623
00624 public void setState(State state) {
00625 this.state = state;
00626 }
00627
00628
00629
00630 }