OSDN Git Service

[backup]NyARToolkit for Java
[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 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.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.artoolkit.INyARRotMatrixOptimize;\r
38 import jp.nyatla.nyartoolkit.core.transmat.optimize.artoolkit.NyARRotMatrixOptimize_O2;\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_ARToolKit implements INyARTransMat\r
49 {\r
50         private final static int AR_GET_TRANS_MAT_MAX_LOOP_COUNT = 5;// #define AR_GET_TRANS_MAT_MAX_LOOP_COUNT 5\r
51         private final static double AR_GET_TRANS_MAT_MAX_FIT_ERROR = 1.0;// #define AR_GET_TRANS_MAT_MAX_FIT_ERROR 1.0\r
52         private final static double AR_GET_TRANS_CONT_MAT_MAX_FIT_ERROR = 1.0;\r
53 \r
54         private final NyARDoublePoint2d _center=new NyARDoublePoint2d(0,0);\r
55         private final NyARTransOffset _offset=new NyARTransOffset();\r
56         protected NyARRotMatrix_ARToolKit _rotmatrix;\r
57         protected INyARTransportVectorSolver _transsolver;\r
58         protected INyARRotMatrixOptimize _mat_optimize;\r
59         private NyARCameraDistortionFactor _ref_dist_factor;\r
60 \r
61         /**\r
62          * 派生クラスで自分でメンバオブジェクトを指定したい場合はこちらを使う。\r
63          *\r
64          */\r
65         protected NyARTransMat_ARToolKit()\r
66         {\r
67                 //_calculator,_rotmatrix,_mat_optimizeをコンストラクタの終了後に\r
68                 //作成して割り当ててください。\r
69                 return;\r
70         }\r
71         public NyARTransMat_ARToolKit(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_ARToolKit(pmat);\r
76                 //互換性が重要な時は、NyARRotMatrix_ARToolKitを使うこと。\r
77                 //理屈はNyARRotMatrix_NyARToolKitもNyARRotMatrix_ARToolKitも同じだけど、少しだけ値がずれる。\r
78                 this._rotmatrix = new NyARRotMatrix_ARToolKit_O2(pmat);\r
79                 this._mat_optimize=new NyARRotMatrixOptimize_O2(pmat);\r
80                 this._ref_dist_factor=dist;\r
81         }\r
82 \r
83         public void setCenter(double i_x, double i_y)\r
84         {\r
85                 this._center.x= i_x;\r
86                 this._center.y= i_y;\r
87         }\r
88 \r
89 \r
90         private final NyARDoublePoint2d[] __transMat_vertex_2d = NyARDoublePoint2d.createArray(4);\r
91         private final NyARDoublePoint3d[] __transMat_vertex_3d = NyARDoublePoint3d.createArray(4);\r
92         private final NyARDoublePoint3d __transMat_trans=new NyARDoublePoint3d();\r
93         /**\r
94          * double arGetTransMat( ARMarkerInfo *marker_info,double center[2], double width, double conv[3][4] )\r
95          * \r
96          * @param i_square\r
97          * 計算対象のNyARSquareオブジェクト\r
98          * @param i_width\r
99          * @return\r
100          * @throws NyARException\r
101          */\r
102         public void transMat(final NyARSquare i_square, double i_width, NyARTransMatResult o_result_conv) throws NyARException\r
103         {\r
104                 final NyARDoublePoint3d trans=this.__transMat_trans;\r
105                 \r
106                 //平行移動量計算機に、2D座標系をセット\r
107                 NyARDoublePoint2d[] vertex_2d=this.__transMat_vertex_2d;\r
108                 NyARDoublePoint3d[] vertex_3d=this.__transMat_vertex_3d;\r
109                 this._ref_dist_factor.ideal2ObservBatch(i_square.sqvertex, vertex_2d,4);                \r
110                 this._transsolver.set2dVertex(vertex_2d,4);\r
111                 \r
112                 //基準矩形の3D座標系を作成\r
113                 this._offset.setSquare(i_width,this._center);\r
114 \r
115                 //回転行列を計算\r
116                 this._rotmatrix.initRotBySquare(i_square.line,i_square.sqvertex);\r
117                 \r
118                 //回転後の3D座標系から、平行移動量を計算\r
119                 this._rotmatrix.getPoint3dBatch(this._offset.vertex,vertex_3d,4);\r
120                 this._transsolver.solveTransportVector(vertex_3d,trans);\r
121                 \r
122                 //計算結果の最適化(平行移動量と回転行列の最適化)\r
123                 o_result_conv.error=this.optimize(this._rotmatrix, trans, this._transsolver,this._offset.vertex, vertex_2d);\r
124                 \r
125                 // マトリクスの保存\r
126                 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv);\r
127                 return;\r
128         }\r
129 \r
130         /*\r
131          * (non-Javadoc)\r
132          * @see jp.nyatla.nyartoolkit.core.transmat.INyARTransMat#transMatContinue(jp.nyatla.nyartoolkit.core.NyARSquare, int, double, jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult)\r
133          */\r
134         public void transMatContinue(NyARSquare i_square, double i_width, NyARTransMatResult o_result_conv) throws NyARException\r
135         {\r
136                 final NyARDoublePoint3d trans=this.__transMat_trans;\r
137 \r
138                 // io_result_convが初期値なら、transMatで計算する。\r
139                 if (!o_result_conv.has_value) {\r
140                         this.transMat(i_square, i_width, o_result_conv);\r
141                         return;\r
142                 }\r
143                 \r
144                 //平行移動量計算機に、2D座標系をセット\r
145                 NyARDoublePoint2d[] vertex_2d=this.__transMat_vertex_2d;\r
146                 NyARDoublePoint3d[] vertex_3d=this.__transMat_vertex_3d;\r
147                 this._ref_dist_factor.ideal2ObservBatch(i_square.sqvertex, vertex_2d,4);                \r
148                 this._transsolver.set2dVertex(vertex_2d,4);\r
149                 \r
150                 //基準矩形の3D座標系を作成\r
151                 this._offset.setSquare(i_width,this._center);\r
152 \r
153                 //回転行列を計算\r
154                 this._rotmatrix.initRotByPrevResult(o_result_conv);\r
155                 \r
156                 //回転後の3D座標系から、平行移動量を計算\r
157                 this._rotmatrix.getPoint3dBatch(this._offset.vertex,vertex_3d,4);\r
158                 this._transsolver.solveTransportVector(vertex_3d,trans);\r
159                 \r
160                 //計算結果の最適化(平行移動量と回転行列の最適化)\r
161                 double err=this.optimize(this._rotmatrix, trans, this._transsolver, this._offset.vertex, vertex_2d);\r
162                 \r
163                 // マトリクスの保存\r
164                 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv);\r
165                 \r
166                 // エラー値が許容範囲でなければTransMatをやり直し\r
167                 if (err > AR_GET_TRANS_CONT_MAT_MAX_FIT_ERROR) {\r
168                         // rotationを矩形情報で初期化\r
169                         this._rotmatrix.initRotBySquare(i_square.line,i_square.sqvertex);\r
170                         //回転行列の平行移動量の計算\r
171                         this._rotmatrix.getPoint3dBatch(this._offset.vertex,vertex_3d,4);\r
172                         this._transsolver.solveTransportVector(vertex_3d,trans);\r
173                         //計算結果の最適化(this._rotmatrix,trans)\r
174                         final double err2=this.optimize(this._rotmatrix, trans, this._transsolver, this._offset.vertex, vertex_2d);\r
175                         //エラー値が低かったら値を差換え\r
176                         if (err2 < err) {\r
177                                 // 良い値が取れたら、差換え\r
178                                 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv);\r
179                         }\r
180                         err=err2;\r
181                 }\r
182                 //エラー値保存\r
183                 o_result_conv.error=err;\r
184                 return;\r
185         }\r
186         private double optimize(NyARRotMatrix_ARToolKit io_rotmat,NyARDoublePoint3d io_transvec,INyARTransportVectorSolver i_solver,NyARDoublePoint3d[] i_offset_3d,NyARDoublePoint2d[] i_2d_vertex) throws NyARException\r
187         {\r
188                 NyARDoublePoint3d[] vertex_3d=this.__transMat_vertex_3d;\r
189                 double err = -1;\r
190                 //System.out.println("START");\r
191                 // ループを抜けるタイミングをARToolKitと合わせるために変なことしてます。 \r
192                 for (int i = 0;; i++) {\r
193                         // <arGetTransMat3>\r
194                         err = this._mat_optimize.modifyMatrix(io_rotmat, io_transvec, i_offset_3d, i_2d_vertex);\r
195                         io_rotmat.getPoint3dBatch(i_offset_3d,vertex_3d,4);\r
196                         i_solver.solveTransportVector(vertex_3d, io_transvec);\r
197                         \r
198                         err = this._mat_optimize.modifyMatrix(io_rotmat, io_transvec, i_offset_3d, i_2d_vertex);\r
199                         //System.out.println("E:"+err*4);\r
200                         // //</arGetTransMat3>\r
201                         if (err < AR_GET_TRANS_MAT_MAX_FIT_ERROR || i == AR_GET_TRANS_MAT_MAX_LOOP_COUNT - 1) {\r
202                                 break;\r
203                         }\r
204                         io_rotmat.getPoint3dBatch(i_offset_3d,vertex_3d,4);\r
205                         i_solver.solveTransportVector(vertex_3d, io_transvec);\r
206                 }\r
207                 //System.out.println("END");\r
208                 return err;\r
209         }       \r
210         /**\r
211          * パラメータで変換行列を更新します。\r
212          * \r
213          * @param i_rot\r
214          * @param i_off\r
215          * @param i_trans\r
216          */\r
217         public void updateMatrixValue(NyARRotMatrix i_rot, NyARDoublePoint3d i_off, NyARDoublePoint3d i_trans,NyARTransMatResult o_result)\r
218         {\r
219                 o_result.m00=i_rot.m00;\r
220                 o_result.m01=i_rot.m01;\r
221                 o_result.m02=i_rot.m02;\r
222                 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
223 \r
224                 o_result.m10 = i_rot.m10;\r
225                 o_result.m11 = i_rot.m11;\r
226                 o_result.m12 = i_rot.m12;\r
227                 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
228 \r
229                 o_result.m20 = i_rot.m20;\r
230                 o_result.m21 = i_rot.m21;\r
231                 o_result.m22 = i_rot.m22;\r
232                 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
233 \r
234                 o_result.has_value = true;\r
235                 return;\r
236         }       \r
237 }\r