OSDN Git Service

Merge branch 'git-svn'
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.3.1 / 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 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.core.param;\r
33 \r
34 import java.io.*;\r
35 import java.nio.*;\r
36 \r
37 import jp.nyatla.nyartoolkit.NyARException;\r
38 import jp.nyatla.nyartoolkit.core.types.*;\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         public NyARPerspectiveProjectionMatrix getPerspectiveProjectionMatrix()\r
58         {\r
59                 return this._projection_matrix;\r
60         }\r
61         public NyARCameraDistortionFactor getDistortionFactor()\r
62         {\r
63                 return this._dist;\r
64         }\r
65         /**\r
66          * \r
67          * @param i_factor\r
68          * NyARCameraDistortionFactorにセットする配列を指定する。要素数は4であること。\r
69          * @param i_projection\r
70          * NyARPerspectiveProjectionMatrixセットする配列を指定する。要素数は12であること。\r
71          */\r
72         public void setValue(double[] i_factor,double[] i_projection)\r
73         {\r
74                 this._dist.setValue(i_factor);\r
75                 this._projection_matrix.setValue(i_projection);\r
76                 return;\r
77         }\r
78 \r
79         /**\r
80          * ARToolKit標準ファイルから1個目の設定をロードする。\r
81          * \r
82          * @param i_filename\r
83          * @throws NyARException\r
84          */\r
85         public void loadARParamFromFile(String i_filename) throws NyARException\r
86         {\r
87                 try {\r
88                         loadARParam(new FileInputStream(i_filename));\r
89                 } catch (Exception e) {\r
90                         throw new NyARException(e);\r
91                 }\r
92         }\r
93 \r
94         /**\r
95          * int arParamChangeSize( ARParam *source, int xsize, int ysize, ARParam *newparam );\r
96          * 関数の代替関数 サイズプロパティをi_xsize,i_ysizeに変更します。\r
97          * @param i_xsize\r
98          * @param i_ysize\r
99          * @param newparam\r
100          * @return\r
101          * \r
102          */\r
103         public void changeScreenSize(int i_xsize, int i_ysize)\r
104         {\r
105                 final double scale = (double) i_xsize / (double) (this._screen_size.w);// scale = (double)xsize / (double)(source->xsize);\r
106                 //スケールを変更\r
107                 this._dist.changeScale(scale);\r
108                 this._projection_matrix.changeScale(scale);\r
109                 this._screen_size.w = i_xsize;// newparam->xsize = xsize;\r
110                 this._screen_size.h = i_ysize;// newparam->ysize = ysize;\r
111                 return;\r
112         }\r
113 \r
114 \r
115         /**\r
116          * int arParamLoad( const char *filename, int num, ARParam *param, ...);\r
117          * i_streamの入力ストリームからi_num個の設定を読み込み、パラメタを配列にして返します。\r
118          * \r
119          * @param i_stream\r
120          * @throws Exception\r
121          */\r
122         public void loadARParam(InputStream i_stream)throws NyARException\r
123         {\r
124                 try {\r
125                         byte[] buf = new byte[SIZE_OF_PARAM_SET];\r
126                         i_stream.read(buf);\r
127                         double[] tmp=new double[12];\r
128 \r
129                         // バッファを加工\r
130                         ByteBuffer bb = ByteBuffer.wrap(buf);\r
131                         bb.order(ByteOrder.BIG_ENDIAN);\r
132                         this._screen_size.w = bb.getInt();\r
133                         this._screen_size.h = bb.getInt();\r
134                         //double値を12個読み込む\r
135                         for(int i=0;i<12;i++){\r
136                                 tmp[i]=bb.getDouble();\r
137                         }\r
138                         //Projectionオブジェクトにセット\r
139                         this._projection_matrix.setValue(tmp);\r
140                         //double値を4個読み込む\r
141                         for (int i = 0; i < 4; i++) {\r
142                                 tmp[i]=bb.getDouble();\r
143                         }\r
144                         //Factorオブジェクトにセット\r
145                         this._dist.setValue(tmp);\r
146                 } catch (Exception e) {\r
147                         throw new NyARException(e);\r
148                 }\r
149                 return;\r
150         }\r
151 \r
152         public void saveARParam(OutputStream i_stream)throws Exception\r
153         {\r
154                 NyARException.trap("未チェックの関数");\r
155                 byte[] buf = new byte[SIZE_OF_PARAM_SET];\r
156                 // バッファをラップ\r
157                 ByteBuffer bb = ByteBuffer.wrap(buf);\r
158                 bb.order(ByteOrder.BIG_ENDIAN);\r
159 \r
160                 // 書き込み\r
161                 bb.putInt(this._screen_size.w);\r
162                 bb.putInt(this._screen_size.h);\r
163                 double[] tmp=new double[12];\r
164                 //Projectionを読み出し\r
165                 this._projection_matrix.getValue(tmp);\r
166                 //double値を12個書き込む\r
167                 for(int i=0;i<12;i++){\r
168                         tmp[i]=bb.getDouble();\r
169                 }\r
170                 //Factorを読み出し\r
171                 this._dist.getValue(tmp);\r
172                 //double値を4個書き込む\r
173                 for (int i = 0; i < 4; i++) {\r
174                         tmp[i]=bb.getDouble();\r
175                 }\r
176                 i_stream.write(buf);\r
177                 return;\r
178         }\r
179 }\r