OSDN Git Service

[バックアップ]NyARToolkit
[nyartoolkit-and/nyartoolkit-and.git] / sample / sandbox / jp / nyatla / nyartoolkit / sandbox / x2 / NyARTransMat_X2.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 import jp.nyatla.nyartoolkit.NyARException;\r
35 import jp.nyatla.nyartoolkit.core.NyARSquare;\r
36 import jp.nyatla.nyartoolkit.core.param.*;\r
37 import jp.nyatla.nyartoolkit.core.transmat.*;\r
38 import jp.nyatla.nyartoolkit.core.transmat.rotmatrix.NyARRotMatrix;\r
39 import jp.nyatla.nyartoolkit.core.types.*;\r
40 import jp.nyatla.nyartoolkit.core2.types.NyARFixedFloat16Point2d;\r
41 import jp.nyatla.nyartoolkit.core2.types.NyARFixedFloat16Point3d;\r
42 \r
43 \r
44 /**\r
45  * This class calculates ARMatrix from square information and holds it. --\r
46  * 変換行列を計算して、結果を保持するクラス。\r
47  * \r
48  */\r
49 public class NyARTransMat_X2 implements INyARTransMat\r
50 {\r
51         private final NyARFixedFloat16Point2d _center=new NyARFixedFloat16Point2d();\r
52         private final NyARFixedFloatTransOffset _offset=new NyARFixedFloatTransOffset();\r
53         private NyARFixedFloatRotMatrix _rotmatrix;\r
54         private NyARFixedFloatFitVecCalculator _calculator;\r
55         private NyARFixedFloatRotTransOptimize_O2 _mat_optimize;\r
56 \r
57         public NyARTransMat_X2(NyARParam i_param,NyARFixedFloatCameraDistortionFactorMap i_dist_factor_map) throws NyARException\r
58         {\r
59                 final NyARPerspectiveProjectionMatrix pmat=i_param.getPerspectiveProjectionMatrix();\r
60                 this._calculator=new NyARFixedFloatFitVecCalculator(pmat,i_dist_factor_map);\r
61                 this._rotmatrix = new NyARFixedFloatRotMatrix(pmat);\r
62                 this._mat_optimize=new NyARFixedFloatRotTransOptimize_O2(pmat);\r
63         }\r
64 \r
65         public void setCenter(double i_x, double i_y)\r
66         {\r
67                 this._center.x= (long)i_x*NyMath.FIXEDFLOAT16_1;\r
68                 this._center.y= (long)i_y*NyMath.FIXEDFLOAT16_1;\r
69                 return;\r
70         }\r
71         /**\r
72          * 頂点順序をi_directionに対応して並べ替えます。\r
73          * @param i_square\r
74          * @param i_direction\r
75          * @param o_sqvertex_ref\r
76          * @param o_liner_ref\r
77          */\r
78         private final void initVertexOrder(NyARSquare i_square, int i_direction, NyARDoublePoint2d[] o_sqvertex_ref, NyARLinear[] o_liner_ref)\r
79         {\r
80                 //頂点順序を考慮した矩形の頂点情報\r
81                 o_sqvertex_ref[0]= i_square.sqvertex[(4 - i_direction) % 4];\r
82                 o_sqvertex_ref[1]= i_square.sqvertex[(5 - i_direction) % 4];\r
83                 o_sqvertex_ref[2]= i_square.sqvertex[(6 - i_direction) % 4];\r
84                 o_sqvertex_ref[3]= i_square.sqvertex[(7 - i_direction) % 4];    \r
85                 o_liner_ref[0]=i_square.line[(4 - i_direction) % 4];\r
86                 o_liner_ref[1]=i_square.line[(5 - i_direction) % 4];\r
87                 o_liner_ref[2]=i_square.line[(6 - i_direction) % 4];\r
88                 o_liner_ref[3]=i_square.line[(7 - i_direction) % 4];\r
89                 return;\r
90         }\r
91 \r
92 \r
93         private final NyARDoublePoint2d[] __transMat_sqvertex_ref = new NyARDoublePoint2d[4];\r
94         private final NyARLinear[] __transMat_linear_ref=new NyARLinear[4];\r
95         private final NyARFixedFloat16Point3d __transMat_trans=new NyARFixedFloat16Point3d();\r
96         /**\r
97          * double arGetTransMat( ARMarkerInfo *marker_info,double center[2], double width, double conv[3][4] )\r
98          * \r
99          * @param i_square\r
100          * 計算対象のNyARSquareオブジェクト\r
101          * @param i_direction\r
102          * @param i_width\r
103          * @return\r
104          * @throws NyARException\r
105          */\r
106         public void transMat(final NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult o_result_conv) throws NyARException\r
107         {\r
108                 final NyARDoublePoint2d[] sqvertex_ref = __transMat_sqvertex_ref;\r
109                 final NyARLinear[] linear_ref=__transMat_linear_ref;\r
110                 final NyARFixedFloat16Point3d trans=this.__transMat_trans;\r
111                 \r
112                 //計算用に頂点情報を初期化(順番調整)\r
113                 initVertexOrder(i_square, i_direction, sqvertex_ref,linear_ref);\r
114                 \r
115                 //基準矩形を設定\r
116                 this._offset.setSquare((long)(i_width*NyMath.FIXEDFLOAT16_1),this._center);\r
117 \r
118                 // rotationを矩形情報から計算\r
119                 this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref);\r
120 \r
121                 //平行移動量計算機にオフセット頂点をセット\r
122                 this._calculator.setOffsetSquare(this._offset);\r
123                 \r
124                 //平行移動量計算機に適応先矩形の情報をセット\r
125                 this._calculator.setFittedSquare(sqvertex_ref); \r
126 \r
127                 //回転行列の平行移動量の計算\r
128                 this._calculator.calculateTransfer(this._rotmatrix,trans);\r
129                 \r
130                 //計算結果の最適化(this._rotmatrix,trans)\r
131                 this._mat_optimize.optimize(this._rotmatrix,trans,this._calculator);\r
132                 \r
133                 // マトリクスの保存\r
134                 this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv);\r
135                 return;\r
136         }\r
137         /**\r
138          * double arGetTransMatCont( ARMarkerInfo *marker_info, double prev_conv[3][4],double center[2], double width, double conv[3][4] )\r
139          * \r
140          * @param i_square\r
141          * @param i_direction\r
142          * マーカーの方位を指定する。\r
143          * @param i_width\r
144          * @param io_result_conv\r
145          * 計算履歴を持つNyARTransMatResultオブジェクトを指定する。 履歴を持たない場合は、transMatと同じ処理を行う。\r
146          * @return\r
147          * @throws NyARException\r
148          */\r
149         public void transMatContinue(NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult io_result_conv) throws NyARException\r
150         {\r
151                 /*      今度実装\r
152                 final NyARDoublePoint2d[] sqvertex_ref = __transMat_sqvertex_ref;\r
153                 final NyARLinear[] linear_ref=__transMat_linear_ref;\r
154                 final NyARDoublePoint3d trans=this.__transMat_trans;\r
155 \r
156                 // io_result_convが初期値なら、transMatで計算する。\r
157                 if (!io_result_conv.hasValue()) {\r
158                         this.transMat(i_square, i_direction, i_width, io_result_conv);\r
159                         return;\r
160                 }\r
161                 \r
162                 //基準矩形を設定\r
163                 this._offset.setSquare(i_width,this._center);\r
164 \r
165                 // rotationを矩形情報を一つ前の変換行列で初期化\r
166                 this._rotmatrix.initRotByPrevResult(io_result_conv);\r
167 \r
168                 //平行移動量計算機に、オフセット頂点をセット\r
169                 this._calculator.setOffsetSquare(this._offset);\r
170                 \r
171                 //平行移動量計算機に、適応先矩形の情報をセット\r
172                 this._calculator.setFittedSquare(sqvertex_ref); \r
173                                 \r
174                 //回転行列の平行移動量の計算\r
175                 this._calculator.calculateTransfer(this._rotmatrix,trans);\r
176                 \r
177                 //計算結果の最適化(this._rotmatrix,trans)\r
178                 final double err=this._mat_optimize.optimize(this._rotmatrix,trans,this._calculator);\r
179                 \r
180                 //計算結果を保存\r
181                 io_result_conv.updateMatrixValue(this._rotmatrix, this._offset.point, trans);\r
182 \r
183                 // エラー値が許容範囲でなければTransMatをやり直し\r
184                 if (err > AR_GET_TRANS_CONT_MAT_MAX_FIT_ERROR) {\r
185                         // rotationを矩形情報で初期化\r
186                         this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref);\r
187                         //回転行列の平行移動量の計算\r
188                         this._calculator.calculateTransfer(this._rotmatrix,trans);\r
189                         //計算結果の最適化(this._rotmatrix,trans)\r
190                         final double err2=this._mat_optimize.optimize(this._rotmatrix,trans,this._calculator);\r
191                         //エラー値が低かったら値を差換え\r
192                         if (err2 < err) {\r
193                                 // 良い値が取れたら、差換え\r
194                                 io_result_conv.updateMatrixValue(this._rotmatrix, this._offset.point, trans);\r
195                         }\r
196                 }               \r
197                 return;*/\r
198                 NyARException.notImplement();\r
199         }\r
200         public void updateMatrixValue(NyARFixedFloatRotMatrix i_rot, NyARFixedFloat16Point3d i_off, NyARFixedFloat16Point3d i_trans,NyARTransMatResult o_result)\r
201         {\r
202                 o_result.m00=(double)i_rot.m00/NyMath.FIXEDFLOAT24_1;\r
203                 o_result.m01=(double)i_rot.m01/NyMath.FIXEDFLOAT24_1;\r
204                 o_result.m02=(double)i_rot.m02/NyMath.FIXEDFLOAT24_1;\r
205                 o_result.m03=(double)(((i_rot.m00 * i_off.x + i_rot.m01 * i_off.y + i_rot.m02 * i_off.z)>>24) + i_trans.x)/NyMath.FIXEDFLOAT16_1;\r
206 \r
207                 o_result.m10 =(double)i_rot.m10/NyMath.FIXEDFLOAT24_1;\r
208                 o_result.m11 =(double)i_rot.m11/NyMath.FIXEDFLOAT24_1;\r
209                 o_result.m12 =(double)i_rot.m12/NyMath.FIXEDFLOAT24_1;\r
210                 o_result.m13 =(double)(((i_rot.m10 * i_off.x + i_rot.m11 * i_off.y + i_rot.m12 * i_off.z)>>24) + i_trans.y)/NyMath.FIXEDFLOAT16_1;\r
211 \r
212                 o_result.m20 =(double)i_rot.m20/NyMath.FIXEDFLOAT24_1;\r
213                 o_result.m21 =(double)i_rot.m21/NyMath.FIXEDFLOAT24_1;\r
214                 o_result.m22 =(double)i_rot.m22/NyMath.FIXEDFLOAT24_1;\r
215                 o_result.m23 =(double)(((i_rot.m20 * i_off.x + i_rot.m21 * i_off.y + i_rot.m22 * i_off.z)>>24) + i_trans.z)/NyMath.FIXEDFLOAT16_1;\r
216 \r
217                 o_result.has_value = true;\r
218                 return;\r
219         }       \r
220 }\r