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.optimize;
\r
35 import jp.nyatla.nyartoolkit.NyARException;
\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.rotmatrix.NyARRotMatrix;
\r
39 import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d;
\r
40 import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d;
\r
42 public class NyARRotTransOptimize
\r
44 private final static int AR_GET_TRANS_MAT_MAX_LOOP_COUNT = 5;// #define AR_GET_TRANS_MAT_MAX_LOOP_COUNT 5
\r
45 private final static double AR_GET_TRANS_MAT_MAX_FIT_ERROR = 1.0;// #define AR_GET_TRANS_MAT_MAX_FIT_ERROR 1.0
\r
46 private final NyARPerspectiveProjectionMatrix _projection_mat_ref;
\r
47 public NyARRotTransOptimize(NyARPerspectiveProjectionMatrix i_projection_mat_ref)
\r
49 this._projection_mat_ref=i_projection_mat_ref;
\r
53 final public double optimize(NyARRotMatrix io_rotmat,NyARDoublePoint3d io_transvec,NyARFitVecCalculator i_calculator) throws NyARException
\r
55 final NyARDoublePoint2d[] fit_vertex=i_calculator.getFitSquare();
\r
56 final NyARDoublePoint3d[] offset_square=i_calculator.getOffsetVertex().vertex;
\r
59 /*ループを抜けるタイミングをARToolKitと合わせるために変なことしてます。*/
\r
60 for (int i = 0;; i++) {
\r
62 err = modifyMatrix(io_rotmat,io_transvec,offset_square,fit_vertex);
\r
63 i_calculator.calculateTransfer(io_rotmat, io_transvec);
\r
64 err = modifyMatrix(io_rotmat,io_transvec,offset_square,fit_vertex);
\r
65 // //</arGetTransMat3>
\r
66 if (err < AR_GET_TRANS_MAT_MAX_FIT_ERROR || i == AR_GET_TRANS_MAT_MAX_LOOP_COUNT-1) {
\r
69 i_calculator.calculateTransfer(io_rotmat, io_transvec);
\r
74 private final double[][] __modifyMatrix_double1D = new double[8][3];
\r
75 private final NyARDoublePoint3d __modifyMatrix_angle = new NyARDoublePoint3d();
\r
77 * arGetRot計算を階層化したModifyMatrix 896
\r
86 * @throws NyARException
\r
88 private double modifyMatrix(NyARRotMatrix io_rot,NyARDoublePoint3d trans, NyARDoublePoint3d[] i_vertex3d, NyARDoublePoint2d i_vertex2d[]) throws NyARException
\r
92 double ma = 0.0, mb = 0.0, mc = 0.0;
\r
94 double err, minerr = 0;
\r
96 int s1 = 0, s2 = 0, s3 = 0;
\r
98 factor = 10.0 * Math.PI / 180.0;
\r
99 double rot0, rot1, rot3, rot4, rot6, rot7;
\r
100 double combo00, combo01, combo02, combo03, combo10, combo11, combo12, combo13, combo20, combo21, combo22, combo23;
\r
101 double combo02_2, combo02_5, combo02_8, combo02_11;
\r
102 double combo22_2, combo22_5, combo22_8, combo22_11;
\r
103 double combo12_2, combo12_5, combo12_8, combo12_11;
\r
105 final double VX00, VX01, VX02, VX10, VX11, VX12, VX20, VX21, VX22, VX30, VX31, VX32;
\r
106 NyARDoublePoint3d d_pt;
\r
107 d_pt = i_vertex3d[0];
\r
111 d_pt = i_vertex3d[1];
\r
115 d_pt = i_vertex3d[2];
\r
119 d_pt = i_vertex3d[3];
\r
123 final double P2D00, P2D01, P2D10, P2D11, P2D20, P2D21, P2D30, P2D31;
\r
124 NyARDoublePoint2d d_pt2;
\r
125 d_pt2 = i_vertex2d[0];
\r
128 d_pt2 = i_vertex2d[1];
\r
131 d_pt2 = i_vertex2d[2];
\r
134 d_pt2 = i_vertex2d[3];
\r
137 final NyARPerspectiveProjectionMatrix prjmat = this._projection_mat_ref;
\r
138 final double CP0, CP1, CP2, CP4, CP5, CP6, CP8, CP9, CP10;
\r
148 combo03 = CP0 * trans.x + CP1 * trans.y + CP2 * trans.z + prjmat.m03;
\r
149 combo13 = CP4 * trans.x + CP5 * trans.y + CP6 * trans.z + prjmat.m13;
\r
150 combo23 = CP8 * trans.x + CP9 * trans.y + CP10 * trans.z + prjmat.m23;
\r
151 double CACA, SASA, SACA, CA, SA;
\r
152 double CACACB, SACACB, SASACB, CASB, SASB;
\r
153 double SACASC, SACACBSC, SACACBCC, SACACC;
\r
154 final double[][] double1D = this.__modifyMatrix_double1D;
\r
156 final NyARDoublePoint3d angle = this.__modifyMatrix_angle;
\r
158 final double[] a_factor = double1D[1];
\r
159 final double[] sinb = double1D[2];
\r
160 final double[] cosb = double1D[3];
\r
161 final double[] b_factor = double1D[4];
\r
162 final double[] sinc = double1D[5];
\r
163 final double[] cosc = double1D[6];
\r
164 final double[] c_factor = double1D[7];
\r
168 io_rot.getAngle(angle);// arGetAngle( rot, &a, &b, &c );
\r
174 for (int i = 0; i < 10; i++) {
\r
175 minerr = 1000000000.0;
\r
176 // sin-cosテーブルを計算(これが外に出せるとは…。)
\r
177 for (int j = 0; j < 3; j++) {
\r
178 w2 = factor * (j - 1);
\r
183 sinb[j] = Math.sin(w);
\r
184 cosb[j] = Math.cos(w);
\r
187 sinc[j] = Math.sin(w);
\r
188 cosc[j] = Math.cos(w);
\r
191 for (t1 = 0; t1 < 3; t1++) {
\r
192 SA = Math.sin(a_factor[t1]);
\r
193 CA = Math.cos(a_factor[t1]);
\r
198 for (t2 = 0; t2 < 3; t2++) {
\r
201 CACACB = CACA * wcos;
\r
202 SACACB = SACA * wcos;
\r
203 SASACB = SASA * wcos;
\r
207 combo02 = CP0 * CASB + CP1 * SASB + CP2 * wcos;
\r
208 combo12 = CP4 * CASB + CP5 * SASB + CP6 * wcos;
\r
209 combo22 = CP8 * CASB + CP9 * SASB + CP10 * wcos;
\r
211 combo02_2 = combo02 * VX02 + combo03;
\r
212 combo02_5 = combo02 * VX12 + combo03;
\r
213 combo02_8 = combo02 * VX22 + combo03;
\r
214 combo02_11 = combo02 * VX32 + combo03;
\r
215 combo12_2 = combo12 * VX02 + combo13;
\r
216 combo12_5 = combo12 * VX12 + combo13;
\r
217 combo12_8 = combo12 * VX22 + combo13;
\r
218 combo12_11 = combo12 * VX32 + combo13;
\r
219 combo22_2 = combo22 * VX02 + combo23;
\r
220 combo22_5 = combo22 * VX12 + combo23;
\r
221 combo22_8 = combo22 * VX22 + combo23;
\r
222 combo22_11 = combo22 * VX32 + combo23;
\r
223 for (t3 = 0; t3 < 3; t3++) {
\r
226 SACASC = SACA * wsin;
\r
227 SACACC = SACA * wcos;
\r
228 SACACBSC = SACACB * wsin;
\r
229 SACACBCC = SACACB * wcos;
\r
231 rot0 = CACACB * wcos + SASA * wcos + SACACBSC - SACASC;
\r
232 rot3 = SACACBCC - SACACC + SASACB * wsin + CACA * wsin;
\r
233 rot6 = -CASB * wcos - SASB * wsin;
\r
235 combo00 = CP0 * rot0 + CP1 * rot3 + CP2 * rot6;
\r
236 combo10 = CP4 * rot0 + CP5 * rot3 + CP6 * rot6;
\r
237 combo20 = CP8 * rot0 + CP9 * rot3 + CP10 * rot6;
\r
239 rot1 = -CACACB * wsin - SASA * wsin + SACACBCC - SACACC;
\r
240 rot4 = -SACACBSC + SACASC + SASACB * wcos + CACA * wcos;
\r
241 rot7 = CASB * wsin - SASB * wcos;
\r
242 combo01 = CP0 * rot1 + CP1 * rot4 + CP2 * rot7;
\r
243 combo11 = CP4 * rot1 + CP5 * rot4 + CP6 * rot7;
\r
244 combo21 = CP8 * rot1 + CP9 * rot4 + CP10 * rot7;
\r
247 h = combo20 * VX00 + combo21 * VX01 + combo22_2;
\r
248 x = P2D00 - (combo00 * VX00 + combo01 * VX01 + combo02_2) / h;
\r
249 y = P2D01 - (combo10 * VX00 + combo11 * VX01 + combo12_2) / h;
\r
250 err += x * x + y * y;
\r
251 h = combo20 * VX10 + combo21 * VX11 + combo22_5;
\r
252 x = P2D10 - (combo00 * VX10 + combo01 * VX11 + combo02_5) / h;
\r
253 y = P2D11 - (combo10 * VX10 + combo11 * VX11 + combo12_5) / h;
\r
254 err += x * x + y * y;
\r
255 h = combo20 * VX20 + combo21 * VX21 + combo22_8;
\r
256 x = P2D20 - (combo00 * VX20 + combo01 * VX21 + combo02_8) / h;
\r
257 y = P2D21 - (combo10 * VX20 + combo11 * VX21 + combo12_8) / h;
\r
258 err += x * x + y * y;
\r
259 h = combo20 * VX30 + combo21 * VX31 + combo22_11;
\r
260 x = P2D30 - (combo00 * VX30 + combo01 * VX31 + combo02_11) / h;
\r
261 y = P2D31 - (combo10 * VX30 + combo11 * VX31 + combo12_11) / h;
\r
262 err += x * x + y * y;
\r
263 if (err < minerr) {
\r
275 if (s1 == 0 && s2 == 0 && s3 == 0) {
\r
282 io_rot.setAngle(ma, mb, mc);
\r
283 /* printf("factor = %10.5f\n", factor*180.0/MD_PI); */
\r