OSDN Git Service

7b44e1b890aaf521c6a174be4efd23698251aa8d
[nyartoolkit-and/nyartoolkit-and.git] / lib / src / jp / nyatla / nyartoolkit / core / param / NyARParam.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 edition ARToolKit class library.\r
11  * Copyright (C)2008-2009 Ryo Iizuka\r
12  *\r
13  * This program is free software: you can redistribute it and/or modify\r
14  * it under the terms of the GNU General Public License as published by\r
15  * the Free Software Foundation, either version 3 of the License, or\r
16  * (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 program.  If not, see <http://www.gnu.org/licenses/>.\r
25  * \r
26  * For further information please contact.\r
27  *      http://nyatla.jp/nyatoolkit/\r
28  *      <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
29  * \r
30  */\r
31 package jp.nyatla.nyartoolkit.core.param;\r
32 \r
33 import java.io.*;\r
34 import java.nio.*;\r
35 \r
36 import jp.nyatla.nyartoolkit.NyARException;\r
37 import jp.nyatla.nyartoolkit.core.types.*;\r
38 import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix44;\r
39 \r
40 /**\r
41  * typedef struct { int xsize, ysize; double mat[3][4]; double dist_factor[4]; } ARParam;\r
42  * NyARの動作パラメータを格納するクラス\r
43  *\r
44  */\r
45 public class NyARParam\r
46 {\r
47         protected NyARIntSize _screen_size=new NyARIntSize();\r
48         private static final int SIZE_OF_PARAM_SET = 4 + 4 + (3 * 4 * 8) + (4 * 8);\r
49         private NyARCameraDistortionFactor _dist=new NyARCameraDistortionFactor();\r
50         private NyARPerspectiveProjectionMatrix _projection_matrix=new NyARPerspectiveProjectionMatrix();\r
51 \r
52         public NyARIntSize getScreenSize()\r
53         {\r
54                 return this._screen_size;\r
55         }\r
56 \r
57         /**\r
58          * ARToolKit形式の透視変換行列を返します。\r
59          * @return\r
60          */\r
61         public NyARPerspectiveProjectionMatrix getPerspectiveProjectionMatrix()\r
62         {\r
63                 return this._projection_matrix;\r
64         }\r
65         /**\r
66          * ARToolKit形式の歪み補正パラメータを返します。\r
67          * @return\r
68          */\r
69         public NyARCameraDistortionFactor getDistortionFactor()\r
70         {\r
71                 return this._dist;\r
72         }\r
73         /**\r
74          * \r
75          * @param i_factor\r
76          * NyARCameraDistortionFactorにセットする配列を指定する。要素数は4であること。\r
77          * @param i_projection\r
78          * NyARPerspectiveProjectionMatrixセットする配列を指定する。要素数は12であること。\r
79          */\r
80         public void setValue(double[] i_factor,double[] i_projection)\r
81         {\r
82                 this._dist.setValue(i_factor);\r
83                 this._projection_matrix.setValue(i_projection);\r
84                 return;\r
85         }\r
86 \r
87         /**\r
88          * ARToolKit標準ファイルから1個目の設定をロードする。\r
89          * \r
90          * @param i_filename\r
91          * @throws NyARException\r
92          */\r
93         public void loadARParamFromFile(String i_filename) throws NyARException\r
94         {\r
95                 try {\r
96                         loadARParam(new FileInputStream(i_filename));\r
97                 } catch (Exception e) {\r
98                         throw new NyARException(e);\r
99                 }\r
100         }\r
101 \r
102         /**\r
103          * int arParamChangeSize( ARParam *source, int xsize, int ysize, ARParam *newparam );\r
104          * 関数の代替関数 サイズプロパティをi_xsize,i_ysizeに変更します。\r
105          * @param i_xsize\r
106          * @param i_ysize\r
107          * @param newparam\r
108          * @return\r
109          * \r
110          */\r
111         public void changeScreenSize(int i_xsize, int i_ysize)\r
112         {\r
113                 final double scale = (double) i_xsize / (double) (this._screen_size.w);// scale = (double)xsize / (double)(source->xsize);\r
114                 //スケールを変更\r
115                 this._dist.changeScale(scale);\r
116                 this._projection_matrix.changeScale(scale);\r
117                 this._screen_size.w = i_xsize;// newparam->xsize = xsize;\r
118                 this._screen_size.h = i_ysize;// newparam->ysize = ysize;\r
119                 return;\r
120         }\r
121         /**\r
122          * 右手系の視錐台を作ります。\r
123          * 計算結果を多用するときは、キャッシュするようにして下さい。\r
124          * @param i_dist_min\r
125          * @param i_dist_max\r
126          * @param o_frustum\r
127          */\r
128         public void makeCameraFrustumRH(double i_dist_min,double i_dist_max,NyARDoubleMatrix44 o_frustum)\r
129         {\r
130                 this._projection_matrix.makeCameraFrustumRH(this._screen_size.w, this._screen_size.h, i_dist_min, i_dist_max, o_frustum);\r
131                 return;\r
132         }       \r
133 \r
134 \r
135         /**\r
136          * int arParamLoad( const char *filename, int num, ARParam *param, ...);\r
137          * i_streamの入力ストリームからi_num個の設定を読み込み、パラメタを配列にして返します。\r
138          * \r
139          * @param i_stream\r
140          * @throws Exception\r
141          */\r
142         public void loadARParam(InputStream i_stream)throws NyARException\r
143         {\r
144                 try {\r
145                         byte[] buf = new byte[SIZE_OF_PARAM_SET];\r
146                         i_stream.read(buf);\r
147                         double[] tmp=new double[16];\r
148 \r
149                         // バッファを加工\r
150                         ByteBuffer bb = ByteBuffer.wrap(buf);\r
151                         bb.order(ByteOrder.BIG_ENDIAN);\r
152                         this._screen_size.w = bb.getInt();\r
153                         this._screen_size.h = bb.getInt();\r
154                         //double値を12個読み込む\r
155                         for(int i=0;i<12;i++){\r
156                                 tmp[i]=bb.getDouble();\r
157                         }\r
158                         //パディング\r
159                         tmp[12]=tmp[13]=tmp[14]=0;\r
160                         tmp[15]=1;\r
161                         //Projectionオブジェクトにセット\r
162                         this._projection_matrix.setValue(tmp);\r
163                         //double値を4個読み込む\r
164                         for (int i = 0; i < 4; i++) {\r
165                                 tmp[i]=bb.getDouble();\r
166                         }\r
167                         //Factorオブジェクトにセット\r
168                         this._dist.setValue(tmp);\r
169                 } catch (Exception e) {\r
170                         throw new NyARException(e);\r
171                 }\r
172                 return;\r
173         }\r
174 \r
175         public void saveARParam(OutputStream i_stream)throws Exception\r
176         {\r
177                 NyARException.trap("未チェックの関数");\r
178                 byte[] buf = new byte[SIZE_OF_PARAM_SET];\r
179                 // バッファをラップ\r
180                 ByteBuffer bb = ByteBuffer.wrap(buf);\r
181                 bb.order(ByteOrder.BIG_ENDIAN);\r
182 \r
183                 // 書き込み\r
184                 bb.putInt(this._screen_size.w);\r
185                 bb.putInt(this._screen_size.h);\r
186                 double[] tmp=new double[12];\r
187                 //Projectionを読み出し\r
188                 this._projection_matrix.getValue(tmp);\r
189                 //double値を12個書き込む\r
190                 for(int i=0;i<12;i++){\r
191                         tmp[i]=bb.getDouble();\r
192                 }\r
193                 //Factorを読み出し\r
194                 this._dist.getValue(tmp);\r
195                 //double値を4個書き込む\r
196                 for (int i = 0; i < 4; i++) {\r
197                         tmp[i]=bb.getDouble();\r
198                 }\r
199                 i_stream.write(buf);\r
200                 return;\r
201         }\r
202 }\r