OSDN Git Service

Merge branch 'git-svn'
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.5.1 / sample / sandbox / jp / nyatla / nyartoolkit / sandbox / x2 / NyARFixedFloatRotTransOptimize_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.sandbox.x2;\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.types.NyARFixedFloat16Point2d;\r
38 import jp.nyatla.nyartoolkit.core.types.NyARFixedFloat16Point3d;\r
39 \r
40 /**\r
41  * 基本姿勢と実画像を一致するように、角度を微調整→平行移動量を再計算\r
42  * を繰り返して、変換行列を最適化する。\r
43  *\r
44  */\r
45 public class NyARFixedFloatRotTransOptimize_O2\r
46 {\r
47         private final static int AR_GET_TRANS_MAT_MAX_LOOP_COUNT = 1;// #define AR_GET_TRANS_MAT_MAX_LOOP_COUNT 5\r
48         private final NyARPerspectiveProjectionMatrix _projection_mat_ref;\r
49         public NyARFixedFloatRotTransOptimize_O2(NyARPerspectiveProjectionMatrix i_projection_mat_ref)\r
50         {\r
51                 this._projection_mat_ref=i_projection_mat_ref;\r
52                 return;\r
53         }\r
54         \r
55         final public double optimize(NyARFixedFloatRotMatrix io_rotmat,NyARFixedFloat16Point3d io_transvec,NyARFixedFloatFitVecCalculator i_calculator) throws NyARException\r
56         {\r
57                 final NyARFixedFloat16Point2d[] fit_vertex=i_calculator.getFitSquare();\r
58                 final NyARFixedFloat16Point3d[] offset_square=i_calculator.getOffsetVertex().vertex;\r
59                 \r
60                 double err = -1;\r
61                 err = modifyMatrix(io_rotmat,io_transvec,offset_square,fit_vertex);\r
62                 /*ループを抜けるタイミングをARToolKitと合わせるために変なことしてます。*/\r
63         for (int i = 0; ; i++)\r
64         {\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 < 1.0 || i == AR_GET_TRANS_MAT_MAX_LOOP_COUNT - 1)\r
71             {\r
72                 break;\r
73             }\r
74             i_calculator.calculateTransfer(io_rotmat, io_transvec);\r
75         }\r
76         return err;\r
77         }\r
78         \r
79         private final long[][] __modifyMatrix_double1D = new long[8][3];\r
80         private final long INITIAL_FACTOR=(long)(0x10000*5.0 * Math.PI / 180.0);\r
81         /**\r
82          * arGetRot計算を階層化したModifyMatrix 896\r
83          * \r
84          * @param nyrot\r
85          * @param trans\r
86          * @param i_vertex3d\r
87          * [m][3]\r
88          * @param i_vertex2d\r
89          * [n][2]\r
90          * @return\r
91          * @throws NyARException\r
92          */\r
93         private double modifyMatrix(NyARFixedFloatRotMatrix io_rot,NyARFixedFloat16Point3d trans, NyARFixedFloat16Point3d[] i_vertex3d, NyARFixedFloat16Point2d[] i_vertex2d) throws NyARException\r
94         {\r
95                 long a2, b2, c2;\r
96                 long h, x, y;\r
97                 long err, minerr = 0;\r
98                 int t1, t2, t3;\r
99                 int best_idx=0;\r
100 \r
101                 long factor = INITIAL_FACTOR;\r
102                 long rot0, rot1, rot2;\r
103                 long combo00, combo01, combo02, combo03, combo10, combo11, combo12, combo13, combo20, combo21, combo22, combo23;\r
104                 long combo02_2, combo02_5, combo02_8, combo02_11;\r
105                 long combo22_2, combo22_5, combo22_8, combo22_11;\r
106                 long combo12_2, combo12_5, combo12_8, combo12_11;\r
107                 // vertex展開\r
108                 final long 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 long 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 long CP0,CP1,CP2,CP3,CP4,CP5,CP6,CP7,CP8,CP9,CP10;\r
132                 CP0 = (long)(prjmat.m00*0x10000L);\r
133                 CP1 = (long)(prjmat.m01*0x10000L);\r
134                 CP2 = (long)(prjmat.m02*0x10000L);\r
135                 CP4 = (long)(prjmat.m10*0x10000L);\r
136                 CP5 = (long)(prjmat.m11*0x10000L);\r
137                 CP6 = (long)(prjmat.m12*0x10000L);\r
138                 CP8 = (long)(prjmat.m20*0x10000L);\r
139                 CP9 = (long)(prjmat.m21*0x10000L);\r
140                 CP10 =(long)(prjmat.m22*0x10000L);\r
141 \r
142                 combo03 = ((CP0 * trans.x + CP1 * trans.y + CP2 * trans.z)>>16) + (long)(prjmat.m03*0x10000L);\r
143                 combo13 = ((CP4 * trans.x + CP5 * trans.y + CP6 * trans.z)>>16) + (long)(prjmat.m13*0x10000L);\r
144                 combo23 = ((CP8 * trans.x + CP9 * trans.y + CP10 * trans.z)>>16) + (long)(prjmat.m23*0x10000L);\r
145                 long CACA, SASA, SACA, CA, SA;\r
146                 long CACACB, SACACB, SASACB, CASB, SASB;\r
147                 long SACASC, SACACBSC, SACACBCC, SACACC;\r
148                 final long[][] double1D = this.__modifyMatrix_double1D;\r
149 \r
150                 final long[] a_factor = double1D[1];\r
151                 final long[] sinb = double1D[2];\r
152                 final long[] cosb = double1D[3];\r
153                 final long[] b_factor = double1D[4];\r
154                 final long[] sinc = double1D[5];\r
155                 final long[] cosc = double1D[6];\r
156                 final long[] c_factor = double1D[7];\r
157                 long w, w2;\r
158                 long wsin, wcos;\r
159                 \r
160                 final NyARFixedFloat16Point3d angle=io_rot.refAngle();// arGetAngle( rot, &a, &b, &c );\r
161                 a2 =angle.x;\r
162                 b2 =angle.y;\r
163                 c2 =angle.z;\r
164 \r
165                 // comboの3行目を先に計算\r
166                 for (int i = 0; i < 10; i++) {\r
167                         minerr = 0x4000000000000000L;\r
168                         // sin-cosテーブルを計算(これが外に出せるとは…。)\r
169                         for (int j = 0; j < 3; j++) {\r
170                                 w2 = factor * (j - 1);//S16\r
171                                 w = a2 + w2;//S16\r
172                                 a_factor[j] = w;//S16\r
173                                 w = b2 + w2;//S16\r
174                                 b_factor[j] = w;//S16\r
175                                 sinb[j] = NyMath.sinFixedFloat24((int)w);\r
176                                 cosb[j] = NyMath.cosFixedFloat24((int)w);\r
177                                 w = c2 + w2;//S16\r
178                                 c_factor[j] = w;//S16\r
179                                 sinc[j] = NyMath.sinFixedFloat24((int)w);\r
180                                 cosc[j] = NyMath.cosFixedFloat24((int)w);\r
181                         }\r
182                         //\r
183                         for (t1 = 0; t1 < 3; t1++) {\r
184                                 SA = NyMath.sinFixedFloat24((int)a_factor[t1]);\r
185                                 CA = NyMath.cosFixedFloat24((int)a_factor[t1]);\r
186                                 // Optimize\r
187                                 CACA = (CA * CA)>>24;//S24\r
188                                 SASA = (SA * SA)>>24;//S24\r
189                                 SACA = (SA * CA)>>24;//S24\r
190                                 for (t2 = 0; t2 < 3; t2++) {\r
191                                         wsin = sinb[t2];//S24\r
192                                         wcos = cosb[t2];//S24\r
193                                         CACACB = (CACA * wcos)>>24;//S24\r
194                                         SACACB = (SACA * wcos)>>24;//S24\r
195                                         SASACB = (SASA * wcos)>>24;//S24\r
196                                         CASB = (CA * wsin)>>24;//S24\r
197                                         SASB = (SA * wsin)>>24;//S24\r
198 \r
199                                         // comboの計算1\r
200                                         combo02 = (CP0 * CASB + CP1 * SASB + CP2 * wcos)>>24;//S24*S16>>24=S16\r
201                                         combo12 = (CP4 * CASB + CP5 * SASB + CP6 * wcos)>>24;//S24*S16>>24=S16\r
202                                         combo22 = (CP8 * CASB + CP9 * SASB + CP10 * wcos)>>24;//S24*S16>>24=S16\r
203 \r
204                                         combo02_2 = ((combo02 * VX02)>>16) + combo03;//S16\r
205                                         combo02_5 = ((combo02 * VX12)>>16) + combo03;//S16\r
206                                         combo02_8 = ((combo02 * VX22)>>16) + combo03;//S16\r
207                                         combo02_11 = ((combo02 * VX32)>>16) + combo03;//S16\r
208                                         combo12_2 = ((combo12 * VX02)>>16) + combo13;//S16\r
209                                         combo12_5 = ((combo12 * VX12)>>16) + combo13;//S16\r
210                                         combo12_8 = ((combo12 * VX22)>>16) + combo13;//S16\r
211                                         combo12_11 = ((combo12 * VX32)>>16) + combo13;//S16\r
212                                         combo22_2 = ((combo22 * VX02)>>16) + combo23;//S16\r
213                                         combo22_5 = ((combo22 * VX12)>>16) + combo23;//S16\r
214                                         combo22_8 = ((combo22 * VX22)>>16) + combo23;//S16\r
215                                         combo22_11 = ((combo22 * VX32)>>16) + combo23;//S16\r
216                                         for (t3 = 0; t3 < 3; t3++) {\r
217                                                 wsin = sinc[t3];//S24\r
218                                                 wcos = cosc[t3];//S24\r
219                                                 SACASC = (SACA * wsin)>>24;//S24\r
220                                                 SACACC = (SACA * wcos)>>24;//S24\r
221                                                 SACACBSC =(SACACB * wsin)>>24;//S24;\r
222                                                 SACACBCC = (SACACB * wcos)>>24;//S24;\r
223 \r
224                                                 rot0 = ((CACACB * wcos + SASA * wcos)>>24) + SACACBSC - SACASC;//S24;\r
225                                                 rot1 = SACACBCC - SACACC + ((SASACB * wsin + CACA * wsin)>>24);//S24;\r
226                                                 rot2 = (-CASB * wcos - SASB * wsin)>>24;//S24;\r
227                                                 combo00 = (CP0 * rot0 + CP1 * rot1 + CP2 * rot2)>>24;//S16\r
228                                                 combo10 = (CP4 * rot0 + CP5 * rot1 + CP6 * rot2)>>24;//S16\r
229                                                 combo20 = (CP8 * rot0 + CP9 * rot1 + CP10 * rot2)>>24;//S16\r
230 \r
231                                                 rot0 = ((-CACACB * wsin - SASA * wsin)>>24) + SACACBCC - SACACC;//S24\r
232                                                 rot1 = -SACACBSC + SACASC + ((SASACB * wcos + CACA * wcos)>>24);//S24\r
233                                                 rot2 = (CASB * wsin - SASB * wcos)>>24;//S24\r
234 \r
235                                                 combo01 =(CP0 * rot0 + CP1 * rot1 + CP2 * rot2)>>24;//S16\r
236                                                 combo11 =(CP4 * rot0 + CP5 * rot1 + CP6 * rot2)>>24;//S16\r
237                                                 combo21 =(CP8 * rot0 + CP9 * rot1 + CP10 * rot2)>>24;//S16\r
238                                                 //\r
239                                                 err =0;\r
240                                                 h = ((combo20 * VX00 + combo21 * VX01)>>16) + combo22_2;//S16\r
241                                                 x = P2D00 - ((((combo00 * VX00 + combo01 * VX01)>>16) + combo02_2)<<16) / h;//S16\r
242                                                 y = P2D01 - ((((combo10 * VX00 + combo11 * VX01)>>16) + combo12_2)<<16) / h;//S16\r
243                                                 err += ((x * x + y * y)>>16);\r
244                                                 h = ((combo20 * VX10 + combo21 * VX11)>>16) + combo22_5;\r
245                                                 x = P2D10 - ((((combo00 * VX10 + combo01 * VX11)>>16) + combo02_5)<<16) / h;//S16\r
246                                                 y = P2D11 - ((((combo10 * VX10 + combo11 * VX11)>>16) + combo12_5)<<16) / h;//S16\r
247                                                 err += ((x * x + y * y)>>16);\r
248                                                 h = ((combo20 * VX20 + combo21 * VX21)>>16) + combo22_8;\r
249                                                 x = P2D20 - ((((combo00 * VX20 + combo01 * VX21)>>16) + combo02_8)<<16) / h;//S16\r
250                                                 y = P2D21 - ((((combo10 * VX20 + combo11 * VX21)>>16) + combo12_8)<<16) / h;//S16\r
251                                                 err += ((x * x + y * y)>>16);\r
252                                                 h = ((combo20 * VX30 + combo21 * VX31)>>16) + combo22_11;\r
253                                                 x = P2D30 - ((((combo00 * VX30 + combo01 * VX31)>>16) + combo02_11)<<16) / h;//S16\r
254                                                 y = P2D31 - ((((combo10 * VX30 + combo11 * VX31)>>16) + combo12_11)<<16) / h;//S16\r
255                                                 err += ((x * x + y * y)>>16);\r
256                                                 if (err < minerr) {\r
257                                                         minerr = err;\r
258                                                         a2 = a_factor[t1];\r
259                                                         b2 = b_factor[t2];\r
260                                                         c2 = c_factor[t3];\r
261                                                         best_idx=t1+t2*3+t3*9;\r
262                                                 }\r
263                                         }\r
264                                 }\r
265                         }\r
266                         if (best_idx==(1+3+9)) {\r
267                                 factor=factor>>1;\r
268                         }\r
269                 }\r
270                 io_rot.setAngle((int)a2,(int)b2,(int)c2);\r
271                 /* printf("factor = %10.5f\n", factor*180.0/MD_PI); */\r
272                 return minerr /4;//この設定値おかしくね?16bitfixedfloatなら16で割らないと。\r
273         }       \r
274         \r
275         \r
276 }\r