From: nyatla Date: Mon, 14 Sep 2009 03:19:18 +0000 (+0000) Subject: [Backup]NyARToolkit for Java X-Git-Url: http://git.osdn.net/view?p=nyartoolkit-and%2Fnyartoolkit-and.git;a=commitdiff_plain;h=d353991ae8f159b005b387c9a14f0eb087440659 [Backup]NyARToolkit for Java 平行移動ベクトル計算機の再実装 git-svn-id: http://svn.sourceforge.jp/svnroot/nyartoolkit/NyARToolkit@289 7cac0a50-4618-4814-88d0-24b83990f816 --- diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/labeling/rlelabeling/NyARLabeling_Rle.java b/trunk/src/jp/nyatla/nyartoolkit/core/labeling/rlelabeling/NyARLabeling_Rle.java index a1b71db..e2639e7 100644 --- a/trunk/src/jp/nyatla/nyartoolkit/core/labeling/rlelabeling/NyARLabeling_Rle.java +++ b/trunk/src/jp/nyatla/nyartoolkit/core/labeling/rlelabeling/NyARLabeling_Rle.java @@ -167,7 +167,7 @@ public class NyARLabeling_Rle return current; } - private void addFragment(RleElement i_rel_img, int i_nof, int i_row_index, int i_rel_index,RleInfoStack o_stack) throws NyARException + private void addFragment(RleElement i_rel_img, int i_nof, int i_row_index,RleInfoStack o_stack) throws NyARException { int l=i_rel_img.l; final int len=i_rel_img.r - l; @@ -207,7 +207,7 @@ public class NyARLabeling_Rle len_prev = toRel(in_buf, i_top, width, rle_prev); for (int i = 0; i < len_prev; i++) { // フラグメントID=フラグメント初期値、POS=Y値、RELインデクス=行 - addFragment(rle_prev[i], id_max, i_top, i,rlestack); + addFragment(rle_prev[i], id_max, i_top,rlestack); id_max++; // nofの最大値チェック label_count++; @@ -230,7 +230,7 @@ public class NyARLabeling_Rle continue; } else if (rle_prev[index_prev].l - rle_current[i].r > 0) {// 0なら8方位ラベリングになる // prevがcur右方にある→独立フラグメント - addFragment(rle_current[i], id_max, y, i,rlestack); + addFragment(rle_current[i], id_max, y,rlestack); id_max++; label_count++; // 次のindexをしらべる @@ -327,7 +327,7 @@ public class NyARLabeling_Rle // curにidが割り当てられたかを確認 // 右端独立フラグメントを追加 if (id < 0){ - addFragment(rle_current[i], id_max, y, i,rlestack); + addFragment(rle_current[i], id_max, y,rlestack); id_max++; label_count++; } diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/pickup/NyARColorPatt_Perspective_O2.java b/trunk/src/jp/nyatla/nyartoolkit/core/pickup/NyARColorPatt_Perspective_O2.java index 6dd7410..287466c 100644 --- a/trunk/src/jp/nyatla/nyartoolkit/core/pickup/NyARColorPatt_Perspective_O2.java +++ b/trunk/src/jp/nyatla/nyartoolkit/core/pickup/NyARColorPatt_Perspective_O2.java @@ -34,7 +34,7 @@ import jp.nyatla.nyartoolkit.core.types.*; interface IpickFromRaster_Impl { - public void pickFromRaster(double[] i_cpara,INyARRgbRaster image, NyARSquare i_square,int[] o_patt)throws NyARException; + public void pickFromRaster(double[] i_cpara,INyARRgbRaster image, int[] o_patt)throws NyARException; } /** @@ -68,7 +68,7 @@ class pickFromRaster_N implements IpickFromRaster_Impl private double[] _cp4cy_cp5; private double[] _cp7cy_1; - public void pickFromRaster(double[] i_cpara,INyARRgbRaster image, NyARSquare i_square,int[] o_patt)throws NyARException + public void pickFromRaster(double[] i_cpara,INyARRgbRaster image,int[] o_patt)throws NyARException { int i2x,i2y;//プライム変数 int x,y; @@ -186,7 +186,7 @@ class pickFromRaster_1 implements IpickFromRaster_Impl private int[] _rgb_py; - public void pickFromRaster(double[] i_cpara,INyARRgbRaster image, NyARSquare i_square,int[] o_patt)throws NyARException + public void pickFromRaster(double[] i_cpara,INyARRgbRaster image,int[] o_patt)throws NyARException { double d0,m0; int x,y; @@ -289,7 +289,7 @@ class pickFromRaster_2x implements IpickFromRaster_Impl private int[] _rgb_py; - public void pickFromRaster(double[] i_cpara,INyARRgbRaster image, NyARSquare i_square,int[] o_patt)throws NyARException + public void pickFromRaster(double[] i_cpara,INyARRgbRaster image,int[] o_patt)throws NyARException { double d0,m0,d1,m1; int x,y; @@ -418,7 +418,7 @@ class pickFromRaster_4x implements IpickFromRaster_Impl private int[] _rgb_px; private int[] _rgb_py; - public void pickFromRaster(double[] i_cpara,INyARRgbRaster image, NyARSquare i_square,int[] o_patt)throws NyARException + public void pickFromRaster(double[] i_cpara,INyARRgbRaster image,int[] o_patt)throws NyARException { int x,y; double d,m; @@ -680,7 +680,7 @@ public class NyARColorPatt_Perspective_O2 extends NyARColorPatt_Perspective if (!this._perspective_gen.getParam(i_square.imvertex, cpara)) { return false; } - this._pickup.pickFromRaster(cpara, image, i_square, this._patdata); + this._pickup.pickFromRaster(cpara, image,this._patdata); return true; } diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/pickup/NyARColorPatt_PseudoAffine.java b/trunk/src/jp/nyatla/nyartoolkit/core/pickup/NyARColorPatt_PseudoAffine.java index 0db011d..9be65b1 100644 --- a/trunk/src/jp/nyatla/nyartoolkit/core/pickup/NyARColorPatt_PseudoAffine.java +++ b/trunk/src/jp/nyatla/nyartoolkit/core/pickup/NyARColorPatt_PseudoAffine.java @@ -32,7 +32,7 @@ import jp.nyatla.nyartoolkit.core.rasterreader.*; import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare; import jp.nyatla.nyartoolkit.core.types.*; import jp.nyatla.nyartoolkit.core.types.matrix.*; -import jp.nyatla.nyartoolkit.core.utils.NyARDoubleMatrixProcessor; + /** @@ -101,7 +101,7 @@ public class NyARColorPatt_PseudoAffine implements INyARColorPatt mat.m31=0; mat.m32=i_height-1; mat.m33=1.0; - NyARDoubleMatrixProcessor.inverse(mat,mat); + mat.inverse(mat); return; } diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransMat.java b/trunk/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransMat.java index e729173..5ee4087 100644 --- a/trunk/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransMat.java +++ b/trunk/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransMat.java @@ -34,7 +34,7 @@ package jp.nyatla.nyartoolkit.core.transmat; import jp.nyatla.nyartoolkit.NyARException; import jp.nyatla.nyartoolkit.core.param.*; import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare; -import jp.nyatla.nyartoolkit.core.transmat.fitveccalc.NyARFitVecCalculator; +import jp.nyatla.nyartoolkit.core.transmat.solver.*; import jp.nyatla.nyartoolkit.core.transmat.optimize.*; import jp.nyatla.nyartoolkit.core.transmat.rotmatrix.*; import jp.nyatla.nyartoolkit.core.types.*; @@ -47,13 +47,16 @@ import jp.nyatla.nyartoolkit.core.types.*; */ public class NyARTransMat implements INyARTransMat { + private final static int AR_GET_TRANS_MAT_MAX_LOOP_COUNT = 5;// #define AR_GET_TRANS_MAT_MAX_LOOP_COUNT 5 + private final static double AR_GET_TRANS_MAT_MAX_FIT_ERROR = 1.0;// #define AR_GET_TRANS_MAT_MAX_FIT_ERROR 1.0 private final static double AR_GET_TRANS_CONT_MAT_MAX_FIT_ERROR = 1.0; private final NyARDoublePoint2d _center=new NyARDoublePoint2d(0,0); private final NyARTransOffset _offset=new NyARTransOffset(); protected NyARRotMatrix _rotmatrix; - protected NyARFitVecCalculator _calculator; - protected INyARRotTransOptimize _mat_optimize; + protected INyARTransportVectorSolver _transsolver; + protected INyARRotMatrixOptimize _mat_optimize; + private NyARCameraDistortionFactor _ref_dist_factor; /** * 派生クラスで自分でメンバオブジェクトを指定したい場合はこちらを使う。 @@ -69,12 +72,12 @@ public class NyARTransMat implements INyARTransMat { final NyARCameraDistortionFactor dist=i_param.getDistortionFactor(); final NyARPerspectiveProjectionMatrix pmat=i_param.getPerspectiveProjectionMatrix(); - this._calculator=new NyARFitVecCalculator(pmat,dist); + this._transsolver=new NyARTransportVectorSolver_ARToolKit(pmat); //互換性が重要な時は、NyARRotMatrix_ARToolKitを使うこと。 //理屈はNyARRotMatrix_NyARToolKitもNyARRotMatrix_ARToolKitも同じだけど、少しだけ値がずれる。 this._rotmatrix = new NyARRotMatrix_NyARToolKit(pmat); -// this._rotmatrix = new NyARRotMatrix_ARToolKit(pmat); - this._mat_optimize=new NyARRotTransOptimize_O2(pmat); + this._mat_optimize=new NyARRotMatrixOptimize_O2(pmat); + this._ref_dist_factor=dist; } public void setCenter(double i_x, double i_y) @@ -109,6 +112,8 @@ public class NyARTransMat implements INyARTransMat private final NyARDoublePoint2d[] __transMat_sqvertex_ref = new NyARDoublePoint2d[4]; + private final NyARDoublePoint2d[] __transMat_vertex_2d = NyARDoublePoint2d.createArray(4); + private final NyARDoublePoint3d[] __transMat_vertex_3d = NyARDoublePoint3d.createArray(4); private final NyARLinear[] __transMat_linear_ref=new NyARLinear[4]; private final NyARDoublePoint3d __transMat_trans=new NyARDoublePoint3d(); /** @@ -130,23 +135,24 @@ public class NyARTransMat implements INyARTransMat //計算用に頂点情報を初期化(順番調整) initVertexOrder(i_square, i_direction, sqvertex_ref,linear_ref); - //基準矩形を設定 + //平行移動量計算機に、2D座標系をセット + NyARDoublePoint2d[] vertex_2d=this.__transMat_vertex_2d; + NyARDoublePoint3d[] vertex_3d=this.__transMat_vertex_3d; + this._ref_dist_factor.ideal2ObservBatch(sqvertex_ref, vertex_2d,4); + this._transsolver.set2dVertex(vertex_2d,4); + + //基準矩形の3D座標系を作成 this._offset.setSquare(i_width,this._center); - // rotationを矩形情報から計算 + //回転行列を計算 this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref); - - //平行移動量計算機にオフセット頂点をセット - this._calculator.setOffsetSquare(this._offset); - //平行移動量計算機に適応先矩形の情報をセット - this._calculator.setFittedSquare(sqvertex_ref); - - //回転行列の平行移動量の計算 - this._calculator.calculateTransfer(this._rotmatrix,trans); + //回転後の3D座標系から、平行移動量を計算 + this._rotmatrix.getPoint3dBatch(this._offset.vertex,vertex_3d,4); + this._transsolver.solveTransportVector(vertex_3d,trans); - //計算結果の最適化(this._rotmatrix,trans) - this._mat_optimize.optimize(this._rotmatrix,trans,this._calculator); + //計算結果の最適化(平行移動量と回転行列の最適化) + this.optimize(this._rotmatrix, trans, this._transsolver,this._offset.vertex, vertex_2d); // マトリクスの保存 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv); @@ -157,58 +163,82 @@ public class NyARTransMat implements INyARTransMat * (non-Javadoc) * @see jp.nyatla.nyartoolkit.core.transmat.INyARTransMat#transMatContinue(jp.nyatla.nyartoolkit.core.NyARSquare, int, double, jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult) */ - public void transMatContinue(NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult io_result_conv) throws NyARException + public void transMatContinue(NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult o_result_conv) throws NyARException { final NyARDoublePoint2d[] sqvertex_ref = __transMat_sqvertex_ref; final NyARLinear[] linear_ref=__transMat_linear_ref; final NyARDoublePoint3d trans=this.__transMat_trans; // io_result_convが初期値なら、transMatで計算する。 - if (!io_result_conv.has_value) { - this.transMat(i_square, i_direction, i_width, io_result_conv); + if (!o_result_conv.has_value) { + this.transMat(i_square, i_direction, i_width, o_result_conv); return; } //計算用に頂点情報を初期化(順番調整) initVertexOrder(i_square, i_direction, sqvertex_ref,linear_ref); + - //基準矩形を設定 + //平行移動量計算機に、2D座標系をセット + NyARDoublePoint2d[] vertex_2d=this.__transMat_vertex_2d; + NyARDoublePoint3d[] vertex_3d=this.__transMat_vertex_3d; + this._ref_dist_factor.ideal2ObservBatch(sqvertex_ref, vertex_2d,4); + this._transsolver.set2dVertex(vertex_2d,4); + + //基準矩形の3D座標系を作成 this._offset.setSquare(i_width,this._center); - // rotationを矩形情報を一つ前の変換行列で初期化 - this._rotmatrix.initRotByPrevResult(io_result_conv); - - //平行移動量計算機に、オフセット頂点をセット - this._calculator.setOffsetSquare(this._offset); + //回転行列を計算 + this._rotmatrix.initRotByPrevResult(o_result_conv); - //平行移動量計算機に、適応先矩形の情報をセット - this._calculator.setFittedSquare(sqvertex_ref); - - //回転行列の平行移動量の計算 - this._calculator.calculateTransfer(this._rotmatrix,trans); + //回転後の3D座標系から、平行移動量を計算 + this._rotmatrix.getPoint3dBatch(this._offset.vertex,vertex_3d,4); + this._transsolver.solveTransportVector(vertex_3d,trans); - //計算結果の最適化(this._rotmatrix,trans) - final double err=this._mat_optimize.optimize(this._rotmatrix,trans,this._calculator); + //計算結果の最適化(平行移動量と回転行列の最適化) + double err=this.optimize(this._rotmatrix, trans, this._transsolver, this._offset.vertex, vertex_2d); + + // マトリクスの保存 + this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv); - //計算結果を保存 - this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,io_result_conv); - // エラー値が許容範囲でなければTransMatをやり直し if (err > AR_GET_TRANS_CONT_MAT_MAX_FIT_ERROR) { // rotationを矩形情報で初期化 this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref); //回転行列の平行移動量の計算 - this._calculator.calculateTransfer(this._rotmatrix,trans); + this._rotmatrix.getPoint3dBatch(this._offset.vertex,vertex_3d,4); + this._transsolver.solveTransportVector(vertex_3d,trans); //計算結果の最適化(this._rotmatrix,trans) - final double err2=this._mat_optimize.optimize(this._rotmatrix,trans,this._calculator); + final double err2=this.optimize(this._rotmatrix, trans, this._transsolver, this._offset.vertex, vertex_2d); //エラー値が低かったら値を差換え if (err2 < err) { // 良い値が取れたら、差換え - this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,io_result_conv); + this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv); } } return; } + private double optimize(NyARRotMatrix io_rotmat,NyARDoublePoint3d io_transvec,INyARTransportVectorSolver i_solver,NyARDoublePoint3d[] i_offset_3d,NyARDoublePoint2d[] i_2d_vertex) throws NyARException + { + NyARDoublePoint3d[] vertex_3d=this.__transMat_vertex_3d; + double err = -1; + /* ループを抜けるタイミングをARToolKitと合わせるために変なことしてます。 */ + for (int i = 0;; i++) { + // + err = this._mat_optimize.modifyMatrix(io_rotmat, io_transvec, i_offset_3d, i_2d_vertex); + io_rotmat.getPoint3dBatch(i_offset_3d,vertex_3d,4); + i_solver.solveTransportVector(vertex_3d, io_transvec); + + err = this._mat_optimize.modifyMatrix(io_rotmat, io_transvec, i_offset_3d, i_2d_vertex); + // // + if (err < AR_GET_TRANS_MAT_MAX_FIT_ERROR || i == AR_GET_TRANS_MAT_MAX_LOOP_COUNT - 1) { + break; + } + io_rotmat.getPoint3dBatch(i_offset_3d,vertex_3d,4); + i_solver.solveTransportVector(vertex_3d, io_transvec); + } + return err; + } /** * パラメータで変換行列を更新します。 * diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/transmat/fitveccalc/NyARFitVecCalculator.java b/trunk/src/jp/nyatla/nyartoolkit/core/transmat/fitveccalc/NyARFitVecCalculator.java deleted file mode 100644 index 5830d02..0000000 --- a/trunk/src/jp/nyatla/nyartoolkit/core/transmat/fitveccalc/NyARFitVecCalculator.java +++ /dev/null @@ -1,181 +0,0 @@ -/* - * PROJECT: NyARToolkit - * -------------------------------------------------------------------------------- - * This work is based on the original ARToolKit developed by - * Hirokazu Kato - * Mark Billinghurst - * HITLab, University of Washington, Seattle - * http://www.hitl.washington.edu/artoolkit/ - * - * The NyARToolkit is Java version ARToolkit class library. - * Copyright (C)2008 R.Iizuka - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this framework; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * For further information please contact. - * http://nyatla.jp/nyatoolkit/ - * - * - */ -package jp.nyatla.nyartoolkit.core.transmat.fitveccalc; - -import jp.nyatla.nyartoolkit.core.*; -import jp.nyatla.nyartoolkit.core.transmat.NyARTransOffset; -import jp.nyatla.nyartoolkit.core.transmat.rotmatrix.NyARRotMatrix; -import jp.nyatla.nyartoolkit.core.types.*; -import jp.nyatla.nyartoolkit.*; -import jp.nyatla.nyartoolkit.core.param.*; -/** - * 平行移動量を計算するクラス - * - * NyARPerspectiveProjectionMatrixに直接アクセスしてる場所があるけど、 - * この辺の計算はNyARPerspectiveProjectionMatrixクラスの関数にして押し込む予定。 - * - */ -public class NyARFitVecCalculator -{ - private final NyARMat _mat_b = new NyARMat(3,8);//3,NUMBER_OF_VERTEX*2 - private final NyARMat _mat_a = new NyARMat(8,3);/*NUMBER_OF_VERTEX,3*/ - private final NyARMat _mat_d = new NyARMat(3,3); - private final NyARPerspectiveProjectionMatrix _projection_mat; - private final NyARCameraDistortionFactor _distortionfactor; - - -// private NyARDoublePoint2d[] _vertex_2d_ref; - public NyARFitVecCalculator(final NyARPerspectiveProjectionMatrix i_projection_mat_ref,final NyARCameraDistortionFactor i_distortion_ref) - { - // 変換マトリクスdとbの準備(arGetTransMatSubの一部) - final double[][] a_array = this._mat_a.getArray(); - final double[][] b_array = this._mat_b.getArray(); - - //変換用行列のcpara固定値の部分を先に初期化してしまう。 - for (int i = 0; i < 4; i++) { - final int x2 = i * 2; - a_array[x2][0] = b_array[0][x2] = i_projection_mat_ref.m00;// mat_a->m[j*6+0]=mat_b->m[num*0+j*2] =cpara[0][0]; - a_array[x2][1] = b_array[1][x2] = i_projection_mat_ref.m01;// mat_a->m[j*6+1]=mat_b->m[num*2+j*2]=cpara[0][1]; - //a_array[x2][2] = b_array[2][x2] = cpara[0 * 4 + 2] - o_marker_vertex_2d[i].x;// mat_a->m[j*6+2]=mat_b->m[num*4+j*2]=cpara[0][2]-pos2d[j][0]; - a_array[x2 + 1][0] = b_array[0][x2 + 1] = 0.0;// mat_a->m[j*6+3] =mat_b->m[num*0+j*2+1]= 0.0; - a_array[x2 + 1][1] = b_array[1][x2 + 1] = i_projection_mat_ref.m11;// mat_a->m[j*6+4] =mat_b->m[num*2+j*2+1]= cpara[1][1]; - //a_array[x2 + 1][2] = b_array[2][x2 + 1] = cpara[1 * 4 + 2] - o_marker_vertex_2d[i].y;// mat_a->m[j*6+5]=mat_b->m[num*4+j*2+1]=cpara[1][2]-pos2d[j][1]; - } - this._projection_mat=i_projection_mat_ref; - this._distortionfactor=i_distortion_ref; - return; - } - private final NyARDoublePoint2d[] _fitsquare_vertex=NyARDoublePoint2d.createArray(4);; - private NyARTransOffset _offset_square; - public void setOffsetSquare(NyARTransOffset i_offset) - { - this._offset_square=i_offset; - return; - } - public NyARDoublePoint2d[] getFitSquare() - { - return this._fitsquare_vertex; - } - public NyARTransOffset getOffsetVertex() - { - return this._offset_square; - } - - /** - * 適合させる矩形座標を指定します。 - * @param i_square_vertex - * @throws NyARException - */ - public void setFittedSquare(NyARDoublePoint2d[] i_square_vertex) throws NyARException - { - final NyARDoublePoint2d[] vertex=_fitsquare_vertex; -// int i; -// if (arFittingMode == AR_FITTING_TO_INPUT) { -// // arParamIdeal2Observをバッチ処理 - this._distortionfactor.ideal2ObservBatch(i_square_vertex, vertex,4); -// } else { -// for (i = 0; i < NUMBER_OF_VERTEX; i++) { -// o_marker_vertex_2d[i].x = i_square_vertex[i].x; -// o_marker_vertex_2d[i].y = i_square_vertex[i].y; -// } -// } - - - final double cpara02=this._projection_mat.m02; - final double cpara12=this._projection_mat.m12; - final NyARMat mat_d=_mat_d; - final NyARMat mat_a=this._mat_a; - final NyARMat mat_b=this._mat_b; - final double[][] a_array = mat_a.getArray(); - final double[][] b_array = mat_b.getArray(); - for (int i = 0; i < 4; i++) { - final int x2 = i * 2; - a_array[x2][2] = b_array[2][x2] = cpara02 - vertex[i].x;// mat_a->m[j*6+2]=mat_b->m[num*4+j*2]=cpara[0][2]-pos2d[j][0]; - a_array[x2 + 1][2] = b_array[2][x2 + 1] = cpara12 - vertex[i].y;// mat_a->m[j*6+5]=mat_b->m[num*4+j*2+1]=cpara[1][2]-pos2d[j][1]; - } - // mat_d - mat_d.matrixMul(mat_b, mat_a); - mat_d.matrixSelfInv(); - return; - } - private final NyARMat _mat_e = new NyARMat(3, 1); - private final NyARMat _mat_f = new NyARMat(3, 1); - private final NyARMat __calculateTransferVec_mat_c = new NyARMat(8, 1);//NUMBER_OF_VERTEX * 2, 1 - private final NyARDoublePoint3d[] __calculateTransfer_point3d=NyARDoublePoint3d.createArray(4); - - /** - * 現在のオフセット矩形、適合先矩形と、回転行列から、平行移動量を計算します。 - * @param i_rotation - * @param o_transfer - * @throws NyARException - */ - final public void calculateTransfer(NyARRotMatrix i_rotation,NyARDoublePoint3d o_transfer) throws NyARException - { - assert(this._offset_square!=null); - final double cpara00=this._projection_mat.m00; - final double cpara01=this._projection_mat.m01; - final double cpara02=this._projection_mat.m02; - final double cpara11=this._projection_mat.m11; - final double cpara12=this._projection_mat.m12; - - final NyARDoublePoint3d[] point3d=this.__calculateTransfer_point3d; - final NyARDoublePoint3d[] vertex3d=this._offset_square.vertex; - final NyARDoublePoint2d[] vertex2d=this._fitsquare_vertex; - final NyARMat mat_c = this.__calculateTransferVec_mat_c;// 次処理で値をもらうので、初期化の必要は無い。 - - final double[][] f_array = this._mat_f.getArray(); - final double[][] c_array = mat_c.getArray(); - - - //(3D座標?)を一括請求 - i_rotation.getPoint3dBatch(vertex3d,point3d,4); - for (int i = 0; i < 4; i++) { - final int x2 = i+i; - final NyARDoublePoint3d point3d_ptr=point3d[i]; -// i_rotation.getPoint3d(vertex3d[i],point3d); - //透視変換? - c_array[x2][0] = point3d_ptr.z * vertex2d[i].x - cpara00 * point3d_ptr.x - cpara01 * point3d_ptr.y - cpara02 * point3d_ptr.z;// mat_c->m[j*2+0] = wz*pos2d[j][0]-cpara[0][0]*wx-cpara[0][1]*wy-cpara[0][2]*wz; - c_array[x2 + 1][0] = point3d_ptr.z * vertex2d[i].y - cpara11 * point3d_ptr.y - cpara12 * point3d_ptr.z;// mat_c->m[j*2+1]= wz*pos2d[j][1]-cpara[1][1]*wy-cpara[1][2]*wz; - } - this._mat_e.matrixMul(this._mat_b, mat_c); - this._mat_f.matrixMul(this._mat_d, this._mat_e); - - // double[] trans=wk_arGetTransMatSub_trans;//double trans[3]; - o_transfer.x= f_array[0][0];// trans[0] = mat_f->m[0]; - o_transfer.y= f_array[1][0]; - o_transfer.z= f_array[2][0];// trans[2] = mat_f->m[2]; - return; - } - - - -} diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/INyARRotTransOptimize.java b/trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/INyARRotMatrixOptimize.java similarity index 63% rename from trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/INyARRotTransOptimize.java rename to trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/INyARRotMatrixOptimize.java index 879b9a7..2dbcb14 100644 --- a/trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/INyARRotTransOptimize.java +++ b/trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/INyARRotMatrixOptimize.java @@ -32,12 +32,26 @@ package jp.nyatla.nyartoolkit.core.transmat.optimize; import jp.nyatla.nyartoolkit.NyARException; -import jp.nyatla.nyartoolkit.core.transmat.fitveccalc.NyARFitVecCalculator; import jp.nyatla.nyartoolkit.core.transmat.rotmatrix.NyARRotMatrix; -import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d; +import jp.nyatla.nyartoolkit.core.types.*; -public interface INyARRotTransOptimize +public interface INyARRotMatrixOptimize { - public double optimize(NyARRotMatrix io_rotmat,NyARDoublePoint3d io_transvec,NyARFitVecCalculator i_calculator) throws NyARException; - + /** + * @param io_rotmat + * 初期回転行列 + * @param io_transvec + * 初期並進ベクトル + * @param i_solver + * i_offset_3dを設定したsolver + * @param i_offset_3d + * 3dオフセット座標 + * @param i_2d_vertex + * 画面上の点 + * @return + * @throws NyARException + */ +// public double optimize(NyARRotMatrix io_rotmat,NyARDoublePoint3d io_transvec,INyARTransportVectorSolver i_solver,NyARDoublePoint3d[] i_offset_3d,NyARDoublePoint2d[] i_2d_vertex) throws NyARException; + public double modifyMatrix(NyARRotMatrix io_rot, NyARDoublePoint3d i_trans, NyARDoublePoint3d[] i_vertex3d, NyARDoublePoint2d[] i_vertex2d) throws NyARException; + } diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotTransOptimize.java b/trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotMatrixOptimize.java similarity index 88% rename from trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotTransOptimize.java rename to trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotMatrixOptimize.java index 9b6f5bb..a51677a 100644 --- a/trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotTransOptimize.java +++ b/trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotMatrixOptimize.java @@ -34,8 +34,8 @@ package jp.nyatla.nyartoolkit.core.transmat.optimize; import jp.nyatla.nyartoolkit.NyARException; import jp.nyatla.nyartoolkit.core.param.*; -import jp.nyatla.nyartoolkit.core.transmat.fitveccalc.NyARFitVecCalculator; import jp.nyatla.nyartoolkit.core.transmat.rotmatrix.NyARRotMatrix; +import jp.nyatla.nyartoolkit.core.transmat.solver.INyARTransportVectorSolver; import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d; import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d; /** @@ -43,34 +43,30 @@ import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d; * を繰り返して、変換行列を最適化する。 * */ -public class NyARRotTransOptimize implements INyARRotTransOptimize +public class NyARRotMatrixOptimize implements INyARRotMatrixOptimize { private final static int AR_GET_TRANS_MAT_MAX_LOOP_COUNT = 5;// #define AR_GET_TRANS_MAT_MAX_LOOP_COUNT 5 private final static double AR_GET_TRANS_MAT_MAX_FIT_ERROR = 1.0;// #define AR_GET_TRANS_MAT_MAX_FIT_ERROR 1.0 private final NyARPerspectiveProjectionMatrix _projection_mat_ref; - public NyARRotTransOptimize(NyARPerspectiveProjectionMatrix i_projection_mat_ref) + public NyARRotMatrixOptimize(NyARPerspectiveProjectionMatrix i_projection_mat_ref) { this._projection_mat_ref=i_projection_mat_ref; return; } - - final public double optimize(NyARRotMatrix io_rotmat,NyARDoublePoint3d io_transvec,NyARFitVecCalculator i_calculator) throws NyARException + final public double optimize(NyARRotMatrix io_rotmat,NyARDoublePoint3d io_transvec,INyARTransportVectorSolver i_solver,NyARDoublePoint3d[] i_offset_3d,NyARDoublePoint2d[] i_2d_vertex) throws NyARException { - final NyARDoublePoint2d[] fit_vertex=i_calculator.getFitSquare(); - final NyARDoublePoint3d[] offset_square=i_calculator.getOffsetVertex().vertex; - double err = -1; /*ループを抜けるタイミングをARToolKitと合わせるために変なことしてます。*/ for (int i = 0;; i++) { // - err = modifyMatrix(io_rotmat,io_transvec,offset_square,fit_vertex); - i_calculator.calculateTransfer(io_rotmat, io_transvec); - err = modifyMatrix(io_rotmat,io_transvec,offset_square,fit_vertex); + err = modifyMatrix(io_rotmat,io_transvec,i_offset_3d,i_2d_vertex); + i_solver.solveTransportVector(i_offset_3d, io_transvec); + err = modifyMatrix(io_rotmat,io_transvec,i_offset_3d,i_2d_vertex); // // if (err < AR_GET_TRANS_MAT_MAX_FIT_ERROR || i == AR_GET_TRANS_MAT_MAX_LOOP_COUNT-1) { break; } - i_calculator.calculateTransfer(io_rotmat, io_transvec); + i_solver.solveTransportVector(i_offset_3d, io_transvec); } return err; } @@ -88,7 +84,7 @@ public class NyARRotTransOptimize implements INyARRotTransOptimize * @return * @throws NyARException */ - private double modifyMatrix(NyARRotMatrix io_rot,NyARDoublePoint3d trans, NyARDoublePoint3d[] i_vertex3d, NyARDoublePoint2d[] i_vertex2d) throws NyARException + public double modifyMatrix(NyARRotMatrix io_rot,NyARDoublePoint3d trans, NyARDoublePoint3d[] i_vertex3d, NyARDoublePoint2d[] i_vertex2d) throws NyARException { double factor; double a2, b2, c2; diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotTransOptimize_Base.java b/trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotMatrixOptimize_Base.java similarity index 78% rename from trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotTransOptimize_Base.java rename to trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotMatrixOptimize_Base.java index bf5ca7b..de3afce 100644 --- a/trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotTransOptimize_Base.java +++ b/trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotMatrixOptimize_Base.java @@ -33,7 +33,6 @@ package jp.nyatla.nyartoolkit.core.transmat.optimize; import jp.nyatla.nyartoolkit.NyARException; import jp.nyatla.nyartoolkit.core.param.*; -import jp.nyatla.nyartoolkit.core.transmat.fitveccalc.NyARFitVecCalculator; import jp.nyatla.nyartoolkit.core.transmat.rotmatrix.NyARRotMatrix; import jp.nyatla.nyartoolkit.core.types.matrix.*; import jp.nyatla.nyartoolkit.core.types.*; @@ -42,40 +41,15 @@ import jp.nyatla.nyartoolkit.core.types.*; * 処理構造がわかる程度に展開したNyARRotTransOptimize * */ -public class NyARRotTransOptimize_Base implements INyARRotTransOptimize +public class NyARRotMatrixOptimize_Base implements INyARRotMatrixOptimize { - private final static int AR_GET_TRANS_MAT_MAX_LOOP_COUNT = 5;// #define AR_GET_TRANS_MAT_MAX_LOOP_COUNT 5 - - private final static double AR_GET_TRANS_MAT_MAX_FIT_ERROR = 1.0;// #define AR_GET_TRANS_MAT_MAX_FIT_ERROR 1.0 - private final NyARPerspectiveProjectionMatrix _projection_mat_ref; - public NyARRotTransOptimize_Base(NyARPerspectiveProjectionMatrix i_projection_mat_ref) + public NyARRotMatrixOptimize_Base(NyARPerspectiveProjectionMatrix i_projection_mat_ref) { this._projection_mat_ref = i_projection_mat_ref; return; } - - final public double optimize(NyARRotMatrix io_rotmat, NyARDoublePoint3d io_transvec, NyARFitVecCalculator i_calculator) throws NyARException - { - final NyARDoublePoint2d[] fit_vertex = i_calculator.getFitSquare(); - final NyARDoublePoint3d[] offset_square = i_calculator.getOffsetVertex().vertex; - - double err = -1; - /* ループを抜けるタイミングをARToolKitと合わせるために変なことしてます。 */ - for (int i = 0;; i++) { - // - err = modifyMatrix(io_rotmat, io_transvec, offset_square, fit_vertex); - i_calculator.calculateTransfer(io_rotmat, io_transvec); - err = modifyMatrix(io_rotmat, io_transvec, offset_square, fit_vertex); - // // - if (err < AR_GET_TRANS_MAT_MAX_FIT_ERROR || i == AR_GET_TRANS_MAT_MAX_LOOP_COUNT - 1) { - break; - } - i_calculator.calculateTransfer(io_rotmat, io_transvec); - } - return err; - } private double[] __createRotationMap_b_map=new double[6]; private double[] __createRotationMap_c_map=new double[6]; private double[] __createRotationMap_f=new double[3]; @@ -167,7 +141,7 @@ public class NyARRotTransOptimize_Base implements INyARRotTransOptimize private final NyARDoublePoint3d __modifyMatrix_angle = new NyARDoublePoint3d(); private final NyARDoubleMatrix34 __modifyMatrix_combo=new NyARDoubleMatrix34(); private final NyARDoubleMatrix33[] __modifyMatrix_next_rot_matrix=NyARDoubleMatrix33.createArray(27); - public final double modifyMatrix(NyARRotMatrix io_rot, NyARDoublePoint3d trans, NyARDoublePoint3d[] i_vertex3d, NyARDoublePoint2d[] i_vertex2d) throws NyARException + public double modifyMatrix(NyARRotMatrix io_rot, NyARDoublePoint3d i_trans, NyARDoublePoint3d[] i_vertex3d, NyARDoublePoint2d[] i_vertex2d) throws NyARException { final NyARDoublePoint3d angle = this.__modifyMatrix_angle; final NyARDoubleMatrix34 combo=this.__modifyMatrix_combo; @@ -186,7 +160,7 @@ public class NyARRotTransOptimize_Base implements INyARRotTransOptimize //評価して一番宜しいIDを保存 best_idx=(1+1*3+1*9); for(i2=0;i2<27;i2++){ - this.getNewMatrix(next_rot_matrix[i2],trans,combo); + this.getNewMatrix(next_rot_matrix[i2],i_trans,combo); err = 0.0; for (i = 0; i < 4; i++) { hx = combo.m00 * i_vertex3d[i].x + combo.m01 * i_vertex3d[i].y + combo.m02 * i_vertex3d[i].z + combo.m03; diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotTransOptimize_O2.java b/trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotMatrixOptimize_O2.java similarity index 82% rename from trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotTransOptimize_O2.java rename to trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotMatrixOptimize_O2.java index c322f48..b3dffaa 100644 --- a/trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotTransOptimize_O2.java +++ b/trunk/src/jp/nyatla/nyartoolkit/core/transmat/optimize/NyARRotMatrixOptimize_O2.java @@ -34,7 +34,6 @@ package jp.nyatla.nyartoolkit.core.transmat.optimize; import jp.nyatla.nyartoolkit.NyARException; import jp.nyatla.nyartoolkit.core.param.*; -import jp.nyatla.nyartoolkit.core.transmat.fitveccalc.NyARFitVecCalculator; import jp.nyatla.nyartoolkit.core.transmat.rotmatrix.NyARRotMatrix; import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d; import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d; @@ -43,38 +42,14 @@ import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d; * を繰り返して、変換行列を最適化する。 * */ -public class NyARRotTransOptimize_O2 implements INyARRotTransOptimize +public class NyARRotMatrixOptimize_O2 implements INyARRotMatrixOptimize { - private final static int AR_GET_TRANS_MAT_MAX_LOOP_COUNT = 5;// #define AR_GET_TRANS_MAT_MAX_LOOP_COUNT 5 - private final static double AR_GET_TRANS_MAT_MAX_FIT_ERROR = 1.0;// #define AR_GET_TRANS_MAT_MAX_FIT_ERROR 1.0 private final NyARPerspectiveProjectionMatrix _projection_mat_ref; - public NyARRotTransOptimize_O2(NyARPerspectiveProjectionMatrix i_projection_mat_ref) + public NyARRotMatrixOptimize_O2(NyARPerspectiveProjectionMatrix i_projection_mat_ref) { this._projection_mat_ref=i_projection_mat_ref; return; } - - final public double optimize(NyARRotMatrix io_rotmat,NyARDoublePoint3d io_transvec,NyARFitVecCalculator i_calculator) throws NyARException - { - final NyARDoublePoint2d[] fit_vertex=i_calculator.getFitSquare(); - final NyARDoublePoint3d[] offset_square=i_calculator.getOffsetVertex().vertex; - - double err = -1; - /*ループを抜けるタイミングをARToolKitと合わせるために変なことしてます。*/ - for (int i = 0;; i++) { - // - err = modifyMatrix(io_rotmat,io_transvec,offset_square,fit_vertex); - i_calculator.calculateTransfer(io_rotmat, io_transvec); - err = modifyMatrix(io_rotmat,io_transvec,offset_square,fit_vertex); - // // - if (err < AR_GET_TRANS_MAT_MAX_FIT_ERROR || i == AR_GET_TRANS_MAT_MAX_LOOP_COUNT-1) { - break; - } - i_calculator.calculateTransfer(io_rotmat, io_transvec); - } - return err; - } - private final double[][] __modifyMatrix_double1D = new double[8][3]; /** * arGetRot計算を階層化したModifyMatrix 896 @@ -87,7 +62,7 @@ public class NyARRotTransOptimize_O2 implements INyARRotTransOptimize * @return * @throws NyARException */ - private double modifyMatrix(NyARRotMatrix io_rot,NyARDoublePoint3d trans, NyARDoublePoint3d[] i_vertex3d, NyARDoublePoint2d[] i_vertex2d) throws NyARException + public double modifyMatrix(NyARRotMatrix io_rot,NyARDoublePoint3d trans, NyARDoublePoint3d[] i_vertex3d, NyARDoublePoint2d[] i_vertex2d) throws NyARException { double factor; double a2, b2, c2; diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/transmat/solver/INyARTransportVectorSolver.java b/trunk/src/jp/nyatla/nyartoolkit/core/transmat/solver/INyARTransportVectorSolver.java new file mode 100644 index 0000000..c85b6cf --- /dev/null +++ b/trunk/src/jp/nyatla/nyartoolkit/core/transmat/solver/INyARTransportVectorSolver.java @@ -0,0 +1,57 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.transmat.solver; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d; +import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d; + +/** + * 並進ベクトル[T]を3次元座標[b]と基点の回転済行列[M]から計算するインタフェイスです。 + * [M][T]=[b] + * + */ +public interface INyARTransportVectorSolver +{ + public void set2dVertex(NyARDoublePoint2d[] i_ref_vertex_2d,int i_number_of_vertex) throws NyARException; + /** + * 画面座標群と3次元座標群から、平行移動量を計算します。 + * 2d座標系は、直前に実行したset2dVertexのものを使用します。 + * @param i_vertex_2d + * 直前のset2dVertexコールで指定したものと同じものを指定してください。 + * @param i_vertex3d + * 3次元空間の座標群を設定します。頂点の順番は、画面座標群と同じ順序で格納してください。 + * @param o_transfer + * @throws NyARException + */ + public void solveTransportVector(NyARDoublePoint3d[] i_vertex3d,NyARDoublePoint3d o_transfer) throws NyARException; +} \ No newline at end of file diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/transmat/solver/NyARTransportVectorSolver.java b/trunk/src/jp/nyatla/nyartoolkit/core/transmat/solver/NyARTransportVectorSolver.java new file mode 100644 index 0000000..0e6377d --- /dev/null +++ b/trunk/src/jp/nyatla/nyartoolkit/core/transmat/solver/NyARTransportVectorSolver.java @@ -0,0 +1,156 @@ +/* + * PROJECT: NyARToolkit(Extension) + * -------------------------------------------------------------------------------- + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.core.transmat.solver; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.param.NyARPerspectiveProjectionMatrix; +import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d; +import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d; + +/** + * 並進ベクトル[T]を3次元座標[b]と基点の回転済行列[M]から計算します。 + * + * アルゴリズムは、ARToolKit 拡張現実プログラミング入門 の、P207のものです。 + * + * 計算手順 + * [A]*[T]=bを、[A]T*[A]*[T]=[A]T*[b]にする。 + * set2dVertexで[A]T*[A]=[M]を計算して、Aの3列目の情報だけ保存しておく。 + * getTransportVectorで[M]*[T]=[A]T*[b]を連立方程式で解いて、[T]を得る。 + */ +public class NyARTransportVectorSolver implements INyARTransportVectorSolver +{ + private double[] _cx; + private double[] _cy; + private final NyARPerspectiveProjectionMatrix _projection_mat; + private int _nmber_of_vertex; + public NyARTransportVectorSolver(final NyARPerspectiveProjectionMatrix i_projection_mat_ref,int i_max_vertex) + { + this._projection_mat=i_projection_mat_ref; + this._cx=new double[i_max_vertex]; + this._cy=new double[i_max_vertex]; + return; + } + private double _a00,_a01,_a02_20,_a10,_a11,_a12_21,_a22; + /** + * 画面上の座標群を指定します。 + * @param i_ref_vertex_2d + * 歪み矯正済の画面上の頂点座標群への参照値を指定します。 + * @throws NyARException + * + */ + public void set2dVertex(NyARDoublePoint2d[] i_ref_vertex_2d,int i_number_of_vertex) throws NyARException + { + //3x2nと2n*3の行列から、最小二乗法計算するために3x3マトリクスを作る。 + //[A]*[A]T + //行列[A]の3列目のキャッシュ + final double[] cx=this._cx; + final double[] cy=this._cy; + + double m22; + double p00=this._projection_mat.m00; + double p01=this._projection_mat.m01; + double p11=this._projection_mat.m11; + double p12=this._projection_mat.m12; + double p02=this._projection_mat.m02; + double w1,w2,w3,w4; + this._a00=i_number_of_vertex*p00*p00; + this._a01=i_number_of_vertex*p00*p01; + this._a10=i_number_of_vertex*p00*p01; + this._a11=i_number_of_vertex*(p01*p01+p11*p11); + + m22=0; + w1=w2=0; + for(int i=0;i + * + */ +package jp.nyatla.nyartoolkit.core.transmat.solver; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.NyARMat; +import jp.nyatla.nyartoolkit.core.param.NyARPerspectiveProjectionMatrix; +import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d; +import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d; + +/** + * 並進ベクトル[T]を3次元座標[b]と基点の回転済行列[M]から計算します。 + * ARToolKit互換の数値を計算します。 + * + */ +public class NyARTransportVectorSolver_ARToolKit implements INyARTransportVectorSolver +{ + private final NyARMat _mat_at = new NyARMat(3,8);//3,NUMBER_OF_VERTEX*2 + private final NyARMat _mat_a = new NyARMat(8,3);//NUMBER_OF_VERTEX,3 + private final NyARMat _mat_t = new NyARMat(3,3);//NUMBER_OF_VERTEX,3 + private final NyARMat _mat_c = new NyARMat(8,1);//NUMBER_OF_VERTEX * 2, 1 + private final NyARMat _mat_e = new NyARMat(3,1); + private final NyARMat _mat_f = new NyARMat(3,1); + private double[] _cx=new double[4]; + private double[] _cy=new double[4]; + + private final NyARPerspectiveProjectionMatrix _projection_mat; + public NyARTransportVectorSolver_ARToolKit(final NyARPerspectiveProjectionMatrix i_projection_mat_ref) + { + this._projection_mat=i_projection_mat_ref; + //aとb(aの転置行列)の固定部分を設定。 + final double[][] mata = this._mat_a.getArray(); + final double[][] matat = this._mat_at.getArray(); + + //変換用行列のcpara部分を先に作成 + for (int i = 0; i < 4; i++) { + final int x2 = i * 2; + mata[x2][0] = matat[0][x2] = i_projection_mat_ref.m00;// mat_a->m[j*6+0]=mat_b->m[num*0+j*2] =cpara[0][0]; + mata[x2][1] = matat[1][x2] = i_projection_mat_ref.m01;// mat_a->m[j*6+1]=mat_b->m[num*2+j*2]=cpara[0][1]; + mata[x2 + 1][0] = matat[0][x2 + 1] = 0.0;// mat_a->m[j*6+3] =mat_b->m[num*0+j*2+1]= 0.0; + mata[x2 + 1][1] = matat[1][x2 + 1] = i_projection_mat_ref.m11;// mat_a->m[j*6+4] =mat_b->m[num*2+j*2+1]= cpara[1][1]; + } + return; + } + public void set2dVertex(NyARDoublePoint2d[] i_ref_vertex_2d,int i_number_of_vertex) throws NyARException + { + assert(i_number_of_vertex==4); + final double[] cx=this._cx; + final double[] cy=this._cy; + final double cpara02=this._projection_mat.m02; + final double cpara12=this._projection_mat.m12; + final NyARMat mat_t=this._mat_t; + final double[][] mata = this._mat_a.getArray(); + final double[][] matat= this._mat_at.getArray(); + for (int i = 0; i < 4; i++){ + cx[i]=i_ref_vertex_2d[i].x; + cy[i]=i_ref_vertex_2d[i].y; + final int x2 = i * 2; + mata[x2][2] = matat[2][x2] = cpara02 - i_ref_vertex_2d[i].x;// mat_a->m[j*6+2]=mat_b->m[num*4+j*2]=cpara[0][2]-pos2d[j][0]; + mata[x2 + 1][2] = matat[2][x2 + 1] = cpara12 - i_ref_vertex_2d[i].y;// mat_a->m[j*6+5]=mat_b->m[num*4+j*2+1]=cpara[1][2]-pos2d[j][1]; + } + //T(3x3行列)の作成 + mat_t.matrixMul(this._mat_at, this._mat_a); + mat_t.matrixSelfInv(); + return; + } + /** + * 画面座標群と3次元座標群から、平行移動量を計算します。 + * 2d座標系は、直前に実行したset2dVertexのものを使用します。 + * @param i_vertex_2d + * 直前のset2dVertexコールで指定したものと同じものを指定してください。 + * @param i_vertex3d + * 3次元空間の座標群を設定します。頂点の順番は、画面座標群と同じ順序で格納してください。 + * @param o_transfer + * @throws NyARException + */ + public void solveTransportVector(NyARDoublePoint3d[] i_vertex3d,NyARDoublePoint3d o_transfer) throws NyARException + { + final double[][] matc = this._mat_c.getArray(); + final double cpara00=this._projection_mat.m00; + final double cpara01=this._projection_mat.m01; + final double cpara02=this._projection_mat.m02; + final double cpara11=this._projection_mat.m11; + final double cpara12=this._projection_mat.m12; + final double[] cx=this._cx; + final double[] cy=this._cy; + + //(3D座標?)を一括請求 + for (int i = 0; i < 4; i++) { + final int x2 = i+i; + final NyARDoublePoint3d point3d_ptr=i_vertex3d[i]; + //透視変換? + matc[x2][0] = point3d_ptr.z * cx[i] - cpara00 * point3d_ptr.x - cpara01 * point3d_ptr.y - cpara02 * point3d_ptr.z;// mat_c->m[j*2+0] = wz*pos2d[j][0]-cpara[0][0]*wx-cpara[0][1]*wy-cpara[0][2]*wz; + matc[x2 + 1][0] = point3d_ptr.z * cy[i] - cpara11 * point3d_ptr.y - cpara12 * point3d_ptr.z;// mat_c->m[j*2+1]= wz*pos2d[j][1]-cpara[1][1]*wy-cpara[1][2]*wz; + } + this._mat_e.matrixMul(this._mat_at,this._mat_c); + this._mat_f.matrixMul(this._mat_t, this._mat_e); + + final double[][] matf = this._mat_f.getArray(); + o_transfer.x= matf[0][0];// trans[0] = mat_f->m[0]; + o_transfer.y= matf[1][0]; + o_transfer.z= matf[2][0];// trans[2] = mat_f->m[2]; + return; + } +} diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix22.java b/trunk/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix22.java index 27097b2..3c57b18 100644 --- a/trunk/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix22.java +++ b/trunk/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix22.java @@ -1,12 +1,6 @@ /* - * PROJECT: NyARToolkit + * PROJECT: NyARToolkit(Extension) * -------------------------------------------------------------------------------- - * This work is based on the original ARToolKit developed by - * Hirokazu Kato - * Mark Billinghurst - * HITLab, University of Washington, Seattle - * http://www.hitl.washington.edu/artoolkit/ - * * The NyARToolkit is Java version ARToolkit class library. * Copyright (C)2008 R.Iizuka * @@ -59,4 +53,22 @@ public class NyARDoubleMatrix22 implements INyARDoubleMatrix o_value[4]=this.m11; return; } + public boolean inverse(NyARDoubleMatrix22 i_src) + { + final double a11,a12,a21,a22; + a11=i_src.m00; + a12=i_src.m01; + a21=i_src.m10; + a22=i_src.m11; + double det=a11*a22-a12*a21; + if(det==0){ + return false; + } + det=1/det; + this.m00=a22*det; + this.m01=-a12*det; + this.m10=a21*det; + this.m11=-a11*det; + return true; + } } diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix33.java b/trunk/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix33.java index 9c0727b..1fa9733 100644 --- a/trunk/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix33.java +++ b/trunk/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix33.java @@ -1,12 +1,6 @@ /* - * PROJECT: NyARToolkit + * PROJECT: NyARToolkit(Extension) * -------------------------------------------------------------------------------- - * This work is based on the original ARToolKit developed by - * Hirokazu Kato - * Mark Billinghurst - * HITLab, University of Washington, Seattle - * http://www.hitl.washington.edu/artoolkit/ - * * The NyARToolkit is Java version ARToolkit class library. * Copyright (C)2008 R.Iizuka * @@ -85,4 +79,45 @@ public class NyARDoubleMatrix33 implements INyARDoubleMatrix o_value[8]=this.m22; return; } + public boolean inverse(NyARDoubleMatrix33 i_src) + { + final double a11,a12,a13,a21,a22,a23,a31,a32,a33; + final double b11,b12,b13,b21,b22,b23,b31,b32,b33; + a11=i_src.m00;a12=i_src.m01;a13=i_src.m02; + a21=i_src.m10;a22=i_src.m11;a23=i_src.m12; + a31=i_src.m20;a32=i_src.m21;a33=i_src.m22; + + b11=a22*a33-a23*a32; + b12=a32*a13-a33*a12; + b13=a12*a23-a13*a22; + + b21=a13*a31-a21*a33; + b22=a33*a11-a31*a13; + b23=a13*a21-a11*a23; + + b31=a21*a32-a22*a31; + b32=a31*a12-a32*a11; + b33=a11*a22-a12*a21; + + double det_1=a11*b11+a21*b12+a31*b13; + if(det_1==0){ + return false; + } + det_1=1/det_1; + + this.m00=b11*det_1; + this.m01=b12*det_1; + this.m02=b13*det_1; + + this.m10=b21*det_1; + this.m11=b22*det_1; + this.m12=b23*det_1; + + this.m20=b31*det_1; + this.m21=b32*det_1; + this.m22=b33*det_1; + + return true; + } + } diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix34.java b/trunk/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix34.java index a719b2e..0611079 100644 --- a/trunk/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix34.java +++ b/trunk/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix34.java @@ -1,15 +1,9 @@ /* - * PROJECT: NyARToolkit + * PROJECT: NyARToolkit(Extension) * -------------------------------------------------------------------------------- - * This work is based on the original ARToolKit developed by - * Hirokazu Kato - * Mark Billinghurst - * HITLab, University of Washington, Seattle - * http://www.hitl.washington.edu/artoolkit/ - * * The NyARToolkit is Java version ARToolkit class library. * Copyright (C)2008 R.Iizuka - * + * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix44.java b/trunk/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix44.java index 6a1ca49..45f2da4 100644 --- a/trunk/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix44.java +++ b/trunk/src/jp/nyatla/nyartoolkit/core/types/matrix/NyARDoubleMatrix44.java @@ -1,12 +1,6 @@ /* - * PROJECT: NyARToolkit + * PROJECT: NyARToolkit(Extension) * -------------------------------------------------------------------------------- - * This work is based on the original ARToolKit developed by - * Hirokazu Kato - * Mark Billinghurst - * HITLab, University of Washington, Seattle - * http://www.hitl.washington.edu/artoolkit/ - * * The NyARToolkit is Java version ARToolkit class library. * Copyright (C)2008 R.Iizuka * @@ -105,4 +99,90 @@ public class NyARDoubleMatrix44 implements INyARDoubleMatrix o_value[15]=this.m33; return; } + public boolean inverse(NyARDoubleMatrix44 i_src) + { + final double a11,a12,a13,a14,a21,a22,a23,a24,a31,a32,a33,a34,a41,a42,a43,a44; + final double b11,b12,b13,b14,b21,b22,b23,b24,b31,b32,b33,b34,b41,b42,b43,b44; + double t1,t2,t3,t4,t5,t6; + a11=i_src.m00;a12=i_src.m01;a13=i_src.m02;a14=i_src.m03; + a21=i_src.m10;a22=i_src.m11;a23=i_src.m12;a24=i_src.m13; + a31=i_src.m20;a32=i_src.m21;a33=i_src.m22;a34=i_src.m23; + a41=i_src.m30;a42=i_src.m31;a43=i_src.m32;a44=i_src.m33; + + t1=a33*a44-a34*a43; + t2=a34*a42-a32*a44; + t3=a32*a43-a33*a42; + t4=a34*a41-a31*a44; + t5=a31*a43-a33*a41; + t6=a31*a42-a32*a41; + + b11=a22*t1+a23*t2+a24*t3; + b21=-(a23*t4+a24*t5+a21*t1); + b31=a24*t6-a21*t2+a22*t4; + b41=-(a21*t3-a22*t5+a23*t6); + + t1=a43*a14-a44*a13; + t2=a44*a12-a42*a14; + t3=a42*a13-a43*a12; + t4=a44*a11-a41*a14; + t5=a41*a13-a43*a11; + t6=a41*a12-a42*a11; + + b12=-(a32*t1+a33*t2+a34*t3); + b22=a33*t4+a34*t5+a31*t1; + b32=-(a34*t6-a31*t2+a32*t4); + b42=a31*t3-a32*t5+a33*t6; + + t1=a13*a24-a14*a23; + t2=a14*a22-a12*a24; + t3=a12*a23-a13*a22; + t4=a14*a21-a11*a24; + t5=a11*a23-a13*a21; + t6=a11*a22-a12*a21; + + b13=a42*t1+a43*t2+a44*t3; + b23=-(a43*t4+a44*t5+a41*t1); + b33=a44*t6-a41*t2+a42*t4; + b43=-(a41*t3-a42*t5+a43*t6); + + t1=a23*a34-a24*a33; + t2=a24*a32-a22*a34; + t3=a22*a33-a23*a32; + t4=a24*a31-a21*a34; + t5=a21*a33-a23*a31; + t6=a21*a32-a22*a31; + + b14=-(a12*t1+a13*t2+a14*t3); + b24=a13*t4+a14*t5+a11*t1; + b34=-(a14*t6-a11*t2+a12*t4); + b44=a11*t3-a12*t5+a13*t6; + + double det_1=(a11*b11+a21*b12+a31*b13+a41*b14); + if(det_1==0){ + return false; + } + det_1=1/det_1; + + this.m00=b11*det_1; + this.m01=b12*det_1; + this.m02=b13*det_1; + this.m03=b14*det_1; + + this.m10=b21*det_1; + this.m11=b22*det_1; + this.m12=b23*det_1; + this.m13=b24*det_1; + + this.m20=b31*det_1; + this.m21=b32*det_1; + this.m22=b33*det_1; + this.m23=b34*det_1; + + this.m30=b41*det_1; + this.m31=b42*det_1; + this.m32=b43*det_1; + this.m33=b44*det_1; + + return true; + } } diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/utils/NyARDoubleMatrixProcessor.java b/trunk/src/jp/nyatla/nyartoolkit/core/utils/NyARDoubleMatrixProcessor.java deleted file mode 100644 index 5961656..0000000 --- a/trunk/src/jp/nyatla/nyartoolkit/core/utils/NyARDoubleMatrixProcessor.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * PROJECT: NyARToolkit(Extension) - * -------------------------------------------------------------------------------- - * The NyARToolkit is Java version ARToolkit class library. - * Copyright (C)2008 R.Iizuka - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this framework; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * For further information please contact. - * http://nyatla.jp/nyatoolkit/ - * - * - */ -package jp.nyatla.nyartoolkit.core.utils; - -import jp.nyatla.nyartoolkit.NyARException; -import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix22; -import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix33; -import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix44; - -public class NyARDoubleMatrixProcessor -{ - /** - * i_srcの逆行列を計算して、thisへ格納します。 - * @param i_src - * @return - */ - public static boolean inverse(NyARDoubleMatrix22 i_src,NyARDoubleMatrix22 o_dest) - { - final double a11,a12,a21,a22; - a11=i_src.m00; - a12=i_src.m01; - a21=i_src.m10; - a22=i_src.m11; - double det=a11*a22-a12*a21; - if(det==0){ - return false; - } - det=1/det; - o_dest.m00=a22*det; - o_dest.m01=-a12*det; - o_dest.m10=a21*det; - o_dest.m11=-a11*det; - return true; - } - public static boolean inverse(NyARDoubleMatrix33 i_src,NyARDoubleMatrix33 o_dest) throws NyARException - { - /*i_srcの逆行列をthisへ格納するコードを書くこと。*/ - NyARException.notImplement(); - return false; - } - /** - * i_srcの逆行列を計算して、結果をo_destへセットします。 - * @param i_src - * @return - */ - public static boolean inverse(NyARDoubleMatrix44 i_src,NyARDoubleMatrix44 o_dest) - { - final double a11,a12,a13,a14,a21,a22,a23,a24,a31,a32,a33,a34,a41,a42,a43,a44; - final double b11,b12,b13,b14,b21,b22,b23,b24,b31,b32,b33,b34,b41,b42,b43,b44; - double t1,t2,t3,t4,t5,t6; - a11=i_src.m00;a12=i_src.m01;a13=i_src.m02;a14=i_src.m03; - a21=i_src.m10;a22=i_src.m11;a23=i_src.m12;a24=i_src.m13; - a31=i_src.m20;a32=i_src.m21;a33=i_src.m22;a34=i_src.m23; - a41=i_src.m30;a42=i_src.m31;a43=i_src.m32;a44=i_src.m33; - - t1=a33*a44-a34*a43; - t2=a34*a42-a32*a44; - t3=a32*a43-a33*a42; - t4=a34*a41-a31*a44; - t5=a31*a43-a33*a41; - t6=a31*a42-a32*a41; - - b11=a22*t1+a23*t2+a24*t3; - b21=-(a23*t4+a24*t5+a21*t1); - b31=a24*t6-a21*t2+a22*t4; - b41=-(a21*t3-a22*t5+a23*t6); - - t1=a43*a14-a44*a13; - t2=a44*a12-a42*a14; - t3=a42*a13-a43*a12; - t4=a44*a11-a41*a14; - t5=a41*a13-a43*a11; - t6=a41*a12-a42*a11; - - b12=-(a32*t1+a33*t2+a34*t3); - b22=a33*t4+a34*t5+a31*t1; - b32=-(a34*t6-a31*t2+a32*t4); - b42=a31*t3-a32*t5+a33*t6; - - t1=a13*a24-a14*a23; - t2=a14*a22-a12*a24; - t3=a12*a23-a13*a22; - t4=a14*a21-a11*a24; - t5=a11*a23-a13*a21; - t6=a11*a22-a12*a21; - - b13=a42*t1+a43*t2+a44*t3; - b23=-(a43*t4+a44*t5+a41*t1); - b33=a44*t6-a41*t2+a42*t4; - b43=-(a41*t3-a42*t5+a43*t6); - - t1=a23*a34-a24*a33; - t2=a24*a32-a22*a34; - t3=a22*a33-a23*a32; - t4=a24*a31-a21*a34; - t5=a21*a33-a23*a31; - t6=a21*a32-a22*a31; - - b14=-(a12*t1+a13*t2+a14*t3); - b24=a13*t4+a14*t5+a11*t1; - b34=-(a14*t6-a11*t2+a12*t4); - b44=a11*t3-a12*t5+a13*t6; - - double det_1=(a11*b11+a21*b12+a31*b13+a41*b14); - if(det_1==0){ - return false; - } - det_1=1/det_1; - - o_dest.m00=b11*det_1; - o_dest.m01=b12*det_1; - o_dest.m02=b13*det_1; - o_dest.m03=b14*det_1; - - o_dest.m10=b21*det_1; - o_dest.m11=b22*det_1; - o_dest.m12=b23*det_1; - o_dest.m13=b24*det_1; - - o_dest.m20=b31*det_1; - o_dest.m21=b32*det_1; - o_dest.m22=b33*det_1; - o_dest.m23=b34*det_1; - - o_dest.m30=b41*det_1; - o_dest.m31=b42*det_1; - o_dest.m32=b43*det_1; - o_dest.m33=b44*det_1; - - return true; - } -} diff --git a/trunk/src/jp/nyatla/nyartoolkit/detector/NyARSingleDetectMarker.java b/trunk/src/jp/nyatla/nyartoolkit/detector/NyARSingleDetectMarker.java index 6564f90..86434d9 100644 --- a/trunk/src/jp/nyatla/nyartoolkit/detector/NyARSingleDetectMarker.java +++ b/trunk/src/jp/nyatla/nyartoolkit/detector/NyARSingleDetectMarker.java @@ -46,7 +46,7 @@ import jp.nyatla.nyartoolkit.core.pickup.*; */ public class NyARSingleDetectMarker extends NyARCustomSingleDetectMarker { - public final static int PF_ARTOOLKIT_CONPATIBLE=1; + public final static int PF_ARTOOLKIT_COMPATIBLE=1; public final static int PF_NYARTOOLKIT=2; /** @@ -95,7 +95,7 @@ public class NyARSingleDetectMarker extends NyARCustomSingleDetectMarker INyARSquareDetector sqdetect_inst; switch(i_profile_id){ - case PF_ARTOOLKIT_CONPATIBLE: + case PF_ARTOOLKIT_COMPATIBLE: patt_inst=new NyARColorPatt_O3(i_ref_code.getWidth(), i_ref_code.getHeight()); sqdetect_inst=new NyARSquareDetector_ARToolKit(i_ref_param.getDistortionFactor(),i_ref_param.getScreenSize()); break; diff --git a/trunk/src/jp/nyatla/nyartoolkit/sample/SingleARMarkerTest.java b/trunk/src/jp/nyatla/nyartoolkit/sample/SingleARMarkerTest.java new file mode 100644 index 0000000..b821744 --- /dev/null +++ b/trunk/src/jp/nyatla/nyartoolkit/sample/SingleARMarkerTest.java @@ -0,0 +1,123 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.sample; + +import java.io.*; + + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.param.NyARParam; +import jp.nyatla.nyartoolkit.core.raster.rgb.*; +import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare; +import jp.nyatla.nyartoolkit.core.transmat.*; +import jp.nyatla.nyartoolkit.core.*; +import jp.nyatla.nyartoolkit.processor.*; + +public class SingleARMarkerTest +{ + class MarkerProcessor extends SingleARMarkerProcesser + { + private Object _sync_object=new Object(); + public NyARTransMatResult transmat=null; + public int current_code=-1; + + public MarkerProcessor(NyARParam i_cparam,int i_raster_format) throws NyARException + { + //アプリケーションフレームワークの初期化 + super(); + initInstance(i_cparam,i_raster_format); + return; + } + protected void onEnterHandler(int i_code) + { + synchronized(this._sync_object){ + current_code=i_code; + } + } + protected void onLeaveHandler() + { + synchronized(this._sync_object){ + current_code=-1; + this.transmat=null; + } + return; + } + + protected void onUpdateHandler(NyARSquare i_square, NyARTransMatResult result) + { + synchronized(this._sync_object){ + this.transmat=result; + } + } + } + private final static String CARCODE_FILE = "../Data/patt.hiro"; + private final static String PARAM_FILE = "../Data/camera_para.dat"; + private final String data_file = "../Data/320x240ABGR.raw"; + + public SingleARMarkerTest() + { + } + public void Test() throws Exception + { + //AR用カメラパラメタファイルをロード + NyARParam ap = new NyARParam(); + ap.loadARParamFromFile(PARAM_FILE); + ap.changeScreenSize(320, 240); + + // 試験イメージの読み出し(320x240 BGRAのRAWデータ) + File f = new File(data_file); + FileInputStream fs = new FileInputStream(data_file); + byte[] buf = new byte[(int) f.length()]; + fs.read(buf); + + NyARRgbRaster_BGRA ra = NyARRgbRaster_BGRA.wrap(buf, 320, 240); + + MarkerProcessor pr = new MarkerProcessor(ap, ra.getBufferReader().getBufferType()); + NyARCode[] codes=new NyARCode[1]; + codes[0]=new NyARCode(16,16); + codes[0].loadARPattFromFile(CARCODE_FILE); + pr.setARCodeTable(codes,16,80.0); + pr.detectMarker(ra); + return; + } + public static void main(String[] args) + { + + try { + SingleARMarkerTest t = new SingleARMarkerTest(); + // t.Test_arGetVersion(); + t.Test(); + } catch (Exception e) { + e.printStackTrace(); + } + } +}