.#io.c.1.14 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875
  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_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
  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_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. Ouverture d'un fichier contenant un modèle
  1883. @param file chemin du fichier à ouvrir
  1884. @param m pointeur sur le modèle
  1885. @return 1 si succès, 0 sinon
  1886. */
  1887. int
  1888. a2ri_vef_open_file (
  1889. char *file,
  1890. vef_model * m)
  1891. {
  1892. char ext[5];
  1893. memcpy (ext, file + (strlen (file) - 4), 4);
  1894. ext[4] = 0;
  1895. //appel de la fonction correspondant à l'extension du fichier
  1896. if (!strcmp (ext, ".wrl"))
  1897. return (a2ri_vef_load_wrl (file, m));
  1898. if (!strcmp (ext, ".vef"))
  1899. return (a2ri_vef_load_vef (file, m));
  1900. if (!strcmp (ext, ".pgn"))
  1901. return (a2ri_vef_load_pgn (file, m));
  1902. if (!strcmp (ext, ".ply"))
  1903. return (a2ri_vef_load_ply (file, m));
  1904. if (!strcmp (ext, ".off"))
  1905. return (a2ri_vef_load_off (file, m));
  1906. if (!strcmp (ext, ".obj"))
  1907. return (a2ri_vef_load_obj (file, m));
  1908. if (!strcmp (ext, ".gts"))
  1909. return (a2ri_vef_load_gts (file, m));
  1910. if (!strcmp (ext, ".stl"))
  1911. return (a2ri_vef_load_stl (file, m));
  1912. return 0;
  1913. }
  1914. /**
  1915. Ouverture d'un fichier pgn contenant un modèle
  1916. @param file chemin du fichier à ouvrir
  1917. @param m pointeur sur le modèle
  1918. @return 1 si succès, 0 sinon
  1919. */
  1920. int
  1921. a2ri_vef_load_pgn (
  1922. char *file,
  1923. vef_model * m)
  1924. {
  1925. FILE *is = NULL;
  1926. double coordx,
  1927. coordy,
  1928. coordz;
  1929. int classif,
  1930. ar1,
  1931. ar2,
  1932. ar3,
  1933. nbvertex,
  1934. nbface;
  1935. setlocale(LC_NUMERIC,"C");
  1936. is = fopen (file, "r");
  1937. if (is == NULL)
  1938. return 0;
  1939. a2ri_vef_init (m);
  1940. //lecture du nombre de sommets
  1941. fscanf (is, "%d", &nbvertex);
  1942. //lecture du nombre de faces
  1943. fscanf (is, "%d", &nbface);
  1944. //lecture des sommets
  1945. for (int i = 0; i < nbvertex; i++)
  1946. {
  1947. fscanf (is, "%lf", &coordx);
  1948. fscanf (is, "%lf", &coordy);
  1949. fscanf (is, "%lf", &coordz);
  1950. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  1951. //mis à jour de la bounding box
  1952. if (i == 0)
  1953. {
  1954. m->xmin = coordx;
  1955. m->xmax = coordx;
  1956. m->ymin = coordy;
  1957. m->ymax = coordy;
  1958. m->zmin = coordz;
  1959. m->zmax = coordz;
  1960. }
  1961. else
  1962. {
  1963. if (coordx < m->xmin)
  1964. m->xmin = coordx;
  1965. if (coordx > m->xmax)
  1966. m->xmax = coordx;
  1967. if (coordy < m->ymin)
  1968. m->ymin = coordy;
  1969. if (coordy > m->ymax)
  1970. m->ymax = coordy;
  1971. if (coordz < m->zmin)
  1972. m->zmin = coordz;
  1973. if (coordz > m->zmax)
  1974. m->zmax = coordz;
  1975. }
  1976. }
  1977. //lecture des faces
  1978. for (int i = 0; i < nbface; i++)
  1979. {
  1980. //lecture des trois sommets composant la face
  1981. fscanf (is, "%d", &ar1);
  1982. fscanf (is, "%d", &ar2);
  1983. fscanf (is, "%d", &ar3);
  1984. //ajout des aretes avec vérification au préalable de leur existence
  1985. a2ri_vef_add_edge (m, ar1, ar2, 1);
  1986. a2ri_vef_add_edge (m, ar2, ar3, 1);
  1987. a2ri_vef_add_edge (m, ar3, ar1, 1);
  1988. //recherche des aretes
  1989. int ed1 = a2ri_vef_search_edge (*m, ar1, ar2);
  1990. int ed2 = a2ri_vef_search_edge (*m, ar2, ar3);
  1991. int ed3 = a2ri_vef_search_edge (*m, ar3, ar1);
  1992. //ajout de la faces avec les trois aretes
  1993. a2ri_vef_add_face (m, ed1, ed2, ed3);
  1994. // reading the -1
  1995. fscanf (is, "%d", &ar1);
  1996. }
  1997. //lecture des classifications si disponible
  1998. for (int i = 0; i < nbface; i++)
  1999. {
  2000. if (!feof (is))
  2001. {
  2002. fscanf (is, "%d", &classif);
  2003. m->fa[i].type = classif;
  2004. }
  2005. }
  2006. fclose (is);
  2007. setlocale(LC_NUMERIC,"");
  2008. return 1;
  2009. }
  2010. /**
  2011. Ouverture d'un fichier stl binaire contenant un modèle
  2012. @param file chemin du fichier à ouvrir
  2013. @param m pointeur sur le modèle
  2014. @return 1 si succès, 0 sinon
  2015. */
  2016. int
  2017. a2ri_vef_load_binary_stl (
  2018. char *file,
  2019. vef_model * m)
  2020. {
  2021. FILE *is = NULL;
  2022. char str[80];
  2023. unsigned int nbface;
  2024. unsigned char finligne;
  2025. float coordx,
  2026. coordy,
  2027. coordz;
  2028. int ve1,
  2029. ve2,
  2030. ve3,
  2031. ed1,
  2032. ed2,
  2033. ed3;
  2034. setlocale(LC_NUMERIC,"C");
  2035. is = fopen (file, "rb");
  2036. if (is == NULL)
  2037. return 0;
  2038. fread (&str, sizeof (char), 80, is);
  2039. fread (&nbface, sizeof (unsigned int), 1, is);
  2040. for (unsigned int i = 0; i < nbface; i++)
  2041. {
  2042. fread (&coordx, sizeof (float), 1, is);
  2043. fread (&coordy, sizeof (float), 1, is);
  2044. fread (&coordz, sizeof (float), 1, is); //read the normal
  2045. fread (&coordx, sizeof (float), 1, is);
  2046. fread (&coordy, sizeof (float), 1, is);
  2047. fread (&coordz, sizeof (float), 1, is); //read the first vertex
  2048. ve1 = a2ri_vef_search_vertex (*m, coordx, coordy, coordz);
  2049. if (ve1 == -1)
  2050. {
  2051. ve1 = m->nbvertex;
  2052. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2053. }
  2054. if (i == 0)
  2055. {
  2056. m->xmin = coordx;
  2057. m->xmax = coordx;
  2058. m->ymin = coordy;
  2059. m->ymax = coordy;
  2060. m->zmin = coordz;
  2061. m->zmax = coordz;
  2062. }
  2063. else
  2064. {
  2065. if (coordx < m->xmin)
  2066. m->xmin = coordx;
  2067. if (coordx > m->xmax)
  2068. m->xmax = coordx;
  2069. if (coordy < m->ymin)
  2070. m->ymin = coordy;
  2071. if (coordy > m->ymax)
  2072. m->ymax = coordy;
  2073. if (coordz < m->zmin)
  2074. m->zmin = coordz;
  2075. if (coordz > m->zmax)
  2076. m->zmax = coordz;
  2077. }
  2078. fread (&coordx, sizeof (float), 1, is);
  2079. fread (&coordy, sizeof (float), 1, is);
  2080. fread (&coordz, sizeof (float), 1, is); //read the second vertex
  2081. ve2 = a2ri_vef_search_vertex (*m, coordx, coordy, coordz);
  2082. if (ve2 == -1)
  2083. {
  2084. ve2 = m->nbvertex;
  2085. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2086. }
  2087. if (coordx < m->xmin)
  2088. m->xmin = coordx;
  2089. if (coordx > m->xmax)
  2090. m->xmax = coordx;
  2091. if (coordy < m->ymin)
  2092. m->ymin = coordy;
  2093. if (coordy > m->ymax)
  2094. m->ymax = coordy;
  2095. if (coordz < m->zmin)
  2096. m->zmin = coordz;
  2097. if (coordz > m->zmax)
  2098. m->zmax = coordz;
  2099. fread (&coordx, sizeof (float), 1, is);
  2100. fread (&coordy, sizeof (float), 1, is);
  2101. fread (&coordz, sizeof (float), 1, is); //read the third vertex
  2102. ve3 = a2ri_vef_search_vertex (*m, coordx, coordy, coordz);
  2103. if (ve3 == -1)
  2104. {
  2105. ve3 = m->nbvertex;
  2106. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2107. }
  2108. if (coordx < m->xmin)
  2109. m->xmin = coordx;
  2110. if (coordx > m->xmax)
  2111. m->xmax = coordx;
  2112. if (coordy < m->ymin)
  2113. m->ymin = coordy;
  2114. if (coordy > m->ymax)
  2115. m->ymax = coordy;
  2116. if (coordz < m->zmin)
  2117. m->zmin = coordz;
  2118. if (coordz > m->zmax)
  2119. m->zmax = coordz;
  2120. ed1 = a2ri_vef_search_edge (*m, ve1, ve2);
  2121. ed2 = a2ri_vef_search_edge (*m, ve1, ve3);
  2122. ed3 = a2ri_vef_search_edge (*m, ve2, ve3);
  2123. if (ed1 == -1)
  2124. {
  2125. ed1 = m->nbedge;
  2126. a2ri_vef_add_edge (m, ve1, ve2, 0);
  2127. }
  2128. if (ed2 == -1)
  2129. {
  2130. ed2 = m->nbedge;
  2131. a2ri_vef_add_edge (m, ve1, ve3, 0);
  2132. }
  2133. if (ed3 == -1)
  2134. {
  2135. ed3 = m->nbedge;
  2136. a2ri_vef_add_edge (m, ve2, ve3, 0);
  2137. }
  2138. a2ri_vef_add_face (m, ed2, ed1, ed3);
  2139. fread (&finligne, 1, 1, is);
  2140. fread (&finligne, 1, 1, is);
  2141. }
  2142. fclose (is);
  2143. setlocale(LC_NUMERIC,"");
  2144. return 1;
  2145. }
  2146. /**
  2147. Ouverture d'un fichier stl ascii contenant un modèle
  2148. @param file chemin du fichier à ouvrir
  2149. @param m pointeur sur le modèle
  2150. @return 1 si succès, 0 sinon
  2151. */
  2152. int
  2153. a2ri_vef_load_ascii_stl (
  2154. char *file,
  2155. vef_model * m)
  2156. {
  2157. FILE *is = NULL;
  2158. int ve1,
  2159. ve2,
  2160. ve3,
  2161. i = 0,
  2162. fin = 0,
  2163. ed1,
  2164. ed2,
  2165. ed3;
  2166. double coordx,
  2167. coordy,
  2168. coordz;
  2169. char str[250];
  2170. setlocale(LC_NUMERIC,"C");
  2171. is = fopen (file, "r");
  2172. if (is == NULL)
  2173. return 0;
  2174. fscanf (is, "%s", str);
  2175. while (strcmp (str, "facet"))
  2176. fscanf (is, "%s", str);
  2177. while (!fin)
  2178. {
  2179. fscanf (is, "%s", str); //read normal
  2180. fscanf (is, "%s", str);
  2181. fscanf (is, "%s", str);
  2182. fscanf (is, "%s", str); //read the 3 values of the normal
  2183. fscanf (is, "%s", str);
  2184. fscanf (is, "%s", str); //outer loop
  2185. fscanf (is, "%s", str); //read vertex
  2186. fscanf (is, "%s", str);
  2187. coordx = atof (str); //coordx
  2188. fscanf (is, "%s", str);
  2189. coordy = atof (str); //coordy
  2190. fscanf (is, "%s", str);
  2191. coordz = atof (str); //coordz
  2192. ve1 = a2ri_vef_search_vertex (*m, coordx, coordy, coordz);
  2193. if (ve1 == -1)
  2194. {
  2195. ve1 = m->nbvertex;
  2196. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2197. }
  2198. if (i == 0)
  2199. {
  2200. m->xmin = coordx;
  2201. m->xmax = coordx;
  2202. m->ymin = coordy;
  2203. m->ymax = coordy;
  2204. m->zmin = coordz;
  2205. m->zmax = coordz;
  2206. i++;
  2207. }
  2208. else
  2209. {
  2210. if (coordx < m->xmin)
  2211. m->xmin = coordx;
  2212. if (coordx > m->xmax)
  2213. m->xmax = coordx;
  2214. if (coordy < m->ymin)
  2215. m->ymin = coordy;
  2216. if (coordy > m->ymax)
  2217. m->ymax = coordy;
  2218. if (coordz < m->zmin)
  2219. m->zmin = coordz;
  2220. if (coordz > m->zmax)
  2221. m->zmax = coordz;
  2222. }
  2223. fscanf (is, "%s", str); //read vertex
  2224. fscanf (is, "%s", str);
  2225. coordx = atof (str); //coordx
  2226. fscanf (is, "%s", str);
  2227. coordy = atof (str); //coordy
  2228. fscanf (is, "%s", str);
  2229. coordz = atof (str); //coordz
  2230. ve2 = a2ri_vef_search_vertex (*m, coordx, coordy, coordz);
  2231. if (ve2 == -1)
  2232. {
  2233. ve2 = m->nbvertex;
  2234. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2235. }
  2236. if (coordx < m->xmin)
  2237. m->xmin = coordx;
  2238. if (coordx > m->xmax)
  2239. m->xmax = coordx;
  2240. if (coordy < m->ymin)
  2241. m->ymin = coordy;
  2242. if (coordy > m->ymax)
  2243. m->ymax = coordy;
  2244. if (coordz < m->zmin)
  2245. m->zmin = coordz;
  2246. if (coordz > m->zmax)
  2247. m->zmax = coordz;
  2248. fscanf (is, "%s", str); //read vertex
  2249. fscanf (is, "%s", str);
  2250. coordx = atof (str); //coordx
  2251. fscanf (is, "%s", str);
  2252. coordy = atof (str); //coordy
  2253. fscanf (is, "%s", str);
  2254. coordz = atof (str); //coordz;
  2255. ve3 = a2ri_vef_search_vertex (*m, coordx, coordy, coordz);
  2256. if (ve3 == -1)
  2257. {
  2258. ve3 = m->nbvertex;
  2259. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2260. }
  2261. if (coordx < m->xmin)
  2262. m->xmin = coordx;
  2263. if (coordx > m->xmax)
  2264. m->xmax = coordx;
  2265. if (coordy < m->ymin)
  2266. m->ymin = coordy;
  2267. if (coordy > m->ymax)
  2268. m->ymax = coordy;
  2269. if (coordz < m->zmin)
  2270. m->zmin = coordz;
  2271. if (coordz > m->zmax)
  2272. m->zmax = coordz;
  2273. ed1 = a2ri_vef_search_edge (*m, ve1, ve2);
  2274. ed2 = a2ri_vef_search_edge (*m, ve1, ve3);
  2275. ed3 = a2ri_vef_search_edge (*m, ve2, ve3);
  2276. if (ed1 == -1)
  2277. {
  2278. ed1 = m->nbedge;
  2279. a2ri_vef_add_edge (m, ve1, ve2, 0);
  2280. }
  2281. if (ed2 == -1)
  2282. {
  2283. ed2 = m->nbedge;
  2284. a2ri_vef_add_edge (m, ve1, ve3, 0);
  2285. }
  2286. if (ed3 == -1)
  2287. {
  2288. ed3 = m->nbedge;
  2289. a2ri_vef_add_edge (m, ve2, ve3, 0);
  2290. }
  2291. a2ri_vef_add_face (m, ed2, ed1, ed3);
  2292. fscanf (is, "%s", str); //read endloop
  2293. fscanf (is, "%s", str); //read endfacet
  2294. fscanf (is, "%s", str); //read facet ou endsolid
  2295. fin = !strcmp (str, "endsolid");
  2296. }
  2297. fclose (is);
  2298. setlocale(LC_NUMERIC,"");
  2299. return 1;
  2300. }
  2301. /**
  2302. Ouverture d'un fichier stl contenant un modèle
  2303. @param file chemin du fichier à ouvrir
  2304. @param m pointeur sur le modèle
  2305. @return 1 si succès, 0 sinon
  2306. */
  2307. int
  2308. a2ri_vef_load_stl (
  2309. char *file,
  2310. vef_model * m)
  2311. {
  2312. FILE *is = NULL;
  2313. char str[250];
  2314. is = fopen (file, "r");
  2315. if (is == NULL)
  2316. return 0;
  2317. fscanf (is, "%s", str);
  2318. fclose (is);
  2319. if (!strcmp (str, "solid"))
  2320. return a2ri_vef_load_ascii_stl (file, m);
  2321. else
  2322. return a2ri_vef_load_binary_stl (file, m);
  2323. }
  2324. /**
  2325. Ouverture d'un fichier obj contenant un modèle
  2326. @param file chemin du fichier à ouvrir
  2327. @param m pointeur sur le modèle
  2328. @return 1 si succès, 0 sinon
  2329. */
  2330. int
  2331. a2ri_vef_load_obj (
  2332. char *file,
  2333. vef_model * m)
  2334. {
  2335. FILE *is = NULL;
  2336. int ve1,
  2337. ve2,
  2338. ve3;
  2339. double coordx,
  2340. coordy,
  2341. coordz;
  2342. char str[250];
  2343. setlocale(LC_NUMERIC,"C");
  2344. is = fopen (file, "r");
  2345. if (is == NULL)
  2346. return 0;
  2347. fscanf (is, "%s", str);
  2348. while (strcmp (str, "v") != 0)
  2349. fscanf (is, "%s", str);
  2350. //lecture des sommets
  2351. while (strcmp (str, "g") != 0)
  2352. {
  2353. fscanf (is, "%lf %lf %lf", &coordx, &coordy, &coordz);
  2354. //ajout d'un sommet
  2355. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2356. fscanf (is, "%s", str);
  2357. //mis à jour de la bounding box
  2358. if (m->nbvertex == 1)
  2359. {
  2360. m->xmin = coordx;
  2361. m->xmax = coordx;
  2362. m->ymin = coordy;
  2363. m->ymax = coordy;
  2364. m->zmin = coordz;
  2365. m->zmax = coordz;
  2366. }
  2367. else
  2368. {
  2369. if (coordx < m->xmin)
  2370. m->xmin = coordx;
  2371. if (coordx > m->xmax)
  2372. m->xmax = coordx;
  2373. if (coordy < m->ymin)
  2374. m->ymin = coordy;
  2375. if (coordy > m->ymax)
  2376. m->ymax = coordy;
  2377. if (coordz < m->zmin)
  2378. m->zmin = coordz;
  2379. if (coordz > m->zmax)
  2380. m->zmax = coordz;
  2381. }
  2382. }
  2383. fscanf (is, "%s", str);
  2384. fscanf (is, "%s", str);
  2385. //lecture des faces
  2386. while (!feof (is))
  2387. {
  2388. //lecture des trois sommets
  2389. fscanf (is, "%d %d %d", &ve1, &ve2, &ve3);
  2390. ve1--;
  2391. ve2--;
  2392. ve3--;
  2393. //ajout des aretes avec vérification au préalable de leur existence
  2394. a2ri_vef_add_edge (m, ve1, ve2, 1);
  2395. a2ri_vef_add_edge (m, ve2, ve3, 1);
  2396. a2ri_vef_add_edge (m, ve3, ve1, 1);
  2397. //recherche des aretes
  2398. int ed1 = a2ri_vef_search_edge (*m, ve1, ve2);
  2399. int ed2 = a2ri_vef_search_edge (*m, ve2, ve3);
  2400. int ed3 = a2ri_vef_search_edge (*m, ve3, ve1);
  2401. //ajout de la faces avec les trois aretes
  2402. a2ri_vef_add_face (m, ed1, ed2, ed3);
  2403. fscanf (is, "%s", str);
  2404. //si la face compte plus de trois sommets, on relit et on ajoute des aretes et une seconde face
  2405. if (strcmp (str, "f") != 0)
  2406. {
  2407. ve2 = atoi (str);
  2408. ve2--;
  2409. a2ri_vef_add_edge (m, ve1, ve2, 1);
  2410. a2ri_vef_add_edge (m, ve2, ve3, 1);
  2411. a2ri_vef_add_edge (m, ve3, ve1, 1);
  2412. int ed1 = a2ri_vef_search_edge (*m, ve1, ve2);
  2413. int ed2 = a2ri_vef_search_edge (*m, ve2, ve3);
  2414. int ed3 = a2ri_vef_search_edge (*m, ve3, ve1);
  2415. a2ri_vef_add_face (m, ed1, ed3, ed2);
  2416. fscanf (is, "%s", str);
  2417. }
  2418. }
  2419. fclose (is);
  2420. setlocale(LC_NUMERIC,"");
  2421. return 1;
  2422. }
  2423. /**
  2424. Ouverture d'un fichier wrl contenant un modèle
  2425. @param file chemin du fichier à ouvrir
  2426. @param m pointeur sur le modèle
  2427. @return 1 si succès, 0 sinon
  2428. */
  2429. int
  2430. a2ri_vef_load_wrl (
  2431. char *file,
  2432. vef_model * m)
  2433. {
  2434. FILE *is = NULL;
  2435. char str[20];
  2436. char coordx[15],
  2437. coordy[15],
  2438. coordz[15];
  2439. setlocale(LC_NUMERIC,"C");
  2440. is = fopen (file, "r");
  2441. if (is == NULL)
  2442. return 0;
  2443. fscanf (is, "%s", str);
  2444. while (strcmp (str, "point") && strcmp (str, "point["))
  2445. fscanf (is, "%s", str);
  2446. if (!strcmp (str, "point"))
  2447. fscanf (is, "%s", str);
  2448. // reading the file until coordx = } or ]
  2449. fscanf (is, "%s", coordx);
  2450. int i = 0;
  2451. //lecture des sommets
  2452. while (strcmp (coordx, "}") && strcmp (coordx, "]"))
  2453. {
  2454. fscanf (is, "%s", coordy);
  2455. fscanf (is, "%s", coordz);
  2456. if (coordz[strlen (coordz) - 1] == ',')
  2457. {
  2458. memcpy (coordz, coordz, strlen (coordz) - 1);
  2459. coordz[strlen (coordz)] = '\0';
  2460. }
  2461. a2ri_vef_add_vertex (m, atof (coordx), atof (coordy), atof (coordz));
  2462. //mis à jour de la bounding box
  2463. if (i == 0)
  2464. {
  2465. i++;
  2466. m->xmin = atof (coordx);
  2467. m->xmax = atof (coordx);
  2468. m->ymin = atof (coordy);
  2469. m->ymax = atof (coordy);
  2470. m->zmin = atof (coordz);
  2471. m->zmax = atof (coordz);
  2472. }
  2473. else
  2474. {
  2475. if (atof (coordx) < m->xmin)
  2476. m->xmin = atof (coordx);
  2477. if (atof (coordx) > m->xmax)
  2478. m->xmax = atof (coordx);
  2479. if (atof (coordy) < m->ymin)
  2480. m->ymin = atof (coordy);
  2481. if (atof (coordy) > m->ymax)
  2482. m->ymax = atof (coordy);
  2483. if (atof (coordz) < m->zmin)
  2484. m->zmin = atof (coordz);
  2485. if (atof (coordz) > m->zmax)
  2486. m->zmax = atof (coordz);
  2487. }
  2488. fscanf (is, "%s", coordx);
  2489. }
  2490. fscanf (is, "%s", str);
  2491. while (strcmp (str, "coordIndex") && strcmp (str, "coordIndex["))
  2492. fscanf (is, "%s", str);
  2493. if (!strcmp (str, "coordIndex"))
  2494. fscanf (is, "%s", str);
  2495. //lecture des faces
  2496. fscanf (is, "%s", coordx);
  2497. while (strcmp (coordx, "}") && strcmp (coordx, "]"))
  2498. {
  2499. //lecture des trois sommets composant la face
  2500. fscanf (is, "%s", coordy);
  2501. fscanf (is, "%s", coordz);
  2502. //ajout des aretes avec vérification au préalable de leur existence
  2503. a2ri_vef_add_edge (m, atoi (coordx), atoi (coordy), 1);
  2504. a2ri_vef_add_edge (m, atoi (coordy), atoi (coordz), 1);
  2505. a2ri_vef_add_edge (m, atoi (coordz), atoi (coordx), 1);
  2506. //recherche des aretes
  2507. int ed1 = a2ri_vef_search_edge (*m, atoi (coordx), atoi (coordy));
  2508. int ed2 = a2ri_vef_search_edge (*m, atoi (coordy), atoi (coordz));
  2509. int ed3 = a2ri_vef_search_edge (*m, atoi (coordz), atoi (coordx));
  2510. //ajout de la faces avec les trois aretes
  2511. a2ri_vef_add_face (m, ed1, ed2, ed3);
  2512. // reading the -1
  2513. fscanf (is, "%s", coordx);
  2514. fscanf (is, "%s", coordx);
  2515. }
  2516. fclose (is);
  2517. setlocale(LC_NUMERIC,"");
  2518. return 1;
  2519. }
  2520. /**
  2521. Ouverture d'un fichier vef contenant un modèle
  2522. @param file chemin du fichier à ouvrir
  2523. @param m pointeur sur le modèle
  2524. @return 1 si succès, 0 sinon
  2525. */
  2526. int
  2527. a2ri_vef_load_vef (
  2528. char *file,
  2529. vef_model * m)
  2530. {
  2531. FILE *is = NULL;
  2532. int nbvertex,
  2533. nbedge,
  2534. nbface,
  2535. ve1,
  2536. ve2,
  2537. ed1,
  2538. ed2,
  2539. ed3,
  2540. classif;
  2541. double coordx,
  2542. coordy,
  2543. coordz;
  2544. setlocale(LC_NUMERIC,"C");
  2545. is = fopen (file, "r");
  2546. if (is == NULL)
  2547. return 0;
  2548. a2ri_vef_init (m);
  2549. //lecture du nombre de sommets
  2550. fscanf (is, "%d", &nbvertex);
  2551. //lecture du nombre d'aretes
  2552. fscanf (is, "%d", &nbedge);
  2553. //lecture du nombre de face
  2554. fscanf (is, "%d", &nbface);
  2555. //lecture des sommets
  2556. for (int i = 0; i < nbvertex; i++)
  2557. {
  2558. fscanf (is, "%lf", &coordx);
  2559. fscanf (is, "%lf", &coordy);
  2560. fscanf (is, "%lf", &coordz);
  2561. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2562. //mis à jour de la bounding box
  2563. if (i == 0)
  2564. {
  2565. m->xmin = coordx;
  2566. m->xmax = coordx;
  2567. m->ymin = coordy;
  2568. m->ymax = coordy;
  2569. m->zmin = coordz;
  2570. m->zmax = coordz;
  2571. }
  2572. else
  2573. {
  2574. if (coordx < m->xmin)
  2575. m->xmin = coordx;
  2576. if (coordx > m->xmax)
  2577. m->xmax = coordx;
  2578. if (coordy < m->ymin)
  2579. m->ymin = coordy;
  2580. if (coordy > m->ymax)
  2581. m->ymax = coordy;
  2582. if (coordz < m->zmin)
  2583. m->zmin = coordz;
  2584. if (coordz > m->zmax)
  2585. m->zmax = coordz;
  2586. }
  2587. }
  2588. //lecture des aretes
  2589. for (int i = 0; i < nbedge; i++)
  2590. {
  2591. fscanf (is, "%d", &ve1);
  2592. fscanf (is, "%d", &ve2);
  2593. a2ri_vef_add_edge (m, ve1, ve2, 0);
  2594. }
  2595. //lecture des faces
  2596. for (int i = 0; i < nbface; i++)
  2597. {
  2598. fscanf (is, "%d", &ed1);
  2599. fscanf (is, "%d", &ed2);
  2600. fscanf (is, "%d", &ed3);
  2601. a2ri_vef_add_face (m, ed1, ed2, ed3);
  2602. }
  2603. //lecture des classifications si disponible
  2604. for (int i = 0; i < nbface; i++)
  2605. {
  2606. if (!feof (is))
  2607. {
  2608. fscanf (is, "%d", &classif);
  2609. m->fa[i].type = classif;
  2610. }
  2611. }
  2612. fclose (is);
  2613. setlocale(LC_NUMERIC,"");
  2614. return 1;
  2615. }
  2616. /**
  2617. Ouverture d'un fichier gts contenant un modèle
  2618. @param file chemin du fichier à ouvrir
  2619. @param m pointeur sur le modèle
  2620. @return 1 si succès, 0 sinon
  2621. */
  2622. int
  2623. a2ri_vef_load_gts (
  2624. char *file,
  2625. vef_model * m)
  2626. {
  2627. FILE *is = NULL;
  2628. int nbvertex,
  2629. nbedge,
  2630. nbface,
  2631. ve1,
  2632. ve2,
  2633. ed1,
  2634. ed2,
  2635. ed3,
  2636. classif;
  2637. double coordx,
  2638. coordy,
  2639. coordz;
  2640. setlocale(LC_NUMERIC,"C");
  2641. is = fopen (file, "r");
  2642. if (is == NULL)
  2643. return 0;
  2644. a2ri_vef_init (m);
  2645. //lecture du nombre de sommets
  2646. fscanf (is, "%d", &nbvertex);
  2647. //lecture du nombre d'aretes
  2648. fscanf (is, "%d", &nbedge);
  2649. //lecture du nombre de face
  2650. fscanf (is, "%d", &nbface);
  2651. //lecture des sommets
  2652. for (int i = 0; i < nbvertex; i++)
  2653. {
  2654. fscanf (is, "%lf", &coordx);
  2655. fscanf (is, "%lf", &coordy);
  2656. fscanf (is, "%lf", &coordz);
  2657. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2658. //mis à jour de la bounding box
  2659. if (i == 0)
  2660. {
  2661. m->xmin = coordx;
  2662. m->xmax = coordx;
  2663. m->ymin = coordy;
  2664. m->ymax = coordy;
  2665. m->zmin = coordz;
  2666. m->zmax = coordz;
  2667. }
  2668. else
  2669. {
  2670. if (coordx < m->xmin)
  2671. m->xmin = coordx;
  2672. if (coordx > m->xmax)
  2673. m->xmax = coordx;
  2674. if (coordy < m->ymin)
  2675. m->ymin = coordy;
  2676. if (coordy > m->ymax)
  2677. m->ymax = coordy;
  2678. if (coordz < m->zmin)
  2679. m->zmin = coordz;
  2680. if (coordz > m->zmax)
  2681. m->zmax = coordz;
  2682. }
  2683. }
  2684. //lecture des aretes
  2685. for (int i = 0; i < nbedge; i++)
  2686. {
  2687. fscanf (is, "%d", &ve1);
  2688. fscanf (is, "%d", &ve2);
  2689. ve1--;
  2690. ve2--;
  2691. a2ri_vef_add_edge (m, ve1, ve2, 0);
  2692. }
  2693. //lecture des faces
  2694. for (int i = 0; i < nbface; i++)
  2695. {
  2696. fscanf (is, "%d", &ed1);
  2697. fscanf (is, "%d", &ed2);
  2698. fscanf (is, "%d", &ed3);
  2699. ed1--;
  2700. ed2--;
  2701. ed3--;
  2702. a2ri_vef_add_face (m, ed1, ed2, ed3);
  2703. }
  2704. //lecture des classifications si disponible
  2705. for (int i = 0; i < nbface; i++)
  2706. {
  2707. if (!feof (is))
  2708. {
  2709. fscanf (is, "%d", &classif);
  2710. m->fa[i].type = classif;
  2711. }
  2712. }
  2713. fclose (is);
  2714. setlocale(LC_NUMERIC,"");
  2715. return 1;
  2716. }
  2717. /**
  2718. Ouverture d'un fichier ply contenant un modèle
  2719. @param file chemin du fichier à ouvrir
  2720. @param m pointeur sur le modèle
  2721. @return 1 si succès, 0 sinon
  2722. */
  2723. int
  2724. a2ri_vef_load_ply (
  2725. char *file,
  2726. vef_model * m)
  2727. {
  2728. int prop = 0,
  2729. nbpropvertex = 0,
  2730. nbpropface = 0,
  2731. nbvertex,
  2732. nbface,
  2733. ve1,
  2734. ve2,
  2735. ve3;
  2736. char str[20];
  2737. double coordx,
  2738. coordy,
  2739. coordz;
  2740. FILE *is = NULL;
  2741. setlocale(LC_NUMERIC,"C");
  2742. is = fopen (file, "r");
  2743. if (is == NULL)
  2744. return 0;
  2745. fscanf (is, "%s", str);
  2746. if (strcmp (str, "ply"))
  2747. return 0;
  2748. while (strcmp (str, "end_header"))
  2749. {
  2750. fscanf (is, "%s", str);
  2751. if (prop == 1 && !strcmp (str, "property"))
  2752. nbpropvertex++;
  2753. if (prop == 2 && !strcmp (str, "property"))
  2754. nbpropface++;
  2755. if (!strcmp (str, "element"))
  2756. {
  2757. fscanf (is, "%s", str);
  2758. //lecture du nombre de sommets
  2759. if (!strcmp (str, "vertex"))
  2760. {
  2761. prop = 1;
  2762. fscanf (is, "%d", &nbvertex);
  2763. }
  2764. //lecture du nombre de faces
  2765. else
  2766. {
  2767. prop = 1;
  2768. fscanf (is, "%d", &nbface);
  2769. }
  2770. }
  2771. }
  2772. //lecture des sommets
  2773. for (int i = 0; i < nbvertex; i++)
  2774. {
  2775. fscanf (is, "%lf %lf %lf", &coordx, &coordy, &coordz);
  2776. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2777. //mis à jour de la bounding box
  2778. if (i == 0)
  2779. {
  2780. m->xmin = coordx;
  2781. m->xmax = coordx;
  2782. m->ymin = coordy;
  2783. m->ymax = coordy;
  2784. m->zmin = coordz;
  2785. m->zmax = coordz;
  2786. }
  2787. else
  2788. {
  2789. if (coordx < m->xmin)
  2790. m->xmin = coordx;
  2791. if (coordx > m->xmax)
  2792. m->xmax = coordx;
  2793. if (coordy < m->ymin)
  2794. m->ymin = coordy;
  2795. if (coordy > m->ymax)
  2796. m->ymax = coordy;
  2797. if (coordz < m->zmin)
  2798. m->zmin = coordz;
  2799. if (coordz > m->zmax)
  2800. m->zmax = coordz;
  2801. }
  2802. for (int j = 0; j < nbpropvertex - 4; j++)
  2803. fscanf (is, "%s", str);
  2804. }
  2805. //lecture des faces
  2806. for (int i = 0; i < nbface; i++)
  2807. {
  2808. fscanf (is, "%d", &ve1);
  2809. //lecture des trois sommets composant la face
  2810. fscanf (is, "%d %d %d", &ve1, &ve2, &ve3);
  2811. //ajout des aretes avec vérification au préalable de leur existence
  2812. a2ri_vef_add_edge (m, ve1, ve2, 1);
  2813. a2ri_vef_add_edge (m, ve2, ve3, 1);
  2814. a2ri_vef_add_edge (m, ve3, ve1, 1);
  2815. //recherche des aretes
  2816. int ed1 = a2ri_vef_search_edge (*m, ve1, ve2);
  2817. int ed2 = a2ri_vef_search_edge (*m, ve2, ve3);
  2818. int ed3 = a2ri_vef_search_edge (*m, ve3, ve1);
  2819. //ajout de la faces avec les trois aretes
  2820. a2ri_vef_add_face (m, ed1, ed2, ed3);
  2821. }
  2822. fclose (is);
  2823. setlocale(LC_NUMERIC,"");
  2824. return 1;
  2825. }
  2826. /**
  2827. Ouverture d'un fichier off contenant un modèle
  2828. @param file chemin du fichier à ouvrir
  2829. @param m pointeur sur le modèle
  2830. @return 1 si succès, 0 sinon
  2831. */
  2832. int
  2833. a2ri_vef_load_off (
  2834. char *file,
  2835. vef_model * m)
  2836. {
  2837. FILE *is = NULL;
  2838. char str[20];
  2839. double coordx,
  2840. coordy,
  2841. coordz;
  2842. int classif,
  2843. ar1,
  2844. ar2,
  2845. ar3,
  2846. nbvertex,
  2847. nbface;
  2848. setlocale(LC_NUMERIC,"C");
  2849. is = fopen (file, "r");
  2850. if (is == NULL)
  2851. return 0;
  2852. a2ri_vef_init (m);
  2853. fscanf (is, "%s", str);
  2854. if (strcmp (str, "OFF"))
  2855. return 0;
  2856. //lecture du nombre de sommets
  2857. fscanf (is, "%d", &nbvertex);
  2858. //lecture du nombre de faces
  2859. fscanf (is, "%d", &nbface);
  2860. fscanf (is, "%d", &ar1);
  2861. //lecture des sommets
  2862. for (int i = 0; i < nbvertex; i++)
  2863. {
  2864. fscanf (is, "%lf", &coordx);
  2865. fscanf (is, "%lf", &coordy);
  2866. fscanf (is, "%lf", &coordz);
  2867. a2ri_vef_add_vertex (m, coordx, coordy, coordz);
  2868. //mis à jour de la bounding box
  2869. if (i == 0)
  2870. {
  2871. m->xmin = coordx;
  2872. m->xmax = coordx;
  2873. m->ymin = coordy;
  2874. m->ymax = coordy;
  2875. m->zmin = coordz;
  2876. m->zmax = coordz;
  2877. }
  2878. else
  2879. {
  2880. if (coordx < m->xmin)
  2881. m->xmin = coordx;
  2882. if (coordx > m->xmax)
  2883. m->xmax = coordx;
  2884. if (coordy < m->ymin)
  2885. m->ymin = coordy;
  2886. if (coordy > m->ymax)
  2887. m->ymax = coordy;
  2888. if (coordz < m->zmin)
  2889. m->zmin = coordz;
  2890. if (coordz > m->zmax)
  2891. m->zmax = coordz;
  2892. }
  2893. }
  2894. //lecture des faces
  2895. for (int i = 0; i < nbface; i++)
  2896. {
  2897. //lecture des trois sommets composant la face
  2898. fscanf (is, "%d", &ar1);
  2899. fscanf (is, "%d", &ar1);
  2900. fscanf (is, "%d", &ar2);
  2901. fscanf (is, "%d", &ar3);
  2902. //ajout des aretes avec vérification au préalable de leur existence
  2903. a2ri_vef_add_edge (m, ar1, ar2, 1);
  2904. a2ri_vef_add_edge (m, ar2, ar3, 1);
  2905. a2ri_vef_add_edge (m, ar3, ar1, 1);
  2906. //recherche des aretes
  2907. int ed1 = a2ri_vef_search_edge (*m, ar1, ar2);
  2908. int ed2 = a2ri_vef_search_edge (*m, ar2, ar3);
  2909. int ed3 = a2ri_vef_search_edge (*m, ar3, ar1);
  2910. //ajout de la faces avec les trois aretes
  2911. a2ri_vef_add_face (m, ed1, ed2, ed3);
  2912. }
  2913. //lecture des classifications si disponible
  2914. for (int i = 0; i < nbface; i++)
  2915. {
  2916. if (!feof (is))
  2917. {
  2918. fscanf (is, "%d", &classif);
  2919. m->fa[i].type = classif;
  2920. }
  2921. }
  2922. fclose (is);
  2923. setlocale(LC_NUMERIC,"");
  2924. return 1;
  2925. }
  2926. /**
  2927. Enregistrement d'un modèle dans un format défini par l'extension du nom de fichier
  2928. @param file nom du fichier
  2929. @param m le modèle
  2930. @return aucun
  2931. */
  2932. int
  2933. a2ri_vef_save_file (
  2934. char *file,
  2935. vef_model m)
  2936. {
  2937. char ext[4];
  2938. memcpy (ext, file + (strlen (file) - 3), 3);
  2939. ext[3] = 0;
  2940. //appel de la fonction d'enregistrement en fonction du type de l'extension du fichier
  2941. if (!strcmp (ext, "wrl"))
  2942. return a2ri_vef_save_wrl (file, m);
  2943. if (!strcmp (ext, "vef"))
  2944. return a2ri_vef_save_vef (file, m);
  2945. if (!strcmp (ext, "pgn"))
  2946. return a2ri_vef_save_pgn (file, m);
  2947. if (!strcmp (ext, "ply"))
  2948. return a2ri_vef_save_ply (file, m);
  2949. if (!strcmp (ext, "off"))
  2950. return a2ri_vef_save_off (file, m);
  2951. if (!strcmp (ext, "obj"))
  2952. return a2ri_vef_save_obj (file, m);
  2953. if (!strcmp (ext, "gts"))
  2954. return a2ri_vef_save_gts (file, m);
  2955. return 0;
  2956. }
  2957. /**
  2958. Enregistrement d'un modèle dans un fichier wrl
  2959. @param file chemin du fichier
  2960. @param m modèle à sauvegarder
  2961. @return 1 si succès, 0 sinon
  2962. */
  2963. int
  2964. a2ri_vef_save_wrl (
  2965. char *file,
  2966. vef_model m)
  2967. {
  2968. setlocale(LC_NUMERIC,"C");
  2969. if (strlen (file) < 4)
  2970. {
  2971. strcat (file, ".wrl");
  2972. }
  2973. char ext[5];
  2974. memcpy (ext, file + (strlen (file) - 4), 4);
  2975. ext[4] = '\0';
  2976. if (strcmp (ext, ".wrl"))
  2977. strcat (file, ".wrl");
  2978. FILE *os = NULL;
  2979. os = fopen (file, "w");
  2980. if (os == NULL)
  2981. return 0;
  2982. //ecriture de l'en-tete
  2983. fprintf (os, "#VRML V2.0 utf8\n");
  2984. fprintf (os, "Shape{\n");
  2985. fprintf (os, "appearance Appearance {\n");
  2986. fprintf (os, "material Material {\n");
  2987. fprintf (os, "}\n");
  2988. fprintf (os, "}\n");
  2989. fprintf (os, "geometry IndexedFaceSet{\n");
  2990. fprintf (os, "coord Coordinate{\n");
  2991. fprintf (os, "point[\n");
  2992. //ecriture des sommets
  2993. for (int i = 0; i < m.nbvertex; i++)
  2994. fprintf (os, "%f %f %f,\n", m.ve[i].x, m.ve[i].y, m.ve[i].z);
  2995. fprintf (os, "]\n");
  2996. fprintf (os, "}\n");
  2997. //ecriture des faces
  2998. fprintf (os, "coordIndex[\n");
  2999. for (int i = 0; i < m.nbface; i++)
  3000. {
  3001. int nb1,
  3002. nb2,
  3003. nb3;
  3004. vef_face_get_vertices (m.fa[i], m.ed, &nb1, &nb2, &nb3);
  3005. fprintf (os, "%d %d %d -1\n", nb1, nb2, nb3);
  3006. }
  3007. fprintf (os, "]\n");
  3008. fprintf (os, "}\n");
  3009. fprintf (os, "}\n");
  3010. fclose (os);
  3011. setlocale(LC_NUMERIC,"");
  3012. return 1;
  3013. }
  3014. /**
  3015. Enregistrement d'un modèle dans un fichier pgn
  3016. @param file chemin du fichier
  3017. @param m modèle à sauvegarder
  3018. @return 1 si succès, 0 sinon
  3019. */
  3020. int
  3021. a2ri_vef_save_pgn (
  3022. char *file,
  3023. vef_model m)
  3024. {
  3025. setlocale(LC_NUMERIC,"C");
  3026. if (strlen (file) < 4)
  3027. {
  3028. strcat (file, ".pgn");
  3029. }
  3030. char ext[5];
  3031. memcpy (ext, file + (strlen (file) - 4), 4);
  3032. ext[4] = '\0';
  3033. if (strcmp (ext, ".pgn"))
  3034. strcat (file, ".pgn");
  3035. FILE *os = NULL;
  3036. os = fopen (file, "w");
  3037. if (os == NULL)
  3038. return 0;
  3039. //ecriture du nombre de sommets et faces
  3040. fprintf (os, "%d %d \n\n", m.nbvertex, m.nbface);
  3041. //ecriture des sommets
  3042. for (int i = 0; i < m.nbvertex; i++)
  3043. fprintf (os, "%f %f %f\n", m.ve[i].x, m.ve[i].y, m.ve[i].z);
  3044. //ecriture des faces
  3045. for (int i = 0; i < m.nbface; i++)
  3046. {
  3047. int ve1,
  3048. ve2,
  3049. ve3;
  3050. vef_face_get_vertices (m.fa[i], m.ed, &ve1, &ve2, &ve3);
  3051. fprintf (os, "%d %d %d -1\n", ve1, ve2, ve3);
  3052. }
  3053. fclose (os);
  3054. setlocale(LC_NUMERIC,"");
  3055. return 1;
  3056. }
  3057. /**
  3058. Enregistrement d'un modèle dans un fichier vef
  3059. @param file chemin du fichier
  3060. @param m modèle à sauvegarder
  3061. @return 1 si succès, 0 sinon
  3062. */
  3063. int
  3064. a2ri_vef_save_vef (
  3065. char *file,
  3066. vef_model m)
  3067. {
  3068. setlocale(LC_NUMERIC,"C");
  3069. if (strlen (file) < 4)
  3070. {
  3071. strcat (file, ".vef");
  3072. }
  3073. char ext[5];
  3074. memcpy (ext, file + (strlen (file) - 4), 4);
  3075. ext[4] = '\0';
  3076. if (strcmp (ext, ".vef"))
  3077. strcat (file, ".vef");
  3078. FILE *os = NULL;
  3079. os = fopen (file, "w");
  3080. if (os == NULL)
  3081. return 0;
  3082. //ecriture des nombre de sommets, aretes et faces
  3083. fprintf (os, "%d %d %d\n\n", m.nbvertex, m.nbedge, m.nbface);
  3084. //ecriture des sommets
  3085. for (int i = 0; i < m.nbvertex; i++)
  3086. fprintf (os, "%f %f %f\n", m.ve[i].x, m.ve[i].y, m.ve[i].z);
  3087. //ecriture des aretes
  3088. for (int i = 0; i < m.nbedge; i++)
  3089. fprintf (os, "%d %d\n", m.ed[i].ve1, m.ed[i].ve2);
  3090. //ecriture des faces
  3091. for (int i = 0; i < m.nbface; i++)
  3092. fprintf (os, "%d %d %d\n", m.fa[i].ed1, m.fa[i].ed2, m.fa[i].ed3);
  3093. fclose (os);
  3094. setlocale(LC_NUMERIC,"");
  3095. return 1;
  3096. }
  3097. /**
  3098. Enregistrement d'un modèle dans un fichier gts
  3099. @param file chemin du fichier
  3100. @param m modèle à sauvegarder
  3101. @return 1 si succès, 0 sinon
  3102. */
  3103. int
  3104. a2ri_vef_save_gts (
  3105. char *file,
  3106. vef_model m)
  3107. {
  3108. setlocale(LC_NUMERIC,"C");
  3109. if (strlen (file) < 4)
  3110. {
  3111. strcat (file, ".gts");
  3112. }
  3113. char ext[5];
  3114. memcpy (ext, file + (strlen (file) - 4), 4);
  3115. ext[4] = '\0';
  3116. if (strcmp (ext, ".gts"))
  3117. strcat (file, ".gts");
  3118. FILE *os = NULL;
  3119. os = fopen (file, "w");
  3120. if (os == NULL)
  3121. return 0;
  3122. //ecriture des nombre de sommets, aretes et faces
  3123. fprintf (os, "%d %d %d\n\n", m.nbvertex, m.nbedge, m.nbface);
  3124. //ecriture des sommets
  3125. for (int i = 0; i < m.nbvertex; i++)
  3126. fprintf (os, "%f %f %f\n", m.ve[i].x, m.ve[i].y, m.ve[i].z);
  3127. //ecriture des aretes
  3128. for (int i = 0; i < m.nbedge; i++)
  3129. fprintf (os, "%d %d\n", m.ed[i].ve1 + 1, m.ed[i].ve2 + 1);
  3130. //ecriture des faces
  3131. for (int i = 0; i < m.nbface; i++)
  3132. fprintf (os, "%d %d %d\n", m.fa[i].ed1 + 1, m.fa[i].ed2 + 1,
  3133. m.fa[i].ed3 + 1);
  3134. fclose (os);
  3135. setlocale(LC_NUMERIC,"");
  3136. return 1;
  3137. }
  3138. /**
  3139. Enregistrement d'un modèle dans un fichier off
  3140. @param file chemin du fichier
  3141. @param m modèle à sauvegarder
  3142. @return 1 si succès, 0 sinon
  3143. */
  3144. int
  3145. a2ri_vef_save_off (
  3146. char *file,
  3147. vef_model m)
  3148. {
  3149. setlocale(LC_NUMERIC,"C");
  3150. if (strlen (file) < 4)
  3151. {
  3152. strcat (file, ".off");
  3153. }
  3154. char ext[5];
  3155. memcpy (ext, file + (strlen (file) - 4), 4);
  3156. ext[4] = '\0';
  3157. if (strcmp (ext, ".off"))
  3158. strcat (file, ".off");
  3159. FILE *os = NULL;
  3160. os = fopen (file, "w");
  3161. if (os == NULL)
  3162. return 0;
  3163. //ecriture de l'en-tete et des nombres de sommets et faces
  3164. fprintf (os, "OFF\n%d %d 0\n\n", m.nbvertex, m.nbface);
  3165. //ecriture des sommets
  3166. for (int i = 0; i < m.nbvertex; i++)
  3167. fprintf (os, "%f %f %f\n", m.ve[i].x, m.ve[i].y, m.ve[i].z);
  3168. //ecriture des faces
  3169. for (int i = 0; i < m.nbface; i++)
  3170. {
  3171. int ve1,
  3172. ve2,
  3173. ve3;
  3174. vef_face_get_vertices (m.fa[i], m.ed, &ve1, &ve2, &ve3);
  3175. fprintf (os, "3 %d %d %d \n", ve1, ve2, ve3);
  3176. }
  3177. fclose (os);
  3178. setlocale(LC_NUMERIC,"");
  3179. return 1;
  3180. }
  3181. /**
  3182. Enregistrement d'un modèle dans un fichier ply
  3183. @param file chemin du fichier
  3184. @param m modèle à sauvegarder
  3185. @return 1 si succès, 0 sinon
  3186. */
  3187. int
  3188. a2ri_vef_save_ply (
  3189. char *file,
  3190. vef_model m)
  3191. {
  3192. setlocale(LC_NUMERIC,"C");
  3193. if (strlen (file) < 4)
  3194. {
  3195. strcat (file, ".ply");
  3196. }
  3197. char ext[5];
  3198. memcpy (ext, file + (strlen (file) - 4), 4);
  3199. ext[4] = '\0';
  3200. if (strcmp (ext, ".ply"))
  3201. strcat (file, ".ply");
  3202. FILE *os = NULL;
  3203. os = fopen (file, "w");
  3204. if (os == NULL)
  3205. return 0;
  3206. //ecriture de l'en-tete avec le nombre de sommets et de faces
  3207. fprintf (os, "ply\nformat ascii 1.0\ncomment zipper output\n");
  3208. fprintf (os,
  3209. "element vertex %d\nproperty float x\nproperty float y\nproperty float z\nelement face %d\n",
  3210. m.nbvertex, m.nbface);
  3211. fprintf (os, "property list uchar int vertex_indices\nend_header\n");
  3212. //ecriture des sommets
  3213. for (int i = 0; i < m.nbvertex; i++)
  3214. fprintf (os, "%f %f %f\n", m.ve[i].x, m.ve[i].y, m.ve[i].z);
  3215. //ecriture des faces
  3216. for (int i = 0; i < m.nbface; i++)
  3217. {
  3218. int ve1,
  3219. ve2,
  3220. ve3;
  3221. vef_face_get_vertices (m.fa[i], m.ed, &ve1, &ve2, &ve3);
  3222. fprintf (os, "3 %d %d %d \n", ve1, ve2, ve3);
  3223. }
  3224. fclose (os);
  3225. setlocale(LC_NUMERIC,"");
  3226. return 1;
  3227. }
  3228. /**
  3229. Enregistrement d'un modèle dans un fichier obj
  3230. @param file chemin du fichier
  3231. @param m modèle à sauvegarder
  3232. @return 1 si succès, 0 sinon
  3233. */
  3234. int
  3235. a2ri_vef_save_obj (
  3236. char *file,
  3237. vef_model m)
  3238. {
  3239. setlocale(LC_NUMERIC,"C");
  3240. if (strlen (file) < 4)
  3241. {
  3242. strcat (file, ".obj");
  3243. }
  3244. char ext[5];
  3245. memcpy (ext, file + (strlen (file) - 4), 4);
  3246. ext[4] = '\0';
  3247. if (strcmp (ext, ".obj"))
  3248. strcat (file, ".obj");
  3249. FILE *os = NULL;
  3250. os = fopen (file, "w");
  3251. if (os == NULL)
  3252. return 0;
  3253. //ecriture des sommets
  3254. for (int i = 0; i < m.nbvertex; i++)
  3255. fprintf (os, "v %f %f %f\n", m.ve[i].x, m.ve[i].y, m.ve[i].z);
  3256. fprintf (os, "\n");
  3257. //ecriture des faces
  3258. for (int i = 0; i < m.nbface; i++)
  3259. {
  3260. int ve1,
  3261. ve2,
  3262. ve3;
  3263. vef_face_get_vertices (m.fa[i], m.ed, &ve1, &ve2, &ve3);
  3264. fprintf (os, "f %d %d %d\n", ve1 + 1, ve2 + 1, ve3 + 1);
  3265. }
  3266. fclose (os);
  3267. setlocale(LC_NUMERIC,"");
  3268. return 1;
  3269. }