OSDN Git Service

639e0c0803b4a598bfdc257a15cb16f4909d3a1b
[nyartoolkit-and/nyartoolkit-and.git] / src / jp / nyatla / nyartoolkit / core / transmat / NyARTransMat_ARToolKit.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\r
14  * modify it under the terms of the GNU Lesser General Public License\r
15  * as published by the Free Software Foundation; either version 3\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 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/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.transmat;\r
32 \r
33 import jp.nyatla.nyartoolkit.NyARException;\r
34 import jp.nyatla.nyartoolkit.core.param.*;\r
35 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare;\r
36 import jp.nyatla.nyartoolkit.core.transmat.solver.*;\r
37 import jp.nyatla.nyartoolkit.core.transmat.optimize.*;\r
38 import jp.nyatla.nyartoolkit.core.transmat.optimize.artoolkit.INyARRotMatrixOptimize;\r
39 import jp.nyatla.nyartoolkit.core.transmat.optimize.artoolkit.NyARRotMatrixOptimize_O2;\r
40 import jp.nyatla.nyartoolkit.core.transmat.rotmatrix.*;\r
41 import jp.nyatla.nyartoolkit.core.types.*;\r
42 \r
43 \r
44 /**\r
45  * This class calculates ARMatrix from square information and holds it. --\r
46  * 変換行列を計算して、結果を保持するクラス。\r
47  * \r
48  */\r
49 public class NyARTransMat_ARToolKit implements INyARTransMat\r
50 {\r
51         private final static int AR_GET_TRANS_MAT_MAX_LOOP_COUNT = 5;// #define AR_GET_TRANS_MAT_MAX_LOOP_COUNT 5\r
52         private final static double AR_GET_TRANS_MAT_MAX_FIT_ERROR = 1.0;// #define AR_GET_TRANS_MAT_MAX_FIT_ERROR 1.0\r
53         private final static double AR_GET_TRANS_CONT_MAT_MAX_FIT_ERROR = 1.0;\r
54 \r
55         private final NyARDoublePoint2d _center=new NyARDoublePoint2d(0,0);\r
56         private final NyARTransOffset _offset=new NyARTransOffset();\r
57         protected NyARRotMatrix_ARToolKit _rotmatrix;\r
58         protected INyARTransportVectorSolver _transsolver;\r
59         protected INyARRotMatrixOptimize _mat_optimize;\r
60         private NyARCameraDistortionFactor _ref_dist_factor;\r
61 \r
62         /**\r
63          * 派生クラスで自分でメンバオブジェクトを指定したい場合はこちらを使う。\r
64          *\r
65          */\r
66         protected NyARTransMat_ARToolKit()\r
67         {\r
68                 //_calculator,_rotmatrix,_mat_optimizeをコンストラクタの終了後に\r
69                 //作成して割り当ててください。\r
70                 return;\r
71         }\r
72         public NyARTransMat_ARToolKit(NyARParam i_param) throws NyARException\r
73         {\r
74                 final NyARCameraDistortionFactor dist=i_param.getDistortionFactor();\r
75                 final NyARPerspectiveProjectionMatrix pmat=i_param.getPerspectiveProjectionMatrix();\r
76                 this._transsolver=new NyARTransportVectorSolver_ARToolKit(pmat);\r
77                 //互換性が重要な時は、NyARRotMatrix_ARToolKitを使うこと。\r
78                 //理屈はNyARRotMatrix_NyARToolKitもNyARRotMatrix_ARToolKitも同じだけど、少しだけ値がずれる。\r
79                 this._rotmatrix = new NyARRotMatrix_ARToolKit_O2(pmat);\r
80                 this._mat_optimize=new NyARRotMatrixOptimize_O2(pmat);\r
81                 this._ref_dist_factor=dist;\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 err=this.optimize(this._rotmatrix, trans, this._transsolver, this._offset.vertex, vertex_2d);\r
201                 \r
202                 // マトリクスの保存\r
203                 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv);\r
204                 \r
205                 // エラー値が許容範囲でなければTransMatをやり直し\r
206                 if (err > AR_GET_TRANS_CONT_MAT_MAX_FIT_ERROR) {\r
207                         // rotationを矩形情報で初期化\r
208                         this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref);\r
209                         //回転行列の平行移動量の計算\r
210                         this._rotmatrix.getPoint3dBatch(this._offset.vertex,vertex_3d,4);\r
211                         this._transsolver.solveTransportVector(vertex_3d,trans);\r
212                         //計算結果の最適化(this._rotmatrix,trans)\r
213                         final double err2=this.optimize(this._rotmatrix, trans, this._transsolver, this._offset.vertex, vertex_2d);\r
214                         //エラー値が低かったら値を差換え\r
215                         if (err2 < err) {\r
216                                 // 良い値が取れたら、差換え\r
217                                 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv);\r
218                         }\r
219                 }\r
220                 return;\r
221         }\r
222         private double optimize(NyARRotMatrix_ARToolKit io_rotmat,NyARDoublePoint3d io_transvec,INyARTransportVectorSolver i_solver,NyARDoublePoint3d[] i_offset_3d,NyARDoublePoint2d[] i_2d_vertex) throws NyARException\r
223         {\r
224                 NyARDoublePoint3d[] vertex_3d=this.__transMat_vertex_3d;\r
225                 double err = -1;\r
226                 // ループを抜けるタイミングをARToolKitと合わせるために変なことしてます。 \r
227                 for (int i = 0;; i++) {\r
228                         // <arGetTransMat3>\r
229                         err = this._mat_optimize.modifyMatrix(io_rotmat, io_transvec, i_offset_3d, i_2d_vertex);\r
230                         io_rotmat.getPoint3dBatch(i_offset_3d,vertex_3d,4);\r
231                         i_solver.solveTransportVector(vertex_3d, io_transvec);\r
232                         \r
233                         err = this._mat_optimize.modifyMatrix(io_rotmat, io_transvec, i_offset_3d, i_2d_vertex);\r
234                         // //</arGetTransMat3>\r
235                         if (err < AR_GET_TRANS_MAT_MAX_FIT_ERROR || i == AR_GET_TRANS_MAT_MAX_LOOP_COUNT - 1) {\r
236                                 break;\r
237                         }\r
238                         io_rotmat.getPoint3dBatch(i_offset_3d,vertex_3d,4);\r
239                         i_solver.solveTransportVector(vertex_3d, io_transvec);\r
240                 }\r
241                 return err;\r
242         }       \r
243         /**\r
244          * パラメータで変換行列を更新します。\r
245          * \r
246          * @param i_rot\r
247          * @param i_off\r
248          * @param i_trans\r
249          */\r
250         public void updateMatrixValue(NyARRotMatrix i_rot, NyARDoublePoint3d i_off, NyARDoublePoint3d i_trans,NyARTransMatResult o_result)\r
251         {\r
252                 o_result.m00=i_rot.m00;\r
253                 o_result.m01=i_rot.m01;\r
254                 o_result.m02=i_rot.m02;\r
255                 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
256 \r
257                 o_result.m10 = i_rot.m10;\r
258                 o_result.m11 = i_rot.m11;\r
259                 o_result.m12 = i_rot.m12;\r
260                 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
261 \r
262                 o_result.m20 = i_rot.m20;\r
263                 o_result.m21 = i_rot.m21;\r
264                 o_result.m22 = i_rot.m22;\r
265                 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
266 \r
267                 o_result.has_value = true;\r
268                 return;\r
269         }       \r
270 }\r