rply.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #ifndef PLY_H
  2. #define PLY_H
  3. /* ----------------------------------------------------------------------
  4. * RPly library, read/write PLY files
  5. * Diego Nehab, IMPA
  6. * http://www.impa.br/~diego/software/rply
  7. *
  8. * This library is distributed under the MIT License. See notice
  9. * at the end of this file.
  10. *
  11. * Modifications:
  12. * - DGM (25/01/06) - get_plystorage_mode method added
  13. *
  14. * ---------------------------------------------------------------------- */
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #define RPLY_VERSION "RPly 1.1.1"
  19. #define RPLY_COPYRIGHT "Copyright (C) 2003-2011 Diego Nehab"
  20. #define RPLY_AUTHORS "Diego Nehab"
  21. /* ----------------------------------------------------------------------
  22. * Types
  23. * ---------------------------------------------------------------------- */
  24. /* structures are opaque */
  25. typedef struct t_ply_ *p_ply;
  26. typedef struct t_ply_element_ *p_ply_element;
  27. typedef struct t_ply_property_ *p_ply_property;
  28. typedef struct t_ply_argument_ *p_ply_argument;
  29. /* ply format mode type */
  30. typedef enum e_ply_storage_mode_ {
  31. PLY_BIG_ENDIAN,
  32. PLY_LITTLE_ENDIAN,
  33. PLY_ASCII,
  34. PLY_DEFAULT /* has to be the last in enum */
  35. } e_ply_storage_mode; /* order matches ply_storage_mode_list */
  36. /* ply data type */
  37. typedef enum e_ply_type {
  38. PLY_INT8, PLY_UINT8, PLY_INT16, PLY_UINT16,
  39. PLY_INT32, PLY_UIN32, PLY_FLOAT32, PLY_FLOAT64,
  40. PLY_CHAR, PLY_UCHAR, PLY_SHORT, PLY_USHORT,
  41. PLY_INT, PLY_UINT, PLY_FLOAT, PLY_DOUBLE,
  42. PLY_LIST /* has to be the last in enum */
  43. } e_ply_type; /* order matches ply_type_list */
  44. /* ----------------------------------------------------------------------
  45. * Error callback prototype
  46. *
  47. * message: error message
  48. * ply: handle returned by ply_open or ply_create
  49. * ---------------------------------------------------------------------- */
  50. typedef void (*p_ply_error_cb)(p_ply ply, const char *message);
  51. /* ----------------------------------------------------------------------
  52. * Gets user data from within an error callback
  53. *
  54. * ply: handle returned by ply_open or ply_create
  55. * idata,pdata: contextual information set in ply_open or ply_create
  56. * ---------------------------------------------------------------------- */
  57. int ply_get_ply_user_data(p_ply ply, void **pdata, long *idata);
  58. /* ----------------------------------------------------------------------
  59. * Opens a PLY file for reading (fails if file is not a PLY file)
  60. *
  61. * name: file name
  62. * error_cb: error callback function
  63. * idata,pdata: contextual information available to users
  64. *
  65. * Returns 1 if successful, 0 otherwise
  66. * ---------------------------------------------------------------------- */
  67. #ifdef _MSC_VER
  68. p_ply ply_open(const wchar_t* name,
  69. #else
  70. p_ply ply_open(const char *name,
  71. #endif
  72. p_ply_error_cb error_cb,
  73. long idata,
  74. void *pdata);
  75. /* ----------------------------------------------------------------------
  76. * Reads and parses the header of a PLY file returned by ply_open
  77. *
  78. * ply: handle returned by ply_open
  79. *
  80. * Returns 1 if successful, 0 otherwise
  81. * ---------------------------------------------------------------------- */
  82. int ply_read_header(p_ply ply);
  83. /* ----------------------------------------------------------------------
  84. * Property reading callback prototype
  85. *
  86. * argument: parameters for property being processed when callback is called
  87. *
  88. * Returns 1 if should continue processing file, 0 if should abort.
  89. * ---------------------------------------------------------------------- */
  90. typedef int (*p_ply_read_cb)(p_ply_argument argument);
  91. /* ----------------------------------------------------------------------
  92. * Sets up callbacks for property reading after header was parsed
  93. *
  94. * ply: handle returned by ply_open
  95. * element_name: element where property is
  96. * property_name: property to associate element with
  97. * read_cb: function to be called for each property value
  98. * pdata/idata: user data that will be passed to callback
  99. *
  100. * Returns 0 if no element or no property in element, returns the
  101. * number of element instances otherwise.
  102. * ---------------------------------------------------------------------- */
  103. long ply_set_read_cb(p_ply ply, const char *element_name,
  104. const char *property_name, p_ply_read_cb read_cb,
  105. void *pdata, long idata);
  106. /* ----------------------------------------------------------------------
  107. * Returns information about the element originating a callback
  108. *
  109. * argument: handle to argument
  110. * element: receives a the element handle (if non-null)
  111. * instance_index: receives the index of the current element instance
  112. * (if non-null)
  113. *
  114. * Returns 1 if successful, 0 otherwise
  115. * ---------------------------------------------------------------------- */
  116. int ply_get_argument_element(p_ply_argument argument,
  117. p_ply_element *element, long *instance_index);
  118. /* ----------------------------------------------------------------------
  119. * Returns information about the property originating a callback
  120. *
  121. * argument: handle to argument
  122. * property: receives the property handle (if non-null)
  123. * length: receives the number of values in this property (if non-null)
  124. * value_index: receives the index of current property value (if non-null)
  125. *
  126. * Returns 1 if successful, 0 otherwise
  127. * ---------------------------------------------------------------------- */
  128. int ply_get_argument_property(p_ply_argument argument,
  129. p_ply_property *property, long *length, long *value_index);
  130. /* ----------------------------------------------------------------------
  131. * Returns user data associated with callback
  132. *
  133. * pdata: receives a copy of user custom data pointer (if non-null)
  134. * idata: receives a copy of user custom data integer (if non-null)
  135. *
  136. * Returns 1 if successful, 0 otherwise
  137. * ---------------------------------------------------------------------- */
  138. int ply_get_argument_user_data(p_ply_argument argument, void **pdata,
  139. long *idata);
  140. /* ----------------------------------------------------------------------
  141. * Returns the value associated with a callback
  142. *
  143. * argument: handle to argument
  144. *
  145. * Returns the current data item
  146. * ---------------------------------------------------------------------- */
  147. double ply_get_argument_value(p_ply_argument argument);
  148. /* ----------------------------------------------------------------------
  149. * Reads all elements and properties calling the callbacks defined with
  150. * calls to ply_set_read_cb
  151. *
  152. * ply: handle returned by ply_open
  153. *
  154. * Returns 1 if successful, 0 otherwise
  155. * ---------------------------------------------------------------------- */
  156. int ply_read(p_ply ply);
  157. /* ----------------------------------------------------------------------
  158. * Iterates over all elements by returning the next element.
  159. * Call with NULL to return handle to first element.
  160. *
  161. * ply: handle returned by ply_open
  162. * last: handle of last element returned (NULL for first element)
  163. *
  164. * Returns element if successful or NULL if no more elements
  165. * ---------------------------------------------------------------------- */
  166. p_ply_element ply_get_next_element(p_ply ply, p_ply_element last);
  167. /* ----------------------------------------------------------------------
  168. * Iterates over all comments by returning the next comment.
  169. * Call with NULL to return pointer to first comment.
  170. *
  171. * ply: handle returned by ply_open
  172. * last: pointer to last comment returned (NULL for first comment)
  173. *
  174. * Returns comment if successful or NULL if no more comments
  175. * ---------------------------------------------------------------------- */
  176. const char *ply_get_next_comment(p_ply ply, const char *last);
  177. /* ----------------------------------------------------------------------
  178. * Iterates over all obj_infos by returning the next obj_info.
  179. * Call with NULL to return pointer to first obj_info.
  180. *
  181. * ply: handle returned by ply_open
  182. * last: pointer to last obj_info returned (NULL for first obj_info)
  183. *
  184. * Returns obj_info if successful or NULL if no more obj_infos
  185. * ---------------------------------------------------------------------- */
  186. const char *ply_get_next_obj_info(p_ply ply, const char *last);
  187. /* ----------------------------------------------------------------------
  188. * Returns information about an element
  189. *
  190. * element: element of interest
  191. * name: receives a pointer to internal copy of element name (if non-null)
  192. * ninstances: receives the number of instances of this element (if non-null)
  193. *
  194. * Returns 1 if successful or 0 otherwise
  195. * ---------------------------------------------------------------------- */
  196. int ply_get_element_info(p_ply_element element, const char** name,
  197. long *ninstances);
  198. /* ----------------------------------------------------------------------
  199. * Iterates over all properties by returning the next property.
  200. * Call with NULL to return handle to first property.
  201. *
  202. * element: handle of element with the properties of interest
  203. * last: handle of last property returned (NULL for first property)
  204. *
  205. * Returns element if successful or NULL if no more properties
  206. * ---------------------------------------------------------------------- */
  207. p_ply_property ply_get_next_property(p_ply_element element,
  208. p_ply_property last);
  209. /* ----------------------------------------------------------------------
  210. * Returns information about a property
  211. *
  212. * property: handle to property of interest
  213. * name: receives a pointer to internal copy of property name (if non-null)
  214. * type: receives the property type (if non-null)
  215. * length_type: for list properties, receives the scalar type of
  216. * the length field (if non-null)
  217. * value_type: for list properties, receives the scalar type of the value
  218. * fields (if non-null)
  219. *
  220. * Returns 1 if successful or 0 otherwise
  221. * ---------------------------------------------------------------------- */
  222. int ply_get_property_info(p_ply_property property, const char** name,
  223. e_ply_type *type, e_ply_type *length_type, e_ply_type *value_type);
  224. /* ----------------------------------------------------------------------
  225. * Creates new PLY file
  226. *
  227. * name: file name
  228. * storage_mode: file format mode
  229. *
  230. * Returns handle to PLY file if successful, NULL otherwise
  231. * ---------------------------------------------------------------------- */
  232. #ifdef _MSC_VER
  233. p_ply ply_create(const wchar_t *name,
  234. #else
  235. p_ply ply_create(const char *name,
  236. #endif
  237. e_ply_storage_mode storage_mode,
  238. p_ply_error_cb error_cb, long idata, void *pdata);
  239. /* ----------------------------------------------------------------------
  240. * Adds a new element to the PLY file created by ply_create
  241. *
  242. * ply: handle returned by ply_create
  243. * name: name of new element
  244. * ninstances: number of element of this time in file
  245. *
  246. * Returns 1 if successful, 0 otherwise
  247. * ---------------------------------------------------------------------- */
  248. int ply_add_element(p_ply ply, const char *name, long ninstances);
  249. /* ----------------------------------------------------------------------
  250. * Adds a new property to the last element added by ply_add_element
  251. *
  252. * ply: handle returned by ply_create
  253. * name: name of new property
  254. * type: property type
  255. * length_type: scalar type of length field of a list property
  256. * value_type: scalar type of value fields of a list property
  257. *
  258. * Returns 1 if successful, 0 otherwise
  259. * ---------------------------------------------------------------------- */
  260. int ply_add_property(p_ply ply, const char *name, e_ply_type type,
  261. e_ply_type length_type, e_ply_type value_type);
  262. /* ----------------------------------------------------------------------
  263. * Adds a new list property to the last element added by ply_add_element
  264. *
  265. * ply: handle returned by ply_create
  266. * name: name of new property
  267. * length_type: scalar type of length field of a list property
  268. * value_type: scalar type of value fields of a list property
  269. *
  270. * Returns 1 if successful, 0 otherwise
  271. * ---------------------------------------------------------------------- */
  272. int ply_add_list_property(p_ply ply, const char *name,
  273. e_ply_type length_type, e_ply_type value_type);
  274. /* ----------------------------------------------------------------------
  275. * Adds a new property to the last element added by ply_add_element
  276. *
  277. * ply: handle returned by ply_create
  278. * name: name of new property
  279. * type: property type
  280. *
  281. * Returns 1 if successful, 0 otherwise
  282. * ---------------------------------------------------------------------- */
  283. int ply_add_scalar_property(p_ply ply, const char *name, e_ply_type type);
  284. /* ----------------------------------------------------------------------
  285. * Adds a new comment item
  286. *
  287. * ply: handle returned by ply_create
  288. * comment: pointer to string with comment text
  289. *
  290. * Returns 1 if successful, 0 otherwise
  291. * ---------------------------------------------------------------------- */
  292. int ply_add_comment(p_ply ply, const char *comment);
  293. /* ----------------------------------------------------------------------
  294. * Adds a new obj_info item
  295. *
  296. * ply: handle returned by ply_create
  297. * comment: pointer to string with obj_info data
  298. *
  299. * Returns 1 if successful, 0 otherwise
  300. * ---------------------------------------------------------------------- */
  301. int ply_add_obj_info(p_ply ply, const char *obj_info);
  302. /* ----------------------------------------------------------------------
  303. * Writes the PLY file header after all element and properties have been
  304. * defined by calls to ply_add_element and ply_add_property
  305. *
  306. * ply: handle returned by ply_create
  307. *
  308. * Returns 1 if successful, 0 otherwise
  309. * ---------------------------------------------------------------------- */
  310. int ply_write_header(p_ply ply);
  311. /* ----------------------------------------------------------------------
  312. * Writes one property value, in the order they should be written to the
  313. * file. For each element type, write all elements of that type in order.
  314. * For each element, write all its properties in order. For scalar
  315. * properties, just write the value. For list properties, write the length
  316. * and then each of the values.
  317. *
  318. * ply: handle returned by ply_create
  319. *
  320. * Returns 1 if successful, 0 otherwise
  321. * ---------------------------------------------------------------------- */
  322. int ply_write(p_ply ply, double value);
  323. /* ----------------------------------------------------------------------
  324. * Closes a PLY file handle. Releases all memory used by handle
  325. *
  326. * ply: handle to be closed.
  327. *
  328. * Returns 1 if successful, 0 otherwise
  329. * ---------------------------------------------------------------------- */
  330. int ply_close(p_ply ply);
  331. /* ----------------------------------------------------------------------
  332. * Added by DGM : returns the file type
  333. *
  334. * ply: handle returned by ply_open
  335. *
  336. * Returns 1 if successful, 0 otherwise
  337. * ---------------------------------------------------------------------- */
  338. int get_plystorage_mode(p_ply ply, e_ply_storage_mode *storage_mode);
  339. #ifdef __cplusplus
  340. }
  341. #endif
  342. #endif /* RPLY_H */
  343. /* ----------------------------------------------------------------------
  344. * Copyright (C) 2003-2011 Diego Nehab. All rights reserved.
  345. *
  346. * Permission is hereby granted, free of charge, to any person obtaining
  347. * a copy of this software and associated documentation files (the
  348. * "Software"), to deal in the Software without restriction, including
  349. * without limitation the rights to use, copy, modify, merge, publish,
  350. * distribute, sublicense, and/or sell copies of the Software, and to
  351. * permit persons to whom the Software is furnished to do so, subject to
  352. * the following conditions:
  353. *
  354. * The above copyright notice and this permission notice shall be
  355. * included in all copies or substantial portions of the Software.
  356. *
  357. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  358. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  359. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  360. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  361. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  362. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  363. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  364. * ---------------------------------------------------------------------- */