00001 package commandline.Encoder.dynamicProg; 00002 00003 import java.util.ArrayList; 00004 00005 import commandline.ImageStuff.BreathFirstVisitor; 00006 import commandline.ImageStuff.HierachicalInterFace; 00007 import commandline.ImageStuff.Image; 00008 import commandline.ImageStuff.LeavesCollector; 00009 import commandline.ImageStuff.GeometricObjects.GenericEdge; 00010 import commandline.ImageStuff.GeometricObjects.GeometricObject; 00011 import commandline.Norm.NormenMaker; 00012 00019 public class Node implements HierachicalInterFace<Node>{ 00020 protected GeometricObject geometricObject; 00021 protected boolean isPruned; 00022 private int level; 00023 ArrayList<Node> children; 00024 private Node parent; 00025 00026 00027 00028 00029 00030 public Node(GeometricObject geometricObject,int level){ 00031 if(this.geometricObject!=null){ 00032 this.isPruned=!geometricObject.hasChildren(); 00033 }else{this.isPruned=false;} 00034 this.level=level; 00035 this.geometricObject=geometricObject; 00036 this.children=new ArrayList<Node>(); 00037 00038 } 00039 00040 00041 /* 00042 * adds a child node to navigate in the tree 00043 */ 00044 00045 public void addChildNode(Node child){ 00046 00047 //System.out.println(this.toString() + " adding child node"); 00048 this.children.add(child); 00049 } 00050 00051 00052 00053 /* 00054 * Adds the parent node to navigate in the tree 00055 */ 00056 public void addParentNode(Node parent){ 00057 this.parent=parent; 00058 //System.out.println(this.toString()+ " adding parent node"); 00059 } 00060 00061 public Node getParent(){ 00062 return this.parent; 00063 } 00064 00065 public boolean hasParent(){ 00066 return !(this.parent==null); 00067 } 00068 00073 public boolean hasChildren(){ 00074 //System.out.println("hasChildren...?"); 00075 00076 return !children.isEmpty() && !isPruned(); 00077 } 00078 00083 public ArrayList<Node> getChildren(){ 00084 if(!isPruned()) return children; 00085 else { 00086 return null; 00087 } 00088 } 00089 00096 public Node getChildCoupledtoGeomObj(GeometricObject geometricObject){ 00097 for (Node child : children) { 00098 if(child.getGeometricObject()==geometricObject){ 00099 return child;} 00100 } 00101 return null; 00102 } 00103 00104 00105 public GeometricObject getGeometricObject() { 00106 return geometricObject; 00107 } 00108 00109 00113 public boolean isPruned() { 00114 return isPruned; 00115 } 00116 00117 00122 public void setPruned(boolean isPruned) { 00123 this.isPruned = isPruned; 00124 } 00125 00126 00127 00128 public int getLevel() { 00129 return level; 00130 } 00131 00132 00133 /************** RATE/COST= AFFINE COST FUNCTIONAL 00134 ******* INVOLVES COST OF ALL INTERMEDIATE NODES *************/ 00135 00136 00137 00138 00143 protected float getOneLevelBitBudget(){ 00144 float bits=0; 00145 if(this.isPruned || !hasChildren()) return 0; 00146 for (Node node : this.getChildren()) { 00147 if (node.getGeometricObject().isAnEdge()) { 00148 bits+=1f/3f; // edge 00149 } else { 00150 bits+= 1; // triangle 00151 } 00152 } 00153 return bits; 00154 } 00155 00156 00157 00158 public float getCostOfSubTree(){ 00159 float accumulated_cost=0; 00160 BreathFirstVisitor<Node> breathFirstVisitor=new BreathFirstVisitor<Node>(this,new ArrayList<Node>()); 00161 while (breathFirstVisitor.hasNext()) { 00162 Node n= breathFirstVisitor.next(); 00163 accumulated_cost+=getOneLevelBitBudget(); 00164 } 00165 return accumulated_cost; 00166 } 00167 00168 00169 00170 00171 00172 00173 00177 public void reset(){ 00178 setPruned(false); 00179 } 00180 00181 00182 00183 /**************ERROR = linear tree functional **** 00184 **************ONLY DIFFERENCE BETWEEN LEAVES ****/ 00185 00186 00191 public float getDeltaSubtreeError(int WHICHNORM){ 00192 return getErrorBetweenThisTriangleAndTheSurface(WHICHNORM)-getErrorDifferenceOfThisAndLeaveNodes(WHICHNORM); 00193 } 00194 00195 00200 protected float getErrorBetweenThisTriangleAndTheSurface(int WHICHNORM){ 00201 return this.getGeometricObject().getError(Image.getInstance()).getNorm(WHICHNORM); 00202 } 00203 00204 00205 00212 protected float getErrorDifferenceOfThisAndLeaveNodes(int WHICHNORM){ 00213 float error=0; 00214 for(Node leave:LeavesCollector.getLeavesFromNode(this)){ 00215 error+=leave.getGeometricObject().getError(Image.getInstance()).getNorm(WHICHNORM); 00216 } 00217 return (error-getRedundantLeavesBorderError(WHICHNORM)); 00218 } 00219 00220 00221 00227 protected float getRedundantLeavesBorderError(int WHICH_NORM){ 00228 ArrayList<GenericEdge> redundantEdges=new ArrayList<GenericEdge>(6); 00229 for(Node leave:LeavesCollector.getLeavesFromNode(this)){ 00230 for(int i=0;i!=leave.getGeometricObject().getNbrOfEdges();i++){ 00231 redundantEdges.add(leave.getGeometricObject().getEdge(i)); 00232 } 00233 00234 } 00235 00236 float cost=0; 00237 for(GenericEdge edge:redundantEdges){ 00238 cost+=edge.getError(Image.getInstance()).getNorm(WHICH_NORM); 00239 } 00240 return cost; 00241 00242 } 00243 00244 }
1.5.5