00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <stdio.h>
00030 #include <iostream>
00031 #include <CMD/CMD_Args.h>
00032 #include <IMG/IMG_DeepShadow.h>
00033 #include <IMG3D/IMG3D_Manager.h>
00034 #include <UT/UT_Vector3.h>
00035 #include <UT/UT_DMatrix4.h>
00036 #include <UT/UT_DMatrix3.h>
00037 #include <UT/UT_Assert.h>
00038
00039 static void
00040 usage(const char *program)
00041 {
00042 cerr << "Usage: " << program << "\n\t-t <light transform> \n";
00043 cerr << "\t-x <x resolution> -y <y resolution>\n";
00044 cerr << "\t-l <projection type> <focal length> <aperture> <ortho width>\n";
00045 cerr << "\t-i <I3D file>\n";
00046 cerr << "\t-f <DSM file>\n";
00047 cerr << "\t[-s <Step size>]\n";
00048 cerr << "\t[-d <Max Depth>]\n";
00049 }
00050
00051 static void
00052 fillPixel(IMG_DeepShadow &dsm, IMG3D_Manager &i3d,
00053 const UT_Vector4 &orig, const UT_Vector3 &dir,
00054 float dist, float step, float tau)
00055 {
00056 float pixel[3];
00057 float den, pden, zval, zinc;
00058 int i, nsteps;
00059 UT_Vector3 p0, p1;
00060 UT_Vector3 i0, i1;
00061
00062
00063 p0 = orig;
00064 p1 = orig + dist*dir;
00065 i3d.integrate(p0, p1, pixel, 0.001F, 1, 0);
00066 i3d.integrate(p1, p0, pixel, 0.001F, 1, 0);
00067
00068
00069 if (dot(p1-p0, dir) <= 0)
00070 return;
00071
00072 nsteps = (int)((p1-p0).length() / step);
00073 #if 0
00074 fprintf(stderr, "Integrating %d steps (from %f %f %f to %f %f %f)\n",
00075 nsteps, p0[0], p0[1], p0[2], p1[0], p1[1], p1[2]);
00076 #endif
00077
00078 pden = 0;
00079 zinc = step*dir.length();
00080 zval = (p0-orig).length();
00081 for (i = 0; i < nsteps; i++)
00082 {
00083 den = 0;
00084 i1 = p0 + (float)i*step*dir;
00085 i3d.sample(i1, &den);
00086 den *= tau*step;
00087 if (den > 0)
00088 {
00089 pden = den + (1-den)*pden;
00090 pixel[0] = pixel[1] = pixel[2] = pden;
00091 #if 0
00092 fprintf(stderr, "Storing pixel data: %f %f %f %f\n",
00093 pixel[0], pixel[1], pixel[2], pixel[3]);
00094 #endif
00095 dsm.pixelWrite(zval, pixel, 3);
00096 }
00097 zval += zinc;
00098 }
00099 }
00100
00101 int
00102 main(int argc, char *argv[])
00103 {
00104 char buffer[1024];
00105 CMD_Args args;
00106 IMG_DeepShadow dsm;
00107 IMG3D_Manager i3d;
00108 UT_DMatrix4 trans;
00109 UT_Vector4 orig;
00110 UT_Vector3 dir, axis;
00111 float dist, step, zoom, xwin, ywin, tau, orthow;
00112 const char *fname;
00113 const char *iname;
00114 int xres, yres;
00115 int i, j;
00116 bool ortho = true;
00117
00118 args.initialize(argc, argv);
00119 args.stripOptions("t:f:x:y:l::::i:s:d:");
00120
00121 dist = 1000;
00122 tau = 1;
00123 step = 0.05;
00124 orthow = 1;
00125 fname = iname = 0;
00126 if (args.found('f'))
00127 fname = args.argp('f');
00128 if (args.found('i'))
00129 iname = args.argp('i');
00130 if (args.found('s'))
00131 step = args.fargp('s');
00132 if (args.found('d'))
00133 dist = args.fargp('d');
00134
00135 if (args.found('t'))
00136 {
00137 strcpy(buffer, args.argp('t'));
00138 trans(0, 0) = atof(strtok(buffer, "[],"));
00139 for (i = 1; i < 16; i++)
00140 trans(i/4, i%4) = atof(strtok(0, "[],"));
00141 }
00142 else
00143 {
00144 cerr << "Must specify global transformation (-t)\n";
00145 usage(argv[0]);
00146 return 1;
00147 }
00148
00149 if (args.found('l') && args.found('x') && args.found('y'))
00150 {
00151 xres = args.iargp('x', 0);
00152 yres = args.iargp('y', 0);
00153 ortho = args.iargp('l', 0) == 1;
00154 if (ortho)
00155 {
00156 orthow = args.fargp('l', 3);
00157 zoom = 1;
00158 }
00159 else
00160 {
00161 if (args.fargp('l', 2) == 0)
00162 {
00163 cerr << "Must specify non-zero light aperture\n";
00164 usage(argv[0]);
00165 return 1;
00166 }
00167 zoom = args.fargp('l', 1) / args.fargp('l', 2);
00168 }
00169 }
00170 else
00171 {
00172 cerr << "Must specify light parameters\n";
00173 usage(argv[0]);
00174 return 1;
00175 }
00176
00177 if (!fname || !iname)
00178 {
00179 cerr << "Must specify DSM and I3D file names\n";
00180 usage(argv[0]);
00181 return 1;
00182 }
00183
00184 if (!i3d.openTexture(iname))
00185 {
00186 cerr << "Could not open I3D texture " << iname << "\n";
00187 usage(argv[0]);
00188 return 1;
00189 }
00190 if (!i3d.openChannel("density"))
00191 {
00192 cerr << "Could not open density channel of " << iname << "\n";
00193 usage(argv[0]);
00194 return 1;
00195 }
00196
00197 cout << "Creating DSM " << fname << " from i3d " << iname << "\n";
00198
00199 xwin = orthow * 0.5F / zoom;
00200 ywin = xwin * (yres / (float)xres);
00201
00202 dsm.setOption("compression", "5");
00203 dsm.setOption("zbias", "0.05");
00204 dsm.setOption("depth_mode", "nearest");
00205 dsm.setOption("depth_interp", "continuous");
00206 dsm.open(fname, xres, yres);
00207
00208 if (!ortho)
00209 {
00210 orig = UT_Vector4(0, 0, 0, 1);
00211 orig *= trans;
00212 for (j = 0; j < yres; j++)
00213 {
00214 for (i = 0; i < xres; i++)
00215 {
00216 dir = UT_Vector3(xwin*2*(i+0.5F)/(float)xres-xwin,
00217 ywin*2*(j+0.5F)/(float)yres-ywin, -1);
00218 dir *= UT_DMatrix3(trans);
00219
00220 dsm.pixelStart(i, j);
00221 fillPixel(dsm, i3d, orig, dir, dist, step, tau);
00222 dsm.pixelClose();
00223 }
00224 }
00225 }
00226 else
00227 {
00228 dir = UT_Vector3(0, 0, -1);
00229 dir *= UT_DMatrix3(trans);
00230 for (j = 0; j < yres; j++)
00231 {
00232 for (i = 0; i < xres; i++)
00233 {
00234 orig = UT_Vector3(xwin*2*(i+0.5F)/(float)xres-xwin,
00235 ywin*2*(j+0.5F)/(float)yres-ywin, 0);
00236 orig *= trans;
00237
00238 dsm.pixelStart(i, j);
00239 fillPixel(dsm, i3d, orig, dir, dist, step, tau);
00240 dsm.pixelClose();
00241 }
00242 }
00243 }
00244 dsm.close();
00245
00246 return 0;
00247 }