Exemplo n.º 1
0
void  gradint2_init (float** coord          /* coordinates [nd][2] */, 
		     float o1, float o2, 
		     float d1, float d2,
		     int   n1, int   n2     /* axes */, 
		     sf_interpolator interp /* interpolation function */, 
		     int nf                 /* interpolator length */, 
		     int nd                 /* number of data points */)
/*< initialize >*/
{
    tmp = sf_floatalloc(nd);
    sf_int2_init(coord,o1,o2,d1,d2,n1,n2,interp,nf,nd);
}
Exemplo n.º 2
0
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);
}
Exemplo n.º 3
0
int main(int argc, char* argv[])
{
    int n12, i, i1, i2, rect, order, niter;
    float **img, **grad, **grad1, **grad2, *y, **x;
    float med, t, t2, tol[2], band;
    sf_eno2 g[2];
    sf_file in, out;
    
    sf_init(argc,argv);
    in = sf_input("in");
    out = sf_output("out");

    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in input");
    n12 = n1*n2;

    if (!sf_getint("rect",&rect)) rect=3;
    /* smoothing radius */
    t = (rect*rect-1)/6.0;
    sf_runge_init(2,1,t/2);

    if (!sf_getint("order",&order)) order=3;
    /* interpolation order */
    if (!sf_getfloat("tol",&tol[0])) tol[0]=0.1;
    /* error tolerance */
    tol[1]=tol[0];
    if (!sf_getint("niter",&niter)) niter=100;
    /* number of iterations */
    if (!sf_getfloat("band",&band)) band=1.;
    /* narrow band */

    img = sf_floatalloc2(n1,n2);
    grad = sf_floatalloc2(n1,n2);
    grad1 = sf_floatalloc2(n1,n2);
    grad2 = sf_floatalloc2(n1,n2);
    y = sf_floatalloc(n12);
    x = sf_floatalloc2(2,n12);

    g[0] = sf_eno2_init (order, n1, n2);
    g[1] = sf_eno2_init (order, n1, n2);

    sf_floatread(img[0],n12,in);

    /* compute gradient squared */
    sf_sobel2 (n1,n2,img,grad);

    /* normalize by median */
    for (i=0; i < n12; i++) {
	y[i] = grad[0][i];
    }

    med = sf_quantile(n12/2,n12,y);

    for (i=0; i < n12; i++) {
	grad[0][i] = -0.5*logf(1.+grad[0][i]/med);
    }

    /* compute gradient */
    sf_sobel (n1,n2,grad,grad1,grad2);
    sf_eno2_set (g[0], grad1);
    sf_eno2_set (g[1], grad2);

    /* convection */
    i=0;
    for (i2=0; i2 < n2; i2++) {
	for (i1=0; i1 < n1; i1++) {
	    if (fabsf(img[i2][i1]) < band) {
		x[i][0] = i1;
		x[i][1] = i2;
		t2 = sf_ode23 (t,tol, x[i], g, rhs, term);
		if (t2 >= t) {
		    y[i] = img[i2][i1];
		    i++;
		}
	    }
	}
    }

    /* diffusion */
    sf_int2_init (x, 0,0,1,1,n1,n2, sf_lin_int, 2, i);
    sf_triangle2_init(rect,rect, n1, n2, 1);
    sf_conjgrad_init(n12, n12, i, i, 1.0/i, 1.e-8, true, false);
    sf_conjgrad(NULL, 
		sf_int2_lop, sf_triangle2_lop, grad[0], img[0], y, niter);

    sf_floatwrite(img[0],n12,out);

    exit(0);
}
Exemplo n.º 4
0
int main (int argc, char* argv[])
{
    int id, nk, nd, nm, nt, it, nx, ny, xkey, ykey, interp;
    float *mm, *dd, **xy, *hdr, x0, y0, dx, dy;
    sf_file in, out, head;

    sf_init (argc,argv);
    in  = sf_input ( "in");
    out = sf_output("out");

    if (!sf_histint(in,"n1",&nx)) sf_error("Need n1= in in");
    if (!sf_histint(in,"n2",&ny)) sf_error("Need n2= in in");
    nt = sf_leftsize(in,2);
    if (SF_FLOAT != sf_gettype(in)) sf_error("Need float input");

    if (!sf_getint("xkey",&xkey)) xkey=0;
    /* x key number */
    if (!sf_getint("ykey",&ykey)) ykey=1;
    /* y key number */

    /* create coordinates */
    head = sf_input("head");
    if (!sf_histint(head,"n1",&nk)) sf_error("No   n1= in head");
    if (!sf_histint(head,"n2",&nd)) sf_error("Need n2= in head");
    if (SF_FLOAT != sf_gettype(head)) sf_error("Need float header");

    xy  = sf_floatalloc2(2,nd);
    hdr = sf_floatalloc(nk);

    for (id=0; id<nd; id++) {	
	sf_floatread (hdr,nk,head);
	xy[id][0] = hdr[xkey];
	xy[id][1] = hdr[ykey];
    }

    sf_fileclose (head);

    sf_putint(out,"n1",nd);
    sf_putint(out,"n2",1);

    if (!sf_histfloat(in,"o1",&x0)) sf_error("No o1= in input");
    if (!sf_histfloat(in,"o2",&y0)) sf_error("No o2= in input");
    
    if (!sf_histfloat(in,"d1",&dx)) sf_error("No d1= in input");
    if (!sf_histfloat(in,"d2",&dy)) sf_error("No d2= in input");
    
    /* initialize interpolation */
    if (!sf_getint("interp",&interp)) interp=2;
    /* [1,2] interpolation method, 1: nearest neighbor, 2: bi-linear */

    switch (interp) {
	case 1:
	    sf_int2_init (xy, x0,y0,dx,dy,nx,ny, sf_bin_int, 1, nd);
	    sf_warning("Using nearest-neighbor interpolation");
	    break;
	case 2:
	    sf_int2_init (xy, x0,y0,dx,dy,nx,ny, sf_lin_int, 2, nd);
	    sf_warning("Using linear interpolation");
	    break;
	case 3:
	    sf_error("Unsupported interp=%d",interp);
	    break;
    }

    nm = nx*ny;
    mm = sf_floatalloc(nm);
    dd = sf_floatalloc(nd);

    for (it=0; it < nt; it++) { /* loop over time slices */
	sf_floatread (mm,nm,in);

	sf_int2_lop(false,false,nm,nd,mm,dd);

	sf_floatwrite (dd,nd,out);
    }


    exit(0);
}