OSDN Git Service

[NyARToolKit for java]update document
[nyartoolkit-and/nyartoolkit-and.git] / lib / 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.NyARIntCoordinates;\r
39 import jp.nyatla.nyartoolkit.core.types.NyARIntSize;\r
40 import jp.nyatla.nyartoolkit.core.types.NyARLinear;\r
41 import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix22;\r
42 \r
43 \r
44 \r
45 /**\r
46  * このクラスは、座標配列を直線式に変換します。\r
47  * 座標配列の連続する要素を主成分分析にかけて、直線式にします。\r
48  */\r
49 public class NyARCoord2Linear\r
50 {\r
51         private final double[] _xpos;\r
52         private final double[] _ypos;   \r
53         private final INyARPca2d _pca;\r
54         private final NyARDoubleMatrix22 __getSquareLine_evec=new NyARDoubleMatrix22();\r
55         private final double[] __getSquareLine_mean=new double[2];\r
56         private final double[] __getSquareLine_ev=new double[2];\r
57         private final NyARObserv2IdealMap _dist_factor;\r
58         /**\r
59          * コンストラクタです。\r
60          * 輪郭取得元画像の歪み矯正オブジェクトとサイズを指定して、インスタンスを生成します。\r
61          * @param i_size\r
62          * 入力画像のサイズ\r
63          * @param i_distfactor_ref\r
64          * 樽型歪みを補正する場合に、オブジェクトを指定します。\r
65          * nullの場合、補正を行いません。\r
66          */\r
67         public NyARCoord2Linear(NyARIntSize i_size,NyARCameraDistortionFactor i_distfactor)\r
68         {\r
69                 if(i_distfactor!=null){\r
70                         this._dist_factor = new NyARObserv2IdealMap(i_distfactor,i_size);\r
71                 }else{\r
72                         this._dist_factor=null;\r
73                 }\r
74                 // 輪郭バッファ\r
75                 this._pca=new NyARPca2d_MatrixPCA_O2();\r
76                 this._xpos=new double[i_size.w+i_size.h];//最大辺長はthis._width+this._height\r
77                 this._ypos=new double[i_size.w+i_size.h];//最大辺長はthis._width+this._height\r
78                 return;\r
79         }\r
80 \r
81 \r
82         /**\r
83          * この関数は、輪郭点集合からay+bx+c=0の直線式を計算します。\r
84          * @param i_st\r
85          * 直線計算の対象とする、輪郭点の開始インデックス\r
86          * @param i_ed\r
87          * 直線計算の対象とする、輪郭点の終了インデックス\r
88          * @param i_coord\r
89          * 輪郭点集合のオブジェクト。\r
90          * @param o_line\r
91          * 直線式を受け取るオブジェクト\r
92          * @return\r
93          * 直線式の計算に成功すると、trueを返します。\r
94          * @throws NyARException\r
95          */\r
96         public boolean coord2Line(int i_st,int i_ed,NyARIntCoordinates i_coord, NyARLinear o_line) throws NyARException\r
97         {\r
98                 //頂点を取得\r
99                 int n,st,ed;\r
100                 double w1;\r
101                 int cood_num=i_coord.length;\r
102         \r
103                 //探索区間の決定\r
104                 if(i_ed>=i_st){\r
105                         //頂点[i]から頂点[i+1]までの輪郭が、1区間にあるとき\r
106                         w1 = (double) (i_ed - i_st + 1) * 0.05 + 0.5;\r
107                         //探索区間の決定\r
108                         st = (int) (i_st+w1);\r
109                         ed = (int) (i_ed - w1);\r
110                 }else{\r
111                         //頂点[i]から頂点[i+1]までの輪郭が、2区間に分かれているとき\r
112                         w1 = (double)((i_ed+cood_num-i_st+1)%cood_num) * 0.05 + 0.5;\r
113                         //探索区間の決定\r
114                         st = ((int) (i_st+w1))%cood_num;\r
115                         ed = ((int) (i_ed+cood_num-w1))%cood_num;\r
116                 }\r
117                 //探索区間数を確認\r
118                 if(st<=ed){\r
119                         //探索区間は1区間\r
120                         n = ed - st + 1;\r
121                         if(this._dist_factor!=null){\r
122                                 this._dist_factor.observ2IdealBatch(i_coord.items, st, n,this._xpos,this._ypos,0);\r
123                         }\r
124                 }else{\r
125                         //探索区間は2区間\r
126                         n=ed+1+cood_num-st;\r
127                         if(this._dist_factor!=null){\r
128                                 this._dist_factor.observ2IdealBatch(i_coord.items, st,cood_num-st,this._xpos,this._ypos,0);\r
129                                 this._dist_factor.observ2IdealBatch(i_coord.items, 0,ed+1,this._xpos,this._ypos,cood_num-st);\r
130                         }\r
131                 }\r
132                 //要素数の確認\r
133                 if (n < 2) {\r
134                         // nが2以下でmatrix.PCAを計算することはできないので、エラー\r
135                         return false;\r
136                 }\r
137                 //主成分分析する。\r
138                 final NyARDoubleMatrix22 evec=this.__getSquareLine_evec;\r
139                 final double[] mean=this.__getSquareLine_mean;\r
140 \r
141                 \r
142                 this._pca.pca(this._xpos,this._ypos,n,evec, this.__getSquareLine_ev,mean);\r
143                 o_line.a = evec.m01;// line[i][0] = evec->m[1];\r
144                 o_line.b = -evec.m00;// line[i][1] = -evec->m[0];\r
145                 o_line.c = -(o_line.a * mean[0] + o_line.b * mean[1]);// line[i][2] = -(line[i][0]*mean->v[0] + line[i][1]*mean->v[1]);\r
146 \r
147                 return true;\r
148         }\r
149 }