ply.h 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168
  1. #ifndef IGL_PLY_H
  2. #define IGL_PLY_H
  3. /*
  4. Header for PLY polygon files.
  5. - Greg Turk, March 1994
  6. A PLY file contains a single polygonal _object_.
  7. An object is composed of lists of _elements_. Typical elements are
  8. vertices, faces, edges and materials.
  9. Each type of element for a given object has one or more _properties_
  10. associated with the element type. For instance, a vertex element may
  11. have as properties three floating-point values x,y,z and three unsigned
  12. chars for red, green and blue.
  13. ---------------------------------------------------------------
  14. Copyright (c) 1994 The Board of Trustees of The Leland Stanford
  15. Junior University. All rights reserved.
  16. Permission to use, copy, modify and distribute this software and its
  17. documentation for any purpose is hereby granted without fee, provided
  18. that the above copyright notice and this permission notice appear in
  19. all copies of this software and that you do not sell the software.
  20. THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
  21. EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  22. WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  23. */
  24. /*
  25. --------------------------------------------------------------------------------
  26. Joao Fradinho Oliveira, July 2005
  27. Copyright (c) 2005 University College London
  28. copyright conditions as above
  29. update for ply reading of multi OS ply files, in any OS (Unix, Macintosh, PC)
  30. --------------------------------------------------------------------------------
  31. ply_open_for_reading
  32. * was changed to always open files in binary mode, files written in ascii can also be
  33. read with this binary mode.
  34. * allows opening of filenames that are alias files in macintosh
  35. * code tested on pc and mac
  36. get_words
  37. * was changed to handle line breaks in UNIX, MACINTOSH, PC, it resets the file pointer
  38. accordingly for the next read.
  39. NOTES:
  40. The ply file, has always an ascii part for the header, and a binary or ascii
  41. part for the data.
  42. The header part in ascii, dictates that linebreaks are used, this make models
  43. operating system dependent, as a line break in unix is indicated with the escape character \n,
  44. on a macintosh, with \r, and on a pc with \r\n <--2 unsigned chars, 2 bytes, instead of 1 byte.
  45. get_words allows reading of any OS, text editors such as BBEdit do not save the linebreaks
  46. properly to target OSs with binary files.
  47. */
  48. #ifndef __PLY_H__
  49. #define __PLY_H__
  50. #include <stdlib.h>
  51. #include <stdio.h>
  52. #include <stddef.h>
  53. #include <string.h>
  54. namespace igl {
  55. namespace ply {
  56. #define PLY_ASCII 1 /* ascii PLY file */
  57. #define PLY_BINARY_BE 2 /* binary PLY file, big endian */
  58. #define PLY_BINARY_LE 3 /* binary PLY file, little endian */
  59. #define PLY_BINARY_NATIVE 4 /* binary PLY file, same endianness as
  60. current architecture */
  61. #define PLY_OKAY 0 /* ply routine worked okay */
  62. #define PLY_ERROR -1 /* error in ply routine */
  63. /* scalar data types supported by PLY format */
  64. #define PLY_START_TYPE 0
  65. #define PLY_CHAR 1
  66. #define PLY_SHORT 2
  67. #define PLY_INT 3
  68. #define PLY_UCHAR 4
  69. #define PLY_USHORT 5
  70. #define PLY_UINT 6
  71. #define PLY_FLOAT 7
  72. #define PLY_DOUBLE 8
  73. #define PLY_END_TYPE 9
  74. #define PLY_SCALAR 0
  75. #define PLY_LIST 1
  76. typedef struct PlyProperty { /* description of a property */
  77. const char *name; /* property name */
  78. int external_type; /* file's data type */
  79. int internal_type; /* program's data type */
  80. int offset; /* offset bytes of prop in a struct */
  81. int is_list; /* 1 = list, 0 = scalar */
  82. int count_external; /* file's count type */
  83. int count_internal; /* program's count type */
  84. int count_offset; /* offset byte for list count */
  85. } PlyProperty;
  86. typedef struct PlyElement { /* description of an element */
  87. const char *name; /* element name */
  88. int num; /* number of elements in this object */
  89. int size; /* size of element (bytes) or -1 if variable */
  90. int nprops; /* number of properties for this element */
  91. PlyProperty **props; /* list of properties in the file */
  92. char *store_prop; /* flags: property wanted by user? */
  93. int other_offset; /* offset to un-asked-for props, or -1 if none*/
  94. int other_size; /* size of other_props structure */
  95. } PlyElement;
  96. typedef struct PlyOtherProp { /* describes other properties in an element */
  97. const char *name; /* element name */
  98. int size; /* size of other_props */
  99. int nprops; /* number of properties in other_props */
  100. PlyProperty **props; /* list of properties in other_props */
  101. } PlyOtherProp;
  102. typedef struct OtherData { /* for storing other_props for an other element */
  103. void *other_props;
  104. } OtherData;
  105. typedef struct OtherElem { /* data for one "other" element */
  106. char *elem_name; /* names of other elements */
  107. int elem_count; /* count of instances of each element */
  108. OtherData **other_data; /* actual property data for the elements */
  109. PlyOtherProp *other_props; /* description of the property data */
  110. } OtherElem;
  111. typedef struct PlyOtherElems { /* "other" elements, not interpreted by user */
  112. int num_elems; /* number of other elements */
  113. OtherElem *other_list; /* list of data for other elements */
  114. } PlyOtherElems;
  115. typedef struct PlyFile { /* description of PLY file */
  116. FILE *fp; /* file pointer */
  117. int file_type; /* ascii or binary */
  118. float version; /* version number of file */
  119. int nelems; /* number of elements of object */
  120. PlyElement **elems; /* list of elements */
  121. int num_comments; /* number of comments */
  122. char **comments; /* list of comments */
  123. int num_obj_info; /* number of items of object information */
  124. char **obj_info; /* list of object info items */
  125. PlyElement *which_elem; /* which element we're currently writing */
  126. PlyOtherElems *other_elems; /* "other" elements from a PLY file */
  127. } PlyFile;
  128. /* memory allocation */
  129. extern char *my_alloc();
  130. #define myalloc(mem_size) my_alloc((mem_size), __LINE__, __FILE__)
  131. #ifndef ALLOCN
  132. #define REALLOCN(PTR,TYPE,OLD_N,NEW_N) \
  133. { \
  134. if ((OLD_N) == 0) \
  135. { ALLOCN((PTR),TYPE,(NEW_N));} \
  136. else \
  137. { \
  138. (PTR) = (TYPE *)realloc((PTR),(NEW_N)*sizeof(TYPE)); \
  139. if (((PTR) == NULL) && ((NEW_N) != 0)) \
  140. { \
  141. fprintf(stderr, "Memory reallocation failed on line %d in %s\n", \
  142. __LINE__, __FILE__); \
  143. fprintf(stderr, " tried to reallocate %d->%d\n", \
  144. (OLD_N), (NEW_N)); \
  145. exit(-1); \
  146. } \
  147. if ((NEW_N)>(OLD_N)) \
  148. memset((char *)(PTR)+(OLD_N)*sizeof(TYPE), 0, \
  149. ((NEW_N)-(OLD_N))*sizeof(TYPE)); \
  150. } \
  151. }
  152. #define ALLOCN(PTR,TYPE,N) \
  153. { (PTR) = (TYPE *) calloc(((unsigned)(N)),sizeof(TYPE));\
  154. if ((PTR) == NULL) { \
  155. fprintf(stderr, "Memory allocation failed on line %d in %s\n", \
  156. __LINE__, __FILE__); \
  157. exit(-1); \
  158. } \
  159. }
  160. #define FREE(PTR) { free((PTR)); (PTR) = NULL; }
  161. #endif
  162. /*** delcaration of routines ***/
  163. inline int get_native_binary_type2();
  164. inline PlyFile *ply_write(FILE *, int,const char **, int);
  165. inline PlyFile *ply_open_for_writing(char *, int,const char **, int, float *);
  166. inline void ply_describe_element(PlyFile *, const char *, int, int, PlyProperty *);
  167. inline void ply_describe_property(PlyFile *, const char *, PlyProperty *);
  168. inline void ply_element_count(PlyFile *, const char *, int);
  169. inline void ply_header_complete(PlyFile *);
  170. inline void ply_put_element_setup(PlyFile *, const char *);
  171. inline void ply_put_element(PlyFile *, void *, int*);
  172. inline void ply_put_comment(PlyFile *, char *);
  173. inline void ply_put_obj_info(PlyFile *, char *);
  174. inline PlyFile *ply_read(FILE *, int *, char ***);
  175. inline PlyFile *ply_open_for_reading( const char *, int *, char ***, int *, float *);
  176. inline PlyProperty **ply_get_element_description(PlyFile *, const char *, int*, int*);
  177. inline void ply_get_element_setup( PlyFile *, const char *, int, PlyProperty *);
  178. inline void ply_get_property(PlyFile *, const char *, PlyProperty *);
  179. inline PlyOtherProp *ply_get_other_properties(PlyFile *, const char *, int);
  180. inline void ply_get_element(PlyFile *, void *, int *);
  181. inline char **ply_get_comments(PlyFile *, int *);
  182. inline char **ply_get_obj_info(PlyFile *, int *);
  183. inline void ply_close(PlyFile *);
  184. inline void ply_get_info(PlyFile *, float *, int *);
  185. inline PlyOtherElems *ply_get_other_element (PlyFile *, const char *, int);
  186. inline void ply_describe_other_elements ( PlyFile *, PlyOtherElems *);
  187. inline void ply_put_other_elements (PlyFile *);
  188. inline void ply_free_other_elements (PlyOtherElems *);
  189. inline void ply_describe_other_properties(PlyFile *, PlyOtherProp *, int);
  190. inline int equal_strings(const char *, const char *);
  191. }
  192. }
  193. #endif /* !__PLY_H__ */
  194. /*
  195. The interface routines for reading and writing PLY polygon files.
  196. Greg Turk, February 1994
  197. ---------------------------------------------------------------
  198. A PLY file contains a single polygonal _object_.
  199. An object is composed of lists of _elements_. Typical elements are
  200. vertices, faces, edges and materials.
  201. Each type of element for a given object has one or more _properties_
  202. associated with the element type. For instance, a vertex element may
  203. have as properties the floating-point values x,y,z and the three unsigned
  204. chars representing red, green and blue.
  205. ---------------------------------------------------------------
  206. Copyright (c) 1994 The Board of Trustees of The Leland Stanford
  207. Junior University. All rights reserved.
  208. Permission to use, copy, modify and distribute this software and its
  209. documentation for any purpose is hereby granted without fee, provided
  210. that the above copyright notice and this permission notice appear in
  211. all copies of this software and that you do not sell the software.
  212. THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
  213. EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  214. WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  215. */
  216. /*
  217. --------------------------------------------------------------------------------
  218. Joao Fradinho Oliveira, July 2005
  219. University College London
  220. update for ply reading of multi OS ply files, in any OS (Unix, Macintosh, PC)
  221. --------------------------------------------------------------------------------
  222. ply_open_for_reading
  223. * was changed to always open files in binary mode, files written in ascii can also be
  224. read with this binary mode.
  225. * allows opening of filenames that are alias files in macintosh
  226. * code tested on pc and mac
  227. get_words
  228. * was changed to handle line breaks in UNIX, MACINTOSH, PC, it resets the file pointer
  229. accordingly for the next read.
  230. NOTES:
  231. The ply file, has always an ascii part for the header, and a binary or ascii
  232. part for the data.
  233. The header part in ascii, dictates that linebreaks are used, this make models
  234. operating system dependent, as a line break in unix is indicated with the escape character \n,
  235. on a macintosh, with \r, and on a pc with \r\n <--2 unsigned chars, 2 bytes, instead of 1 byte.
  236. get_words allows reading of any OS, text editors such as BBEdit do not save the linebreaks
  237. properly to target OSs with binary files.
  238. */
  239. #include <stdio.h>
  240. #include <stdlib.h>
  241. #include <math.h>
  242. #include <string.h>
  243. //#include "ply.h"
  244. namespace igl {
  245. namespace ply {
  246. // Use unnamed namespace to avoid duplicate symbols
  247. /*
  248. namespace
  249. {
  250. const char *type_names[] = {
  251. "invalid",
  252. "char", "short", "int",
  253. "uchar", "ushort", "uint",
  254. "float", "double",
  255. };
  256. // names of scalar types
  257. const char *alt_type_names[] = {
  258. "invalid",
  259. "int8", "int16", "int32", "uint8", "uint16", "uint32", "float32", "float64",
  260. };
  261. int ply_type_size[] = {
  262. 0, 1, 2, 4, 1, 2, 4, 4, 8
  263. };
  264. }
  265. typedef union
  266. {
  267. int int_value;
  268. char byte_values[sizeof(int)];
  269. } endian_test_type;
  270. namespace
  271. {
  272. static int native_binary_type = -1;
  273. static int types_checked = 0;
  274. }
  275. */
  276. #define NO_OTHER_PROPS -1
  277. #define DONT_STORE_PROP 0
  278. #define STORE_PROP 1
  279. #define OTHER_PROP 0
  280. #define NAMED_PROP 1
  281. /* returns 1 if strings are equal, 0 if not */
  282. inline int equal_strings(const char *, const char *);
  283. /* find an element in a plyfile's list */
  284. inline PlyElement *find_element(PlyFile *, const char *);
  285. /* find a property in an element's list */
  286. inline PlyProperty *find_property(PlyElement *, const char *, int *);
  287. /* write to a file the word describing a PLY file data type */
  288. inline void write_scalar_type (FILE *, int);
  289. /* read a line from a file and break it up into separate words */
  290. inline char **get_words(FILE *, int *, char **);
  291. inline char **old_get_words(FILE *, int *);
  292. /* write an item to a file */
  293. inline void write_binary_item(FILE *, int, int, unsigned int, double, int, int*);
  294. inline void write_ascii_item(FILE *, int, unsigned int, double, int);
  295. inline double old_write_ascii_item(FILE *, char *, int);
  296. /* add information to a PLY file descriptor */
  297. inline void add_element(PlyFile *, char **);
  298. inline void add_property(PlyFile *, char **);
  299. inline void add_comment(PlyFile *, char *);
  300. inline void add_obj_info(PlyFile *, char *);
  301. /* copy a property */
  302. inline void copy_property(PlyProperty *, PlyProperty *);
  303. /* store a value into where a pointer and a type specify */
  304. inline void store_item(char *, int, int, unsigned int, double);
  305. /* return the value of a stored item */
  306. inline void get_stored_item( void *, int, int *, unsigned int *, double *);
  307. /* return the value stored in an item, given ptr to it and its type */
  308. inline double get_item_value(char *, int);
  309. /* get binary or ascii item and store it according to ptr and type */
  310. inline void get_ascii_item(char *, int, int *, unsigned int *, double *);
  311. inline void get_binary_item(FILE *, int, int, int *, unsigned int *, double *, int*);
  312. /* get a bunch of elements from a file */
  313. inline void ascii_get_element(PlyFile *, char *);
  314. inline void binary_get_element(PlyFile *, char *, int*);
  315. /* memory allocation */
  316. inline char *my_alloc(int, int, const char *);
  317. /* byte ordering */
  318. inline void get_native_binary_type(int*);
  319. inline void swap_bytes(char *, int);
  320. inline int check_types();
  321. /*************/
  322. /* Writing */
  323. /*************/
  324. /******************************************************************************
  325. Given a file pointer, get ready to write PLY data to the file.
  326. Entry:
  327. fp - the given file pointer
  328. nelems - number of elements in object
  329. elem_names - list of element names
  330. file_type - file type, either ascii or binary
  331. Exit:
  332. returns a pointer to a PlyFile, used to refer to this file, or NULL if error
  333. ******************************************************************************/
  334. inline PlyFile *ply_write(
  335. FILE *fp,
  336. int nelems,
  337. const char **elem_names,
  338. int file_type
  339. )
  340. {
  341. int i;
  342. PlyFile *plyfile;
  343. PlyElement *elem;
  344. /* check for NULL file pointer */
  345. if (fp == NULL)
  346. return (NULL);
  347. int native_binary_type = -1;
  348. int types_checked = 0;
  349. if (native_binary_type == -1)
  350. native_binary_type = get_native_binary_type2();
  351. if (!types_checked)
  352. types_checked = check_types();
  353. /* create a record for this object */
  354. plyfile = (PlyFile *) myalloc (sizeof (PlyFile));
  355. if (file_type == PLY_BINARY_NATIVE)
  356. plyfile->file_type = native_binary_type;
  357. else
  358. plyfile->file_type = file_type;
  359. plyfile->num_comments = 0;
  360. plyfile->num_obj_info = 0;
  361. plyfile->nelems = nelems;
  362. plyfile->version = 1.0;
  363. plyfile->fp = fp;
  364. plyfile->other_elems = NULL;
  365. /* tuck aside the names of the elements */
  366. plyfile->elems = (PlyElement **) myalloc (sizeof (PlyElement *) * nelems);
  367. for (i = 0; i < nelems; i++) {
  368. elem = (PlyElement *) myalloc (sizeof (PlyElement));
  369. plyfile->elems[i] = elem;
  370. elem->name = strdup (elem_names[i]);
  371. elem->num = 0;
  372. elem->nprops = 0;
  373. }
  374. /* return pointer to the file descriptor */
  375. return (plyfile);
  376. }
  377. /******************************************************************************
  378. Open a polygon file for writing.
  379. Entry:
  380. filename - name of file to read from
  381. nelems - number of elements in object
  382. elem_names - list of element names
  383. file_type - file type, either ascii or binary
  384. Exit:
  385. version - version number of PLY file
  386. returns a file identifier, used to refer to this file, or NULL if error
  387. ******************************************************************************/
  388. inline PlyFile *ply_open_for_writing(
  389. const char *filename,
  390. int nelems,
  391. const char **elem_names,
  392. int file_type,
  393. float *version
  394. )
  395. {
  396. PlyFile *plyfile;
  397. char *name;
  398. FILE *fp;
  399. /* tack on the extension .ply, if necessary */
  400. name = (char *) myalloc (sizeof (char) * (strlen (filename) + 5));
  401. strcpy (name, filename);
  402. if (strlen (name) < 4 ||
  403. strcmp (name + strlen (name) - 4, ".ply") != 0)
  404. strcat (name, ".ply");
  405. /* open the file for writing */
  406. fp = fopen (name, "w");
  407. if (fp == NULL) {
  408. return (NULL);
  409. }
  410. /* create the actual PlyFile structure */
  411. plyfile = ply_write (fp, nelems, elem_names, file_type);
  412. if (plyfile == NULL)
  413. return (NULL);
  414. /* say what PLY file version number we're writing */
  415. *version = plyfile->version;
  416. /* return pointer to the file descriptor */
  417. return (plyfile);
  418. }
  419. /******************************************************************************
  420. Describe an element, including its properties and how many will be written
  421. to the file.
  422. Entry:
  423. plyfile - file identifier
  424. elem_name - name of element that information is being specified about
  425. nelems - number of elements of this type to be written
  426. nprops - number of properties contained in the element
  427. prop_list - list of properties
  428. ******************************************************************************/
  429. inline void ply_describe_element(
  430. PlyFile *plyfile,
  431. const char *elem_name,
  432. int nelems,
  433. int nprops,
  434. PlyProperty *prop_list
  435. )
  436. {
  437. int i;
  438. PlyElement *elem;
  439. PlyProperty *prop;
  440. /* look for appropriate element */
  441. elem = find_element (plyfile, elem_name);
  442. if (elem == NULL) {
  443. fprintf(stderr,"ply_describe_element: can't find element '%s'\n",elem_name);
  444. exit (-1);
  445. }
  446. elem->num = nelems;
  447. /* copy the list of properties */
  448. elem->nprops = nprops;
  449. elem->props = (PlyProperty **) myalloc (sizeof (PlyProperty *) * nprops);
  450. elem->store_prop = (char *) myalloc (sizeof (char) * nprops);
  451. for (i = 0; i < nprops; i++) {
  452. prop = (PlyProperty *) myalloc (sizeof (PlyProperty));
  453. elem->props[i] = prop;
  454. elem->store_prop[i] = NAMED_PROP;
  455. copy_property (prop, &prop_list[i]);
  456. }
  457. }
  458. /******************************************************************************
  459. Describe a property of an element.
  460. Entry:
  461. plyfile - file identifier
  462. elem_name - name of element that information is being specified about
  463. prop - the new property
  464. ******************************************************************************/
  465. inline void ply_describe_property(
  466. PlyFile *plyfile,
  467. const char *elem_name,
  468. PlyProperty *prop
  469. )
  470. {
  471. PlyElement *elem;
  472. PlyProperty *elem_prop;
  473. /* look for appropriate element */
  474. elem = find_element (plyfile, elem_name);
  475. if (elem == NULL) {
  476. fprintf(stderr, "ply_describe_property: can't find element '%s'\n",
  477. elem_name);
  478. return;
  479. }
  480. /* create room for new property */
  481. if (elem->nprops == 0) {
  482. elem->props = (PlyProperty **) myalloc (sizeof (PlyProperty *));
  483. elem->store_prop = (char *) myalloc (sizeof (char));
  484. elem->nprops = 1;
  485. }
  486. else {
  487. elem->nprops++;
  488. elem->props = (PlyProperty **)
  489. realloc (elem->props, sizeof (PlyProperty *) * elem->nprops);
  490. elem->store_prop = (char *)
  491. realloc (elem->store_prop, sizeof (char) * elem->nprops);
  492. }
  493. /* copy the new property */
  494. elem_prop = (PlyProperty *) myalloc (sizeof (PlyProperty));
  495. elem->props[elem->nprops - 1] = elem_prop;
  496. elem->store_prop[elem->nprops - 1] = NAMED_PROP;
  497. copy_property (elem_prop, prop);
  498. }
  499. /******************************************************************************
  500. Describe what the "other" properties are that are to be stored, and where
  501. they are in an element.
  502. ******************************************************************************/
  503. inline void ply_describe_other_properties(
  504. PlyFile *plyfile,
  505. PlyOtherProp *other,
  506. int offset
  507. )
  508. {
  509. int i;
  510. PlyElement *elem;
  511. PlyProperty *prop;
  512. /* look for appropriate element */
  513. elem = find_element (plyfile, other->name);
  514. if (elem == NULL) {
  515. fprintf(stderr, "ply_describe_other_properties: can't find element '%s'\n",
  516. other->name);
  517. return;
  518. }
  519. /* create room for other properties */
  520. if (elem->nprops == 0) {
  521. elem->props = (PlyProperty **)
  522. myalloc (sizeof (PlyProperty *) * other->nprops);
  523. elem->store_prop = (char *) myalloc (sizeof (char) * other->nprops);
  524. elem->nprops = 0;
  525. }
  526. else {
  527. int newsize;
  528. newsize = elem->nprops + other->nprops;
  529. elem->props = (PlyProperty **)
  530. realloc (elem->props, sizeof (PlyProperty *) * newsize);
  531. elem->store_prop = (char *)
  532. realloc (elem->store_prop, sizeof (char) * newsize);
  533. }
  534. /* copy the other properties */
  535. for (i = 0; i < other->nprops; i++) {
  536. prop = (PlyProperty *) myalloc (sizeof (PlyProperty));
  537. copy_property (prop, other->props[i]);
  538. elem->props[elem->nprops] = prop;
  539. elem->store_prop[elem->nprops] = OTHER_PROP;
  540. elem->nprops++;
  541. }
  542. /* save other info about other properties */
  543. elem->other_size = other->size;
  544. elem->other_offset = offset;
  545. }
  546. /******************************************************************************
  547. State how many of a given element will be written.
  548. Entry:
  549. plyfile - file identifier
  550. elem_name - name of element that information is being specified about
  551. nelems - number of elements of this type to be written
  552. ******************************************************************************/
  553. inline void ply_element_count(
  554. PlyFile *plyfile,
  555. const char *elem_name,
  556. int nelems
  557. )
  558. {
  559. PlyElement *elem;
  560. /* look for appropriate element */
  561. elem = find_element (plyfile, elem_name);
  562. if (elem == NULL) {
  563. fprintf(stderr,"ply_element_count: can't find element '%s'\n",elem_name);
  564. exit (-1);
  565. }
  566. elem->num = nelems;
  567. }
  568. /******************************************************************************
  569. Signal that we've described everything a PLY file's header and that the
  570. header should be written to the file.
  571. Entry:
  572. plyfile - file identifier
  573. ******************************************************************************/
  574. inline void ply_header_complete(PlyFile *plyfile)
  575. {
  576. int i,j;
  577. FILE *fp = plyfile->fp;
  578. PlyElement *elem;
  579. PlyProperty *prop;
  580. fprintf (fp, "ply\n");
  581. switch (plyfile->file_type) {
  582. case PLY_ASCII:
  583. fprintf (fp, "format ascii 1.0\n");
  584. break;
  585. case PLY_BINARY_BE:
  586. fprintf (fp, "format binary_big_endian 1.0\n");
  587. break;
  588. case PLY_BINARY_LE:
  589. fprintf (fp, "format binary_little_endian 1.0\n");
  590. break;
  591. default:
  592. fprintf (stderr, "ply_header_complete: bad file type = %d\n",
  593. plyfile->file_type);
  594. exit (-1);
  595. }
  596. /* write out the comments */
  597. for (i = 0; i < plyfile->num_comments; i++)
  598. fprintf (fp, "comment %s\n", plyfile->comments[i]);
  599. /* write out object information */
  600. for (i = 0; i < plyfile->num_obj_info; i++)
  601. fprintf (fp, "obj_info %s\n", plyfile->obj_info[i]);
  602. /* write out information about each element */
  603. for (i = 0; i < plyfile->nelems; i++) {
  604. elem = plyfile->elems[i];
  605. fprintf (fp, "element %s %d\n", elem->name, elem->num);
  606. /* write out each property */
  607. for (j = 0; j < elem->nprops; j++) {
  608. prop = elem->props[j];
  609. if (prop->is_list) {
  610. fprintf (fp, "property list ");
  611. write_scalar_type (fp, prop->count_external);
  612. fprintf (fp, " ");
  613. write_scalar_type (fp, prop->external_type);
  614. fprintf (fp, " %s\n", prop->name);
  615. }
  616. else {
  617. fprintf (fp, "property ");
  618. write_scalar_type (fp, prop->external_type);
  619. fprintf (fp, " %s\n", prop->name);
  620. }
  621. }
  622. }
  623. fprintf (fp, "end_header\n");
  624. }
  625. /******************************************************************************
  626. Specify which elements are going to be written. This should be called
  627. before a call to the routine ply_put_element().
  628. Entry:
  629. plyfile - file identifier
  630. elem_name - name of element we're talking about
  631. ******************************************************************************/
  632. inline void ply_put_element_setup(PlyFile *plyfile, const char *elem_name)
  633. {
  634. PlyElement *elem;
  635. elem = find_element (plyfile, elem_name);
  636. if (elem == NULL) {
  637. fprintf(stderr, "ply_elements_setup: can't find element '%s'\n", elem_name);
  638. exit (-1);
  639. }
  640. plyfile->which_elem = elem;
  641. }
  642. /******************************************************************************
  643. Write an element to the file. This routine assumes that we're
  644. writing the type of element specified in the last call to the routine
  645. ply_put_element_setup().
  646. Entry:
  647. plyfile - file identifier
  648. elem_ptr - pointer to the element
  649. ******************************************************************************/
  650. inline void ply_put_element(PlyFile *plyfile, void *elem_ptr, int *native_binary_type)
  651. {
  652. int j,k;
  653. FILE *fp = plyfile->fp;
  654. PlyElement *elem;
  655. PlyProperty *prop;
  656. char *elem_data,*item;
  657. char **item_ptr;
  658. int list_count;
  659. int item_size;
  660. int int_val;
  661. unsigned int uint_val;
  662. double double_val;
  663. char **other_ptr;
  664. elem = plyfile->which_elem;
  665. elem_data = (char *)elem_ptr;
  666. other_ptr = (char **) (((char *) elem_ptr) + elem->other_offset);
  667. /* write out either to an ascii or binary file */
  668. int ply_type_size[] = {
  669. 0, 1, 2, 4, 1, 2, 4, 4, 8
  670. };
  671. if (plyfile->file_type == PLY_ASCII) {
  672. /* write an ascii file */
  673. /* write out each property of the element */
  674. for (j = 0; j < elem->nprops; j++) {
  675. prop = elem->props[j];
  676. if (elem->store_prop[j] == OTHER_PROP)
  677. elem_data = *other_ptr;
  678. else
  679. elem_data = (char *)elem_ptr;
  680. if (prop->is_list) {
  681. item = elem_data + prop->count_offset;
  682. get_stored_item ((void *) item, prop->count_internal,
  683. &int_val, &uint_val, &double_val);
  684. write_ascii_item (fp, int_val, uint_val, double_val,
  685. prop->count_external);
  686. list_count = uint_val;
  687. item_ptr = (char **) (elem_data + prop->offset);
  688. item = item_ptr[0];
  689. item_size = ply_type_size[prop->internal_type];
  690. for (k = 0; k < list_count; k++) {
  691. get_stored_item ((void *) item, prop->internal_type,
  692. &int_val, &uint_val, &double_val);
  693. write_ascii_item (fp, int_val, uint_val, double_val,
  694. prop->external_type);
  695. item += item_size;
  696. }
  697. }
  698. else {
  699. item = elem_data + prop->offset;
  700. get_stored_item ((void *) item, prop->internal_type,
  701. &int_val, &uint_val, &double_val);
  702. write_ascii_item (fp, int_val, uint_val, double_val,
  703. prop->external_type);
  704. }
  705. }
  706. fprintf (fp, "\n");
  707. }
  708. else {
  709. /* write a binary file */
  710. /* write out each property of the element */
  711. for (j = 0; j < elem->nprops; j++) {
  712. prop = elem->props[j];
  713. if (elem->store_prop[j] == OTHER_PROP)
  714. elem_data = *other_ptr;
  715. else
  716. elem_data = (char *)elem_ptr;
  717. if (prop->is_list) {
  718. item = elem_data + prop->count_offset;
  719. item_size = ply_type_size[prop->count_internal];
  720. get_stored_item ((void *) item, prop->count_internal,
  721. &int_val, &uint_val, &double_val);
  722. write_binary_item (fp, plyfile->file_type, int_val, uint_val,
  723. double_val, prop->count_external, native_binary_type);
  724. list_count = uint_val;
  725. item_ptr = (char **) (elem_data + prop->offset);
  726. item = item_ptr[0];
  727. item_size = ply_type_size[prop->internal_type];
  728. for (k = 0; k < list_count; k++) {
  729. get_stored_item ((void *) item, prop->internal_type,
  730. &int_val, &uint_val, &double_val);
  731. write_binary_item (fp, plyfile->file_type, int_val, uint_val,
  732. double_val, prop->external_type, native_binary_type);
  733. item += item_size;
  734. }
  735. }
  736. else {
  737. item = elem_data + prop->offset;
  738. item_size = ply_type_size[prop->internal_type];
  739. get_stored_item ((void *) item, prop->internal_type,
  740. &int_val, &uint_val, &double_val);
  741. write_binary_item (fp, plyfile->file_type, int_val, uint_val,
  742. double_val, prop->external_type, native_binary_type);
  743. }
  744. }
  745. }
  746. }
  747. /******************************************************************************
  748. Specify a comment that will be written in the header.
  749. Entry:
  750. plyfile - file identifier
  751. comment - the comment to be written
  752. ******************************************************************************/
  753. inline void ply_put_comment(PlyFile *plyfile, char *comment)
  754. {
  755. /* (re)allocate space for new comment */
  756. if (plyfile->num_comments == 0)
  757. plyfile->comments = (char **) myalloc (sizeof (char *));
  758. else
  759. plyfile->comments = (char **) realloc (plyfile->comments,
  760. sizeof (char *) * (plyfile->num_comments + 1));
  761. /* add comment to list */
  762. plyfile->comments[plyfile->num_comments] = strdup (comment);
  763. plyfile->num_comments++;
  764. }
  765. /******************************************************************************
  766. Specify a piece of object information (arbitrary text) that will be written
  767. in the header.
  768. Entry:
  769. plyfile - file identifier
  770. obj_info - the text information to be written
  771. ******************************************************************************/
  772. inline void ply_put_obj_info(PlyFile *plyfile, char *obj_info)
  773. {
  774. /* (re)allocate space for new info */
  775. if (plyfile->num_obj_info == 0)
  776. plyfile->obj_info = (char **) myalloc (sizeof (char *));
  777. else
  778. plyfile->obj_info = (char **) realloc (plyfile->obj_info,
  779. sizeof (char *) * (plyfile->num_obj_info + 1));
  780. /* add info to list */
  781. plyfile->obj_info[plyfile->num_obj_info] = strdup (obj_info);
  782. plyfile->num_obj_info++;
  783. }
  784. /*************/
  785. /* Reading */
  786. /*************/
  787. /******************************************************************************
  788. Given a file pointer, get ready to read PLY data from the file.
  789. Entry:
  790. fp - the given file pointer
  791. Exit:
  792. nelems - number of elements in object
  793. elem_names - list of element names
  794. returns a pointer to a PlyFile, used to refer to this file, or NULL if error
  795. ******************************************************************************/
  796. inline PlyFile *ply_read(FILE *fp, int *nelems, char ***elem_names)
  797. {
  798. int i,j;
  799. PlyFile *plyfile;
  800. int nwords;
  801. char **words;
  802. char **elist;
  803. PlyElement *elem;
  804. char *orig_line;
  805. /* check for NULL file pointer */
  806. if (fp == NULL)
  807. return (NULL);
  808. int native_binary_type = -1;
  809. int types_checked = 0;
  810. if (native_binary_type == -1)
  811. native_binary_type = get_native_binary_type2();
  812. if (!types_checked)
  813. types_checked = check_types();
  814. /* create record for this object */
  815. plyfile = (PlyFile *) myalloc (sizeof (PlyFile));
  816. plyfile->nelems = 0;
  817. plyfile->comments = NULL;
  818. plyfile->num_comments = 0;
  819. plyfile->obj_info = NULL;
  820. plyfile->num_obj_info = 0;
  821. plyfile->fp = fp;
  822. plyfile->other_elems = NULL;
  823. /* read and parse the file's header */
  824. words = get_words (plyfile->fp, &nwords, &orig_line);
  825. if (nwords == 0 || !words || !equal_strings (words[0], "ply"))
  826. {
  827. if (words)
  828. free(words);
  829. return (NULL);
  830. }
  831. while (words) {
  832. /* parse words */
  833. if (equal_strings (words[0], "format")) {
  834. if (nwords != 3) {
  835. free(words);
  836. return (NULL);
  837. }
  838. if (equal_strings (words[1], "ascii"))
  839. plyfile->file_type = PLY_ASCII;
  840. else if (equal_strings (words[1], "binary_big_endian"))
  841. plyfile->file_type = PLY_BINARY_BE;
  842. else if (equal_strings (words[1], "binary_little_endian"))
  843. plyfile->file_type = PLY_BINARY_LE;
  844. else {
  845. free(words);
  846. return (NULL);
  847. }
  848. plyfile->version = atof (words[2]);
  849. }
  850. else if (equal_strings (words[0], "element"))
  851. add_element (plyfile, words);
  852. else if (equal_strings (words[0], "property"))
  853. add_property (plyfile, words);
  854. else if (equal_strings (words[0], "comment"))
  855. add_comment (plyfile, orig_line);
  856. else if (equal_strings (words[0], "obj_info"))
  857. add_obj_info (plyfile, orig_line);
  858. else if (equal_strings (words[0], "end_header")) {
  859. free(words);
  860. break;
  861. }
  862. /* free up words space */
  863. free (words);
  864. words = get_words (plyfile->fp, &nwords, &orig_line);
  865. }
  866. /* create tags for each property of each element, to be used */
  867. /* later to say whether or not to store each property for the user */
  868. for (i = 0; i < plyfile->nelems; i++) {
  869. elem = plyfile->elems[i];
  870. elem->store_prop = (char *) myalloc (sizeof (char) * elem->nprops);
  871. for (j = 0; j < elem->nprops; j++)
  872. elem->store_prop[j] = DONT_STORE_PROP;
  873. elem->other_offset = NO_OTHER_PROPS; /* no "other" props by default */
  874. }
  875. /* set return values about the elements */
  876. elist = (char **) myalloc (sizeof (char *) * plyfile->nelems);
  877. for (i = 0; i < plyfile->nelems; i++)
  878. elist[i] = strdup (plyfile->elems[i]->name);
  879. *elem_names = elist;
  880. *nelems = plyfile->nelems;
  881. /* return a pointer to the file's information */
  882. return (plyfile);
  883. }
  884. /******************************************************************************
  885. Open a polygon file for reading.
  886. Entry:
  887. filename - name of file to read from
  888. Exit:
  889. nelems - number of elements in object
  890. elem_names - list of element names
  891. file_type - file type, either ascii or binary
  892. version - version number of PLY file
  893. returns a file identifier, used to refer to this file, or NULL if error
  894. ******************************************************************************/
  895. inline PlyFile *ply_open_for_reading(
  896. char *filename,
  897. int *nelems,
  898. char ***elem_names,
  899. int *file_type,
  900. float *version
  901. )
  902. {
  903. FILE *fp;
  904. PlyFile *plyfile;
  905. //char *name;
  906. /* tack on the extension .ply, if necessary */
  907. // removing below, to handle also macintosh alias filenames
  908. //name = (char *) myalloc (sizeof (char) * (strlen (filename) + 5));
  909. //strcpy (name, filename);
  910. //if (strlen (name) < 4 ||
  911. // strcmp (name + strlen (name) - 4, ".ply") != 0)
  912. // strcat (name, ".ply");
  913. /* open the file for reading */
  914. //fp = fopen (name, "r");
  915. //opening file in binary, ascii data can be read in binary with get_words
  916. fp = fopen (filename, "rb");
  917. if (fp == NULL)
  918. return (NULL);
  919. /* create the PlyFile data structure */
  920. plyfile = ply_read (fp, nelems, elem_names);
  921. /* determine the file type and version */
  922. *file_type = plyfile->file_type;
  923. *version = plyfile->version;
  924. /* return a pointer to the file's information */
  925. return (plyfile);
  926. }
  927. /******************************************************************************
  928. Get information about a particular element.
  929. Entry:
  930. plyfile - file identifier
  931. elem_name - name of element to get information about
  932. Exit:
  933. nelems - number of elements of this type in the file
  934. nprops - number of properties
  935. returns a list of properties, or NULL if the file doesn't contain that elem
  936. ******************************************************************************/
  937. inline PlyProperty **ply_get_element_description(
  938. PlyFile *plyfile,
  939. const char *elem_name,
  940. int *nelems,
  941. int *nprops
  942. )
  943. {
  944. int i;
  945. PlyElement *elem;
  946. PlyProperty *prop;
  947. PlyProperty **prop_list;
  948. /* find information about the element */
  949. elem = find_element (plyfile, elem_name);
  950. if (elem == NULL)
  951. return (NULL);
  952. *nelems = elem->num;
  953. *nprops = elem->nprops;
  954. /* make a copy of the element's property list */
  955. prop_list = (PlyProperty **) myalloc (sizeof (PlyProperty *) * elem->nprops);
  956. for (i = 0; i < elem->nprops; i++) {
  957. prop = (PlyProperty *) myalloc (sizeof (PlyProperty));
  958. copy_property (prop, elem->props[i]);
  959. prop_list[i] = prop;
  960. }
  961. /* return this duplicate property list */
  962. return (prop_list);
  963. }
  964. /******************************************************************************
  965. Specify which properties of an element are to be returned. This should be
  966. called before a call to the routine ply_get_element().
  967. Entry:
  968. plyfile - file identifier
  969. elem_name - which element we're talking about
  970. nprops - number of properties
  971. prop_list - list of properties
  972. ******************************************************************************/
  973. inline void ply_get_element_setup(
  974. PlyFile *plyfile,
  975. const char *elem_name,
  976. int nprops,
  977. PlyProperty *prop_list
  978. )
  979. {
  980. int i;
  981. PlyElement *elem;
  982. PlyProperty *prop;
  983. int index;
  984. /* find information about the element */
  985. elem = find_element (plyfile, elem_name);
  986. plyfile->which_elem = elem;
  987. /* deposit the property information into the element's description */
  988. for (i = 0; i < nprops; i++) {
  989. /* look for actual property */
  990. prop = find_property (elem, prop_list[i].name, &index);
  991. if (prop == NULL) {
  992. fprintf (stderr, "Warning: Can't find property '%s' in element '%s'\n",
  993. prop_list[i].name, elem_name);
  994. continue;
  995. }
  996. /* store its description */
  997. prop->internal_type = prop_list[i].internal_type;
  998. prop->offset = prop_list[i].offset;
  999. prop->count_internal = prop_list[i].count_internal;
  1000. prop->count_offset = prop_list[i].count_offset;
  1001. /* specify that the user wants this property */
  1002. elem->store_prop[index] = STORE_PROP;
  1003. }
  1004. }
  1005. /******************************************************************************
  1006. Specify a property of an element that is to be returned. This should be
  1007. called (usually multiple times) before a call to the routine ply_get_element().
  1008. This routine should be used in preference to the less flexible old routine
  1009. called ply_get_element_setup().
  1010. Entry:
  1011. plyfile - file identifier
  1012. elem_name - which element we're talking about
  1013. prop - property to add to those that will be returned
  1014. ******************************************************************************/
  1015. inline void ply_get_property(
  1016. PlyFile *plyfile,
  1017. const char *elem_name,
  1018. PlyProperty *prop
  1019. )
  1020. {
  1021. PlyElement *elem;
  1022. PlyProperty *prop_ptr;
  1023. int index;
  1024. /* find information about the element */
  1025. elem = find_element (plyfile, elem_name);
  1026. plyfile->which_elem = elem;
  1027. /* deposit the property information into the element's description */
  1028. prop_ptr = find_property (elem, prop->name, &index);
  1029. if (prop_ptr == NULL) {
  1030. fprintf (stderr, "Warning: Can't find property '%s' in element '%s'\n",
  1031. prop->name, elem_name);
  1032. return;
  1033. }
  1034. prop_ptr->internal_type = prop->internal_type;
  1035. prop_ptr->offset = prop->offset;
  1036. prop_ptr->count_internal = prop->count_internal;
  1037. prop_ptr->count_offset = prop->count_offset;
  1038. /* specify that the user wants this property */
  1039. elem->store_prop[index] = STORE_PROP;
  1040. }
  1041. /******************************************************************************
  1042. Read one element from the file. This routine assumes that we're reading
  1043. the type of element specified in the last call to the routine
  1044. ply_get_element_setup().
  1045. Entry:
  1046. plyfile - file identifier
  1047. elem_ptr - pointer to location where the element information should be put
  1048. ******************************************************************************/
  1049. inline void ply_get_element(PlyFile *plyfile, void *elem_ptr, int *native_binary_type)
  1050. {
  1051. if (plyfile->file_type == PLY_ASCII)
  1052. ascii_get_element (plyfile, (char *) elem_ptr);
  1053. else
  1054. binary_get_element (plyfile, (char *) elem_ptr, native_binary_type);
  1055. }
  1056. /******************************************************************************
  1057. Extract the comments from the header information of a PLY file.
  1058. Entry:
  1059. plyfile - file identifier
  1060. Exit:
  1061. num_comments - number of comments returned
  1062. returns a pointer to a list of comments
  1063. ******************************************************************************/
  1064. inline char **ply_get_comments(PlyFile *plyfile, int *num_comments)
  1065. {
  1066. *num_comments = plyfile->num_comments;
  1067. return (plyfile->comments);
  1068. }
  1069. /******************************************************************************
  1070. Extract the object information (arbitrary text) from the header information
  1071. of a PLY file.
  1072. Entry:
  1073. plyfile - file identifier
  1074. Exit:
  1075. num_obj_info - number of lines of text information returned
  1076. returns a pointer to a list of object info lines
  1077. ******************************************************************************/
  1078. inline char **ply_get_obj_info(PlyFile *plyfile, int *num_obj_info)
  1079. {
  1080. *num_obj_info = plyfile->num_obj_info;
  1081. return (plyfile->obj_info);
  1082. }
  1083. /******************************************************************************
  1084. Make ready for "other" properties of an element-- those properties that
  1085. the user has not explicitly asked for, but that are to be stashed away
  1086. in a special structure to be carried along with the element's other
  1087. information.
  1088. Entry:
  1089. plyfile - file identifier
  1090. elem - element for which we want to save away other properties
  1091. ******************************************************************************/
  1092. inline void setup_other_props(PlyElement *elem)
  1093. {
  1094. int i;
  1095. PlyProperty *prop;
  1096. int size = 0;
  1097. int type_size;
  1098. /* Examine each property in decreasing order of size. */
  1099. /* We do this so that all data types will be aligned by */
  1100. /* word, half-word, or whatever within the structure. */
  1101. int ply_type_size[] = {
  1102. 0, 1, 2, 4, 1, 2, 4, 4, 8
  1103. };
  1104. for (type_size = 8; type_size > 0; type_size /= 2) {
  1105. /* add up the space taken by each property, and save this information */
  1106. /* away in the property descriptor */
  1107. for (i = 0; i < elem->nprops; i++) {
  1108. /* don't bother with properties we've been asked to store explicitly */
  1109. if (elem->store_prop[i])
  1110. continue;
  1111. prop = elem->props[i];
  1112. /* internal types will be same as external */
  1113. prop->internal_type = prop->external_type;
  1114. prop->count_internal = prop->count_external;
  1115. /* check list case */
  1116. if (prop->is_list) {
  1117. /* pointer to list */
  1118. if (type_size == sizeof (void *)) {
  1119. prop->offset = size;
  1120. size += sizeof (void *); /* always use size of a pointer here */
  1121. }
  1122. /* count of number of list elements */
  1123. if (type_size == ply_type_size[prop->count_external]) {
  1124. prop->count_offset = size;
  1125. size += ply_type_size[prop->count_external];
  1126. }
  1127. }
  1128. /* not list */
  1129. else if (type_size == ply_type_size[prop->external_type]) {
  1130. prop->offset = size;
  1131. size += ply_type_size[prop->external_type];
  1132. }
  1133. }
  1134. }
  1135. /* save the size for the other_props structure */
  1136. elem->other_size = size;
  1137. }
  1138. /******************************************************************************
  1139. Specify that we want the "other" properties of an element to be tucked
  1140. away within the user's structure. The user needn't be concerned for how
  1141. these properties are stored.
  1142. Entry:
  1143. plyfile - file identifier
  1144. elem_name - name of element that we want to store other_props in
  1145. offset - offset to where other_props will be stored inside user's structure
  1146. Exit:
  1147. returns pointer to structure containing description of other_props
  1148. ******************************************************************************/
  1149. inline PlyOtherProp *ply_get_other_properties(
  1150. PlyFile *plyfile,
  1151. const char *elem_name,
  1152. int offset
  1153. )
  1154. {
  1155. int i;
  1156. PlyElement *elem;
  1157. PlyOtherProp *other;
  1158. PlyProperty *prop;
  1159. int nprops;
  1160. /* find information about the element */
  1161. elem = find_element (plyfile, elem_name);
  1162. if (elem == NULL) {
  1163. fprintf (stderr, "ply_get_other_properties: Can't find element '%s'\n",
  1164. elem_name);
  1165. return (NULL);
  1166. }
  1167. /* remember that this is the "current" element */
  1168. plyfile->which_elem = elem;
  1169. /* save the offset to where to store the other_props */
  1170. elem->other_offset = offset;
  1171. /* place the appropriate pointers, etc. in the element's property list */
  1172. setup_other_props (elem);
  1173. /* create structure for describing other_props */
  1174. other = (PlyOtherProp *) myalloc (sizeof (PlyOtherProp));
  1175. other->name = strdup (elem_name);
  1176. #if 0
  1177. if (elem->other_offset == NO_OTHER_PROPS) {
  1178. other->size = 0;
  1179. other->props = NULL;
  1180. other->nprops = 0;
  1181. return (other);
  1182. }
  1183. #endif
  1184. other->size = elem->other_size;
  1185. other->props = (PlyProperty **) myalloc (sizeof(PlyProperty) * elem->nprops);
  1186. /* save descriptions of each "other" property */
  1187. nprops = 0;
  1188. for (i = 0; i < elem->nprops; i++) {
  1189. if (elem->store_prop[i])
  1190. continue;
  1191. prop = (PlyProperty *) myalloc (sizeof (PlyProperty));
  1192. copy_property (prop, elem->props[i]);
  1193. other->props[nprops] = prop;
  1194. nprops++;
  1195. }
  1196. other->nprops = nprops;
  1197. #if 1
  1198. /* set other_offset pointer appropriately if there are NO other properties */
  1199. if (other->nprops == 0) {
  1200. elem->other_offset = NO_OTHER_PROPS;
  1201. }
  1202. #endif
  1203. /* return structure */
  1204. return (other);
  1205. }
  1206. /*************************/
  1207. /* Other Element Stuff */
  1208. /*************************/
  1209. /******************************************************************************
  1210. Grab all the data for an element that a user does not want to explicitly
  1211. read in.
  1212. Entry:
  1213. plyfile - pointer to file
  1214. elem_name - name of element whose data is to be read in
  1215. elem_count - number of instances of this element stored in the file
  1216. Exit:
  1217. returns pointer to ALL the "other" element data for this PLY file
  1218. ******************************************************************************/
  1219. inline PlyOtherElems *ply_get_other_element (
  1220. PlyFile *plyfile,
  1221. char *elem_name,
  1222. int elem_count
  1223. )
  1224. {
  1225. int i;
  1226. PlyElement *elem;
  1227. PlyOtherElems *other_elems;
  1228. OtherElem *other;
  1229. /* look for appropriate element */
  1230. elem = find_element (plyfile, elem_name);
  1231. if (elem == NULL) {
  1232. fprintf (stderr,
  1233. "ply_get_other_element: can't find element '%s'\n", elem_name);
  1234. exit (-1);
  1235. }
  1236. /* create room for the new "other" element, initializing the */
  1237. /* other data structure if necessary */
  1238. if (plyfile->other_elems == NULL) {
  1239. plyfile->other_elems = (PlyOtherElems *) myalloc (sizeof (PlyOtherElems));
  1240. other_elems = plyfile->other_elems;
  1241. other_elems->other_list = (OtherElem *) myalloc (sizeof (OtherElem));
  1242. other = &(other_elems->other_list[0]);
  1243. other_elems->num_elems = 1;
  1244. }
  1245. else {
  1246. other_elems = plyfile->other_elems;
  1247. other_elems->other_list = (OtherElem *) realloc (other_elems->other_list,
  1248. sizeof (OtherElem) * (other_elems->num_elems + 1));
  1249. other = &(other_elems->other_list[other_elems->num_elems]);
  1250. other_elems->num_elems++;
  1251. }
  1252. /* count of element instances in file */
  1253. other->elem_count = elem_count;
  1254. /* save name of element */
  1255. other->elem_name = strdup (elem_name);
  1256. /* create a list to hold all the current elements */
  1257. other->other_data = (OtherData **)
  1258. malloc (sizeof (OtherData *) * other->elem_count);
  1259. /* set up for getting elements */
  1260. other->other_props = ply_get_other_properties (plyfile, elem_name,
  1261. offsetof(OtherData,other_props));
  1262. /* grab all these elements */
  1263. int native_binary_type = get_native_binary_type2();
  1264. for (i = 0; i < other->elem_count; i++) {
  1265. /* grab and element from the file */
  1266. other->other_data[i] = (OtherData *) malloc (sizeof (OtherData));
  1267. ply_get_element (plyfile, (void *) other->other_data[i], &native_binary_type);
  1268. }
  1269. /* return pointer to the other elements data */
  1270. return (other_elems);
  1271. }
  1272. /******************************************************************************
  1273. Pass along a pointer to "other" elements that we want to save in a given
  1274. PLY file. These other elements were presumably read from another PLY file.
  1275. Entry:
  1276. plyfile - file pointer in which to store this other element info
  1277. other_elems - info about other elements that we want to store
  1278. ******************************************************************************/
  1279. inline void ply_describe_other_elements (
  1280. PlyFile *plyfile,
  1281. PlyOtherElems *other_elems
  1282. )
  1283. {
  1284. int i;
  1285. OtherElem *other;
  1286. PlyElement *elem;
  1287. /* ignore this call if there is no other element */
  1288. if (other_elems == NULL)
  1289. return;
  1290. /* save pointer to this information */
  1291. plyfile->other_elems = other_elems;
  1292. /* describe the other properties of this element */
  1293. /* store them in the main element list as elements with
  1294. only other properties */
  1295. REALLOCN(plyfile->elems, PlyElement *,
  1296. plyfile->nelems, plyfile->nelems + other_elems->num_elems);
  1297. for (i = 0; i < other_elems->num_elems; i++) {
  1298. other = &(other_elems->other_list[i]);
  1299. elem = (PlyElement *) myalloc (sizeof (PlyElement));
  1300. plyfile->elems[plyfile->nelems++] = elem;
  1301. elem->name = strdup (other->elem_name);
  1302. elem->num = other->elem_count;
  1303. elem->nprops = 0;
  1304. ply_describe_other_properties (plyfile, other->other_props,
  1305. offsetof(OtherData,other_props));
  1306. }
  1307. }
  1308. /******************************************************************************
  1309. Write out the "other" elements specified for this PLY file.
  1310. Entry:
  1311. plyfile - pointer to PLY file to write out other elements for
  1312. ******************************************************************************/
  1313. inline void ply_put_other_elements (PlyFile *plyfile, int *native_binary_type)
  1314. {
  1315. int i,j;
  1316. OtherElem *other;
  1317. /* make sure we have other elements to write */
  1318. if (plyfile->other_elems == NULL)
  1319. return;
  1320. /* write out the data for each "other" element */
  1321. for (i = 0; i < plyfile->other_elems->num_elems; i++) {
  1322. other = &(plyfile->other_elems->other_list[i]);
  1323. ply_put_element_setup (plyfile, other->elem_name);
  1324. /* write out each instance of the current element */
  1325. for (j = 0; j < other->elem_count; j++)
  1326. ply_put_element (plyfile, (void *) other->other_data[j], native_binary_type);
  1327. }
  1328. }
  1329. /******************************************************************************
  1330. Free up storage used by an "other" elements data structure.
  1331. Entry:
  1332. other_elems - data structure to free up
  1333. ******************************************************************************/
  1334. inline void ply_free_other_elements (PlyOtherElems *other_elems)
  1335. {
  1336. // Alec:
  1337. //other_elems = other_elems;
  1338. delete(other_elems);
  1339. }
  1340. /*******************/
  1341. /* Miscellaneous */
  1342. /*******************/
  1343. /******************************************************************************
  1344. Close a PLY file.
  1345. Entry:
  1346. plyfile - identifier of file to close
  1347. ******************************************************************************/
  1348. inline void ply_close(PlyFile *plyfile)
  1349. {
  1350. fclose (plyfile->fp);
  1351. // Alec:
  1352. plyfile->fp = NULL;
  1353. /* free up memory associated with the PLY file */
  1354. free (plyfile);
  1355. }
  1356. /******************************************************************************
  1357. Get version number and file type of a PlyFile.
  1358. Entry:
  1359. ply - pointer to PLY file
  1360. Exit:
  1361. version - version of the file
  1362. file_type - PLY_ASCII, PLY_BINARY_BE, or PLY_BINARY_LE
  1363. ******************************************************************************/
  1364. inline void ply_get_info(PlyFile *ply, float *version, int *file_type)
  1365. {
  1366. if (ply == NULL)
  1367. return;
  1368. *version = ply->version;
  1369. *file_type = ply->file_type;
  1370. }
  1371. /******************************************************************************
  1372. Compare two strings. Returns 1 if they are the same, 0 if not.
  1373. ******************************************************************************/
  1374. inline int equal_strings(const char *s1, const char *s2)
  1375. {
  1376. while (*s1 && *s2)
  1377. if (*s1++ != *s2++)
  1378. return (0);
  1379. if (*s1 != *s2)
  1380. return (0);
  1381. else
  1382. return (1);
  1383. }
  1384. /******************************************************************************
  1385. Find an element from the element list of a given PLY object.
  1386. Entry:
  1387. plyfile - file id for PLY file
  1388. element - name of element we're looking for
  1389. Exit:
  1390. returns the element, or NULL if not found
  1391. ******************************************************************************/
  1392. inline PlyElement *find_element(PlyFile *plyfile, const char *element)
  1393. {
  1394. int i;
  1395. for (i = 0; i < plyfile->nelems; i++)
  1396. if (equal_strings (element, plyfile->elems[i]->name))
  1397. return (plyfile->elems[i]);
  1398. return (NULL);
  1399. }
  1400. /******************************************************************************
  1401. Find a property in the list of properties of a given element.
  1402. Entry:
  1403. elem - pointer to element in which we want to find the property
  1404. prop_name - name of property to find
  1405. Exit:
  1406. index - index to position in list
  1407. returns a pointer to the property, or NULL if not found
  1408. ******************************************************************************/
  1409. inline PlyProperty *find_property(PlyElement *elem, const char *prop_name, int *index)
  1410. {
  1411. int i;
  1412. for (i = 0; i < elem->nprops; i++)
  1413. if (equal_strings (prop_name, elem->props[i]->name)) {
  1414. *index = i;
  1415. return (elem->props[i]);
  1416. }
  1417. *index = -1;
  1418. return (NULL);
  1419. }
  1420. /******************************************************************************
  1421. Read an element from an ascii file.
  1422. Entry:
  1423. plyfile - file identifier
  1424. elem_ptr - pointer to element
  1425. ******************************************************************************/
  1426. inline void ascii_get_element(PlyFile *plyfile, char *elem_ptr)
  1427. {
  1428. int ply_type_size[] = {
  1429. 0, 1, 2, 4, 1, 2, 4, 4, 8
  1430. };
  1431. int j,k;
  1432. PlyElement *elem;
  1433. PlyProperty *prop;
  1434. char **words;
  1435. int nwords;
  1436. int which_word;
  1437. char *elem_data,*item=NULL;
  1438. char *item_ptr;
  1439. int item_size;
  1440. int int_val;
  1441. unsigned int uint_val;
  1442. double double_val;
  1443. int list_count;
  1444. int store_it;
  1445. char **store_array;
  1446. char *orig_line;
  1447. char *other_data=NULL;
  1448. int other_flag;
  1449. /* the kind of element we're reading currently */
  1450. elem = plyfile->which_elem;
  1451. /* do we need to setup for other_props? */
  1452. if (elem->other_offset != NO_OTHER_PROPS) {
  1453. char **ptr;
  1454. other_flag = 1;
  1455. /* make room for other_props */
  1456. other_data = (char *) myalloc (elem->other_size);
  1457. /* store pointer in user's structure to the other_props */
  1458. ptr = (char **) (elem_ptr + elem->other_offset);
  1459. *ptr = other_data;
  1460. }
  1461. else
  1462. other_flag = 0;
  1463. /* read in the element */
  1464. words = get_words (plyfile->fp, &nwords, &orig_line);
  1465. if (words == NULL) {
  1466. fprintf (stderr, "ply_get_element: unexpected end of file\n");
  1467. exit (-1);
  1468. }
  1469. which_word = 0;
  1470. for (j = 0; j < elem->nprops; j++) {
  1471. prop = elem->props[j];
  1472. store_it = (elem->store_prop[j] | other_flag);
  1473. /* store either in the user's structure or in other_props */
  1474. // if (elem->store_prop[j])
  1475. elem_data = elem_ptr;
  1476. //else
  1477. //elem_data = other_data;
  1478. if (prop->is_list) { /* a list */
  1479. /* get and store the number of items in the list */
  1480. get_ascii_item (words[which_word++], prop->count_external,
  1481. &int_val, &uint_val, &double_val);
  1482. if (store_it) {
  1483. item = elem_data + prop->count_offset;
  1484. store_item(item, prop->count_internal, int_val, uint_val, double_val);
  1485. }
  1486. /* allocate space for an array of items and store a ptr to the array */
  1487. list_count = int_val;
  1488. item_size = ply_type_size[prop->internal_type];
  1489. store_array = (char **) (elem_data + prop->offset);
  1490. if (list_count == 0) {
  1491. if (store_it)
  1492. *store_array = NULL;
  1493. }
  1494. else {
  1495. if (store_it) {
  1496. item_ptr = (char *) myalloc (sizeof (char) * item_size * list_count);
  1497. item = item_ptr;
  1498. *store_array = item_ptr;
  1499. }
  1500. /* read items and store them into the array */
  1501. for (k = 0; k < list_count; k++) {
  1502. get_ascii_item (words[which_word++], prop->external_type,
  1503. &int_val, &uint_val, &double_val);
  1504. if (store_it) {
  1505. store_item (item, prop->internal_type,
  1506. int_val, uint_val, double_val);
  1507. item += item_size;
  1508. }
  1509. }
  1510. }
  1511. }
  1512. else { /* not a list */
  1513. get_ascii_item (words[which_word++], prop->external_type,
  1514. &int_val, &uint_val, &double_val);
  1515. if (store_it) {
  1516. item = elem_data + prop->offset;
  1517. store_item (item, prop->internal_type, int_val, uint_val, double_val);
  1518. }
  1519. }
  1520. }
  1521. free (words);
  1522. }
  1523. /******************************************************************************
  1524. Read an element from a binary file.
  1525. Entry:
  1526. plyfile - file identifier
  1527. elem_ptr - pointer to an element
  1528. ******************************************************************************/
  1529. inline void binary_get_element(PlyFile *plyfile, char *elem_ptr, int *native_binary_type)
  1530. {
  1531. int j,k;
  1532. PlyElement *elem;
  1533. PlyProperty *prop;
  1534. FILE *fp = plyfile->fp;
  1535. char *elem_data,*item=NULL;
  1536. char *item_ptr;
  1537. int item_size;
  1538. int int_val;
  1539. unsigned int uint_val;
  1540. double double_val;
  1541. int list_count;
  1542. int store_it;
  1543. char **store_array;
  1544. char *other_data=NULL;
  1545. int other_flag;
  1546. int ply_type_size[] = {
  1547. 0, 1, 2, 4, 1, 2, 4, 4, 8
  1548. };
  1549. /* the kind of element we're reading currently */
  1550. elem = plyfile->which_elem;
  1551. /* do we need to setup for other_props? */
  1552. if (elem->other_offset != NO_OTHER_PROPS) {
  1553. char **ptr;
  1554. other_flag = 1;
  1555. /* make room for other_props */
  1556. other_data = (char *) myalloc (elem->other_size);
  1557. /* store pointer in user's structure to the other_props */
  1558. ptr = (char **) (elem_ptr + elem->other_offset);
  1559. *ptr = other_data;
  1560. }
  1561. else
  1562. other_flag = 0;
  1563. /* read in a number of elements */
  1564. for (j = 0; j < elem->nprops; j++) {
  1565. prop = elem->props[j];
  1566. store_it = (elem->store_prop[j] | other_flag);
  1567. /* store either in the user's structure or in other_props */
  1568. // if (elem->store_prop[j])
  1569. elem_data = elem_ptr;
  1570. // else
  1571. // elem_data = other_data;
  1572. if (prop->is_list) { /* a list */
  1573. /* get and store the number of items in the list */
  1574. get_binary_item (fp, plyfile->file_type, prop->count_external,
  1575. &int_val, &uint_val, &double_val, native_binary_type);
  1576. if (store_it) {
  1577. item = elem_data + prop->count_offset;
  1578. store_item(item, prop->count_internal, int_val, uint_val, double_val);
  1579. }
  1580. /* allocate space for an array of items and store a ptr to the array */
  1581. list_count = int_val;
  1582. item_size = ply_type_size[prop->internal_type];
  1583. store_array = (char **) (elem_data + prop->offset);
  1584. if (list_count == 0) {
  1585. if (store_it)
  1586. *store_array = NULL;
  1587. }
  1588. else {
  1589. if (store_it) {
  1590. item_ptr = (char *) myalloc (sizeof (char) * item_size * list_count);
  1591. item = item_ptr;
  1592. *store_array = item_ptr;
  1593. }
  1594. // read items and store them into the array
  1595. for (k = 0; k < list_count; k++) {
  1596. get_binary_item (fp, plyfile->file_type, prop->external_type,
  1597. &int_val, &uint_val, &double_val, native_binary_type);
  1598. if (store_it) {
  1599. store_item (item, prop->internal_type,
  1600. int_val, uint_val, double_val);
  1601. item += item_size;
  1602. }
  1603. }
  1604. }
  1605. }
  1606. else { /* not a list */
  1607. get_binary_item (fp, plyfile->file_type, prop->external_type,
  1608. &int_val, &uint_val, &double_val, native_binary_type);
  1609. if (store_it) {
  1610. item = elem_data + prop->offset;
  1611. store_item (item, prop->internal_type, int_val, uint_val, double_val);
  1612. }
  1613. }
  1614. }
  1615. }
  1616. /******************************************************************************
  1617. Write to a file the word that represents a PLY data type.
  1618. Entry:
  1619. fp - file pointer
  1620. code - code for type
  1621. ******************************************************************************/
  1622. inline void write_scalar_type (FILE *fp, int code)
  1623. {
  1624. /* make sure this is a valid code */
  1625. if (code <= PLY_START_TYPE || code >= PLY_END_TYPE) {
  1626. fprintf (stderr, "write_scalar_type: bad data code = %d\n", code);
  1627. exit (-1);
  1628. }
  1629. /* write the code to a file */
  1630. const char *type_names[] = {
  1631. "invalid",
  1632. "char", "short", "int",
  1633. "uchar", "ushort", "uint",
  1634. "float", "double",
  1635. };
  1636. fprintf (fp, "%s", type_names[code]);
  1637. }
  1638. /******************************************************************************
  1639. Reverse the order in an array of bytes. This is the conversion from big
  1640. endian to little endian and vice versa
  1641. Entry:
  1642. bytes - array of bytes to reverse (in place)
  1643. num_bytes - number of bytes in array
  1644. ******************************************************************************/
  1645. inline void swap_bytes(char *bytes, int num_bytes)
  1646. {
  1647. int i;
  1648. char temp;
  1649. for (i=0; i < num_bytes/2; i++)
  1650. {
  1651. temp = bytes[i];
  1652. bytes[i] = bytes[(num_bytes-1)-i];
  1653. bytes[(num_bytes-1)-i] = temp;
  1654. }
  1655. }
  1656. /******************************************************************************
  1657. Find out if this machine is big endian or little endian
  1658. Exit:
  1659. set global variable, native_binary_type =
  1660. either PLY_BINARY_BE or PLY_BINARY_LE
  1661. ******************************************************************************/
  1662. inline void get_native_binary_type(int *native_binary_type)
  1663. {
  1664. typedef union
  1665. {
  1666. int int_value;
  1667. char byte_values[sizeof(int)];
  1668. } endian_test_type;
  1669. endian_test_type test;
  1670. test.int_value = 0;
  1671. test.int_value = 1;
  1672. if (test.byte_values[0] == 1)
  1673. *native_binary_type = PLY_BINARY_LE;
  1674. else if (test.byte_values[sizeof(int)-1] == 1)
  1675. *native_binary_type = PLY_BINARY_BE;
  1676. else
  1677. {
  1678. fprintf(stderr, "ply: Couldn't determine machine endianness.\n");
  1679. fprintf(stderr, "ply: Exiting...\n");
  1680. exit(1);
  1681. }
  1682. }
  1683. inline int get_native_binary_type2()
  1684. {
  1685. typedef union
  1686. {
  1687. int int_value;
  1688. char byte_values[sizeof(int)];
  1689. } endian_test_type;
  1690. endian_test_type test;
  1691. test.int_value = 0;
  1692. test.int_value = 1;
  1693. if (test.byte_values[0] == 1)
  1694. return PLY_BINARY_LE;
  1695. else if (test.byte_values[sizeof(int)-1] == 1)
  1696. return PLY_BINARY_BE;
  1697. else
  1698. {
  1699. fprintf(stderr, "ply: Couldn't determine machine endianness.\n");
  1700. fprintf(stderr, "ply: Exiting...\n");
  1701. exit(1);
  1702. }
  1703. }
  1704. /******************************************************************************
  1705. Verify that all the native types are the sizes we need
  1706. ******************************************************************************/
  1707. inline int check_types()
  1708. {
  1709. int ply_type_size[] = {
  1710. 0, 1, 2, 4, 1, 2, 4, 4, 8
  1711. };
  1712. if ((ply_type_size[PLY_CHAR] != sizeof(char)) ||
  1713. (ply_type_size[PLY_SHORT] != sizeof(short)) ||
  1714. (ply_type_size[PLY_INT] != sizeof(int)) ||
  1715. (ply_type_size[PLY_UCHAR] != sizeof(unsigned char)) ||
  1716. (ply_type_size[PLY_USHORT] != sizeof(unsigned short)) ||
  1717. (ply_type_size[PLY_UINT] != sizeof(unsigned int)) ||
  1718. (ply_type_size[PLY_FLOAT] != sizeof(float)) ||
  1719. (ply_type_size[PLY_DOUBLE] != sizeof(double)))
  1720. {
  1721. fprintf(stderr, "ply: Type sizes do not match built-in types\n");
  1722. fprintf(stderr, "ply: Exiting...\n");
  1723. exit(1);
  1724. }
  1725. return 1;
  1726. }
  1727. /******************************************************************************
  1728. Get a text line from a file and break it up into words.
  1729. IMPORTANT: The calling routine call "free" on the returned pointer once
  1730. finished with it.
  1731. Entry:
  1732. fp - file to read from
  1733. Exit:
  1734. nwords - number of words returned
  1735. orig_line - the original line of characters
  1736. returns a list of words from the line, or NULL if end-of-file
  1737. ******************************************************************************/
  1738. inline char **get_words(FILE *fp, int *nwords, char **orig_line)
  1739. {
  1740. #define BIG_STRING 4096
  1741. char str[BIG_STRING];
  1742. char str_copy[BIG_STRING];
  1743. char **words;
  1744. int max_words = 10;
  1745. int num_words = 0;
  1746. char *ptr,*ptr2;
  1747. char *result;
  1748. fpos_t pos; //keep track of file pointer
  1749. int nbytes;
  1750. int nonUNIX;
  1751. nonUNIX=0;
  1752. nbytes=0;
  1753. fgetpos(fp, &pos);
  1754. words = (char **) myalloc (sizeof (char *) * max_words);
  1755. /* read in a line */
  1756. result = fgets (str, BIG_STRING, fp);
  1757. if (result == NULL) {
  1758. *nwords = 0;
  1759. *orig_line = NULL;
  1760. return (NULL);
  1761. }
  1762. /* convert line-feed and tabs into spaces */
  1763. /* (this guarantees that there will be a space before the */
  1764. /* null character at the end of the string) */
  1765. str[BIG_STRING-2] = ' ';
  1766. str[BIG_STRING-1] = '\0';
  1767. for (ptr = str, ptr2 = str_copy; *ptr != '\0'; ptr++, ptr2++) {
  1768. *ptr2 = *ptr;
  1769. nbytes++;
  1770. if (*ptr == '\t') {
  1771. *ptr = ' ';
  1772. *ptr2 = ' ';
  1773. }
  1774. else if (*ptr == '\n') {
  1775. *ptr = ' '; //has to have a space, to be caught later when grouping words
  1776. *ptr2 = '\0';
  1777. break;
  1778. }
  1779. else if (*ptr == '\r')
  1780. { //MAC line break
  1781. nonUNIX=1;
  1782. if(*(ptr+1)=='\n') //actuall PC line break
  1783. {
  1784. nbytes++;
  1785. }
  1786. *ptr = ' ';
  1787. *(ptr+1) = '\0'; //when reading mac, best end string here
  1788. *ptr2 = '\0'; //note a pc \r is followed by \n
  1789. break;
  1790. }
  1791. }
  1792. /*check to see if a PC or MAC header was detected instead of UNIX*/
  1793. if(nonUNIX==1)
  1794. {
  1795. fsetpos(fp, &pos);
  1796. fseek(fp, nbytes, SEEK_CUR);
  1797. }
  1798. /* find the words in the line */
  1799. ptr = str;
  1800. while (*ptr != '\0') {
  1801. /* jump over leading spaces */
  1802. while (*ptr == ' ')
  1803. ptr++;
  1804. /* break if we reach the end */
  1805. if (*ptr == '\0')
  1806. break;
  1807. /* save pointer to beginning of word */
  1808. if (num_words >= max_words) {
  1809. max_words += 10;
  1810. words = (char **) realloc (words, sizeof (char *) * max_words);
  1811. }
  1812. words[num_words++] = ptr;
  1813. /* jump over non-spaces */
  1814. while (*ptr != ' ')
  1815. ptr++;
  1816. /* place a null character here to mark the end of the word */
  1817. *ptr++ = '\0';
  1818. }
  1819. /* return the list of words */
  1820. *nwords = num_words;
  1821. *orig_line = str_copy;
  1822. return (words);
  1823. }
  1824. /*
  1825. char **get_words(FILE *fp, int *nwords, char **orig_line)
  1826. {
  1827. #define BIG_STRING 4096
  1828. static char str[BIG_STRING];
  1829. static char str_copy[BIG_STRING];
  1830. char **words;
  1831. int max_words = 10;
  1832. int num_words = 0;
  1833. char *ptr,*ptr2;
  1834. char *result;
  1835. words = (char **) myalloc (sizeof (char *) * max_words);
  1836. // read in a line
  1837. result = fgets (str, BIG_STRING, fp);
  1838. if (result == NULL) {
  1839. *nwords = 0;
  1840. *orig_line = NULL;
  1841. return (NULL);
  1842. }
  1843. // convert line-feed and tabs into spaces
  1844. // (this guarantees that there will be a space before the
  1845. // null character at the end of the string)
  1846. str[BIG_STRING-2] = ' ';
  1847. str[BIG_STRING-1] = '\0';
  1848. for (ptr = str, ptr2 = str_copy; *ptr != '\0'; ptr++, ptr2++) {
  1849. *ptr2 = *ptr;
  1850. if (*ptr == '\t') {
  1851. *ptr = ' ';
  1852. *ptr2 = ' ';
  1853. }
  1854. else if (*ptr == '\n') {
  1855. *ptr = ' ';
  1856. *ptr2 = '\0';
  1857. break;
  1858. }
  1859. else if (*ptr == '\r') {
  1860. *ptr = '\0';
  1861. *ptr2 = '\0'; //note don't break yet, on a pc \r is followed by \n
  1862. }
  1863. }
  1864. // find the words in the line
  1865. ptr = str;
  1866. while (*ptr != '\0') {
  1867. // jump over leading spaces
  1868. while (*ptr == ' ')
  1869. ptr++;
  1870. // break if we reach the end
  1871. if (*ptr == '\0')
  1872. break;
  1873. // save pointer to beginning of word
  1874. if (num_words >= max_words) {
  1875. max_words += 10;
  1876. words = (char **) realloc (words, sizeof (char *) * max_words);
  1877. }
  1878. words[num_words++] = ptr;
  1879. // jump over non-spaces
  1880. while (*ptr != ' ')
  1881. ptr++;
  1882. // place a null character here to mark the end of the word
  1883. *ptr++ = '\0';
  1884. }
  1885. // return the list of words
  1886. *nwords = num_words;
  1887. *orig_line = str_copy;
  1888. return (words);
  1889. }*/
  1890. /******************************************************************************
  1891. Return the value of an item, given a pointer to it and its type.
  1892. Entry:
  1893. item - pointer to item
  1894. type - data type that "item" points to
  1895. Exit:
  1896. returns a double-precision float that contains the value of the item
  1897. ******************************************************************************/
  1898. inline double get_item_value(char *item, int type)
  1899. {
  1900. unsigned char *puchar;
  1901. char *pchar;
  1902. short int *pshort;
  1903. unsigned short int *pushort;
  1904. int *pint;
  1905. unsigned int *puint;
  1906. float *pfloat;
  1907. double *pdouble;
  1908. int int_value;
  1909. unsigned int uint_value;
  1910. double double_value;
  1911. switch (type) {
  1912. case PLY_CHAR:
  1913. pchar = (char *) item;
  1914. int_value = *pchar;
  1915. return ((double) int_value);
  1916. case PLY_UCHAR:
  1917. puchar = (unsigned char *) item;
  1918. int_value = *puchar;
  1919. return ((double) int_value);
  1920. case PLY_SHORT:
  1921. pshort = (short int *) item;
  1922. int_value = *pshort;
  1923. return ((double) int_value);
  1924. case PLY_USHORT:
  1925. pushort = (unsigned short int *) item;
  1926. int_value = *pushort;
  1927. return ((double) int_value);
  1928. case PLY_INT:
  1929. pint = (int *) item;
  1930. int_value = *pint;
  1931. return ((double) int_value);
  1932. case PLY_UINT:
  1933. puint = (unsigned int *) item;
  1934. uint_value = *puint;
  1935. return ((double) uint_value);
  1936. case PLY_FLOAT:
  1937. pfloat = (float *) item;
  1938. double_value = *pfloat;
  1939. return (double_value);
  1940. case PLY_DOUBLE:
  1941. pdouble = (double *) item;
  1942. double_value = *pdouble;
  1943. return (double_value);
  1944. default:
  1945. fprintf (stderr, "get_item_value: bad type = %d\n", type);
  1946. exit (-1);
  1947. }
  1948. }
  1949. /******************************************************************************
  1950. Write out an item to a file as raw binary bytes.
  1951. Entry:
  1952. fp - file to write to
  1953. int_val - integer version of item
  1954. uint_val - unsigned integer version of item
  1955. double_val - double-precision float version of item
  1956. type - data type to write out
  1957. ******************************************************************************/
  1958. inline void write_binary_item(
  1959. FILE *fp,
  1960. int file_type,
  1961. int int_val,
  1962. unsigned int uint_val,
  1963. double double_val,
  1964. int type,
  1965. int *native_binary_type
  1966. )
  1967. {
  1968. unsigned char uchar_val;
  1969. char char_val;
  1970. unsigned short ushort_val;
  1971. short short_val;
  1972. float float_val;
  1973. void *value;
  1974. switch (type) {
  1975. case PLY_CHAR:
  1976. char_val = int_val;
  1977. value = &char_val;
  1978. break;
  1979. case PLY_SHORT:
  1980. short_val = int_val;
  1981. value = &short_val;
  1982. break;
  1983. case PLY_INT:
  1984. value = &int_val;
  1985. break;
  1986. case PLY_UCHAR:
  1987. uchar_val = uint_val;
  1988. value = &uchar_val;
  1989. break;
  1990. case PLY_USHORT:
  1991. ushort_val = uint_val;
  1992. value = &ushort_val;
  1993. break;
  1994. case PLY_UINT:
  1995. value = &uint_val;
  1996. break;
  1997. case PLY_FLOAT:
  1998. float_val = double_val;
  1999. value = &float_val;
  2000. break;
  2001. case PLY_DOUBLE:
  2002. value = &double_val;
  2003. break;
  2004. default:
  2005. fprintf (stderr, "write_binary_item: bad type = %d\n", type);
  2006. exit (-1);
  2007. }
  2008. int ply_type_size[] = {
  2009. 0, 1, 2, 4, 1, 2, 4, 4, 8
  2010. };
  2011. if ((file_type != *native_binary_type) && (ply_type_size[type] > 1))
  2012. swap_bytes((char *)value, ply_type_size[type]);
  2013. if (fwrite (value, ply_type_size[type], 1, fp) != 1)
  2014. {
  2015. fprintf(stderr, "PLY ERROR: fwrite() failed -- aborting.\n");
  2016. exit(1);
  2017. }
  2018. }
  2019. /******************************************************************************
  2020. Write out an item to a file as ascii characters.
  2021. Entry:
  2022. fp - file to write to
  2023. int_val - integer version of item
  2024. uint_val - unsigned integer version of item
  2025. double_val - double-precision float version of item
  2026. type - data type to write out
  2027. ******************************************************************************/
  2028. inline void write_ascii_item(
  2029. FILE *fp,
  2030. int int_val,
  2031. unsigned int uint_val,
  2032. double double_val,
  2033. int type
  2034. )
  2035. {
  2036. switch (type) {
  2037. case PLY_CHAR:
  2038. case PLY_SHORT:
  2039. case PLY_INT:
  2040. if (fprintf (fp, "%d ", int_val) <= 0)
  2041. {
  2042. fprintf(stderr, "PLY ERROR: fprintf() failed -- aborting.\n");
  2043. exit(1);
  2044. }
  2045. break;
  2046. case PLY_UCHAR:
  2047. case PLY_USHORT:
  2048. case PLY_UINT:
  2049. if (fprintf (fp, "%u ", uint_val) <= 0)
  2050. {
  2051. fprintf(stderr, "PLY ERROR: fprintf() failed -- aborting.\n");
  2052. exit(1);
  2053. }
  2054. break;
  2055. case PLY_FLOAT:
  2056. case PLY_DOUBLE:
  2057. if (fprintf (fp, "%g ", double_val) <= 0)
  2058. {
  2059. fprintf(stderr, "PLY ERROR: fprintf() failed -- aborting.\n");
  2060. exit(1);
  2061. }
  2062. break;
  2063. default:
  2064. fprintf (stderr, "write_ascii_item: bad type = %d\n", type);
  2065. exit (-1);
  2066. }
  2067. }
  2068. /******************************************************************************
  2069. Write out an item to a file as ascii characters.
  2070. Entry:
  2071. fp - file to write to
  2072. item - pointer to item to write
  2073. type - data type that "item" points to
  2074. Exit:
  2075. returns a double-precision float that contains the value of the written item
  2076. ******************************************************************************/
  2077. inline double old_write_ascii_item(FILE *fp, char *item, int type)
  2078. {
  2079. unsigned char *puchar;
  2080. char *pchar;
  2081. short int *pshort;
  2082. unsigned short int *pushort;
  2083. int *pint;
  2084. unsigned int *puint;
  2085. float *pfloat;
  2086. double *pdouble;
  2087. int int_value;
  2088. unsigned int uint_value;
  2089. double double_value;
  2090. switch (type) {
  2091. case PLY_CHAR:
  2092. pchar = (char *) item;
  2093. int_value = *pchar;
  2094. fprintf (fp, "%d ", int_value);
  2095. return ((double) int_value);
  2096. case PLY_UCHAR:
  2097. puchar = (unsigned char *) item;
  2098. int_value = *puchar;
  2099. fprintf (fp, "%d ", int_value);
  2100. return ((double) int_value);
  2101. case PLY_SHORT:
  2102. pshort = (short int *) item;
  2103. int_value = *pshort;
  2104. fprintf (fp, "%d ", int_value);
  2105. return ((double) int_value);
  2106. case PLY_USHORT:
  2107. pushort = (unsigned short int *) item;
  2108. int_value = *pushort;
  2109. fprintf (fp, "%d ", int_value);
  2110. return ((double) int_value);
  2111. case PLY_INT:
  2112. pint = (int *) item;
  2113. int_value = *pint;
  2114. fprintf (fp, "%d ", int_value);
  2115. return ((double) int_value);
  2116. case PLY_UINT:
  2117. puint = (unsigned int *) item;
  2118. uint_value = *puint;
  2119. fprintf (fp, "%u ", uint_value);
  2120. return ((double) uint_value);
  2121. case PLY_FLOAT:
  2122. pfloat = (float *) item;
  2123. double_value = *pfloat;
  2124. fprintf (fp, "%g ", double_value);
  2125. return (double_value);
  2126. case PLY_DOUBLE:
  2127. pdouble = (double *) item;
  2128. double_value = *pdouble;
  2129. fprintf (fp, "%g ", double_value);
  2130. return (double_value);
  2131. default:
  2132. fprintf (stderr, "old_write_ascii_item: bad type = %d\n", type);
  2133. exit (-1);
  2134. }
  2135. }
  2136. /******************************************************************************
  2137. Get the value of an item that is in memory, and place the result
  2138. into an integer, an unsigned integer and a double.
  2139. Entry:
  2140. ptr - pointer to the item
  2141. type - data type supposedly in the item
  2142. Exit:
  2143. int_val - integer value
  2144. uint_val - unsigned integer value
  2145. double_val - double-precision floating point value
  2146. ******************************************************************************/
  2147. inline void get_stored_item(
  2148. void *ptr,
  2149. int type,
  2150. int *int_val,
  2151. unsigned int *uint_val,
  2152. double *double_val
  2153. )
  2154. {
  2155. switch (type) {
  2156. case PLY_CHAR:
  2157. *int_val = *((char *) ptr);
  2158. *uint_val = *int_val;
  2159. *double_val = *int_val;
  2160. break;
  2161. case PLY_UCHAR:
  2162. *uint_val = *((unsigned char *) ptr);
  2163. *int_val = *uint_val;
  2164. *double_val = *uint_val;
  2165. break;
  2166. case PLY_SHORT:
  2167. *int_val = *((short int *) ptr);
  2168. *uint_val = *int_val;
  2169. *double_val = *int_val;
  2170. break;
  2171. case PLY_USHORT:
  2172. *uint_val = *((unsigned short int *) ptr);
  2173. *int_val = *uint_val;
  2174. *double_val = *uint_val;
  2175. break;
  2176. case PLY_INT:
  2177. *int_val = *((int *) ptr);
  2178. *uint_val = *int_val;
  2179. *double_val = *int_val;
  2180. break;
  2181. case PLY_UINT:
  2182. *uint_val = *((unsigned int *) ptr);
  2183. *int_val = *uint_val;
  2184. *double_val = *uint_val;
  2185. break;
  2186. case PLY_FLOAT:
  2187. *double_val = *((float *) ptr);
  2188. *int_val = (int) *double_val;
  2189. *uint_val = (unsigned int) *double_val;
  2190. break;
  2191. case PLY_DOUBLE:
  2192. *double_val = *((double *) ptr);
  2193. *int_val = (int) *double_val;
  2194. *uint_val = (unsigned int) *double_val;
  2195. break;
  2196. default:
  2197. fprintf (stderr, "get_stored_item: bad type = %d\n", type);
  2198. exit (-1);
  2199. }
  2200. }
  2201. /******************************************************************************
  2202. Get the value of an item from a binary file, and place the result
  2203. into an integer, an unsigned integer and a double.
  2204. Entry:
  2205. fp - file to get item from
  2206. type - data type supposedly in the word
  2207. Exit:
  2208. int_val - integer value
  2209. uint_val - unsigned integer value
  2210. double_val - double-precision floating point value
  2211. ******************************************************************************/
  2212. inline void get_binary_item(
  2213. FILE *fp,
  2214. int file_type,
  2215. int type,
  2216. int *int_val,
  2217. unsigned int *uint_val,
  2218. double *double_val,
  2219. int *native_binary_type
  2220. )
  2221. {
  2222. char c[8];
  2223. void *ptr;
  2224. ptr = (void *) c;
  2225. int ply_type_size[] = {
  2226. 0, 1, 2, 4, 1, 2, 4, 4, 8
  2227. };
  2228. if (fread (ptr, ply_type_size[type], 1, fp) != 1)
  2229. {
  2230. fprintf(stderr, "PLY ERROR: fread() failed -- aborting.\n");
  2231. exit(1);
  2232. }
  2233. if ((file_type != *native_binary_type) && (ply_type_size[type] > 1))
  2234. swap_bytes((char *)ptr, ply_type_size[type]);
  2235. switch (type) {
  2236. case PLY_CHAR:
  2237. *int_val = *((char *) ptr);
  2238. *uint_val = *int_val;
  2239. *double_val = *int_val;
  2240. break;
  2241. case PLY_UCHAR:
  2242. *uint_val = *((unsigned char *) ptr);
  2243. *int_val = *uint_val;
  2244. *double_val = *uint_val;
  2245. break;
  2246. case PLY_SHORT:
  2247. *int_val = *((short int *) ptr);
  2248. *uint_val = *int_val;
  2249. *double_val = *int_val;
  2250. break;
  2251. case PLY_USHORT:
  2252. *uint_val = *((unsigned short int *) ptr);
  2253. *int_val = *uint_val;
  2254. *double_val = *uint_val;
  2255. break;
  2256. case PLY_INT:
  2257. *int_val = *((int *) ptr);
  2258. *uint_val = *int_val;
  2259. *double_val = *int_val;
  2260. break;
  2261. case PLY_UINT:
  2262. *uint_val = *((unsigned int *) ptr);
  2263. *int_val = *uint_val;
  2264. *double_val = *uint_val;
  2265. break;
  2266. case PLY_FLOAT:
  2267. *double_val = *((float *) ptr);
  2268. *int_val = (int) *double_val;
  2269. *uint_val = (unsigned int) *double_val;
  2270. break;
  2271. case PLY_DOUBLE:
  2272. *double_val = *((double *) ptr);
  2273. *int_val = (int) *double_val;
  2274. *uint_val = (unsigned int) *double_val;
  2275. break;
  2276. default:
  2277. fprintf (stderr, "get_binary_item: bad type = %d\n", type);
  2278. exit (-1);
  2279. }
  2280. }
  2281. /******************************************************************************
  2282. Extract the value of an item from an ascii word, and place the result
  2283. into an integer, an unsigned integer and a double.
  2284. Entry:
  2285. word - word to extract value from
  2286. type - data type supposedly in the word
  2287. Exit:
  2288. int_val - integer value
  2289. uint_val - unsigned integer value
  2290. double_val - double-precision floating point value
  2291. ******************************************************************************/
  2292. inline void get_ascii_item(
  2293. char *word,
  2294. int type,
  2295. int *int_val,
  2296. unsigned int *uint_val,
  2297. double *double_val
  2298. )
  2299. {
  2300. switch (type) {
  2301. case PLY_CHAR:
  2302. case PLY_UCHAR:
  2303. case PLY_SHORT:
  2304. case PLY_USHORT:
  2305. case PLY_INT:
  2306. *int_val = atoi (word);
  2307. *uint_val = (unsigned int) *int_val;
  2308. *double_val = (double) *int_val;
  2309. break;
  2310. case PLY_UINT:
  2311. *uint_val = strtol (word, (char **) NULL, 10);
  2312. *int_val = (int) *uint_val;
  2313. *double_val = (double) *uint_val;
  2314. break;
  2315. case PLY_FLOAT:
  2316. case PLY_DOUBLE:
  2317. *double_val = atof (word);
  2318. *int_val = (int) *double_val;
  2319. *uint_val = (unsigned int) *double_val;
  2320. break;
  2321. default:
  2322. fprintf (stderr, "get_ascii_item: bad type = %d\n", type);
  2323. exit (-1);
  2324. }
  2325. }
  2326. /******************************************************************************
  2327. Store a value into a place being pointed to, guided by a data type.
  2328. Entry:
  2329. item - place to store value
  2330. type - data type
  2331. int_val - integer version of value
  2332. uint_val - unsigned integer version of value
  2333. double_val - double version of value
  2334. Exit:
  2335. item - pointer to stored value
  2336. ******************************************************************************/
  2337. inline void store_item (
  2338. char *item,
  2339. int type,
  2340. int int_val,
  2341. unsigned int uint_val,
  2342. double double_val
  2343. )
  2344. {
  2345. unsigned char *puchar;
  2346. short int *pshort;
  2347. unsigned short int *pushort;
  2348. int *pint;
  2349. unsigned int *puint;
  2350. float *pfloat;
  2351. double *pdouble;
  2352. switch (type) {
  2353. case PLY_CHAR:
  2354. *item = int_val;
  2355. break;
  2356. case PLY_UCHAR:
  2357. puchar = (unsigned char *) item;
  2358. *puchar = uint_val;
  2359. break;
  2360. case PLY_SHORT:
  2361. pshort = (short *) item;
  2362. *pshort = int_val;
  2363. break;
  2364. case PLY_USHORT:
  2365. pushort = (unsigned short *) item;
  2366. *pushort = uint_val;
  2367. break;
  2368. case PLY_INT:
  2369. pint = (int *) item;
  2370. *pint = int_val;
  2371. break;
  2372. case PLY_UINT:
  2373. puint = (unsigned int *) item;
  2374. *puint = uint_val;
  2375. break;
  2376. case PLY_FLOAT:
  2377. pfloat = (float *) item;
  2378. *pfloat = double_val;
  2379. break;
  2380. case PLY_DOUBLE:
  2381. pdouble = (double *) item;
  2382. *pdouble = double_val;
  2383. break;
  2384. default:
  2385. fprintf (stderr, "store_item: bad type = %d\n", type);
  2386. exit (-1);
  2387. }
  2388. }
  2389. /******************************************************************************
  2390. Add an element to a PLY file descriptor.
  2391. Entry:
  2392. plyfile - PLY file descriptor
  2393. words - list of words describing the element
  2394. nwords - number of words in the list
  2395. ******************************************************************************/
  2396. inline void add_element (PlyFile *plyfile, char **words)
  2397. {
  2398. PlyElement *elem;
  2399. /* create the new element */
  2400. elem = (PlyElement *) myalloc (sizeof (PlyElement));
  2401. elem->name = strdup (words[1]);
  2402. elem->num = atoi (words[2]);
  2403. elem->nprops = 0;
  2404. /* make room for new element in the object's list of elements */
  2405. if (plyfile->nelems == 0)
  2406. plyfile->elems = (PlyElement **) myalloc (sizeof (PlyElement *));
  2407. else
  2408. plyfile->elems = (PlyElement **) realloc (plyfile->elems,
  2409. sizeof (PlyElement *) * (plyfile->nelems + 1));
  2410. /* add the new element to the object's list */
  2411. plyfile->elems[plyfile->nelems] = elem;
  2412. plyfile->nelems++;
  2413. }
  2414. /******************************************************************************
  2415. Return the type of a property, given the name of the property.
  2416. Entry:
  2417. name - name of property type
  2418. Exit:
  2419. returns integer code for property, or 0 if not found
  2420. ******************************************************************************/
  2421. inline int get_prop_type(char *type_name)
  2422. {
  2423. int i;
  2424. const char *type_names[] = {
  2425. "invalid",
  2426. "char", "short", "int",
  2427. "uchar", "ushort", "uint",
  2428. "float", "double",
  2429. };
  2430. const char *alt_type_names[] = {
  2431. "invalid",
  2432. "int8", "int16", "int32", "uint8", "uint16", "uint32", "float32", "float64",
  2433. };
  2434. for (i = PLY_START_TYPE + 1; i < PLY_END_TYPE; i++)
  2435. if (equal_strings (type_name, type_names[i]))
  2436. return (i);
  2437. for (i = PLY_START_TYPE + 1; i < PLY_END_TYPE; i++)
  2438. if (equal_strings (type_name, alt_type_names[i]))
  2439. return (i);
  2440. /* if we get here, we didn't find the type */
  2441. return (0);
  2442. }
  2443. /******************************************************************************
  2444. Add a property to a PLY file descriptor.
  2445. Entry:
  2446. plyfile - PLY file descriptor
  2447. words - list of words describing the property
  2448. nwords - number of words in the list
  2449. ******************************************************************************/
  2450. inline void add_property (PlyFile *plyfile, char **words)
  2451. {
  2452. PlyProperty *prop;
  2453. PlyElement *elem;
  2454. /* create the new property */
  2455. prop = (PlyProperty *) myalloc (sizeof (PlyProperty));
  2456. if (equal_strings (words[1], "list")) { /* is a list */
  2457. prop->count_external = get_prop_type (words[2]);
  2458. prop->external_type = get_prop_type (words[3]);
  2459. prop->name = strdup (words[4]);
  2460. prop->is_list = 1;
  2461. }
  2462. else { /* not a list */
  2463. prop->external_type = get_prop_type (words[1]);
  2464. prop->name = strdup (words[2]);
  2465. prop->is_list = 0;
  2466. }
  2467. /* add this property to the list of properties of the current element */
  2468. elem = plyfile->elems[plyfile->nelems - 1];
  2469. if (elem->nprops == 0)
  2470. elem->props = (PlyProperty **) myalloc (sizeof (PlyProperty *));
  2471. else
  2472. elem->props = (PlyProperty **) realloc (elem->props,
  2473. sizeof (PlyProperty *) * (elem->nprops + 1));
  2474. elem->props[elem->nprops] = prop;
  2475. elem->nprops++;
  2476. }
  2477. /******************************************************************************
  2478. Add a comment to a PLY file descriptor.
  2479. Entry:
  2480. plyfile - PLY file descriptor
  2481. line - line containing comment
  2482. ******************************************************************************/
  2483. inline void add_comment (PlyFile *plyfile, char *line)
  2484. {
  2485. int i;
  2486. /* skip over "comment" and leading spaces and tabs */
  2487. i = 7;
  2488. while (line[i] == ' ' || line[i] == '\t')
  2489. i++;
  2490. ply_put_comment (plyfile, &line[i]);
  2491. }
  2492. /******************************************************************************
  2493. Add a some object information to a PLY file descriptor.
  2494. Entry:
  2495. plyfile - PLY file descriptor
  2496. line - line containing text info
  2497. ******************************************************************************/
  2498. inline void add_obj_info (PlyFile *plyfile, char *line)
  2499. {
  2500. int i;
  2501. /* skip over "obj_info" and leading spaces and tabs */
  2502. i = 8;
  2503. while (line[i] == ' ' || line[i] == '\t')
  2504. i++;
  2505. ply_put_obj_info (plyfile, &line[i]);
  2506. }
  2507. /******************************************************************************
  2508. Copy a property.
  2509. ******************************************************************************/
  2510. inline void copy_property(PlyProperty *dest, PlyProperty *src)
  2511. {
  2512. dest->name = strdup (src->name);
  2513. dest->external_type = src->external_type;
  2514. dest->internal_type = src->internal_type;
  2515. dest->offset = src->offset;
  2516. dest->is_list = src->is_list;
  2517. dest->count_external = src->count_external;
  2518. dest->count_internal = src->count_internal;
  2519. dest->count_offset = src->count_offset;
  2520. }
  2521. /******************************************************************************
  2522. Allocate some memory.
  2523. Entry:
  2524. size - amount of memory requested (in bytes)
  2525. lnum - line number from which memory was requested
  2526. fname - file name from which memory was requested
  2527. ******************************************************************************/
  2528. inline char *my_alloc(int size, int lnum, const char *fe)
  2529. {
  2530. char *ptr;
  2531. ptr = (char *) malloc (size);
  2532. if (ptr == 0) {
  2533. fprintf(stderr, "Memory allocation bombed on line %d in %s\n", lnum, fe);
  2534. }
  2535. return (ptr);
  2536. }
  2537. }
  2538. }
  2539. #endif