OSDN Git Service

Merge branch 'git-svn'
[nyartoolkit-and/nyartoolkit-and.git] / tags / 1.2.0 / src / jp / nyatla / nyartoolkit / core / NyARCode.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 \r
33 package jp.nyatla.nyartoolkit.core;\r
34 \r
35 import java.io.FileInputStream;\r
36 import java.io.InputStream;\r
37 import java.io.InputStreamReader;\r
38 import java.io.StreamTokenizer;\r
39 \r
40 import jp.nyatla.nyartoolkit.NyARException;\r
41 \r
42 \r
43 \r
44 \r
45 \r
46 /**\r
47  * ARToolKitのマーカーコードを1個保持します。\r
48  *\r
49  */\r
50 public class NyARCode{\r
51     private int[][][][] pat;//static int    pat[AR_PATT_NUM_MAX][4][AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3];\r
52     private double[]            patpow=new double[4];//static double patpow[AR_PATT_NUM_MAX][4];\r
53     private short[][][] patBW;//static int    patBW[AR_PATT_NUM_MAX][4][AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3];\r
54     private double[]            patpowBW=new double[4];//static double patpowBW[AR_PATT_NUM_MAX][4];    \r
55     private int width,height;\r
56     public int[][][][] getPat()\r
57     {\r
58         return pat;\r
59     }\r
60     public double [] getPatPow()\r
61     {\r
62         return patpow;\r
63     }\r
64     public short[][][] getPatBW()\r
65     {\r
66         return patBW;\r
67     }\r
68     public double[] getPatPowBW()\r
69     {\r
70         return patpowBW;\r
71     }\r
72     public int getWidth()\r
73     {\r
74         return width;\r
75     }\r
76     public int getHeight()\r
77     {\r
78         return height;\r
79     }\r
80     \r
81     public NyARCode(int i_width,int i_height)\r
82     {\r
83         width=i_width;\r
84         height=i_height;\r
85         pat=new int[4][height][width][3];//static int    pat[AR_PATT_NUM_MAX][4][AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3];\r
86         patBW=new short[4][height][width];//static int    patBW[AR_PATT_NUM_MAX][4][AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3];\r
87     }\r
88     /**\r
89      * int arLoadPatt( const char *filename );\r
90      * ARToolKitのパターンファイルをロードする。\r
91      * ファイル形式はBGR形式で記録されたパターンファイルであること。\r
92      * @param filename\r
93      * @return\r
94      * @throws Exception\r
95      */\r
96     public void loadFromARFile(String filename) throws NyARException\r
97     {\r
98         try {\r
99             loadFromARFile(new FileInputStream(filename));\r
100 \r
101         } catch (Exception e) {\r
102         throw new NyARException(e);\r
103         }\r
104     }\r
105     /**\r
106      * \r
107      * @param i_stream\r
108      * @throws NyARException\r
109      */\r
110     public void loadFromARFile(InputStream i_stream) throws NyARException\r
111     {\r
112         try{\r
113             StreamTokenizer st=new StreamTokenizer(new InputStreamReader(i_stream));\r
114             //パターンデータはGBRAで並んでる。\r
115             for(int h=0; h<4; h++ ) {\r
116                 int l = 0;\r
117                 for(int i3 = 0; i3 < 3; i3++ ) {\r
118                     for(int i2 = 0; i2 < height; i2++ ) {\r
119                         for(int i1 = 0; i1 < width; i1++ ){\r
120                             //数値のみ読み出す\r
121                             switch(st.nextToken()){//if( fscanf(fp, "%d", &j) != 1 ) {\r
122                             case StreamTokenizer.TT_NUMBER:\r
123                                 break;\r
124                             default:\r
125                                 throw new NyARException();\r
126                             }\r
127                             short j=(short)(255-st.nval);//j = 255-j;\r
128                             //標準ファイルのパターンはBGRでならんでるからRGBに並べなおす\r
129                             switch(i3){\r
130                             case 0:pat[h][i2][i1][2] = j;break;//pat[patno][h][(i2*Config.AR_PATT_SIZE_X+i1)*3+2] = j;break;\r
131                             case 1:pat[h][i2][i1][1] = j;break;//pat[patno][h][(i2*Config.AR_PATT_SIZE_X+i1)*3+1] = j;break;\r
132                             case 2:pat[h][i2][i1][0] = j;break;//pat[patno][h][(i2*Config.AR_PATT_SIZE_X+i1)*3+0] = j;break;\r
133                             }\r
134                             //pat[patno][h][(i2*Config.AR_PATT_SIZE_X+i1)*3+i3] = j;\r
135                             if( i3 == 0 ){\r
136                                 patBW[h][i2][i1]  = j;//patBW[patno][h][i2*Config.AR_PATT_SIZE_X+i1]  = j;\r
137                             }else{\r
138                                 patBW[h][i2][i1] += j;//patBW[patno][h][i2*Config.AR_PATT_SIZE_X+i1] += j;\r
139                             }\r
140                             if( i3 == 2 ){\r
141                                 patBW[h][i2][i1] /= 3;//patBW[patno][h][i2*Config.AR_PATT_SIZE_X+i1] /= 3;\r
142                             }\r
143                             l += j;\r
144                         }\r
145                     }\r
146                 }\r
147   \r
148                 l /= (height*width*3);\r
149        \r
150                 int m = 0;\r
151                 for(int i = 0; i < height; i++ ) {//for( i = 0; i < AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3; i++ ) {\r
152                     for(int i2 = 0; i2 < width; i2++ ) {\r
153                         for(int i3 = 0; i3 < 3; i3++ ) {\r
154                             pat[h][i][i2][i3] -= l;\r
155                             m += (pat[h][i][i2][i3]*pat[h][i][i2][i3]);\r
156                         }\r
157                     }\r
158                 }\r
159                 patpow[h] = Math.sqrt((double)m);\r
160                 if( patpow[h] == 0.0 ){\r
161                     patpow[h] = 0.0000001;\r
162                 }\r
163     \r
164                 m = 0;\r
165                 for(int i = 0; i < height; i++ ) {\r
166                     for(int i2 = 0; i2 < width; i2++ ) {\r
167                         patBW[h][i][i2] -= l;\r
168                         m += (patBW[h][i][i2]*patBW[h][i][i2]);\r
169                     }\r
170                 }\r
171                 patpowBW[h] = Math.sqrt((double)m);\r
172                 if(patpowBW[h] == 0.0 ){\r
173                     patpowBW[h] = 0.0000001;\r
174                 }\r
175             }\r
176         }catch(Exception e){\r
177             throw new NyARException(e);\r
178         }\r
179     }\r
180 }\r