OSDN Git Service

4eb48085df66889f41bc67e56c372aea1b170d9d
[nyartoolkit-and/nyartoolkit-and.git] / trunk / src / jp / nyatla / nyartoolkit / core / squaredetect / NyARCoord2Linear.java
1 /* \r
2  * PROJECT: NyARToolkit\r
3  * --------------------------------------------------------------------------------\r
4  * This work is based on the original ARToolKit developed by\r
5  *   Hirokazu Kato\r
6  *   Mark Billinghurst\r
7  *   HITLab, University of Washington, Seattle\r
8  * http://www.hitl.washington.edu/artoolkit/\r
9  *\r
10  * The NyARToolkit is Java edition ARToolKit class library.\r
11  * Copyright (C)2008-2009 Ryo Iizuka\r
12  *\r
13  * This program is free software: you can redistribute it and/or modify\r
14  * it under the terms of the GNU General Public License as published by\r
15  * the Free Software Foundation, either version 3 of the License, or\r
16  * (at your option) any later version.\r
17  * \r
18  * This program is distributed in the hope that it will be useful,\r
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
21  * GNU General Public License for more details.\r
22  *\r
23  * You should have received a copy of the GNU General Public License\r
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
25  * \r
26  * For further information please contact.\r
27  *      http://nyatla.jp/nyatoolkit/\r
28  *      <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
29  * \r
30  */\r
31 package jp.nyatla.nyartoolkit.core.squaredetect;\r
32 \r
33 import jp.nyatla.nyartoolkit.NyARException;\r
34 import jp.nyatla.nyartoolkit.core.param.NyARCameraDistortionFactor;\r
35 import jp.nyatla.nyartoolkit.core.param.NyARObserv2IdealMap;\r
36 import jp.nyatla.nyartoolkit.core.pca2d.INyARPca2d;\r
37 import jp.nyatla.nyartoolkit.core.pca2d.NyARPca2d_MatrixPCA_O2;\r
38 import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d;\r
39 import jp.nyatla.nyartoolkit.core.types.NyARIntPoint2d;\r
40 import jp.nyatla.nyartoolkit.core.types.NyARIntSize;\r
41 import jp.nyatla.nyartoolkit.core.types.NyARLinear;\r
42 import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix22;\r
43 \r
44 \r
45 \r
46 /**\r
47  * 頂点集合を一次方程式のパラメータに変換します。\r
48  * \r
49  *\r
50  */\r
51 public class NyARCoord2Linear\r
52 {\r
53         private final double[] _xpos;\r
54         private final double[] _ypos;   \r
55         private final INyARPca2d _pca;\r
56         private final NyARDoubleMatrix22 __getSquareLine_evec=new NyARDoubleMatrix22();\r
57         private final double[] __getSquareLine_mean=new double[2];\r
58         private final double[] __getSquareLine_ev=new double[2];\r
59         private final NyARObserv2IdealMap _dist_factor;\r
60         public NyARCoord2Linear(NyARIntSize i_size,NyARCameraDistortionFactor i_distfactor_ref)\r
61         {\r
62                 //歪み計算テーブルを作ると、8*width/height*2の領域を消費します。\r
63                 //領域を取りたくない場合は、i_dist_factor_refの値をそのまま使ってください。\r
64                 this._dist_factor = new NyARObserv2IdealMap(i_distfactor_ref,i_size);\r
65 \r
66 \r
67                 // 輪郭バッファ\r
68                 this._pca=new NyARPca2d_MatrixPCA_O2();\r
69                 this._xpos=new double[i_size.w+i_size.h];//最大辺長はthis._width+this._height\r
70                 this._ypos=new double[i_size.w+i_size.h];//最大辺長はthis._width+this._height\r
71                 return;\r
72         }\r
73 \r
74 \r
75         /**\r
76          * 輪郭点集合からay+bx+c=0の直線式を計算します。\r
77          * @param i_st\r
78          * @param i_ed\r
79          * @param i_xcoord\r
80          * @param i_ycoord\r
81          * @param i_cood_num\r
82          * @param o_line\r
83          * @return\r
84          * @throws NyARException\r
85          */\r
86         public boolean coord2Line(int i_st,int i_ed,int[] i_xcoord, int[] i_ycoord,int i_cood_num, NyARLinear o_line) throws NyARException\r
87         {\r
88                 //頂点を取得\r
89                 int n,st,ed;\r
90                 double w1;\r
91         \r
92                 //探索区間の決定\r
93                 if(i_ed>=i_st){\r
94                         //頂点[i]から頂点[i+1]までの輪郭が、1区間にあるとき\r
95                         w1 = (double) (i_ed - i_st + 1) * 0.05 + 0.5;\r
96                         //探索区間の決定\r
97                         st = (int) (i_st+w1);\r
98                         ed = (int) (i_ed - w1);\r
99                 }else{\r
100                         //頂点[i]から頂点[i+1]までの輪郭が、2区間に分かれているとき\r
101                         w1 = (double) (i_ed+i_cood_num-i_st+1)%i_cood_num * 0.05 + 0.5;\r
102                         //探索区間の決定\r
103                         st = (int) (i_st+w1)%i_cood_num;\r
104                         ed = (int) (i_ed+i_cood_num-w1)%i_cood_num;\r
105                 }\r
106                 //探索区間数を確認\r
107                 if(st<=ed){\r
108                         //探索区間は1区間\r
109                         n = ed - st + 1;\r
110                         this._dist_factor.observ2IdealBatch(i_xcoord, i_ycoord, st, n,this._xpos,this._ypos,0);\r
111                 }else{\r
112                         //探索区間は2区間\r
113                         n=ed+1+i_cood_num-st;\r
114                         this._dist_factor.observ2IdealBatch(i_xcoord, i_ycoord, st,i_cood_num-st,this._xpos,this._ypos,0);\r
115                         this._dist_factor.observ2IdealBatch(i_xcoord, i_ycoord, 0,ed+1,this._xpos,this._ypos,i_cood_num-st);\r
116                 }\r
117                 //要素数の確認\r
118                 if (n < 2) {\r
119                         // nが2以下でmatrix.PCAを計算することはできないので、エラー\r
120                         return false;\r
121                 }\r
122                 //主成分分析する。\r
123                 final NyARDoubleMatrix22 evec=this.__getSquareLine_evec;\r
124                 final double[] mean=this.__getSquareLine_mean;\r
125 \r
126                 \r
127                 this._pca.pca(this._xpos,this._ypos,n,evec, this.__getSquareLine_ev,mean);\r
128                 o_line.dy = evec.m01;// line[i][0] = evec->m[1];\r
129                 o_line.dx = -evec.m00;// line[i][1] = -evec->m[0];\r
130                 o_line.c = -(o_line.dy * mean[0] + o_line.dx * mean[1]);// line[i][2] = -(line[i][0]*mean->v[0] + line[i][1]*mean->v[1]);\r
131 \r
132                 return true;\r
133         }\r
134 }