ply.h 87 KB

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