OSDN Git Service

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