00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Mark Elendt 00008 * Side Effects Software Inc 00009 * 477 Richmond Street West 00010 * Toronto, Ontario 00011 * Canada M5V 3E7 00012 * 416-504-9876 00013 * 00014 * NAME: IMG3D_Manager.h ( IMG3D Library, C++) 00015 * 00016 * COMMENTS: API into the 3D texture library. 00017 */ 00018 00019 #ifndef __IMG3D_Manager__ 00020 #define __IMG3D_Manager__ 00021 00022 #include "IMG3D_API.h" 00023 #include <SYS/SYS_Types.h> 00024 #include <UT/UT_Vector3.h> 00025 #include <UT/UT_BoundingBox.h> 00026 00027 class UT_String; 00028 00029 /// Callback function for evaluating the values of a 3D texture at a particular 00030 /// position in space. The callback takes: 00031 /// - nvalues = the number of positions to evaluate 00032 /// - pos = an array which is nvalues long containing the position to 00033 /// evaluate. 00034 /// - result = an array whose size is <tt>nvalues * sum(ch_size)</tt> long. 00035 /// The resulting values should be interleaved. That is, all the 00036 /// channel values for the first position, then all the channel 00037 /// values for the second position, etc. 00038 /// - ch_name = The array of channel names being evaluated 00039 /// - ch_size = The array of channel sizes being evaluated. 00040 /// - for_aa = Whether the samples being evaluated are primary or 00041 /// anti-aliasing samples. 00042 typedef void (*IMG3D_TextureEval)(int nvalues, 00043 const UT_Vector3 *pos, 00044 fpreal32 *result[], 00045 const char *names[], 00046 int sizes[], 00047 int for_aa); 00048 00049 typedef void (*IMG3D_Manager_Eval)(int nvalues, 00050 const fpreal32 *pos, 00051 fpreal32 *result[], 00052 const char *names[], 00053 int sizes[], 00054 int for_aa); 00055 00056 /// @brief Class to handle reading/writing 3D texture images 00057 /// 00058 /// A 3D texture file is similar to a 2D texture file, but with an added 00059 /// dimension. The 3D texture is broken down into 3D tiles. Each tile is 00060 /// stored independently in the file. This allows for partial loading of the 00061 /// 3D texture (without having to load the entire image at one time). 00062 /// 00063 /// However, the tiled approach makes it difficult to create images since the 00064 /// writer works on a single tile at a time. 00065 /// 00066 /// Just like image files, the 3D texture file can have an arbitrary number of 00067 /// channels, each with its own name, storage and tuple size. 00068 /// 00069 /// In order to read an 3D texture, you have to open both an image and a 00070 /// channel: @code 00071 /// IMG3D_Manager fp; 00072 /// int channel_index; 00073 /// fp.openTexture(filename); 00074 /// fp.openChannel(channel); 00075 /// @endcode 00076 class IMG3D_API IMG3D_Manager { 00077 public: 00078 IMG3D_Manager(); 00079 ~IMG3D_Manager(); 00080 00081 /// Close the file (whether opened for read or write). 00082 /// This is done automatically on destruction. 00083 int closeTexture(); 00084 00085 public: 00086 /// Open a texture for reading. Returns 0 if there was an error opening 00087 /// the file. 00088 int openTexture(const char *filename); 00089 00090 // Query functions, require the texture to be open (and specified 00091 // channel indices to exist) 00092 00093 // Size of the bounding box of the i3d. 00094 // Null will be returned if the file has not been opened. 00095 00096 /// Returns an array of 3 floats for xmin(), ymin(), zmin() representing 00097 /// the left, bottom, front corner of the 3D texture. 00098 const float *getCorner() const; // Left, bottom, front corner 00099 /// Returns an array of 3 floats for xsize(), ysize(), zsize() representing 00100 /// the size of the bounding box. 00101 const float *getSize() const; // width, height, depth. 00102 /// Returns the number of voxels in x, y, z. 00103 const int *getRes() const; // Raw size in voxels. w, h, d 00104 00105 /// Number of channels in the texture image 00106 int getNChannels() const; 00107 00108 /// Query channel names 00109 const char *getChannelName(int idx) const; 00110 00111 /// Return the number of floats in the given channel 00112 int getChannelSize(int idx) const; 00113 00114 /// Find the index of a named channel. Returns -1 if not found. 00115 int getChannelNumber(const char *name) const; 00116 00117 /// Once a texture is open, you must open a handle to a channel to be able 00118 /// to access data in the channel. Returns 0 on failure or 1 on success. 00119 /// @note Only a single channel can be open per manager. 00120 int openChannel(const char *channel_name); 00121 00122 /// Set the evaluation filter. This filter filter can be any one of the 00123 /// standard Houdini filter types. The filtering is use for evaluation and 00124 /// integration of the texture. Returns 0 on failure. 00125 /// @see UT_Filter. 00126 int setFilter(const char *filter_name, fpreal filter_width); 00127 00128 /// Evaluate the 3D texture at the given position. Returns 0 on failure. 00129 /// Uses the filter set by setFilter(). 00130 /// @note The @c result buffer should have enough storage to hold 00131 /// getChannelSize(). 00132 int sample(const UT_Vector3 &pos, float *result); 00133 00134 /// Evaluate the gradient of the 3D texture at the given position. 00135 /// Uses the filter set by setFilter(). 00136 /// @note The gradient can only be computed on scalar channels. 00137 /// @note 3 floats will be returned for a scalar channel. 00138 int gradient(const UT_Vector3 &pos, float *result); 00139 00140 /// Integrate from p0 to p1 and return the integration result. 00141 /// Optional controls are: 00142 /// @param p0 Start of integration 00143 /// @param p1 End of integration 00144 /// @param result 00145 /// Integration result. This should large enough to hold 00146 /// getChannelSize() floats. 00147 /// @param limit_max if this value of the integration exceeds 00148 /// this value, then the integration will be 00149 /// terminated early. 00150 /// @param value_scale scale the integration value by this amount. 00151 /// This scaling is applied BEFORE limit checks. 00152 /// @param accuracy 00153 /// A scaling on stepping. Increasing the accuracy will perform 00154 /// more sub-steps on the integration, yielding a more accurate 00155 /// result (at a higher cost). Increasing the accuracy can have a 00156 /// significant effect on performance. 00157 /// 00158 /// As a result of integration, p1 will be clipped to the bounds of the 00159 /// texture. As well, p0 will be moved to the point where the limit was 00160 /// reached (if the limit was reached). 00161 int integrate(UT_Vector3 &p0, UT_Vector3 &p1, float *result, 00162 fpreal limit_max=1, fpreal value_scale=1, 00163 fpreal accuracy=0); 00164 00165 /// Find the intersection against the iso-surface defined by density 00166 /// specified. The accuracy is how close f(p0) will be to zero. 00167 /// If an intersection is found, the function will return 1. 00168 /// p0 will be moved to the intersection point (or to p1 if there was no 00169 /// intersection found). 00170 int intersect(UT_Vector3 &p0, const UT_Vector3 &p1, 00171 fpreal density, fpreal accuracy = 1e-5F); 00172 00173 /// Load a channel as a flat list of floats. The data is allocated using 00174 /// malloc(). It's the users responsibility for freeing this memory using 00175 /// free(). Given the getRes() function returning W, H, D (width, height, 00176 /// depth), the array returned will be <tt>W*H*D * getChannelSize() * 00177 /// sizeof(float)</tt> bytes long. 00178 /// Given an index (ix, iy, iz), the channel data can be indexed by: 00179 /// <tt>ix + iy*W + iz*W*H</tt> 00180 float *loadUntiledChannel(); 00181 00182 /// Same as loadUntitledChannel() but fills a user-allocated array. 00183 int loadUntiledChannel(float *data); 00184 00185 public: 00186 /// This method allows the creation of a 3D texture. The bounding box 00187 /// defines the are where the map is sampled. 00188 /// 00189 /// Compression is the level of gzip compression to use. Valid values are 00190 /// between 0 (no compression) and 9 (maximum compression). 00191 int createTexture( 00192 const char *filename, 00193 const UT_BoundingBox &box, 00194 int xres, 00195 int yres, 00196 int zres, 00197 int compression=5); 00198 00199 /// Once the texture is created, please call the following code to generate 00200 /// the values for the texture. 00201 /// @param num_channels The number of channels 00202 /// @param channel_names An array of channel names 00203 /// @param channel_sizes The number of floats in each channel. 00204 /// @param evaluator Callback to evaluate channel positions 00205 /// @param max_samples Anti-aliasing samples 00206 /// @param variance Variance threshhold for anti-aliasing to occur 00207 /// @param filter_width Anti-aliasing filter width 00208 /// @param jitter Amount of jitter for anti-aliasing 00209 /// @note currently, only box filtering is supported for anti-aliasing 00210 /// @return 0 if the process fails. 00211 int fillTexture( 00212 int num_channels, 00213 const char *channel_names[], 00214 int channel_sizes[], 00215 IMG3D_TextureEval evaluator, 00216 int max_samples=4, 00217 fpreal variance=0.005, 00218 fpreal filter_width=1, 00219 fpreal jitter=1); 00220 00221 /// Save flat untiled data to the file. This method perfoms the inverse 00222 /// operation of loadUntiledChannel, but for multiple channels. 00223 /// @param num_channels - The number of channels 00224 /// @param channel_names - An array of channel names 00225 /// @param channel_sizes - The number of floats in each channel 00226 /// @param data - Source data for each channel 00227 /// @param varaiance - Ignored 00228 /// 00229 /// Given the @c getRes() function returning W, H, D (width, height, 00230 /// depth), the data array for channel i must be 00231 /// <tt>W*H*D * channel_sizes[i] * sizeof(float) </tt> bytes long. 00232 /// Given an index (ix, iy, iz), the channel data can be indexed by: 00233 /// <tt>ix + iy*W + iz*W*H</tt> 00234 /// 00235 /// @return 0 if the process fails. 00236 int fillUntiledTexture( 00237 int num_channels, 00238 const char *channel_names[], 00239 int channel_sizes[], 00240 const float *data[], 00241 fpreal variance=0.005); 00242 00243 /// @{ 00244 /// 00245 /// It's possible store and retrieve tags (based on the read/write mode of 00246 /// the file). These tags can be queried using texinfo() in VEX. 00247 /// 00248 bool exportTag(const char *name, int value); 00249 bool exportTag(const char *name, float value); 00250 bool exportTag(const char *name, const UT_Vector3 &value); 00251 bool exportTag(const char *name, const UT_Vector4 &value); 00252 bool exportTag(const char *name, const UT_Matrix3 &value); 00253 bool exportTag(const char *name, const UT_Matrix4 &value); 00254 bool exportTag(const char *name, const char *value); 00255 /// @} 00256 00257 /// @{ 00258 /// When importing, if the type doesn't match, casting will be done (where 00259 /// possible). Importing a string is always possible. If the import 00260 /// fails, the value will be untouched and the import will return false. 00261 bool importTag(const char *name, int &value); 00262 bool importTag(const char *name, float &value); 00263 bool importTag(const char *name, UT_Vector3 &value); 00264 bool importTag(const char *name, UT_Vector4 &value); 00265 bool importTag(const char *name, UT_Matrix3 &value); 00266 bool importTag(const char *name, UT_Matrix4 &value); 00267 bool importTag(const char *name, UT_String &value); 00268 /// @} 00269 00270 public: 00271 // Deprecated methods that take floating point parameters. You should 00272 // use the new methods above. 00273 /// @private - Please use the UT_Vector3 version 00274 int SYS_DEPRECATED sample(const float *pos, float *result) 00275 { return sample(*(UT_Vector3 *)pos, result); } 00276 /// @private - Please use the UT_Vector3 version 00277 int SYS_DEPRECATED gradient(const float *pos, float *result) 00278 { return gradient(*(UT_Vector3 *)pos, result); } 00279 /// @private - Please use the UT_Vector3 version 00280 int SYS_DEPRECATED integrate(float *p0, float *p1, float *result, 00281 fpreal limit_max=1, fpreal value_scale=1, 00282 fpreal accuracy=0) 00283 { return integrate(*(UT_Vector3 *)p0, *(UT_Vector3 *)p1, 00284 result, limit_max, value_scale, accuracy); } 00285 /// @private - Please use the UT_Vector3 version 00286 int SYS_DEPRECATED intersect(float *p0, const float *p1, 00287 fpreal density, fpreal accuracy = 1e-5F) 00288 { return intersect(*(UT_Vector3 *)p0, *(UT_Vector3 *)p1, 00289 density, accuracy); } 00290 /// @private - Please use the UT_BoundingBox version 00291 int SYS_DEPRECATED createTexture( 00292 const char *filename, 00293 const float box[3][2], 00294 int xres, 00295 int yres, 00296 int zres, 00297 int compression=5) 00298 { 00299 return createTexture(filename, UT_BoundingBox( 00300 box[0][0], box[1][0], box[2][0], 00301 box[0][1], box[1][1], box[2][1]), 00302 xres, yres, zres, compression); 00303 } 00304 /// @private - Please use the IMG3D_TextureEval version 00305 int SYS_DEPRECATED fillTexture( 00306 int num_channels, 00307 const char *channel_names[], 00308 int channel_sizes[], 00309 IMG3D_Manager_Eval evaluator, 00310 int max_samples=4, 00311 fpreal variance=0.005, 00312 fpreal filter_width=1, 00313 fpreal jitter=1); 00314 00315 protected: 00316 void *myData; // Internal data structure 00317 }; 00318 00319 #endif
1.5.9