OSDN Git Service

[バックアップ]NyARToolkit
[nyartoolkit-and/nyartoolkit-and.git] / trunk / src / jp / nyatla / nyartoolkit / core / transmat / optimize / NyARRotTransOptimize_O2.java
1 /* \r
2  * PROJECT: NyARToolkit\r
3  * --------------------------------------------------------------------------------\r
4  * This work is based on the original ARToolKit developed by\r
5  *   Hirokazu Kato\r
6  *   Mark Billinghurst\r
7  *   HITLab, University of Washington, Seattle\r
8  * http://www.hitl.washington.edu/artoolkit/\r
9  *\r
10  * The NyARToolkit is Java version ARToolkit class library.\r
11  * Copyright (C)2008 R.Iizuka\r
12  *\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
17  * \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
22  * \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
26  * \r
27  * For further information please contact.\r
28  *      http://nyatla.jp/nyatoolkit/\r
29  *      <airmail(at)ebony.plala.or.jp>\r
30  * \r
31  */\r
32 package jp.nyatla.nyartoolkit.core.transmat.optimize;\r
33 \r
34 \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
41 /**\r
42  * 基本姿勢と実画像を一致するように、角度を微調整→平行移動量を再計算\r
43  * を繰り返して、変換行列を最適化する。\r
44  *\r
45  */\r
46 public class NyARRotTransOptimize_O2 implements INyARRotTransOptimize\r
47 {\r
48         private final static int AR_GET_TRANS_MAT_MAX_LOOP_COUNT = 5;// #define AR_GET_TRANS_MAT_MAX_LOOP_COUNT 5\r
49         private final static double AR_GET_TRANS_MAT_MAX_FIT_ERROR = 1.0;// #define AR_GET_TRANS_MAT_MAX_FIT_ERROR 1.0\r
50         private final NyARPerspectiveProjectionMatrix _projection_mat_ref;\r
51         public NyARRotTransOptimize_O2(NyARPerspectiveProjectionMatrix i_projection_mat_ref)\r
52         {\r
53                 this._projection_mat_ref=i_projection_mat_ref;\r
54                 return;\r
55         }\r
56         \r
57         final public double optimize(NyARRotMatrix io_rotmat,NyARDoublePoint3d io_transvec,NyARFitVecCalculator i_calculator) throws NyARException\r
58         {\r
59                 final NyARDoublePoint2d[] fit_vertex=i_calculator.getFitSquare();\r
60                 final NyARDoublePoint3d[] offset_square=i_calculator.getOffsetVertex().vertex;\r
61                 \r
62                 double err = -1;\r
63                 /*ループを抜けるタイミングをARToolKitと合わせるために変なことしてます。*/\r
64                 for (int i = 0;; i++) {\r
65                         // <arGetTransMat3>\r
66                         err = modifyMatrix(io_rotmat,io_transvec,offset_square,fit_vertex);\r
67                         i_calculator.calculateTransfer(io_rotmat, io_transvec);\r
68                         err = modifyMatrix(io_rotmat,io_transvec,offset_square,fit_vertex);                     \r
69                         // //</arGetTransMat3>\r
70                         if (err < AR_GET_TRANS_MAT_MAX_FIT_ERROR || i == AR_GET_TRANS_MAT_MAX_LOOP_COUNT-1) {\r
71                                 break;\r
72                         }\r
73                         i_calculator.calculateTransfer(io_rotmat, io_transvec);\r
74                 }               \r
75                 return err;\r
76         }\r
77         \r
78         private final double[][] __modifyMatrix_double1D = new double[8][3];\r
79         private final NyARDoublePoint3d __modifyMatrix_angle = new NyARDoublePoint3d(); \r
80         /**\r
81          * arGetRot計算を階層化したModifyMatrix 896\r
82          * \r
83          * @param nyrot\r
84          * @param trans\r
85          * @param i_vertex3d\r
86          * [m][3]\r
87          * @param i_vertex2d\r
88          * [n][2]\r
89          * @return\r
90          * @throws NyARException\r
91          */\r
92         private double modifyMatrix(NyARRotMatrix io_rot,NyARDoublePoint3d trans, NyARDoublePoint3d[] i_vertex3d, NyARDoublePoint2d[] i_vertex2d) throws NyARException\r
93         {\r
94                 double factor;\r
95                 double a2, b2, c2;\r
96                 double h, x, y;\r
97                 double err, minerr = 0;\r
98                 int t1, t2, t3;\r
99                 int best_idx=0;\r
100 \r
101                 factor = 10.0 * Math.PI / 180.0;\r
102                 double rot0, rot1, rot2;\r
103                 double combo00, combo01, combo02, combo03, combo10, combo11, combo12, combo13, combo20, combo21, combo22, combo23;\r
104                 double combo02_2, combo02_5, combo02_8, combo02_11;\r
105                 double combo22_2, combo22_5, combo22_8, combo22_11;\r
106                 double combo12_2, combo12_5, combo12_8, combo12_11;\r
107                 // vertex展開\r
108                 final double VX00, VX01, VX02, VX10, VX11, VX12, VX20, VX21, VX22, VX30, VX31, VX32;\r
109                 VX00 = i_vertex3d[0].x;\r
110                 VX01 = i_vertex3d[0].y;\r
111                 VX02 = i_vertex3d[0].z;\r
112                 VX10 = i_vertex3d[1].x;\r
113                 VX11 = i_vertex3d[1].y;\r
114                 VX12 = i_vertex3d[1].z;\r
115                 VX20 = i_vertex3d[2].x;\r
116                 VX21 = i_vertex3d[2].y;\r
117                 VX22 = i_vertex3d[2].z;\r
118                 VX30 = i_vertex3d[3].x;\r
119                 VX31 = i_vertex3d[3].y;\r
120                 VX32 = i_vertex3d[3].z;\r
121                 final double P2D00, P2D01, P2D10, P2D11, P2D20, P2D21, P2D30, P2D31;\r
122                 P2D00 = i_vertex2d[0].x;\r
123                 P2D01 = i_vertex2d[0].y;\r
124                 P2D10 = i_vertex2d[1].x;\r
125                 P2D11 = i_vertex2d[1].y;\r
126                 P2D20 = i_vertex2d[2].x;\r
127                 P2D21 = i_vertex2d[2].y;\r
128                 P2D30 = i_vertex2d[3].x;\r
129                 P2D31 = i_vertex2d[3].y;\r
130                 final NyARPerspectiveProjectionMatrix prjmat = this._projection_mat_ref;\r
131                 final double CP0 = prjmat.m00,CP1 = prjmat.m01,CP2 = prjmat.m02,CP4 = prjmat.m10,CP5 = prjmat.m11,CP6 = prjmat.m12,CP8 = prjmat.m20,CP9 = prjmat.m21,CP10 = prjmat.m22;\r
132                 combo03 = CP0 * trans.x + CP1 * trans.y + CP2 * trans.z + prjmat.m03;\r
133                 combo13 = CP4 * trans.x + CP5 * trans.y + CP6 * trans.z + prjmat.m13;\r
134                 combo23 = CP8 * trans.x + CP9 * trans.y + CP10 * trans.z + prjmat.m23;\r
135                 double CACA, SASA, SACA, CA, SA;\r
136                 double CACACB, SACACB, SASACB, CASB, SASB;\r
137                 double SACASC, SACACBSC, SACACBCC, SACACC;\r
138                 final double[][] double1D = this.__modifyMatrix_double1D;\r
139 \r
140                 final NyARDoublePoint3d angle = this.__modifyMatrix_angle;\r
141                 final double[] a_factor = double1D[1];\r
142                 final double[] sinb = double1D[2];\r
143                 final double[] cosb = double1D[3];\r
144                 final double[] b_factor = double1D[4];\r
145                 final double[] sinc = double1D[5];\r
146                 final double[] cosc = double1D[6];\r
147                 final double[] c_factor = double1D[7];\r
148                 double w, w2;\r
149                 double wsin, wcos;\r
150                 \r
151                 io_rot.getAngle(angle);// arGetAngle( rot, &a, &b, &c );\r
152                 a2 = angle.x;\r
153                 b2 = angle.y;\r
154                 c2 = angle.z;\r
155 \r
156                 // comboの3行目を先に計算\r
157                 for (int i = 0; i < 10; i++) {\r
158                         minerr = 1000000000.0;\r
159                         // sin-cosテーブルを計算(これが外に出せるとは…。)\r
160                         for (int j = 0; j < 3; j++) {\r
161                                 w2 = factor * (j - 1);\r
162                                 w = a2 + w2;\r
163                                 a_factor[j] = w;\r
164                                 w = b2 + w2;\r
165                                 b_factor[j] = w;\r
166                                 sinb[j] = Math.sin(w);\r
167                                 cosb[j] = Math.cos(w);\r
168                                 w = c2 + w2;\r
169                                 c_factor[j] = w;\r
170                                 sinc[j] = Math.sin(w);\r
171                                 cosc[j] = Math.cos(w);\r
172                         }\r
173                         //\r
174                         for (t1 = 0; t1 < 3; t1++) {\r
175                                 SA = Math.sin(a_factor[t1]);\r
176                                 CA = Math.cos(a_factor[t1]);\r
177                                 // Optimize\r
178                                 CACA = CA * CA;\r
179                                 SASA = SA * SA;\r
180                                 SACA = SA * CA;\r
181                                 for (t2 = 0; t2 < 3; t2++) {\r
182                                         wsin = sinb[t2];\r
183                                         wcos = cosb[t2];\r
184                                         CACACB = CACA * wcos;\r
185                                         SACACB = SACA * wcos;\r
186                                         SASACB = SASA * wcos;\r
187                                         CASB = CA * wsin;\r
188                                         SASB = SA * wsin;\r
189                                         // comboの計算1\r
190                                         combo02 = CP0 * CASB + CP1 * SASB + CP2 * wcos;\r
191                                         combo12 = CP4 * CASB + CP5 * SASB + CP6 * wcos;\r
192                                         combo22 = CP8 * CASB + CP9 * SASB + CP10 * wcos;\r
193 \r
194                                         combo02_2 = combo02 * VX02 + combo03;\r
195                                         combo02_5 = combo02 * VX12 + combo03;\r
196                                         combo02_8 = combo02 * VX22 + combo03;\r
197                                         combo02_11 = combo02 * VX32 + combo03;\r
198                                         combo12_2 = combo12 * VX02 + combo13;\r
199                                         combo12_5 = combo12 * VX12 + combo13;\r
200                                         combo12_8 = combo12 * VX22 + combo13;\r
201                                         combo12_11 = combo12 * VX32 + combo13;\r
202                                         combo22_2 = combo22 * VX02 + combo23;\r
203                                         combo22_5 = combo22 * VX12 + combo23;\r
204                                         combo22_8 = combo22 * VX22 + combo23;\r
205                                         combo22_11 = combo22 * VX32 + combo23;\r
206                                         for (t3 = 0; t3 < 3; t3++) {\r
207                                                 wsin = sinc[t3];\r
208                                                 wcos = cosc[t3];\r
209                                                 SACASC = SACA * wsin;\r
210                                                 SACACC = SACA * wcos;\r
211                                                 SACACBSC = SACACB * wsin;\r
212                                                 SACACBCC = SACACB * wcos;\r
213 \r
214                                                 rot0 = CACACB * wcos + SASA * wcos + SACACBSC - SACASC;\r
215                                                 rot1 = SACACBCC - SACACC + SASACB * wsin + CACA * wsin;\r
216                                                 rot2 = -CASB * wcos - SASB * wsin;\r
217                                                 combo00 = CP0 * rot0 + CP1 * rot1 + CP2 * rot2;\r
218                                                 combo10 = CP4 * rot0 + CP5 * rot1 + CP6 * rot2;\r
219                                                 combo20 = CP8 * rot0 + CP9 * rot1 + CP10 * rot2;\r
220 \r
221                                                 rot0 = -CACACB * wsin - SASA * wsin + SACACBCC - SACACC;\r
222                                                 rot1 = -SACACBSC + SACASC + SASACB * wcos + CACA * wcos;\r
223                                                 rot2 = CASB * wsin - SASB * wcos;\r
224                                                 combo01 = CP0 * rot0 + CP1 * rot1 + CP2 * rot2;\r
225                                                 combo11 = CP4 * rot0 + CP5 * rot1 + CP6 * rot2;\r
226                                                 combo21 = CP8 * rot0 + CP9 * rot1 + CP10 * rot2;\r
227                                                 //\r
228                                                 err = 0.0;\r
229                                                 h = combo20 * VX00 + combo21 * VX01 + combo22_2;\r
230                                                 x = P2D00 - (combo00 * VX00 + combo01 * VX01 + combo02_2) / h;\r
231                                                 y = P2D01 - (combo10 * VX00 + combo11 * VX01 + combo12_2) / h;\r
232                                                 err += x * x + y * y;\r
233                                                 h = combo20 * VX10 + combo21 * VX11 + combo22_5;\r
234                                                 x = P2D10 - (combo00 * VX10 + combo01 * VX11 + combo02_5) / h;\r
235                                                 y = P2D11 - (combo10 * VX10 + combo11 * VX11 + combo12_5) / h;\r
236                                                 err += x * x + y * y;\r
237                                                 h = combo20 * VX20 + combo21 * VX21 + combo22_8;\r
238                                                 x = P2D20 - (combo00 * VX20 + combo01 * VX21 + combo02_8) / h;\r
239                                                 y = P2D21 - (combo10 * VX20 + combo11 * VX21 + combo12_8) / h;\r
240                                                 err += x * x + y * y;\r
241                                                 h = combo20 * VX30 + combo21 * VX31 + combo22_11;\r
242                                                 x = P2D30 - (combo00 * VX30 + combo01 * VX31 + combo02_11) / h;\r
243                                                 y = P2D31 - (combo10 * VX30 + combo11 * VX31 + combo12_11) / h;\r
244                                                 err += x * x + y * y;\r
245                                                 if (err < minerr) {\r
246                                                         minerr = err;\r
247                                                         a2 = a_factor[t1];\r
248                                                         b2 = b_factor[t2];\r
249                                                         c2 = c_factor[t3];\r
250                                                         best_idx=t1+t2*3+t3*9;\r
251                                                 }\r
252                                         }\r
253                                 }\r
254                         }\r
255                         if (best_idx==(1+3+9)) {\r
256                                 factor *= 0.5;\r
257                         }\r
258                 }\r
259                 io_rot.setAngle(a2, b2, c2);\r
260                 /* printf("factor = %10.5f\n", factor*180.0/MD_PI); */\r
261                 return minerr /4;\r
262         }       \r
263         \r
264         \r
265 }\r