OSDN Git Service

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