2 * PROJECT: NyARToolkit
\r
3 * --------------------------------------------------------------------------------
\r
4 * This work is based on the original ARToolKit developed by
\r
7 * HITLab, University of Washington, Seattle
\r
8 * http://www.hitl.washington.edu/artoolkit/
\r
10 * The NyARToolkit is Java version ARToolkit class library.
\r
11 * Copyright (C)2008 R.Iizuka
\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
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
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
27 * For further information please contact.
\r
28 * http://nyatla.jp/nyatoolkit/
\r
29 * <airmail(at)ebony.plala.or.jp>
\r
33 package jp.nyatla.nyartoolkit.core;
\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
40 import jp.nyatla.nyartoolkit.NyARException;
\r
43 * ARToolKitのマーカーコードを1個保持します。
\r
46 public class NyARCode
\r
48 private int[][][][] pat;// static int
\r
49 // pat[AR_PATT_NUM_MAX][4][AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3];
\r
51 private double[] patpow = new double[4];// static double
\r
52 // patpow[AR_PATT_NUM_MAX][4];
\r
54 private short[][][] patBW;// static int
\r
55 // patBW[AR_PATT_NUM_MAX][4][AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3];
\r
57 private double[] patpowBW = new double[4];// static double
\r
58 // patpowBW[AR_PATT_NUM_MAX][4];
\r
60 private int width, height;
\r
62 public int[][][][] getPat()
\r
67 public double[] getPatPow()
\r
72 public short[][][] getPatBW()
\r
77 public double[] getPatPowBW()
\r
82 public int getWidth()
\r
87 public int getHeight()
\r
92 public NyARCode(int i_width, int i_height)
\r
96 pat = new int[4][height][width][3];// static int
\r
97 // pat[AR_PATT_NUM_MAX][4][AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3];
\r
98 patBW = new short[4][height][width];// static int
\r
99 // patBW[AR_PATT_NUM_MAX][4][AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3];
\r
103 * int arLoadPatt( const char *filename ); ARToolKitのパターンファイルをロードする。
\r
104 * ファイル形式はBGR形式で記録されたパターンファイルであること。
\r
108 * @throws Exception
\r
110 public void loadFromARFile(String filename) throws NyARException
\r
113 loadFromARFile(new FileInputStream(filename));
\r
115 } catch (Exception e) {
\r
116 throw new NyARException(e);
\r
123 * @throws NyARException
\r
125 public void loadFromARFile(InputStream i_stream) throws NyARException
\r
128 StreamTokenizer st = new StreamTokenizer(new InputStreamReader(
\r
130 // パターンデータはGBRAで並んでる。
\r
131 for (int h = 0; h < 4; h++) {
\r
133 for (int i3 = 0; i3 < 3; i3++) {
\r
134 for (int i2 = 0; i2 < height; i2++) {
\r
135 for (int i1 = 0; i1 < width; i1++) {
\r
137 switch (st.nextToken()) {// if( fscanf(fp, "%d",
\r
139 case StreamTokenizer.TT_NUMBER:
\r
142 throw new NyARException();
\r
144 short j = (short) (255 - st.nval);// j = 255-j;
\r
145 // 標準ファイルのパターンはBGRでならんでるからRGBに並べなおす
\r
148 pat[h][i2][i1][2] = j;
\r
149 break;// pat[patno][h][(i2*Config.AR_PATT_SIZE_X+i1)*3+2]
\r
152 pat[h][i2][i1][1] = j;
\r
153 break;// pat[patno][h][(i2*Config.AR_PATT_SIZE_X+i1)*3+1]
\r
156 pat[h][i2][i1][0] = j;
\r
157 break;// pat[patno][h][(i2*Config.AR_PATT_SIZE_X+i1)*3+0]
\r
160 // pat[patno][h][(i2*Config.AR_PATT_SIZE_X+i1)*3+i3]
\r
163 patBW[h][i2][i1] = j;// patBW[patno][h][i2*Config.AR_PATT_SIZE_X+i1]
\r
166 patBW[h][i2][i1] += j;// patBW[patno][h][i2*Config.AR_PATT_SIZE_X+i1]
\r
170 patBW[h][i2][i1] /= 3;// patBW[patno][h][i2*Config.AR_PATT_SIZE_X+i1]
\r
178 l /= (height * width * 3);
\r
181 for (int i = 0; i < height; i++) {// for( i = 0; i <
\r
182 // AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3;
\r
184 for (int i2 = 0; i2 < width; i2++) {
\r
185 for (int i3 = 0; i3 < 3; i3++) {
\r
186 pat[h][i][i2][i3] -= l;
\r
187 m += (pat[h][i][i2][i3] * pat[h][i][i2][i3]);
\r
191 patpow[h] = Math.sqrt((double) m);
\r
192 if (patpow[h] == 0.0) {
\r
193 patpow[h] = 0.0000001;
\r
197 for (int i = 0; i < height; i++) {
\r
198 for (int i2 = 0; i2 < width; i2++) {
\r
199 patBW[h][i][i2] -= l;
\r
200 m += (patBW[h][i][i2] * patBW[h][i][i2]);
\r
203 patpowBW[h] = Math.sqrt((double) m);
\r
204 if (patpowBW[h] == 0.0) {
\r
205 patpowBW[h] = 0.0000001;
\r
208 } catch (Exception e) {
\r
209 throw new NyARException(e);
\r