00001 package commandline.Encoder.dynamicProg; 00002 00003 import java.util.ArrayList; 00004 00005 import commandline.ImageStuff.LeavesCollector; 00006 00007 public class JoineAlgorithm { 00008 00009 protected ITree tree; 00010 protected ArrayList<District> districs; 00011 public JoineAlgorithm(ITree tree){ 00012 this.tree=tree; 00013 this.districs=new ArrayList<District>(); 00014 } 00015 00016 public void join(){ 00017 // get all leave nodes 00018 ArrayList<Node> leaves= LeavesCollector.getLeavesFromNode(tree.getRootNode()); 00019 // for each leaveNode you encounter, decide with which leaves it CAN be joined 00020 00021 while(!leaves.isEmpty()){ 00022 Node leaveNode=leaves.remove(0); 00023 District districtLeaveNode= makeNewDistrictIfNeeded(leaveNode); 00024 for (Node adjacentNode : getAdjacentNodes(leaveNode, leaves)) { 00025 // calculate if it would be better to add it to the district 00026 } 00027 00028 } 00029 00030 // whether it SHOULD be joined with those leaves 00031 00032 00033 } 00034 00035 00036 00037 public void addtoDistrict(District district, Node n){ 00038 District merge=district.copy(); 00039 merge.addNode(n); 00040 if(allreadyPartOfDistrict(n)!=null){ 00041 double distortionbefore_district= 00042 District.getDistortion(district.getLocs(), 00043 District.caculateLeastSquaresPlaneFitOf(district.getLocs())); 00044 double distortionbefore_node= 00045 District.getDistortion(n.getGeometricObject().getLocs(), 00046 District.caculateLeastSquaresPlaneFitOf(n.getGeometricObject().getLocs())); 00047 00048 double distortionafter= 00049 District.getDistortion(merge.getLocs(), 00050 District.caculateLeastSquaresPlaneFitOf(merge.getLocs())); 00051 } 00052 00053 } 00054 00058 private ArrayList<Node> getAdjacentNodes(Node n,ArrayList<Node> others){ 00059 ArrayList<Node> adjacentNodes=new ArrayList<Node>(); 00060 for (Node node : others) { 00061 if(node.getGeometricObject().hasPartiallyEdgeInCommon(n.getGeometricObject())){ 00062 adjacentNodes.add(node); 00063 } 00064 } 00065 return adjacentNodes; 00066 } 00067 00068 00073 private District makeNewDistrictIfNeeded(Node n){ 00074 District district= allreadyPartOfDistrict(n); 00075 if(district==null){ 00076 district=new District(n); 00077 districs.add(district); 00078 } 00079 return district; 00080 } 00081 00089 public District allreadyPartOfDistrict(Node n){ 00090 for (District district : districs) { 00091 if(district.contains(n)) return district; 00092 } 00093 return null; 00094 } 00095 00096 00097 00098 00099 00100 00101 00102 } 00103 00104
1.5.5