OSDN Git Service

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