00001 package commandline.NormalMeshAlgorithm;
00002
00003 import junit.framework.Assert;
00004
00005 import commandline.CodingUtils.ByteConvertor;
00006 import commandline.Encoder.Children_OffsetsCompoud;
00007 import commandline.Encoder.EncoderEdge;
00008 import commandline.ImageStuff.Image;
00009 import commandline.ImageStuff.Loc;
00010 import commandline.ImageStuff.Pixel;
00011 import commandline.ImageStuff.GeometricObjects.GenericEdge;
00012 import commandline.ImageStuff.GeometricObjects.ParametrizedPiercingVertex;
00013 import commandline.ImageStuff.GeometricObjects.Vertex;
00014 import commandline.ImageStuff.util.Index_VertexCompound;
00015 import commandline.debuggingCode.Debug;
00016
00017
00022 public abstract class SnijPixelStrategy extends ChildMaker{
00023
00024
00025 public SnijPixelStrategy(Configuration configuration){
00026 super(configuration);
00027 }
00028
00029
00030 public abstract void printName();
00031 public abstract boolean gebruikFunctieWaarden();
00032
00033
00034 public Children_OffsetsCompoud getChildEdgesOfEdge(int welke_edge){
00035 Debug.print("not to be used");
00036 System.exit(-1);
00037 return null;
00038 }
00039
00040
00041 public static int calculateVerticalOffset(float approx, float function){
00042 if(Debug.ASSERT_STATEMENTS) Assert.assertTrue(function-Math.round(function)==0);
00043 return Math.round(function)-Math.round(approx);
00044 }
00045
00050 public Children_OffsetsCompoud buildChildEdges(GenericEdge edge) {
00051 EncoderEdge e=null;
00052
00053 try{
00054 e=(EncoderEdge) edge;
00055 }catch(ClassCastException ex){
00056 Debug.print("The edge is not an EncoderEdge");
00057 System.exit(-2);
00058 }
00059
00060
00061 EncoderEdge beginEdge,eindEdge;
00062
00063
00064
00065 if(e.getLengte()==1){
00066 return new Children_OffsetsCompoud(e,e,0,calculateVerticalOffset(e.getBegin().getZ(),Image.getInstance().getPuntOpEnty(e.getEind()).getZ()));
00067 }
00068
00069
00070 if(e.getLengte()==2){
00071 if(e.getBegin().getZ()>e.getEind().getZ()){
00072 Children_OffsetsCompoud ch=new Children_OffsetsCompoud(new EncoderEdge(e.getBegin(),e.getBegin(),configuration.getMaxRico()),e,0,calculateVerticalOffset(e.getBegin().getZ(),Image.getInstance().getPuntOpEnty(e.getBegin()).getZ()));
00073 return ch;
00074 }else{
00075 Children_OffsetsCompoud ch= new Children_OffsetsCompoud(e,new EncoderEdge(e.getEind(),e.getEind(),configuration.getMaxRico()),1,calculateVerticalOffset(e.getEind().getZ(),Image.getInstance().getPuntOpEnty(e.getEind()).getZ()));
00076 return ch;
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 }
00092
00093
00094 Index_VertexCompound i_v= SnijPixel_echte_edge(e, Image.getInstance());
00095 int [][]lijn = maakLijn(0,i_v.index(),e);
00096 beginEdge=makeEdge(e.getBegin(),i_v.getVertex(),lijn);
00097 lijn = maakLijn(i_v.index(),e.getLengte()-1,e);
00098 eindEdge =makeEdge(i_v.getVertex(),e.getEind(),lijn);
00099 Assert.assertTrue(obayPiercingConstraint(i_v.getVertex()));
00100
00101 Children_OffsetsCompoud ch= new Children_OffsetsCompoud(beginEdge,eindEdge,i_v.index(),calculateVerticalOffset(i_v.getVertex().getZ(),Image.getInstance().getPuntOpEnty(i_v.getVertex()).getZ()));
00102 return ch;
00103 }
00104
00105
00106
00114 protected abstract float getBeginPixelWaarde(EncoderEdge e);
00115
00116 protected abstract float getEindPixelWaarde(EncoderEdge e);
00117
00118 protected abstract float getPixelWaarde(Pixel p);
00119
00120
00125 public abstract float berekenRicoNormale(GenericEdge e);
00126 public abstract float berekenRicoEdge(GenericEdge e);
00127
00128
00129 public abstract float berekenMiddenHoogte(GenericEdge e);
00130
00141 protected Index_VertexCompound SnijPixel_echte_edge(EncoderEdge e, Image image) {
00142 Assert.assertTrue(e.getLengte()>2);
00143 int midden_index = e.getMiddenIndex();
00144
00145
00146
00147 float rico_normale = berekenRicoNormale(e);
00148 float midden_hoogte = berekenMiddenHoogte(e);
00149 Assert.assertTrue(Math.abs(midden_hoogte)<=ByteConvertor.MAX_COLOR_VALUE);
00150
00151
00152 if (e.isFlat()) {
00153
00154 float waarde=Image.getInstance().getPuntOpEnty(e.getMidLoc()).getZ();
00155 Index_VertexCompound i_v =new Index_VertexCompound(midden_index, new ParametrizedPiercingVertex(e.getMidLoc(),waarde,0,e.getNormalisationFactor(),e));
00156
00157
00158 return i_v;
00159 }
00160 if(Debug.DEBUG && Debug.DEBUG_ENCODERSTUFF){
00161 Debug.print("rico normal :=" + rico_normale);
00162 }
00163
00164 int max_delta_a = midden_index-1;
00165 max_delta_a= max_delta_a(e,rico_normale,midden_index,midden_hoogte,max_delta_a);
00166 int max_delta_b = e.getLengte() - midden_index-1-1;
00167 max_delta_b= max_delta_b(e,rico_normale,midden_index,midden_hoogte,max_delta_b);
00168
00169
00170
00171 float functiewaarde =getFunctionValue(midden_index, e);
00172 boolean is_normale_boven_functie = false;
00173 if (midden_hoogte >= functiewaarde) {
00174 is_normale_boven_functie = true;
00175 }
00176
00177 float normale_waarde;
00178 float afstand;
00179 int temp_index;
00180
00181
00182
00183 PiercingResult resultnaar_a=new PiercingResult();
00184
00185
00186
00187 boolean temp_is_normale_boven_functie = is_normale_boven_functie;
00188 for (int delta=1;delta <= max_delta_a;delta++) {
00189
00190 afstand = e.getLoc(midden_index).distance(e.getLoc(midden_index - delta));
00191 normale_waarde = midden_hoogte - afstand * rico_normale;
00192 functiewaarde =getFunctionValue(midden_index-delta, e);
00193
00194
00195
00196
00197
00198
00199 temp_is_normale_boven_functie = (normale_waarde >= functiewaarde);
00200
00201 if (is_normale_boven_functie != temp_is_normale_boven_functie) {
00202 PiercingResult resultnaar_a_voorpiercing=new PiercingResult();
00203 ParametrizedPiercingVertex voorpiercing_a= new ParametrizedPiercingVertex(e.getLoc(midden_index-delta),normale_waarde,calculateVerticalOffset(normale_waarde,Image.getInstance().getPuntOpEnty(e.getLoc(midden_index-delta)).getZ()),e.getNormalisationFactor(),e);
00204 Index_VertexCompound voorpiercing_naar_a=new Index_VertexCompound(midden_index-delta, voorpiercing_a);
00205 resultnaar_a_voorpiercing.setIndex_VertexCompound(voorpiercing_naar_a,delta);
00206
00207 temp_index=midden_index - delta + 1;
00208 afstand = e.getLoc(midden_index).distance(
00209 e.getLoc(temp_index));
00210 normale_waarde = midden_hoogte - afstand * rico_normale;
00211
00212 functiewaarde =getFunctionValue(temp_index, e);
00213
00214
00215
00216 if(temp_index==0){
00217 Debug.print("temp_index valt samen met de beginlocatie dus vertex met zelfde loc wordt aangemaakt");
00218 System.exit(-1);
00219 }
00220
00221 Loc l=e.getLoc(temp_index);
00222 int verticalOffset=this.calculateVerticalOffset(normale_waarde,Image.getInstance().getPuntOpEnty(l).getZ());
00223 ParametrizedPiercingVertex piercing= new ParametrizedPiercingVertex(l,normale_waarde,verticalOffset,e.getNormalisationFactor(),e);
00224 Index_VertexCompound piercing_naar_a=new Index_VertexCompound(temp_index, piercing);
00225 resultnaar_a.setIndex_VertexCompound(piercing_naar_a,Math.abs(temp_index-midden_index));
00226
00227
00228 resultnaar_a=this.choosePiercingPoint(resultnaar_a_voorpiercing,resultnaar_a, e);
00229
00230 Assert.assertTrue(obayPiercingConstraint(resultnaar_a.getIndexVertexCompound().getVertex()));
00231 break;
00232 }
00233
00234 }
00235
00236
00237 PiercingResult resultnaar_b=new PiercingResult();
00238
00239
00240 temp_is_normale_boven_functie = is_normale_boven_functie;
00241 for( int delta =1; delta <=max_delta_b;delta++){
00242 afstand = e.getLoc(midden_index).distance(
00243 e.getLoc(midden_index + delta));
00244 normale_waarde = midden_hoogte + afstand * rico_normale;
00245 functiewaarde =getFunctionValue(midden_index+delta, e);
00246
00247 temp_is_normale_boven_functie = (normale_waarde >= functiewaarde);
00248
00249 if (is_normale_boven_functie != temp_is_normale_boven_functie) {
00250 PiercingResult resultnaar_b_voorpiercing=new PiercingResult();
00251 ParametrizedPiercingVertex voorpiercing_b= new ParametrizedPiercingVertex(e.getLoc(midden_index+delta),normale_waarde,calculateVerticalOffset(normale_waarde,Image.getInstance().getPuntOpEnty(e.getLoc(midden_index+delta)).getZ()),e.getNormalisationFactor(),e);
00252 Index_VertexCompound voorpiercing_naar_b=new Index_VertexCompound(midden_index+delta, voorpiercing_b);
00253 resultnaar_b_voorpiercing.setIndex_VertexCompound(voorpiercing_naar_b,delta);
00254
00255
00256
00257 temp_index= midden_index + delta -1;
00258
00259 afstand = e.getLoc(midden_index).distance(
00260 e.getLoc(temp_index));
00261 normale_waarde = midden_hoogte + afstand * rico_normale;
00262
00263 functiewaarde =getFunctionValue(temp_index, e);
00264
00265
00266 Loc l=e.getLoc(temp_index);
00267 int verticalOffset=calculateVerticalOffset(normale_waarde,Image.getInstance().getPuntOpEnty(l).getZ());
00268
00269 ParametrizedPiercingVertex piercing=new ParametrizedPiercingVertex(l,normale_waarde,verticalOffset,e.getNormalisationFactor(),e);
00270 Index_VertexCompound piercing_naar_b =new Index_VertexCompound(temp_index,piercing);
00271 resultnaar_b.setIndex_VertexCompound(piercing_naar_b,Math.abs(temp_index-midden_index));
00272
00273 resultnaar_b=this.choosePiercingPoint(resultnaar_b_voorpiercing,resultnaar_b ,e);
00274
00275 Assert.assertTrue(obayPiercingConstraint(resultnaar_b.getIndexVertexCompound().getVertex()));
00276 break;
00277 }
00278
00279 }
00280
00281
00282
00283 if(resultnaar_a.isFound() && resultnaar_b.isFound()){
00284 return getPiercingClosest(resultnaar_a,max_delta_a,resultnaar_b,max_delta_b);
00285 }
00286 else{
00287 if(resultnaar_a.isFound()){return resultnaar_a.getIndexVertexCompound();}else{
00288 if(resultnaar_b.isFound()){ return resultnaar_b.getIndexVertexCompound();}else{
00289
00290 PiercingResult naar_a= getFarestPiercingTo_a(e,rico_normale,midden_index,midden_hoogte,max_delta_a);
00291 PiercingResult naar_b= getFarestPiercingTo_b(e,rico_normale,midden_index,midden_hoogte,max_delta_b);
00292 PiercingResult choice=getPiercingSmallestAbsVerticalOffset(naar_a,naar_b);
00293 Assert.assertTrue(obayPiercingConstraint(choice.getIndexVertexCompound().getVertex()));
00294 return choice.getIndexVertexCompound();
00295 }
00296 }
00297 }
00298
00299
00300 }
00301
00309 protected float getFunctionValue(int index, GenericEdge e){
00310 return Image.getInstance().getPuntOpEnty(e.getLoc(index)).getZ();
00311 }
00312
00313
00322 protected int max_delta_b(GenericEdge e,float rico_normale,int midden_index,float midden_hoogte,int max_delta){
00323
00324 float afstand_0=-midden_hoogte/rico_normale;
00325
00326 float afstand_255=(ByteConvertor.MAX_COLOR_VALUE-midden_hoogte)/rico_normale;
00327
00328 float max_afstand=Math.max(afstand_0,afstand_255);
00329 Assert.assertTrue(max_afstand>=0);
00330 int delta=max_delta;
00331 for(int d=0;d!=max_delta;d++){
00332 float afstand=e.getMidLoc().distance(e.getLoc(midden_index+d));
00333 if(afstand>max_afstand){delta=d-1; break;}
00334 }
00335 return delta;
00336 }
00337
00338
00357 protected int max_delta_a(GenericEdge e,float rico_normale,int midden_index,float midden_hoogte,int max_delta){
00358
00359 float afstand_0=-midden_hoogte/rico_normale;
00360
00361 float afstand_255=(ByteConvertor.MAX_COLOR_VALUE-midden_hoogte)/rico_normale;
00362
00363 float min_afstand=Math.min(afstand_0,afstand_255);
00364 Assert.assertTrue(min_afstand<=0);
00365 min_afstand=Math.abs(min_afstand);
00366 int delta=max_delta;
00367 for(int d=0;d!=max_delta;d++){
00368 float afstand=e.getMidLoc().distance(e.getLoc(midden_index-d));
00369 if(afstand>min_afstand){delta=d-1; break;}
00370 }
00371 return delta;
00372 }
00373
00374
00375
00385 protected PiercingResult getFarestPiercingTo_b(GenericEdge e,float rico_normale,int midden_index,float midden_hoogte,int max_delta){
00386 int delta=max_delta_b(e,rico_normale,midden_index,midden_hoogte,max_delta);
00387
00388 float afstand = e.getLoc(midden_index).distance(
00389 e.getLoc(midden_index + delta));
00390 float normale_waarde = midden_hoogte + afstand * rico_normale;
00391 int temp_index=midden_index + delta;
00392 Loc l=e.getLoc(temp_index);
00393 int verticalOffset=calculateVerticalOffset(normale_waarde,Image.getInstance().getPuntOpEnty(l).getZ());
00394 ParametrizedPiercingVertex piercing=new ParametrizedPiercingVertex(l,normale_waarde,verticalOffset,e.getNormalisationFactor(),e);
00395 Index_VertexCompound piercing_naar_b =new Index_VertexCompound(temp_index,piercing);
00396 PiercingResult piercingResult=new PiercingResult();
00397 piercingResult.setIndex_VertexCompound(piercing_naar_b,delta);
00398 return piercingResult;
00399
00400 }
00401
00402
00412 protected PiercingResult getFarestPiercingTo_a(GenericEdge e,float rico_normale,int midden_index,float midden_hoogte,int max_delta){
00413 int delta=max_delta_a(e,rico_normale,midden_index,midden_hoogte,max_delta);
00414
00415 float afstand = e.getLoc(midden_index).distance(
00416 e.getLoc(midden_index - delta));
00417 float normale_waarde = midden_hoogte - afstand * rico_normale;
00418 int temp_index=midden_index - delta;
00419 Loc l=e.getLoc(temp_index);
00420 int verticalOffset=calculateVerticalOffset(normale_waarde,Image.getInstance().getPuntOpEnty(l).getZ());
00421 ParametrizedPiercingVertex piercing=new ParametrizedPiercingVertex(l,normale_waarde,verticalOffset,e.getNormalisationFactor(),e);
00422 Index_VertexCompound piercing_naar_a =new Index_VertexCompound(temp_index,piercing);
00423 PiercingResult piercingResult=new PiercingResult();
00424 piercingResult.setIndex_VertexCompound(piercing_naar_a,delta);
00425 return piercingResult;
00426 }
00427
00428
00429
00430 protected boolean obayPiercingConstraint(Vertex piercing){
00431 return (Math.round(piercing.getZ())>=0 &&Math.round(piercing.getZ())<=ByteConvertor.MAX_COLOR_VALUE);
00432 }
00433
00434
00441 protected Index_VertexCompound getPiercingClosest(PiercingResult resultnaar_a,int max_delta_a, PiercingResult resultnaar_b, int max_delta_b){
00442
00443 if(resultnaar_a.getAantalPixelsVanafMidden()==max_delta_a-1 && resultnaar_b.getAantalPixelsVanafMidden()==max_delta_b-1){
00444 return getPiercingSmallestAbsVerticalOffset(resultnaar_a,resultnaar_b).getIndexVertexCompound();
00445 }
00446 if(resultnaar_a.getAantalPixelsVanafMidden()<resultnaar_b.getAantalPixelsVanafMidden()){
00447 return resultnaar_a.getIndexVertexCompound();
00448 }
00449 else{
00450 return resultnaar_b.getIndexVertexCompound();
00451 }
00452
00453
00454 }
00455
00463 protected PiercingResult choosePiercingPoint(PiercingResult resultBeforePiercing,PiercingResult resultAfter_Piercing,EncoderEdge edge){
00464 return getPiercingSmallestAbsVerticalOffset(resultBeforePiercing,resultAfter_Piercing);
00465 }
00466
00467
00474 protected PiercingResult getPiercingSmallestAbsVerticalOffset(PiercingResult resultnaar_a,PiercingResult resultnaar_b){
00475 boolean ensure= obayPiercingConstraint(resultnaar_a.getIndexVertexCompound().getVertex())|| obayPiercingConstraint(resultnaar_b.getIndexVertexCompound().getVertex());
00476 Assert.assertTrue(ensure);
00477
00478 if(!obayPiercingConstraint(resultnaar_a.getIndexVertexCompound().getVertex())){
00479 return resultnaar_b;
00480 }
00481
00482 if(!obayPiercingConstraint(resultnaar_b.getIndexVertexCompound().getVertex())){
00483 return resultnaar_a;
00484 }
00485
00486 if (Math.abs(resultnaar_a.getIndexVertexCompound().getVertex().getVerticalOffset())<
00487 Math.abs(resultnaar_b.getIndexVertexCompound().getVertex().getVerticalOffset()))
00488 {return resultnaar_a;}else{
00489 return resultnaar_b;
00490 }
00491 }
00492
00493
00494
00495
00496 protected EncoderEdge makeEdge(ParametrizedPiercingVertex begin, ParametrizedPiercingVertex eind, int[][] lijn) {
00497 Image image=Image.getInstance();
00498 if(begin.isOn(eind)){
00499
00500
00501 int[][] lijn_2 =new int[1][2];
00502 lijn_2[0][0]=begin.getX();
00503 lijn_2[0][1]=begin.getY();
00504 EncoderEdge degenereerd= new EncoderEdge(begin, eind, image,lijn_2,configuration.getMaxRico());
00505 return degenereerd;
00506 }
00507
00508 EncoderEdge edge=new EncoderEdge(begin, eind, image,lijn,configuration.getMaxRico());
00509 return edge;
00510 }
00511
00512
00513
00514
00515 }
00516
00517
00518