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 this._rotmatrix = new NyARRotMatrix_ARToolKit(pmat);
\r
74 this._mat_optimize=new NyARRotTransOptimize_O2(pmat);
\r
77 public void setCenter(double i_x, double i_y)
\r
79 this._center.x= i_x;
\r
80 this._center.y= i_y;
\r
87 * 頂点順序をi_directionに対応して並べ替えます。
\r
89 * @param i_direction
\r
90 * @param o_sqvertex_ref
\r
91 * @param o_liner_ref
\r
93 private final void initVertexOrder(NyARSquare i_square, int i_direction, NyARDoublePoint2d[] o_sqvertex_ref, NyARLinear[] o_liner_ref)
\r
96 o_sqvertex_ref[0]= i_square.sqvertex[(4 - i_direction) % 4];
\r
97 o_sqvertex_ref[1]= i_square.sqvertex[(5 - i_direction) % 4];
\r
98 o_sqvertex_ref[2]= i_square.sqvertex[(6 - i_direction) % 4];
\r
99 o_sqvertex_ref[3]= i_square.sqvertex[(7 - i_direction) % 4];
\r
100 o_liner_ref[0]=i_square.line[(4 - i_direction) % 4];
\r
101 o_liner_ref[1]=i_square.line[(5 - i_direction) % 4];
\r
102 o_liner_ref[2]=i_square.line[(6 - i_direction) % 4];
\r
103 o_liner_ref[3]=i_square.line[(7 - i_direction) % 4];
\r
108 private final NyARDoublePoint2d[] __transMat_sqvertex_ref = new NyARDoublePoint2d[4];
\r
109 private final NyARLinear[] __transMat_linear_ref=new NyARLinear[4];
\r
110 private final NyARDoublePoint3d __transMat_trans=new NyARDoublePoint3d();
\r
112 * double arGetTransMat( ARMarkerInfo *marker_info,double center[2], double width, double conv[3][4] )
\r
115 * 計算対象のNyARSquareオブジェクト
\r
116 * @param i_direction
\r
119 * @throws NyARException
\r
121 public void transMat(final NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult o_result_conv) throws NyARException
\r
123 final NyARDoublePoint2d[] sqvertex_ref = __transMat_sqvertex_ref;
\r
124 final NyARLinear[] linear_ref=__transMat_linear_ref;
\r
125 final NyARDoublePoint3d trans=this.__transMat_trans;
\r
127 //計算用に頂点情報を初期化(順番調整)
\r
128 initVertexOrder(i_square, i_direction, sqvertex_ref,linear_ref);
\r
131 this._offset.setSquare(i_width,this._center);
\r
133 // rotationを矩形情報から計算
\r
134 this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref);
\r
136 //平行移動量計算機にオフセット頂点をセット
\r
137 this._calculator.setOffsetSquare(this._offset);
\r
139 //平行移動量計算機に適応先矩形の情報をセット
\r
140 this._calculator.setFittedSquare(sqvertex_ref);
\r
143 this._calculator.calculateTransfer(this._rotmatrix,trans);
\r
145 //計算結果の最適化(this._rotmatrix,trans)
\r
146 this._mat_optimize.optimize(this._rotmatrix,trans,this._calculator);
\r
149 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv);
\r
153 * double arGetTransMatCont( ARMarkerInfo *marker_info, double prev_conv[3][4],double center[2], double width, double conv[3][4] )
\r
156 * @param i_direction
\r
159 * @param io_result_conv
\r
160 * 計算履歴を持つNyARTransMatResultオブジェクトを指定する。 履歴を持たない場合は、transMatと同じ処理を行う。
\r
162 * @throws NyARException
\r
164 public void transMatContinue(NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult io_result_conv) throws NyARException
\r
166 final NyARDoublePoint2d[] sqvertex_ref = __transMat_sqvertex_ref;
\r
167 final NyARLinear[] linear_ref=__transMat_linear_ref;
\r
168 final NyARDoublePoint3d trans=this.__transMat_trans;
\r
170 // io_result_convが初期値なら、transMatで計算する。
\r
171 if (!io_result_conv.has_value) {
\r
172 this.transMat(i_square, i_direction, i_width, io_result_conv);
\r
177 this._offset.setSquare(i_width,this._center);
\r
179 // rotationを矩形情報を一つ前の変換行列で初期化
\r
180 this._rotmatrix.initRotByPrevResult(io_result_conv);
\r
182 //平行移動量計算機に、オフセット頂点をセット
\r
183 this._calculator.setOffsetSquare(this._offset);
\r
185 //平行移動量計算機に、適応先矩形の情報をセット
\r
186 this._calculator.setFittedSquare(sqvertex_ref);
\r
189 this._calculator.calculateTransfer(this._rotmatrix,trans);
\r
191 //計算結果の最適化(this._rotmatrix,trans)
\r
192 final double err=this._mat_optimize.optimize(this._rotmatrix,trans,this._calculator);
\r
195 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,io_result_conv);
\r
197 // エラー値が許容範囲でなければTransMatをやり直し
\r
198 if (err > AR_GET_TRANS_CONT_MAT_MAX_FIT_ERROR) {
\r
199 // rotationを矩形情報で初期化
\r
200 this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref);
\r
202 this._calculator.calculateTransfer(this._rotmatrix,trans);
\r
203 //計算結果の最適化(this._rotmatrix,trans)
\r
204 final double err2=this._mat_optimize.optimize(this._rotmatrix,trans,this._calculator);
\r
208 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,io_result_conv);
\r
214 * パラメータで変換行列を更新します。
\r
220 public void updateMatrixValue(NyARRotMatrix i_rot, NyARDoublePoint3d i_off, NyARDoublePoint3d i_trans,NyARTransMatResult o_result)
\r
222 o_result.m00=i_rot.m00;
\r
223 o_result.m01=i_rot.m01;
\r
224 o_result.m02=i_rot.m02;
\r
225 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
227 o_result.m10 = i_rot.m10;
\r
228 o_result.m11 = i_rot.m11;
\r
229 o_result.m12 = i_rot.m12;
\r
230 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
232 o_result.m20 = i_rot.m20;
\r
233 o_result.m21 = i_rot.m21;
\r
234 o_result.m22 = i_rot.m22;
\r
235 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
237 o_result.has_value = true;
\r