main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <a2ri/io.h>
  5. #include <a2ri/subdivision.h>
  6. #include <a2ri/geometry.h>
  7. #include <a2ri/vector.h>
  8. void usage(char* argv[], char* type)
  9. {
  10. if(!strcmp(type,"general"))
  11. {
  12. printf("usage :\n%s <grille|boite|icosphere|uvsphere|cylindre|cone> <argument> <nom fichier>\n",argv[0]);
  13. printf("--> %s grille <largeur> <hauteur> <nbcase largeur> <nbcase hauteur> <nom fichier>\n",argv[0]);
  14. printf("--> %s cylindre <rayon> <hauteur> <nb segment> <nb anneau> <nom fichier>\n",argv[0]);
  15. printf("--> %s cone <rayon> <hauteur> <nb segment> <nb anneau> <nom fichier>\n",argv[0]);
  16. printf("--> %s uvsphere <rayon> <nb segment> <nb anneau> <nom fichier>\n",argv[0]);
  17. printf("--> %s icosphere <rayon> <nb subdivision> <nom fichier>\n",argv[0]);
  18. printf("--> %s boite <tailleX> <tailleY> <tailleZ> <nbcaseX> <nbcaseY> <nbcaseZ> <nom fichier>\n",argv[0]);
  19. exit(EXIT_FAILURE);
  20. }
  21. if(!strcmp(type,"grille"))
  22. {
  23. printf("usage :\n%s grille <largeur> <hauteur> <nbcase largeur> <nbcase hauteur> <nom fichier>\n",argv[0]);
  24. exit(EXIT_FAILURE);
  25. }
  26. if(!strcmp(type,"boite"))
  27. {
  28. printf("usage :\n%s boite <tailleX> <tailleY> <tailleZ> <nbcaseX> <nbcaseY> <nbcaseZ> <nom fichier>\n",argv[0]);
  29. exit(EXIT_FAILURE);
  30. }
  31. if(!strcmp(type,"icosphere"))
  32. {
  33. printf("usage :\n%s icosphere <rayon> <nb subdivision> <nom fichier>\n",argv[0]);
  34. exit(EXIT_FAILURE);
  35. }
  36. if(!strcmp(type,"uvsphere"))
  37. {
  38. printf("usage :\n%s uvsphere <rayon> <nb segment> <nb anneau> <nom fichier>\n",argv[0]);
  39. exit(EXIT_FAILURE);
  40. }
  41. if(!strcmp(type,"cylindre"))
  42. {
  43. printf("usage :\n%s cylindre <rayon> <hauteur> <nb segment> <nb anneau> <nom fichier>\n",argv[0]);
  44. exit(EXIT_FAILURE);
  45. }
  46. if(!strcmp(type,"cone"))
  47. {
  48. printf("usage :\n%s cone <rayon> <hauteur> <nb segment> <nb anneau> <nom fichier>\n",argv[0]);
  49. exit(EXIT_FAILURE);
  50. }
  51. }
  52. /*************************************************************/
  53. void
  54. gen_grille(char* argv[], vf_model *primitive)
  55. {
  56. double tailleh=atof(argv[2]),taillev=atof(argv[3]);
  57. int nbcaseh=atoi(argv[4]),nbcasev=atoi(argv[5]);
  58. float dh=tailleh/(nbcaseh*1.),dv=taillev/(nbcasev*1.);
  59. for(int j=0;j<=nbcasev;j++)
  60. for(int i=0;i<=nbcaseh;i++)
  61. a2ri_vf_add_vertex(primitive,i*dh,j*dv,0);
  62. for(int j=0;j<nbcasev;j++)
  63. for(int i=0;i<nbcaseh;i++)
  64. {
  65. //printf("creation face : %d %d %d\n",j*(nbcaseh+1)+i,j*(nbcaseh+1)+i+1,(j+1)*(nbcaseh+1)+i);
  66. a2ri_vf_add_face(primitive,j*(nbcaseh+1)+i,j*(nbcaseh+1)+i+1,(j+1)*(nbcaseh+1)+i);
  67. //printf("creation face : %d %d %d\n",j*(nbcaseh+1)+i+1,(j+1)*(nbcaseh+1)+i+1,(j+1)*(nbcaseh+1)+i);
  68. a2ri_vf_add_face(primitive,j*(nbcaseh+1)+i+1,(j+1)*(nbcaseh+1)+i+1,(j+1)*(nbcaseh+1)+i);
  69. }
  70. }
  71. void
  72. gen_boite(char* argv[], vf_model *primitive)
  73. {
  74. double tailleX=atof(argv[2]),tailleY=atof(argv[3]),tailleZ=atof(argv[4]);
  75. int nbcaseX=atoi(argv[5]),nbcaseY=atoi(argv[6]),nbcaseZ=atoi(argv[7]);
  76. double dX=tailleX/(nbcaseX*1.),dY=tailleY/(nbcaseY*1.),dZ=tailleZ/(nbcaseZ*1.);
  77. int nbpt_1,nbpt_2,nbpt_3,nbpt_4,nbpt_5;
  78. if(nbcaseX<1 || nbcaseY<1 || nbcaseZ<1)
  79. {
  80. printf("impossible de generer une boite dont le nombre de case suivant la direction X, Y ou Z est inférieur a 1\n");
  81. exit(EXIT_FAILURE);
  82. }
  83. //sommets avant/arriere
  84. for(int j=0;j<=nbcaseY;j++)
  85. for(int i=0;i<=nbcaseX;i++)
  86. a2ri_vf_add_vertex(primitive,i*dX,j*dY,0);
  87. nbpt_1=primitive->nbvertex;
  88. for(int j=0;j<=nbcaseY;j++)
  89. for(int i=0;i<=nbcaseX;i++)
  90. a2ri_vf_add_vertex(primitive,i*dX,j*dY,tailleZ);
  91. nbpt_2=primitive->nbvertex;
  92. //sommets dessous/dessus
  93. for(int k=1;k<nbcaseZ;k++)
  94. for(int i=0;i<=nbcaseX;i++)
  95. a2ri_vf_add_vertex(primitive,i*dX,0,k*dZ);
  96. nbpt_3=primitive->nbvertex;
  97. for(int k=1;k<nbcaseZ;k++)
  98. for(int i=0;i<=nbcaseX;i++)
  99. a2ri_vf_add_vertex(primitive,i*dX,tailleY,k*dZ);
  100. nbpt_4=primitive->nbvertex;
  101. //sommets gauche/droite
  102. for(int j=1;j<nbcaseY;j++)
  103. for(int k=1;k<nbcaseZ;k++)
  104. a2ri_vf_add_vertex(primitive,0,j*dY,k*dZ);
  105. nbpt_5=primitive->nbvertex;
  106. for(int j=1;j<nbcaseY;j++)
  107. for(int k=1;k<nbcaseZ;k++)
  108. a2ri_vf_add_vertex(primitive,tailleX,j*dY,k*dZ);
  109. //faces avant/arriere
  110. for(int j=0;j<nbcaseY;j++)
  111. for(int i=0;i<nbcaseX;i++)
  112. {
  113. a2ri_vf_add_face(primitive,j*(nbcaseX+1)+i,(j+1)*(nbcaseX+1)+i,j*(nbcaseX+1)+i+1);
  114. a2ri_vf_add_face(primitive,j*(nbcaseX+1)+i+1,(j+1)*(nbcaseX+1)+i,(j+1)*(nbcaseX+1)+i+1);
  115. a2ri_vf_add_face(primitive,j*(nbcaseX+1)+i+nbpt_1,j*(nbcaseX+1)+i+1+nbpt_1,(j+1)*(nbcaseX+1)+i+nbpt_1);
  116. a2ri_vf_add_face(primitive,j*(nbcaseX+1)+i+1+nbpt_1,(j+1)*(nbcaseX+1)+i+1+nbpt_1,(j+1)*(nbcaseX+1)+i+nbpt_1);
  117. }
  118. //faces dessous/dessus
  119. if(nbcaseZ>2)
  120. {
  121. for(int k=0;k<nbcaseZ-2;k++)
  122. for(int i=0;i<nbcaseX;i++)
  123. {
  124. a2ri_vf_add_face(primitive,k*(nbcaseX+1)+i+nbpt_2,k*(nbcaseX+1)+i+1+nbpt_2,(k+1)*(nbcaseX+1)+i+nbpt_2);
  125. a2ri_vf_add_face(primitive,k*(nbcaseX+1)+i+1+nbpt_2,(k+1)*(nbcaseX+1)+i+1+nbpt_2,(k+1)*(nbcaseX+1)+i+nbpt_2);
  126. a2ri_vf_add_face(primitive,k*(nbcaseX+1)+i+nbpt_3,(k+1)*(nbcaseX+1)+i+nbpt_3,k*(nbcaseX+1)+i+1+nbpt_3);
  127. a2ri_vf_add_face(primitive,k*(nbcaseX+1)+i+1+nbpt_3,(k+1)*(nbcaseX+1)+i+nbpt_3,(k+1)*(nbcaseX+1)+i+1+nbpt_3);
  128. }
  129. }
  130. if(nbcaseZ>1)
  131. {
  132. for(int i=0;i<nbcaseX;i++)
  133. {
  134. a2ri_vf_add_face(primitive,i,i+1,nbpt_2+i);
  135. a2ri_vf_add_face(primitive,i+1,nbpt_2+i+1,nbpt_2+i);
  136. a2ri_vf_add_face(primitive,nbpt_1+i,nbpt_2+(nbcaseX+1)*(nbcaseZ-2)+i+1,nbpt_1+i+1);
  137. a2ri_vf_add_face(primitive,nbpt_1+i,nbpt_2+(nbcaseX+1)*(nbcaseZ-2)+i,nbpt_2+(nbcaseX+1)*(nbcaseZ-2)+i+1);
  138. a2ri_vf_add_face(primitive,i+(nbcaseX+1)*(nbcaseY),nbpt_3+i,i+1+(nbcaseX+1)*(nbcaseY));
  139. a2ri_vf_add_face(primitive,i+1+(nbcaseX+1)*(nbcaseY),nbpt_3+i,nbpt_3+i+1);
  140. a2ri_vf_add_face(primitive,nbpt_1+i+(nbcaseX+1)*(nbcaseY),nbpt_1+i+1+(nbcaseX+1)*(nbcaseY),nbpt_3+(nbcaseX+1)*(nbcaseZ-2)+i+1);
  141. a2ri_vf_add_face(primitive,nbpt_1+i+(nbcaseX+1)*(nbcaseY),nbpt_3+(nbcaseX+1)*(nbcaseZ-2)+i+1,nbpt_3+(nbcaseX+1)*(nbcaseZ-2)+i);
  142. }
  143. }
  144. else
  145. {
  146. for(int i=0;i<nbcaseX;i++)
  147. {
  148. a2ri_vf_add_face(primitive,i,i+1,nbpt_1+i);
  149. a2ri_vf_add_face(primitive,i+1,nbpt_1+i+1,nbpt_1+i);
  150. a2ri_vf_add_face(primitive,i+(nbcaseX+1)*(nbcaseY),nbpt_1+i+(nbcaseX+1)*(nbcaseY),i+1+(nbcaseX+1)*(nbcaseY));
  151. a2ri_vf_add_face(primitive,i+1+(nbcaseX+1)*(nbcaseY),nbpt_1+i+(nbcaseX+1)*(nbcaseY),nbpt_1+i+1+(nbcaseX+1)*(nbcaseY));
  152. }
  153. }
  154. //faces gauche/droite
  155. if(nbcaseY>2)
  156. {
  157. for(int j=0;j<nbcaseY-2;j++)
  158. for(int k=0;k<nbcaseZ-2;k++)
  159. {
  160. a2ri_vf_add_face(primitive,j*(nbcaseZ-1)+k+nbpt_4,j*(nbcaseZ-1)+k+1+nbpt_4,(j+1)*(nbcaseZ-1)+k+nbpt_4);
  161. a2ri_vf_add_face(primitive,j*(nbcaseZ-1)+k+1+nbpt_4,(j+1)*(nbcaseZ-1)+k+1+nbpt_4,(j+1)*(nbcaseZ-1)+k+nbpt_4);
  162. a2ri_vf_add_face(primitive,j*(nbcaseZ-1)+k+nbpt_5,(j+1)*(nbcaseZ-1)+k+nbpt_5,j*(nbcaseZ-1)+k+1+nbpt_5);
  163. a2ri_vf_add_face(primitive,j*(nbcaseZ-1)+k+1+nbpt_5,(j+1)*(nbcaseZ-1)+k+nbpt_5,(j+1)*(nbcaseZ-1)+k+1+nbpt_5);
  164. }
  165. }
  166. if(nbcaseY>1 && nbcaseZ>1)
  167. {
  168. for(int k=0;k<nbcaseZ-2;k++)
  169. {
  170. a2ri_vf_add_face(primitive,nbpt_4+k,nbpt_2+(k+1)*(nbcaseX+1),nbpt_4+k+1);
  171. a2ri_vf_add_face(primitive,nbpt_4+k,nbpt_2+k*(nbcaseX+1),nbpt_2+(k+1)*(nbcaseX+1));
  172. a2ri_vf_add_face(primitive,nbpt_4+(nbcaseZ-1)*(nbcaseY-2)+k,nbpt_4+(nbcaseZ-1)*(nbcaseY-2)+k+1,nbpt_3+k*(nbcaseX+1));
  173. a2ri_vf_add_face(primitive,nbpt_3+(k+1)*(nbcaseX+1),nbpt_3+k*(nbcaseX+1),nbpt_4+(nbcaseZ-1)*(nbcaseY-2)+k+1);
  174. a2ri_vf_add_face(primitive,nbpt_5+k,nbpt_5+k+1,nbpt_2+(k+1)*(nbcaseX+1)+nbcaseX);
  175. a2ri_vf_add_face(primitive,nbpt_5+k,nbpt_2+(k+1)*(nbcaseX+1)+nbcaseX,nbpt_2+k*(nbcaseX+1)+nbcaseX);
  176. a2ri_vf_add_face(primitive,nbpt_5+(nbcaseZ-1)*(nbcaseY-2)+k,nbpt_3+k*(nbcaseX+1)+nbcaseX,nbpt_5+(nbcaseZ-1)*(nbcaseY-2)+k+1);
  177. a2ri_vf_add_face(primitive,nbpt_3+(k+1)*(nbcaseX+1)+nbcaseX,nbpt_5+(nbcaseZ-1)*(nbcaseY-2)+k+1,nbpt_3+k*(nbcaseX+1)+nbcaseX);
  178. }
  179. for(int j=0;j<nbcaseY-2;j++)
  180. {
  181. a2ri_vf_add_face(primitive,(nbcaseX+1)*(j+2),(nbcaseX+1)*(j+1),nbpt_4+(nbcaseZ-1)*j);
  182. a2ri_vf_add_face(primitive,(nbcaseX+1)*(j+2),nbpt_4+(nbcaseZ-1)*j,nbpt_4+(nbcaseZ-1)*(j+1));
  183. a2ri_vf_add_face(primitive,nbpt_1+(nbcaseX+1)*(j+2),nbpt_4+(nbcaseZ-1)*(j+2)-1,nbpt_1+(nbcaseX+1)*(j+1));
  184. a2ri_vf_add_face(primitive,nbpt_1+(nbcaseX+1)*(j+1),nbpt_4+(nbcaseZ-1)*(j+2)-1,nbpt_4+(nbcaseZ-1)*(j+1)-1);
  185. a2ri_vf_add_face(primitive,(nbcaseX+1)*(j+2)+nbcaseX,nbpt_5+(nbcaseZ-1)*j,(nbcaseX+1)*(j+1)+nbcaseX);
  186. a2ri_vf_add_face(primitive,(nbcaseX+1)*(j+2)+nbcaseX,nbpt_5+(nbcaseZ-1)*(j+1),nbpt_5+(nbcaseZ-1)*j);
  187. a2ri_vf_add_face(primitive,nbpt_1+(nbcaseX+1)*(j+2)+nbcaseX,nbpt_1+(nbcaseX+1)*(j+1)+nbcaseX,nbpt_5+(nbcaseZ-1)*(j+2)-1);
  188. a2ri_vf_add_face(primitive,nbpt_1+(nbcaseX+1)*(j+1)+nbcaseX,nbpt_5+(nbcaseZ-1)*(j+1)-1,nbpt_5+(nbcaseZ-1)*(j+2)-1);
  189. }
  190. }
  191. else
  192. {
  193. if(nbcaseY==1 && nbcaseZ>1)
  194. {
  195. a2ri_vf_add_face(primitive,nbcaseX+1,0,nbpt_2);
  196. a2ri_vf_add_face(primitive,nbcaseX+1,nbpt_2,nbpt_3);
  197. a2ri_vf_add_face(primitive,2*nbcaseX+1,nbpt_2+nbcaseX,nbcaseX);
  198. a2ri_vf_add_face(primitive,2*nbcaseX+1,nbpt_3+nbcaseX,nbpt_2+nbcaseX);
  199. for(int i=0;i<nbcaseZ-2;i++)
  200. {
  201. a2ri_vf_add_face(primitive,nbpt_3+i*(nbcaseX+1),nbpt_2+(i+1)*(nbcaseX+1),nbpt_3+(i+1)*(nbcaseX+1));
  202. a2ri_vf_add_face(primitive,nbpt_3+i*(nbcaseX+1),nbpt_2+i*(nbcaseX+1),nbpt_2+(i+1)*(nbcaseX+1));
  203. a2ri_vf_add_face(primitive,nbpt_3+i*(nbcaseX+1)+nbcaseX,nbpt_3+(i+1)*(nbcaseX+1)+nbcaseX,nbpt_2+(i+1)*(nbcaseX+1)+nbcaseX);
  204. a2ri_vf_add_face(primitive,nbpt_3+i*(nbcaseX+1)+nbcaseX,nbpt_2+(i+1)*(nbcaseX+1)+nbcaseX,nbpt_2+i*(nbcaseX+1)+nbcaseX);
  205. }
  206. a2ri_vf_add_face(primitive,nbpt_3+(nbcaseZ-2)*(nbcaseX+1),nbpt_2+(nbcaseZ-2)*(nbcaseX+1),nbpt_1);
  207. a2ri_vf_add_face(primitive,nbpt_3+(nbcaseZ-2)*(nbcaseX+1),nbpt_1,nbpt_1+nbcaseX+1);
  208. a2ri_vf_add_face(primitive,nbpt_3+nbcaseX+(nbcaseZ-2)*(nbcaseX+1),nbpt_1+nbcaseX,nbpt_2+nbcaseX+(nbcaseZ-2)*(nbcaseX+1));
  209. a2ri_vf_add_face(primitive,nbpt_3+nbcaseX+(nbcaseZ-2)*(nbcaseX+1),nbpt_1+nbcaseX*2+1,nbpt_1+nbcaseX);
  210. }
  211. if(nbcaseY>1 && nbcaseZ==1)
  212. {
  213. for(int i=0;i<nbcaseY;i++)
  214. {
  215. a2ri_vf_add_face(primitive,i*(nbcaseX+1),nbpt_1+i*(nbcaseX+1),(i+1)*(nbcaseX+1));
  216. a2ri_vf_add_face(primitive,(i+1)*(nbcaseX+1),nbpt_1+i*(nbcaseX+1),nbpt_1+(i+1)*(nbcaseX+1));
  217. a2ri_vf_add_face(primitive,i*(nbcaseX+1)+nbcaseX,(i+1)*(nbcaseX+1)+nbcaseX,nbpt_1+i*(nbcaseX+1)+nbcaseX);
  218. a2ri_vf_add_face(primitive,(i+1)*(nbcaseX+1)+nbcaseX,nbpt_1+(i+1)*(nbcaseX+1)+nbcaseX,nbpt_1+i*(nbcaseX+1)+nbcaseX);
  219. }
  220. }
  221. if(nbcaseY==1 && nbcaseZ==1)
  222. {
  223. a2ri_vf_add_face(primitive,nbcaseX+1,0,nbpt_1);
  224. a2ri_vf_add_face(primitive,nbcaseX+1,nbpt_1,nbpt_1+nbcaseX+1);
  225. a2ri_vf_add_face(primitive,2*nbcaseX+1,nbpt_1+nbcaseX,nbcaseX);
  226. a2ri_vf_add_face(primitive,2*nbcaseX+1,nbpt_1+nbcaseX*2+1,nbpt_1+nbcaseX);
  227. }
  228. }
  229. if(nbcaseY>=2 && nbcaseZ>=2)
  230. {
  231. a2ri_vf_add_face(primitive,0,nbpt_2,nbcaseX+1);
  232. a2ri_vf_add_face(primitive,nbcaseX+1,nbpt_2,nbpt_4);
  233. a2ri_vf_add_face(primitive,(nbcaseX+1)*nbcaseY,(nbcaseX+1)*(nbcaseY-1),nbpt_4+(nbcaseZ-1)*(nbcaseY-2));
  234. a2ri_vf_add_face(primitive,(nbcaseX+1)*nbcaseY,nbpt_4+(nbcaseZ-1)*(nbcaseY-2),nbpt_3);
  235. a2ri_vf_add_face(primitive,nbcaseX,nbcaseX+1+nbcaseX,nbpt_2+nbcaseX);
  236. a2ri_vf_add_face(primitive,nbcaseX+1+nbcaseX,nbpt_5,nbpt_2+nbcaseX);
  237. a2ri_vf_add_face(primitive,(nbcaseX+1)*nbcaseY+nbcaseX,nbpt_5+(nbcaseZ-1)*(nbcaseY-2),(nbcaseX+1)*(nbcaseY-1)+nbcaseX);
  238. a2ri_vf_add_face(primitive,(nbcaseX+1)*nbcaseY+nbcaseX,nbpt_3+nbcaseX,nbpt_5+(nbcaseZ-1)*(nbcaseY-2));
  239. a2ri_vf_add_face(primitive,nbpt_4+nbcaseZ-2,nbpt_1,nbpt_1+nbcaseX+1);
  240. a2ri_vf_add_face(primitive,nbpt_2+(nbcaseX+1)*(nbcaseZ-2),nbpt_1,nbpt_4+nbcaseZ-2);
  241. a2ri_vf_add_face(primitive,nbpt_3+(nbcaseX+1)*(nbcaseZ-2),nbpt_4+(nbcaseZ-1)*(nbcaseY-1)-1,nbpt_1+(nbcaseX+1)*(nbcaseY-1));
  242. a2ri_vf_add_face(primitive,nbpt_1+(nbcaseX+1)*(nbcaseY),nbpt_3+(nbcaseX+1)*(nbcaseZ-2),nbpt_1+(nbcaseX+1)*(nbcaseY-1));
  243. a2ri_vf_add_face(primitive,nbpt_5+nbcaseZ-2,nbpt_1+nbcaseX+1+nbcaseX,nbpt_1+nbcaseX);
  244. a2ri_vf_add_face(primitive,nbpt_2+(nbcaseX+1)*(nbcaseZ-2)+nbcaseX,nbpt_5+nbcaseZ-2,nbpt_1+nbcaseX);
  245. a2ri_vf_add_face(primitive,nbpt_3+(nbcaseX+1)*(nbcaseZ-2)+nbcaseX,nbpt_1+(nbcaseX+1)*(nbcaseY-1)+nbcaseX,nbpt_5+(nbcaseZ-1)*(nbcaseY-1)-1);
  246. a2ri_vf_add_face(primitive,nbpt_1+(nbcaseX+1)*(nbcaseY)+nbcaseX,nbpt_1+(nbcaseX+1)*(nbcaseY-1)+nbcaseX,nbpt_3+(nbcaseX+1)*(nbcaseZ-2)+nbcaseX);
  247. }
  248. }
  249. void
  250. gen_icosphere(char* argv[], vf_model *primitive)
  251. {
  252. double rayon=atof(argv[2]);
  253. int subdivision=atoi(argv[3]);
  254. vector3d p;
  255. a2ri_vf_add_vertex(primitive,0.000000,-0.525731,0.850651);
  256. a2ri_vf_add_vertex(primitive,0.000000,0.525731,0.850651);
  257. a2ri_vf_add_vertex(primitive,0.000000,-0.525731,-0.850651);
  258. a2ri_vf_add_vertex(primitive,0.000000,0.525731,-0.850651);
  259. a2ri_vf_add_vertex(primitive,0.850651,0.000000,0.525731);
  260. a2ri_vf_add_vertex(primitive,0.850651,0.000000,-0.525731);
  261. a2ri_vf_add_vertex(primitive,-0.850651,0.000000,0.525731);
  262. a2ri_vf_add_vertex(primitive,-0.850651,0.000000,-0.525731);
  263. a2ri_vf_add_vertex(primitive,0.525731,0.850651,0.000000);
  264. a2ri_vf_add_vertex(primitive,0.525731,-0.850651,0.000000);
  265. a2ri_vf_add_vertex(primitive,-0.525731,0.850651,0.000000);
  266. a2ri_vf_add_vertex(primitive,-0.525731,-0.850651,0.000000);
  267. a2ri_vf_add_face(primitive,0,4,1);
  268. a2ri_vf_add_face(primitive,0,9,4);
  269. a2ri_vf_add_face(primitive,9,5,4);
  270. a2ri_vf_add_face(primitive,4,5,8);
  271. a2ri_vf_add_face(primitive,4,8,1);
  272. a2ri_vf_add_face(primitive,8,10,1);
  273. a2ri_vf_add_face(primitive,8,3,10);
  274. a2ri_vf_add_face(primitive,5,3,8);
  275. a2ri_vf_add_face(primitive,5,2,3);
  276. a2ri_vf_add_face(primitive,2,7,3);
  277. a2ri_vf_add_face(primitive,7,10,3);
  278. a2ri_vf_add_face(primitive,7,6,10);
  279. a2ri_vf_add_face(primitive,7,11,6);
  280. a2ri_vf_add_face(primitive,11,0,6);
  281. a2ri_vf_add_face(primitive,0,1,6);
  282. a2ri_vf_add_face(primitive,6,1,10);
  283. a2ri_vf_add_face(primitive,9,0,11);
  284. a2ri_vf_add_face(primitive,9,11,2);
  285. a2ri_vf_add_face(primitive,9,2,5);
  286. a2ri_vf_add_face(primitive,7,2,11);
  287. a2ri_vf_4_subdivision(primitive,subdivision-1);
  288. for(int i=0;i<primitive->nbvertex;i++)
  289. {
  290. vector3d_init(&p,primitive->ve[i].x,primitive->ve[i].y,primitive->ve[i].z);
  291. vector3d_normalize(&p);
  292. primitive->ve[i].x=p.dx*rayon;
  293. primitive->ve[i].y=p.dy*rayon;
  294. primitive->ve[i].z=p.dz*rayon;
  295. }
  296. }
  297. void
  298. gen_uvsphere(char* argv[], vf_model *primitive)
  299. {
  300. double rayon=atof(argv[2]);
  301. int segment=atoi(argv[3]),anneau=atoi(argv[4]);
  302. if(segment<3 || anneau<3)
  303. {
  304. printf("impossible de creer une sphere avec moins de 3 anneaux segments\n");
  305. exit(EXIT_FAILURE);
  306. }
  307. for(int j=1;j<anneau;j++)
  308. {
  309. double anglecourant=j*PI/(anneau*1.);
  310. double rayoncourant=sin(anglecourant)*rayon;
  311. double hauteurcourante=-cos(anglecourant)*rayon;
  312. for(int i=0;i<segment;i++)
  313. a2ri_vf_add_vertex(primitive,rayoncourant*cos(i*(2*PI/(segment*1.))),hauteurcourante,rayoncourant*sin(i*(2*PI/(segment*1.))));
  314. }
  315. for(int j=0;j<anneau-2;j++)
  316. for(int i=0;i<segment;i++)
  317. {
  318. //printf("creation face : %d %d %d\n",j*segment+i,(j+1)*segment+i,j*segment+((i+1)%segment));
  319. a2ri_vf_add_face(primitive,j*segment+i,(j+1)*segment+i,j*segment+((i+1)%segment));
  320. //printf("creation face : %d %d %d\n",j*segment+((i+1)%segment),(j+1)*segment+i,(j+1)*segment+((i+1)%segment));
  321. a2ri_vf_add_face(primitive,j*segment+((i+1)%segment),(j+1)*segment+i,(j+1)*segment+((i+1)%segment));
  322. }
  323. a2ri_vf_add_vertex(primitive,0,-rayon,0);
  324. a2ri_vf_add_vertex(primitive,0,rayon,0);
  325. for(int i=0;i<segment;i++)
  326. {
  327. a2ri_vf_add_face(primitive,i,(i+1)%segment,segment*(anneau-1));
  328. a2ri_vf_add_face(primitive,(anneau-2)*segment+i,segment*(anneau-1)+1,(anneau-2)*segment+(i+1)%segment);
  329. }
  330. }
  331. void
  332. gen_cylindre(char* argv[], vf_model *primitive)
  333. {
  334. double rayon=atof(argv[2]),hauteur=atof(argv[3]);
  335. int segment=atoi(argv[4]),anneau=atoi(argv[5]);
  336. double delta=hauteur/(anneau*1.);
  337. if(segment<3)
  338. {
  339. printf("impossible de construire une cylindre avec moins de 3 segments\n");
  340. exit(EXIT_SUCCESS);
  341. }
  342. for(int j=0;j<=anneau;j++)
  343. for(int i=0;i<segment;i++)
  344. a2ri_vf_add_vertex(primitive,rayon*cos(i*(2*PI/(segment*1.))),j*delta,rayon*sin(i*(2*PI/(segment*1.))));
  345. for(int j=0;j<anneau;j++)
  346. for(int i=0;i<segment;i++)
  347. {
  348. //printf("creation face : %d %d %d\n",j*segment+i,(j+1)*segment+i,j*segment+((i+1)%segment));
  349. a2ri_vf_add_face(primitive,j*segment+i,(j+1)*segment+i,j*segment+((i+1)%segment));
  350. //printf("creation face : %d %d %d\n",j*segment+((i+1)%segment),(j+1)*segment+i,(j+1)*segment+((i+1)%segment));
  351. a2ri_vf_add_face(primitive,j*segment+((i+1)%segment),(j+1)*segment+i,(j+1)*segment+((i+1)%segment));
  352. }
  353. a2ri_vf_add_vertex(primitive,0,0,0);
  354. a2ri_vf_add_vertex(primitive,0,hauteur,0);
  355. for(int i=0;i<segment;i++)
  356. {
  357. a2ri_vf_add_face(primitive,i,(i+1)%segment,segment*(anneau+1));
  358. a2ri_vf_add_face(primitive,anneau*segment+i,segment*(anneau+1)+1,anneau*segment+(i+1)%segment);
  359. }
  360. }
  361. void
  362. gen_cone(char* argv[], vf_model *primitive)
  363. {
  364. double rayon=atof(argv[2]),hauteur=atof(argv[3]);
  365. int segment=atoi(argv[4]),anneau=atoi(argv[5]);
  366. double delta=hauteur/(anneau*1.),delta_rayon=rayon/(anneau*1.);
  367. if(segment<3)
  368. {
  369. printf("impossible de construire une cone avec moins de 3 segments\n");
  370. exit(EXIT_SUCCESS);
  371. }
  372. for(int j=0;j<anneau;j++)
  373. for(int i=0;i<segment;i++)
  374. a2ri_vf_add_vertex(primitive,(rayon-(j*delta_rayon))*cos(i*(2*PI/(segment*1.))),j*delta,(rayon-(j*delta_rayon))*sin(i*(2*PI/(segment*1.))));
  375. a2ri_vf_add_vertex(primitive,0,hauteur,0);
  376. for(int j=0;j<anneau-1;j++)
  377. for(int i=0;i<segment;i++)
  378. {
  379. //printf("creation face : %d %d %d\n",j*segment+i,(j+1)*segment+i,j*segment+((i+1)%segment));
  380. a2ri_vf_add_face(primitive,j*segment+i,(j+1)*segment+i,j*segment+((i+1)%segment));
  381. //printf("creation face : %d %d %d\n",j*segment+((i+1)%segment),(j+1)*segment+i,(j+1)*segment+((i+1)%segment));
  382. a2ri_vf_add_face(primitive,j*segment+((i+1)%segment),(j+1)*segment+i,(j+1)*segment+((i+1)%segment));
  383. }
  384. for(int i=0;i<segment;i++)
  385. a2ri_vf_add_face(primitive,(anneau-1)*segment+i,segment*anneau,(anneau-1)*segment+(i+1)%segment);
  386. a2ri_vf_add_vertex(primitive,0,0,0);
  387. for(int i=0;i<segment;i++)
  388. a2ri_vf_add_face(primitive,i,(i+1)%segment,segment*anneau+1);
  389. }
  390. /*************************************************************/
  391. int main(int argc, char* argv[])
  392. {
  393. int arg_ok=0;
  394. vf_model primitive;
  395. a2ri_vf_init(&primitive);
  396. if(argc<4)
  397. usage(argv,"general");
  398. a2ri_vf_init(&primitive);
  399. if(!strcmp(argv[1],"grille"))
  400. {
  401. arg_ok=1;
  402. if(argc!=7)
  403. usage(argv,"grille");
  404. gen_grille(argv,&primitive);
  405. }
  406. if(!strcmp(argv[1],"boite"))
  407. {
  408. arg_ok=1;
  409. if(argc!=9)
  410. usage(argv,"boite");
  411. gen_boite(argv,&primitive);
  412. }
  413. if(!strcmp(argv[1],"icosphere"))
  414. {
  415. arg_ok=1;
  416. if(argc!=5)
  417. usage(argv,"icosphere");
  418. gen_icosphere(argv,&primitive);
  419. }
  420. if(!strcmp(argv[1],"uvsphere"))
  421. {
  422. arg_ok=1;
  423. if(argc!=6)
  424. usage(argv,"uvsphere");
  425. gen_uvsphere(argv,&primitive);
  426. }
  427. if(!strcmp(argv[1],"cylindre"))
  428. {
  429. arg_ok=1;
  430. if(argc!=7)
  431. usage(argv,"cylindre");
  432. gen_cylindre(argv,&primitive);
  433. }
  434. if(!strcmp(argv[1],"cone"))
  435. {
  436. arg_ok=1;
  437. if(argc!=7)
  438. usage(argv,"cone");
  439. gen_cone(argv,&primitive);
  440. }
  441. if(!arg_ok)
  442. usage(argv,"general");
  443. a2ri_vf_save_file(argv[argc-1],&primitive);
  444. exit(EXIT_SUCCESS);
  445. }