OSDN Git Service

[Backup]NyARToolkit[Backup]NyARToolkit for Java
[nyartoolkit-and/nyartoolkit-and.git] / trunk / src / jp / nyatla / nyartoolkit / core / transmat / NyARTransMat.java
1 /* \r
2  * PROJECT: NyARToolkit (Extension)\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 modify\r
14  * it under the terms of the GNU Lesser General Public License as \r
15  * published by the Free Software Foundation; either version 3 of the\r
16  * 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.\r
21  * See the GNU Lesser General Public License for more details.\r
22  *\r
23  * You should have received a copy of the GNU Lesser General Public \r
24  * License 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/\r
28  *      <airmail(at)ebony.plala.or.jp>\r
29  */\r
30 package jp.nyatla.nyartoolkit.core.transmat;\r
31 \r
32 import jp.nyatla.nyartoolkit.NyARException;\r
33 import jp.nyatla.nyartoolkit.core.param.*;\r
34 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare;\r
35 import jp.nyatla.nyartoolkit.core.transmat.solver.*;\r
36 import jp.nyatla.nyartoolkit.core.transmat.optimize.*;\r
37 import jp.nyatla.nyartoolkit.core.transmat.rotmatrix.*;\r
38 import jp.nyatla.nyartoolkit.core.types.*;\r
39 import jp.nyatla.nyartoolkit.core.types.matrix.*;\r
40 \r
41 /**\r
42  * This class calculates ARMatrix from square information and holds it. --\r
43  * 変換行列を計算して、結果を保持するクラス。\r
44  * \r
45  */\r
46 public class NyARTransMat implements INyARTransMat\r
47 {\r
48         private final static double FIT_DIFF_THRESHOLD = 0.01;\r
49         private final static double FIT_DIFF_THRESHOLD_CONT = 1.0;\r
50 \r
51         private final NyARDoublePoint2d _center=new NyARDoublePoint2d(0,0);\r
52         private final NyARTransOffset _offset=new NyARTransOffset();\r
53         private NyARPerspectiveProjectionMatrix _projection_mat_ref;\r
54         protected NyARRotMatrix _rotmatrix;\r
55         protected INyARTransportVectorSolver _transsolver;\r
56         protected NyARPartialDifferentiationOptimize _mat_optimize;\r
57 \r
58 \r
59         private NyARCameraDistortionFactor _ref_dist_factor;\r
60 \r
61         /**\r
62          * 派生クラスで自分でメンバオブジェクトを指定したい場合はこちらを使う。\r
63          *\r
64          */\r
65         protected NyARTransMat()\r
66         {\r
67                 //_calculator,_rotmatrix,_mat_optimizeをコンストラクタの終了後に\r
68                 //作成して割り当ててください。\r
69                 return;\r
70         }\r
71         public NyARTransMat(NyARParam i_param) throws NyARException\r
72         {\r
73                 final NyARCameraDistortionFactor dist=i_param.getDistortionFactor();\r
74                 final NyARPerspectiveProjectionMatrix pmat=i_param.getPerspectiveProjectionMatrix();\r
75                 this._transsolver=new NyARTransportVectorSolver(pmat,4);\r
76                 //互換性が重要な時は、NyARRotMatrix_ARToolKitを使うこと。\r
77                 //理屈はNyARRotMatrix_NyARToolKitもNyARRotMatrix_ARToolKitも同じだけど、少しだけ値がずれる。\r
78                 this._rotmatrix = new NyARRotMatrix(pmat);\r
79                 this._mat_optimize=new NyARPartialDifferentiationOptimize(pmat);\r
80                 this._ref_dist_factor=dist;\r
81                 this._projection_mat_ref=pmat;\r
82         }\r
83 \r
84         public void setCenter(double i_x, double i_y)\r
85         {\r
86                 this._center.x= i_x;\r
87                 this._center.y= i_y;\r
88         }\r
89 \r
90 \r
91 \r
92 \r
93         /**\r
94          * 頂点順序をi_directionに対応して並べ替えます。\r
95          * @param i_square\r
96          * @param i_direction\r
97          * @param o_sqvertex_ref\r
98          * @param o_liner_ref\r
99          */\r
100         private final void initVertexOrder(NyARSquare i_square, int i_direction, NyARDoublePoint2d[] o_sqvertex_ref, NyARLinear[] o_liner_ref)\r
101         {\r
102                 //頂点順序を考慮した矩形の頂点情報\r
103                 o_sqvertex_ref[0]= i_square.sqvertex[(4 - i_direction) % 4];\r
104                 o_sqvertex_ref[1]= i_square.sqvertex[(5 - i_direction) % 4];\r
105                 o_sqvertex_ref[2]= i_square.sqvertex[(6 - i_direction) % 4];\r
106                 o_sqvertex_ref[3]= i_square.sqvertex[(7 - i_direction) % 4];    \r
107                 o_liner_ref[0]=i_square.line[(4 - i_direction) % 4];\r
108                 o_liner_ref[1]=i_square.line[(5 - i_direction) % 4];\r
109                 o_liner_ref[2]=i_square.line[(6 - i_direction) % 4];\r
110                 o_liner_ref[3]=i_square.line[(7 - i_direction) % 4];\r
111                 return;\r
112         }\r
113 \r
114 \r
115         private final NyARDoublePoint2d[] __transMat_sqvertex_ref = new NyARDoublePoint2d[4];\r
116         private final NyARDoublePoint2d[] __transMat_vertex_2d = NyARDoublePoint2d.createArray(4);\r
117         private final NyARDoublePoint3d[] __transMat_vertex_3d = NyARDoublePoint3d.createArray(4);\r
118         private final NyARLinear[] __transMat_linear_ref=new NyARLinear[4];\r
119         private final NyARDoublePoint3d __transMat_trans=new NyARDoublePoint3d();\r
120         /**\r
121          * double arGetTransMat( ARMarkerInfo *marker_info,double center[2], double width, double conv[3][4] )\r
122          * \r
123          * @param i_square\r
124          * 計算対象のNyARSquareオブジェクト\r
125          * @param i_direction\r
126          * @param i_width\r
127          * @return\r
128          * @throws NyARException\r
129          */\r
130         public void transMat(final NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult o_result_conv) throws NyARException\r
131         {\r
132                 final NyARDoublePoint2d[] sqvertex_ref = __transMat_sqvertex_ref;\r
133                 final NyARLinear[] linear_ref=__transMat_linear_ref;\r
134                 final NyARDoublePoint3d trans=this.__transMat_trans;\r
135                 \r
136                 //計算用に頂点情報を初期化(順番調整)\r
137                 initVertexOrder(i_square, i_direction, sqvertex_ref,linear_ref);\r
138                 \r
139                 //平行移動量計算機に、2D座標系をセット\r
140                 NyARDoublePoint2d[] vertex_2d=this.__transMat_vertex_2d;\r
141                 NyARDoublePoint3d[] vertex_3d=this.__transMat_vertex_3d;\r
142                 this._ref_dist_factor.ideal2ObservBatch(sqvertex_ref, vertex_2d,4);             \r
143                 this._transsolver.set2dVertex(vertex_2d,4);\r
144                 \r
145                 //基準矩形の3D座標系を作成\r
146                 this._offset.setSquare(i_width,this._center);\r
147 \r
148                 //回転行列を計算\r
149                 this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref);\r
150                 \r
151                 //回転後の3D座標系から、平行移動量を計算\r
152                 this._rotmatrix.getPoint3dBatch(this._offset.vertex,vertex_3d,4);\r
153                 this._transsolver.solveTransportVector(vertex_3d,trans);\r
154                 \r
155                 //計算結果の最適化(平行移動量と回転行列の最適化)\r
156                 this.optimize(this._rotmatrix, trans, this._transsolver,this._offset.vertex, vertex_2d);\r
157                 \r
158                 // マトリクスの保存\r
159                 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv);\r
160                 return;\r
161         }\r
162 \r
163         /*\r
164          * (non-Javadoc)\r
165          * @see jp.nyatla.nyartoolkit.core.transmat.INyARTransMat#transMatContinue(jp.nyatla.nyartoolkit.core.NyARSquare, int, double, jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult)\r
166          */\r
167         public void transMatContinue(NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult o_result_conv) throws NyARException\r
168         {\r
169                 final NyARDoublePoint2d[] sqvertex_ref = __transMat_sqvertex_ref;\r
170                 final NyARLinear[] linear_ref=__transMat_linear_ref;\r
171                 final NyARDoublePoint3d trans=this.__transMat_trans;\r
172 \r
173                 // io_result_convが初期値なら、transMatで計算する。\r
174                 if (!o_result_conv.has_value) {\r
175                         this.transMat(i_square, i_direction, i_width, o_result_conv);\r
176                         return;\r
177                 }\r
178 \r
179                 //計算用に頂点情報を初期化(順番調整)\r
180                 initVertexOrder(i_square, i_direction, sqvertex_ref,linear_ref);\r
181 \r
182                 \r
183                 //平行移動量計算機に、2D座標系をセット\r
184                 NyARDoublePoint2d[] vertex_2d=this.__transMat_vertex_2d;\r
185                 NyARDoublePoint3d[] vertex_3d=this.__transMat_vertex_3d;\r
186                 this._ref_dist_factor.ideal2ObservBatch(sqvertex_ref, vertex_2d,4);             \r
187                 this._transsolver.set2dVertex(vertex_2d,4);\r
188                 \r
189                 //基準矩形の3D座標系を作成\r
190                 this._offset.setSquare(i_width,this._center);\r
191 \r
192                 //回転行列を計算\r
193                 this._rotmatrix.initRotByPrevResult(o_result_conv);\r
194                 \r
195                 //回転後の3D座標系から、平行移動量を計算\r
196                 this._rotmatrix.getPoint3dBatch(this._offset.vertex,vertex_3d,4);\r
197                 this._transsolver.solveTransportVector(vertex_3d,trans);\r
198 \r
199                 //現在のエラーレートを計算しておく\r
200                 double min_err=errRate(this._rotmatrix,trans, this._offset.vertex, vertex_2d,4,vertex_3d);\r
201                 NyARDoubleMatrix33 rot=this.__rot;\r
202                 //エラーレートが閾値超えてたらアゲイン\r
203                 if(min_err<FIT_DIFF_THRESHOLD_CONT){\r
204                         rot.setValue(this._rotmatrix);\r
205                         //最適化してみる。\r
206                         for (int i = 0;i<5; i++) {\r
207                                 //変換行列の最適化\r
208                                 this._mat_optimize.modifyMatrix(rot, trans, this._offset.vertex, vertex_2d, 4);\r
209                                 double err=errRate(rot,trans,this._offset.vertex, vertex_2d,4,vertex_3d);\r
210                                 //System.out.println("E:"+err);\r
211                                 if(min_err-err<FIT_DIFF_THRESHOLD){\r
212                                         //System.out.println("BREAK");\r
213                                         break;\r
214                                 }\r
215                                 this._transsolver.solveTransportVector(vertex_3d, trans);\r
216                                 this._rotmatrix.setValue(rot);\r
217                                 min_err=err;\r
218                         }\r
219                         this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv);\r
220                 }else{\r
221                         //回転行列を計算\r
222                         this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref);\r
223                         \r
224                         //回転後の3D座標系から、平行移動量を計算\r
225                         this._rotmatrix.getPoint3dBatch(this._offset.vertex,vertex_3d,4);\r
226                         this._transsolver.solveTransportVector(vertex_3d,trans);\r
227                         \r
228                         //計算結果の最適化(平行移動量と回転行列の最適化)\r
229                         this.optimize(this._rotmatrix, trans, this._transsolver,this._offset.vertex, vertex_2d);\r
230                         this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv);\r
231                 }\r
232                 return;\r
233         }\r
234         private NyARDoubleMatrix33 __rot=new NyARDoubleMatrix33();\r
235         private double optimize(NyARRotMatrix io_rotmat,NyARDoublePoint3d io_transvec,INyARTransportVectorSolver i_solver,NyARDoublePoint3d[] i_offset_3d,NyARDoublePoint2d[] i_2d_vertex) throws NyARException\r
236         {\r
237                 //System.out.println("START");\r
238                 NyARDoublePoint3d[] vertex_3d=this.__transMat_vertex_3d;\r
239                 //初期のエラー値を計算\r
240                 double min_err=errRate(io_rotmat, io_transvec, i_offset_3d, i_2d_vertex,4,vertex_3d);\r
241                 NyARDoubleMatrix33 rot=this.__rot;\r
242                 rot.setValue(io_rotmat);\r
243                 for (int i = 0;i<5; i++) {\r
244                         //変換行列の最適化\r
245                         this._mat_optimize.modifyMatrix(rot, io_transvec, i_offset_3d, i_2d_vertex, 4);\r
246                         double err=errRate(rot,io_transvec, i_offset_3d, i_2d_vertex,4,vertex_3d);\r
247                         //System.out.println("E:"+err);\r
248                         if(min_err-err<FIT_DIFF_THRESHOLD){\r
249                                 //System.out.println("BREAK");\r
250                                 break;\r
251                         }\r
252                         i_solver.solveTransportVector(vertex_3d, io_transvec);\r
253                         io_rotmat.setValue(rot);\r
254                         min_err=err;\r
255                 }\r
256                 //System.out.println("END");\r
257                 return min_err;\r
258         }\r
259         \r
260         //エラーレート計算機\r
261         public double errRate(NyARDoubleMatrix33 io_rot,NyARDoublePoint3d i_trans, NyARDoublePoint3d[] i_vertex3d, NyARDoublePoint2d[] i_vertex2d,int i_number_of_vertex,NyARDoublePoint3d[] o_rot_vertex) throws NyARException\r
262         {\r
263                 NyARPerspectiveProjectionMatrix cp = this._projection_mat_ref;\r
264                 final double cp00=cp.m00;\r
265                 final double cp01=cp.m01;\r
266                 final double cp02=cp.m02;\r
267                 final double cp11=cp.m11;\r
268                 final double cp12=cp.m12;\r
269 \r
270                 double err=0;\r
271                 for(int i=0;i<i_number_of_vertex;i++){\r
272                         double x3d,y3d,z3d;\r
273                         o_rot_vertex[i].x=x3d=io_rot.m00*i_vertex3d[i].x+io_rot.m01*i_vertex3d[i].y+io_rot.m02*i_vertex3d[i].z;\r
274                         o_rot_vertex[i].y=y3d=io_rot.m10*i_vertex3d[i].x+io_rot.m11*i_vertex3d[i].y+io_rot.m12*i_vertex3d[i].z;\r
275                         o_rot_vertex[i].z=z3d=io_rot.m20*i_vertex3d[i].x+io_rot.m21*i_vertex3d[i].y+io_rot.m22*i_vertex3d[i].z;\r
276                         x3d+=i_trans.x;\r
277                         y3d+=i_trans.y;\r
278                         z3d+=i_trans.z;\r
279                         \r
280                         //射影変換\r
281                         double x2d=x3d*cp00+y3d*cp01+z3d*cp02;\r
282                         double y2d=y3d*cp11+z3d*cp12;\r
283                         double h2d=z3d;\r
284                         \r
285                         //エラーレート計算\r
286                         double t1=i_vertex2d[i].x-x2d/h2d;\r
287                         double t2=i_vertex2d[i].y-y2d/h2d;\r
288                         err+=t1*t1+t2*t2;\r
289                         \r
290                 }\r
291                 return err/i_number_of_vertex;\r
292         }               \r
293         \r
294         \r
295         \r
296         /**\r
297          * パラメータで変換行列を更新します。\r
298          * \r
299          * @param i_rot\r
300          * @param i_off\r
301          * @param i_trans\r
302          */\r
303         public void updateMatrixValue(NyARRotMatrix i_rot, NyARDoublePoint3d i_off, NyARDoublePoint3d i_trans,NyARTransMatResult o_result)\r
304         {\r
305                 o_result.m00=i_rot.m00;\r
306                 o_result.m01=i_rot.m01;\r
307                 o_result.m02=i_rot.m02;\r
308                 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
309 \r
310                 o_result.m10 = i_rot.m10;\r
311                 o_result.m11 = i_rot.m11;\r
312                 o_result.m12 = i_rot.m12;\r
313                 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
314 \r
315                 o_result.m20 = i_rot.m20;\r
316                 o_result.m21 = i_rot.m21;\r
317                 o_result.m22 = i_rot.m22;\r
318                 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
319 \r
320                 o_result.has_value = true;\r
321                 return;\r
322         }       \r
323 }\r