int main(int argc,char *argv[]) { AGMain main; MyApp app; AntargisView *av=new AntargisView(0,AGRect(0,0,100,100),Pos3D(0,0,0)); int x,y,z; int w=64; int s=0; Terrain t(16,16); AGSurface ms; int test=4; if(argc>1) test=atoi(argv[1]); if(test==1) { ms=test1(); av->insert(new VoxelImage(ms,Pos3D(0,0,0))); } if(test==2) { ms=ball(); av->insert(new VoxelImage(ms,Pos3D(0,0,0))); } else if(test==3) { ms=getTerrain(t.getInfo(2,0)); av->insert(new VoxelImage(ms,Pos3D(0,0,0))); } else if(test==4) { ms=getTerrain(t.getInfo(1,0)); av->insert(new VoxelImage(ms,Pos3D(0,0,0))); ms=getTerrain(t.getInfo(2,0)); av->insert(new VoxelImage(ms,Pos3D(64,0,0))); ms=getTerrain(t.getInfo(1,1)); av->insert(new VoxelImage(ms,Pos3D(32,0,-32))); ms=getTerrain(t.getInfo(2,1)); av->insert(new VoxelImage(ms,Pos3D(64+32,0,-32))); } else if(test==5) { int x,y; for(int x=0;x<4;x++) for(int y=0;y<4;y++) { ms=getTerrain(t.getInfo(x,y)); av->insert(new VoxelImage(ms,Pos3D(x*64+(y%2)*32,0,-y*32))); } } else if(test==6) { SplineMap<float> m(16,16,50); SplineMap<float> gm(16,16,20); int w=64; VoxelView v(w,w*2,Pos3D(0,0,0),true); for(int x=0;x<w;x++) for(int z=0;z<w;z++) { float mx=x/16.0; float mz=z/16.0; float h=m.get(mx,mz); for(int y=0;y<h;y++) { float a=std::min(1.0f,h-y); // cdebug(a); v.set(Pos3D(x,y,z),Color(0xAA,0xAA,0,a)); } // grass above float gh=rand()%int(gm.get(mx,mz)); if(gh>0) for(int y=0;y<gh;y++) { float a=1.0f-(y/gh); // cdebug(a); v.set(Pos3D(x,y+h,z),Color(0,0xAA,0,a)); } } /* // small ball above for(int x=0;x<8;x++) for(int y=0;y<8;y++) for(int z=0;z<8;z++) { int mx=x-4; int my=y-4; int mz=z-4; if(sqrt(mx*mx+my*my+mz*mz)<4) v.set(Pos3D(x+10,y+40,z+20),Color(0xAA,0,0)); } */ av->insert(new VoxelImage(v.getSurface(),Pos3D(0,0,0))); } else if(test==7) { float v0=-20; float v1=60; float v2=60; float v3=-20; float v4=0; float v5=0; float v6=0; int x; SDL_Surface *s=getScreen().surface(); Color c(0,0,1); for(x=0;x<32;x++) { int y=spline2(x/32.0,v0,v1,v2);//,v3); sge_PutPixel(s,x,y+50,c.toColor(s)); } for(x=0;x<32;x++) { int y=spline2(x/32.0,v1,v2,v3);//,v4); sge_PutPixel(s,x+32,y+50,c.toColor(s)); } for(x=0;x<32;x++) { int y=spline2(x/32.0,v2,v3,v4);//,v5); sge_PutPixel(s,x+64,y+50,c.toColor(s)); } for(x=0;x<32;x++) { int y=spline2(x/32.0,v3,v4,v5);//,v6); sge_PutPixel(s,x+96,y+50,c.toColor(s)); } SDL_Flip(s); // SDL_Delay(1000); } else if(test==8) { float v0=-20; float v1=60; float v2=60; float v3=-20; float v4=0; float v5=0; float v6=0; int x; SDL_Surface *s=getScreen().surface(); Color c(1,0,1); for(x=0;x<32;x++) { int y=spline3(x/32.0,v0,v1,v2,v3); sge_PutPixel(s,x,y+50,c.toColor(s)); } for(x=0;x<32;x++) { int y=spline3(x/32.0,v1,v2,v3,v4); sge_PutPixel(s,x+32,y+50,c.toColor(s)); } for(x=0;x<32;x++) { int y=spline3(x/32.0,v2,v3,v4,v5); sge_PutPixel(s,x+64,y+50,c.toColor(s)); } for(x=0;x<32;x++) { int y=spline3(x/32.0,v3,v4,v5,v6); sge_PutPixel(s,x+96,y+50,c.toColor(s)); } SDL_Flip(s); // SDL_Delay(1000); } else if(test==9) { SplineMap<float> m(64,64,50); int w=64; VoxelView v(w,w*2,Pos3D(0,0,0),false); int z=0; for(int x=0;x<w;x++) { float h=20+x/16.0; for(int y=0;y<h;y++) { float a=std::min(1.0f,h-y); // if(a<1) if(a>0) { cdebug(a); v.set(Pos3D(x,y,z),Color(0xFF,0xFF,0xFF,a));//Color(0xAA*a,0xAA*a,0));//,a)); } } } av->insert(new VoxelImage(v.getSurface(),Pos3D(0,0,0))); } else if(test==10) { SDL_Surface *s=getScreen().surface(); for(int x=0;x<100;x++) { float h=20+x/16.0; for(int y=0;y<h;y++) { float a=std::min(1.0f,h-y); if(a>0) { sge_PutPixelAlpha(s,x,100-y,SDL_MapRGBA(s->format,0xFF,0xFF,0xFF,a*0xFF),a*0xFF); } } } SDL_Flip(s); SDL_Delay(1000); } else if(test==11 || test==12) { int w=8; SplineMap<float> m(POINTS_PER_TILE*(w+2),POINTS_PER_TILE*(w+2),40); SplineMap<float> gm(POINTS_PER_TILE*(w+2),POINTS_PER_TILE*(w+2),10); if(test==12) w=2; for(int y=0;y<w;y++) for(int x=0;x<w;x++) { int mx=x*(POINTS_PER_TILE); int my=y*(POINTS_PER_TILE/2); if(y&1) mx+=(POINTS_PER_TILE/2); av->insert(new VoxelImage(makeTerrainTile(m,gm,mx,my),Pos3D(mx*TILE_WIDTH/POINTS_PER_TILE,0,my*TILE_WIDTH/POINTS_PER_TILE))); } } else if(test==13) { AGSurface s=makeWaterTile(); av->insert(new VoxelImage(s,Pos3D(64,0,0))); av->insert(new VoxelImage(s,Pos3D(0,0,0))); av->insert(new VoxelImage(s,Pos3D(64+32,0,32))); av->insert(new VoxelImage(s,Pos3D(32,0,32))); av->insert(new VoxelImage(s,Pos3D(64+32,0,-32))); av->insert(new VoxelImage(s,Pos3D(32,0,-32))); } else if(test==14) { AGSurface s=makeWaterTile(); std::string ms=toPNG(s.surface()); s=fromPNG(ms); av->insert(new VoxelImage(s,Pos3D(64,0,0))); } app.setMainWidget(av); app.run(); }
long randomWavelet3D(wavPar wav, srcPar src, float *trace, float tbeg, float tend, long verbose) { long optn, nfreq, j, iwmax; long iw, n1, itbeg, itmax, nsmth; float df, amp1; float *rtrace; float x1, x2, z1, z2, dzdx1, dzdx2, a, b, c, d, t; complex *ctrace; n1 = wav.nt; /* this is set to the maximum length (tlength/dt) */ optn = loptncr(2*n1); nfreq = optn/2 + 1; ctrace = (complex *)calloc(nfreq,sizeof(complex)); rtrace = (float *)calloc(optn,sizeof(float)); df = 1.0/(optn*wav.dt); iwmax = MIN(NINT(wav.fmax/df),nfreq); for (iw=1;iw<iwmax;iw++) { ctrace[iw].r = (float)(drand48()-0.5); ctrace[iw].i = (float)(drand48()-0.5); } for (iw=iwmax;iw<nfreq;iw++) { ctrace[iw].r = 0.0; ctrace[iw].i = 0.0; } cr1fft(ctrace,rtrace,optn,1); /* find first zero crossing in wavelet */ amp1 = rtrace[0]; j = 1; if (amp1 < 0.0) { while (rtrace[j] < 0.0) j++; } else { while (rtrace[j] > 0.0) j++; } itbeg = j; /* find last zero crossing in wavelet */ // itmax = itbeg+MIN(NINT((tend-tbeg)/wav.dt),n1); itmax = MIN(NINT(itbeg+(tend-tbeg)/wav.dt),n1); amp1 = rtrace[itmax-1]; j = itmax; if (amp1 < 0.0) { while (rtrace[j] < 0.0 && j>itbeg) j--; } else { while (rtrace[j] > 0.0 && j>itbeg) j--; } itmax = j; /* make smooth transitions to zero aamplitude */ nsmth=MIN(10,itmax); x1 = 0.0; z1 = 0.0; dzdx1 = 0.0; x2 = nsmth; z2 = rtrace[itbeg+nsmth]; dzdx2 = (rtrace[itbeg+nsmth-2]-8.0*rtrace[itbeg+nsmth-1]+ 8.0*rtrace[itbeg+nsmth+1]-rtrace[itbeg+nsmth+2])/(12.0); spline3(x1, x2, z1, z2, dzdx1, dzdx2, &a, &b, &c, &d); for (j=0; j<nsmth; j++) { t = j; rtrace[itbeg+j] = a*t*t*t+b*t*t+c*t+d; } x1 = 0.0; z1 = rtrace[itmax-nsmth]; dzdx1 = (rtrace[itmax-nsmth-2]-8.0*rtrace[itmax-nsmth-1]+ 8.0*rtrace[itmax-nsmth+1]-rtrace[itmax-nsmth+2])/(12.0); x2 = nsmth; z2 = 0.0; dzdx2 = 0.0; spline3(x1, x2, z1, z2, dzdx1, dzdx2, &a, &b, &c, &d); for (j=0; j<nsmth; j++) { t = j; rtrace[itmax-nsmth+j] = a*t*t*t+b*t*t+c*t+d; } for (j=itbeg; j<itmax; j++) trace[j-itbeg] = rtrace[j]; free(ctrace); free(rtrace); return 0; }
void apply_general_transformation(float *input, float *transformx, float* transformy, float *out, float bg, int order, int width, int height, int nwidth,int nheight) { float *coeffs; float *ref; float cx[12],cy[12],ak[13]; if (order!=0 && order!=1 && order!=-3 && order!=3 && order!=5 && order!=7 && order!=9 && order!=11) { printf("unrecognized interpolation order.\n"); exit(-1); } if (order>=3) { coeffs = new float[width*height]; finvspline(input,order,coeffs,width,height); ref = coeffs; if (order>3) init_splinen(ak,order); } else { coeffs = NULL; ref = input; } int xi,yi; float xp,yp; float res; int n1,n2; float p=-0.5; for(int i=0; i < nwidth; i++) for(int j=0; j < nheight; j++) { xp = (float) transformx[j*nwidth+i]; yp = (float) transformy[j*nwidth+i]; if (order == 0) { xi = (int)floor((double)xp); yi = (int)floor((double)yp); if (xi<0 || xi>=width || yi<0 || yi>=height) res = bg; else res = input[yi*width+xi]; } else { if (xp<0. || xp>(float)width || yp<0. || yp>(float)height) res=bg; else { //xp -= 0.5; yp -= 0.5; int xi = (int)floor((double)xp); int yi = (int)floor((double)yp); float ux = xp-(float)xi; float uy = yp-(float)yi; switch (order) { case 1: /* first order interpolation (bilinear) */ n2 = 1; cx[0]=ux; cx[1]=1.f-ux; cy[0]=uy; cy[1]=1.f-uy; break; case -3: /* third order interpolation (bicubic Keys' function) */ n2 = 2; keys(cx,ux,p); keys(cy,uy,p); break; case 3: /* spline of order 3 */ n2 = 2; spline3(cx,ux); spline3(cy,uy); break; default: /* spline of order >3 */ n2 = (1+order)/2; splinen(cx,ux,ak,order); splinen(cy,uy,ak,order); break; } res = 0.; n1 = 1-n2; if (xi+n1>=0 && xi+n2<width && yi+n1>=0 && yi+n2<height) { int adr = yi*width+xi; for (int dy=n1;dy<=n2;dy++) for (int dx=n1;dx<=n2;dx++) res += cy[n2-dy]*cx[n2-dx]*ref[adr+width*dy+dx]; } else for (int dy=n1;dy<=n2;dy++) for (int dx=n1;dx<=n2;dx++) res += cy[n2-dy]*cx[n2-dx]*v(ref,xi+dx,yi+dy,bg,width,height); } } out[j*nwidth+i] = res; } }
void apply_planar_homography(float *input, int width, int height, float **H, float bg, int order, float *out, float x0, float y0, int nwidth, int nheight) { if (DEBUG) printf("width: %d height: %d ------> nwidth: %d nheight: %d \n", width, height, nwidth, nheight); /// We compute inverse transformation if (DEBUG) { printf("Matrix: \n"); print_float_matrix(H,3,3); } float **V = allocate_float_matrix(3,3); luinv(H, V, 3); if (DEBUG) { printf("Inverse Matrix: \n"); print_float_matrix(V,3,3); } float *coeffs; float *ref; float cx[12],cy[12],ak[13]; if (order!=0 && order!=1 && order!=-3 && order!=3 && order!=5 && order!=7 && order!=9 && order!=11) { printf("unrecognized interpolation order.\n"); exit(-1); } if (order>=3) { coeffs = new float[width*height]; finvspline(input,order,coeffs,width,height); ref = coeffs; if (order>3) init_splinen(ak,order); } else { coeffs = NULL; ref = input; } int xi,yi; float xp,yp; float res; int n1,n2; float p=-0.5; /// For each point in new image we compute its anti image and interpolate the new value for(int i=0; i < nwidth; i++) for(int j=0; j < nheight; j++) { float vec[3]; vec[0] = (float) i + x0; vec[1] = (float) j + y0; vec[2] = 1.0f; float vres[3]; float_vector_matrix_product(V, vec ,vres , 3); if (vres[2] != 0.0f) { vres[0] /= vres[2]; vres[1] /= vres[2]; xp = (float) vres[0]; yp = (float) vres[1]; if (order == 0) { xi = (int)floor((double)xp); yi = (int)floor((double)yp); if (xi<0 || xi>=width || yi<0 || yi>=height) res = bg; else res = input[yi*width+xi]; } else { if (xp<0. || xp>=(float)width || yp<0. || yp>=(float)height) res=bg; else { //xp -= 0.5; yp -= 0.5; int xi = (int)floor((double)xp); int yi = (int)floor((double)yp); float ux = xp-(float)xi; float uy = yp-(float)yi; switch (order) { case 1: /* first order interpolation (bilinear) */ n2 = 1; cx[0]=ux; cx[1]=1.f-ux; cy[0]=uy; cy[1]=1.f-uy; break; case -3: /* third order interpolation (bicubic Keys' function) */ n2 = 2; keys(cx,ux,p); keys(cy,uy,p); break; case 3: /* spline of order 3 */ n2 = 2; spline3(cx,ux); spline3(cy,uy); break; default: /* spline of order >3 */ n2 = (1+order)/2; splinen(cx,ux,ak,order); splinen(cy,uy,ak,order); break; } res = 0.; n1 = 1-n2; if (xi+n1>=0 && xi+n2<width && yi+n1>=0 && yi+n2<height) { int adr = yi*width+xi; for (int dy=n1;dy<=n2;dy++) for (int dx=n1;dx<=n2;dx++) res += cy[n2-dy]*cx[n2-dx]*ref[adr+width*dy+dx]; } else for (int dy=n1;dy<=n2;dy++) for (int dx=n1;dx<=n2;dx++) res += cy[n2-dy]*cx[n2-dx]*v(ref,xi+dx,yi+dy,bg,width,height); } } out[j*nwidth+i] = res; } } }
// Spline interpolation of given order of image im at point (x,y). // out must be an array of size the number of components. // Supported orders: 0(nn), 1(bilinear), -3(Keys's bicubic), 3, 5, 7, 9, 11. // Success means a valid order and pixel in image. bool evaluate_spline_at(float *out, float *img, int w, int h, int pd, int order, float x, float y) { float cx[12],cy[12]; /* CHECK ORDER */ if (order != 0 && order != 1 && order != -3 && order != 3 && order != 5 && order != 7 && order != 9 && order != 11) return false; float ak[13]; if (order > 3) init_splinen(ak, order); /* INTERPOLATION */ if(order == 0) { /* zero order interpolation (pixel replication) */ int xi = x; int yi = y; if (!insideP(w, h, xi, yi)) return false; float* p = img + (w*yi+xi) * pd; for(int i = 0; i < pd; i++) out[i] = p[i]; } else { /* higher order interpolations */ if (!(x>=0 && x<=w && y>=0 && y<=h)) return false; x -= 0.5; y -= 0.5; int xi = (x<0)? -1: x; int yi = (y<0)? -1: y; float ux = x - xi; float uy = y - yi; float paramKeys = -0.5; switch(order) { case 1: /* first order interpolation (bilinear) */ cx[0] = ux; cx[1] = 1-ux; cy[0] = uy; cy[1] = 1-uy; break; case -3: /* third order interpolation (bicubic Keys) */ keys(cx, ux, paramKeys); keys(cy, uy, paramKeys); break; case 3: /* spline of order 3 */ spline3(cx, ux); spline3(cy, uy); break; default: /* spline of order >3 */ splinen(cx, ux, ak, order); splinen(cy, uy, ak, order); break; } int n2 = (order == -3) ? 2 : (order+1)/2; int n1 = 1 - n2; /* this test saves computation time */ if (insideP(w, h, xi+n1, yi+n1) && insideP(w, h, xi+n1, yi+n2)) { for (int k = 0; k < pd; k++) { out[k] = 0; for (int dy = n1; dy <= n2; dy++) { int ii = xi + n1; int jj = yi + dy; float* v = img + (jj*w+ii)*pd+k; for (int dx = n1; dx <= n2; dx++) { float f = *v; out[k]+=cy[n2-dy]*cx[n2-dx] * f; v += pd; } } } } else for (int k = 0; k < pd; k++) { out[k] = 0; for (int dy = n1; dy <= n2; dy++) for (int dx = n1; dx <= n2; dx++) { int ii = xi + dx; int jj = yi + dy; getsample_t S = getsample_2; float v = S(img, w, h, pd, ii, jj, k); out[k] += cy[n2-dy] * cx[n2-dx] * v; } } } return true; }
void apply_zoom(float *input, float *out, float zoom, int order, int width, int height) { int nwidth = (int)( zoom * (float) width); int nheight = (int)( zoom * (float) height); float *coeffs; float *ref; float cx[12],cy[12],ak[13]; // Guoshen Yu, 2010.09.22, Windows versions vector<float> input_vec, coeffs_vec, ref_vec; input_vec = vector<float>(width*height); coeffs_vec = vector<float>(width*height); ref_vec = vector<float>(width*height); for (int i = 0; i < width*height; i++) input_vec[i] = input[i]; if (order!=0 && order!=1 && order!=-3 && order!=3 && order!=5 && order!=7 && order!=9 && order!=11) { printf("unrecognized interpolation order.\n"); exit(-1); } if (order>=3) { coeffs = new float[width*height]; // Guoshen Yu, 2010.09.21, Windows version //finvspline(input,order,coeffs,width,height); finvspline(input_vec,order,coeffs_vec,width,height); for (int i = 0; i < width*height; i++) coeffs[i] = coeffs_vec[i]; ref = coeffs; if (order>3) init_splinen(ak,order); } else { coeffs = NULL; ref = input; } int xi,yi; float xp,yp; float res; int n1,n2; float bg = 0.0f; float p=-0.5; for(int i=0; i < nwidth; i++) for(int j=0; j < nheight; j++) { xp = (float) i / zoom; yp = (float) j / zoom; if (order == 0) { xi = (int)floor((double)xp); yi = (int)floor((double)yp); if (xi<0 || xi>=width || yi<0 || yi>=height) res = bg; else res = input[yi*width+xi]; } else { if (xp<0. || xp>=(float)width || yp<0. || yp>=(float)height) res=bg; else { xp -= 0.5; yp -= 0.5; int xi = (int)floor((double)xp); int yi = (int)floor((double)yp); float ux = xp-(float)xi; float uy = yp-(float)yi; switch (order) { case 1: /* first order interpolation (bilinear) */ n2 = 1; cx[0]=ux; cx[1]=1.-ux; cy[0]=uy; cy[1]=1.-uy; break; case -3: /* third order interpolation (bicubic Keys' function) */ n2 = 2; keys(cx,ux,p); keys(cy,uy,p); break; case 3: /* spline of order 3 */ n2 = 2; spline3(cx,ux); spline3(cy,uy); break; default: /* spline of order >3 */ n2 = (1+order)/2; splinen(cx,ux,ak,order); splinen(cy,uy,ak,order); break; } res = 0.; n1 = 1-n2; if (xi+n1>=0 && xi+n2<width && yi+n1>=0 && yi+n2<height) { int adr = yi*width+xi; for (int dy=n1;dy<=n2;dy++) for (int dx=n1;dx<=n2;dx++) res += cy[n2-dy]*cx[n2-dx]*ref[adr+width*dy+dx]; } else // Guoshen Yu, 2010.09.21, Windows for (int i = 0; i < width*height; i++) ref_vec[i] = ref[i]; for (int dy=n1;dy<=n2;dy++) for (int dx=n1;dx<=n2;dx++) // Guoshen Yu, 2010.09.21, Windows // res += cy[n2-dy]*cx[n2-dx]*v(ref,xi+dx,yi+dy,bg,width,height); res += cy[n2-dy]*cx[n2-dx]*v(ref_vec,xi+dx,yi+dy,bg,width,height); } } out[j*nwidth+i] = res; } }