int main (int argc,char* argv[]) { int i, j, ip, n1, n2, np, ndim, order, n123, *pp, n[2], box[2], **shift, b; float o1, o2, d1, d2, slow, *dd, **pts, *vv, *h, *bin, *vor, d[2], **rect; bool isvel, dist, voro; sf_upgrad upg; sf_file coord, ord, grid, vel; sf_init (argc, argv); ord = sf_input("in"); coord = sf_input("coord"); grid = sf_output("out"); if (NULL != sf_getstring("velocity")) { vel = sf_input("velocity"); if(!sf_histint(vel,"n1",&n1)) sf_error("No n1= in vel"); if(!sf_histint(vel,"n2",&n2)) sf_error("No n2= in vel"); /* dimensions */ if(!sf_histfloat(vel,"d1",&d1)) sf_error("No d1= in vel"); if(!sf_histfloat(vel,"d2",&d2)) sf_error("No d2= in vel"); /* sampling */ if(!sf_histfloat(vel,"o1",&o1)) o1=0.; if(!sf_histfloat(vel,"o2",&o2)) o2=0.; /* origin */ } else { vel = NULL; if(!sf_getint("n1",&n1)) sf_error("Need n1="); if(!sf_getint("n2",&n2)) sf_error("Need n2="); /* dimensions */ if(!sf_getfloat("d1",&d1)) sf_error("Need d1="); if(!sf_getfloat("d2",&d2)) sf_error("Need d2="); /* sampling */ if(!sf_getfloat("o1",&o1)) o1=0.; if(!sf_getfloat("o2",&o2)) o2=0.; /* origin */ } sf_putint(grid,"n1",n1); sf_putint(grid,"n2",n2); sf_putfloat(grid,"d1",d1); sf_putfloat(grid,"d2",d2); sf_putfloat(grid,"o1",o1); sf_putfloat(grid,"o2",o2); n[0]=n1; d[0]=d1; n[1]=n2; d[1]=d2; if(!sf_getint("order",&order)) order=2; /* [1,2] Accuracy order for distance calculation */ if(!sf_getbool("vel",&isvel)) isvel=true; /* if y, the input is velocity; n, slowness squared */ if (SF_FLOAT != sf_gettype(coord)) sf_error("Need float input"); if(!sf_histint(coord,"n2",&np)) sf_error("No n2= in input"); if(!sf_histint(coord,"n1",&ndim) || ndim > 3) sf_error("Need n1 <= 3 in input"); pts = sf_floatalloc2 (3,np); for (ip=0; ip < np; ip++) { sf_floatread(pts[ip],ndim,coord); pts[ip][2] = 0.0f; } n123 = n1*n2; dd = sf_floatalloc (n123); vv = sf_floatalloc (n123); pp = sf_intalloc (n123); if (NULL != vel) { sf_floatread(vv,n123,vel); sf_fileclose(vel); /* transform velocity to slowness squared */ if (isvel) { for(i = 0; i < n123; i++) { slow = vv[i]; vv[i] = 1./(slow*slow); } } } else { for(i = 0; i < n123; i++) { vv[i] = 1.; } } /* 1. find distance */ distance_init (1,n2,n1,np); distance(np,pts,dd,vv,pp, 1,n2,n1, 0.,o2,o1, 1.,d2,d1, order); if (!sf_getbool("dist",&dist)) dist=false; /* if output distance */ if (dist) { sf_floatwrite(dd,n123,grid); exit(0); } /* 2. binning */ sf_int2_init (pts, o1,o2,d1,d2,n1,n2, sf_bin_int, 1, np); h = sf_floatalloc(np); for (ip=0; ip<np; ip++) { h[ip]=1.0f; } sf_int2_lop (true,false,n123,np,vv,h); if (SF_FLOAT != sf_gettype(ord)) sf_error("Need float input"); sf_floatread(h,np,ord); bin = sf_floatalloc(n123); sf_int2_lop (true,false,n123,np,bin,h); for (i=0; i < n123; i++) { /* normalize by the fold */ if (vv[i] > FLT_EPSILON) bin[i] /=vv[i]; vv[i]=0.0f; } /* 3. voronoi interpolation */ vor = sf_floatalloc(n123); upg = sf_upgrad_init(2,n,d); sf_upgrad_set(upg,dd); sf_upgrad_solve(upg,vv,vor,bin); if (!sf_getbool("voro",&voro)) voro=false; /* if output Voronoi diagram */ if (voro) { sf_floatwrite(vor,n123,grid); exit(0); } /* 4. smoothing */ rect = sf_floatalloc2(n123,2); shift = sf_intalloc2(n123,2); for (j=0; j < 2; j++) { box[j] = 1; for (i=0; i < n123; i++) { rect[j][i] = 1.0f+dd[i]/d[j]; b = ceilf(rect[j][i]); if (b > box[j]) box[j] = b; shift[j][i] = 0; } } ntrianglen_init(2,box,n,rect,shift,1); ntrianglen_lop(false,false,n123,n123,vor,bin); sf_floatwrite(bin,n123,grid); exit (0); }
int main (int argc, char* argv[]) { int *sft[SF_MAX_DIM]; int box[SF_MAX_DIM], n[SF_MAX_DIM]; int dim, dim1, i, n1, n2, i1, i2, b, nrep; float *data, *smoo, *rct[SF_MAX_DIM]; char key[8]; sf_file in, out, rect[SF_MAX_DIM], shift[SF_MAX_DIM]; sf_init (argc, argv); in = sf_input ("in"); out = sf_output ("out"); if (SF_FLOAT != sf_gettype(in)) sf_error("Need float input"); if (!sf_getint("repeat",&nrep)) nrep=1; /* repeat filtering several times */ dim = sf_filedims (in,n); dim1 = -1; for (i=0; i < dim; i++) { snprintf(key,6,"rect%d",i+1); if (NULL != sf_getstring(key)) { /*( rect# size of the smoothing stencil in #-th dimension /auxiliary input file/ )*/ rect[i] = sf_input(key); if (SF_FLOAT != sf_gettype(rect[i])) sf_error("Need float %s",key); dim1 = i; snprintf(key,8,"shift%d",i+1); if (NULL != sf_getstring(key)) { /*( shift# shifting of the smoothing stencil in #-th dimension /auxiliary input file/ )*/ shift[i] = sf_input(key); if (SF_INT != sf_gettype(shift[i])) sf_error("Need int %s",key); } else { shift[i] = NULL; } } else { rect[i] = NULL; shift[i] = NULL; } } n1 = n2 = 1; for (i=0; i < dim; i++) { if (i <= dim1) { n1 *= n[i]; } else { n2 *= n[i]; } } data = sf_floatalloc (n1); smoo = sf_floatalloc (n1); for (i=0; i <= dim1; i++) { box[i] = 1; if (NULL != rect[i]) { rct[i] = sf_floatalloc (n1); sft[i] = sf_intalloc (n1); sf_floatread(rct[i],n1,rect[i]); sf_fileclose(rect[i]); if (NULL != shift[i]) { sf_intread(sft[i],n1,shift[i]); sf_fileclose(shift[i]); } else { for (i1=0; i1 < n1; i1++) { sft[i][i1] = 0; } } for (i1=0; i1 < n1; i1++) { b = ceilf(rct[i][i1])+SF_ABS(sft[i][i1]); if (b > box[i]) box[i] = b; } } else { rct[i] = NULL; sft[i] = NULL; } } ntrianglen_init(dim1+1,box,n,rct,sft,nrep); for (i2=0; i2 < n2; i2++) { sf_floatread(data,n1,in); ntrianglen_lop(false,false,n1,n1,data,smoo); sf_floatwrite(smoo,n1,out); } exit (0); }