GEL  2
GEL is a library for Geometry and Linear Algebra
/Users/jab/Documents/Teaching/02585/GEL2_and_demos/GEL/src/Geometry/rply.h
00001 #ifndef __GEOMETRY_PLY_H
00002 #define __GEOMETRY_PLY_H
00003 /* ----------------------------------------------------------------------
00004  * RPly library, read/write PLY files
00005  * Diego Nehab, Princeton University
00006  * http://www.cs.princeton.edu/~diego/professional/rply
00007  *
00008  * This library is distributed under the MIT License. See notice
00009  * at the end of this file.
00010  * ---------------------------------------------------------------------- */
00011 
00012 #ifdef __cplusplus
00013 extern "C" {
00014 #endif
00015 
00016 #define RPLY_VERSION   "RPly 1.01"
00017 #define RPLY_COPYRIGHT "Copyright (C) 2003-2005 Diego Nehab"
00018 #define RPLY_AUTHORS   "Diego Nehab"
00019 
00020 /* ----------------------------------------------------------------------
00021  * Types
00022  * ---------------------------------------------------------------------- */
00023 /* structures are opaque */
00024 typedef struct t_ply_ *p_ply;
00025 typedef struct t_ply_element_ *p_ply_element;
00026 typedef struct t_ply_property_ *p_ply_property;
00027 typedef struct t_ply_argument_ *p_ply_argument;
00028 
00029 /* ply format mode type */
00030 typedef enum e_ply_storage_mode_ {
00031     PLY_BIG_ENDIAN,
00032     PLY_LITTLE_ENDIAN,
00033     PLY_ASCII,   
00034     PLY_DEFAULT      /* has to be the last in enum */
00035 } e_ply_storage_mode; /* order matches ply_storage_mode_list */
00036 
00037 /* ply data type */
00038 typedef enum e_ply_type {
00039     PLY_INT8, PLY_UINT8, PLY_INT16, PLY_UINT16, 
00040     PLY_INT32, PLY_UIN32, PLY_FLOAT32, PLY_FLOAT64,
00041     PLY_CHAR, PLY_UCHAR, PLY_SHORT, PLY_USHORT,
00042     PLY_INT, PLY_UINT, PLY_FLOAT, PLY_DOUBLE,
00043     PLY_LIST    /* has to be the last in enum */
00044 } e_ply_type;   /* order matches ply_type_list */
00045 
00046 /* ----------------------------------------------------------------------
00047  * Property reading callback prototype
00048  *
00049  * message: error message
00050  * ---------------------------------------------------------------------- */
00051 typedef void (*p_ply_error_cb)(const char *message);
00052 
00053 /* ----------------------------------------------------------------------
00054  * Opens a ply file for reading (fails if file is not a ply file)
00055  *
00056  * error_cb: error callback function
00057  * name: file name
00058  *
00059  * Returns 1 if successful, 0 otherwise
00060  * ---------------------------------------------------------------------- */
00061 p_ply ply_open(const char *name, p_ply_error_cb error_cb);
00062 
00063 /* ----------------------------------------------------------------------
00064  * Reads and parses the header of a ply file returned by ply_open
00065  *
00066  * ply: handle returned by ply_open
00067  *
00068  * Returns 1 if successfull, 0 otherwise
00069  * ---------------------------------------------------------------------- */
00070 int ply_read_header(p_ply ply);
00071 
00072 /* ----------------------------------------------------------------------
00073  * Property reading callback prototype
00074  *
00075  * argument: parameters for property being processed when callback is called
00076  *
00077  * Returns 1 if should continue processing file, 0 if should abort.
00078  * ---------------------------------------------------------------------- */
00079 typedef int (*p_ply_read_cb)(p_ply_argument argument);
00080 
00081 /* ----------------------------------------------------------------------
00082  * Sets up callbacks for property reading after header was parsed
00083  *
00084  * ply: handle returned by ply_open
00085  * element_name: element where property is
00086  * property_name: property to associate element with
00087  * read_cb: function to be called for each property value
00088  * pdata/idata: user data that will be passed to callback
00089  *
00090  * Returns 0 if no element or no property in element, returns the
00091  * number of element instances otherwise. 
00092  * ---------------------------------------------------------------------- */
00093 int ply_set_read_cb(p_ply ply, const char *element_name, 
00094         const char *property_name, p_ply_read_cb read_cb, 
00095         void *pdata, int idata);
00096 
00097 /* ----------------------------------------------------------------------
00098  * Returns information about the element originating a callback
00099  *
00100  * argument: handle to argument 
00101  * element: receives a the element handle (if non-null)
00102  * instance_index: receives the index of the current element instance 
00103  *     (if non-null)
00104  *
00105  * Returns 1 if successfull, 0 otherwise
00106  * ---------------------------------------------------------------------- */
00107 int ply_get_argument_element(p_ply_argument argument, 
00108         p_ply_element *element, int *instance_index);
00109 
00110 /* ----------------------------------------------------------------------
00111  * Returns information about the property originating a callback
00112  *
00113  * argument: handle to argument 
00114  * property: receives the property handle (if non-null)
00115  * length: receives the number of values in this property (if non-null)
00116  * value_index: receives the index of current property value (if non-null)
00117  *
00118  * Returns 1 if successfull, 0 otherwise
00119  * ---------------------------------------------------------------------- */
00120 int ply_get_argument_property(p_ply_argument argument, 
00121         p_ply_property *property, int *length, int *value_index);
00122 
00123 /* ----------------------------------------------------------------------
00124  * Returns user data associated with callback 
00125  *
00126  * pdata: receives a copy of user custom data pointer (if non-null)
00127  * idata: receives a copy of user custom data integer (if non-null)
00128  *
00129  * Returns 1 if successfull, 0 otherwise
00130  * ---------------------------------------------------------------------- */
00131 int ply_get_argument_user_data(p_ply_argument argument, void **pdata, 
00132         int *idata);
00133 
00134 /* ----------------------------------------------------------------------
00135  * Returns the value associated with a callback
00136  *
00137  * argument: handle to argument 
00138  *
00139  * Returns the current data item
00140  * ---------------------------------------------------------------------- */
00141 double ply_get_argument_value(p_ply_argument argument); 
00142 
00143 /* ----------------------------------------------------------------------
00144  * Reads all elements and properties calling the callbacks defined with
00145  * calls to ply_set_read_cb
00146  *
00147  * ply: handle returned by ply_open
00148  *
00149  * Returns 1 if successfull, 0 otherwise
00150  * ---------------------------------------------------------------------- */
00151 int ply_read(p_ply ply);
00152 
00153 /* ----------------------------------------------------------------------
00154  * Iterates over all elements by returning the next element.
00155  * Call with NULL to return handle to first element.
00156  *
00157  * ply: handle returned by ply_open
00158  * last: handle of last element returned (NULL for first element)
00159  *
00160  * Returns element if successfull or NULL if no more elements
00161  * ---------------------------------------------------------------------- */
00162 p_ply_element ply_get_next_element(p_ply ply, p_ply_element last);
00163 
00164 /* ----------------------------------------------------------------------
00165  * Iterates over all comments by returning the next comment.
00166  * Call with NULL to return pointer to first comment.
00167  *
00168  * ply: handle returned by ply_open
00169  * last: pointer to last comment returned (NULL for first comment)
00170  *
00171  * Returns comment if successfull or NULL if no more comments
00172  * ---------------------------------------------------------------------- */
00173 const char *ply_get_next_comment(p_ply ply, const char *last);
00174 
00175 /* ----------------------------------------------------------------------
00176  * Iterates over all obj_infos by returning the next obj_info.
00177  * Call with NULL to return pointer to first obj_info.
00178  *
00179  * ply: handle returned by ply_open
00180  * last: pointer to last obj_info returned (NULL for first obj_info)
00181  *
00182  * Returns obj_info if successfull or NULL if no more obj_infos
00183  * ---------------------------------------------------------------------- */
00184 const char *ply_get_next_obj_info(p_ply ply, const char *last);
00185 
00186 /* ----------------------------------------------------------------------
00187  * Returns information about an element
00188  *
00189  * element: element of interest
00190  * name: receives a pointer to internal copy of element name (if non-null)
00191  * ninstances: receives the number of instances of this element (if non-null)
00192  *
00193  * Returns 1 if successfull or 0 otherwise
00194  * ---------------------------------------------------------------------- */
00195 int ply_get_element_info(p_ply_element element, const char** name,
00196         int *ninstances);
00197 
00198 /* ----------------------------------------------------------------------
00199  * Iterates over all properties by returning the next property.
00200  * Call with NULL to return handle to first property.
00201  *
00202  * element: handle of element with the properties of interest
00203  * last: handle of last property returned (NULL for first property)
00204  *
00205  * Returns element if successfull or NULL if no more properties
00206  * ---------------------------------------------------------------------- */
00207 p_ply_property ply_get_next_property(p_ply_element element, 
00208         p_ply_property last);
00209 
00210 /* ----------------------------------------------------------------------
00211  * Returns information about a property
00212  *
00213  * property: handle to property of interest
00214  * name: receives a pointer to internal copy of property name (if non-null)
00215  * type: receives the property type (if non-null)
00216  * length_type: for list properties, receives the scalar type of
00217  *     the length field (if non-null)
00218  * value_type: for list properties, receives the scalar type of the value 
00219  *     fields  (if non-null)
00220  *
00221  * Returns 1 if successfull or 0 otherwise
00222  * ---------------------------------------------------------------------- */
00223 int ply_get_property_info(p_ply_property property, const char** name,
00224         e_ply_type *type, e_ply_type *length_type, e_ply_type *value_type);
00225 
00226 /* ----------------------------------------------------------------------
00227  * Creates new ply file
00228  *
00229  * name: file name
00230  * storage_mode: file format mode
00231  *
00232  * Returns handle to ply file if successfull, NULL otherwise
00233  * ---------------------------------------------------------------------- */
00234 p_ply ply_create(const char *name, e_ply_storage_mode storage_mode, 
00235         p_ply_error_cb error_cb);
00236 
00237 /* ----------------------------------------------------------------------
00238  * Adds a new element to the ply file created by ply_create
00239  *
00240  * ply: handle returned by ply_create
00241  * name: name of new element
00242  * ninstances: number of element of this time in file
00243  *
00244  * Returns 1 if successfull, 0 otherwise
00245  * ---------------------------------------------------------------------- */
00246 int ply_add_element(p_ply ply, const char *name, int ninstances);
00247 
00248 /* ----------------------------------------------------------------------
00249  * Adds a new property to the last element added by ply_add_element
00250  *
00251  * ply: handle returned by ply_create
00252  * name: name of new property
00253  * type: property type
00254  * length_type: scalar type of length field of a list property 
00255  * value_type: scalar type of value fields of a list property
00256  *
00257  * Returns 1 if successfull, 0 otherwise
00258  * ---------------------------------------------------------------------- */
00259 int ply_add_property(p_ply ply, const char *name, e_ply_type type,
00260         e_ply_type length_type, e_ply_type value_type);
00261 
00262 /* ----------------------------------------------------------------------
00263  * Adds a new list property to the last element added by ply_add_element
00264  *
00265  * ply: handle returned by ply_create
00266  * name: name of new property
00267  * length_type: scalar type of length field of a list property 
00268  * value_type: scalar type of value fields of a list property
00269  *
00270  * Returns 1 if successfull, 0 otherwise
00271  * ---------------------------------------------------------------------- */
00272 int ply_add_list_property(p_ply ply, const char *name, 
00273         e_ply_type length_type, e_ply_type value_type);
00274 
00275 /* ----------------------------------------------------------------------
00276  * Adds a new property to the last element added by ply_add_element
00277  *
00278  * ply: handle returned by ply_create
00279  * name: name of new property
00280  * type: property type
00281  *
00282  * Returns 1 if successfull, 0 otherwise
00283  * ---------------------------------------------------------------------- */
00284 int ply_add_scalar_property(p_ply ply, const char *name, e_ply_type type);
00285 
00286 /* ----------------------------------------------------------------------
00287  * Adds a new comment item 
00288  *
00289  * ply: handle returned by ply_create
00290  * comment: pointer to string with comment text
00291  *
00292  * Returns 1 if successfull, 0 otherwise
00293  * ---------------------------------------------------------------------- */
00294 int ply_add_comment(p_ply ply, const char *comment);
00295 
00296 /* ----------------------------------------------------------------------
00297  * Adds a new obj_info item 
00298  *
00299  * ply: handle returned by ply_create
00300  * comment: pointer to string with obj_info data
00301  *
00302  * Returns 1 if successfull, 0 otherwise
00303  * ---------------------------------------------------------------------- */
00304 int ply_add_obj_info(p_ply ply, const char *obj_info);
00305 
00306 /* ----------------------------------------------------------------------
00307  * Writes the ply file header after all element and properties have been
00308  * defined by calls to ply_add_element and ply_add_property
00309  *
00310  * ply: handle returned by ply_create
00311  *
00312  * Returns 1 if successfull, 0 otherwise
00313  * ---------------------------------------------------------------------- */
00314 int ply_write_header(p_ply ply);
00315 
00316 /* ----------------------------------------------------------------------
00317  * Writes one property value, in the order they should be written to the
00318  * file. For each element type, write all elements of that type in order.
00319  * For each element, write all its properties in order. For scalar
00320  * properties, just write the value. For list properties, write the length 
00321  * and then each of the values.
00322  *
00323  * ply: handle returned by ply_create
00324  *
00325  * Returns 1 if successfull, 0 otherwise
00326  * ---------------------------------------------------------------------- */
00327 int ply_write(p_ply ply, double value);
00328 
00329 /* ----------------------------------------------------------------------
00330  * Closes a ply file handle. Releases all memory used by handle
00331  *
00332  * ply: handle to be closed. 
00333  *
00334  * Returns 1 if successfull, 0 otherwise
00335  * ---------------------------------------------------------------------- */
00336 int ply_close(p_ply ply);
00337 
00338 #ifdef __cplusplus
00339 }
00340 #endif
00341 
00342 #endif /* RPLY_H */
00343 
00344 /* ----------------------------------------------------------------------
00345  * Copyright (C) 2003-2005 Diego Nehab. All rights reserved.
00346  *
00347  * Permission is hereby granted, free of charge, to any person obtaining
00348  * a copy of this software and associated documentation files (the
00349  * "Software"), to deal in the Software without restriction, including
00350  * without limitation the rights to use, copy, modify, merge, publish,
00351  * distribute, sublicense, and/or sell copies of the Software, and to
00352  * permit persons to whom the Software is furnished to do so, subject to
00353  * the following conditions:
00354  *
00355  * The above copyright notice and this permission notice shall be
00356  * included in all copies or substantial portions of the Software.
00357  *
00358  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00359  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00360  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00361  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
00362  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
00363  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00364  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00365  * ---------------------------------------------------------------------- */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations