OSDN Git Service

git-svn-id: http://svn.sourceforge.jp/svnroot/nyartoolkit/NyARToolkit/trunk@802 7cac0...
[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  * このクラスは、NyARToolkitの環境パラメータを格納します。\r
42  * 環境パラメータは、ARToolKitのパラメータと同一です。\r
43  * パラメータの要素には、以下のものがあります。\r
44  * <ul>\r
45  * <li>樽型歪みパラメータ - 入力画像の樽型歪みパラメータです。\r
46  * <li>スクリーンサイズ - 入力画像の解像度です。\r
47  * <li>透視変換パラメータ - 4x4行列です。\r
48  * </ul>\r
49  */\r
50 public class NyARParam\r
51 {\r
52         /** スクリーンサイズです。*/\r
53         protected NyARIntSize _screen_size=new NyARIntSize();\r
54         private static final int SIZE_OF_PARAM_SET = 4 + 4 + (3 * 4 * 8) + (4 * 8);\r
55         private NyARCameraDistortionFactor _dist=new NyARCameraDistortionFactor();\r
56         private NyARPerspectiveProjectionMatrix _projection_matrix=new NyARPerspectiveProjectionMatrix();\r
57 \r
58         public NyARIntSize getScreenSize()\r
59         {\r
60                 return this._screen_size;\r
61         }\r
62 \r
63         /**\r
64          * この関数は、ARToolKit形式の透視変換行列を返します。\r
65          * @return\r
66          * [read only]透視変換行列を返します。\r
67          */\r
68         public NyARPerspectiveProjectionMatrix getPerspectiveProjectionMatrix()\r
69         {\r
70                 return this._projection_matrix;\r
71         }\r
72         /**\r
73          * この関数は、ARToolKit形式の歪み補正パラメータを返します。\r
74          * @return\r
75          * [read only]歪み補正パラメータオブジェクト\r
76          */\r
77         public NyARCameraDistortionFactor getDistortionFactor()\r
78         {\r
79                 return this._dist;\r
80         }\r
81         /**\r
82          * この関数は、配列から値を設定します。\r
83          * @param i_factor\r
84          * NyARCameraDistortionFactorにセットする配列を指定する。要素数は4であること。\r
85          * @param i_projection\r
86          * NyARPerspectiveProjectionMatrixセットする配列を指定する。要素数は12であること。\r
87          */\r
88         public void setValue(double[] i_factor,double[] i_projection)\r
89         {\r
90                 this._dist.setValue(i_factor);\r
91                 this._projection_matrix.setValue(i_projection);\r
92                 return;\r
93         }\r
94         /**\r
95          * この関数は、現在のスクリーンサイズを変更します。\r
96          * ARToolKitのarParamChangeSize関数に相当します。\r
97          * @param i_xsize\r
98          * 新しいサイズ\r
99          * @param i_ysize\r
100          * 新しいサイズ\r
101          */\r
102         public void changeScreenSize(int i_xsize, int i_ysize)\r
103         {\r
104                 final double scale = (double) i_xsize / (double) (this._screen_size.w);// scale = (double)xsize / (double)(source->xsize);\r
105                 //スケールを変更\r
106                 this._dist.changeScale(scale);\r
107                 this._projection_matrix.changeScale(scale);\r
108                 this._screen_size.w = i_xsize;// newparam->xsize = xsize;\r
109                 this._screen_size.h = i_ysize;// newparam->ysize = ysize;\r
110                 return;\r
111         }\r
112         /**\r
113          * この関数は、カメラパラメータから右手系の視錐台を作ります。\r
114          * <p>注意 -\r
115          * この処理は低速です。繰り返しの使用はできるだけ避けてください。\r
116          * </p>\r
117          * @param i_dist_min\r
118          * 視錐台のnear point(mm指定)\r
119          * @param i_dist_max\r
120          * 視錐台のfar point(mm指定)\r
121          * @param o_frustum\r
122          * 視錐台を受け取る配列。\r
123          * @see NyARPerspectiveProjectionMatrix#makeCameraFrustumRH\r
124          */\r
125         public void makeCameraFrustumRH(double i_dist_min,double i_dist_max,NyARDoubleMatrix44 o_frustum)\r
126         {\r
127                 this._projection_matrix.makeCameraFrustumRH(this._screen_size.w, this._screen_size.h, i_dist_min, i_dist_max, o_frustum);\r
128                 return;\r
129         }       \r
130 \r
131         /**\r
132          * この関数は、ARToolKit形式のカメラパラメータファイルから、1個目の設定をロードします。\r
133          * @param i_filename\r
134          * パラメータファイルのファイルパス\r
135          * @throws NyARException\r
136          */\r
137         public void loadARParamFromFile(String i_filename) throws NyARException\r
138         {\r
139                 try {\r
140                         loadARParam(new FileInputStream(i_filename));\r
141                 } catch (Exception e) {\r
142                         throw new NyARException(e);\r
143                 }\r
144         }\r
145 \r
146         \r
147 \r
148 \r
149         /**\r
150          * この関数は、ストリームからARToolKit形式のカメラパラメーを1個目の設定をロードします。\r
151          * @param i_stream\r
152          * 読み込むストリームです。\r
153          * @throws Exception\r
154          */\r
155         public void loadARParam(InputStream i_stream)throws NyARException\r
156         {\r
157                 try {\r
158                         byte[] buf = new byte[SIZE_OF_PARAM_SET];\r
159                         i_stream.read(buf);\r
160                         double[] tmp=new double[16];\r
161 \r
162                         // バッファを加工\r
163                         ByteBuffer bb = ByteBuffer.wrap(buf);\r
164                         bb.order(ByteOrder.BIG_ENDIAN);\r
165                         this._screen_size.w = bb.getInt();\r
166                         this._screen_size.h = bb.getInt();\r
167                         //double値を12個読み込む\r
168                         for(int i=0;i<12;i++){\r
169                                 tmp[i]=bb.getDouble();\r
170                         }\r
171                         //パディング\r
172                         tmp[12]=tmp[13]=tmp[14]=0;\r
173                         tmp[15]=1;\r
174                         //Projectionオブジェクトにセット\r
175                         this._projection_matrix.setValue(tmp);\r
176                         //double値を4個読み込む\r
177                         for (int i = 0; i < 4; i++) {\r
178                                 tmp[i]=bb.getDouble();\r
179                         }\r
180                         //Factorオブジェクトにセット\r
181                         this._dist.setValue(tmp);\r
182                 } catch (Exception e) {\r
183                         throw new NyARException(e);\r
184                 }\r
185                 return;\r
186         }\r
187         /**\r
188          * この関数は機能しません。\r
189          * @param i_stream\r
190          * 未定義\r
191          * @throws Exception\r
192          */\r
193         public void saveARParam(OutputStream i_stream)throws Exception\r
194         {\r
195                 NyARException.trap("未チェックの関数");\r
196                 byte[] buf = new byte[SIZE_OF_PARAM_SET];\r
197                 // バッファをラップ\r
198                 ByteBuffer bb = ByteBuffer.wrap(buf);\r
199                 bb.order(ByteOrder.BIG_ENDIAN);\r
200 \r
201                 // 書き込み\r
202                 bb.putInt(this._screen_size.w);\r
203                 bb.putInt(this._screen_size.h);\r
204                 double[] tmp=new double[12];\r
205                 //Projectionを読み出し\r
206                 this._projection_matrix.getValue(tmp);\r
207                 //double値を12個書き込む\r
208                 for(int i=0;i<12;i++){\r
209                         tmp[i]=bb.getDouble();\r
210                 }\r
211                 //Factorを読み出し\r
212                 this._dist.getValue(tmp);\r
213                 //double値を4個書き込む\r
214                 for (int i = 0; i < 4; i++) {\r
215                         tmp[i]=bb.getDouble();\r
216                 }\r
217                 i_stream.write(buf);\r
218                 return;\r
219         }\r
220 }\r