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 edition ARToolKit class library.
\r
11 * Copyright (C)2008-2009 Ryo Iizuka
\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
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 program. If not, see <http://www.gnu.org/licenses/>.
\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
31 package jp.nyatla.nyartoolkit.core.pickup;
\r
33 import jp.nyatla.nyartoolkit.NyARException;
\r
34 import jp.nyatla.nyartoolkit.core.NyARMat;
\r
35 import jp.nyatla.nyartoolkit.core.raster.rgb.*;
\r
36 import jp.nyatla.nyartoolkit.core.rasterreader.*;
\r
37 import jp.nyatla.nyartoolkit.core.types.*;
\r
39 * 24ビットカラーのマーカーを保持するために使うクラスです。 このクラスは、ARToolkitのパターンと、ラスタから取得したパターンを保持します。
\r
43 public class NyARColorPatt_O3 implements INyARColorPatt
\r
45 private static final int AR_PATT_SAMPLE_NUM = 64;
\r
47 private int[] _patdata;
\r
48 private NyARBufferReader _buf_reader;
\r
49 private NyARIntSize _size;
\r
50 private NyARRgbPixelReader_INT1D_X8R8G8B8_32 _pixelreader;
\r
52 public NyARColorPatt_O3(int i_width, int i_height)
\r
54 this._size=new NyARIntSize(i_width,i_height);
\r
55 this._patdata = new int[i_height*i_width];
\r
56 this._buf_reader=new NyARBufferReader(this._patdata,NyARBufferReader.BUFFERFORMAT_INT1D_X8R8G8B8_32);
\r
57 this._pixelreader=new NyARRgbPixelReader_INT1D_X8R8G8B8_32(this._patdata,this._size);
\r
59 public int getWidth()
\r
61 return this._size.w;
\r
63 public int getHeight()
\r
65 return this._size.h;
\r
67 public NyARIntSize getSize()
\r
71 public INyARBufferReader getBufferReader()
\r
73 return this._buf_reader;
\r
75 public INyARRgbPixelReader getRgbPixelReader()
\r
77 return this._pixelreader;
\r
79 private final NyARMat wk_get_cpara_a = new NyARMat(8, 8);
\r
80 private final NyARMat wk_get_cpara_b = new NyARMat(8, 1);
\r
81 private final NyARMat wk_pickFromRaster_cpara = new NyARMat(8, 1);
\r
87 * @throws NyARException
\r
89 private boolean get_cpara(final NyARIntPoint2d[] i_vertex, NyARMat o_para)throws NyARException
\r
91 int[][] world = wk_pickFromRaster_world;
\r
92 NyARMat a = wk_get_cpara_a;// 次処理で値を設定するので、初期化不要// new NyARMat( 8, 8 );
\r
93 double[][] a_array = a.getArray();
\r
94 NyARMat b = wk_get_cpara_b;// 次処理で値を設定するので、初期化不要// new NyARMat( 8, 1 );
\r
95 double[][] b_array = b.getArray();
\r
96 double[] a_pt0, a_pt1;
\r
99 for (int i = 0; i < 4; i++) {
\r
100 a_pt0 = a_array[i * 2];
\r
101 a_pt1 = a_array[i * 2 + 1];
\r
102 world_pti = world[i];
\r
104 a_pt0[0] = (double) world_pti[0];// a->m[i*16+0] = world[i][0];
\r
105 a_pt0[1] = (double) world_pti[1];// a->m[i*16+1] = world[i][1];
\r
106 a_pt0[2] = 1.0;// a->m[i*16+2] = 1.0;
\r
107 a_pt0[3] = 0.0;// a->m[i*16+3] = 0.0;
\r
108 a_pt0[4] = 0.0;// a->m[i*16+4] = 0.0;
\r
109 a_pt0[5] = 0.0;// a->m[i*16+5] = 0.0;
\r
110 a_pt0[6] = (double) (-world_pti[0] * i_vertex[i].x);// a->m[i*16+6]= -world[i][0]*vertex[i][0];
\r
111 a_pt0[7] = (double) (-world_pti[1] * i_vertex[i].x);// a->m[i*16+7]=-world[i][1]*vertex[i][0];
\r
112 a_pt1[0] = 0.0;// a->m[i*16+8] = 0.0;
\r
113 a_pt1[1] = 0.0;// a->m[i*16+9] = 0.0;
\r
114 a_pt1[2] = 0.0;// a->m[i*16+10] = 0.0;
\r
115 a_pt1[3] = (double) world_pti[0];// a->m[i*16+11] = world[i][0];
\r
116 a_pt1[4] = (double) world_pti[1];// a->m[i*16+12] = world[i][1];
\r
117 a_pt1[5] = 1.0;// a->m[i*16+13] = 1.0;
\r
118 a_pt1[6] = (double) (-world_pti[0] * i_vertex[i].y);// a->m[i*16+14]=-world[i][0]*vertex[i][1];
\r
119 a_pt1[7] = (double) (-world_pti[1] * i_vertex[i].y);// a->m[i*16+15]=-world[i][1]*vertex[i][1];
\r
120 b_array[i * 2 + 0][0] = (double) i_vertex[i].x;// b->m[i*2+0] =vertex[i][0];
\r
121 b_array[i * 2 + 1][0] = (double) i_vertex[i].y;// b->m[i*2+1] =vertex[i][1];
\r
123 if (!a.matrixSelfInv()) {
\r
127 o_para.matrixMul(a, b);
\r
131 // private final double[] wk_pickFromRaster_para=new double[9];//[3][3];
\r
132 private final static int[][] wk_pickFromRaster_world = {// double world[4][2];
\r
133 { 100, 100 }, { 100 + 10, 100 }, { 100 + 10, 100 + 10 }, { 100, 100 + 10 } };
\r
138 * @see INyARColorPatt#pickFromRaster
\r
140 public boolean pickFromRaster(INyARRgbRaster image,NyARIntPoint2d[] i_vertexs)throws NyARException
\r
142 NyARMat cpara = this.wk_pickFromRaster_cpara;
\r
148 w1 = i_vertexs[0].x - i_vertexs[1].x;
\r
149 w2 = i_vertexs[0].y - i_vertexs[1].y;
\r
150 l1 = (w1 * w1 + w2 * w2);
\r
151 w1 = i_vertexs[2].x - i_vertexs[3].x;
\r
152 w2 = i_vertexs[2].y - i_vertexs[3].y;
\r
153 l2 = (w1 * w1 + w2 * w2);
\r
158 xdiv2 = this._size.w;
\r
159 while (xdiv2 * xdiv2 < l1) {
\r
162 if (xdiv2 > AR_PATT_SAMPLE_NUM) {
\r
163 xdiv2 = AR_PATT_SAMPLE_NUM;
\r
167 w1 = i_vertexs[1].x - i_vertexs[2].x;
\r
168 w2 = i_vertexs[1].y - i_vertexs[2].y;
\r
169 l1 = (w1 * w1 + w2 * w2);
\r
170 w1 = i_vertexs[3].x - i_vertexs[0].x;
\r
171 w2 = i_vertexs[3].y - i_vertexs[0].y;
\r
172 l2 = (w1 * w1 + w2 * w2);
\r
176 ydiv2 = this._size.h;
\r
178 while (ydiv2 * ydiv2 < l1) {
\r
181 if (ydiv2 > AR_PATT_SAMPLE_NUM) {
\r
182 ydiv2 = AR_PATT_SAMPLE_NUM;
\r
186 if (!get_cpara(i_vertexs, cpara)) {
\r
189 updateExtpat(image, cpara, xdiv2, ydiv2);
\r
193 private int[] __updateExtpat_rgbset;
\r
194 private int[] __updateExtpat_xc;
\r
195 private int[] __updateExtpat_yc;
\r
196 private double[] __updateExtpat_xw;
\r
197 private double[] __updateExtpat_yw;
\r
198 private int _last_pix_resolution_x=0;
\r
199 private int _last_pix_resolution_y=0;
\r
200 private void reservWorkBuffers(int i_xdiv,int i_ydiv)
\r
202 if(this._last_pix_resolution_x<i_xdiv || this._last_pix_resolution_y<i_ydiv){
\r
203 this.__updateExtpat_xc=new int[i_xdiv*i_ydiv];
\r
204 this.__updateExtpat_yc=new int[i_xdiv*i_ydiv];
\r
205 this.__updateExtpat_xw=new double[i_xdiv];
\r
206 this.__updateExtpat_yw=new double[i_ydiv];
\r
207 this.__updateExtpat_rgbset=new int[i_xdiv*i_ydiv*3];
\r
208 this._last_pix_resolution_x=i_xdiv;
\r
209 this._last_pix_resolution_y=i_ydiv;
\r
213 private static double LT_POS=102.5;
\r
214 private static double SQ_SIZE=5.0;
\r
216 //分割数16未満になると少し遅くなるかも。
\r
217 private void updateExtpat(INyARRgbRaster image, NyARMat i_cpara, int i_xdiv2,int i_ydiv2) throws NyARException
\r
223 final int pat_size_w=this._size.w;
\r
224 final int xdiv = i_xdiv2 / pat_size_w;// xdiv = xdiv2/Config.AR_PATT_SIZE_X;
\r
225 final int ydiv = i_ydiv2 / this._size.h;// ydiv = ydiv2/Config.AR_PATT_SIZE_Y;
\r
226 final int xdiv_x_ydiv = xdiv * ydiv;
\r
228 final double[][] para=i_cpara.getArray();
\r
229 final double para00=para[0*3+0][0];
\r
230 final double para01=para[0*3+1][0];
\r
231 final double para02=para[0*3+2][0];
\r
232 final double para10=para[1*3+0][0];
\r
233 final double para11=para[1*3+1][0];
\r
234 final double para12=para[1*3+2][0];
\r
235 final double para20=para[2*3+0][0];
\r
236 final double para21=para[2*3+1][0];
\r
238 INyARRgbPixelReader reader=image.getRgbPixelReader();
\r
239 final int img_width=image.getWidth();
\r
240 final int img_height=image.getHeight();
\r
243 reservWorkBuffers(xdiv,ydiv);
\r
244 final double[] xw=this.__updateExtpat_xw;
\r
245 final double[] yw=this.__updateExtpat_yw;
\r
246 final int[] xc=this.__updateExtpat_xc;
\r
247 final int[] yc=this.__updateExtpat_yc;
\r
248 int[] rgb_set = this.__updateExtpat_rgbset;
\r
251 for(int iy=this._size.h-1;iy>=0;iy--){
\r
252 for(int ix=pat_size_w-1;ix>=0;ix--){
\r
254 reciprocal= 1.0 / i_xdiv2;
\r
255 for(i=xdiv-1;i>=0;i--){
\r
256 xw[i]=LT_POS + SQ_SIZE * (ix*xdiv+i + 0.5) * reciprocal;
\r
258 reciprocal= 1.0 / i_ydiv2;
\r
259 for(i=ydiv-1;i>=0;i--){
\r
260 yw[i]=LT_POS + SQ_SIZE * (iy*ydiv+i + 0.5) * reciprocal;
\r
262 //1ピクセルを構成するピクセル座標の集合をxc,yc配列に取得
\r
263 int number_of_pix=0;
\r
264 for(i=ydiv-1;i>=0;i--)
\r
266 final double para01_x_yw_para02=para01 * yw[i] + para02;
\r
267 final double para11_x_yw_para12=para11 * yw[i] + para12;
\r
268 final double para12_x_yw_para22=para21 * yw[i]+ 1.0;
\r
269 for(j=xdiv-1;j>=0;j--){
\r
271 final double d = para20 * xw[j] + para12_x_yw_para22;
\r
273 throw new NyARException();
\r
275 final int xcw= (int) ((para00 * xw[j] + para01_x_yw_para02) / d);
\r
276 final int ycw= (int) ((para10 * xw[j] + para11_x_yw_para12) / d);
\r
277 if(xcw<0 || xcw>=img_width || ycw<0 ||ycw>=img_height){
\r
280 xc[number_of_pix] =xcw;
\r
281 yc[number_of_pix] =ycw;
\r
286 reader.getPixelSet(xc,yc,number_of_pix, rgb_set);
\r
288 for(i=number_of_pix*3-1;i>=0;i-=3){
\r
289 r += rgb_set[i-2];// R
\r
290 g += rgb_set[i-1];// G
\r
291 b += rgb_set[i];// B
\r
294 this._patdata[iy*pat_size_w+ix]=(((r / xdiv_x_ydiv)&0xff)<<16)|(((g / xdiv_x_ydiv)&0xff)<<8)|(((b / xdiv_x_ydiv)&0xff));
\r