HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IMG3D_Manager.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: IMG3D_Manager.h ( IMG3D Library, C++)
7  *
8  * COMMENTS: API into the 3D texture library.
9  */
10 
11 #ifndef __IMG3D_Manager__
12 #define __IMG3D_Manager__
13 
14 #include "IMG3D_API.h"
15 #include <UT/UT_BoundingBox.h>
16 #include <UT/UT_NonCopyable.h>
17 #include <UT/UT_Vector3.h>
18 #include <SYS/SYS_Types.h>
19 
20 class UT_String;
21 
22 /// Callback function for evaluating the values of a 3D texture at a particular
23 /// position in space. The callback takes:
24 /// - nvalues = the number of positions to evaluate
25 /// - pos = an array which is nvalues long containing the position to
26 /// evaluate.
27 /// - result = an array whose size is <tt>nvalues * sum(ch_size)</tt> long.
28 /// The resulting values should be interleaved. That is, all the
29 /// channel values for the first position, then all the channel
30 /// values for the second position, etc.
31 /// - ch_name = The array of channel names being evaluated
32 /// - ch_size = The array of channel sizes being evaluated.
33 /// - for_aa = Whether the samples being evaluated are primary or
34 /// anti-aliasing samples.
35 typedef void (*IMG3D_TextureEval)(int nvalues,
36  const UT_Vector3 *pos,
37  fpreal32 *result[],
38  const char *names[],
39  int sizes[],
40  int for_aa);
41 
42 typedef void (*IMG3D_Manager_Eval)(int nvalues,
43  const fpreal32 *pos,
44  fpreal32 *result[],
45  const char *names[],
46  int sizes[],
47  int for_aa);
48 
49 /// @brief Class to handle reading/writing 3D texture images
50 ///
51 /// A 3D texture file is similar to a 2D texture file, but with an added
52 /// dimension. The 3D texture is broken down into 3D tiles. Each tile is
53 /// stored independently in the file. This allows for partial loading of the
54 /// 3D texture (without having to load the entire image at one time).
55 ///
56 /// However, the tiled approach makes it difficult to create images since the
57 /// writer works on a single tile at a time.
58 ///
59 /// Just like image files, the 3D texture file can have an arbitrary number of
60 /// channels, each with its own name, storage and tuple size.
61 ///
62 /// In order to read an 3D texture, you have to open both an image and a
63 /// channel: @code
64 /// IMG3D_Manager fp;
65 /// int channel_index;
66 /// fp.openTexture(filename);
67 /// fp.openChannel(channel);
68 /// @endcode
70 {
71 public:
72  IMG3D_Manager();
73  ~IMG3D_Manager();
74 
76 
77  /// Close the file (whether opened for read or write).
78  /// This is done automatically on destruction.
79  int closeTexture();
80 
81 public:
82  /// Open a texture for reading. Returns 0 if there was an error opening
83  /// the file.
84  int openTexture(const char *filename);
85 
86  // Query functions, require the texture to be open (and specified
87  // channel indices to exist)
88 
89  // Size of the bounding box of the i3d.
90  // Null will be returned if the file has not been opened.
91 
92  /// Returns an array of 3 floats for xmin(), ymin(), zmin() representing
93  /// the left, bottom, front corner of the 3D texture.
94  const float *getCorner() const; // Left, bottom, front corner
95  /// Returns an array of 3 floats for xsize(), ysize(), zsize() representing
96  /// the size of the bounding box.
97  const float *getSize() const; // width, height, depth.
98  /// Returns the number of voxels in x, y, z.
99  const int *getRes() const; // Raw size in voxels. w, h, d
100 
101  /// Number of channels in the texture image
102  int getNChannels() const;
103 
104  /// Query channel names
105  const char *getChannelName(int idx) const;
106 
107  /// Return the number of floats in the given channel
108  int getChannelSize(int idx) const;
109 
110  /// Find the index of a named channel. Returns -1 if not found.
111  int getChannelNumber(const char *name) const;
112 
113  /// Once a texture is open, you must open a handle to a channel to be able
114  /// to access data in the channel. Returns 0 on failure or 1 on success.
115  /// @note Only a single channel can be open per manager.
116  int openChannel(const char *channel_name);
117 
118  /// Set the evaluation filter. This filter filter can be any one of the
119  /// standard Houdini filter types. The filtering is use for evaluation and
120  /// integration of the texture. Returns 0 on failure.
121  /// @see UT_Filter.
122  int setFilter(const char *filter_name, fpreal filter_width);
123 
124  /// Evaluate the 3D texture at the given position. Returns 0 on failure.
125  /// Uses the filter set by setFilter().
126  /// @note The @c result buffer should have enough storage to hold
127  /// getChannelSize().
128  int sample(const UT_Vector3 &pos, float *result);
129 
130  /// Evaluate the gradient of the 3D texture at the given position.
131  /// Uses the filter set by setFilter().
132  /// @note The gradient can only be computed on scalar channels.
133  /// @note 3 floats will be returned for a scalar channel.
134  int gradient(const UT_Vector3 &pos, float *result);
135 
136  /// Integrate from p0 to p1 and return the integration result.
137  /// Optional controls are:
138  /// @param p0 Start of integration
139  /// @param p1 End of integration
140  /// @param result
141  /// Integration result. This should large enough to hold
142  /// getChannelSize() floats.
143  /// @param limit_max if this value of the integration exceeds
144  /// this value, then the integration will be
145  /// terminated early.
146  /// @param value_scale scale the integration value by this amount.
147  /// This scaling is applied BEFORE limit checks.
148  /// @param accuracy
149  /// A scaling on stepping. Increasing the accuracy will perform
150  /// more sub-steps on the integration, yielding a more accurate
151  /// result (at a higher cost). Increasing the accuracy can have a
152  /// significant effect on performance.
153  ///
154  /// As a result of integration, p1 will be clipped to the bounds of the
155  /// texture. As well, p0 will be moved to the point where the limit was
156  /// reached (if the limit was reached).
157  int integrate(UT_Vector3 &p0, UT_Vector3 &p1, float *result,
158  fpreal limit_max=1, fpreal value_scale=1,
159  fpreal accuracy=0);
160 
161  /// Find the intersection against the iso-surface defined by density
162  /// specified. The accuracy is how close f(p0) will be to zero.
163  /// If an intersection is found, the function will return 1.
164  /// p0 will be moved to the intersection point (or to p1 if there was no
165  /// intersection found).
166  int intersect(UT_Vector3 &p0, const UT_Vector3 &p1,
167  fpreal density, fpreal accuracy = 1e-5F);
168 
169  /// Load a channel as a flat list of floats. The data is allocated using
170  /// malloc(). It's the users responsibility for freeing this memory using
171  /// free(). Given the getRes() function returning W, H, D (width, height,
172  /// depth), the array returned will be <tt>W*H*D * getChannelSize() *
173  /// sizeof(float)</tt> bytes long.
174  /// Given an index (ix, iy, iz), the channel data can be indexed by:
175  /// <tt>ix + iy*W + iz*W*H</tt>
176  float *loadUntiledChannel();
177 
178  /// Same as loadUntitledChannel() but fills a user-allocated array.
179  int loadUntiledChannel(float *data);
180 
181 public:
182  /// This method allows the creation of a 3D texture. The bounding box
183  /// defines the are where the map is sampled.
184  ///
185  /// Compression is the level of gzip compression to use. Valid values are
186  /// between 0 (no compression) and 9 (maximum compression).
187  int createTexture(
188  const char *filename,
189  const UT_BoundingBox &box,
190  int xres,
191  int yres,
192  int zres,
193  int compression=5);
194 
195  /// Once the texture is created, please call the following code to generate
196  /// the values for the texture.
197  /// @param num_channels The number of channels
198  /// @param channel_names An array of channel names
199  /// @param channel_sizes The number of floats in each channel.
200  /// @param evaluator Callback to evaluate channel positions
201  /// @param max_samples Anti-aliasing samples
202  /// @param variance Variance threshhold for anti-aliasing to occur
203  /// @param filter_width Anti-aliasing filter width
204  /// @param jitter Amount of jitter for anti-aliasing
205  /// @note currently, only box filtering is supported for anti-aliasing
206  /// @return 0 if the process fails.
207  int fillTexture(
208  int num_channels,
209  const char *channel_names[],
210  int channel_sizes[],
211  IMG3D_TextureEval evaluator,
212  int max_samples=4,
213  fpreal variance=0.005,
214  fpreal filter_width=1,
215  fpreal jitter=1);
216 
217  /// Save flat untiled data to the file. This method perfoms the inverse
218  /// operation of loadUntiledChannel, but for multiple channels.
219  /// @param num_channels - The number of channels
220  /// @param channel_names - An array of channel names
221  /// @param channel_sizes - The number of floats in each channel
222  /// @param data - Source data for each channel
223  /// @param variance - Ignored
224  ///
225  /// Given the @c getRes() function returning W, H, D (width, height,
226  /// depth), the data array for channel i must be
227  /// <tt>W*H*D * channel_sizes[i] * sizeof(float) </tt> bytes long.
228  /// Given an index (ix, iy, iz), the channel data can be indexed by:
229  /// <tt>ix + iy*W + iz*W*H</tt>
230  ///
231  /// @return 0 if the process fails.
232  int fillUntiledTexture(
233  int num_channels,
234  const char *channel_names[],
235  int channel_sizes[],
236  const float *data[],
237  fpreal variance=0.005);
238 
239  /// @{
240  ///
241  /// It's possible store and retrieve tags (based on the read/write mode of
242  /// the file). These tags can be queried using texinfo() in VEX.
243  ///
244  bool exportTag(const char *name, int value);
245  bool exportTag(const char *name, float value);
246  bool exportTag(const char *name, const UT_Vector3 &value);
247  bool exportTag(const char *name, const UT_Vector4 &value);
248  bool exportTag(const char *name, const UT_Matrix3 &value);
249  bool exportTag(const char *name, const UT_Matrix4 &value);
250  bool exportTag(const char *name, const char *value);
251  /// @}
252 
253  /// @{
254  /// When importing, if the type doesn't match, casting will be done (where
255  /// possible). Importing a string is always possible. If the import
256  /// fails, the value will be untouched and the import will return false.
257  bool importTag(const char *name, int &value);
258  bool importTag(const char *name, float &value);
259  bool importTag(const char *name, UT_Vector3 &value);
260  bool importTag(const char *name, UT_Vector4 &value);
261  bool importTag(const char *name, UT_Matrix3 &value);
262  bool importTag(const char *name, UT_Matrix4 &value);
263  bool importTag(const char *name, UT_String &value);
264  /// @}
265 
266 public:
267  // Deprecated methods that take floating point parameters. You should
268  // use the new methods above.
269  /// @private - Please use the UT_Vector3 version
270  int SYS_DEPRECATED(8.2) sample(const float *pos, float *result)
271  { return sample(*(UT_Vector3 *)pos, result); }
272  /// @private - Please use the UT_Vector3 version
273  int SYS_DEPRECATED(8.2) gradient(const float *pos, float *result)
274  { return gradient(*(UT_Vector3 *)pos, result); }
275  /// @private - Please use the UT_Vector3 version
276  int SYS_DEPRECATED(8.2) integrate(float *p0, float *p1, float *result,
277  fpreal limit_max=1, fpreal value_scale=1,
278  fpreal accuracy=0)
279  { return integrate(*(UT_Vector3 *)p0, *(UT_Vector3 *)p1,
280  result, limit_max, value_scale, accuracy); }
281  /// @private - Please use the UT_Vector3 version
282  int SYS_DEPRECATED(8.2) intersect(float *p0, const float *p1,
283  fpreal density, fpreal accuracy = 1e-5F)
284  { return intersect(*(UT_Vector3 *)p0, *(UT_Vector3 *)p1,
285  density, accuracy); }
286  /// @private - Please use the UT_BoundingBox version
287  int SYS_DEPRECATED(8.2) createTexture(
288  const char *filename,
289  const float box[3][2],
290  int xres,
291  int yres,
292  int zres,
293  int compression=5)
294  {
295  return createTexture(filename, UT_BoundingBox(
296  box[0][0], box[1][0], box[2][0],
297  box[0][1], box[1][1], box[2][1]),
298  xres, yres, zres, compression);
299  }
300  /// @private - Please use the IMG3D_TextureEval version
301  int SYS_DEPRECATED(8.2) fillTexture(
302  int num_channels,
303  const char *channel_names[],
304  int channel_sizes[],
305  IMG3D_Manager_Eval evaluator,
306  int max_samples=4,
307  fpreal variance=0.005,
308  fpreal filter_width=1,
309  fpreal jitter=1);
310 
311 protected:
312  void *myData; // Internal data structure
313 };
314 
315 #endif
GLuint GLsizei const GLuint const GLintptr const GLsizeiptr * sizes
Definition: glcorearb.h:2621
void(* IMG3D_TextureEval)(int nvalues, const UT_Vector3 *pos, fpreal32 *result[], const char *names[], int sizes[], int for_aa)
Definition: IMG3D_Manager.h:35
GT_API const UT_StringHolder filename
#define SYS_DEPRECATED(__V__)
void
Definition: png.h:1083
**But if you need a result
Definition: thread.h:613
float fpreal32
Definition: SYS_Types.h:200
UT_BoundingBoxT< float > UT_BoundingBox
Definition: GEO_Detail.h:41
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
GLuint const GLchar * name
Definition: glcorearb.h:786
fpreal64 fpreal
Definition: SYS_Types.h:277
ScalarToVectorConverter< GridType >::Type::Ptr gradient(const GridType &grid, bool threaded, InterruptT *interrupt)
Compute the gradient of the given scalar grid.
IMATH_CONSTEXPR14 bool intersect(const Line3< T > &line, const Vec3< T > &v0, const Vec3< T > &v1, const Vec3< T > &v2, Vec3< T > &pt, Vec3< T > &barycentric, bool &front) IMATH_NOEXCEPT
Definition: ImathLineAlgo.h:80
void(* IMG3D_Manager_Eval)(int nvalues, const fpreal32 *pos, fpreal32 *result[], const char *names[], int sizes[], int for_aa)
Definition: IMG3D_Manager.h:42
#define IMG3D_API
Definition: IMG3D_API.h:10
Definition: core.h:1131
Class to handle reading/writing 3D texture images.
Definition: IMG3D_Manager.h:69
Definition: format.h:895