Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 203 | chris | 1 | /* |
| 2 | * PROJECT: NyARToolkit |
||
| 3 | * -------------------------------------------------------------------------------- |
||
| 4 | * This work is based on the original ARToolKit developed by |
||
| 5 | * Hirokazu Kato |
||
| 6 | * Mark Billinghurst |
||
| 7 | * HITLab, University of Washington, Seattle |
||
| 8 | * http://www.hitl.washington.edu/artoolkit/ |
||
| 9 | * |
||
| 10 | * The NyARToolkit is Java version ARToolkit class library. |
||
| 11 | * Copyright (C)2008 R.Iizuka |
||
| 12 | * |
||
| 13 | * This program is free software; you can redistribute it and/or |
||
| 14 | * modify it under the terms of the GNU General Public License |
||
| 15 | * as published by the Free Software Foundation; either version 2 |
||
| 16 | * of the License, or (at your option) any later version. |
||
| 17 | * |
||
| 18 | * This program is distributed in the hope that it will be useful, |
||
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 21 | * GNU General Public License for more details. |
||
| 22 | * |
||
| 23 | * You should have received a copy of the GNU General Public License |
||
| 24 | * along with this framework; if not, write to the Free Software |
||
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
| 26 | * |
||
| 27 | * For further information please contact. |
||
| 28 | * http://nyatla.jp/nyatoolkit/ |
||
| 29 | * <airmail(at)ebony.plala.or.jp> |
||
| 30 | * |
||
| 31 | */ |
||
| 32 | package jp.nyatla.nyartoolkit.core; |
||
| 33 | |||
| 34 | import jp.nyatla.nyartoolkit.NyARException; |
||
| 35 | import jp.nyatla.nyartoolkit.core.raster.NyARRaster; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * 24ビットカラーのマーカーを保持するために使うクラスです。 |
||
| 39 | * このクラスは、ARToolkitのパターンと、ラスタから取得したパターンを保持します。 |
||
| 40 | * 演算順序以外の最適化をしたもの |
||
| 41 | * |
||
| 42 | */ |
||
| 43 | public class NyARColorPatt_O1 implements NyARColorPatt |
||
| 44 | { |
||
| 45 | private static final int AR_PATT_SAMPLE_NUM=64;//#define AR_PATT_SAMPLE_NUM 64 |
||
| 46 | private int extpat[][][]; |
||
| 47 | private int width; |
||
| 48 | private int height; |
||
| 49 | public NyARColorPatt_O1(int i_width,int i_height) |
||
| 50 | { |
||
| 51 | this.width =i_width; |
||
| 52 | this.height=i_height; |
||
| 53 | this.extpat=new int[i_height][i_width][3]; |
||
| 54 | this.wk_pickFromRaster_ext_pat2=new int[i_height][i_width][3]; |
||
| 55 | } |
||
| 56 | // public void setSize(int i_new_width,int i_new_height) |
||
| 57 | // { |
||
| 58 | // int array_w=this.extpat[0].length; |
||
| 59 | // int array_h=this.extpat.length; |
||
| 60 | // //十分なサイズのバッファがあるか確認 |
||
| 61 | // if(array_w>=i_new_width && array_h>=i_new_height){ |
||
| 62 | // //OK 十分だ→サイズ調整のみ |
||
| 63 | // }else{ |
||
| 64 | // //足りないよ→取り直し |
||
| 65 | // this.wk_pickFromRaster_ext_pat2=new int[i_new_height][i_new_width][3]; |
||
| 66 | // this.extpat=new int[i_new_height][i_new_width][3]; |
||
| 67 | // } |
||
| 68 | // this.width =i_new_width; |
||
| 69 | // this.height=i_new_height; |
||
| 70 | // return; |
||
| 71 | // } |
||
| 72 | |||
| 73 | public int[][][] getPatArray() |
||
| 74 | { |
||
| 75 | return extpat; |
||
| 76 | } |
||
| 77 | public int getWidth() |
||
| 78 | { |
||
| 79 | return width; |
||
| 80 | } |
||
| 81 | public int getHeight() |
||
| 82 | { |
||
| 83 | return height; |
||
| 84 | } |
||
| 85 | private final NyARMat wk_get_cpara_a=new NyARMat(8,8); |
||
| 86 | private final NyARMat wk_get_cpara_b=new NyARMat(8,1); |
||
| 87 | private final NyARMat wk_get_cpara_c=new NyARMat(8,1); |
||
| 88 | /** |
||
| 89 | * |
||
| 90 | * @param world |
||
| 91 | * @param vertex |
||
| 92 | * @param para |
||
| 93 | * [3x3] |
||
| 94 | * @throws NyARException |
||
| 95 | */ |
||
| 96 | private boolean get_cpara( double world[][], double vertex[][],double[] para) throws NyARException |
||
| 97 | { |
||
| 98 | NyARMat a =wk_get_cpara_a;//次処理で値を設定するので、初期化不要// new NyARMat( 8, 8 ); |
||
| 99 | double[][] a_array=a.getArray(); |
||
| 100 | NyARMat b =wk_get_cpara_b;//次処理で値を設定するので、初期化不要// new NyARMat( 8, 1 ); |
||
| 101 | double[][] b_array=b.getArray(); |
||
| 102 | double[] a_pt0,a_pt1,world_pti; |
||
| 103 | |||
| 104 | for(int i = 0; i < 4; i++ ) { |
||
| 105 | a_pt0=a_array[i*2]; |
||
| 106 | a_pt1=a_array[i*2+1]; |
||
| 107 | world_pti=world[i]; |
||
| 108 | |||
| 109 | a_pt0[0]=world_pti[0];//a->m[i*16+0] = world[i][0]; |
||
| 110 | a_pt0[1]=world_pti[1];//a->m[i*16+1] = world[i][1]; |
||
| 111 | a_pt0[2]=1.0;//a->m[i*16+2] = 1.0; |
||
| 112 | a_pt0[3]=0.0;//a->m[i*16+3] = 0.0; |
||
| 113 | a_pt0[4]=0.0;//a->m[i*16+4] = 0.0; |
||
| 114 | a_pt0[5]=0.0;//a->m[i*16+5] = 0.0; |
||
| 115 | a_pt0[6]=-world_pti[0] * vertex[i][0];//a->m[i*16+6] = -world[i][0] * vertex[i][0]; |
||
| 116 | a_pt0[7]=-world_pti[1] * vertex[i][0];//a->m[i*16+7] = -world[i][1] * vertex[i][0]; |
||
| 117 | a_pt1[0]=0.0;//a->m[i*16+8] = 0.0; |
||
| 118 | a_pt1[1]=0.0;//a->m[i*16+9] = 0.0; |
||
| 119 | a_pt1[2]=0.0;//a->m[i*16+10] = 0.0; |
||
| 120 | a_pt1[3]=world_pti[0];//a->m[i*16+11] = world[i][0]; |
||
| 121 | a_pt1[4]=world_pti[1];//a->m[i*16+12] = world[i][1]; |
||
| 122 | a_pt1[5]=1.0;//a->m[i*16+13] = 1.0; |
||
| 123 | a_pt1[6]=-world_pti[0] * vertex[i][1];//a->m[i*16+14] = -world[i][0] * vertex[i][1]; |
||
| 124 | a_pt1[7]=-world_pti[1] * vertex[i][1];//a->m[i*16+15] = -world[i][1] * vertex[i][1]; |
||
| 125 | b_array[i*2+0][0]=vertex[i][0];//b->m[i*2+0] = vertex[i][0]; |
||
| 126 | b_array[i*2+1][0]=vertex[i][1];//b->m[i*2+1] = vertex[i][1]; |
||
| 127 | } |
||
| 128 | // JartkException.trap("未チェックのパス"); |
||
| 129 | if(!a.matrixSelfInv()){ |
||
| 130 | return false;//逆行列を求められないので失敗 |
||
| 131 | } |
||
| 132 | |||
| 133 | // JartkException.trap("未チェックのパス"); |
||
| 134 | NyARMat c = wk_get_cpara_c;//次処理で結果を受け取るので、初期化不要//new NyARMat( 8, 1 ); |
||
| 135 | double[][] c_array=c.getArray(); |
||
| 136 | |||
| 137 | c.matrixMul(a, b); |
||
| 138 | for(int i = 0; i < 2; i++ ) { |
||
| 139 | para[i*3+0] = c_array[i*3+0][0];//para[i][0] = c->m[i*3+0]; |
||
| 140 | para[i*3+1] = c_array[i*3+1][0];//para[i][1] = c->m[i*3+1]; |
||
| 141 | para[i*3+2] = c_array[i*3+2][0];//para[i][2] = c->m[i*3+2]; |
||
| 142 | } |
||
| 143 | para[2*3+0] = c_array[2*3+0][0];//para[2][0] = c->m[2*3+0]; |
||
| 144 | para[2*3+1] = c_array[2*3+1][0];//para[2][1] = c->m[2*3+1]; |
||
| 145 | para[2*3+2] = 1.0;//para[2][2] = 1.0; |
||
| 146 | return true; |
||
| 147 | } |
||
| 148 | |||
| 149 | private final double[][] wk_pickFromRaster_local=new double[4][2]; |
||
| 150 | private final double[] wk_pickFromRaster_para=new double[9];//[3][3]; |
||
| 151 | private int[][][] wk_pickFromRaster_ext_pat2=null;//コンストラクタでint[height][width][3]を作る |
||
| 152 | private final double[][] wk_pickFromRaster_world={//double world[4][2]; |
||
| 153 | {100.0, 100.0}, |
||
| 154 | {100.0+10.0,100.0}, |
||
| 155 | {100.0+10.0,100.0 + 10.0}, |
||
| 156 | {100.0, 100.0 + 10.0} |
||
| 157 | }; |
||
| 158 | /** |
||
| 159 | * pickFromRaster関数から使う変数です。 |
||
| 160 | * |
||
| 161 | */ |
||
| 162 | private static void initValue_wk_pickFromRaster_ext_pat2(int[][][] i_ext_pat2,int i_width,int i_height) |
||
| 163 | { |
||
| 164 | int i,i2; |
||
| 165 | int[][] pt2; |
||
| 166 | int[] pt1; |
||
| 167 | for(i=i_height-1;i>=0;i--){ |
||
| 168 | pt2=i_ext_pat2[i]; |
||
| 169 | for(i2=i_width-1;i2>=0;i2--){ |
||
| 170 | pt1=pt2[i2]; |
||
| 171 | pt1[0]=0; |
||
| 172 | pt1[1]=0; |
||
| 173 | pt1[2]=0; |
||
| 174 | } |
||
| 175 | } |
||
| 176 | } |
||
| 177 | private final int[] wk_pickFromRaster_rgb_tmp=new int[3]; |
||
| 178 | /** |
||
| 179 | * imageから、i_markerの位置にあるパターンを切り出して、保持します。 |
||
| 180 | * Optimize:STEP[769->] |
||
| 181 | * @param image |
||
| 182 | * @param i_marker |
||
| 183 | * @return |
||
| 184 | * 切り出しに失敗した |
||
| 185 | * @throws Exception |
||
| 186 | */ |
||
| 187 | public boolean pickFromRaster(NyARRaster image, NyARMarker i_marker) throws NyARException |
||
| 188 | { |
||
| 189 | double d, xw, yw; |
||
| 190 | int xc, yc; |
||
| 191 | int xdiv, ydiv; |
||
| 192 | int xdiv2, ydiv2; |
||
| 193 | int lx1, lx2, ly1, ly2; |
||
| 194 | |||
| 195 | int img_x=image.getWidth(); |
||
| 196 | int img_y=image.getHeight(); |
||
| 197 | |||
| 198 | double xdiv2_reciprocal; // [tp] |
||
| 199 | double ydiv2_reciprocal; // [tp] |
||
| 200 | |||
| 201 | |||
| 202 | int[] x_coord=i_marker.x_coord; |
||
| 203 | int[] y_coord=i_marker.y_coord; |
||
| 204 | double[][] local=wk_pickFromRaster_local;//double local[4][2]; |
||
| 205 | int[] vertex=i_marker.mkvertex; |
||
| 206 | for(int i = 0; i < 4; i++ ) { |
||
| 207 | local[i][0] = x_coord[vertex[i]]; |
||
| 208 | local[i][1] = y_coord[vertex[i]]; |
||
| 209 | } |
||
| 210 | double[][] world=wk_pickFromRaster_world; |
||
| 211 | /* world[0][0] = 100.0; |
||
| 212 | world[0][1] = 100.0; |
||
| 213 | world[1][0] = 100.0 + 10.0; |
||
| 214 | world[1][1] = 100.0; |
||
| 215 | world[2][0] = 100.0 + 10.0; |
||
| 216 | world[2][1] = 100.0 + 10.0; |
||
| 217 | world[3][0] = 100.0; |
||
| 218 | world[3][1] = 100.0 + 10.0;*/ |
||
| 219 | double[] para =wk_pickFromRaster_para; //double para[3][3]; |
||
| 220 | //パターンの切り出しに失敗することもある。 |
||
| 221 | if(!get_cpara( world, local, para )){ |
||
| 222 | return false; |
||
| 223 | } |
||
| 224 | lx1 = (int)((local[0][0] - local[1][0])*(local[0][0] - local[1][0])+ (local[0][1] - local[1][1])*(local[0][1] - local[1][1])); |
||
| 225 | lx2 = (int)((local[2][0] - local[3][0])*(local[2][0] - local[3][0])+ (local[2][1] - local[3][1])*(local[2][1] - local[3][1])); |
||
| 226 | ly1 = (int)((local[1][0] - local[2][0])*(local[1][0] - local[2][0])+ (local[1][1] - local[2][1])*(local[1][1] - local[2][1])); |
||
| 227 | ly2 = (int)((local[3][0] - local[0][0])*(local[3][0] - local[0][0])+ (local[3][1] - local[0][1])*(local[3][1] - local[0][1])); |
||
| 228 | if( lx2 > lx1 ){ |
||
| 229 | lx1 = lx2; |
||
| 230 | } |
||
| 231 | if( ly2 > ly1 ){ |
||
| 232 | ly1 = ly2; |
||
| 233 | } |
||
| 234 | xdiv2 =this.width; |
||
| 235 | ydiv2 =this.height; |
||
| 236 | |||
| 237 | while( xdiv2*xdiv2 < lx1/4 ){ |
||
| 238 | xdiv2*=2; |
||
| 239 | } |
||
| 240 | while( ydiv2*ydiv2 < ly1/4 ){ |
||
| 241 | ydiv2*=2; |
||
| 242 | } |
||
| 243 | |||
| 244 | if( xdiv2 > AR_PATT_SAMPLE_NUM) |
||
| 245 | { |
||
| 246 | xdiv2 =AR_PATT_SAMPLE_NUM; |
||
| 247 | } |
||
| 248 | if( ydiv2 >AR_PATT_SAMPLE_NUM) |
||
| 249 | { |
||
| 250 | ydiv2 = AR_PATT_SAMPLE_NUM; |
||
| 251 | } |
||
| 252 | |||
| 253 | xdiv = xdiv2/width;//xdiv = xdiv2/Config.AR_PATT_SIZE_X; |
||
| 254 | ydiv = ydiv2/height;//ydiv = ydiv2/Config.AR_PATT_SIZE_Y; |
||
| 255 | |||
| 256 | /*wk_pickFromRaster_ext_pat2ワーク変数を初期化する。*/ |
||
| 257 | int[][][] ext_pat2=wk_pickFromRaster_ext_pat2;//ARUint32 ext_pat2[AR_PATT_SIZE_Y][AR_PATT_SIZE_X][3]; |
||
| 258 | int extpat_j[][],extpat_j_i[]; |
||
| 259 | int ext_pat2_j[][],ext_pat2_j_i[]; |
||
| 260 | |||
| 261 | initValue_wk_pickFromRaster_ext_pat2(ext_pat2,this.width,this.height); |
||
| 262 | |||
| 263 | xdiv2_reciprocal = 1.0 / xdiv2; |
||
| 264 | ydiv2_reciprocal = 1.0 / ydiv2; |
||
| 265 | int i,j; |
||
| 266 | int[] rgb_tmp=wk_pickFromRaster_rgb_tmp; |
||
| 267 | // arGetCode_put_zero(ext_pat2);//put_zero( (ARUint8 *)ext_pat2, AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3*sizeof(ARUint32) ); |
||
| 268 | for(j = 0; j < ydiv2; j++ ) { |
||
| 269 | yw = 102.5 + 5.0 * (j+0.5) * ydiv2_reciprocal; |
||
| 270 | for(i = 0; i < xdiv2; i++ ) { |
||
| 271 | xw = 102.5 + 5.0 * (i+0.5) * xdiv2_reciprocal; |
||
| 272 | d = para[2*3+0]*xw + para[2*3+1]*yw + para[2*3+2]; |
||
| 273 | if( d == 0 ){ |
||
| 274 | throw new NyARException(); |
||
| 275 | } |
||
| 276 | xc = (int)((para[0*3+0]*xw + para[0*3+1]*yw + para[0*3+2])/d); |
||
| 277 | yc = (int)((para[1*3+0]*xw + para[1*3+1]*yw + para[1*3+2])/d); |
||
| 278 | |||
| 279 | |||
| 280 | if( xc >= 0 && xc < img_x && yc >= 0 && yc < img_y ) { |
||
| 281 | image.getPixel(xc, yc, rgb_tmp); |
||
| 282 | ext_pat2_j_i=ext_pat2[j/ydiv][i/xdiv]; |
||
| 283 | |||
| 284 | ext_pat2_j_i[0] += rgb_tmp[0];//R |
||
| 285 | ext_pat2_j_i[1] += rgb_tmp[1];//G |
||
| 286 | ext_pat2_j_i[2] += rgb_tmp[2];//B |
||
| 287 | // System.out.println(xc+":"+yc+":"+rgb_tmp[0]+":"+rgb_tmp[1]+":"+rgb_tmp[2]); |
||
| 288 | } |
||
| 289 | } |
||
| 290 | } |
||
| 291 | // short[][][] ext_pat=new short[Config.AR_PATT_SIZE_Y][Config.AR_PATT_SIZE_X][3];//ARUint32 ext_pat2[AR_PATT_SIZE_Y][AR_PATT_SIZE_X][3]; |
||
| 292 | /*<Optimize>*/ |
||
| 293 | int xdiv_x_ydiv=xdiv*ydiv; |
||
| 294 | for(j = this.height-1; j>=0; j--){ |
||
| 295 | extpat_j=extpat[j]; |
||
| 296 | ext_pat2_j=ext_pat2[j]; |
||
| 297 | for(i = this.width-1; i>=0; i--){ // PRL 2006-06-08. |
||
| 298 | ext_pat2_j_i=ext_pat2_j[i]; |
||
| 299 | extpat_j_i=extpat_j[i]; |
||
| 300 | extpat_j_i[0]=(ext_pat2_j_i[0] / xdiv_x_ydiv);//ext_pat[j][i][0] = (byte)(ext_pat2[j][i][0] / (xdiv*ydiv)); |
||
| 301 | extpat_j_i[1]=(ext_pat2_j_i[1] / xdiv_x_ydiv);//ext_pat[j][i][1] = (byte)(ext_pat2[j][i][1] / (xdiv*ydiv)); |
||
| 302 | extpat_j_i[2]=(ext_pat2_j_i[2] / xdiv_x_ydiv);//ext_pat[j][i][2] = (byte)(ext_pat2[j][i][2] / (xdiv*ydiv)); |
||
| 303 | } |
||
| 304 | }/* |
||
| 305 | int xdiv_mul_ydiv=xdiv*ydiv; |
||
| 306 | short [][] extpat_pt_2; |
||
| 307 | short[] extpat_pt_1; |
||
| 308 | int[][] ext_pat2_pt_2; |
||
| 309 | int[] ext_pat2_pt_1; |
||
| 310 | for(int j=this.height-1; j>=0; j--){//for(int j = 0; j < this.height; j++ ){ |
||
| 311 | extpat_pt_2=extpat[j]; |
||
| 312 | ext_pat2_pt_2=ext_pat2[j]; |
||
| 313 | for(int i = this.width-1; i>=0; i--){//for(int i = 0; i < this.width; i++ ){ // PRL 2006-06-08. |
||
| 314 | extpat_pt_1=extpat_pt_2[i]; |
||
| 315 | ext_pat2_pt_1=ext_pat2_pt_2[i]; |
||
| 316 | extpat_pt_1[0]=(short)(ext_pat2_pt_1[0] / xdiv_mul_ydiv);//ext_pat[j][i][0] = (byte)(ext_pat2[j][i][0] / (xdiv*ydiv)); |
||
| 317 | extpat_pt_1[1]=(short)(ext_pat2_pt_1[1] / xdiv_mul_ydiv);//ext_pat[j][i][1] = (byte)(ext_pat2[j][i][1] / (xdiv*ydiv)); |
||
| 318 | extpat_pt_1[2]=(short)(ext_pat2_pt_1[2] / xdiv_mul_ydiv);//ext_pat[j][i][2] = (byte)(ext_pat2[j][i][2] / (xdiv*ydiv)); |
||
| 319 | } |
||
| 320 | } |
||
| 321 | /*</Optimize>*/ |
||
| 322 | return true; |
||
| 323 | } |
||
| 324 | } |