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 version ARToolkit class library.
\r
11 * Copyright (C)2008 R.Iizuka
\r
13 * This program is free software; you can redistribute it and/or
\r
14 * modify it under the terms of the GNU General Public License
\r
15 * as published by the Free Software Foundation; either version 2
\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 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 framework; if not, write to the Free Software
\r
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
27 * For further information please contact.
\r
28 * http://nyatla.jp/nyatoolkit/
\r
29 * <airmail(at)ebony.plala.or.jp>
\r
32 package jp.nyatla.nyartoolkit.core.transmat;
\r
34 import jp.nyatla.nyartoolkit.NyARException;
\r
35 import jp.nyatla.nyartoolkit.core.NyARSquare;
\r
36 import jp.nyatla.nyartoolkit.core.param.*;
\r
37 import jp.nyatla.nyartoolkit.core.transmat.fitveccalc.NyARFitVecCalculator;
\r
38 import jp.nyatla.nyartoolkit.core.transmat.optimize.*;
\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 implements INyARTransMat
\r
50 private final static double AR_GET_TRANS_CONT_MAT_MAX_FIT_ERROR = 1.0;
\r
52 private final NyARDoublePoint2d _center=new NyARDoublePoint2d(0,0);
\r
53 private final NyARTransOffset _offset=new NyARTransOffset();
\r
54 protected NyARRotMatrix _rotmatrix;
\r
55 protected NyARFitVecCalculator _calculator;
\r
56 protected INyARRotTransOptimize _mat_optimize;
\r
59 * 派生クラスで自分でメンバオブジェクトを指定したい場合はこちらを使う。
\r
62 protected NyARTransMat()
\r
64 //_calculator,_rotmatrix,_mat_optimizeをコンストラクタの終了後に
\r
68 public NyARTransMat(NyARParam i_param) throws NyARException
\r
70 final NyARCameraDistortionFactor dist=i_param.getDistortionFactor();
\r
71 final NyARPerspectiveProjectionMatrix pmat=i_param.getPerspectiveProjectionMatrix();
\r
72 this._calculator=new NyARFitVecCalculator(pmat,dist);
\r
73 //互換性が重要な時は、NyARRotMatrix_ARToolKitを使うこと。
\r
74 //理屈はNyARRotMatrix_NyARToolKitもNyARRotMatrix_ARToolKitも同じだけど、少しだけ値がずれる。
\r
75 this._rotmatrix = new NyARRotMatrix_NyARToolKit(pmat);
\r
76 // this._rotmatrix = new NyARRotMatrix_ARToolKit(pmat);
\r
77 this._mat_optimize=new NyARRotTransOptimize_O2(pmat);
\r
80 public void setCenter(double i_x, double i_y)
\r
82 this._center.x= i_x;
\r
83 this._center.y= i_y;
\r
90 * 頂点順序をi_directionに対応して並べ替えます。
\r
92 * @param i_direction
\r
93 * @param o_sqvertex_ref
\r
94 * @param o_liner_ref
\r
96 private final void initVertexOrder(NyARSquare i_square, int i_direction, NyARDoublePoint2d[] o_sqvertex_ref, NyARLinear[] o_liner_ref)
\r
99 o_sqvertex_ref[0]= i_square.sqvertex[(4 - i_direction) % 4];
\r
100 o_sqvertex_ref[1]= i_square.sqvertex[(5 - i_direction) % 4];
\r
101 o_sqvertex_ref[2]= i_square.sqvertex[(6 - i_direction) % 4];
\r
102 o_sqvertex_ref[3]= i_square.sqvertex[(7 - i_direction) % 4];
\r
103 o_liner_ref[0]=i_square.line[(4 - i_direction) % 4];
\r
104 o_liner_ref[1]=i_square.line[(5 - i_direction) % 4];
\r
105 o_liner_ref[2]=i_square.line[(6 - i_direction) % 4];
\r
106 o_liner_ref[3]=i_square.line[(7 - i_direction) % 4];
\r
111 private final NyARDoublePoint2d[] __transMat_sqvertex_ref = new NyARDoublePoint2d[4];
\r
112 private final NyARLinear[] __transMat_linear_ref=new NyARLinear[4];
\r
113 private final NyARDoublePoint3d __transMat_trans=new NyARDoublePoint3d();
\r
115 * double arGetTransMat( ARMarkerInfo *marker_info,double center[2], double width, double conv[3][4] )
\r
118 * 計算対象のNyARSquareオブジェクト
\r
119 * @param i_direction
\r
122 * @throws NyARException
\r
124 public void transMat(final NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult o_result_conv) throws NyARException
\r
126 final NyARDoublePoint2d[] sqvertex_ref = __transMat_sqvertex_ref;
\r
127 final NyARLinear[] linear_ref=__transMat_linear_ref;
\r
128 final NyARDoublePoint3d trans=this.__transMat_trans;
\r
130 //計算用に頂点情報を初期化(順番調整)
\r
131 initVertexOrder(i_square, i_direction, sqvertex_ref,linear_ref);
\r
134 this._offset.setSquare(i_width,this._center);
\r
136 // rotationを矩形情報から計算
\r
137 this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref);
\r
139 //平行移動量計算機にオフセット頂点をセット
\r
140 this._calculator.setOffsetSquare(this._offset);
\r
142 //平行移動量計算機に適応先矩形の情報をセット
\r
143 this._calculator.setFittedSquare(sqvertex_ref);
\r
146 this._calculator.calculateTransfer(this._rotmatrix,trans);
\r
148 //計算結果の最適化(this._rotmatrix,trans)
\r
149 this._mat_optimize.optimize(this._rotmatrix,trans,this._calculator);
\r
152 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv);
\r
156 * double arGetTransMatCont( ARMarkerInfo *marker_info, double prev_conv[3][4],double center[2], double width, double conv[3][4] )
\r
159 * @param i_direction
\r
162 * @param io_result_conv
\r
163 * 計算履歴を持つNyARTransMatResultオブジェクトを指定する。 履歴を持たない場合は、transMatと同じ処理を行う。
\r
165 * @throws NyARException
\r
167 public void transMatContinue(NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult io_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 (!io_result_conv.has_value) {
\r
175 this.transMat(i_square, i_direction, i_width, io_result_conv);
\r
180 this._offset.setSquare(i_width,this._center);
\r
182 // rotationを矩形情報を一つ前の変換行列で初期化
\r
183 this._rotmatrix.initRotByPrevResult(io_result_conv);
\r
185 //平行移動量計算機に、オフセット頂点をセット
\r
186 this._calculator.setOffsetSquare(this._offset);
\r
188 //平行移動量計算機に、適応先矩形の情報をセット
\r
189 this._calculator.setFittedSquare(sqvertex_ref);
\r
192 this._calculator.calculateTransfer(this._rotmatrix,trans);
\r
194 //計算結果の最適化(this._rotmatrix,trans)
\r
195 final double err=this._mat_optimize.optimize(this._rotmatrix,trans,this._calculator);
\r
198 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,io_result_conv);
\r
200 // エラー値が許容範囲でなければTransMatをやり直し
\r
201 if (err > AR_GET_TRANS_CONT_MAT_MAX_FIT_ERROR) {
\r
202 // rotationを矩形情報で初期化
\r
203 this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref);
\r
205 this._calculator.calculateTransfer(this._rotmatrix,trans);
\r
206 //計算結果の最適化(this._rotmatrix,trans)
\r
207 final double err2=this._mat_optimize.optimize(this._rotmatrix,trans,this._calculator);
\r
211 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,io_result_conv);
\r
217 * パラメータで変換行列を更新します。
\r
223 public void updateMatrixValue(NyARRotMatrix i_rot, NyARDoublePoint3d i_off, NyARDoublePoint3d i_trans,NyARTransMatResult o_result)
\r
225 o_result.m00=i_rot.m00;
\r
226 o_result.m01=i_rot.m01;
\r
227 o_result.m02=i_rot.m02;
\r
228 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
230 o_result.m10 = i_rot.m10;
\r
231 o_result.m11 = i_rot.m11;
\r
232 o_result.m12 = i_rot.m12;
\r
233 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
235 o_result.m20 = i_rot.m20;
\r
236 o_result.m21 = i_rot.m21;
\r
237 o_result.m22 = i_rot.m22;
\r
238 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
240 o_result.angle.copyFrom(i_rot.refAngle());
\r
241 o_result.has_value = true;
\r