00001 package commandline.Encoder.dynamicProg;
00002
00003 import java.util.ArrayList;
00004 import java.util.HashSet;
00005
00006 import Jama.Matrix;
00007
00008 import commandline.ImageStuff.Image;
00009 import commandline.ImageStuff.Loc;
00010
00011
00018 public class District {
00019
00020
00021 protected ArrayList<Node> nodes;
00022 protected HashSet<Loc> locs;
00023 public District(Node n){
00024 this();
00025 this.addNode(n);
00026 }
00027
00028
00029 public District(){
00030 nodes=new ArrayList<Node>();
00031 this.locs=new HashSet<Loc>();
00032 }
00033
00038 public void addNode(Node node){
00039 if(!nodes.contains(node)){
00040 nodes.add(node);
00041 this.locs.addAll(node.getGeometricObject().getLocs());
00042 }
00043
00044 }
00045
00046 public District copy(){
00047 District d=new District();
00048 for(Node n:this.nodes){
00049 d.addNode(n);
00050 }
00051 return d;
00052 }
00053
00059 public boolean contains(Node node){
00060 return nodes.contains(node);
00061 }
00062
00063
00064
00065
00066
00067
00068
00069 public static double[] caculateLeastSquaresPlaneFitOf(HashSet<Loc> inputlocs){
00070
00071 int rows=inputlocs.size();
00072 int cols=3;
00073 Matrix z=new Matrix(rows,1);
00074 Matrix temp=new Matrix(rows,cols);
00075 int i=0;
00076 for (Loc l : inputlocs) {
00077 z.set(i, 0,Image.getInstance().getPuntOpEnty(l).getZ());
00078 temp.set(i, 0,l.getX());
00079 temp.set(i, 1,l.getY());
00080 temp.set(i, 2,1);
00081 i++;
00082 }
00083
00084 Matrix three_by_three= temp.transpose().times(temp);
00085
00086 three_by_three.print(2, 4);
00087 Matrix Right = temp.transpose().times(z);
00088
00089 if(three_by_three.rank()<3){
00090 System.out.println("matrix is singular: triangle -> degenerate");
00091 return null;
00092 }
00093
00094 Matrix ABC = three_by_three.solve(Right);
00095
00096 double A=ABC.get(0, 0);
00097 double B=ABC.get(1, 0);
00098 double C=ABC.get(2, 0);
00099 System.out.println("A B C:=" + A + " "+ B + " " + C);
00100
00101 return new double[]{A,B,C};
00102 }
00103
00104
00105
00106 public static double getDistortion(HashSet<Loc> inputlocs, double[] ABC){
00107 double distortion=0;
00108 for (Loc l : inputlocs) {
00109 distortion+=Math.pow(Image.getInstance().getPuntOpEnty(l).getZ()-getFunctionValueOfPlaneOn(ABC,l),2);
00110 }
00111 return distortion;
00112
00113 }
00114
00115
00116 public static float getFunctionValueOfPlaneOn(double[] ABC, Loc loc){
00117 double A = ABC[0];
00118 double B = ABC[1];
00119 double C = ABC[2];
00120 float z=(float)((A*loc.getX()+B*loc.getY())+ C);
00121 return z;
00122 }
00123
00124
00128 public HashSet<Loc> getLocs() {
00129 return locs;
00130 }
00131
00132 }