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
\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
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
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
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.*;
\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
45 * This class calculates ARMatrix from square information and holds it. --
\r
46 * 変換行列を計算して、結果を保持するクラス。
\r
49 public class NyARTransMat_ARToolKit implements INyARTransMat
\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
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
63 * 派生クラスで自分でメンバオブジェクトを指定したい場合はこちらを使う。
\r
66 protected NyARTransMat_ARToolKit()
\r
68 //_calculator,_rotmatrix,_mat_optimizeをコンストラクタの終了後に
\r
72 public NyARTransMat_ARToolKit(NyARParam i_param) throws NyARException
\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
84 public void setCenter(double i_x, double i_y)
\r
86 this._center.x= i_x;
\r
87 this._center.y= i_y;
\r
94 * 頂点順序をi_directionに対応して並べ替えます。
\r
96 * @param i_direction
\r
97 * @param o_sqvertex_ref
\r
98 * @param o_liner_ref
\r
100 private final void initVertexOrder(NyARSquare i_square, int i_direction, NyARDoublePoint2d[] o_sqvertex_ref, NyARLinear[] o_liner_ref)
\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
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
121 * double arGetTransMat( ARMarkerInfo *marker_info,double center[2], double width, double conv[3][4] )
\r
124 * 計算対象のNyARSquareオブジェクト
\r
125 * @param i_direction
\r
128 * @throws NyARException
\r
130 public void transMat(final NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult o_result_conv) throws NyARException
\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
136 //計算用に頂点情報を初期化(順番調整)
\r
137 initVertexOrder(i_square, i_direction, sqvertex_ref,linear_ref);
\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
146 this._offset.setSquare(i_width,this._center);
\r
149 this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref);
\r
151 //回転後の3D座標系から、平行移動量を計算
\r
152 this._rotmatrix.getPoint3dBatch(this._offset.vertex,vertex_3d,4);
\r
153 this._transsolver.solveTransportVector(vertex_3d,trans);
\r
155 //計算結果の最適化(平行移動量と回転行列の最適化)
\r
156 this.optimize(this._rotmatrix, trans, this._transsolver,this._offset.vertex, vertex_2d);
\r
159 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv);
\r
165 * @see jp.nyatla.nyartoolkit.core.transmat.INyARTransMat#transMatContinue(jp.nyatla.nyartoolkit.core.NyARSquare, int, double, jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult)
\r
167 public void transMatContinue(NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult o_result_conv) throws NyARException
\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
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
179 //計算用に頂点情報を初期化(順番調整)
\r
180 initVertexOrder(i_square, i_direction, sqvertex_ref,linear_ref);
\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
190 this._offset.setSquare(i_width,this._center);
\r
193 this._rotmatrix.initRotByPrevResult(o_result_conv);
\r
195 //回転後の3D座標系から、平行移動量を計算
\r
196 this._rotmatrix.getPoint3dBatch(this._offset.vertex,vertex_3d,4);
\r
197 this._transsolver.solveTransportVector(vertex_3d,trans);
\r
199 //計算結果の最適化(平行移動量と回転行列の最適化)
\r
200 double err=this.optimize(this._rotmatrix, trans, this._transsolver, this._offset.vertex, vertex_2d);
\r
203 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv);
\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
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
217 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv);
\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
224 NyARDoublePoint3d[] vertex_3d=this.__transMat_vertex_3d;
\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
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
238 io_rotmat.getPoint3dBatch(i_offset_3d,vertex_3d,4);
\r
239 i_solver.solveTransportVector(vertex_3d, io_transvec);
\r
244 * パラメータで変換行列を更新します。
\r
250 public void updateMatrixValue(NyARRotMatrix i_rot, NyARDoublePoint3d i_off, NyARDoublePoint3d i_trans,NyARTransMatResult o_result)
\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
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
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
267 o_result.has_value = true;
\r