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
32 package jp.nyatla.nyartoolkit.core;
\r
38 import jp.nyatla.nyartoolkit.NyARException;
\r
39 import jp.nyatla.util.DoubleValue;
\r
44 double dist_factor[4];
\r
46 public class NyARParam{
\r
47 private static final int SIZE_OF_PARAM_SET=4+4+(3*4*8)+(4*8);
\r
48 private static final int PD_LOOP = 3;
\r
49 protected int xsize, ysize;
\r
50 private double[] array34=new double[3*4];//Double2dArray mat=new Double2dArray(3,4);
\r
51 private double[] dist_factor=new double[4];
\r
60 public double[] getDistFactor()
\r
65 * パラメタを格納した[4x3]配列を返します。
\r
68 public final double[] get34Array()
\r
73 * ARToolKit標準ファイルから1個目の設定をロードする。
\r
75 * @throws NyARException
\r
77 public void loadFromARFile(String i_filename) throws NyARException
\r
80 loadFromARFile(new FileInputStream(i_filename));
\r
81 } catch (Exception e) {
\r
82 throw new NyARException(e);
\r
85 public void loadFromARFile(InputStream i_stream) throws NyARException
\r
88 NyARParam new_inst[] = arParamLoad(i_stream, 1);
\r
90 xsize = new_inst[0].xsize;
\r
91 ysize = new_inst[0].ysize;
\r
92 array34 = new_inst[0].array34;
\r
93 dist_factor = new_inst[0].dist_factor;
\r
94 } catch (Exception e) {
\r
95 throw new NyARException(e);
\r
98 /*static double dot( double a1, double a2, double a3,double b1, double b2, double b3 )*/
\r
99 private final static double dot( double a1, double a2, double a3,double b1, double b2, double b3 )
\r
101 return( a1 * b1 + a2 * b2 + a3 * b3 );
\r
103 /* static double norm( double a, double b, double c )*/
\r
104 private final static double norm( double a, double b, double c )
\r
106 return Math.sqrt( a*a + b*b + c*c );
\r
109 * int arParamDecompMat( double source[3][4], double cpara[3][4], double trans[3][4] );
\r
111 * Optimize STEP[754->665]
\r
113 * 戻り引数。3x4のマトリクスを指定すること。
\r
115 * 戻り引数。3x4のマトリクスを指定すること。
\r
118 public void decompMat(NyARMat o_cpara, NyARMat o_trans)
\r
120 double[] source=array34;
\r
121 double[] Cpara=new double[3*4];//double Cpara[3][4];
\r
122 double rem1, rem2, rem3;
\r
124 if(source[2*4+3]>= 0 ){//if( source[2][3] >= 0 ) {
\r
126 //for(int r = 0; r < 3; r++ ){
\r
127 // for(int c = 0; c < 4; c++ ){
\r
128 // Cpara[r][c]=source[r][c];//Cpara[r][c] = source[r][c];
\r
132 Cpara[i]=source[i];//Cpara[r][c] = source[r][c];
\r
137 //for(int r = 0; r < 3; r++ ){
\r
138 // for(int c = 0; c < 4; c++ ){
\r
139 // Cpara[r][c]=-source[r][c];//Cpara[r][c] = -(source[r][c]);
\r
143 Cpara[i]=source[i];//Cpara[r][c] = source[r][c];
\r
148 double[][] cpara=o_cpara.getArray();
\r
149 double[][] trans=o_trans.getArray();
\r
150 for(int r = 0; r < 3; r++ ){
\r
151 for(int c = 0; c < 4; c++ ){
\r
152 cpara[r][c]=0.0;//cpara[r][c] = 0.0;
\r
155 cpara[2][2]=norm(Cpara[2*4+0],Cpara[2*4+1], Cpara[2*4+2]);//cpara[2][2] = norm( Cpara[2][0], Cpara[2][1], Cpara[2][2] );
\r
156 trans[2][0]=Cpara[2*4+0] / cpara[2][2];//trans[2][0] = Cpara[2][0] / cpara[2][2];
\r
157 trans[2][1]=Cpara[2*4+1]/ cpara[2][2];//trans[2][1] = Cpara[2][1] / cpara[2][2];
\r
158 trans[2][2]=Cpara[2*4+2]/ cpara[2][2];//trans[2][2] = Cpara[2][2] / cpara[2][2];
\r
159 trans[2][3]=Cpara[2*4+3] / cpara[2][2];//trans[2][3] = Cpara[2][3] / cpara[2][2];
\r
161 cpara[1][2]=dot(trans[2][0], trans[2][1], trans[2][2],Cpara[1*4+0], Cpara[1*4+1], Cpara[1*4+2]);//cpara[1][2] = dot( trans[2][0], trans[2][1], trans[2][2],Cpara[1][0], Cpara[1][1], Cpara[1][2] );
\r
162 rem1=Cpara[1*4+0]- cpara[1][2] * trans[2][0];//rem1 = Cpara[1][0] - cpara[1][2] * trans[2][0];
\r
163 rem2=Cpara[1*4+1] - cpara[1][2] * trans[2][1];//rem2 = Cpara[1][1] - cpara[1][2] * trans[2][1];
\r
164 rem3=Cpara[1*4+2] - cpara[1][2] * trans[2][2];//rem3 = Cpara[1][2] - cpara[1][2] * trans[2][2];
\r
165 cpara[1][1]=norm(rem1, rem2, rem3 );//cpara[1][1] = norm( rem1, rem2, rem3 );
\r
166 trans[1][0]= rem1/cpara[1][1];//trans[1][0] = rem1 / cpara[1][1];
\r
167 trans[1][1]=rem2/cpara[1][1];//trans[1][1] = rem2 / cpara[1][1];
\r
168 trans[1][2]=rem3 / cpara[1][1];//trans[1][2] = rem3 / cpara[1][1];
\r
170 cpara[0][2]=dot(trans[2][0],trans[2][1], trans[2][2],Cpara[0*4+0], Cpara[0*4+1], Cpara[0*4+2] );//cpara[0][2] = dot( trans[2][0], trans[2][1], trans[2][2],Cpara[0][0], Cpara[0][1], Cpara[0][2] );
\r
171 cpara[0][1]=dot(trans[1][0],trans[1][1], trans[1][2],Cpara[0*4+0], Cpara[0*4+1], Cpara[0*4+2]);//cpara[0][1] = dot( trans[1][0], trans[1][1], trans[1][2],Cpara[0][0], Cpara[0][1], Cpara[0][2] );
\r
172 rem1=Cpara[0*4+0]- cpara[0][1]*trans[1][0] - cpara[0][2]*trans[2][0];//rem1 = Cpara[0][0] - cpara[0][1]*trans[1][0] - cpara[0][2]*trans[2][0];
\r
173 rem2 = Cpara[0*4+1] - cpara[0][1]*trans[1][1] - cpara[0][2]*trans[2][1];//rem2 = Cpara[0][1] - cpara[0][1]*trans[1][1] - cpara[0][2]*trans[2][1];
\r
174 rem3 = Cpara[0*4+2] - cpara[0][1]*trans[1][2] - cpara[0][2]*trans[2][2];//rem3 = Cpara[0][2] - cpara[0][1]*trans[1][2] - cpara[0][2]*trans[2][2];
\r
175 cpara[0][0]=norm(rem1, rem2, rem3);//cpara[0][0] = norm( rem1, rem2, rem3 );
\r
176 trans[0][0]=rem1 / cpara[0][0];//trans[0][0] = rem1 / cpara[0][0];
\r
177 trans[0][1]= rem2 / cpara[0][0];//trans[0][1] = rem2 / cpara[0][0];
\r
178 trans[0][2]= rem3 / cpara[0][0];//trans[0][2] = rem3 / cpara[0][0];
\r
180 trans[1][3]=(Cpara[1*4+3] - cpara[1][2]*trans[2][3]) / cpara[1][1];//trans[1][3] = (Cpara[1][3] - cpara[1][2]*trans[2][3]) / cpara[1][1];
\r
181 trans[0][3]=(Cpara[0*4+3] - cpara[0][1]*trans[1][3]- cpara[0][2]*trans[2][3]) / cpara[0][0];//trans[0][3] = (Cpara[0][3] - cpara[0][1]*trans[1][3]- cpara[0][2]*trans[2][3]) / cpara[0][0];
\r
183 for(int r = 0; r < 3; r++ ){
\r
184 for(int c = 0; c < 3; c++ ){
\r
185 cpara[r][c]/=cpara[2][2];//cpara[r][c] /= cpara[2][2];
\r
191 /*int arParamDisp( ARParam *param );*/
\r
192 public int paramDisp()
\r
194 System.out.println("--------------------------------------");//printf("--------------------------------------\n");
\r
195 System.out.print("SIZE = "+xsize+", "+ysize);//printf("SIZE = %d, %d\n", param->xsize, param->ysize);
\r
196 System.out.println("Distortion factor = "+dist_factor[0]+" "+dist_factor[1]+" "+dist_factor[2]+" "+dist_factor[3]);//printf("Distortion factor = %f %f %f %f\n", param->dist_factor[0],param->dist_factor[1], param->dist_factor[2], param->dist_factor[3] );
\r
197 for(int j = 0; j < 3; j++ ) {//for(j = 0; j < 3; j++ ) {
\r
198 for(int i = 0; i < 4; i++ ){
\r
199 System.out.print(array34[j*4+i]+" ");//printf("%7.5f ", param->mat[j][i]);
\r
201 System.out.println();// printf("\n");
\r
203 System.out.println("--------------------------------------");//printf("--------------------------------------\n");
\r
206 // /*int arParamDecomp( ARParam *source, ARParam *icpara, double trans[3][4] );*/
\r
207 // private static int arParamDecomp( NyARParam source, NyARParam icpara, double[][] trans)
\r
209 // icpara.xsize = source.xsize;//icpara->xsize = source->xsize;
\r
210 // icpara.ysize = source.ysize;//icpara->ysize = source->ysize;
\r
211 // icpara.dist_factor[0] = source.dist_factor[0];//icpara->dist_factor[0] = source->dist_factor[0];
\r
212 // icpara.dist_factor[1] = source.dist_factor[1];// icpara->dist_factor[1] = source->dist_factor[1];
\r
213 // icpara.dist_factor[2] = source.dist_factor[2];//icpara->dist_factor[2] = source->dist_factor[2];
\r
214 // icpara.dist_factor[3] = source.dist_factor[3];//icpara->dist_factor[3] = source->dist_factor[3];
\r
215 // return arParamDecompMat(source.mat, icpara.mat, trans );
\r
218 * int arParamChangeSize( ARParam *source, int xsize, int ysize, ARParam *newparam );
\r
220 * サイズプロパティをi_xsize,i_ysizeに変更します。
\r
227 public void changeSize(int i_xsize, int i_ysize)
\r
230 scale = (double)i_xsize / (double)(xsize);//scale = (double)xsize / (double)(source->xsize);
\r
232 for(int i = 0; i < 4; i++ ) {
\r
233 array34[0*4+i]=array34[0*4+i]*scale;//newparam->mat[0][i] = source->mat[0][i] * scale;
\r
234 array34[1*4+i]=array34[1*4+i]*scale;//newparam->mat[1][i] = source->mat[1][i] * scale;
\r
235 array34[2*4+i]=array34[2*4+i];//newparam->mat[2][i] = source->mat[2][i];
\r
238 dist_factor[0] = dist_factor[0] * scale;//newparam->dist_factor[0] = source->dist_factor[0] * scale;
\r
239 dist_factor[1] = dist_factor[1] * scale;//newparam->dist_factor[1] = source->dist_factor[1] * scale;
\r
240 dist_factor[2] = dist_factor[2] / (scale*scale);//newparam->dist_factor[2] = source->dist_factor[2] / (scale*scale);
\r
241 dist_factor[3] = dist_factor[3];//newparam->dist_factor[3] = source->dist_factor[3];
\r
243 xsize = i_xsize;//newparam->xsize = xsize;
\r
244 ysize = i_ysize;//newparam->ysize = ysize;
\r
247 * int arParamIdeal2Observ( const double dist_factor[4], const double ix, const double iy,double *ox, double *oy )
\r
254 public void ideal2Observ(double ix,double iy,DoubleValue ox, DoubleValue oy)
\r
258 final double d0,d1,d3;
\r
259 final double df[]=this.dist_factor;
\r
263 x = (ix - d0) * d3;
\r
264 y = (iy - d1) * d3;
\r
265 if( x == 0.0 && y == 0.0 ) {
\r
269 d = 1.0 - df[2]/100000000.0 * (x*x+y*y);
\r
270 ox.value=x * d + d0;
\r
271 oy.value=y * d + d1;
\r
275 * ideal2Observをまとめて実行します。
\r
281 public void ideal2ObservBatch(double[][] i_in,double[][] o_out,int i_size)
\r
285 final double d0,d1,d3,d2_w;
\r
286 final double df[]=this.dist_factor;
\r
290 d2_w=df[2]/100000000.0;
\r
291 for(int i=0;i<i_size;i++){
\r
292 x = (i_in[i][0] - d0) * d3;
\r
293 y = (i_in[i][1] - d1) * d3;
\r
294 if( x == 0.0 && y == 0.0 ) {
\r
298 d = 1.0 - d2_w * (x*x+y*y);
\r
299 o_out[i][0]=x * d + d0;
\r
300 o_out[i][1]=y * d + d1;
\r
310 * int arParamObserv2Ideal( const double dist_factor[4], const double ox, const double oy,double *ix, double *iy );
\r
318 public int observ2Ideal(double ox,double oy,DoubleValue ix,DoubleValue iy)
\r
320 double z02, z0, p, q, z, px, py,opttmp_1;
\r
321 final double d0,d1,d3;
\r
322 final double df[]=this.dist_factor;
\r
328 p = df[2]/100000000.0;
\r
330 q = z0 = Math.sqrt(z02);//Optimize//q = z0 = Math.sqrt(px*px+ py*py);
\r
332 for(int i = 1; ; i++ ) {
\r
334 //Optimize opttmp_1
\r
336 z = z0 - ((1.0 - opttmp_1)*z0 - q) / (1.0 - 3.0*opttmp_1);
\r
344 if( i == PD_LOOP ){
\r
347 z02 = px*px+ py*py;
\r
348 z0 = Math.sqrt(z02);//Optimize//z0 = Math.sqrt(px*px+ py*py);
\r
351 ix.value=px / d3 + d0;
\r
352 iy.value=py / d3 + d1;
\r
356 * 指定範囲のobserv2Idealをまとめて実行して、結果をo_idealに格納します。
\r
364 * 出力バッファ[i_num][2]であること。
\r
366 public void observ2IdealBatch(int[] i_x_coord,int[] i_y_coord,int i_start,int i_num,double[][] o_ideal)
\r
368 double z02, z0,q, z, px, py,opttmp_1;
\r
369 final double df[]=this.dist_factor;
\r
370 final double d0=df[0];
\r
371 final double d1=df[1];
\r
372 final double d3=df[3];
\r
373 final double p = df[2]/100000000.0;
\r
374 for(int j = 0; j < i_num; j++ ){
\r
376 px = i_x_coord[i_start+j] - d0;
\r
377 py = i_y_coord[i_start+j] - d1;
\r
380 q = z0 = Math.sqrt(z02);//Optimize//q = z0 = Math.sqrt(px*px+ py*py);
\r
382 for(int i = 1; ; i++ ) {
\r
384 //Optimize opttmp_1
\r
386 z = z0 - ((1.0 - opttmp_1)*z0 - q) / (1.0 - 3.0*opttmp_1);
\r
394 if( i == PD_LOOP ){
\r
397 z02 = px*px+ py*py;
\r
398 z0 = Math.sqrt(z02);//Optimize//z0 = Math.sqrt(px*px+ py*py);
\r
400 o_ideal[j][0]=px / d3 + d0;
\r
401 o_ideal[j][1]=py / d3 + d1;
\r
409 * int arParamLoad( const char *filename, int num, ARParam *param, ...);
\r
410 * i_streamの入力ストリームからi_num個の設定を読み込み、パラメタを配列にして返します。
\r
416 * @throws Exception
\r
417 * i_num個の設定が読み出せない場合、JartkExceptionを発生します。
\r
419 private static NyARParam[] arParamLoad(InputStream i_stream, int i_num) throws NyARException
\r
422 int read_size=SIZE_OF_PARAM_SET*i_num;
\r
423 byte[] buf=new byte[read_size];
\r
424 i_stream.read(buf);
\r
426 NyARParam[] result=new NyARParam[i_num];
\r
429 ByteBuffer bb = ByteBuffer.wrap(buf);
\r
430 bb.order(ByteOrder.BIG_ENDIAN);
\r
433 for(int i=0;i<i_num;i++){
\r
434 NyARParam new_param=new NyARParam();;
\r
435 new_param.xsize=bb.getInt();
\r
436 new_param.ysize=bb.getInt();
\r
437 for(int i2=0;i2<3;i2++){
\r
438 for(int i3=0;i3<4;i3++){
\r
439 new_param.array34[i2*4+i3]=bb.getDouble();
\r
442 for(int i2=0;i2<4;i2++){
\r
443 new_param.dist_factor[i2]=bb.getDouble();
\r
445 result[i]=new_param;
\r
448 }catch(Exception e){
\r
449 throw new NyARException(e);
\r
452 public static int arParamSave(String filename,int num, NyARParam param[]) throws Exception
\r
454 NyARException.trap("未チェックの関数");
\r
455 byte buf[]=new byte[SIZE_OF_PARAM_SET*param.length];
\r
457 ByteBuffer bb = ByteBuffer.wrap(buf);
\r
458 bb.order(ByteOrder.BIG_ENDIAN);
\r
461 for(int i=0;i<param.length;i++){
\r
462 bb.putInt(param[i].xsize);
\r
463 bb.putInt(param[i].ysize);
\r
464 for(int i2=0;i2<3;i2++){
\r
465 for(int i3=0;i3<4;i3++){
\r
466 bb.putDouble(param[i].array34[i2*4+i3]);
\r
469 for(int i2=0;i2<4;i2++){
\r
470 bb.putDouble(param[i].dist_factor[i2]);
\r
474 FileOutputStream fs=new FileOutputStream(filename);
\r