HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
i3ddsmgen.C
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018
3  * Side Effects Software Inc. All rights reserved.
4  *
5  * Redistribution and use of Houdini Development Kit samples in source and
6  * binary forms, with or without modification, are permitted provided that the
7  * following conditions are met:
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. The name of Side Effects Software may not be used to endorse or
11  * promote products derived from this software without specific prior
12  * written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY SIDE EFFECTS SOFTWARE `AS IS' AND ANY EXPRESS
15  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17  * NO EVENT SHALL SIDE EFFECTS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
20  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  *----------------------------------------------------------------------------
26  */
27 
28 
29 #include <stdio.h>
30 #include <iostream>
31 #include <CMD/CMD_Args.h>
32 #include <IMG/IMG_DeepShadow.h>
33 #include <IMG3D/IMG3D_Manager.h>
34 #include <UT/UT_Vector3.h>
35 #include <UT/UT_DMatrix4.h>
36 #include <UT/UT_DMatrix3.h>
37 #include <UT/UT_Assert.h>
38 #include <UT/UT_WorkArgs.h>
39 #include <UT/UT_String.h>
40 
41 using std::cerr;
42 
43 static void
44 usage(const char *program)
45 {
46  cerr << "Usage: " << program << "\n\t-t <light transform> \n";
47  cerr << "\t-x <x resolution> -y <y resolution>\n";
48  cerr << "\t-l <projection type> <focal length> <aperture> <ortho width>\n";
49  cerr << "\t-i <I3D file>\n";
50  cerr << "\t-f <DSM file>\n";
51  cerr << "\t[-s <Step size>]\n";
52  cerr << "\t[-d <Max Depth>]\n";
53 }
54 
55 static void
56 fillPixel(IMG_DeepPixelWriter &writer, IMG3D_Manager &i3d,
57  const UT_Vector3 &orig, const UT_Vector3 &dir,
58  float dist, float step, float tau)
59 {
60  float pixel[3];
61  float den, pden, zval, zinc;
62  int i, nsteps;
63  UT_Vector3 p0, p1;
64  UT_Vector3 i1;
65 
66  // Find the interval we need to sample
67  p0 = orig;
68  p1 = orig + dist*dir;
69  i3d.integrate(p0, p1, pixel, 0.001F, 1, 0);
70  i3d.integrate(p1, p0, pixel, 0.001F, 1, 0);
71 
72  // Make sure there are cells to integrate
73  if (dot(p1-p0, dir) <= 0)
74  return;
75 
76  nsteps = (int)((p1-p0).length() / step);
77 #if 0
78  fprintf(stderr, "Integrating %d steps (from %f %f %f to %f %f %f)\n",
79  nsteps, p0[0], p0[1], p0[2], p1[0], p1[1], p1[2]);
80 #endif
81 
82  pden = 0;
83  zinc = step*dir.length();
84  zval = (p0-orig).length();
85  for (i = 0; i < nsteps; i++)
86  {
87  den = 0;
88  i1 = p0 + (float)i*step*dir;
89  i3d.sample(i1, &den);
90  den *= tau*step;
91  if (den > 0)
92  {
93  pden = den + (1-den)*pden;
94  pixel[0] = pixel[1] = pixel[2] = pden;
95 #if 0
96  fprintf(stderr, "Storing pixel data: %f %f %f %f\n",
97  pixel[0], pixel[1], pixel[2], pixel[3]);
98 #endif
99  writer.writeOrdered(zval, pixel, 3, 0, -1, 0);
100  }
101  zval += zinc;
102  }
103 }
104 
105 int
106 main(int argc, char *argv[])
107 {
108  CMD_Args args;
109  IMG_DeepShadow dsm;
110  IMG3D_Manager i3d;
112  UT_Vector3 orig;
113  UT_Vector3 dir;
114  float dist, step, zoom, xwin, ywin, tau, orthow;
115  const char *fname;
116  const char *iname;
117  int xres, yres;
118  int i, j;
119  bool ortho = true;
120 
121  args.initialize(argc, argv);
122  args.stripOptions("t:f:x:y:l::::i:s:d:");
123 
124  dist = 1000;
125  tau = 1;
126  step = 0.05;
127  orthow = 1;
128  fname = iname = 0;
129  if (args.found('f'))
130  fname = args.argp('f');
131  if (args.found('i'))
132  iname = args.argp('i');
133  if (args.found('s'))
134  step = args.fargp('s');
135  if (args.found('d'))
136  dist = args.fargp('d');
137 
138  if (args.found('t'))
139  {
140  UT_String str = args.argp('t');
141  UT_WorkArgs argv;
142 
143  str.tokenize(argv, "[],");
144  if (argv.getArgc() != 16)
145  {
146  cerr << "Invalid transform parameters "
147  "(example: [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1])\n";
148  return 1;
149  }
150 
151  for (i = 0; i < 4; i++)
152  for (j = 0; j < 4; j++)
153  trans(i, j) = atof(argv[i*4+j]);
154  }
155  else
156  {
157  cerr << "Must specify global transformation (-t)\n";
158  usage(argv[0]);
159  return 1;
160  }
161 
162  if (args.found('l') && args.found('x') && args.found('y'))
163  {
164  xres = args.iargp('x', 0);
165  yres = args.iargp('y', 0);
166  ortho = args.iargp('l', 0) == 1;
167  if (ortho)
168  {
169  orthow = args.fargp('l', 3);
170  zoom = 1;
171  }
172  else
173  {
174  if (args.fargp('l', 2) == 0)
175  {
176  cerr << "Must specify non-zero light aperture\n";
177  usage(argv[0]);
178  return 1;
179  }
180  zoom = args.fargp('l', 1) / args.fargp('l', 2);
181  }
182  }
183  else
184  {
185  cerr << "Must specify light parameters\n";
186  usage(argv[0]);
187  return 1;
188  }
189 
190  if (!fname || !iname)
191  {
192  cerr << "Must specify DSM and I3D file names\n";
193  usage(argv[0]);
194  return 1;
195  }
196 
197  if (!i3d.openTexture(iname))
198  {
199  cerr << "Could not open I3D texture " << iname << "\n";
200  usage(argv[0]);
201  return 1;
202  }
203  if (!i3d.openChannel("density"))
204  {
205  cerr << "Could not open density channel of " << iname << "\n";
206  usage(argv[0]);
207  return 1;
208  }
209 
210  std::cout << "Creating DSM " << fname << " from i3d " << iname << "\n";
211 
212  xwin = orthow * 0.5F / zoom;
213  ywin = xwin * (yres / (float)xres);
214 
215  dsm.setOption("compression", "5");
216  dsm.setOption("zbias", "0.05");
217  dsm.setOption("depth_planes", "zfront,zback");
218  dsm.create(fname, xres, yres, 1, 1);
219 
220  IMG_DeepPixelWriter writer(dsm);
221  if (!ortho)
222  {
223  trans.getTranslates(orig);
224  for (j = 0; j < yres; j++)
225  {
226  for (i = 0; i < xres; i++)
227  {
228  dir = UT_Vector3(xwin*2*(i+0.5F)/(float)xres-xwin,
229  ywin*2*(j+0.5F)/(float)yres-ywin, -1);
230  dir *= UT_DMatrix3(trans);
231 
232  writer.open(i, j);
233  fillPixel(writer, i3d, orig, dir, dist, step, tau);
234  writer.close();
235  }
236  }
237  }
238  else
239  {
240  dir = UT_Vector3(0, 0, -1);
241  dir.rowVecMult3(trans);
242  for (j = 0; j < yres; j++)
243  {
244  for (i = 0; i < xres; i++)
245  {
246  orig = UT_Vector3(xwin*2*(i+0.5F)/(float)xres-xwin,
247  ywin*2*(j+0.5F)/(float)yres-ywin, 0);
248  orig *= trans;
249 
250  writer.open(i, j);
251  fillPixel(writer, i3d, orig, dir, dist, step, tau);
252  writer.close();
253  }
254  }
255  }
256  dsm.close();
257 
258  return 0;
259 }
SYS_FORCE_INLINE void rowVecMult3(const UT_Matrix4 &m)
Definition: UT_Vector3.h:371
int iargp(int opt, int which=0) const
Definition: UT_Args.h:86
GA_API const UT_StringHolder dist
UT_Matrix3T< double > UT_DMatrix3
void setOption(const char *option, const UT_StringHolder &value)
UT_Vector3T< float > UT_Vector3
void stripOptions(const char *options)
int found(int opt) const
Definition: UT_Args.h:56
Thread-safe convenience class to make writing DSM pixels easy.
int openChannel(const char *channel_name)
int tokenize(char *argv[], int maxArgs, char separator)
Definition: UT_String.h:805
Class to read or write deep shadow/camera images.
bool create(const char *name, int xres, int yres, int sxres, int syres, float pixel_aspect=1.0, const UT_DimRect *crop=NULL)
png_uint_32 i
Definition: png.h:2877
png_infop png_bytep * trans
Definition: png.h:2520
fpreal fargp(int opt, int which=0) const
Definition: UT_Args.h:77
fpreal64 dot(const CE_VectorT< T > &a, const CE_VectorT< T > &b)
Definition: CE_Vector.h:218
bool open(int x, int y)
Open a pixel for writing.
int getArgc() const
Definition: UT_WorkArgs.h:34
int sample(const UT_Vector3 &pos, float *result)
GLsizeiptr const void GLenum usage
Definition: glcorearb.h:663
int integrate(UT_Vector3 &p0, UT_Vector3 &p1, float *result, fpreal limit_max=1, fpreal value_scale=1, fpreal accuracy=0)
typedef int
Definition: png.h:1175
int main(int argc, char *argv[])
Definition: i3ddsmgen.C:106
int openTexture(const char *filename)
const char * argp(int opt, int which=0) const
Definition: UT_Args.h:61
bool close()
}
void initialize(int argc, const char *const argv[])
Class to handle reading/writing 3D texture images.
Definition: IMG3D_Manager.h:68
GLbitfield GLuint program
Definition: glcorearb.h:1930
bool writeOrdered(float z, const float *chdata, int chsize, int flags, int sampleid, float dz=0)
void getTranslates(UT_Vector3T< S > &translates) const
Definition: UT_Matrix4.h:1190
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:794