opengl.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. #include "opengl.h"
  2. #include <a2ri/triangulation.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. vef_model *tomove;
  6. vef_model *todisplay;
  7. vef_model *GG,*NNG,*EMST,*EGG;
  8. vef_model *tetraedrisation;
  9. int zoom=0;
  10. float rinc;
  11. float tinc;
  12. int initx,inity,rotation,deplacement;
  13. double angle,angle2,longdiag;
  14. void
  15. init (int argc, char **argv)
  16. {
  17. int i, j;
  18. glutInit (&argc, argv);
  19. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  20. glutInitWindowSize (WIDTH, HEIGHT);
  21. glutInitWindowPosition (100, 25);
  22. glutCreateWindow ("Construction de graphes");
  23. glClearColor (0.0, 0.0, 0.0, 0.0);
  24. glEnable(GL_DEPTH_TEST);
  25. glViewport (0, 0, (GLsizei)WIDTH, (GLsizei)HEIGHT);
  26. rx = ry = rz = tx = ty = tz = 0;
  27. for (i=0; i < NB_VAL; i++)
  28. for (j=0; j < 6; j++)
  29. hist [i][j] = 0;
  30. }
  31. void
  32. display_triangles()
  33. {
  34. point3d p1,p2;
  35. point3d_init(&p1,todisplay->xmin,todisplay->ymin,todisplay->zmin);
  36. point3d_init(&p2,todisplay->xmax,todisplay->ymax,todisplay->zmax);
  37. double longdiag=point3d_length(&p1,&p2);
  38. if(todisplay->nbface>0)
  39. for(int i=0;i<todisplay->nbface;i++)
  40. {
  41. int ve1,ve2,ve3;
  42. vef_face_get_vertices(&(todisplay->fa[i]),todisplay->ed,&ve1,&ve2,&ve3);
  43. glBegin(GL_POLYGON);
  44. glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
  45. glVertex3f(todisplay->ve[ve1].x,
  46. todisplay->ve[ve1].y,
  47. todisplay->ve[ve1].z);
  48. glVertex3f(todisplay->ve[ve2].x,
  49. todisplay->ve[ve2].y,
  50. todisplay->ve[ve2].z);
  51. glVertex3f(todisplay->ve[ve3].x,
  52. todisplay->ve[ve3].y,
  53. todisplay->ve[ve3].z);
  54. glEnd();
  55. glLineWidth(2);
  56. glBegin(GL_LINES);
  57. glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
  58. glVertex3f(todisplay->ve[ve1].x,
  59. todisplay->ve[ve1].y,
  60. todisplay->ve[ve1].z);
  61. glVertex3f(todisplay->ve[ve2].x,
  62. todisplay->ve[ve2].y,
  63. todisplay->ve[ve2].z);
  64. glVertex3f(todisplay->ve[ve2].x,
  65. todisplay->ve[ve2].y,
  66. todisplay->ve[ve2].z);
  67. glVertex3f(todisplay->ve[ve3].x,
  68. todisplay->ve[ve3].y,
  69. todisplay->ve[ve3].z);
  70. glVertex3f(todisplay->ve[ve3].x,
  71. todisplay->ve[ve3].y,
  72. todisplay->ve[ve3].z);
  73. glVertex3f(todisplay->ve[ve1].x,
  74. todisplay->ve[ve1].y,
  75. todisplay->ve[ve1].z);
  76. glEnd();
  77. }
  78. else
  79. {
  80. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  81. for(int i=0;i<todisplay->nbvertex;i++)
  82. {
  83. glTranslatef(todisplay->ve[i].x,todisplay->ve[i].y,todisplay->ve[i].z);
  84. glutWireCube(longdiag/400.0);
  85. glTranslatef(-todisplay->ve[i].x,-todisplay->ve[i].y,-todisplay->ve[i].z);
  86. }
  87. for(int i=0;i<todisplay->nbedge;i++)
  88. {
  89. glBegin(GL_LINES);
  90. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  91. glVertex3f(todisplay->ve[todisplay->ed[i].ve1].x,
  92. todisplay->ve[todisplay->ed[i].ve1].y,
  93. todisplay->ve[todisplay->ed[i].ve1].z);
  94. glVertex3f(todisplay->ve[todisplay->ed[i].ve2].x,
  95. todisplay->ve[todisplay->ed[i].ve2].y,
  96. todisplay->ve[todisplay->ed[i].ve2].z);
  97. glEnd();
  98. }
  99. }
  100. glFlush();
  101. }
  102. void
  103. transform (int i)
  104. {
  105. glTranslatef((todisplay->xmin+todisplay->xmax)/2.0,
  106. (todisplay->ymin+todisplay->ymax)/2.0,
  107. (todisplay->zmin+todisplay->zmax)/2.0);
  108. glRotatef (hist[i][0], 1.0, 0.0, 0.0);
  109. glRotatef (hist[i][1], 0.0, 1.0, 0.0);
  110. glRotatef (hist[i][2], 0.0, 0.0, 1.0);
  111. glTranslatef(-(todisplay->xmin+todisplay->xmax)/2.0,
  112. -(todisplay->ymin+todisplay->ymax)/2.0,
  113. -(todisplay->zmin+todisplay->zmax)/2.0);
  114. }
  115. void
  116. display ()
  117. {
  118. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  119. glMatrixMode (GL_PROJECTION);
  120. glLoadIdentity ();
  121. gluPerspective (45, WIDTH/((float)HEIGHT), 0.1, 900);
  122. glMatrixMode (GL_MODELVIEW);
  123. glLoadIdentity ();
  124. if(todisplay->zmax==0)
  125. gluLookAt (((todisplay->xmin+todisplay->xmax)/2.0)+tx,
  126. ((todisplay->ymin+todisplay->ymax)/2.0)+ty,
  127. 70+tz,
  128. ((todisplay->xmin+todisplay->xmax)/2.0)+tx,
  129. ((todisplay->ymin+todisplay->ymax)/2.0)+ty,
  130. ((todisplay->zmin+todisplay->zmax)/2.0)+tz,
  131. 0, 1, 0);
  132. else
  133. gluLookAt (((todisplay->xmin+todisplay->xmax)/2.0)+tx,
  134. ((todisplay->ymin+todisplay->ymax)/2.0)+ty,
  135. todisplay->zmax*1.5+tz,
  136. ((todisplay->xmin+todisplay->xmax)/2.0)+tx,
  137. ((todisplay->ymin+todisplay->ymax)/2.0)+ty,
  138. ((todisplay->zmin+todisplay->zmax)/2.0)+tz,
  139. 0, 1, 0);
  140. transform (0);
  141. glTranslatef((todisplay->xmin+todisplay->xmax)/2.0,
  142. (todisplay->ymin+todisplay->ymax)/2.0,
  143. (todisplay->zmin+todisplay->zmax)/2.0);
  144. glRotatef(angle,0.0,1.0,0.0);
  145. glRotatef(angle2,1.0,0.0,0.0);
  146. glTranslatef(-(todisplay->xmin+todisplay->xmax)/2.0,
  147. -(todisplay->ymin+todisplay->ymax)/2.0,
  148. -(todisplay->zmin+todisplay->zmax)/2.0);
  149. display_triangles();
  150. glutSwapBuffers ();
  151. }
  152. void
  153. exitFunc ()
  154. {
  155. a2ri_vef_free(tomove);
  156. if(GG!=NULL)
  157. a2ri_vef_free(GG);
  158. if(NNG!=NULL)
  159. a2ri_vef_free(NNG);
  160. if(EMST!=NULL)
  161. a2ri_vef_free(EMST);
  162. if(EGG!=NULL)
  163. a2ri_vef_free(EGG);
  164. exit (EXIT_SUCCESS);
  165. }
  166. void mouse(int button, int state, int x, int y)
  167. {
  168. if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
  169. {
  170. rotation=1;
  171. initx=x;
  172. inity=y;
  173. }
  174. if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
  175. rotation=0;
  176. if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
  177. {
  178. deplacement=1;
  179. initx=x;
  180. inity=y;
  181. }
  182. if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
  183. deplacement=0;
  184. if(button==GLUT_WHEEL_MOUSE_UP)
  185. tz+=longdiag/50;
  186. if(button==GLUT_WHEEL_MOUSE_DOWN)
  187. tz-=longdiag/50;
  188. }
  189. void motion(int x, int y)
  190. {
  191. if(rotation)
  192. {
  193. angle+=(x-initx)*0.5;
  194. angle2+=(y-inity)*0.5;
  195. initx=x;
  196. inity=y;
  197. glutPostRedisplay();
  198. }
  199. if(deplacement)
  200. {
  201. tx-=(x-initx)*0.001*longdiag;
  202. ty+=(y-inity)*0.001*longdiag;
  203. initx=x;
  204. inity=y;
  205. glutPostRedisplay();
  206. }
  207. }
  208. void
  209. keyboard (unsigned char key, int x, int y)
  210. {
  211. int i, j;
  212. time_t dep,fin;
  213. switch (key)
  214. {
  215. case 'a':
  216. case 'A':
  217. if(NNG==NULL)
  218. {
  219. dep=clock();
  220. NNG=a2ri_vef_nearest_neighbour_graph(tomove);
  221. fin=clock();
  222. printf("temps d'execution : %lf\n",((fin-dep)*1.0)/(CLOCKS_PER_SEC*1.0));
  223. }
  224. todisplay=NNG;
  225. break;
  226. case 'z':
  227. case 'Z':
  228. if(EMST==NULL)
  229. {
  230. dep=clock();
  231. EMST=a2ri_vef_euclidean_minimal_spanning_tree(tomove);
  232. fin=clock();
  233. printf("temps d'execution : %lf\n",((fin-dep)*1.0)/(CLOCKS_PER_SEC*1.0));
  234. }
  235. todisplay=EMST;
  236. break;
  237. case 'e':
  238. case 'E':
  239. if(GG==NULL)
  240. {
  241. dep=clock();
  242. GG=a2ri_vef_gabriel_graph(tomove);
  243. fin=clock();
  244. printf("temps d'execution : %lf\n",((fin-dep)*1.0)/(CLOCKS_PER_SEC*1.0));
  245. }
  246. todisplay=GG;
  247. break;
  248. case 'r':
  249. case 'R':
  250. if(EGG==NULL)
  251. {
  252. dep=clock();
  253. EGG=a2ri_vef_extended_gabriel_hypergraph(tomove);
  254. fin=clock();
  255. printf("temps d'execution : %lf\n",((fin-dep)*1.0)/(CLOCKS_PER_SEC*1.0));
  256. }
  257. todisplay=EGG;
  258. break;
  259. case 's':
  260. case 'S':
  261. todisplay=tomove;
  262. break;
  263. case 'Q':
  264. case 'q':
  265. case 27:
  266. exitFunc ();
  267. break;
  268. }
  269. for (i=NB_VAL-1; i > 0; i--)
  270. for (j=0; j < 6; j++)
  271. hist [i][j] = hist [i-1][j];
  272. hist [0][0] = rx;
  273. hist [0][1] = ry;
  274. hist [0][2] = rz;
  275. hist [0][3] = tx;
  276. hist [0][4] = ty;
  277. hist [0][5] = tz;
  278. }
  279. void
  280. idleFunc ()
  281. {
  282. int i, j;
  283. for (i=NB_VAL-1; i > 0; i--)
  284. for (j=0; j < 6; j++)
  285. hist [i][j] = hist [i-1][j];
  286. glutPostRedisplay ();
  287. }
  288. int
  289. go (int argc, char **argv, vef_model *m)
  290. {
  291. point3d ptmin,ptmax;
  292. tomove=m;
  293. todisplay=m;
  294. GG=NULL;
  295. NNG=NULL;
  296. EMST=NULL;
  297. EGG=NULL;
  298. tetraedrisation=NULL;
  299. rinc=R_INC;
  300. tinc=T_INC;
  301. if(tomove->zmax-tomove->zmin!=0)
  302. tinc=(tomove->zmax-tomove->zmin)/10;
  303. ptmin.x=tomove->xmin;
  304. ptmin.y=tomove->ymin;
  305. ptmin.z=tomove->zmin;
  306. ptmax.x=tomove->xmax;
  307. ptmax.y=tomove->ymax;
  308. ptmax.z=tomove->zmax;
  309. longdiag=point3d_length(&ptmin,&ptmax);
  310. init (argc, argv);
  311. glutDisplayFunc (display);
  312. glutMouseFunc(mouse);
  313. glutMotionFunc(motion);
  314. glutKeyboardFunc (keyboard);
  315. glutIdleFunc (idleFunc);
  316. glutMainLoop ();
  317. return EXIT_SUCCESS;
  318. }