.#io.c.1.16 83 KB

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