HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
i3ddsmgen.C
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024
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_Assert.h>
35 #include <UT/UT_Exit.h>
36 #include <UT/UT_Main.h>
37 #include <UT/UT_Matrix3.h>
38 #include <UT/UT_Matrix4.h>
39 #include <UT/UT_String.h>
40 #include <UT/UT_Vector3.h>
41 #include <UT/UT_WorkArgs.h>
42 
43 using std::cerr;
44 
45 static void
46 usage(const char *program)
47 {
48  cerr << "Usage: " << program << "\n\t-t <light transform> \n";
49  cerr << "\t-x <x resolution> -y <y resolution>\n";
50  cerr << "\t-l <projection type> <focal length> <aperture> <ortho width>\n";
51  cerr << "\t-i <I3D file>\n";
52  cerr << "\t-f <DSM file>\n";
53  cerr << "\t[-s <Step size>]\n";
54  cerr << "\t[-d <Max Depth>]\n";
55 }
56 
57 static void
58 fillPixel(IMG_DeepPixelWriter &writer, IMG3D_Manager &i3d,
59  const UT_Vector3 &orig, const UT_Vector3 &dir,
60  float dist, float step, float tau)
61 {
62  float pixel[3];
63  float den, pden, zval, zinc;
64  int i, nsteps;
65  UT_Vector3 p0, p1;
66  UT_Vector3 i1;
67 
68  // Find the interval we need to sample
69  p0 = orig;
70  p1 = orig + dist*dir;
71  i3d.integrate(p0, p1, pixel, 0.001F, 1, 0);
72  i3d.integrate(p1, p0, pixel, 0.001F, 1, 0);
73 
74  // Make sure there are cells to integrate
75  if (dot(p1-p0, dir) <= 0)
76  return;
77 
78  nsteps = (int)((p1-p0).length() / step);
79 #if 0
80  fprintf(stderr, "Integrating %d steps (from %f %f %f to %f %f %f)\n",
81  nsteps, p0[0], p0[1], p0[2], p1[0], p1[1], p1[2]);
82 #endif
83 
84  pden = 0;
85  zinc = step*dir.length();
86  zval = (p0-orig).length();
87  for (i = 0; i < nsteps; i++)
88  {
89  den = 0;
90  i1 = p0 + (float)i*step*dir;
91  i3d.sample(i1, &den);
92  den *= tau*step;
93  if (den > 0)
94  {
95  pden = den + (1-den)*pden;
96  pixel[0] = pixel[1] = pixel[2] = pden;
97 #if 0
98  fprintf(stderr, "Storing pixel data: %f %f %f %f\n",
99  pixel[0], pixel[1], pixel[2], pixel[3]);
100 #endif
101  writer.writeOrdered(zval, pixel, 3, 0, -1, 0);
102  }
103  zval += zinc;
104  }
105 }
106 
107 int
108 theMain(int argc, char *argv[])
109 {
110  CMD_Args args;
111  IMG_DeepShadow dsm;
112  IMG3D_Manager i3d;
114  UT_Vector3 orig;
115  UT_Vector3 dir;
116  float dist, step, zoom, xwin, ywin, tau, orthow;
117  const char *fname;
118  const char *iname;
119  int xres, yres;
120  int i, j;
121  bool ortho = true;
122 
123  args.initialize(argc, argv);
124  args.stripOptions("t:f:x:y:l::::i:s:d:");
125 
126  dist = 1000;
127  tau = 1;
128  step = 0.05;
129  orthow = 1;
130  fname = iname = 0;
131  if (args.found('f'))
132  fname = args.argp('f');
133  if (args.found('i'))
134  iname = args.argp('i');
135  if (args.found('s'))
136  step = args.fargp('s');
137  if (args.found('d'))
138  dist = args.fargp('d');
139 
140  if (args.found('t'))
141  {
142  UT_String str = args.argp('t');
143  UT_WorkArgs argv;
144 
145  str.tokenize(argv, "[],");
146  if (argv.getArgc() != 16)
147  {
148  cerr << "Invalid transform parameters "
149  "(example: [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1])\n";
150  return 1;
151  }
152 
153  for (i = 0; i < 4; i++)
154  for (j = 0; j < 4; j++)
155  trans(i, j) = atof(argv[i*4+j]);
156  }
157  else
158  {
159  cerr << "Must specify global transformation (-t)\n";
160  usage(argv[0]);
161  return 1;
162  }
163 
164  if (args.found('l') && args.found('x') && args.found('y'))
165  {
166  xres = args.iargp('x', 0);
167  yres = args.iargp('y', 0);
168  ortho = args.iargp('l', 0) == 1;
169  if (ortho)
170  {
171  orthow = args.fargp('l', 3);
172  zoom = 1;
173  }
174  else
175  {
176  if (args.fargp('l', 2) == 0)
177  {
178  cerr << "Must specify non-zero light aperture\n";
179  usage(argv[0]);
180  return 1;
181  }
182  zoom = args.fargp('l', 1) / args.fargp('l', 2);
183  }
184  }
185  else
186  {
187  cerr << "Must specify light parameters\n";
188  usage(argv[0]);
189  return 1;
190  }
191 
192  if (!fname || !iname)
193  {
194  cerr << "Must specify DSM and I3D file names\n";
195  usage(argv[0]);
196  return 1;
197  }
198 
199  if (!i3d.openTexture(iname))
200  {
201  cerr << "Could not open I3D texture " << iname << "\n";
202  usage(argv[0]);
203  return 1;
204  }
205  if (!i3d.openChannel("density"))
206  {
207  cerr << "Could not open density channel of " << iname << "\n";
208  usage(argv[0]);
209  return 1;
210  }
211 
212  std::cout << "Creating DSM " << fname << " from i3d " << iname << "\n";
213 
214  xwin = orthow * 0.5F / zoom;
215  ywin = xwin * (yres / (float)xres);
216 
217  dsm.setOption("compression", "5");
218  dsm.setOption("zbias", "0.05");
219  dsm.setOption("depth_planes", "zfront,zback");
220  dsm.create(fname, xres, yres, 1, 1);
221 
222  IMG_DeepPixelWriter writer(dsm);
223  if (!ortho)
224  {
225  trans.getTranslates(orig);
226  for (j = 0; j < yres; j++)
227  {
228  for (i = 0; i < xres; i++)
229  {
230  dir = UT_Vector3(xwin*2*(i+0.5F)/(float)xres-xwin,
231  ywin*2*(j+0.5F)/(float)yres-ywin, -1);
232  dir *= UT_DMatrix3(trans);
233 
234  writer.open(i, j);
235  fillPixel(writer, i3d, orig, dir, dist, step, tau);
236  writer.close();
237  }
238  }
239  }
240  else
241  {
242  dir = UT_Vector3(0, 0, -1);
243  dir.rowVecMult3(trans);
244  for (j = 0; j < yres; j++)
245  {
246  for (i = 0; i < xres; i++)
247  {
248  orig = UT_Vector3(xwin*2*(i+0.5F)/(float)xres-xwin,
249  ywin*2*(j+0.5F)/(float)yres-ywin, 0);
250  orig *= trans;
251 
252  writer.open(i, j);
253  fillPixel(writer, i3d, orig, dir, dist, step, tau);
254  writer.close();
255  }
256  }
257  }
258  dsm.close();
259 
260  return 0;
261 }
262 UT_MAIN(theMain);// exit with proper tear down
int iargp(int opt, int which=0) const
Definition: UT_Args.h:92
int theMain(int argc, char *argv[])
Definition: i3ddsmgen.C:108
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
GA_API const UT_StringHolder dist
UT_Matrix3T< double > UT_DMatrix3
void setOption(const char *option, const UT_StringHolder &value)
UT_MAIN(theMain)
UT_Vector3T< float > UT_Vector3
void stripOptions(const char *options)
int found(int opt) const
Definition: UT_Args.h:62
Thread-safe convenience class to make writing DSM pixels easy.
int openChannel(const char *channel_name)
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
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)
fpreal fargp(int opt, int which=0) const
Definition: UT_Args.h:83
IMATH_NAMESPACE::V2f float
int tokenize(char *argv[], int max_args, char separator)
Definition: UT_String.h:842
GA_API const UT_StringHolder trans
fpreal64 dot(const CE_VectorT< T > &a, const CE_VectorT< T > &b)
Definition: CE_Vector.h:140
GLint i1
Definition: glad.h:2724
SYS_FORCE_INLINE void rowVecMult3(const UT_Matrix4F &m)
Definition: UT_Matrix4.h:2036
bool open(int x, int y)
Open a pixel for writing.
int getArgc() const
Definition: UT_WorkArgs.h:34
auto fprintf(std::FILE *f, const S &fmt, const T &...args) -> int
Definition: printf.h:602
int sample(const UT_Vector3 &pos, float *result)
GLint j
Definition: glad.h:2733
GLsizeiptr const void GLenum usage
Definition: glcorearb.h:664
int integrate(UT_Vector3 &p0, UT_Vector3 &p1, float *result, fpreal limit_max=1, fpreal value_scale=1, fpreal accuracy=0)
int openTexture(const char *filename)
**If you just want to fire and args
Definition: thread.h:609
const char * argp(int opt, int which=0) const
Definition: UT_Args.h:67
bool close()
}
void initialize(int argc, const char *const argv[])
Class to handle reading/writing 3D texture images.
Definition: IMG3D_Manager.h:69
GLbitfield GLuint program
Definition: glcorearb.h:1931
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:1432