OSDN Git Service

[TAG]NyARToolkit/2.0.0
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.1.0 / src / jp / nyatla / nyartoolkit / core / transmat / NyARTransMat.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 version ARToolkit class library.\r
11  * Copyright (C)2008 R.Iizuka\r
12  *\r
13  * This program is free software; you can redistribute it and/or\r
14  * modify it under the terms of the GNU General Public License\r
15  * as published by the Free Software Foundation; either version 2\r
16  * of the License, or (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 framework; if not, write to the Free Software\r
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
26  * \r
27  * For further information please contact.\r
28  *      http://nyatla.jp/nyatoolkit/\r
29  *      <airmail(at)ebony.plala.or.jp>\r
30  * \r
31  */\r
32 package jp.nyatla.nyartoolkit.core.transmat;\r
33 \r
34 import jp.nyatla.nyartoolkit.NyARException;\r
35 import jp.nyatla.nyartoolkit.core.NyARSquare;\r
36 import jp.nyatla.nyartoolkit.core.param.*;\r
37 import jp.nyatla.nyartoolkit.core.transmat.fitveccalc.NyARFitVecCalculator;\r
38 import jp.nyatla.nyartoolkit.core.transmat.optimize.*;\r
39 import jp.nyatla.nyartoolkit.core.transmat.rotmatrix.*;\r
40 import jp.nyatla.nyartoolkit.core.types.*;\r
41 \r
42 \r
43 /**\r
44  * This class calculates ARMatrix from square information and holds it. --\r
45  * 変換行列を計算して、結果を保持するクラス。\r
46  * \r
47  */\r
48 public class NyARTransMat implements INyARTransMat\r
49 {\r
50         private final static double AR_GET_TRANS_CONT_MAT_MAX_FIT_ERROR = 1.0;\r
51 \r
52         private final NyARDoublePoint2d _center=new NyARDoublePoint2d(0,0);\r
53         private final NyARTransOffset _offset=new NyARTransOffset();\r
54         protected NyARRotMatrix _rotmatrix;\r
55         protected NyARFitVecCalculator _calculator;\r
56         protected INyARRotTransOptimize _mat_optimize;\r
57 \r
58         /**\r
59          * 派生クラスで自分でメンバオブジェクトを指定したい場合はこちらを使う。\r
60          *\r
61          */\r
62         protected NyARTransMat()\r
63         {\r
64                 //_calculator,_rotmatrix,_mat_optimizeをコンストラクタの終了後に\r
65                 //作成して割り当ててください。\r
66                 return;\r
67         }\r
68         public NyARTransMat(NyARParam i_param) throws NyARException\r
69         {\r
70                 final NyARCameraDistortionFactor dist=i_param.getDistortionFactor();\r
71                 final NyARPerspectiveProjectionMatrix pmat=i_param.getPerspectiveProjectionMatrix();\r
72                 this._calculator=new NyARFitVecCalculator(pmat,dist);\r
73                 this._rotmatrix = new NyARRotMatrix_ARToolKit(pmat);\r
74                 this._mat_optimize=new NyARRotTransOptimize_O2(pmat);\r
75         }\r
76 \r
77         public void setCenter(double i_x, double i_y)\r
78         {\r
79                 this._center.x= i_x;\r
80                 this._center.y= i_y;\r
81         }\r
82 \r
83 \r
84 \r
85 \r
86         /**\r
87          * 頂点順序をi_directionに対応して並べ替えます。\r
88          * @param i_square\r
89          * @param i_direction\r
90          * @param o_sqvertex_ref\r
91          * @param o_liner_ref\r
92          */\r
93         private final void initVertexOrder(NyARSquare i_square, int i_direction, NyARDoublePoint2d[] o_sqvertex_ref, NyARLinear[] o_liner_ref)\r
94         {\r
95                 //頂点順序を考慮した矩形の頂点情報\r
96                 o_sqvertex_ref[0]= i_square.sqvertex[(4 - i_direction) % 4];\r
97                 o_sqvertex_ref[1]= i_square.sqvertex[(5 - i_direction) % 4];\r
98                 o_sqvertex_ref[2]= i_square.sqvertex[(6 - i_direction) % 4];\r
99                 o_sqvertex_ref[3]= i_square.sqvertex[(7 - i_direction) % 4];    \r
100                 o_liner_ref[0]=i_square.line[(4 - i_direction) % 4];\r
101                 o_liner_ref[1]=i_square.line[(5 - i_direction) % 4];\r
102                 o_liner_ref[2]=i_square.line[(6 - i_direction) % 4];\r
103                 o_liner_ref[3]=i_square.line[(7 - i_direction) % 4];\r
104                 return;\r
105         }\r
106 \r
107 \r
108         private final NyARDoublePoint2d[] __transMat_sqvertex_ref = new NyARDoublePoint2d[4];\r
109         private final NyARLinear[] __transMat_linear_ref=new NyARLinear[4];\r
110         private final NyARDoublePoint3d __transMat_trans=new NyARDoublePoint3d();\r
111         /**\r
112          * double arGetTransMat( ARMarkerInfo *marker_info,double center[2], double width, double conv[3][4] )\r
113          * \r
114          * @param i_square\r
115          * 計算対象のNyARSquareオブジェクト\r
116          * @param i_direction\r
117          * @param i_width\r
118          * @return\r
119          * @throws NyARException\r
120          */\r
121         public void transMat(final NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult o_result_conv) throws NyARException\r
122         {\r
123                 final NyARDoublePoint2d[] sqvertex_ref = __transMat_sqvertex_ref;\r
124                 final NyARLinear[] linear_ref=__transMat_linear_ref;\r
125                 final NyARDoublePoint3d trans=this.__transMat_trans;\r
126                 \r
127                 //計算用に頂点情報を初期化(順番調整)\r
128                 initVertexOrder(i_square, i_direction, sqvertex_ref,linear_ref);\r
129                 \r
130                 //基準矩形を設定\r
131                 this._offset.setSquare(i_width,this._center);\r
132 \r
133                 // rotationを矩形情報から計算\r
134                 this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref);\r
135 \r
136                 //平行移動量計算機にオフセット頂点をセット\r
137                 this._calculator.setOffsetSquare(this._offset);\r
138                 \r
139                 //平行移動量計算機に適応先矩形の情報をセット\r
140                 this._calculator.setFittedSquare(sqvertex_ref); \r
141 \r
142                 //回転行列の平行移動量の計算\r
143                 this._calculator.calculateTransfer(this._rotmatrix,trans);\r
144                 \r
145                 //計算結果の最適化(this._rotmatrix,trans)\r
146                 this._mat_optimize.optimize(this._rotmatrix,trans,this._calculator);\r
147                 \r
148                 // マトリクスの保存\r
149                 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv);\r
150                 return;\r
151         }\r
152         /**\r
153          * double arGetTransMatCont( ARMarkerInfo *marker_info, double prev_conv[3][4],double center[2], double width, double conv[3][4] )\r
154          * \r
155          * @param i_square\r
156          * @param i_direction\r
157          * マーカーの方位を指定する。\r
158          * @param i_width\r
159          * @param io_result_conv\r
160          * 計算履歴を持つNyARTransMatResultオブジェクトを指定する。 履歴を持たない場合は、transMatと同じ処理を行う。\r
161          * @return\r
162          * @throws NyARException\r
163          */\r
164         public void transMatContinue(NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult io_result_conv) throws NyARException\r
165         {\r
166                 final NyARDoublePoint2d[] sqvertex_ref = __transMat_sqvertex_ref;\r
167                 final NyARLinear[] linear_ref=__transMat_linear_ref;\r
168                 final NyARDoublePoint3d trans=this.__transMat_trans;\r
169 \r
170                 // io_result_convが初期値なら、transMatで計算する。\r
171                 if (!io_result_conv.has_value) {\r
172                         this.transMat(i_square, i_direction, i_width, io_result_conv);\r
173                         return;\r
174                 }\r
175                 \r
176                 //基準矩形を設定\r
177                 this._offset.setSquare(i_width,this._center);\r
178 \r
179                 // rotationを矩形情報を一つ前の変換行列で初期化\r
180                 this._rotmatrix.initRotByPrevResult(io_result_conv);\r
181 \r
182                 //平行移動量計算機に、オフセット頂点をセット\r
183                 this._calculator.setOffsetSquare(this._offset);\r
184                 \r
185                 //平行移動量計算機に、適応先矩形の情報をセット\r
186                 this._calculator.setFittedSquare(sqvertex_ref); \r
187                                 \r
188                 //回転行列の平行移動量の計算\r
189                 this._calculator.calculateTransfer(this._rotmatrix,trans);\r
190                 \r
191                 //計算結果の最適化(this._rotmatrix,trans)\r
192                 final double err=this._mat_optimize.optimize(this._rotmatrix,trans,this._calculator);\r
193                 \r
194                 //計算結果を保存\r
195                 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,io_result_conv);\r
196 \r
197                 // エラー値が許容範囲でなければTransMatをやり直し\r
198                 if (err > AR_GET_TRANS_CONT_MAT_MAX_FIT_ERROR) {\r
199                         // rotationを矩形情報で初期化\r
200                         this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref);\r
201                         //回転行列の平行移動量の計算\r
202                         this._calculator.calculateTransfer(this._rotmatrix,trans);\r
203                         //計算結果の最適化(this._rotmatrix,trans)\r
204                         final double err2=this._mat_optimize.optimize(this._rotmatrix,trans,this._calculator);\r
205                         //エラー値が低かったら値を差換え\r
206                         if (err2 < err) {\r
207                                 // 良い値が取れたら、差換え\r
208                                 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,io_result_conv);\r
209                         }\r
210                 }\r
211                 return;\r
212         }\r
213         /**\r
214          * パラメータで変換行列を更新します。\r
215          * \r
216          * @param i_rot\r
217          * @param i_off\r
218          * @param i_trans\r
219          */\r
220         public void updateMatrixValue(NyARRotMatrix i_rot, NyARDoublePoint3d i_off, NyARDoublePoint3d i_trans,NyARTransMatResult o_result)\r
221         {\r
222                 o_result.m00=i_rot.m00;\r
223                 o_result.m01=i_rot.m01;\r
224                 o_result.m02=i_rot.m02;\r
225                 o_result.m03=i_rot.m00 * i_off.x + i_rot.m01 * i_off.y + i_rot.m02 * i_off.z + i_trans.x;\r
226 \r
227                 o_result.m10 = i_rot.m10;\r
228                 o_result.m11 = i_rot.m11;\r
229                 o_result.m12 = i_rot.m12;\r
230                 o_result.m13 = i_rot.m10 * i_off.x + i_rot.m11 * i_off.y + i_rot.m12 * i_off.z + i_trans.y;\r
231 \r
232                 o_result.m20 = i_rot.m20;\r
233                 o_result.m21 = i_rot.m21;\r
234                 o_result.m22 = i_rot.m22;\r
235                 o_result.m23 = i_rot.m20 * i_off.x + i_rot.m21 * i_off.y + i_rot.m22 * i_off.z + i_trans.z;\r
236 \r
237                 o_result.has_value = true;\r
238                 return;\r
239         }       \r
240 }\r