io.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740
  1. /*************************************/
  2. /* Auteur : Rémi Synave */
  3. /* Date de création : 10/03/10 */
  4. /* Date de modification : 18/09/16 */
  5. /* Version : 0.4 */
  6. /*************************************/
  7. /***************************************************************************/
  8. /* This file is part of a2ri. */
  9. /* */
  10. /* a2ri is free software: you can redistribute it and/or modify it */
  11. /* under the terms of the GNU Lesser General Public License as published */
  12. /* by the Free Software Foundation, either version 3 of the License, or */
  13. /* (at your option) any later version. */
  14. /* */
  15. /* a2ri is distributed in the hope that it will be useful, */
  16. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  17. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  18. /* GNU Lesser General Public License for more details. */
  19. /* */
  20. /* You should have received a copy of the GNU Lesser General Public */
  21. /* License along with a2ri. */
  22. /* If not, see <http://www.gnu.org/licenses/>. */
  23. /***************************************************************************/
  24. #include "io.h"
  25. /********** INTERMEDIATE TYPES AND FUNCTIONS **********/
  26. /* Les fonctions intermédiaires sont préfixées de IF */
  27. /* et les types intermédiaires de IT */
  28. void
  29. IFaffichage_chargement (
  30. FILE * is,
  31. long posmax)
  32. {
  33. long poscur;
  34. poscur = ftell (is);
  35. printf ("%c%c%c%c%c%c%c%c%c%c%c%c%c%lf %c", 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  36. 8, 8, ((poscur) * 100.0) / (posmax * 1.0), 37);
  37. }
  38. //fonction qui ecrit les aretes dans un fichier vef
  39. void IFsave_vef_write_edge (
  40. const vf_model * const m,
  41. hashtable * table,
  42. FILE * os)
  43. {
  44. int p1,
  45. p2;
  46. int indexarete = 0;
  47. //pour toutes les faces, on regarde si l'arete existe deja dans la table de hachage
  48. //si oui, on ne fait rien
  49. //sinon on entre l'arete dans la table et on l'ecrit dans le fichier.
  50. //on enregistre également son index pour l'écriture des faces définies par trois aretes
  51. for (int i = 0; i < m->nbface; i++)
  52. {
  53. p1 = m->fa[i].ve1;
  54. p2 = m->fa[i].ve2;
  55. vf_edge *edgetemp = hashtable_look_for (table, p1, p2);
  56. if (edgetemp == NULL)
  57. {
  58. edgetemp = (vf_edge *) malloc (sizeof (vf_edge));
  59. a2ri_erreur_critique_si (edgetemp == NULL,
  60. "erreur allocation memoire pour edgetemp\nIFsave_vef_write_edge");
  61. edgetemp->ve1 = p1;
  62. edgetemp->ve2 = p2;
  63. edgetemp->nbsharedfaces=0;
  64. edgetemp->sharedfaces=NULL;
  65. edgetemp->att_int = indexarete;
  66. indexarete++;
  67. fprintf (os, "%d %d\n", p1, p2);
  68. hashtable_add (table, edgetemp);
  69. }
  70. p1 = m->fa[i].ve2;
  71. p2 = m->fa[i].ve3;
  72. edgetemp = hashtable_look_for (table, p1, p2);
  73. if (edgetemp == NULL)
  74. {
  75. edgetemp = (vf_edge *) malloc (sizeof (vf_edge));
  76. a2ri_erreur_critique_si (edgetemp == NULL,
  77. "erreur allocation memoire pour edgetemp\nIFsave_vef_write_edge");
  78. edgetemp->ve1 = p1;
  79. edgetemp->ve2 = p2;
  80. edgetemp->nbsharedfaces=0;
  81. edgetemp->sharedfaces=NULL;
  82. edgetemp->att_int = indexarete;
  83. indexarete++;
  84. fprintf (os, "%d %d\n", p1, p2);
  85. hashtable_add (table, edgetemp);
  86. }
  87. p1 = m->fa[i].ve3;
  88. p2 = m->fa[i].ve1;
  89. edgetemp = hashtable_look_for (table, p1, p2);
  90. if (edgetemp == NULL)
  91. {
  92. edgetemp = (vf_edge *) malloc (sizeof (vf_edge));
  93. a2ri_erreur_critique_si (edgetemp == NULL,
  94. "erreur allocation memoire pour edgetemp\nIFsave_vef_write_edge");
  95. edgetemp->ve1 = p1;
  96. edgetemp->ve2 = p2;
  97. edgetemp->nbsharedfaces=0;
  98. edgetemp->sharedfaces=NULL;
  99. edgetemp->att_int = indexarete;
  100. indexarete++;
  101. fprintf (os, "%d %d\n", p1, p2);
  102. hashtable_add (table, edgetemp);
  103. }
  104. }
  105. }
  106. //fonction qui ecrit les aretes dans un fichier gts
  107. void IFsave_gts_write_edge (
  108. const vf_model * const m,
  109. hashtable * table,
  110. FILE * os)
  111. {
  112. int p1,
  113. p2;
  114. int indexarete = 0;
  115. //pour toute les faces, on regarde si l'arete existe deja dans la table de hachage
  116. //si oui, on ne fait rien
  117. //sinon on entre l'arete dans la table et on l'ecrit dans le fichier.
  118. //on enregistre également son index pour l'écriture des faces définies par trois aretes
  119. for (int i = 0; i < m->nbface; i++)
  120. {
  121. p1 = m->fa[i].ve1;
  122. p2 = m->fa[i].ve2;
  123. vf_edge *edgetemp = hashtable_look_for (table, p1, p2);
  124. if (edgetemp == NULL)
  125. {
  126. edgetemp = (vf_edge *) malloc (sizeof (vf_edge));
  127. a2ri_erreur_critique_si (edgetemp == NULL,
  128. "erreur allocation memoire pour edgetemp\nIFsave_gts_write_edge");
  129. edgetemp->ve1 = p1;
  130. edgetemp->ve2 = p2;
  131. edgetemp->att_int = indexarete;
  132. indexarete++;
  133. fprintf (os, "%d %d\n", p1 + 1, p2 + 1);
  134. hashtable_add (table, edgetemp);
  135. }
  136. p1 = m->fa[i].ve2;
  137. p2 = m->fa[i].ve3;
  138. edgetemp = hashtable_look_for (table, p1, p2);
  139. if (edgetemp == NULL)
  140. {
  141. edgetemp = (vf_edge *) malloc (sizeof (vf_edge));
  142. a2ri_erreur_critique_si (edgetemp == NULL,
  143. "erreur allocation memoire pour edgetemp\nIFsave_gts_write_edge");
  144. edgetemp->ve1 = p1;
  145. edgetemp->ve2 = p2;
  146. edgetemp->att_int = indexarete;
  147. indexarete++;
  148. fprintf (os, "%d %d\n", p1 + 1, p2 + 1);
  149. hashtable_add (table, edgetemp);
  150. }
  151. p1 = m->fa[i].ve3;
  152. p2 = m->fa[i].ve1;
  153. edgetemp = hashtable_look_for (table, p1, p2);
  154. if (edgetemp == NULL)
  155. {
  156. edgetemp = (vf_edge *) malloc (sizeof (vf_edge));
  157. a2ri_erreur_critique_si (edgetemp == NULL,
  158. "erreur allocation memoire pour edgetemp\nIFsave_gts_write_edge");
  159. edgetemp->ve1 = p1;
  160. edgetemp->ve2 = p2;
  161. edgetemp->att_int = indexarete;
  162. indexarete++;
  163. fprintf (os, "%d %d\n", p1 + 1, p2 + 1);
  164. hashtable_add (table, edgetemp);
  165. }
  166. }
  167. }
  168. /********** MAIN FUNCTIONS **********/
  169. /**
  170. Ouverture d'un fichier représentant un squelette
  171. @param file chemin vers le fichier à ouvrir
  172. @param s pointeur vers le squelette
  173. @return 1 si succès, 0 sinon
  174. */
  175. int a2ri_skeleton_open_file(
  176. const char * const file,
  177. skeleton * s)
  178. {
  179. //TODO
  180. char ext[5];
  181. FILE *is = NULL;
  182. int nbvertex,
  183. nbedge,
  184. ve1,
  185. ve2;
  186. double coordx,
  187. coordy,
  188. coordz;
  189. memcpy (ext, file + (strlen (file) - 4), 4);
  190. ext[4] = 0;
  191. //appel de la bonne fonction en fonction de l'extension du fichier
  192. if (strcmp (ext, ".skl"))
  193. return 0;
  194. setlocale(LC_NUMERIC,"C");
  195. is = fopen (file, "r");
  196. if (is == NULL)
  197. return 0;
  198. //lecture du nombre de sommets et de faces
  199. a2ri_erreur_critique_si(!fscanf (is, "%d %d", &nbvertex, &nbedge),"erreur en ouverture de fichier");
  200. //lecture des sommets
  201. for (int i = 0; i < nbvertex; i++)
  202. {
  203. a2ri_erreur_critique_si(!fscanf (is, "%lf %lf %lf", &coordx, &coordy, &coordz),"erreur en ouverture de fichier");
  204. a2ri_skeleton_add_vertex (s, coordx, coordy, coordz);
  205. }
  206. //lecture des arêtes
  207. for (int i = 0; i < nbedge; i++)
  208. {
  209. a2ri_erreur_critique_si(!fscanf (is, "%d %d", &ve1, &ve2),"erreur en ouverture de fichier");
  210. a2ri_skeleton_add_edge (s, ve1, ve2);
  211. }
  212. fclose (is);
  213. setlocale(LC_NUMERIC,"");
  214. return 1;
  215. }
  216. /**
  217. Ouverture d'un fichier contenant un modèle
  218. @param file chemin du fichier à ouvrir
  219. @param m pointeur sur le modèle
  220. @return 1 si succès, 0 sinon
  221. */
  222. int
  223. a2ri_vf_open_file (
  224. const char * const file,
  225. vf_model * m)
  226. {
  227. char ext[5];
  228. memcpy (ext, file + (strlen (file) - 4), 4);
  229. ext[4] = 0;
  230. //appel de la bonne fonction en fonction de l'extension du fichier
  231. if (!strcmp (ext, ".wrl"))
  232. return (a2ri_vf_load_wrl (file, m));
  233. if (!strcmp (ext, ".asc"))
  234. return (a2ri_vf_load_asc (file, m));
  235. if (!strcmp (ext, ".vef"))
  236. return (a2ri_vf_load_vef (file, m));
  237. if (!strcmp (ext, ".pgn"))
  238. return (a2ri_vf_load_pgn (file, m));
  239. if (!strcmp (ext, ".ply"))
  240. return (a2ri_vf_load_ply (file, m));
  241. if (!strcmp (ext, ".off"))
  242. return (a2ri_vf_load_off (file, m));
  243. if (!strcmp (ext, ".obj"))
  244. return (a2ri_vf_load_obj (file, m));
  245. if (!strcmp (ext, ".gts"))
  246. return (a2ri_vf_load_gts (file, m));
  247. if (!strcmp (ext, ".stl"))
  248. return (a2ri_vf_load_stl (file, m));
  249. return 0;
  250. }
  251. /**
  252. Ouverture d'un fichier asc contenant un nuage de point
  253. @param file chemin du fichier à ouvrir
  254. @param m pointeur sur le modèle
  255. @return 1 si succès, 0 sinon
  256. */
  257. int
  258. a2ri_vf_load_asc (
  259. const char * const file,
  260. vf_model * m)
  261. {
  262. FILE *is = NULL;
  263. double coordx,
  264. coordy,
  265. coordz;
  266. int i;
  267. setlocale(LC_NUMERIC,"C");
  268. is = fopen (file, "r");
  269. if (is == NULL)
  270. return 0;
  271. i=0;
  272. a2ri_erreur_critique_si(!fscanf (is, "%lf %lf %lf", &coordx, &coordy, &coordz),"erreur en ouverture de fichier");
  273. while(!feof(is))
  274. {
  275. //fscanf (is, "%lf %lf %lf", &coordx, &coordy, &coordz),"erreur en ouverture de fichier");
  276. a2ri_vf_add_vertex (m, coordx, coordy, coordz);
  277. if (i == 0)
  278. {
  279. m->xmin = coordx;
  280. m->xmax = coordx;
  281. m->ymin = coordy;
  282. m->ymax = coordy;
  283. m->zmin = coordz;
  284. m->zmax = coordz;
  285. }
  286. else
  287. {
  288. //mis à jour de la bounding box du modèle
  289. if (coordx < m->xmin)
  290. m->xmin = coordx;
  291. if (coordx > m->xmax)
  292. m->xmax = coordx;
  293. if (coordy < m->ymin)
  294. m->ymin = coordy;
  295. if (coordy > m->ymax)
  296. m->ymax = coordy;
  297. if (coordz < m->zmin)
  298. m->zmin = coordz;
  299. if (coordz > m->zmax)
  300. m->zmax = coordz;
  301. }
  302. a2ri_erreur_critique_si(!fscanf (is, "%lf %lf %lf", &coordx, &coordy, &coordz),"erreur en ouverture de fichier");
  303. a2ri_erreur_critique_si(!fscanf (is, "%lf %lf %lf", &coordx, &coordy, &coordz),"erreur en ouverture de fichier");
  304. i=1;
  305. }
  306. fclose (is);
  307. setlocale(LC_NUMERIC,"");
  308. return 1;
  309. }
  310. /**
  311. Ouverture d'un fichier pgn contenant un modèle
  312. @param file chemin du fichier à ouvrir
  313. @param m pointeur sur le modèle
  314. @return 1 si succès, 0 sinon
  315. */
  316. int
  317. a2ri_vf_load_pgn (
  318. const char * const file,
  319. vf_model * m)
  320. {
  321. FILE *is = NULL;
  322. int nbvertex,
  323. nbface,
  324. ve1,
  325. ve2,
  326. ve3;
  327. double coordx,
  328. coordy,
  329. coordz;
  330. setlocale(LC_NUMERIC,"C");
  331. is = fopen (file, "r");
  332. if (is == NULL)
  333. return 0;
  334. //lecture du nombre de sommets et de faces
  335. a2ri_erreur_critique_si(!fscanf (is, "%d %d", &nbvertex, &nbface),"erreur en ouverture de fichier");
  336. //lecture des sommets
  337. for (int i = 0; i < nbvertex; i++)
  338. {
  339. a2ri_erreur_critique_si(!fscanf (is, "%lf %lf %lf", &coordx, &coordy, &coordz),"erreur en ouverture de fichier");
  340. a2ri_vf_add_vertex (m, coordx, coordy, coordz);
  341. if (i == 0)
  342. {
  343. m->xmin = coordx;
  344. m->xmax = coordx;
  345. m->ymin = coordy;
  346. m->ymax = coordy;
  347. m->zmin = coordz;
  348. m->zmax = coordz;
  349. }
  350. else
  351. {
  352. //mis à jour de la bounding box du modèle
  353. if (coordx < m->xmin)
  354. m->xmin = coordx;
  355. if (coordx > m->xmax)
  356. m->xmax = coordx;
  357. if (coordy < m->ymin)
  358. m->ymin = coordy;
  359. if (coordy > m->ymax)
  360. m->ymax = coordy;
  361. if (coordz < m->zmin)
  362. m->zmin = coordz;
  363. if (coordz > m->zmax)
  364. m->zmax = coordz;
  365. }
  366. }
  367. //lecture des faces
  368. for (int i = 0; i < nbface; i++)
  369. {
  370. a2ri_erreur_critique_si(!fscanf (is, "%d %d %d", &ve1, &ve2, &ve3),"erreur en ouverture de fichier");
  371. a2ri_vf_add_face (m, ve1, ve2, ve3);
  372. a2ri_erreur_critique_si(!fscanf (is, "%d", &ve1),"erreur en ouverture de fichier");
  373. }
  374. fclose (is);
  375. setlocale(LC_NUMERIC,"");
  376. return 1;
  377. }
  378. /**
  379. Ouverture d'un fichier obj contenant un modèle
  380. @param file chemin du fichier à ouvrir
  381. @param m pointeur sur le modèle
  382. @return 1 si succès, 0 sinon
  383. */
  384. int
  385. a2ri_vf_load_obj (
  386. const char * const file,
  387. vf_model * m)
  388. {
  389. FILE *is = NULL;
  390. int ve1,
  391. ve2,
  392. ve3;
  393. double coordx,
  394. coordy,
  395. coordz;
  396. char str[250],
  397. str_ve1[20],
  398. str_ve2[20],
  399. str_ve3[20];
  400. setlocale(LC_NUMERIC,"C");
  401. is = fopen (file, "r");
  402. if (is == NULL)
  403. return 0;
  404. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  405. while (strcmp (str, "v") != 0)
  406. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  407. //lecture des sommets
  408. while (strcmp (str, "v") == 0 && !feof (is))
  409. {
  410. a2ri_erreur_critique_si(!fscanf (is, "%lf %lf %lf", &coordx, &coordy, &coordz),"erreur en ouverture de fichier");
  411. //ajout d'un sommet
  412. a2ri_vf_add_vertex (m, coordx, coordy, coordz);
  413. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  414. //mis à jour de la bounding box
  415. if (m->nbvertex == 1)
  416. {
  417. m->xmin = coordx;
  418. m->xmax = coordx;
  419. m->ymin = coordy;
  420. m->ymax = coordy;
  421. m->zmin = coordz;
  422. m->zmax = coordz;
  423. }
  424. else
  425. {
  426. if (coordx < m->xmin)
  427. m->xmin = coordx;
  428. if (coordx > m->xmax)
  429. m->xmax = coordx;
  430. if (coordy < m->ymin)
  431. m->ymin = coordy;
  432. if (coordy > m->ymax)
  433. m->ymax = coordy;
  434. if (coordz < m->zmin)
  435. m->zmin = coordz;
  436. if (coordz > m->zmax)
  437. m->zmax = coordz;
  438. }
  439. }
  440. while (strcmp (str, "f") != 0 && !feof (is))
  441. {
  442. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  443. }
  444. //lecture des faces
  445. while (strcmp (str, "f") == 0 && !feof (is))
  446. {
  447. a2ri_erreur_critique_si(!fscanf (is, "%s %s %s", str_ve1, str_ve2, str_ve3),"erreur en ouverture de fichier");
  448. ve1 = atoi (str_ve1);
  449. ve2 = atoi (str_ve2);
  450. ve3 = atoi (str_ve3);
  451. ve1--;
  452. ve2--;
  453. ve3--;
  454. //ajout d'une face
  455. a2ri_vf_add_face (m, ve1, ve2, ve3);
  456. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  457. if (str[0] >= '0' && str[0] <= '9')
  458. {
  459. ve2 = atoi (str);
  460. ve2--;
  461. a2ri_vf_add_face (m, ve3, ve2, ve1);
  462. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  463. }
  464. }
  465. fclose (is);
  466. setlocale(LC_NUMERIC,"");
  467. return 1;
  468. }
  469. /**
  470. Ouverture d'un fichier wrl contenant un modèle
  471. @param file chemin du fichier à ouvrir
  472. @param m pointeur sur le modèle
  473. @return 1 si succès, 0 sinon
  474. */
  475. int
  476. a2ri_vf_load_wrl1 (
  477. const char * const file,
  478. vf_model * m)
  479. {
  480. FILE *is = NULL;
  481. char str[20];
  482. char coordx[15],
  483. coordy[15],
  484. coordz[15];
  485. setlocale(LC_NUMERIC,"C");
  486. is = fopen (file, "r");
  487. if (is == NULL)
  488. return 0;
  489. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  490. while (strcmp (str, "point") && strcmp (str, "point["))
  491. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  492. if (!strcmp (str, "point"))
  493. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  494. //lecture des sommets
  495. a2ri_erreur_critique_si(!fscanf (is, "%s", coordx),"erreur en ouverture de fichier");
  496. int i = 0;
  497. while (strcmp (coordx, "}") && strcmp (coordx, "]"))
  498. {
  499. a2ri_erreur_critique_si(!fscanf (is, "%s", coordy),"erreur en ouverture de fichier");
  500. a2ri_erreur_critique_si(!fscanf (is, "%s", coordz),"erreur en ouverture de fichier");
  501. if (coordz[strlen (coordz) - 1] == ',')
  502. {
  503. memcpy (coordz, coordz, strlen (coordz) - 1);
  504. coordz[strlen (coordz)] = '\0';
  505. }
  506. //ajout d'un sommet
  507. a2ri_vf_add_vertex (m, atof (coordx), atof (coordy), atof (coordz));
  508. //mis à jour de la bounding box
  509. if (i == 0)
  510. {
  511. i++;
  512. m->xmin = atof (coordx);
  513. m->xmax = atof (coordx);
  514. m->ymin = atof (coordy);
  515. m->ymax = atof (coordy);
  516. m->zmin = atof (coordz);
  517. m->zmax = atof (coordz);
  518. }
  519. else
  520. {
  521. if (atof (coordx) < m->xmin)
  522. m->xmin = atof (coordx);
  523. if (atof (coordx) > m->xmax)
  524. m->xmax = atof (coordx);
  525. if (atof (coordy) < m->ymin)
  526. m->ymin = atof (coordy);
  527. if (atof (coordy) > m->ymax)
  528. m->ymax = atof (coordy);
  529. if (atof (coordz) < m->zmin)
  530. m->zmin = atof (coordz);
  531. if (atof (coordz) > m->zmax)
  532. m->zmax = atof (coordz);
  533. }
  534. a2ri_erreur_critique_si(!fscanf (is, "%s", coordx),"erreur en ouverture de fichier");
  535. }
  536. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  537. while (strcmp (str, "coordIndex") && strcmp (str, "coordIndex["))
  538. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  539. if (!strcmp (str, "coordIndex"))
  540. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  541. //lecture des faces
  542. a2ri_erreur_critique_si(!fscanf (is, "%s", coordx),"erreur en ouverture de fichier");
  543. while (strcmp (coordx, "}") && strcmp (coordx, "]"))
  544. {
  545. a2ri_erreur_critique_si(!fscanf (is, "%s", coordy),"erreur en ouverture de fichier");
  546. a2ri_erreur_critique_si(!fscanf (is, "%s", coordz),"erreur en ouverture de fichier");
  547. if (coordx[strlen (coordx) - 1] == ',')
  548. {
  549. memcpy (coordx, coordx, strlen (coordx) - 1);
  550. coordz[strlen (coordx)] = '\0';
  551. }
  552. if (coordy[strlen (coordy) - 1] == ',')
  553. {
  554. memcpy (coordy, coordy, strlen (coordy) - 1);
  555. coordy[strlen (coordy)] = '\0';
  556. }
  557. if (coordz[strlen (coordz) - 1] == ',')
  558. {
  559. memcpy (coordz, coordz, strlen (coordz) - 1);
  560. coordz[strlen (coordz)] = '\0';
  561. }
  562. //ajout d'une face
  563. a2ri_vf_add_face (m, atoi (coordx), atoi (coordy), atoi (coordz));
  564. // reading the -1
  565. a2ri_erreur_critique_si(!fscanf (is, "%s", coordy),"erreur en ouverture de fichier");
  566. if (coordy[strlen (coordy) - 1] == ',')
  567. {
  568. memcpy (coordy, coordy, strlen (coordy) - 1);
  569. coordy[strlen (coordy)] = '\0';
  570. }
  571. if (atoi (coordy) != -1)
  572. {
  573. a2ri_vf_add_face (m, atoi (coordz), atoi (coordy), atoi (coordx));
  574. a2ri_erreur_critique_si(!fscanf (is, "%s", coordx),"erreur en ouverture de fichier");
  575. }
  576. a2ri_erreur_critique_si(!fscanf (is, "%s", coordx),"erreur en ouverture de fichier");
  577. }
  578. fclose (is);
  579. setlocale(LC_NUMERIC,"");
  580. return 1;
  581. }
  582. /**
  583. Ouverture d'un fichier wrl contenant un modèle
  584. @param file chemin du fichier à ouvrir
  585. @param m pointeur sur le modèle
  586. @return 1 si succès, 0 sinon
  587. */
  588. int
  589. a2ri_vf_load_wrl2 (
  590. const char * const file,
  591. vf_model * m)
  592. {
  593. FILE *is = NULL;
  594. char str[20];
  595. char coordx[15],
  596. coordy[15],
  597. coordz[15];
  598. setlocale(LC_NUMERIC,"C");
  599. is = fopen (file, "r");
  600. if (is == NULL)
  601. return 0;
  602. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  603. while (strcmp (str, "point") && strcmp (str, "point["))
  604. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  605. if (!strcmp (str, "point"))
  606. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  607. //lecture des sommets
  608. a2ri_erreur_critique_si(!fscanf (is, "%s", coordx),"erreur en ouverture de fichier");
  609. int i = 0;
  610. while (strcmp (coordx, "}") && strcmp (coordx, "]"))
  611. {
  612. a2ri_erreur_critique_si(!fscanf (is, "%s", coordy),"erreur en ouverture de fichier");
  613. a2ri_erreur_critique_si(!fscanf (is, "%s", coordz),"erreur en ouverture de fichier");
  614. if (coordz[strlen (coordz) - 1] == ',')
  615. {
  616. memcpy (coordz, coordz, strlen (coordz) - 1);
  617. coordz[strlen (coordz)] = '\0';
  618. }
  619. //ajout d'un sommet
  620. a2ri_vf_add_vertex (m, atof (coordx), atof (coordy), atof (coordz));
  621. //mis à jour de la bounding box
  622. if (i == 0)
  623. {
  624. i++;
  625. m->xmin = atof (coordx);
  626. m->xmax = atof (coordx);
  627. m->ymin = atof (coordy);
  628. m->ymax = atof (coordy);
  629. m->zmin = atof (coordz);
  630. m->zmax = atof (coordz);
  631. }
  632. else
  633. {
  634. if (atof (coordx) < m->xmin)
  635. m->xmin = atof (coordx);
  636. if (atof (coordx) > m->xmax)
  637. m->xmax = atof (coordx);
  638. if (atof (coordy) < m->ymin)
  639. m->ymin = atof (coordy);
  640. if (atof (coordy) > m->ymax)
  641. m->ymax = atof (coordy);
  642. if (atof (coordz) < m->zmin)
  643. m->zmin = atof (coordz);
  644. if (atof (coordz) > m->zmax)
  645. m->zmax = atof (coordz);
  646. }
  647. a2ri_erreur_critique_si(!fscanf (is, "%s", coordx),"erreur en ouverture de fichier");
  648. }
  649. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  650. while (strcmp (str, "coordIndex") && strcmp (str, "coordIndex["))
  651. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  652. if (!strcmp (str, "coordIndex"))
  653. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  654. //lecture des faces
  655. a2ri_erreur_critique_si(!fscanf (is, "%s", coordx),"erreur en ouverture de fichier");
  656. while (strcmp (coordx, "}") && strcmp (coordx, "]"))
  657. {
  658. a2ri_erreur_critique_si(!fscanf (is, "%s", coordy),"erreur en ouverture de fichier");
  659. a2ri_erreur_critique_si(!fscanf (is, "%s", coordz),"erreur en ouverture de fichier");
  660. //ajout d'une face
  661. a2ri_vf_add_face (m, atoi (coordx), atoi (coordy), atoi (coordz));
  662. // reading the -1
  663. a2ri_erreur_critique_si(!fscanf (is, "%s", coordx),"erreur en ouverture de fichier");
  664. a2ri_erreur_critique_si(!fscanf (is, "%s", coordx),"erreur en ouverture de fichier");
  665. }
  666. fclose (is);
  667. setlocale(LC_NUMERIC,"");
  668. return 1;
  669. }
  670. /**
  671. Ouverture d'un fichier wrl contenant un modèle
  672. @param file chemin du fichier à ouvrir
  673. @param m pointeur sur le modèle
  674. @return 1 si succès, 0 sinon
  675. */
  676. int
  677. a2ri_vf_load_wrl (
  678. const char * const file,
  679. vf_model * m)
  680. {
  681. FILE *is = NULL;
  682. char str[20];
  683. is = fopen (file, "r");
  684. if (is == NULL)
  685. return 0;
  686. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  687. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  688. if (!strcmp (str, "v1.0") || !strcmp (str, "V1.0"))
  689. return a2ri_vf_load_wrl1 (file, m);
  690. if (!strcmp (str, "v2.0") || !strcmp (str, "V2.0"))
  691. return a2ri_vf_load_wrl2 (file, m);
  692. return 0;
  693. }
  694. /**
  695. Ouverture d'un fichier vef contenant un modèle
  696. @param file chemin du fichier à ouvrir
  697. @param m pointeur sur le modèle
  698. @return 1 si succès, 0 sinon
  699. */
  700. int
  701. a2ri_vf_load_vef (
  702. const char * const file,
  703. vf_model * m)
  704. {
  705. FILE *is = NULL;
  706. int nbvertex,
  707. nbedge,
  708. nbface,
  709. ve1,
  710. ve2,
  711. ve3,
  712. ed1,
  713. ed2,
  714. ed3;
  715. double coordx,
  716. coordy,
  717. coordz;
  718. setlocale(LC_NUMERIC,"C");
  719. is = fopen (file, "r");
  720. if (is == NULL)
  721. return 0;
  722. a2ri_vf_init (m);
  723. //lecture du nombre de sommets
  724. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbvertex),"erreur en ouverture de fichier");
  725. //lecture du nombre d'aretes
  726. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbedge),"erreur en ouverture de fichier");
  727. //lecture du nombre de faces
  728. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbface),"erreur en ouverture de fichier");
  729. //lecture des sommets
  730. for (int i = 0; i < nbvertex; i++)
  731. {
  732. a2ri_erreur_critique_si(!fscanf (is, "%lf", &coordx),"erreur en ouverture de fichier");
  733. a2ri_erreur_critique_si(!fscanf (is, "%lf", &coordy),"erreur en ouverture de fichier");
  734. a2ri_erreur_critique_si(!fscanf (is, "%lf", &coordz),"erreur en ouverture de fichier");
  735. //ajout d'un sommet
  736. a2ri_vf_add_vertex (m, coordx, coordy, coordz);
  737. //mis à jour de la bounding box
  738. if (i == 0)
  739. {
  740. m->xmin = coordx;
  741. m->xmax = coordx;
  742. m->ymin = coordy;
  743. m->ymax = coordy;
  744. m->zmin = coordz;
  745. m->zmax = coordz;
  746. }
  747. else
  748. {
  749. if (coordx < m->xmin)
  750. m->xmin = coordx;
  751. if (coordx > m->xmax)
  752. m->xmax = coordx;
  753. if (coordy < m->ymin)
  754. m->ymin = coordy;
  755. if (coordy > m->ymax)
  756. m->ymax = coordy;
  757. if (coordz < m->zmin)
  758. m->zmin = coordz;
  759. if (coordz > m->zmax)
  760. m->zmax = coordz;
  761. }
  762. }
  763. //lecture des aretes et enregistrement dans un tableau temporaire
  764. vf_edge *temp = (vf_edge *) malloc (nbedge * sizeof (vf_edge));
  765. a2ri_erreur_critique_si (temp == NULL,
  766. "erreur allocation memoire pour temp\na2ri_vf_load_vef");
  767. for (int i = 0; i < nbedge; i++)
  768. {
  769. a2ri_erreur_critique_si(!fscanf (is, "%d", &ve1),"erreur en ouverture de fichier");
  770. a2ri_erreur_critique_si(!fscanf (is, "%d", &ve2),"erreur en ouverture de fichier");
  771. temp[i].ve1 = ve1;
  772. temp[i].ve2 = ve2;
  773. }
  774. //lecture des faces
  775. for (int i = 0; i < nbface; i++)
  776. {
  777. a2ri_erreur_critique_si(!fscanf (is, "%d", &ed1),"erreur en ouverture de fichier");
  778. a2ri_erreur_critique_si(!fscanf (is, "%d", &ed2),"erreur en ouverture de fichier");
  779. a2ri_erreur_critique_si(!fscanf (is, "%d", &ed3),"erreur en ouverture de fichier");
  780. //recherche des trois sommets contenu dans les trois aretes
  781. if (temp[ed1].ve1 == temp[ed2].ve1 || temp[ed1].ve1 == temp[ed2].ve2)
  782. {
  783. ve1 = temp[ed1].ve1;
  784. ve3 = temp[ed1].ve2;
  785. }
  786. else
  787. {
  788. ve1 = temp[ed1].ve2;
  789. ve3 = temp[ed1].ve1;
  790. }
  791. if (temp[ed2].ve1 == temp[ed3].ve1 || temp[ed2].ve1 == temp[ed3].ve2)
  792. ve2 = temp[ed2].ve1;
  793. else
  794. ve2 = temp[ed2].ve2;
  795. //ajout de la face
  796. a2ri_vf_add_face (m, ve1, ve2, ve3);
  797. }
  798. fclose (is);
  799. setlocale(LC_NUMERIC,"");
  800. return 1;
  801. }
  802. /**
  803. Ouverture d'un fichier gts contenant un modèle
  804. @param file chemin du fichier à ouvrir
  805. @param m pointeur sur le modèle
  806. @return 1 si succès, 0 sinon
  807. */
  808. int
  809. a2ri_vf_load_gts (
  810. const char * const file,
  811. vf_model * m)
  812. {
  813. FILE *is = NULL;
  814. int nbvertex,
  815. nbedge,
  816. nbface,
  817. ve1,
  818. ve2,
  819. ve3,
  820. ed1,
  821. ed2,
  822. ed3;
  823. double coordx,
  824. coordy,
  825. coordz;
  826. setlocale(LC_NUMERIC,"C");
  827. is = fopen (file, "r");
  828. if (is == NULL)
  829. return 0;
  830. a2ri_vf_init (m);
  831. //lecture du nombre de sommets
  832. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbvertex),"erreur en ouverture de fichier");
  833. //lecture du nombre d'aretes
  834. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbedge),"erreur en ouverture de fichier");
  835. //lecture du nombre de faces
  836. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbface),"erreur en ouverture de fichier");
  837. //lecture des sommets
  838. for (int i = 0; i < nbvertex; i++)
  839. {
  840. a2ri_erreur_critique_si(!fscanf (is, "%lf", &coordx),"erreur en ouverture de fichier");
  841. a2ri_erreur_critique_si(!fscanf (is, "%lf", &coordy),"erreur en ouverture de fichier");
  842. a2ri_erreur_critique_si(!fscanf (is, "%lf", &coordz),"erreur en ouverture de fichier");
  843. //ajout d'un sommet
  844. a2ri_vf_add_vertex (m, coordx, coordy, coordz);
  845. //mis à jour de la bounding box
  846. if (i == 0)
  847. {
  848. m->xmin = coordx;
  849. m->xmax = coordx;
  850. m->ymin = coordy;
  851. m->ymax = coordy;
  852. m->zmin = coordz;
  853. m->zmax = coordz;
  854. }
  855. else
  856. {
  857. if (coordx < m->xmin)
  858. m->xmin = coordx;
  859. if (coordx > m->xmax)
  860. m->xmax = coordx;
  861. if (coordy < m->ymin)
  862. m->ymin = coordy;
  863. if (coordy > m->ymax)
  864. m->ymax = coordy;
  865. if (coordz < m->zmin)
  866. m->zmin = coordz;
  867. if (coordz > m->zmax)
  868. m->zmax = coordz;
  869. }
  870. }
  871. //lecture des aretes et enregistrement dans un tableau temporaire
  872. vf_edge *temp = (vf_edge *) malloc (nbedge * sizeof (vf_edge));
  873. a2ri_erreur_critique_si (temp == NULL,
  874. "erreur allocation memoire pour temp\na2ri_vf_load_gts");
  875. for (int i = 0; i < nbedge; i++)
  876. {
  877. a2ri_erreur_critique_si(!fscanf (is, "%d", &ve1),"erreur en ouverture de fichier");
  878. a2ri_erreur_critique_si(!fscanf (is, "%d", &ve2),"erreur en ouverture de fichier");
  879. ve1--;
  880. ve2--;
  881. temp[i].ve1 = ve1;
  882. temp[i].ve2 = ve2;
  883. }
  884. //lecture des faces
  885. for (int i = 0; i < nbface; i++)
  886. {
  887. a2ri_erreur_critique_si(!fscanf (is, "%d", &ed1),"erreur en ouverture de fichier");
  888. a2ri_erreur_critique_si(!fscanf (is, "%d", &ed2),"erreur en ouverture de fichier");
  889. a2ri_erreur_critique_si(!fscanf (is, "%d", &ed3),"erreur en ouverture de fichier");
  890. ed1--;
  891. ed2--;
  892. ed3--;
  893. //recherche des trois sommets contenu dans les trois aretes
  894. if (temp[ed1].ve1 == temp[ed2].ve1 || temp[ed1].ve1 == temp[ed2].ve2)
  895. {
  896. ve1 = temp[ed1].ve1;
  897. ve3 = temp[ed1].ve2;
  898. }
  899. else
  900. {
  901. ve1 = temp[ed1].ve2;
  902. ve3 = temp[ed1].ve1;
  903. }
  904. if (temp[ed2].ve1 == temp[ed3].ve1 || temp[ed2].ve1 == temp[ed3].ve2)
  905. ve2 = temp[ed2].ve1;
  906. else
  907. ve2 = temp[ed2].ve2;
  908. //ajout de la face
  909. a2ri_vf_add_face (m, ve1, ve2, ve3);
  910. }
  911. fclose (is);
  912. setlocale(LC_NUMERIC,"");
  913. return 1;
  914. }
  915. /**
  916. Ouverture d'un fichier ply contenant un modèle
  917. @param file chemin du fichier à ouvrir
  918. @param m pointeur sur le modèle
  919. @return 1 si succès, 0 sinon
  920. */
  921. int
  922. a2ri_vf_load_ply (
  923. const char * const file,
  924. vf_model * m)
  925. {
  926. int prop = 0,
  927. nbpropvertex = 0,
  928. nbpropface = 0,
  929. nbvertex,
  930. nbface,
  931. ve1,
  932. ve2,
  933. ve3;
  934. char str[20];
  935. double coordx,
  936. coordy,
  937. coordz;
  938. FILE *is = NULL;
  939. setlocale(LC_NUMERIC,"C");
  940. is = fopen (file, "r");
  941. if (is == NULL)
  942. return 0;
  943. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  944. if (strcmp (str, "ply"))
  945. return 0;
  946. while (strcmp (str, "end_header"))
  947. {
  948. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  949. if (prop == 1 && !strcmp (str, "property"))
  950. nbpropvertex++;
  951. if (prop == 2 && !strcmp (str, "property"))
  952. nbpropface++;
  953. if (!strcmp (str, "element"))
  954. {
  955. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  956. //lecture du nombre de sommets
  957. if (!strcmp (str, "vertex"))
  958. {
  959. prop = 1;
  960. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbvertex),"erreur en ouverture de fichier");
  961. }
  962. //lecture du nombre de face
  963. else
  964. {
  965. if (!strcmp (str, "face"))
  966. {
  967. prop = 1;
  968. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbface),"erreur en ouverture de fichier");
  969. }
  970. }
  971. }
  972. }
  973. //lecture des sommets
  974. for (int i = 0; i < nbvertex; i++)
  975. {
  976. a2ri_erreur_critique_si(!fscanf (is, "%lf %lf %lf", &coordx, &coordy, &coordz),"erreur en ouverture de fichier");
  977. //ajout d'un sommet
  978. a2ri_vf_add_vertex (m, coordx, coordy, coordz);
  979. //mis à jour de la bounding box
  980. if (i == 0)
  981. {
  982. m->xmin = coordx;
  983. m->xmax = coordx;
  984. m->ymin = coordy;
  985. m->ymax = coordy;
  986. m->zmin = coordz;
  987. m->zmax = coordz;
  988. }
  989. else
  990. {
  991. if (coordx < m->xmin)
  992. m->xmin = coordx;
  993. if (coordx > m->xmax)
  994. m->xmax = coordx;
  995. if (coordy < m->ymin)
  996. m->ymin = coordy;
  997. if (coordy > m->ymax)
  998. m->ymax = coordy;
  999. if (coordz < m->zmin)
  1000. m->zmin = coordz;
  1001. if (coordz > m->zmax)
  1002. m->zmax = coordz;
  1003. }
  1004. for (int j = 0; j < nbpropvertex - 4; j++)
  1005. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  1006. }
  1007. //lecture des faces
  1008. for (int i = 0; i < nbface; i++)
  1009. {
  1010. a2ri_erreur_critique_si(!fscanf (is, "%d", &ve1),"erreur en ouverture de fichier");
  1011. a2ri_erreur_critique_si(!fscanf (is, "%d %d %d", &ve1, &ve2, &ve3),"erreur en ouverture de fichier");
  1012. //ajout d'une face
  1013. a2ri_vf_add_face (m, ve1, ve2, ve3);
  1014. }
  1015. fclose (is);
  1016. setlocale(LC_NUMERIC,"");
  1017. return 1;
  1018. }
  1019. /**
  1020. Ouverture d'un fichier off contenant un modèle
  1021. @param file chemin du fichier à ouvrir
  1022. @param m pointeur sur le modèle
  1023. @return 1 si succès, 0 sinon
  1024. */
  1025. int
  1026. a2ri_vf_load_off (
  1027. const char * const file,
  1028. vf_model * m)
  1029. {
  1030. FILE *is = NULL;
  1031. int nbvertex,
  1032. nbface,
  1033. ve1,
  1034. ve2,
  1035. ve3;
  1036. double coordx,
  1037. coordy,
  1038. coordz;
  1039. char str[250];
  1040. setlocale(LC_NUMERIC,"C");
  1041. is = fopen (file, "r");
  1042. if (is == NULL)
  1043. return 0;
  1044. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  1045. if (strcmp (str, "OFF"))
  1046. return 0;
  1047. //lecture du nombre de sommets et de faces
  1048. a2ri_erreur_critique_si(!fscanf (is, "%d %d %d", &nbvertex, &nbface, &ve1),"erreur en ouverture de fichier");
  1049. m->ve=(vf_vertex*)malloc(nbvertex*sizeof(vf_vertex));
  1050. m->nbvertex=nbvertex;
  1051. //lecture des sommets
  1052. for (int i = 0; i < nbvertex; i++)
  1053. {
  1054. a2ri_erreur_critique_si(!fscanf (is, "%lf %lf %lf", &coordx, &coordy, &coordz),"erreur en ouverture de fichier");
  1055. //ajout du sommet
  1056. m->ve[i].x=coordx;m->ve[i].y=coordy;m->ve[i].z=coordz;
  1057. m->ve[i].incidentvertices=NULL;
  1058. m->ve[i].nbincidentvertices=0;
  1059. //mis à jour de la bounding box
  1060. if (i == 0)
  1061. {
  1062. m->xmin = coordx;
  1063. m->xmax = coordx;
  1064. m->ymin = coordy;
  1065. m->ymax = coordy;
  1066. m->zmin = coordz;
  1067. m->zmax = coordz;
  1068. }
  1069. else
  1070. {
  1071. if (coordx < m->xmin)
  1072. m->xmin = coordx;
  1073. if (coordx > m->xmax)
  1074. m->xmax = coordx;
  1075. if (coordy < m->ymin)
  1076. m->ymin = coordy;
  1077. if (coordy > m->ymax)
  1078. m->ymax = coordy;
  1079. if (coordz < m->zmin)
  1080. m->zmin = coordz;
  1081. if (coordz > m->zmax)
  1082. m->zmax = coordz;
  1083. }
  1084. }
  1085. m->fa=(vf_face*)malloc(nbface*sizeof(vf_face));
  1086. m->nbface=nbface;
  1087. //lecture des faces
  1088. for (int i = 0; i < nbface; i++)
  1089. {
  1090. a2ri_erreur_critique_si(!fscanf (is, "%d", &ve1),"erreur en ouverture de fichier");
  1091. a2ri_erreur_critique_si(!fscanf (is, "%d %d %d", &ve1, &ve2, &ve3),"erreur en ouverture de fichier");
  1092. //ajout d'une face
  1093. m->fa[i].ve1 = ve1;
  1094. m->fa[i].ve2 = ve2;
  1095. m->fa[i].ve3 = ve3;
  1096. list_int_add (&(m->ve[ve1].incidentvertices),
  1097. &(m->ve[ve1].nbincidentvertices), ve2, WITHOUT_REDUNDANCE);
  1098. list_int_add (&(m->ve[ve2].incidentvertices),
  1099. &(m->ve[ve2].nbincidentvertices), ve1, WITHOUT_REDUNDANCE);
  1100. list_int_add (&(m->ve[ve1].incidentvertices),
  1101. &(m->ve[ve1].nbincidentvertices), ve3, WITHOUT_REDUNDANCE);
  1102. list_int_add (&(m->ve[ve3].incidentvertices),
  1103. &(m->ve[ve3].nbincidentvertices), ve1, WITHOUT_REDUNDANCE);
  1104. list_int_add (&(m->ve[ve3].incidentvertices),
  1105. &(m->ve[ve3].nbincidentvertices), ve2, WITHOUT_REDUNDANCE);
  1106. list_int_add (&(m->ve[ve2].incidentvertices),
  1107. &(m->ve[ve2].nbincidentvertices), ve3, WITHOUT_REDUNDANCE);
  1108. }
  1109. fclose (is);
  1110. setlocale(LC_NUMERIC,"");
  1111. return 1;
  1112. }
  1113. /**
  1114. Ouverture d'un fichier stl contenant un modèle
  1115. @param file chemin du fichier à ouvrir
  1116. @param m pointeur sur le modèle
  1117. @return 1 si succès, 0 sinon
  1118. */
  1119. int
  1120. a2ri_vf_load_stl (
  1121. const char * const file,
  1122. vf_model * m)
  1123. {
  1124. FILE *is = NULL;
  1125. char str[250];
  1126. is = fopen (file, "r");
  1127. if (is == NULL)
  1128. return 0;
  1129. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  1130. fclose (is);
  1131. if (!strcmp (str, "solid"))
  1132. return a2ri_vf_load_ascii_stl (file, m);
  1133. else
  1134. return a2ri_vf_load_binary_stl (file, m);
  1135. }
  1136. /**
  1137. Ouverture d'un fichier stl binaire contenant un modèle
  1138. @param file chemin du fichier à ouvrir
  1139. @param m pointeur sur le modèle
  1140. @return 1 si succès, 0 sinon
  1141. */
  1142. int
  1143. a2ri_vf_load_binary_stl (
  1144. const char * const file,
  1145. vf_model * m)
  1146. {
  1147. FILE *is = NULL;
  1148. char str[80];
  1149. unsigned int nbface;
  1150. unsigned char finligne;
  1151. float coordx,
  1152. coordy,
  1153. coordz;
  1154. int ve1 = 0,
  1155. ve2 = 0,
  1156. ve3 = 0;
  1157. point3d ptmin,
  1158. ptmax;
  1159. space_partition sp;
  1160. setlocale(LC_NUMERIC,"C");
  1161. is = fopen (file, "rb");
  1162. if (is == NULL)
  1163. return 0;
  1164. a2ri_erreur_critique_si(!fread (&str, sizeof (char), 80, is),"erreur en ouverture de fichier");
  1165. a2ri_erreur_critique_si(!fread (&nbface, sizeof (unsigned int), 1, is),"erreur en ouverture de fichier");
  1166. //recherche de la bounding box du modele numerique
  1167. for (unsigned int i = 0; i < nbface; i++)
  1168. {
  1169. a2ri_erreur_critique_si(!fread (&coordx, sizeof (float), 1, is),"erreur en ouverture de fichier");
  1170. a2ri_erreur_critique_si(!fread (&coordy, sizeof (float), 1, is),"erreur en ouverture de fichier");
  1171. a2ri_erreur_critique_si(!fread (&coordz, sizeof (float), 1, is),"erreur en ouverture de fichier");
  1172. for (unsigned int j = 0; j < 3; j++)
  1173. {
  1174. //read the vertex
  1175. a2ri_erreur_critique_si(!fread (&coordx, sizeof (float), 1, is),"erreur en ouverture de fichier");
  1176. a2ri_erreur_critique_si(!fread (&coordy, sizeof (float), 1, is),"erreur en ouverture de fichier");
  1177. a2ri_erreur_critique_si(!fread (&coordz, sizeof (float), 1, is),"erreur en ouverture de fichier");
  1178. if (i == 0 && j == 0)
  1179. {
  1180. m->xmin = coordx;
  1181. m->xmax = coordx;
  1182. m->ymin = coordy;
  1183. m->ymax = coordy;
  1184. m->zmin = coordz;
  1185. m->zmax = coordz;
  1186. }
  1187. else
  1188. {
  1189. if (coordx < m->xmin)
  1190. m->xmin = coordx;
  1191. if (coordx > m->xmax)
  1192. m->xmax = coordx;
  1193. if (coordy < m->ymin)
  1194. m->ymin = coordy;
  1195. if (coordy > m->ymax)
  1196. m->ymax = coordy;
  1197. if (coordz < m->zmin)
  1198. m->zmin = coordz;
  1199. if (coordz > m->zmax)
  1200. m->zmax = coordz;
  1201. }
  1202. }
  1203. a2ri_erreur_critique_si(!fread (&finligne, 1, 1, is),"erreur en ouverture de fichier");
  1204. a2ri_erreur_critique_si(!fread (&finligne, 1, 1, is),"erreur en ouverture de fichier");
  1205. }
  1206. point3d_init (&ptmin, m->xmin, m->ymin, m->zmin);
  1207. point3d_init (&ptmax, m->xmax, m->ymax, m->zmax);
  1208. int nb_part = (int) (pow (nbface/22.0, (1.0 / 3.0))) + 1;
  1209. space_partition_new (&sp, &ptmin, &ptmax, nb_part, nb_part, nb_part);
  1210. //retour au debut du fichier
  1211. fseek (is, 0, SEEK_SET);
  1212. a2ri_erreur_critique_si(!fread (&str, sizeof (char), 80, is),"erreur en ouverture de fichier");
  1213. a2ri_erreur_critique_si(!fread (&nbface, sizeof (unsigned int), 1, is),"erreur en ouverture de fichier");
  1214. for (unsigned int i = 0; i < nbface; i++)
  1215. {
  1216. a2ri_erreur_critique_si(!fread (&coordx, sizeof (float), 1, is),"erreur en ouverture de fichier");
  1217. a2ri_erreur_critique_si(!fread (&coordy, sizeof (float), 1, is),"erreur en ouverture de fichier");
  1218. a2ri_erreur_critique_si(!fread (&coordz, sizeof (float), 1, is),"erreur en ouverture de fichier");
  1219. for (unsigned int j = 0; j < 3; j++)
  1220. {
  1221. a2ri_erreur_critique_si(!fread (&coordx, sizeof (float), 1, is),"erreur en ouverture de fichier");
  1222. a2ri_erreur_critique_si(!fread (&coordy, sizeof (float), 1, is),"erreur en ouverture de fichier");
  1223. a2ri_erreur_critique_si(!fread (&coordz, sizeof (float), 1, is),"erreur en ouverture de fichier"); //read the vertex
  1224. point3d_init (&ptmin, coordx, coordy, coordz);
  1225. if (i == 0 && j == 0)
  1226. {
  1227. ve1 = 0;
  1228. ptmin.att_int = space_partition_nb_points (&sp);
  1229. space_partition_add_point (&sp, &ptmin);
  1230. a2ri_vf_add_vertex (m, ptmin.x, ptmin.y, ptmin.z);
  1231. }
  1232. else
  1233. {
  1234. point3d *ret=space_partition_contains_point (&sp, &ptmin);
  1235. if (ret!=NULL)
  1236. {
  1237. switch (j)
  1238. {
  1239. case 0:
  1240. ve1 = ret->att_int;
  1241. break;
  1242. case 1:
  1243. ve2 = ret->att_int;
  1244. break;
  1245. default:
  1246. ve3 = ret->att_int;
  1247. }
  1248. }
  1249. else
  1250. {
  1251. ptmin.att_int = space_partition_nb_points (&sp);
  1252. switch (j)
  1253. {
  1254. case 0:
  1255. ve1 = ptmin.att_int;
  1256. break;
  1257. case 1:
  1258. ve2 = ptmin.att_int;
  1259. break;
  1260. default:
  1261. ve3 = ptmin.att_int;
  1262. }
  1263. space_partition_add_point (&sp, &ptmin);
  1264. a2ri_vf_add_vertex (m, ptmin.x, ptmin.y, ptmin.z);
  1265. }
  1266. }
  1267. }
  1268. a2ri_vf_add_face (m, ve1, ve2, ve3);
  1269. a2ri_erreur_critique_si(!fread (&finligne, 1, 1, is),"erreur en ouverture de fichier");
  1270. a2ri_erreur_critique_si(!fread (&finligne, 1, 1, is),"erreur en ouverture de fichier");
  1271. }
  1272. fclose (is);
  1273. setlocale(LC_NUMERIC,"");
  1274. return 1;
  1275. }
  1276. /**
  1277. Ouverture d'un fichier stl ascii contenant un modèle
  1278. @param file chemin du fichier à ouvrir
  1279. @param m pointeur sur le modèle
  1280. @return 1 si succès, 0 sinon
  1281. */
  1282. int
  1283. a2ri_vf_load_ascii_stl (
  1284. const char * const file,
  1285. vf_model * m)
  1286. {
  1287. FILE *is = NULL;
  1288. int ve1,
  1289. ve2,
  1290. ve3,
  1291. i = 0,
  1292. fin = 0;
  1293. double coordx,
  1294. coordy,
  1295. coordz;
  1296. char str[250];
  1297. setlocale(LC_NUMERIC,"C");
  1298. is = fopen (file, "r");
  1299. if (is == NULL)
  1300. return 0;
  1301. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  1302. while (strcmp (str, "facet"))
  1303. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  1304. while (!fin)
  1305. {
  1306. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier"); //read normal
  1307. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  1308. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  1309. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier"); //read the 3 values of the normal
  1310. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  1311. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier"); //outer loop
  1312. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier"); //read vertex
  1313. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  1314. coordx = atof (str); //coordx
  1315. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  1316. coordy = atof (str); //coordy
  1317. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  1318. coordz = atof (str); //coordz
  1319. ve1 = a2ri_vf_search_vertex (m, coordx, coordy, coordz);
  1320. if (ve1 == -1)
  1321. {
  1322. ve1 = m->nbvertex;
  1323. a2ri_vf_add_vertex (m, coordx, coordy, coordz);
  1324. }
  1325. if (i == 0)
  1326. {
  1327. m->xmin = coordx;
  1328. m->xmax = coordx;
  1329. m->ymin = coordy;
  1330. m->ymax = coordy;
  1331. m->zmin = coordz;
  1332. m->zmax = coordz;
  1333. i++;
  1334. }
  1335. else
  1336. {
  1337. if (coordx < m->xmin)
  1338. m->xmin = coordx;
  1339. if (coordx > m->xmax)
  1340. m->xmax = coordx;
  1341. if (coordy < m->ymin)
  1342. m->ymin = coordy;
  1343. if (coordy > m->ymax)
  1344. m->ymax = coordy;
  1345. if (coordz < m->zmin)
  1346. m->zmin = coordz;
  1347. if (coordz > m->zmax)
  1348. m->zmax = coordz;
  1349. }
  1350. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier"); //read vertex
  1351. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  1352. coordx = atof (str); //coordx
  1353. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  1354. coordy = atof (str); //coordy
  1355. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  1356. coordz = atof (str); //coordz
  1357. ve2 = a2ri_vf_search_vertex (m, coordx, coordy, coordz);
  1358. if (ve2 == -1)
  1359. {
  1360. ve2 = m->nbvertex;
  1361. a2ri_vf_add_vertex (m, coordx, coordy, coordz);
  1362. }
  1363. if (coordx < m->xmin)
  1364. m->xmin = coordx;
  1365. if (coordx > m->xmax)
  1366. m->xmax = coordx;
  1367. if (coordy < m->ymin)
  1368. m->ymin = coordy;
  1369. if (coordy > m->ymax)
  1370. m->ymax = coordy;
  1371. if (coordz < m->zmin)
  1372. m->zmin = coordz;
  1373. if (coordz > m->zmax)
  1374. m->zmax = coordz;
  1375. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier"); //read vertex
  1376. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  1377. coordx = atof (str); //coordx
  1378. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  1379. coordy = atof (str); //coordy
  1380. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  1381. coordz = atof (str); //coordz;
  1382. ve3 = a2ri_vf_search_vertex (m, coordx, coordy, coordz);
  1383. if (ve3 == -1)
  1384. {
  1385. ve3 = m->nbvertex;
  1386. a2ri_vf_add_vertex (m, coordx, coordy, coordz);
  1387. }
  1388. if (coordx < m->xmin)
  1389. m->xmin = coordx;
  1390. if (coordx > m->xmax)
  1391. m->xmax = coordx;
  1392. if (coordy < m->ymin)
  1393. m->ymin = coordy;
  1394. if (coordy > m->ymax)
  1395. m->ymax = coordy;
  1396. if (coordz < m->zmin)
  1397. m->zmin = coordz;
  1398. if (coordz > m->zmax)
  1399. m->zmax = coordz;
  1400. a2ri_vf_add_face (m, ve1, ve2, ve3);
  1401. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier"); //read endloop
  1402. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier"); //read endfacet
  1403. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier"); //read facet ou endsolid
  1404. fin = !strcmp (str, "endsolid");
  1405. }
  1406. fclose (is);
  1407. setlocale(LC_NUMERIC,"");
  1408. return 1;
  1409. }
  1410. /**
  1411. Enregistrement d'un modèle dans un format défini par l'extension du nom de fichier
  1412. @param file nom du fichier
  1413. @param m le modèle
  1414. @return aucun
  1415. */
  1416. int
  1417. a2ri_vf_save_file (
  1418. const char * const file,
  1419. const vf_model * const m)
  1420. {
  1421. char ext[4];
  1422. memcpy (ext, file + (strlen (file) - 3), 3);
  1423. ext[3] = 0;
  1424. //enregistrement du modèle en fonction de l'extension
  1425. if (!strcmp (ext, "wrl"))
  1426. {
  1427. return a2ri_vf_save_wrl (file, m);
  1428. }
  1429. if (!strcmp (ext, "vef"))
  1430. {
  1431. return a2ri_vf_save_vef (file, m);
  1432. }
  1433. if (!strcmp (ext, "pgn"))
  1434. {
  1435. return a2ri_vf_save_pgn (file, m);
  1436. }
  1437. if (!strcmp (ext, "ply"))
  1438. {
  1439. return a2ri_vf_save_ply (file, m);
  1440. }
  1441. if (!strcmp (ext, "off"))
  1442. {
  1443. return a2ri_vf_save_off (file, m);
  1444. }
  1445. if (!strcmp (ext, "obj"))
  1446. {
  1447. return a2ri_vf_save_obj (file, m);
  1448. }
  1449. if (!strcmp (ext, "gts"))
  1450. {
  1451. return a2ri_vf_save_gts (file, m);
  1452. }
  1453. if (!strcmp (ext, "stl"))
  1454. {
  1455. return a2ri_vf_save_binary_stl (file, m);
  1456. }
  1457. return 0;
  1458. }
  1459. /**
  1460. Enregistrement d'un modèle dans un fichier wr@param file chemin du fichier
  1461. @param m modèle à sauvegarder
  1462. @return 1 si succès, 0 sinon
  1463. */
  1464. int
  1465. a2ri_vf_save_wrl (
  1466. const char * const file,
  1467. const vf_model * const m)
  1468. {
  1469. setlocale(LC_NUMERIC,"C");
  1470. FILE *os = NULL;
  1471. os = fopen (file, "w");
  1472. if (os == NULL)
  1473. return 0;
  1474. //ecriture de l'en-tete
  1475. fprintf (os, "#VRML V2.0 utf8\n");
  1476. fprintf (os, "Shape{\n");
  1477. fprintf (os, "appearance Appearance {\n");
  1478. fprintf (os, "material Material {\n");
  1479. fprintf (os, "}\n");
  1480. fprintf (os, "}\n");
  1481. fprintf (os, "geometry IndexedFaceSet{\n");
  1482. fprintf (os, "coord Coordinate{\n");
  1483. fprintf (os, "point[\n");
  1484. //ecriture des sommets
  1485. for (int i = 0; i < m->nbvertex; i++)
  1486. fprintf (os, "%f %f %f,\n", m->ve[i].x, m->ve[i].y, m->ve[i].z);
  1487. fprintf (os, "]\n");
  1488. fprintf (os, "}\n");
  1489. //ecriture des faces
  1490. fprintf (os, "coordIndex[\n");
  1491. for (int i = 0; i < m->nbface; i++)
  1492. {
  1493. int nb1 = m->fa[i].ve1;
  1494. int nb2 = m->fa[i].ve2;
  1495. int nb3 = m->fa[i].ve3;
  1496. fprintf (os, "%d %d %d -1\n", nb1, nb2, nb3);
  1497. }
  1498. fprintf (os, "]\n");
  1499. fprintf (os, "}\n");
  1500. fprintf (os, "}\n");
  1501. fclose (os);
  1502. setlocale(LC_NUMERIC,"");
  1503. return 1;
  1504. }
  1505. /**
  1506. Enregistrement d'un modèle dans un fichier wrl version 1.0
  1507. @param file chemin du fichier
  1508. @param m modèle à sauvegarder
  1509. @return 1 si succès, 0 sinon
  1510. */
  1511. int
  1512. a2ri_vf_save_wrl_v1 (
  1513. const char * const file,
  1514. const vf_model * const m)
  1515. {
  1516. setlocale(LC_NUMERIC,"C");
  1517. FILE *os = NULL;
  1518. os = fopen (file, "w");
  1519. if (os == NULL)
  1520. return 0;
  1521. //ecriture de l'en-tete
  1522. fprintf (os, "#VRML V1.0 ascii\n");
  1523. fprintf (os, "Separator {\n");
  1524. fprintf (os, "Switch {\n");
  1525. fprintf (os, "DEF Pol{\n");
  1526. fprintf (os, "Separator {\n");
  1527. fprintf (os, "Coordinate3 {\n");
  1528. fprintf (os, "point[\n");
  1529. //ecriture des sommets
  1530. for (int i = 0; i < m->nbvertex; i++)
  1531. fprintf (os, "%f %f %f,\n", m->ve[i].x, m->ve[i].y, m->ve[i].z);
  1532. fprintf (os, "]\n");
  1533. fprintf (os, "}\n");
  1534. //ecriture des faces
  1535. fprintf (os, "IndexedFaceSet {\n");
  1536. fprintf (os, "coordIndex [\n");
  1537. for (int i = 0; i < m->nbface; i++)
  1538. {
  1539. int nb1 = m->fa[i].ve1;
  1540. int nb2 = m->fa[i].ve2;
  1541. int nb3 = m->fa[i].ve3;
  1542. fprintf (os, "%d, %d, %d, -1,\n", nb1, nb2, nb3);
  1543. }
  1544. fprintf (os, "]\n");
  1545. fprintf (os, "}\n");
  1546. fprintf (os, "}\n");
  1547. fprintf (os, "}\n");
  1548. fclose (os);
  1549. setlocale(LC_NUMERIC,"");
  1550. return 1;
  1551. }
  1552. /**
  1553. Enregistrement d'un modèle dans un fichier pgn
  1554. @param file chemin du fichier
  1555. @param m modèle à sauvegarder
  1556. @return 1 si succès, 0 sinon
  1557. */
  1558. int
  1559. a2ri_vf_save_pgn (
  1560. const char * const file,
  1561. const vf_model * const m)
  1562. {
  1563. setlocale(LC_NUMERIC,"C");
  1564. FILE *os = NULL;
  1565. os = fopen (file, "w");
  1566. if (os == NULL)
  1567. return 0;
  1568. //ecriture du nombre de sommets et de faces
  1569. fprintf (os, "%d %d\n", m->nbvertex, m->nbface);
  1570. //ecriture des sommets
  1571. for (int i = 0; i < m->nbvertex; i++)
  1572. fprintf (os, "%lf %lf %lf\n", m->ve[i].x, m->ve[i].y, m->ve[i].z);
  1573. fprintf (os, "\n");
  1574. //ecriture des faces
  1575. for (int i = 0; i < m->nbface; i++)
  1576. fprintf (os, "%d %d %d -1\n", m->fa[i].ve1, m->fa[i].ve2, m->fa[i].ve3);
  1577. fclose (os);
  1578. setlocale(LC_NUMERIC,"");
  1579. return 1;
  1580. }
  1581. /**
  1582. Enregistrement d'un modèle dans un fichier vef
  1583. @param file chemin du fichier
  1584. @param m modèle à sauvegarder
  1585. @return 1 si succès, 0 sinon
  1586. **/
  1587. int
  1588. a2ri_vf_save_vef (
  1589. const char * const file,
  1590. const vf_model * const m)
  1591. {
  1592. hashtable *table = hashtable_new (m->nbvertex + m->nbface);
  1593. hashtable *tabletemp = NULL;
  1594. int p1,
  1595. p2;
  1596. setlocale(LC_NUMERIC,"C");
  1597. FILE *os = NULL;
  1598. os = fopen (file, "w");
  1599. if (os == NULL)
  1600. return 0;
  1601. tabletemp = a2ri_vf_construction_edge_table (m, NULL, 0);
  1602. //ecriture du nombre de sommets, d'aretes et de faces
  1603. fprintf (os, "%d %d %d\n", m->nbvertex, hashtable_size (tabletemp),
  1604. m->nbface);
  1605. hashtable_free (tabletemp);
  1606. free(tabletemp);
  1607. //ecriture des sommets
  1608. for (int i = 0; i < m->nbvertex; i++)
  1609. fprintf (os, "%lf %lf %lf\n", m->ve[i].x, m->ve[i].y, m->ve[i].z);
  1610. fprintf (os, "\n");
  1611. //ecriture des aretes
  1612. IFsave_vef_write_edge (m, table, os);
  1613. fprintf (os, "\n");
  1614. //ecriture des faces
  1615. for (int i = 0; i < m->nbface; i++)
  1616. {
  1617. p1 = m->fa[i].ve1;
  1618. p2 = m->fa[i].ve2;
  1619. //recherche de l'arete dans la table de hachage
  1620. vf_edge *edgetemp = hashtable_look_for (table, p1, p2);
  1621. fprintf (os, "%d ", edgetemp->att_int);
  1622. p1 = m->fa[i].ve2;
  1623. p2 = m->fa[i].ve3;
  1624. edgetemp = hashtable_look_for (table, p1, p2);
  1625. fprintf (os, "%d ", edgetemp->att_int);
  1626. p1 = m->fa[i].ve3;
  1627. p2 = m->fa[i].ve1;
  1628. edgetemp = hashtable_look_for (table, p1, p2);
  1629. fprintf (os, "%d\n", edgetemp->att_int);
  1630. }
  1631. hashtable_free (table);
  1632. free(table);
  1633. fclose (os);
  1634. setlocale(LC_NUMERIC,"");
  1635. return 0;
  1636. }
  1637. /**
  1638. Enregistrement d'un modèle dans un fichier gts
  1639. @param file chemin du fichier
  1640. @param m modèle à sauvegarder
  1641. @return 1 si succès, 0 sinon
  1642. **/
  1643. int
  1644. a2ri_vf_save_gts (
  1645. const char * const file,
  1646. const vf_model * const m)
  1647. {
  1648. hashtable *table = hashtable_new (m->nbvertex + m->nbface);
  1649. hashtable *tabletemp = NULL;
  1650. int p1,
  1651. p2;
  1652. setlocale(LC_NUMERIC,"C");
  1653. FILE *os = NULL;
  1654. os = fopen (file, "w");
  1655. if (os == NULL)
  1656. return 0;
  1657. tabletemp = a2ri_vf_construction_edge_table (m, NULL, 0);
  1658. //ecriture du nombre de sommets, d'aretes et de faces
  1659. fprintf (os, "%d %d %d\n", m->nbvertex, hashtable_size (tabletemp),
  1660. m->nbface);
  1661. hashtable_free (tabletemp);
  1662. //ecriture des sommets
  1663. for (int i = 0; i < m->nbvertex; i++)
  1664. fprintf (os, "%lf %lf %lf\n", m->ve[i].x, m->ve[i].y, m->ve[i].z);
  1665. fprintf (os, "\n");
  1666. //ecriture des aretes
  1667. IFsave_gts_write_edge (m, table, os);
  1668. fprintf (os, "\n");
  1669. //ecriture des faces
  1670. for (int i = 0; i < m->nbface; i++)
  1671. {
  1672. p1 = m->fa[i].ve1;
  1673. p2 = m->fa[i].ve2;
  1674. //recherche de l'arete dans la table de hachage
  1675. vf_edge *edgetemp = hashtable_look_for (table, p1, p2);
  1676. fprintf (os, "%d ", edgetemp->att_int + 1);
  1677. p1 = m->fa[i].ve2;
  1678. p2 = m->fa[i].ve3;
  1679. edgetemp = hashtable_look_for (table, p1, 2);
  1680. fprintf (os, "%d ", edgetemp->att_int + 1);
  1681. p1 = m->fa[i].ve3;
  1682. p2 = m->fa[i].ve1;
  1683. edgetemp = hashtable_look_for (table, p1, p2);
  1684. fprintf (os, "%d\n", edgetemp->att_int + 1);
  1685. }
  1686. hashtable_free (table);
  1687. fclose (os);
  1688. setlocale(LC_NUMERIC,"");
  1689. return 0;
  1690. }
  1691. /**
  1692. Enregistrement d'un modèle dans un fichier off
  1693. @param file chemin du fichier
  1694. @param m modèle à sauvegarder
  1695. @return 1 si succès, 0 sinon
  1696. */
  1697. int
  1698. a2ri_vf_save_off (
  1699. const char * const file,
  1700. const vf_model * const m)
  1701. {
  1702. setlocale(LC_NUMERIC,"C");
  1703. FILE *os = NULL;
  1704. os = fopen (file, "w");
  1705. if (os == NULL)
  1706. return 0;
  1707. //ecriture du nombre de sommets et de faces
  1708. fprintf (os, "OFF\n%d %d 0\n", m->nbvertex, m->nbface);
  1709. //ecriture des sommets
  1710. for (int i = 0; i < m->nbvertex; i++)
  1711. fprintf (os, "%lf %lf %lf\n", m->ve[i].x, m->ve[i].y, m->ve[i].z);
  1712. fprintf (os, "\n");
  1713. //ecriture des faces
  1714. for (int i = 0; i < m->nbface; i++)
  1715. fprintf (os, "3 %d %d %d\n", m->fa[i].ve1, m->fa[i].ve2, m->fa[i].ve3);
  1716. fclose (os);
  1717. setlocale(LC_NUMERIC,"");
  1718. return 1;
  1719. }
  1720. /**
  1721. Enregistrement d'un modèle dans un fichier ply
  1722. @param file chemin du fichier
  1723. @param m modèle à sauvegarder
  1724. @return 1 si succès, 0 sinon
  1725. */
  1726. int
  1727. a2ri_vf_save_ply (
  1728. const char * const file,
  1729. const vf_model * const m)
  1730. {
  1731. int ve1,
  1732. ve2,
  1733. ve3;
  1734. setlocale(LC_NUMERIC,"C");
  1735. FILE *os = NULL;
  1736. os = fopen (file, "w");
  1737. if (os == NULL)
  1738. return 0;
  1739. //ecriture des en-tetes
  1740. fprintf (os, "ply\nformat ascii 1.0\ncomment zipper output\n");
  1741. fprintf (os,
  1742. "element vertex %d\nproperty float x\nproperty float y\nproperty float z\nelement face %d\n",
  1743. m->nbvertex, m->nbface);
  1744. fprintf (os, "property list uchar int vertex_indices\nend_header\n");
  1745. //ecriture des sommets
  1746. for (int i = 0; i < m->nbvertex; i++)
  1747. fprintf (os, "%f %f %f\n", m->ve[i].x, m->ve[i].y, m->ve[i].z);
  1748. //ecriture des faces
  1749. for (int i = 0; i < m->nbface; i++)
  1750. {
  1751. ve1 = m->fa[i].ve1;
  1752. ve2 = m->fa[i].ve2;
  1753. ve3 = m->fa[i].ve3;
  1754. fprintf (os, "3 %d %d %d \n", ve1, ve2, ve3);
  1755. }
  1756. fclose (os);
  1757. setlocale(LC_NUMERIC,"");
  1758. return 1;
  1759. }
  1760. /**
  1761. Enregistrement d'un modèle dans un fichier obj
  1762. @param file chemin du fichier
  1763. @param m modèle à sauvegarder
  1764. @return 1 si succès, 0 sinon
  1765. */
  1766. int
  1767. a2ri_vf_save_obj (
  1768. const char * const file,
  1769. const vf_model * const m)
  1770. {
  1771. setlocale(LC_NUMERIC,"C");
  1772. FILE *os = NULL;
  1773. os = fopen (file, "w");
  1774. if (os == NULL)
  1775. return 0;
  1776. //ecriture des sommets
  1777. for (int i = 0; i < m->nbvertex; i++)
  1778. fprintf (os, "v %lf %lf %lf\n", m->ve[i].x, m->ve[i].y, m->ve[i].z);
  1779. fprintf (os, "\n");
  1780. //ecriture des faces
  1781. for (int i = 0; i < m->nbface; i++)
  1782. fprintf (os, "f %d %d %d\n", m->fa[i].ve1 + 1, m->fa[i].ve2 + 1,
  1783. m->fa[i].ve3 + 1);
  1784. fclose (os);
  1785. setlocale(LC_NUMERIC,"");
  1786. return 1;
  1787. }
  1788. /**
  1789. Enregistrement d'un modèle dans un fichier stl binaire
  1790. @param file chemin du fichier
  1791. @param m modèle à sauvegarder
  1792. @return 1 si succés, 0 sinon
  1793. */
  1794. int
  1795. a2ri_vf_save_binary_stl (
  1796. const char * const file,
  1797. const vf_model * const m)
  1798. {
  1799. setlocale(LC_NUMERIC,"C");
  1800. FILE *os = NULL;
  1801. os = fopen (file, "wb");
  1802. if (os == NULL)
  1803. return 0;
  1804. fwrite
  1805. ("generated by A2RI ",
  1806. 1, 80, os);
  1807. fwrite (&(m->nbface), sizeof (unsigned int), 1, os);
  1808. for (int i = 0; i < m->nbface; i++)
  1809. {
  1810. vector3d AB,
  1811. AC,
  1812. normale;
  1813. vector3d_init (&AB,
  1814. m->ve[m->fa[i].ve2].x - m->ve[m->fa[i].ve1].x,
  1815. m->ve[m->fa[i].ve2].y - m->ve[m->fa[i].ve1].y,
  1816. m->ve[m->fa[i].ve2].z - m->ve[m->fa[i].ve1].z);
  1817. vector3d_init (&AC,
  1818. m->ve[m->fa[i].ve3].x - m->ve[m->fa[i].ve1].x,
  1819. m->ve[m->fa[i].ve3].y - m->ve[m->fa[i].ve1].y,
  1820. m->ve[m->fa[i].ve3].z - m->ve[m->fa[i].ve1].z);
  1821. normale = vector3d_vectorialproduct (&AB, &AC);
  1822. float x,
  1823. y,
  1824. z;
  1825. x = normale.dx;
  1826. y = normale.dy;
  1827. z = normale.dz;
  1828. fwrite (&x, sizeof (float), 1, os);
  1829. fwrite (&y, sizeof (float), 1, os);
  1830. fwrite (&z, sizeof (float), 1, os);
  1831. x = m->ve[m->fa[i].ve1].x;
  1832. y = m->ve[m->fa[i].ve1].y;
  1833. z = m->ve[m->fa[i].ve1].z;
  1834. fwrite (&x, sizeof (float), 1, os);
  1835. fwrite (&y, sizeof (float), 1, os);
  1836. fwrite (&z, sizeof (float), 1, os);
  1837. x = m->ve[m->fa[i].ve2].x;
  1838. y = m->ve[m->fa[i].ve2].y;
  1839. z = m->ve[m->fa[i].ve2].z;
  1840. fwrite (&x, sizeof (float), 1, os);
  1841. fwrite (&y, sizeof (float), 1, os);
  1842. fwrite (&z, sizeof (float), 1, os);
  1843. x = m->ve[m->fa[i].ve3].x;
  1844. y = m->ve[m->fa[i].ve3].y;
  1845. z = m->ve[m->fa[i].ve3].z;
  1846. fwrite (&x, sizeof (float), 1, os);
  1847. fwrite (&y, sizeof (float), 1, os);
  1848. fwrite (&z, sizeof (float), 1, os);
  1849. fwrite (" ", 1, 2, os);
  1850. }
  1851. fclose (os);
  1852. setlocale(LC_NUMERIC,"");
  1853. return 1;
  1854. }
  1855. /**
  1856. Enregistrement d'un modèle dans un fichier stl ascii
  1857. @param file chemin du fichier
  1858. @param m modèle à sauvegarder
  1859. @return 1 si succés, 0 sinon
  1860. */
  1861. int
  1862. a2ri_vf_save_ascii_stl (
  1863. const char * const file,
  1864. const vf_model * const m)
  1865. {
  1866. setlocale(LC_NUMERIC,"C");
  1867. FILE *os = NULL;
  1868. os = fopen (file, "w");
  1869. if (os == NULL)
  1870. return 0;
  1871. fprintf(os,"solid generatedByA2RI\n");
  1872. for (int i = 0; i < m->nbface; i++)
  1873. {
  1874. vector3d AB,
  1875. AC,
  1876. normale;
  1877. vector3d_init (&AB,
  1878. m->ve[m->fa[i].ve2].x - m->ve[m->fa[i].ve1].x,
  1879. m->ve[m->fa[i].ve2].y - m->ve[m->fa[i].ve1].y,
  1880. m->ve[m->fa[i].ve2].z - m->ve[m->fa[i].ve1].z);
  1881. vector3d_init (&AC,
  1882. m->ve[m->fa[i].ve3].x - m->ve[m->fa[i].ve1].x,
  1883. m->ve[m->fa[i].ve3].y - m->ve[m->fa[i].ve1].y,
  1884. m->ve[m->fa[i].ve3].z - m->ve[m->fa[i].ve1].z);
  1885. normale = vector3d_vectorialproduct (&AB, &AC);
  1886. float x,
  1887. y,
  1888. z;
  1889. x = normale.dx;
  1890. y = normale.dy;
  1891. z = normale.dz;
  1892. fprintf (os,"facet normal %f %f %f\n\touter loop\n\t\tvertex ",x,y,z);
  1893. x = m->ve[m->fa[i].ve1].x;
  1894. y = m->ve[m->fa[i].ve1].y;
  1895. z = m->ve[m->fa[i].ve1].z;
  1896. fprintf (os,"%f %f %f\n\t\tvertex ",x,y,z);
  1897. x = m->ve[m->fa[i].ve2].x;
  1898. y = m->ve[m->fa[i].ve2].y;
  1899. z = m->ve[m->fa[i].ve2].z;
  1900. fprintf (os,"%f %f %f\n\t\tvertex ",x,y,z);
  1901. x = m->ve[m->fa[i].ve3].x;
  1902. y = m->ve[m->fa[i].ve3].y;
  1903. z = m->ve[m->fa[i].ve3].z;
  1904. fprintf (os,"%f %f %f\n\tendloop\nendfacet\n",x,y,z);
  1905. }
  1906. fprintf(os,"endsolid generatedByA2RI\n");
  1907. fclose (os);
  1908. setlocale(LC_NUMERIC,"");
  1909. return 1;
  1910. }
  1911. /**
  1912. Ouverture d'un fichier contenant un modèle
  1913. @param file chemin du fichier à ouvrir
  1914. @param m pointeur sur le modèle
  1915. @return 1 si succès, 0 sinon
  1916. */
  1917. int
  1918. a2ri_vef_open_file (
  1919. const char * const file,
  1920. vef_model * m)
  1921. {
  1922. char ext[5];
  1923. memcpy (ext, file + (strlen (file) - 4), 4);
  1924. ext[4] = 0;
  1925. //appel de la fonction correspondant à l'extension du fichier
  1926. if (!strcmp (ext, ".wrl"))
  1927. return (a2ri_vef_load_wrl (file, m));
  1928. if (!strcmp (ext, ".vef"))
  1929. return (a2ri_vef_load_vef (file, m));
  1930. if (!strcmp (ext, ".pgn"))
  1931. return (a2ri_vef_load_pgn (file, m));
  1932. if (!strcmp (ext, ".ply"))
  1933. return (a2ri_vef_load_ply (file, m));
  1934. if (!strcmp (ext, ".off"))
  1935. return (a2ri_vef_load_off (file, m));
  1936. if (!strcmp (ext, ".obj"))
  1937. return (a2ri_vef_load_obj (file, m));
  1938. if (!strcmp (ext, ".gts"))
  1939. return (a2ri_vef_load_gts (file, m));
  1940. if (!strcmp (ext, ".stl"))
  1941. return (a2ri_vef_load_stl (file, m));
  1942. return 0;
  1943. }
  1944. /**
  1945. Ouverture d'un fichier pgn contenant un modèle
  1946. @param file chemin du fichier à ouvrir
  1947. @param m pointeur sur le modèle
  1948. @return 1 si succès, 0 sinon
  1949. */
  1950. int
  1951. a2ri_vef_load_pgn (
  1952. const char * const file,
  1953. vef_model * m)
  1954. {
  1955. FILE *is = NULL;
  1956. double coordx,
  1957. coordy,
  1958. coordz;
  1959. int ar1,
  1960. ar2,
  1961. ar3,
  1962. nbvertex,
  1963. nbface;
  1964. setlocale(LC_NUMERIC,"C");
  1965. is = fopen (file, "r");
  1966. if (is == NULL)
  1967. return 0;
  1968. a2ri_vef_init (m);
  1969. //lecture du nombre de sommets
  1970. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbvertex),"erreur en ouverture de fichier");
  1971. //lecture du nombre de faces
  1972. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbface),"erreur en ouverture de fichier");
  1973. //lecture des sommets
  1974. for (int i = 0; i < nbvertex; i++)
  1975. {
  1976. a2ri_erreur_critique_si(!fscanf (is, "%lf", &coordx),"erreur en ouverture de fichier");
  1977. a2ri_erreur_critique_si(!fscanf (is, "%lf", &coordy),"erreur en ouverture de fichier");
  1978. a2ri_erreur_critique_si(!fscanf (is, "%lf", &coordz),"erreur en ouverture de fichier");
  1979. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  1980. //mis à jour de la bounding box
  1981. if (i == 0)
  1982. {
  1983. m->xmin = coordx;
  1984. m->xmax = coordx;
  1985. m->ymin = coordy;
  1986. m->ymax = coordy;
  1987. m->zmin = coordz;
  1988. m->zmax = coordz;
  1989. }
  1990. else
  1991. {
  1992. if (coordx < m->xmin)
  1993. m->xmin = coordx;
  1994. if (coordx > m->xmax)
  1995. m->xmax = coordx;
  1996. if (coordy < m->ymin)
  1997. m->ymin = coordy;
  1998. if (coordy > m->ymax)
  1999. m->ymax = coordy;
  2000. if (coordz < m->zmin)
  2001. m->zmin = coordz;
  2002. if (coordz > m->zmax)
  2003. m->zmax = coordz;
  2004. }
  2005. }
  2006. //lecture des faces
  2007. for (int i = 0; i < nbface; i++)
  2008. {
  2009. //lecture des trois sommets composant la face
  2010. a2ri_erreur_critique_si(!fscanf (is, "%d", &ar1),"erreur en ouverture de fichier");
  2011. a2ri_erreur_critique_si(!fscanf (is, "%d", &ar2),"erreur en ouverture de fichier");
  2012. a2ri_erreur_critique_si(!fscanf (is, "%d", &ar3),"erreur en ouverture de fichier");
  2013. //ajout des aretes avec vérification au préalable de leur existence
  2014. a2ri_vef_add_edge (m, ar1, ar2, 1);
  2015. a2ri_vef_add_edge (m, ar2, ar3, 1);
  2016. a2ri_vef_add_edge (m, ar3, ar1, 1);
  2017. //recherche des aretes
  2018. int ed1 = a2ri_vef_search_edge (m, ar1, ar2);
  2019. int ed2 = a2ri_vef_search_edge (m, ar2, ar3);
  2020. int ed3 = a2ri_vef_search_edge (m, ar3, ar1);
  2021. //ajout de la faces avec les trois aretes
  2022. a2ri_vef_add_face (m, ed1, ed2, ed3);
  2023. // reading the -1
  2024. a2ri_erreur_critique_si(!fscanf (is, "%d", &ar1),"erreur en ouverture de fichier");
  2025. }
  2026. fclose (is);
  2027. setlocale(LC_NUMERIC,"");
  2028. return 1;
  2029. }
  2030. /**
  2031. Ouverture d'un fichier stl binaire contenant un modèle
  2032. @param file chemin du fichier à ouvrir
  2033. @param m pointeur sur le modèle
  2034. @return 1 si succès, 0 sinon
  2035. */
  2036. int
  2037. a2ri_vef_load_binary_stl (
  2038. const char * const file,
  2039. vef_model * m)
  2040. {
  2041. FILE *is = NULL;
  2042. char str[80];
  2043. unsigned int nbface;
  2044. unsigned char finligne;
  2045. float coordx,
  2046. coordy,
  2047. coordz;
  2048. int ve1,
  2049. ve2,
  2050. ve3,
  2051. ed1,
  2052. ed2,
  2053. ed3;
  2054. setlocale(LC_NUMERIC,"C");
  2055. is = fopen (file, "rb");
  2056. if (is == NULL)
  2057. return 0;
  2058. a2ri_erreur_critique_si(!fread (&str, sizeof (char), 80, is),"erreur en ouverture de fichier");
  2059. a2ri_erreur_critique_si(!fread (&nbface, sizeof (unsigned int), 1, is),"erreur en ouverture de fichier");
  2060. for (unsigned int i = 0; i < nbface; i++)
  2061. {
  2062. a2ri_erreur_critique_si(!fread (&coordx, sizeof (float), 1, is),"erreur en ouverture de fichier");
  2063. a2ri_erreur_critique_si(!fread (&coordy, sizeof (float), 1, is),"erreur en ouverture de fichier");
  2064. a2ri_erreur_critique_si(!fread (&coordz, sizeof (float), 1, is),"erreur en ouverture de fichier"); //read the normal
  2065. a2ri_erreur_critique_si(!fread (&coordx, sizeof (float), 1, is),"erreur en ouverture de fichier");
  2066. a2ri_erreur_critique_si(!fread (&coordy, sizeof (float), 1, is),"erreur en ouverture de fichier");
  2067. a2ri_erreur_critique_si(!fread (&coordz, sizeof (float), 1, is),"erreur en ouverture de fichier"); //read the first vertex
  2068. ve1 = a2ri_vef_search_vertex (m, coordx, coordy, coordz);
  2069. if (ve1 == -1)
  2070. {
  2071. ve1 = m->nbvertex;
  2072. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2073. }
  2074. if (i == 0)
  2075. {
  2076. m->xmin = coordx;
  2077. m->xmax = coordx;
  2078. m->ymin = coordy;
  2079. m->ymax = coordy;
  2080. m->zmin = coordz;
  2081. m->zmax = coordz;
  2082. }
  2083. else
  2084. {
  2085. if (coordx < m->xmin)
  2086. m->xmin = coordx;
  2087. if (coordx > m->xmax)
  2088. m->xmax = coordx;
  2089. if (coordy < m->ymin)
  2090. m->ymin = coordy;
  2091. if (coordy > m->ymax)
  2092. m->ymax = coordy;
  2093. if (coordz < m->zmin)
  2094. m->zmin = coordz;
  2095. if (coordz > m->zmax)
  2096. m->zmax = coordz;
  2097. }
  2098. a2ri_erreur_critique_si(!fread (&coordx, sizeof (float), 1, is),"erreur en ouverture de fichier");
  2099. a2ri_erreur_critique_si(!fread (&coordy, sizeof (float), 1, is),"erreur en ouverture de fichier");
  2100. a2ri_erreur_critique_si(!fread (&coordz, sizeof (float), 1, is),"erreur en ouverture de fichier"); //read the second vertex
  2101. ve2 = a2ri_vef_search_vertex (m, coordx, coordy, coordz);
  2102. if (ve2 == -1)
  2103. {
  2104. ve2 = m->nbvertex;
  2105. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2106. }
  2107. if (coordx < m->xmin)
  2108. m->xmin = coordx;
  2109. if (coordx > m->xmax)
  2110. m->xmax = coordx;
  2111. if (coordy < m->ymin)
  2112. m->ymin = coordy;
  2113. if (coordy > m->ymax)
  2114. m->ymax = coordy;
  2115. if (coordz < m->zmin)
  2116. m->zmin = coordz;
  2117. if (coordz > m->zmax)
  2118. m->zmax = coordz;
  2119. a2ri_erreur_critique_si(!fread (&coordx, sizeof (float), 1, is),"erreur en ouverture de fichier");
  2120. a2ri_erreur_critique_si(!fread (&coordy, sizeof (float), 1, is),"erreur en ouverture de fichier");
  2121. a2ri_erreur_critique_si(!fread (&coordz, sizeof (float), 1, is),"erreur en ouverture de fichier"); //read the third vertex
  2122. ve3 = a2ri_vef_search_vertex (m, coordx, coordy, coordz);
  2123. if (ve3 == -1)
  2124. {
  2125. ve3 = m->nbvertex;
  2126. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2127. }
  2128. if (coordx < m->xmin)
  2129. m->xmin = coordx;
  2130. if (coordx > m->xmax)
  2131. m->xmax = coordx;
  2132. if (coordy < m->ymin)
  2133. m->ymin = coordy;
  2134. if (coordy > m->ymax)
  2135. m->ymax = coordy;
  2136. if (coordz < m->zmin)
  2137. m->zmin = coordz;
  2138. if (coordz > m->zmax)
  2139. m->zmax = coordz;
  2140. ed1 = a2ri_vef_search_edge (m, ve1, ve2);
  2141. ed2 = a2ri_vef_search_edge (m, ve1, ve3);
  2142. ed3 = a2ri_vef_search_edge (m, ve2, ve3);
  2143. if (ed1 == -1)
  2144. {
  2145. ed1 = m->nbedge;
  2146. a2ri_vef_add_edge (m, ve1, ve2, 0);
  2147. }
  2148. if (ed2 == -1)
  2149. {
  2150. ed2 = m->nbedge;
  2151. a2ri_vef_add_edge (m, ve1, ve3, 0);
  2152. }
  2153. if (ed3 == -1)
  2154. {
  2155. ed3 = m->nbedge;
  2156. a2ri_vef_add_edge (m, ve2, ve3, 0);
  2157. }
  2158. a2ri_vef_add_face (m, ed2, ed1, ed3);
  2159. a2ri_erreur_critique_si(!fread (&finligne, 1, 1, is),"erreur en ouverture de fichier");
  2160. a2ri_erreur_critique_si(!fread (&finligne, 1, 1, is),"erreur en ouverture de fichier");
  2161. }
  2162. fclose (is);
  2163. setlocale(LC_NUMERIC,"");
  2164. return 1;
  2165. }
  2166. /**
  2167. Ouverture d'un fichier stl ascii contenant un modèle
  2168. @param file chemin du fichier à ouvrir
  2169. @param m pointeur sur le modèle
  2170. @return 1 si succès, 0 sinon
  2171. */
  2172. int
  2173. a2ri_vef_load_ascii_stl (
  2174. const char * const file,
  2175. vef_model * m)
  2176. {
  2177. FILE *is = NULL;
  2178. int ve1,
  2179. ve2,
  2180. ve3,
  2181. i = 0,
  2182. fin = 0,
  2183. ed1,
  2184. ed2,
  2185. ed3;
  2186. double coordx,
  2187. coordy,
  2188. coordz;
  2189. char str[250];
  2190. setlocale(LC_NUMERIC,"C");
  2191. is = fopen (file, "r");
  2192. if (is == NULL)
  2193. return 0;
  2194. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2195. while (strcmp (str, "facet"))
  2196. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2197. while (!fin)
  2198. {
  2199. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier"); //read normal
  2200. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2201. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2202. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier"); //read the 3 values of the normal
  2203. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2204. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier"); //outer loop
  2205. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier"); //read vertex
  2206. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2207. coordx = atof (str); //coordx
  2208. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2209. coordy = atof (str); //coordy
  2210. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2211. coordz = atof (str); //coordz
  2212. ve1 = a2ri_vef_search_vertex (m, coordx, coordy, coordz);
  2213. if (ve1 == -1)
  2214. {
  2215. ve1 = m->nbvertex;
  2216. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2217. }
  2218. if (i == 0)
  2219. {
  2220. m->xmin = coordx;
  2221. m->xmax = coordx;
  2222. m->ymin = coordy;
  2223. m->ymax = coordy;
  2224. m->zmin = coordz;
  2225. m->zmax = coordz;
  2226. i++;
  2227. }
  2228. else
  2229. {
  2230. if (coordx < m->xmin)
  2231. m->xmin = coordx;
  2232. if (coordx > m->xmax)
  2233. m->xmax = coordx;
  2234. if (coordy < m->ymin)
  2235. m->ymin = coordy;
  2236. if (coordy > m->ymax)
  2237. m->ymax = coordy;
  2238. if (coordz < m->zmin)
  2239. m->zmin = coordz;
  2240. if (coordz > m->zmax)
  2241. m->zmax = coordz;
  2242. }
  2243. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier"); //read vertex
  2244. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2245. coordx = atof (str); //coordx
  2246. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2247. coordy = atof (str); //coordy
  2248. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2249. coordz = atof (str); //coordz
  2250. ve2 = a2ri_vef_search_vertex (m, coordx, coordy, coordz);
  2251. if (ve2 == -1)
  2252. {
  2253. ve2 = m->nbvertex;
  2254. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2255. }
  2256. if (coordx < m->xmin)
  2257. m->xmin = coordx;
  2258. if (coordx > m->xmax)
  2259. m->xmax = coordx;
  2260. if (coordy < m->ymin)
  2261. m->ymin = coordy;
  2262. if (coordy > m->ymax)
  2263. m->ymax = coordy;
  2264. if (coordz < m->zmin)
  2265. m->zmin = coordz;
  2266. if (coordz > m->zmax)
  2267. m->zmax = coordz;
  2268. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier"); //read vertex
  2269. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2270. coordx = atof (str); //coordx
  2271. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2272. coordy = atof (str); //coordy
  2273. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2274. coordz = atof (str); //coordz;
  2275. ve3 = a2ri_vef_search_vertex (m, coordx, coordy, coordz);
  2276. if (ve3 == -1)
  2277. {
  2278. ve3 = m->nbvertex;
  2279. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2280. }
  2281. if (coordx < m->xmin)
  2282. m->xmin = coordx;
  2283. if (coordx > m->xmax)
  2284. m->xmax = coordx;
  2285. if (coordy < m->ymin)
  2286. m->ymin = coordy;
  2287. if (coordy > m->ymax)
  2288. m->ymax = coordy;
  2289. if (coordz < m->zmin)
  2290. m->zmin = coordz;
  2291. if (coordz > m->zmax)
  2292. m->zmax = coordz;
  2293. ed1 = a2ri_vef_search_edge (m, ve1, ve2);
  2294. ed2 = a2ri_vef_search_edge (m, ve1, ve3);
  2295. ed3 = a2ri_vef_search_edge (m, ve2, ve3);
  2296. if (ed1 == -1)
  2297. {
  2298. ed1 = m->nbedge;
  2299. a2ri_vef_add_edge (m, ve1, ve2, 0);
  2300. }
  2301. if (ed2 == -1)
  2302. {
  2303. ed2 = m->nbedge;
  2304. a2ri_vef_add_edge (m, ve1, ve3, 0);
  2305. }
  2306. if (ed3 == -1)
  2307. {
  2308. ed3 = m->nbedge;
  2309. a2ri_vef_add_edge (m, ve2, ve3, 0);
  2310. }
  2311. a2ri_vef_add_face (m, ed2, ed1, ed3);
  2312. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier"); //read endloop
  2313. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier"); //read endfacet
  2314. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier"); //read facet ou endsolid
  2315. fin = !strcmp (str, "endsolid");
  2316. }
  2317. fclose (is);
  2318. setlocale(LC_NUMERIC,"");
  2319. return 1;
  2320. }
  2321. /**
  2322. Ouverture d'un fichier stl contenant un modèle
  2323. @param file chemin du fichier à ouvrir
  2324. @param m pointeur sur le modèle
  2325. @return 1 si succès, 0 sinon
  2326. */
  2327. int
  2328. a2ri_vef_load_stl (
  2329. const char * const file,
  2330. vef_model * m)
  2331. {
  2332. FILE *is = NULL;
  2333. char str[250];
  2334. is = fopen (file, "r");
  2335. if (is == NULL)
  2336. return 0;
  2337. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2338. fclose (is);
  2339. if (!strcmp (str, "solid"))
  2340. return a2ri_vef_load_ascii_stl (file, m);
  2341. else
  2342. return a2ri_vef_load_binary_stl (file, m);
  2343. }
  2344. /**
  2345. Ouverture d'un fichier obj contenant un modèle
  2346. @param file chemin du fichier à ouvrir
  2347. @param m pointeur sur le modèle
  2348. @return 1 si succès, 0 sinon
  2349. */
  2350. int
  2351. a2ri_vef_load_obj (
  2352. const char * const file,
  2353. vef_model * m)
  2354. {
  2355. FILE *is = NULL;
  2356. int ve1,
  2357. ve2,
  2358. ve3;
  2359. double coordx,
  2360. coordy,
  2361. coordz;
  2362. char str[250];
  2363. setlocale(LC_NUMERIC,"C");
  2364. is = fopen (file, "r");
  2365. if (is == NULL)
  2366. return 0;
  2367. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2368. while (strcmp (str, "v") != 0)
  2369. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2370. //lecture des sommets
  2371. while (strcmp (str, "g") != 0)
  2372. {
  2373. a2ri_erreur_critique_si(!fscanf (is, "%lf %lf %lf", &coordx, &coordy, &coordz),"erreur en ouverture de fichier");
  2374. //ajout d'un sommet
  2375. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2376. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2377. //mis à jour de la bounding box
  2378. if (m->nbvertex == 1)
  2379. {
  2380. m->xmin = coordx;
  2381. m->xmax = coordx;
  2382. m->ymin = coordy;
  2383. m->ymax = coordy;
  2384. m->zmin = coordz;
  2385. m->zmax = coordz;
  2386. }
  2387. else
  2388. {
  2389. if (coordx < m->xmin)
  2390. m->xmin = coordx;
  2391. if (coordx > m->xmax)
  2392. m->xmax = coordx;
  2393. if (coordy < m->ymin)
  2394. m->ymin = coordy;
  2395. if (coordy > m->ymax)
  2396. m->ymax = coordy;
  2397. if (coordz < m->zmin)
  2398. m->zmin = coordz;
  2399. if (coordz > m->zmax)
  2400. m->zmax = coordz;
  2401. }
  2402. }
  2403. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2404. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2405. //lecture des faces
  2406. while (!feof (is))
  2407. {
  2408. //lecture des trois sommets
  2409. a2ri_erreur_critique_si(!fscanf (is, "%d %d %d", &ve1, &ve2, &ve3),"erreur en ouverture de fichier");
  2410. ve1--;
  2411. ve2--;
  2412. ve3--;
  2413. //ajout des aretes avec vérification au préalable de leur existence
  2414. a2ri_vef_add_edge (m, ve1, ve2, 1);
  2415. a2ri_vef_add_edge (m, ve2, ve3, 1);
  2416. a2ri_vef_add_edge (m, ve3, ve1, 1);
  2417. //recherche des aretes
  2418. int ed1 = a2ri_vef_search_edge (m, ve1, ve2);
  2419. int ed2 = a2ri_vef_search_edge (m, ve2, ve3);
  2420. int ed3 = a2ri_vef_search_edge (m, ve3, ve1);
  2421. //ajout de la faces avec les trois aretes
  2422. a2ri_vef_add_face (m, ed1, ed2, ed3);
  2423. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2424. //si la face compte plus de trois sommets, on relit et on ajoute des aretes et une seconde face
  2425. if (strcmp (str, "f") != 0)
  2426. {
  2427. ve2 = atoi (str);
  2428. ve2--;
  2429. a2ri_vef_add_edge (m, ve1, ve2, 1);
  2430. a2ri_vef_add_edge (m, ve2, ve3, 1);
  2431. a2ri_vef_add_edge (m, ve3, ve1, 1);
  2432. int ed1 = a2ri_vef_search_edge (m, ve1, ve2);
  2433. int ed2 = a2ri_vef_search_edge (m, ve2, ve3);
  2434. int ed3 = a2ri_vef_search_edge (m, ve3, ve1);
  2435. a2ri_vef_add_face (m, ed1, ed3, ed2);
  2436. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2437. }
  2438. }
  2439. fclose (is);
  2440. setlocale(LC_NUMERIC,"");
  2441. return 1;
  2442. }
  2443. /**
  2444. Ouverture d'un fichier wrl contenant un modèle
  2445. @param file chemin du fichier à ouvrir
  2446. @param m pointeur sur le modèle
  2447. @return 1 si succès, 0 sinon
  2448. */
  2449. int
  2450. a2ri_vef_load_wrl (
  2451. const char * const file,
  2452. vef_model * m)
  2453. {
  2454. FILE *is = NULL;
  2455. char str[20];
  2456. char coordx[15],
  2457. coordy[15],
  2458. coordz[15];
  2459. setlocale(LC_NUMERIC,"C");
  2460. is = fopen (file, "r");
  2461. if (is == NULL)
  2462. return 0;
  2463. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2464. while (strcmp (str, "point") && strcmp (str, "point["))
  2465. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2466. if (!strcmp (str, "point"))
  2467. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2468. // reading the file until coordx = } or ]
  2469. a2ri_erreur_critique_si(!fscanf (is, "%s", coordx),"erreur en ouverture de fichier");
  2470. int i = 0;
  2471. //lecture des sommets
  2472. while (strcmp (coordx, "}") && strcmp (coordx, "]"))
  2473. {
  2474. a2ri_erreur_critique_si(!fscanf (is, "%s", coordy),"erreur en ouverture de fichier");
  2475. a2ri_erreur_critique_si(!fscanf (is, "%s", coordz),"erreur en ouverture de fichier");
  2476. if (coordz[strlen (coordz) - 1] == ',')
  2477. {
  2478. memcpy (coordz, coordz, strlen (coordz) - 1);
  2479. coordz[strlen (coordz)] = '\0';
  2480. }
  2481. a2ri_vef_add_vertex (m, atof (coordx), atof (coordy), atof (coordz));
  2482. //mis à jour de la bounding box
  2483. if (i == 0)
  2484. {
  2485. i++;
  2486. m->xmin = atof (coordx);
  2487. m->xmax = atof (coordx);
  2488. m->ymin = atof (coordy);
  2489. m->ymax = atof (coordy);
  2490. m->zmin = atof (coordz);
  2491. m->zmax = atof (coordz);
  2492. }
  2493. else
  2494. {
  2495. if (atof (coordx) < m->xmin)
  2496. m->xmin = atof (coordx);
  2497. if (atof (coordx) > m->xmax)
  2498. m->xmax = atof (coordx);
  2499. if (atof (coordy) < m->ymin)
  2500. m->ymin = atof (coordy);
  2501. if (atof (coordy) > m->ymax)
  2502. m->ymax = atof (coordy);
  2503. if (atof (coordz) < m->zmin)
  2504. m->zmin = atof (coordz);
  2505. if (atof (coordz) > m->zmax)
  2506. m->zmax = atof (coordz);
  2507. }
  2508. a2ri_erreur_critique_si(!fscanf (is, "%s", coordx),"erreur en ouverture de fichier");
  2509. }
  2510. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2511. while (strcmp (str, "coordIndex") && strcmp (str, "coordIndex["))
  2512. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2513. if (!strcmp (str, "coordIndex"))
  2514. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2515. //lecture des faces
  2516. a2ri_erreur_critique_si(!fscanf (is, "%s", coordx),"erreur en ouverture de fichier");
  2517. while (strcmp (coordx, "}") && strcmp (coordx, "]"))
  2518. {
  2519. //lecture des trois sommets composant la face
  2520. a2ri_erreur_critique_si(!fscanf (is, "%s", coordy),"erreur en ouverture de fichier");
  2521. a2ri_erreur_critique_si(!fscanf (is, "%s", coordz),"erreur en ouverture de fichier");
  2522. //ajout des aretes avec vérification au préalable de leur existence
  2523. a2ri_vef_add_edge (m, atoi (coordx), atoi (coordy), 1);
  2524. a2ri_vef_add_edge (m, atoi (coordy), atoi (coordz), 1);
  2525. a2ri_vef_add_edge (m, atoi (coordz), atoi (coordx), 1);
  2526. //recherche des aretes
  2527. int ed1 = a2ri_vef_search_edge (m, atoi (coordx), atoi (coordy));
  2528. int ed2 = a2ri_vef_search_edge (m, atoi (coordy), atoi (coordz));
  2529. int ed3 = a2ri_vef_search_edge (m, atoi (coordz), atoi (coordx));
  2530. //ajout de la faces avec les trois aretes
  2531. a2ri_vef_add_face (m, ed1, ed2, ed3);
  2532. // reading the -1
  2533. a2ri_erreur_critique_si(!fscanf (is, "%s", coordx),"erreur en ouverture de fichier");
  2534. a2ri_erreur_critique_si(!fscanf (is, "%s", coordx),"erreur en ouverture de fichier");
  2535. }
  2536. fclose (is);
  2537. setlocale(LC_NUMERIC,"");
  2538. return 1;
  2539. }
  2540. /**
  2541. Ouverture d'un fichier vef contenant un modèle
  2542. @param file chemin du fichier à ouvrir
  2543. @param m pointeur sur le modèle
  2544. @return 1 si succès, 0 sinon
  2545. */
  2546. int
  2547. a2ri_vef_load_vef (
  2548. const char * const file,
  2549. vef_model * m)
  2550. {
  2551. FILE *is = NULL;
  2552. int nbvertex,
  2553. nbedge,
  2554. nbface,
  2555. ve1,
  2556. ve2,
  2557. ed1,
  2558. ed2,
  2559. ed3;
  2560. double coordx,
  2561. coordy,
  2562. coordz;
  2563. setlocale(LC_NUMERIC,"C");
  2564. is = fopen (file, "r");
  2565. if (is == NULL)
  2566. return 0;
  2567. a2ri_vef_init (m);
  2568. //lecture du nombre de sommets
  2569. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbvertex),"erreur en ouverture de fichier");
  2570. //lecture du nombre d'aretes
  2571. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbedge),"erreur en ouverture de fichier");
  2572. //lecture du nombre de face
  2573. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbface),"erreur en ouverture de fichier");
  2574. //lecture des sommets
  2575. for (int i = 0; i < nbvertex; i++)
  2576. {
  2577. a2ri_erreur_critique_si(!fscanf (is, "%lf", &coordx),"erreur en ouverture de fichier");
  2578. a2ri_erreur_critique_si(!fscanf (is, "%lf", &coordy),"erreur en ouverture de fichier");
  2579. a2ri_erreur_critique_si(!fscanf (is, "%lf", &coordz),"erreur en ouverture de fichier");
  2580. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2581. //mis à jour de la bounding box
  2582. if (i == 0)
  2583. {
  2584. m->xmin = coordx;
  2585. m->xmax = coordx;
  2586. m->ymin = coordy;
  2587. m->ymax = coordy;
  2588. m->zmin = coordz;
  2589. m->zmax = coordz;
  2590. }
  2591. else
  2592. {
  2593. if (coordx < m->xmin)
  2594. m->xmin = coordx;
  2595. if (coordx > m->xmax)
  2596. m->xmax = coordx;
  2597. if (coordy < m->ymin)
  2598. m->ymin = coordy;
  2599. if (coordy > m->ymax)
  2600. m->ymax = coordy;
  2601. if (coordz < m->zmin)
  2602. m->zmin = coordz;
  2603. if (coordz > m->zmax)
  2604. m->zmax = coordz;
  2605. }
  2606. }
  2607. //lecture des aretes
  2608. for (int i = 0; i < nbedge; i++)
  2609. {
  2610. a2ri_erreur_critique_si(!fscanf (is, "%d", &ve1),"erreur en ouverture de fichier");
  2611. a2ri_erreur_critique_si(!fscanf (is, "%d", &ve2),"erreur en ouverture de fichier");
  2612. a2ri_vef_add_edge (m, ve1, ve2, 0);
  2613. }
  2614. //lecture des faces
  2615. for (int i = 0; i < nbface; i++)
  2616. {
  2617. a2ri_erreur_critique_si(!fscanf (is, "%d", &ed1),"erreur en ouverture de fichier");
  2618. a2ri_erreur_critique_si(!fscanf (is, "%d", &ed2),"erreur en ouverture de fichier");
  2619. a2ri_erreur_critique_si(!fscanf (is, "%d", &ed3),"erreur en ouverture de fichier");
  2620. a2ri_vef_add_face (m, ed1, ed2, ed3);
  2621. }
  2622. fclose (is);
  2623. setlocale(LC_NUMERIC,"");
  2624. return 1;
  2625. }
  2626. /**
  2627. Ouverture d'un fichier gts contenant un modèle
  2628. @param file chemin du fichier à ouvrir
  2629. @param m pointeur sur le modèle
  2630. @return 1 si succès, 0 sinon
  2631. */
  2632. int
  2633. a2ri_vef_load_gts (
  2634. const char * const file,
  2635. vef_model * m)
  2636. {
  2637. FILE *is = NULL;
  2638. int nbvertex,
  2639. nbedge,
  2640. nbface,
  2641. ve1,
  2642. ve2,
  2643. ed1,
  2644. ed2,
  2645. ed3;
  2646. double coordx,
  2647. coordy,
  2648. coordz;
  2649. setlocale(LC_NUMERIC,"C");
  2650. is = fopen (file, "r");
  2651. if (is == NULL)
  2652. return 0;
  2653. a2ri_vef_init (m);
  2654. //lecture du nombre de sommets
  2655. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbvertex),"erreur en ouverture de fichier");
  2656. //lecture du nombre d'aretes
  2657. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbedge),"erreur en ouverture de fichier");
  2658. //lecture du nombre de face
  2659. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbface),"erreur en ouverture de fichier");
  2660. //lecture des sommets
  2661. for (int i = 0; i < nbvertex; i++)
  2662. {
  2663. a2ri_erreur_critique_si(!fscanf (is, "%lf", &coordx),"erreur en ouverture de fichier");
  2664. a2ri_erreur_critique_si(!fscanf (is, "%lf", &coordy),"erreur en ouverture de fichier");
  2665. a2ri_erreur_critique_si(!fscanf (is, "%lf", &coordz),"erreur en ouverture de fichier");
  2666. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2667. //mis à jour de la bounding box
  2668. if (i == 0)
  2669. {
  2670. m->xmin = coordx;
  2671. m->xmax = coordx;
  2672. m->ymin = coordy;
  2673. m->ymax = coordy;
  2674. m->zmin = coordz;
  2675. m->zmax = coordz;
  2676. }
  2677. else
  2678. {
  2679. if (coordx < m->xmin)
  2680. m->xmin = coordx;
  2681. if (coordx > m->xmax)
  2682. m->xmax = coordx;
  2683. if (coordy < m->ymin)
  2684. m->ymin = coordy;
  2685. if (coordy > m->ymax)
  2686. m->ymax = coordy;
  2687. if (coordz < m->zmin)
  2688. m->zmin = coordz;
  2689. if (coordz > m->zmax)
  2690. m->zmax = coordz;
  2691. }
  2692. }
  2693. //lecture des aretes
  2694. for (int i = 0; i < nbedge; i++)
  2695. {
  2696. a2ri_erreur_critique_si(!fscanf (is, "%d", &ve1),"erreur en ouverture de fichier");
  2697. a2ri_erreur_critique_si(!fscanf (is, "%d", &ve2),"erreur en ouverture de fichier");
  2698. ve1--;
  2699. ve2--;
  2700. a2ri_vef_add_edge (m, ve1, ve2, 0);
  2701. }
  2702. //lecture des faces
  2703. for (int i = 0; i < nbface; i++)
  2704. {
  2705. a2ri_erreur_critique_si(!fscanf (is, "%d", &ed1),"erreur en ouverture de fichier");
  2706. a2ri_erreur_critique_si(!fscanf (is, "%d", &ed2),"erreur en ouverture de fichier");
  2707. a2ri_erreur_critique_si(!fscanf (is, "%d", &ed3),"erreur en ouverture de fichier");
  2708. ed1--;
  2709. ed2--;
  2710. ed3--;
  2711. a2ri_vef_add_face (m, ed1, ed2, ed3);
  2712. }
  2713. fclose (is);
  2714. setlocale(LC_NUMERIC,"");
  2715. return 1;
  2716. }
  2717. /**
  2718. Ouverture d'un fichier ply contenant un modèle
  2719. @param file chemin du fichier à ouvrir
  2720. @param m pointeur sur le modèle
  2721. @return 1 si succès, 0 sinon
  2722. */
  2723. int
  2724. a2ri_vef_load_ply (
  2725. const char * const file,
  2726. vef_model * m)
  2727. {
  2728. int prop = 0,
  2729. nbpropvertex = 0,
  2730. nbpropface = 0,
  2731. nbvertex,
  2732. nbface,
  2733. ve1,
  2734. ve2,
  2735. ve3;
  2736. char str[20];
  2737. double coordx,
  2738. coordy,
  2739. coordz;
  2740. FILE *is = NULL;
  2741. setlocale(LC_NUMERIC,"C");
  2742. is = fopen (file, "r");
  2743. if (is == NULL)
  2744. return 0;
  2745. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2746. if (strcmp (str, "ply"))
  2747. return 0;
  2748. while (strcmp (str, "end_header"))
  2749. {
  2750. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2751. if (prop == 1 && !strcmp (str, "property"))
  2752. nbpropvertex++;
  2753. if (prop == 2 && !strcmp (str, "property"))
  2754. nbpropface++;
  2755. if (!strcmp (str, "element"))
  2756. {
  2757. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2758. //lecture du nombre de sommets
  2759. if (!strcmp (str, "vertex"))
  2760. {
  2761. prop = 1;
  2762. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbvertex),"erreur en ouverture de fichier");
  2763. }
  2764. //lecture du nombre de faces
  2765. else
  2766. {
  2767. prop = 1;
  2768. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbface),"erreur en ouverture de fichier");
  2769. }
  2770. }
  2771. }
  2772. //lecture des sommets
  2773. for (int i = 0; i < nbvertex; i++)
  2774. {
  2775. a2ri_erreur_critique_si(!fscanf (is, "%lf %lf %lf", &coordx, &coordy, &coordz),"erreur en ouverture de fichier");
  2776. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2777. //mis à jour de la bounding box
  2778. if (i == 0)
  2779. {
  2780. m->xmin = coordx;
  2781. m->xmax = coordx;
  2782. m->ymin = coordy;
  2783. m->ymax = coordy;
  2784. m->zmin = coordz;
  2785. m->zmax = coordz;
  2786. }
  2787. else
  2788. {
  2789. if (coordx < m->xmin)
  2790. m->xmin = coordx;
  2791. if (coordx > m->xmax)
  2792. m->xmax = coordx;
  2793. if (coordy < m->ymin)
  2794. m->ymin = coordy;
  2795. if (coordy > m->ymax)
  2796. m->ymax = coordy;
  2797. if (coordz < m->zmin)
  2798. m->zmin = coordz;
  2799. if (coordz > m->zmax)
  2800. m->zmax = coordz;
  2801. }
  2802. for (int j = 0; j < nbpropvertex - 4; j++)
  2803. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2804. }
  2805. //lecture des faces
  2806. for (int i = 0; i < nbface; i++)
  2807. {
  2808. a2ri_erreur_critique_si(!fscanf (is, "%d", &ve1),"erreur en ouverture de fichier");
  2809. //lecture des trois sommets composant la face
  2810. a2ri_erreur_critique_si(!fscanf (is, "%d %d %d", &ve1, &ve2, &ve3),"erreur en ouverture de fichier");
  2811. //ajout des aretes avec vérification au préalable de leur existence
  2812. a2ri_vef_add_edge (m, ve1, ve2, 1);
  2813. a2ri_vef_add_edge (m, ve2, ve3, 1);
  2814. a2ri_vef_add_edge (m, ve3, ve1, 1);
  2815. //recherche des aretes
  2816. int ed1 = a2ri_vef_search_edge (m, ve1, ve2);
  2817. int ed2 = a2ri_vef_search_edge (m, ve2, ve3);
  2818. int ed3 = a2ri_vef_search_edge (m, ve3, ve1);
  2819. //ajout de la faces avec les trois aretes
  2820. a2ri_vef_add_face (m, ed1, ed2, ed3);
  2821. }
  2822. fclose (is);
  2823. setlocale(LC_NUMERIC,"");
  2824. return 1;
  2825. }
  2826. /**
  2827. Ouverture d'un fichier off contenant un modèle
  2828. @param file chemin du fichier à ouvrir
  2829. @param m pointeur sur le modèle
  2830. @return 1 si succès, 0 sinon
  2831. */
  2832. int
  2833. a2ri_vef_load_off (
  2834. const char * const file,
  2835. vef_model * m)
  2836. {
  2837. FILE *is = NULL;
  2838. char str[20];
  2839. double coordx,
  2840. coordy,
  2841. coordz;
  2842. int ar1,
  2843. ar2,
  2844. ar3,
  2845. nbvertex,
  2846. nbface;
  2847. setlocale(LC_NUMERIC,"C");
  2848. is = fopen (file, "r");
  2849. if (is == NULL)
  2850. return 0;
  2851. a2ri_vef_init (m);
  2852. a2ri_erreur_critique_si(!fscanf (is, "%s", str),"erreur en ouverture de fichier");
  2853. if (strcmp (str, "OFF"))
  2854. return 0;
  2855. //lecture du nombre de sommets
  2856. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbvertex),"erreur en ouverture de fichier");
  2857. //lecture du nombre de faces
  2858. a2ri_erreur_critique_si(!fscanf (is, "%d", &nbface),"erreur en ouverture de fichier");
  2859. a2ri_erreur_critique_si(!fscanf (is, "%d", &ar1),"erreur en ouverture de fichier");
  2860. //lecture des sommets
  2861. for (int i = 0; i < nbvertex; i++)
  2862. {
  2863. a2ri_erreur_critique_si(!fscanf (is, "%lf", &coordx),"erreur en ouverture de fichier");
  2864. a2ri_erreur_critique_si(!fscanf (is, "%lf", &coordy),"erreur en ouverture de fichier");
  2865. a2ri_erreur_critique_si(!fscanf (is, "%lf", &coordz),"erreur en ouverture de fichier");
  2866. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2867. //mis à jour de la bounding box
  2868. if (i == 0)
  2869. {
  2870. m->xmin = coordx;
  2871. m->xmax = coordx;
  2872. m->ymin = coordy;
  2873. m->ymax = coordy;
  2874. m->zmin = coordz;
  2875. m->zmax = coordz;
  2876. }
  2877. else
  2878. {
  2879. if (coordx < m->xmin)
  2880. m->xmin = coordx;
  2881. if (coordx > m->xmax)
  2882. m->xmax = coordx;
  2883. if (coordy < m->ymin)
  2884. m->ymin = coordy;
  2885. if (coordy > m->ymax)
  2886. m->ymax = coordy;
  2887. if (coordz < m->zmin)
  2888. m->zmin = coordz;
  2889. if (coordz > m->zmax)
  2890. m->zmax = coordz;
  2891. }
  2892. }
  2893. //lecture des faces
  2894. for (int i = 0; i < nbface; i++)
  2895. {
  2896. //lecture des trois sommets composant la face
  2897. a2ri_erreur_critique_si(!fscanf (is, "%d", &ar1),"erreur en ouverture de fichier");
  2898. a2ri_erreur_critique_si(!fscanf (is, "%d", &ar1),"erreur en ouverture de fichier");
  2899. a2ri_erreur_critique_si(!fscanf (is, "%d", &ar2),"erreur en ouverture de fichier");
  2900. a2ri_erreur_critique_si(!fscanf (is, "%d", &ar3),"erreur en ouverture de fichier");
  2901. //ajout des aretes avec vérification au préalable de leur existence
  2902. a2ri_vef_add_edge (m, ar1, ar2, 1);
  2903. a2ri_vef_add_edge (m, ar2, ar3, 1);
  2904. a2ri_vef_add_edge (m, ar3, ar1, 1);
  2905. //recherche des aretes
  2906. int ed1 = a2ri_vef_search_edge (m, ar1, ar2);
  2907. int ed2 = a2ri_vef_search_edge (m, ar2, ar3);
  2908. int ed3 = a2ri_vef_search_edge (m, ar3, ar1);
  2909. //ajout de la faces avec les trois aretes
  2910. a2ri_vef_add_face (m, ed1, ed2, ed3);
  2911. }
  2912. fclose (is);
  2913. setlocale(LC_NUMERIC,"");
  2914. return 1;
  2915. }
  2916. /**
  2917. Enregistrement d'un modèle dans un format défini par l'extension du nom de fichier
  2918. @param file nom du fichier
  2919. @param m le modèle
  2920. @return aucun
  2921. */
  2922. int
  2923. a2ri_vef_save_file (
  2924. const char * const file,
  2925. const vef_model * const m)
  2926. {
  2927. char ext[4];
  2928. memcpy (ext, file + (strlen (file) - 3), 3);
  2929. ext[3] = 0;
  2930. //appel de la fonction d'enregistrement en fonction du type de l'extension du fichier
  2931. if (!strcmp (ext, "wrl"))
  2932. return a2ri_vef_save_wrl (file, m);
  2933. if (!strcmp (ext, "vef"))
  2934. return a2ri_vef_save_vef (file, m);
  2935. if (!strcmp (ext, "pgn"))
  2936. return a2ri_vef_save_pgn (file, m);
  2937. if (!strcmp (ext, "ply"))
  2938. return a2ri_vef_save_ply (file, m);
  2939. if (!strcmp (ext, "off"))
  2940. return a2ri_vef_save_off (file, m);
  2941. if (!strcmp (ext, "obj"))
  2942. return a2ri_vef_save_obj (file, m);
  2943. if (!strcmp (ext, "gts"))
  2944. return a2ri_vef_save_gts (file, m);
  2945. return 0;
  2946. }
  2947. /**
  2948. Enregistrement d'un modèle dans un fichier wrl
  2949. @param file chemin du fichier
  2950. @param m modèle à sauvegarder
  2951. @return 1 si succès, 0 sinon
  2952. */
  2953. int
  2954. a2ri_vef_save_wrl (
  2955. const char * const file,
  2956. const vef_model * const m)
  2957. {
  2958. setlocale(LC_NUMERIC,"C");
  2959. FILE *os = NULL;
  2960. os = fopen (file, "w");
  2961. if (os == NULL)
  2962. return 0;
  2963. //ecriture de l'en-tete
  2964. fprintf (os, "#VRML V2.0 utf8\n");
  2965. fprintf (os, "Shape{\n");
  2966. fprintf (os, "appearance Appearance {\n");
  2967. fprintf (os, "material Material {\n");
  2968. fprintf (os, "}\n");
  2969. fprintf (os, "}\n");
  2970. fprintf (os, "geometry IndexedFaceSet{\n");
  2971. fprintf (os, "coord Coordinate{\n");
  2972. fprintf (os, "point[\n");
  2973. //ecriture des sommets
  2974. for (int i = 0; i < m->nbvertex; i++)
  2975. fprintf (os, "%f %f %f,\n", m->ve[i].x, m->ve[i].y, m->ve[i].z);
  2976. fprintf (os, "]\n");
  2977. fprintf (os, "}\n");
  2978. //ecriture des faces
  2979. fprintf (os, "coordIndex[\n");
  2980. for (int i = 0; i < m->nbface; i++)
  2981. {
  2982. int nb1,
  2983. nb2,
  2984. nb3;
  2985. vef_face_get_vertices (&(m->fa[i]), m->ed, &nb1, &nb2, &nb3);
  2986. fprintf (os, "%d %d %d -1\n", nb1, nb2, nb3);
  2987. }
  2988. fprintf (os, "]\n");
  2989. fprintf (os, "}\n");
  2990. fprintf (os, "}\n");
  2991. fclose (os);
  2992. setlocale(LC_NUMERIC,"");
  2993. return 1;
  2994. }
  2995. /**
  2996. Enregistrement d'un modèle dans un fichier pgn
  2997. @param file chemin du fichier
  2998. @param m modèle à sauvegarder
  2999. @return 1 si succès, 0 sinon
  3000. */
  3001. int
  3002. a2ri_vef_save_pgn (
  3003. const char * const file,
  3004. const vef_model * const m)
  3005. {
  3006. setlocale(LC_NUMERIC,"C");
  3007. FILE *os = NULL;
  3008. os = fopen (file, "w");
  3009. if (os == NULL)
  3010. return 0;
  3011. //ecriture du nombre de sommets et faces
  3012. fprintf (os, "%d %d \n\n", m->nbvertex, m->nbface);
  3013. //ecriture des sommets
  3014. for (int i = 0; i < m->nbvertex; i++)
  3015. fprintf (os, "%f %f %f\n", m->ve[i].x, m->ve[i].y, m->ve[i].z);
  3016. //ecriture des faces
  3017. for (int i = 0; i < m->nbface; i++)
  3018. {
  3019. int ve1,
  3020. ve2,
  3021. ve3;
  3022. vef_face_get_vertices (&(m->fa[i]), m->ed, &ve1, &ve2, &ve3);
  3023. fprintf (os, "%d %d %d -1\n", ve1, ve2, ve3);
  3024. }
  3025. fclose (os);
  3026. setlocale(LC_NUMERIC,"");
  3027. return 1;
  3028. }
  3029. /**
  3030. Enregistrement d'un modèle dans un fichier vef
  3031. @param file chemin du fichier
  3032. @param m modèle à sauvegarder
  3033. @return 1 si succès, 0 sinon
  3034. */
  3035. int
  3036. a2ri_vef_save_vef (
  3037. const char * const file,
  3038. const vef_model * const m)
  3039. {
  3040. setlocale(LC_NUMERIC,"C");
  3041. FILE *os = NULL;
  3042. os = fopen (file, "w");
  3043. if (os == NULL)
  3044. return 0;
  3045. //ecriture des nombre de sommets, aretes et faces
  3046. fprintf (os, "%d %d %d\n\n", m->nbvertex, m->nbedge, m->nbface);
  3047. //ecriture des sommets
  3048. for (int i = 0; i < m->nbvertex; i++)
  3049. fprintf (os, "%f %f %f\n", m->ve[i].x, m->ve[i].y, m->ve[i].z);
  3050. //ecriture des aretes
  3051. for (int i = 0; i < m->nbedge; i++)
  3052. fprintf (os, "%d %d\n", m->ed[i].ve1, m->ed[i].ve2);
  3053. //ecriture des faces
  3054. for (int i = 0; i < m->nbface; i++)
  3055. fprintf (os, "%d %d %d\n", m->fa[i].ed1, m->fa[i].ed2, m->fa[i].ed3);
  3056. fclose (os);
  3057. setlocale(LC_NUMERIC,"");
  3058. return 1;
  3059. }
  3060. /**
  3061. Enregistrement d'un modèle dans un fichier gts
  3062. @param file chemin du fichier
  3063. @param m modèle à sauvegarder
  3064. @return 1 si succès, 0 sinon
  3065. */
  3066. int
  3067. a2ri_vef_save_gts (
  3068. const char * const file,
  3069. const vef_model * const m)
  3070. {
  3071. setlocale(LC_NUMERIC,"C");
  3072. FILE *os = NULL;
  3073. os = fopen (file, "w");
  3074. if (os == NULL)
  3075. return 0;
  3076. //ecriture des nombre de sommets, aretes et faces
  3077. fprintf (os, "%d %d %d\n\n", m->nbvertex, m->nbedge, m->nbface);
  3078. //ecriture des sommets
  3079. for (int i = 0; i < m->nbvertex; i++)
  3080. fprintf (os, "%f %f %f\n", m->ve[i].x, m->ve[i].y, m->ve[i].z);
  3081. //ecriture des aretes
  3082. for (int i = 0; i < m->nbedge; i++)
  3083. fprintf (os, "%d %d\n", m->ed[i].ve1 + 1, m->ed[i].ve2 + 1);
  3084. //ecriture des faces
  3085. for (int i = 0; i < m->nbface; i++)
  3086. fprintf (os, "%d %d %d\n", m->fa[i].ed1 + 1, m->fa[i].ed2 + 1,
  3087. m->fa[i].ed3 + 1);
  3088. fclose (os);
  3089. setlocale(LC_NUMERIC,"");
  3090. return 1;
  3091. }
  3092. /**
  3093. Enregistrement d'un modèle dans un fichier off
  3094. @param file chemin du fichier
  3095. @param m modèle à sauvegarder
  3096. @return 1 si succès, 0 sinon
  3097. */
  3098. int
  3099. a2ri_vef_save_off (
  3100. const char * const file,
  3101. const vef_model * const m)
  3102. {
  3103. setlocale(LC_NUMERIC,"C");
  3104. FILE *os = NULL;
  3105. os = fopen (file, "w");
  3106. if (os == NULL)
  3107. return 0;
  3108. //ecriture de l'en-tete et des nombres de sommets et faces
  3109. fprintf (os, "OFF\n%d %d 0\n\n", m->nbvertex, m->nbface);
  3110. //ecriture des sommets
  3111. for (int i = 0; i < m->nbvertex; i++)
  3112. fprintf (os, "%f %f %f\n", m->ve[i].x, m->ve[i].y, m->ve[i].z);
  3113. //ecriture des faces
  3114. for (int i = 0; i < m->nbface; i++)
  3115. {
  3116. int ve1,
  3117. ve2,
  3118. ve3;
  3119. vef_face_get_vertices (&(m->fa[i]), m->ed, &ve1, &ve2, &ve3);
  3120. fprintf (os, "3 %d %d %d \n", ve1, ve2, ve3);
  3121. }
  3122. fclose (os);
  3123. setlocale(LC_NUMERIC,"");
  3124. return 1;
  3125. }
  3126. /**
  3127. Enregistrement d'un modèle dans un fichier ply
  3128. @param file chemin du fichier
  3129. @param m modèle à sauvegarder
  3130. @return 1 si succès, 0 sinon
  3131. */
  3132. int
  3133. a2ri_vef_save_ply (
  3134. const char * const file,
  3135. const vef_model * const m)
  3136. {
  3137. setlocale(LC_NUMERIC,"C");
  3138. FILE *os = NULL;
  3139. os = fopen (file, "w");
  3140. if (os == NULL)
  3141. return 0;
  3142. //ecriture de l'en-tete avec le nombre de sommets et de faces
  3143. fprintf (os, "ply\nformat ascii 1.0\ncomment zipper output\n");
  3144. fprintf (os,
  3145. "element vertex %d\nproperty float x\nproperty float y\nproperty float z\nelement face %d\n",
  3146. m->nbvertex, m->nbface);
  3147. fprintf (os, "property list uchar int vertex_indices\nend_header\n");
  3148. //ecriture des sommets
  3149. for (int i = 0; i < m->nbvertex; i++)
  3150. fprintf (os, "%f %f %f\n", m->ve[i].x, m->ve[i].y, m->ve[i].z);
  3151. //ecriture des faces
  3152. for (int i = 0; i < m->nbface; i++)
  3153. {
  3154. int ve1,
  3155. ve2,
  3156. ve3;
  3157. vef_face_get_vertices (&(m->fa[i]), m->ed, &ve1, &ve2, &ve3);
  3158. fprintf (os, "3 %d %d %d \n", ve1, ve2, ve3);
  3159. }
  3160. fclose (os);
  3161. setlocale(LC_NUMERIC,"");
  3162. return 1;
  3163. }
  3164. /**
  3165. Enregistrement d'un modèle dans un fichier obj
  3166. @param file chemin du fichier
  3167. @param m modèle à sauvegarder
  3168. @return 1 si succès, 0 sinon
  3169. */
  3170. int
  3171. a2ri_vef_save_obj (
  3172. const char * const file,
  3173. const vef_model * const m)
  3174. {
  3175. setlocale(LC_NUMERIC,"C");
  3176. FILE *os = NULL;
  3177. os = fopen (file, "w");
  3178. if (os == NULL)
  3179. return 0;
  3180. //ecriture des sommets
  3181. for (int i = 0; i < m->nbvertex; i++)
  3182. fprintf (os, "v %f %f %f\n", m->ve[i].x, m->ve[i].y, m->ve[i].z);
  3183. fprintf (os, "\n");
  3184. //ecriture des faces
  3185. for (int i = 0; i < m->nbface; i++)
  3186. {
  3187. int ve1,
  3188. ve2,
  3189. ve3;
  3190. vef_face_get_vertices (&(m->fa[i]), m->ed, &ve1, &ve2, &ve3);
  3191. fprintf (os, "f %d %d %d\n", ve1 + 1, ve2 + 1, ve3 + 1);
  3192. }
  3193. fclose (os);
  3194. setlocale(LC_NUMERIC,"");
  3195. return 1;
  3196. }