2 * PROJECT: NyARToolkit
\r
3 * --------------------------------------------------------------------------------
\r
4 * This work is based on the original ARToolKit developed by
\r
7 * HITLab, University of Washington, Seattle
\r
8 * http://www.hitl.washington.edu/artoolkit/
\r
10 * The NyARToolkit is Java edition ARToolKit class library.
\r
11 * Copyright (C)2008-2009 Ryo Iizuka
\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
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
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
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
31 package jp.nyatla.nyartoolkit.core.transmat;
\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
44 * This class calculates ARMatrix from square information and holds it. --
\r
45 * 変換行列を計算して、結果を保持するクラス。
\r
48 public class NyARTransMat_ARToolKit implements INyARTransMat
\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
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
62 * 派生クラスで自分でメンバオブジェクトを指定したい場合はこちらを使う。
\r
65 protected NyARTransMat_ARToolKit()
\r
67 //_calculator,_rotmatrix,_mat_optimizeをコンストラクタの終了後に
\r
71 public NyARTransMat_ARToolKit(NyARParam i_param) throws NyARException
\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
83 public void setCenter(double i_x, double i_y)
\r
85 this._center.x= i_x;
\r
86 this._center.y= i_y;
\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
94 * double arGetTransMat( ARMarkerInfo *marker_info,double center[2], double width, double conv[3][4] )
\r
97 * 計算対象のNyARSquareオブジェクト
\r
100 * @throws NyARException
\r
102 public void transMat(final NyARSquare i_square, double i_width, NyARTransMatResult o_result_conv) throws NyARException
\r
104 final NyARDoublePoint3d trans=this.__transMat_trans;
\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
113 this._offset.setSquare(i_width,this._center);
\r
116 this._rotmatrix.initRotBySquare(i_square.line,i_square.sqvertex);
\r
118 //回転後の3D座標系から、平行移動量を計算
\r
119 this._rotmatrix.getPoint3dBatch(this._offset.vertex,vertex_3d,4);
\r
120 this._transsolver.solveTransportVector(vertex_3d,trans);
\r
122 //計算結果の最適化(平行移動量と回転行列の最適化)
\r
123 o_result_conv.error=this.optimize(this._rotmatrix, trans, this._transsolver,this._offset.vertex, vertex_2d);
\r
126 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv);
\r
132 * @see jp.nyatla.nyartoolkit.core.transmat.INyARTransMat#transMatContinue(jp.nyatla.nyartoolkit.core.NyARSquare, int, double, jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult)
\r
134 public void transMatContinue(NyARSquare i_square, double i_width, NyARTransMatResult o_result_conv) throws NyARException
\r
136 final NyARDoublePoint3d trans=this.__transMat_trans;
\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
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
151 this._offset.setSquare(i_width,this._center);
\r
154 this._rotmatrix.initRotByPrevResult(o_result_conv);
\r
156 //回転後の3D座標系から、平行移動量を計算
\r
157 this._rotmatrix.getPoint3dBatch(this._offset.vertex,vertex_3d,4);
\r
158 this._transsolver.solveTransportVector(vertex_3d,trans);
\r
160 //計算結果の最適化(平行移動量と回転行列の最適化)
\r
161 double err=this.optimize(this._rotmatrix, trans, this._transsolver, this._offset.vertex, vertex_2d);
\r
164 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv);
\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
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
178 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv);
\r
183 o_result_conv.error=err;
\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
188 NyARDoublePoint3d[] vertex_3d=this.__transMat_vertex_3d;
\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
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
204 io_rotmat.getPoint3dBatch(i_offset_3d,vertex_3d,4);
\r
205 i_solver.solveTransportVector(vertex_3d, io_transvec);
\r
207 //System.out.println("END");
\r
211 * パラメータで変換行列を更新します。
\r
217 public void updateMatrixValue(NyARRotMatrix i_rot, NyARDoublePoint3d i_off, NyARDoublePoint3d i_trans,NyARTransMatResult o_result)
\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
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
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
234 o_result.has_value = true;
\r