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_O2 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_O2(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 | public int[][][] getPatArray() |
||
| 73 | { |
||
| 74 | return extpat; |
||
| 75 | } |
||
| 76 | public int getWidth() |
||
| 77 | { |
||
| 78 | return width; |
||
| 79 | } |
||
| 80 | public int getHeight() |
||
| 81 | { |
||
| 82 | return height; |
||
| 83 | } |
||
| 84 | private final NyARMat wk_get_cpara_a=new NyARMat(8,8); |
||
| 85 | private final NyARMat wk_get_cpara_b=new NyARMat(8,1); |
||
| 86 | // private final NyARMat wk_get_cpara_c=new NyARMat(8,1); |
||
| 87 | /** |
||
| 88 | * @param world |
||
| 89 | * @param vertex |
||
| 90 | * @param o_para |
||
| 91 | * @throws NyARException |
||
| 92 | */ |
||
| 93 | private boolean get_cpara(double vertex_0[], double vertex_1[],NyARMat o_para) throws NyARException |
||
| 94 | { |
||
| 95 | double world[][]=this.wk_pickFromRaster_world; |
||
| 96 | NyARMat a =wk_get_cpara_a;//次処理で値を設定するので、初期化不要// new NyARMat( 8, 8 ); |
||
| 97 | double[][] a_array=a.getArray(); |
||
| 98 | NyARMat b =wk_get_cpara_b;//次処理で値を設定するので、初期化不要// new NyARMat( 8, 1 ); |
||
| 99 | double[][] b_array=b.getArray(); |
||
| 100 | double[] a_pt0,a_pt1,world_pti; |
||
| 101 | |||
| 102 | for(int i = 0; i < 4; i++ ) { |
||
| 103 | a_pt0=a_array[i*2]; |
||
| 104 | a_pt1=a_array[i*2+1]; |
||
| 105 | world_pti=world[i]; |
||
| 106 | |||
| 107 | a_pt0[0]=world_pti[0];//a->m[i*16+0] = world[i][0]; |
||
| 108 | a_pt0[1]=world_pti[1];//a->m[i*16+1] = world[i][1]; |
||
| 109 | a_pt0[2]=1.0;//a->m[i*16+2] = 1.0; |
||
| 110 | a_pt0[3]=0.0;//a->m[i*16+3] = 0.0; |
||
| 111 | a_pt0[4]=0.0;//a->m[i*16+4] = 0.0; |
||
| 112 | a_pt0[5]=0.0;//a->m[i*16+5] = 0.0; |
||
| 113 | a_pt0[6]=-world_pti[0] * vertex_0[i];//a->m[i*16+6] = -world[i][0] * vertex[i][0]; |
||
| 114 | a_pt0[7]=-world_pti[1] * vertex_0[i];//a->m[i*16+7] = -world[i][1] * vertex[i][0]; |
||
| 115 | a_pt1[0]=0.0;//a->m[i*16+8] = 0.0; |
||
| 116 | a_pt1[1]=0.0;//a->m[i*16+9] = 0.0; |
||
| 117 | a_pt1[2]=0.0;//a->m[i*16+10] = 0.0; |
||
| 118 | a_pt1[3]=world_pti[0];//a->m[i*16+11] = world[i][0]; |
||
| 119 | a_pt1[4]=world_pti[1];//a->m[i*16+12] = world[i][1]; |
||
| 120 | a_pt1[5]=1.0;//a->m[i*16+13] = 1.0; |
||
| 121 | a_pt1[6]=-world_pti[0] * vertex_1[i];//a->m[i*16+14] = -world[i][0] * vertex[i][1]; |
||
| 122 | a_pt1[7]=-world_pti[1] * vertex_1[i];//a->m[i*16+15] = -world[i][1] * vertex[i][1]; |
||
| 123 | b_array[i*2+0][0]=vertex_0[i];//b->m[i*2+0] = vertex[i][0]; |
||
| 124 | b_array[i*2+1][0]=vertex_1[i];//b->m[i*2+1] = vertex[i][1]; |
||
| 125 | } |
||
| 126 | // JartkException.trap("未チェックのパス"); |
||
| 127 | if(!a.matrixSelfInv()){ |
||
| 128 | return false; |
||
| 129 | } |
||
| 130 | |||
| 131 | // JartkException.trap("未チェックのパス"); |
||
| 132 | // NyARMat c = wk_get_cpara_c;//次処理で結果を受け取るので、初期化不要//new NyARMat( 8, 1 ); |
||
| 133 | // double[][] c_array=c.getArray(); |
||
| 134 | |||
| 135 | o_para.matrixMul(a, b); |
||
| 136 | // para[0*3+0] = c_array[0*3+0][0];//para[i][0] = c->m[i*3+0]; |
||
| 137 | // para[0*3+1] = c_array[0*3+1][0];//para[i][1] = c->m[i*3+1]; |
||
| 138 | // para[0*3+2] = c_array[0*3+2][0];//para[i][2] = c->m[i*3+2]; |
||
| 139 | // para[1*3+0] = c_array[1*3+0][0];//para[i][0] = c->m[i*3+0]; |
||
| 140 | // para[1*3+1] = c_array[1*3+1][0];//para[i][1] = c->m[i*3+1]; |
||
| 141 | // para[i*3+2] = c_array[1*3+2][0];//para[i][2] = c->m[i*3+2]; |
||
| 142 | // para[2*3+0] = c_array[2*3+0][0];//para[2][0] = c->m[2*3+0]; |
||
| 143 | // para[2*3+1] = c_array[2*3+1][0];//para[2][1] = c->m[2*3+1]; |
||
| 144 | // para[2*3+2] = 1.0;//para[2][2] = 1.0; |
||
| 145 | return true; |
||
| 146 | } |
||
| 147 | |||
| 148 | // private final double[] wk_pickFromRaster_para=new double[9];//[3][3]; |
||
| 149 | private int[][][] wk_pickFromRaster_ext_pat2=null;//コンストラクタでint[height][width][3]を作る |
||
| 150 | private final double[][] wk_pickFromRaster_world={//double world[4][2]; |
||
| 151 | {100.0, 100.0}, |
||
| 152 | {100.0+10.0,100.0}, |
||
| 153 | {100.0+10.0,100.0 + 10.0}, |
||
| 154 | {100.0, 100.0 + 10.0} |
||
| 155 | }; |
||
| 156 | /** |
||
| 157 | * pickFromRaster関数から使う変数です。 |
||
| 158 | * |
||
| 159 | */ |
||
| 160 | private static void initValue_wk_pickFromRaster_ext_pat2(int[][][] i_ext_pat2,int i_width,int i_height) |
||
| 161 | { |
||
| 162 | int i,i2; |
||
| 163 | int[][] pt2; |
||
| 164 | int[] pt1; |
||
| 165 | for(i=i_height-1;i>=0;i--){ |
||
| 166 | pt2=i_ext_pat2[i]; |
||
| 167 | for(i2=i_width-1;i2>=0;i2--){ |
||
| 168 | pt1=pt2[i2]; |
||
| 169 | pt1[0]=0; |
||
| 170 | pt1[1]=0; |
||
| 171 | pt1[2]=0; |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 | private final double[][] wk_pickFromRaster_local=new double[2][4]; |
||
| 176 | private final int[] wk_pickFromRaster_rgb_tmp=new int[3]; |
||
| 177 | private final NyARMat wk_pickFromRaster_cpara=new NyARMat(8,1); |
||
| 178 | /** |
||
| 179 | * imageから、i_markerの位置にあるパターンを切り出して、保持します。 |
||
| 180 | * Optimize:STEP[769->750] |
||
| 181 | * @param image |
||
| 182 | * @param i_marker |
||
| 183 | * @throws Exception |
||
| 184 | */ |
||
| 185 | public boolean pickFromRaster(NyARRaster image, NyARMarker i_marker) throws NyARException |
||
| 186 | { |
||
| 187 | NyARMat cpara=this.wk_pickFromRaster_cpara; |
||
| 188 | //localの計算 |
||
| 189 | int[] x_coord=i_marker.x_coord; |
||
| 190 | int[] y_coord=i_marker.y_coord; |
||
| 191 | int[] vertex=i_marker.mkvertex; |
||
| 192 | double[] local_0=wk_pickFromRaster_local[0];//double local[4][2]; |
||
| 193 | double[] local_1=wk_pickFromRaster_local[1];//double local[4][2]; |
||
| 194 | for(int i = 0; i < 4; i++ ) { |
||
| 195 | local_0[i] = x_coord[vertex[i]]; |
||
| 196 | local_1[i] = y_coord[vertex[i]]; |
||
| 197 | } |
||
| 198 | //xdiv2,ydiv2の計算 |
||
| 199 | int xdiv2, ydiv2; |
||
| 200 | int l1,l2; |
||
| 201 | double w1,w2; |
||
| 202 | |||
| 203 | //x計算 |
||
| 204 | w1=local_0[0] - local_0[1]; |
||
| 205 | w2=local_1[0] - local_1[1]; |
||
| 206 | l1 = (int)(w1*w1+w2*w2); |
||
| 207 | w1=local_0[2] - local_0[3]; |
||
| 208 | w2=local_1[2] - local_1[3]; |
||
| 209 | l2 = (int)(w1*w1+w2*w2); |
||
| 210 | if( l2 > l1 ){ |
||
| 211 | l1 = l2; |
||
| 212 | } |
||
| 213 | l1=l1/4; |
||
| 214 | xdiv2 =this.width; |
||
| 215 | while( xdiv2*xdiv2 < l1 ){ |
||
| 216 | xdiv2*=2; |
||
| 217 | } |
||
| 218 | if( xdiv2 > AR_PATT_SAMPLE_NUM) |
||
| 219 | { |
||
| 220 | xdiv2 =AR_PATT_SAMPLE_NUM; |
||
| 221 | } |
||
| 222 | |||
| 223 | //y計算 |
||
| 224 | w1=local_0[1] - local_0[2]; |
||
| 225 | w2=local_1[1] - local_1[2]; |
||
| 226 | l1 = (int)(w1*w1+ w2*w2); |
||
| 227 | w1=local_0[3] - local_0[0]; |
||
| 228 | w2=local_1[3] - local_1[0]; |
||
| 229 | l2 = (int)(w1*w1+ w2*w2); |
||
| 230 | if( l2 > l1 ){ |
||
| 231 | l1 = l2; |
||
| 232 | } |
||
| 233 | ydiv2 =this.height; |
||
| 234 | l1=l1/4; |
||
| 235 | while( ydiv2*ydiv2 < l1 ){ |
||
| 236 | ydiv2*=2; |
||
| 237 | } |
||
| 238 | if( ydiv2 >AR_PATT_SAMPLE_NUM) |
||
| 239 | { |
||
| 240 | ydiv2 = AR_PATT_SAMPLE_NUM; |
||
| 241 | } |
||
| 242 | |||
| 243 | //cparaの計算 |
||
| 244 | if(!get_cpara(local_0,local_1,cpara)){ |
||
| 245 | return false; |
||
| 246 | } |
||
| 247 | |||
| 248 | int img_x=image.getWidth(); |
||
| 249 | int img_y=image.getHeight(); |
||
| 250 | |||
| 251 | /*wk_pickFromRaster_ext_pat2ワーク変数を初期化する。*/ |
||
| 252 | int[][][] ext_pat2=wk_pickFromRaster_ext_pat2;//ARUint32 ext_pat2[AR_PATT_SIZE_Y][AR_PATT_SIZE_X][3]; |
||
| 253 | int extpat_j[][],extpat_j_i[]; |
||
| 254 | int ext_pat2_j[][],ext_pat2_j_i[]; |
||
| 255 | |||
| 256 | initValue_wk_pickFromRaster_ext_pat2(ext_pat2,this.width,this.height); |
||
| 257 | |||
| 258 | double[][] cpara_array=cpara.getArray(); |
||
| 259 | double para21_x_yw,para01_x_yw,para11_x_yw; |
||
| 260 | double para00,para01,para02,para10,para11,para12,para20,para21; |
||
| 261 | para00 = cpara_array[0*3+0][0];//para[i][0] = c->m[i*3+0]; |
||
| 262 | para01 = cpara_array[0*3+1][0];//para[i][1] = c->m[i*3+1]; |
||
| 263 | para02 = cpara_array[0*3+2][0];//para[i][2] = c->m[i*3+2]; |
||
| 264 | para10 = cpara_array[1*3+0][0];//para[i][0] = c->m[i*3+0]; |
||
| 265 | para11 = cpara_array[1*3+1][0];//para[i][1] = c->m[i*3+1]; |
||
| 266 | para12 = cpara_array[1*3+2][0];//para[i][2] = c->m[i*3+2]; |
||
| 267 | para20 = cpara_array[2*3+0][0];//para[2][0] = c->m[2*3+0]; |
||
| 268 | para21 = cpara_array[2*3+1][0];//para[2][1] = c->m[2*3+1]; |
||
| 269 | //para22 = 1.0;//para[2][2] = 1.0; |
||
| 270 | |||
| 271 | |||
| 272 | double d, xw, yw; |
||
| 273 | int xc, yc; |
||
| 274 | int i,j; |
||
| 275 | int[] rgb_tmp=wk_pickFromRaster_rgb_tmp; |
||
| 276 | // arGetCode_put_zero(ext_pat2);//put_zero( (ARUint8 *)ext_pat2, AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3*sizeof(ARUint32) ); |
||
| 277 | int xdiv = xdiv2/width;//xdiv = xdiv2/Config.AR_PATT_SIZE_X; |
||
| 278 | int ydiv = ydiv2/height;//ydiv = ydiv2/Config.AR_PATT_SIZE_Y; |
||
| 279 | double xdiv2_reciprocal = 1.0 / xdiv2; |
||
| 280 | double ydiv2_reciprocal = 1.0 / ydiv2; |
||
| 281 | |||
| 282 | for(j = 0; j < ydiv2; j++ ) { |
||
| 283 | yw = 102.5 + 5.0 * ((double)j+0.5) * ydiv2_reciprocal; |
||
| 284 | para21_x_yw=para21*yw+1.0; |
||
| 285 | para11_x_yw=para11*yw+para12; |
||
| 286 | para01_x_yw=para01*yw+para02; |
||
| 287 | ext_pat2_j=ext_pat2[j/ydiv]; |
||
| 288 | for(i = 0; i < xdiv2; i++ ) { |
||
| 289 | xw = 102.5 + 5.0 * ((double)i+0.5) * xdiv2_reciprocal; |
||
| 290 | d = para20*xw + para21_x_yw; |
||
| 291 | if( d == 0 ){ |
||
| 292 | throw new NyARException(); |
||
| 293 | } |
||
| 294 | xc = (int)((para00*xw + para01_x_yw)/d); |
||
| 295 | yc = (int)((para10*xw + para11_x_yw)/d); |
||
| 296 | |||
| 297 | |||
| 298 | if( xc >= 0 && xc < img_x && yc >= 0 && yc < img_y ) { |
||
| 299 | image.getPixel(xc, yc, rgb_tmp); |
||
| 300 | ext_pat2_j_i=ext_pat2_j[i/xdiv]; |
||
| 301 | |||
| 302 | ext_pat2_j_i[0] += rgb_tmp[0];//R |
||
| 303 | ext_pat2_j_i[1] += rgb_tmp[1];//G |
||
| 304 | ext_pat2_j_i[2] += rgb_tmp[2];//B |
||
| 305 | } |
||
| 306 | } |
||
| 307 | } |
||
| 308 | /*<Optimize>*/ |
||
| 309 | int xdiv_x_ydiv=xdiv*ydiv; |
||
| 310 | for(j =this.height-1; j>=0; j--){ |
||
| 311 | extpat_j=extpat[j]; |
||
| 312 | ext_pat2_j=ext_pat2[j]; |
||
| 313 | for(i = this.width-1; i>=0; i--){ // PRL 2006-06-08. |
||
| 314 | ext_pat2_j_i=ext_pat2_j[i]; |
||
| 315 | extpat_j_i=extpat_j[i]; |
||
| 316 | 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)); |
||
| 317 | 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)); |
||
| 318 | 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)); |
||
| 319 | } |
||
| 320 | } |
||
| 321 | return true; |
||
| 322 | } |
||
| 323 | } |