opengl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <gsl/gsl_linalg.h>
  4. #include <gsl/gsl_math.h>
  5. #include <gsl/gsl_blas.h>
  6. #include "opengl.h"
  7. #include <a2ri/subdivision.h>
  8. #include <a2ri/icp.h>
  9. #include <a2ri/io.h>
  10. #include <a2ri/boundingbox.h>
  11. #include <a2ri/overlap.h>
  12. #include <time.h>
  13. vf_model *modeltodisplay,*tomove;
  14. int zoom=0;
  15. float rinc;
  16. float tinc;
  17. double recouvrement,sensibilite;
  18. int *facesmodeltodisplay, *facestomove;
  19. point3d *ptmin_aabb,*ptmax_aabb,*ptmin_obb,*ptmax_obb;
  20. vector3d *nvbasex,*nvbasey,*nvbasez;
  21. int affich_aabb,affich_obb,affich_ball_exacte,affich_ball_ritter;
  22. double dkarret;
  23. double perttx=0.0,pertty=0.0,perttz=0.0,pertrotx=0.0,pertroty=0.0,pertrotz=0.0;
  24. int initx,inity,rotation,deplacement;
  25. double angle,angle2,longdiag;
  26. int *liste_vert=NULL;
  27. int *liste_rouge=NULL;
  28. int size_liste_couleur=0;
  29. int affich_tomove,affich_display;
  30. void
  31. init (int argc, char **argv)
  32. {
  33. int i, j;
  34. glutInit (&argc, argv);
  35. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  36. glutInitWindowSize (WIDTH, HEIGHT);
  37. glutInitWindowPosition (100, 25);
  38. glutCreateWindow ("Automated Trimmed Iterative Closest Point");
  39. glClearColor (255.0, 255.0, 255.0, 0.0);
  40. glEnable(GL_DEPTH_TEST);
  41. glViewport (0, 0, (GLsizei)WIDTH, (GLsizei)HEIGHT);
  42. rx = ry = rz = tx = ty = tz = 0;
  43. for (i=0; i < NB_VAL; i++)
  44. for (j=0; j < 6; j++)
  45. hist [i][j] = 0;
  46. }
  47. void
  48. display_triangles()
  49. {
  50. if(affich_tomove)
  51. {
  52. for(int i=0;i<tomove->nbface;i++)
  53. {
  54. int ve1=tomove->fa[i].ve1;
  55. int ve2=tomove->fa[i].ve2;
  56. int ve3=tomove->fa[i].ve3;
  57. glBegin(GL_POLYGON);
  58. glColor4f(0.3f, 0.3f, 0.3f, 1.0f);
  59. if(liste_vert!=NULL)
  60. glColor4f((liste_rouge[ve1]*1.0)/255.0, (liste_vert[ve1]*1.0)/255.0, 0.0f, 1.0f);
  61. glVertex3f(tomove->ve[ve1].x,
  62. tomove->ve[ve1].y,
  63. tomove->ve[ve1].z);
  64. if(liste_vert!=NULL)
  65. glColor4f((liste_rouge[ve2]*1.0)/255.0, (liste_vert[ve2]*1.0)/255.0, 0.0f, 1.0f);
  66. glVertex3f(tomove->ve[ve2].x,
  67. tomove->ve[ve2].y,
  68. tomove->ve[ve2].z);
  69. if(liste_vert!=NULL)
  70. glColor4f((liste_rouge[ve3]*1.0)/255.0, (liste_vert[ve3]*1.0)/255.0, 0.0f, 1.0f);
  71. glVertex3f(tomove->ve[ve3].x,
  72. tomove->ve[ve3].y,
  73. tomove->ve[ve3].z);
  74. glEnd();
  75. glLineWidth(2);
  76. glBegin(GL_LINES);
  77. glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
  78. glVertex3f(tomove->ve[ve1].x,
  79. tomove->ve[ve1].y,
  80. tomove->ve[ve1].z);
  81. glVertex3f(tomove->ve[ve2].x,
  82. tomove->ve[ve2].y,
  83. tomove->ve[ve2].z);
  84. glVertex3f(tomove->ve[ve2].x,
  85. tomove->ve[ve2].y,
  86. tomove->ve[ve2].z);
  87. glVertex3f(tomove->ve[ve3].x,
  88. tomove->ve[ve3].y,
  89. tomove->ve[ve3].z);
  90. glVertex3f(tomove->ve[ve3].x,
  91. tomove->ve[ve3].y,
  92. tomove->ve[ve3].z);
  93. glVertex3f(tomove->ve[ve1].x,
  94. tomove->ve[ve1].y,
  95. tomove->ve[ve1].z);
  96. glEnd();
  97. }
  98. }
  99. if(affich_display)
  100. {
  101. for(int i=0;i<modeltodisplay->nbface;i++)
  102. {
  103. int ve1=modeltodisplay->fa[i].ve1;
  104. int ve2=modeltodisplay->fa[i].ve2;
  105. int ve3=modeltodisplay->fa[i].ve3;
  106. glBegin(GL_POLYGON);
  107. glColor4f(0.7f, 0.7f, 0.7f, 1.0f);
  108. glVertex3f(modeltodisplay->ve[ve1].x,
  109. modeltodisplay->ve[ve1].y,
  110. modeltodisplay->ve[ve1].z);
  111. glVertex3f(modeltodisplay->ve[ve2].x,
  112. modeltodisplay->ve[ve2].y,
  113. modeltodisplay->ve[ve2].z);
  114. glVertex3f(modeltodisplay->ve[ve3].x,
  115. modeltodisplay->ve[ve3].y,
  116. modeltodisplay->ve[ve3].z);
  117. glEnd();
  118. glLineWidth(2);
  119. glBegin(GL_LINES);
  120. glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
  121. glVertex3f(modeltodisplay->ve[ve1].x,
  122. modeltodisplay->ve[ve1].y,
  123. modeltodisplay->ve[ve1].z);
  124. glVertex3f(modeltodisplay->ve[ve2].x,
  125. modeltodisplay->ve[ve2].y,
  126. modeltodisplay->ve[ve2].z);
  127. glVertex3f(modeltodisplay->ve[ve2].x,
  128. modeltodisplay->ve[ve2].y,
  129. modeltodisplay->ve[ve2].z);
  130. glVertex3f(modeltodisplay->ve[ve3].x,
  131. modeltodisplay->ve[ve3].y,
  132. modeltodisplay->ve[ve3].z);
  133. glVertex3f(modeltodisplay->ve[ve3].x,
  134. modeltodisplay->ve[ve3].y,
  135. modeltodisplay->ve[ve3].z);
  136. glVertex3f(modeltodisplay->ve[ve1].x,
  137. modeltodisplay->ve[ve1].y,
  138. modeltodisplay->ve[ve1].z);
  139. glEnd();
  140. }
  141. }
  142. glFlush();
  143. }
  144. void
  145. display_aabb()
  146. {
  147. double xmin,ymin,zmin,xmax,ymax,zmax;
  148. a2ri_vf_axis_aligned_bounding_box(modeltodisplay,facesmodeltodisplay,modeltodisplay->nbface,&ptmin_aabb,&ptmax_aabb);
  149. for(int i=0;i<modeltodisplay->nbface;i++)
  150. {
  151. xmin=ptmin_aabb[i].x;
  152. ymin=ptmin_aabb[i].y;
  153. zmin=ptmin_aabb[i].z;
  154. xmax=ptmax_aabb[i].x;
  155. ymax=ptmax_aabb[i].y;
  156. zmax=ptmax_aabb[i].z;
  157. glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
  158. glLineWidth(1);
  159. glBegin(GL_LINES);
  160. glVertex3f(xmin,ymin,zmin);
  161. glVertex3f(xmax,ymin,zmin);
  162. glVertex3f(xmax,ymin,zmin);
  163. glVertex3f(xmax,ymax,zmin);
  164. glVertex3f(xmax,ymax,zmin);
  165. glVertex3f(xmin,ymax,zmin);
  166. glVertex3f(xmin,ymax,zmin);
  167. glVertex3f(xmin,ymin,zmin);
  168. glVertex3f(xmax,ymin,zmin);
  169. glVertex3f(xmax,ymin,zmax);
  170. glVertex3f(xmax,ymin,zmax);
  171. glVertex3f(xmax,ymax,zmax);
  172. glVertex3f(xmax,ymax,zmax);
  173. glVertex3f(xmax,ymax,zmin);
  174. glVertex3f(xmax,ymax,zmin);
  175. glVertex3f(xmax,ymin,zmin);
  176. glVertex3f(xmin,ymin,zmin);
  177. glVertex3f(xmax,ymin,zmin);
  178. glVertex3f(xmax,ymin,zmin);
  179. glVertex3f(xmax,ymin,zmax);
  180. glVertex3f(xmax,ymin,zmax);
  181. glVertex3f(xmin,ymin,zmax);
  182. glVertex3f(xmin,ymin,zmax);
  183. glVertex3f(xmin,ymin,zmin);
  184. glVertex3f(xmin,ymin,zmax);
  185. glVertex3f(xmax,ymin,zmax);
  186. glVertex3f(xmax,ymin,zmax);
  187. glVertex3f(xmax,ymax,zmax);
  188. glVertex3f(xmax,ymax,zmax);
  189. glVertex3f(xmin,ymax,zmax);
  190. glVertex3f(xmin,ymax,zmax);
  191. glVertex3f(xmin,ymin,zmax);
  192. glVertex3f(xmin,ymin,zmin);
  193. glVertex3f(xmin,ymin,zmax);
  194. glVertex3f(xmin,ymin,zmax);
  195. glVertex3f(xmin,ymax,zmax);
  196. glVertex3f(xmin,ymax,zmax);
  197. glVertex3f(xmin,ymax,zmin);
  198. glVertex3f(xmin,ymax,zmin);
  199. glVertex3f(xmin,ymin,zmin);
  200. glVertex3f(xmin,ymax,zmin);
  201. glVertex3f(xmax,ymax,zmin);
  202. glVertex3f(xmax,ymax,zmin);
  203. glVertex3f(xmax,ymax,zmax);
  204. glVertex3f(xmax,ymax,zmax);
  205. glVertex3f(xmin,ymax,zmax);
  206. glVertex3f(xmin,ymax,zmax);
  207. glVertex3f(xmin,ymax,zmin);
  208. glEnd();
  209. }
  210. free(ptmin_aabb);
  211. free(ptmax_aabb);
  212. ptmin_aabb=NULL;
  213. ptmax_aabb=NULL;
  214. a2ri_vf_axis_aligned_bounding_box(tomove,facestomove,tomove->nbface,&ptmin_aabb,&ptmax_aabb);
  215. for(int i=0;i<tomove->nbface;i++)
  216. {
  217. xmin=ptmin_aabb[i].x;
  218. ymin=ptmin_aabb[i].y;
  219. zmin=ptmin_aabb[i].z;
  220. xmax=ptmax_aabb[i].x;
  221. ymax=ptmax_aabb[i].y;
  222. zmax=ptmax_aabb[i].z;
  223. glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
  224. glLineWidth(1);
  225. glBegin(GL_LINES);
  226. glVertex3f(xmin,ymin,zmin);
  227. glVertex3f(xmax,ymin,zmin);
  228. glVertex3f(xmax,ymin,zmin);
  229. glVertex3f(xmax,ymax,zmin);
  230. glVertex3f(xmax,ymax,zmin);
  231. glVertex3f(xmin,ymax,zmin);
  232. glVertex3f(xmin,ymax,zmin);
  233. glVertex3f(xmin,ymin,zmin);
  234. glVertex3f(xmax,ymin,zmin);
  235. glVertex3f(xmax,ymin,zmax);
  236. glVertex3f(xmax,ymin,zmax);
  237. glVertex3f(xmax,ymax,zmax);
  238. glVertex3f(xmax,ymax,zmax);
  239. glVertex3f(xmax,ymax,zmin);
  240. glVertex3f(xmax,ymax,zmin);
  241. glVertex3f(xmax,ymin,zmin);
  242. glVertex3f(xmin,ymin,zmin);
  243. glVertex3f(xmax,ymin,zmin);
  244. glVertex3f(xmax,ymin,zmin);
  245. glVertex3f(xmax,ymin,zmax);
  246. glVertex3f(xmax,ymin,zmax);
  247. glVertex3f(xmin,ymin,zmax);
  248. glVertex3f(xmin,ymin,zmax);
  249. glVertex3f(xmin,ymin,zmin);
  250. glVertex3f(xmin,ymin,zmax);
  251. glVertex3f(xmax,ymin,zmax);
  252. glVertex3f(xmax,ymin,zmax);
  253. glVertex3f(xmax,ymax,zmax);
  254. glVertex3f(xmax,ymax,zmax);
  255. glVertex3f(xmin,ymax,zmax);
  256. glVertex3f(xmin,ymax,zmax);
  257. glVertex3f(xmin,ymin,zmax);
  258. glVertex3f(xmin,ymin,zmin);
  259. glVertex3f(xmin,ymin,zmax);
  260. glVertex3f(xmin,ymin,zmax);
  261. glVertex3f(xmin,ymax,zmax);
  262. glVertex3f(xmin,ymax,zmax);
  263. glVertex3f(xmin,ymax,zmin);
  264. glVertex3f(xmin,ymax,zmin);
  265. glVertex3f(xmin,ymin,zmin);
  266. glVertex3f(xmin,ymax,zmin);
  267. glVertex3f(xmax,ymax,zmin);
  268. glVertex3f(xmax,ymax,zmin);
  269. glVertex3f(xmax,ymax,zmax);
  270. glVertex3f(xmax,ymax,zmax);
  271. glVertex3f(xmin,ymax,zmax);
  272. glVertex3f(xmin,ymax,zmax);
  273. glVertex3f(xmin,ymax,zmin);
  274. glEnd();
  275. }
  276. free(ptmin_aabb);
  277. free(ptmax_aabb);
  278. glFlush();
  279. }
  280. void display_ball_ritter()
  281. {
  282. point3d *listcentre=NULL;
  283. double *listray=NULL;
  284. a2ri_vf_bounding_ball_ritter(modeltodisplay,facesmodeltodisplay,modeltodisplay->nbface,&listcentre,&listray);
  285. glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
  286. for(int i=0;i<modeltodisplay->nbface;i++)
  287. {
  288. glTranslatef(listcentre[i].x,listcentre[i].y,listcentre[i].z);
  289. glutWireSphere(listray[i],10,10);
  290. glTranslatef(-listcentre[i].x,-listcentre[i].y,-listcentre[i].z);
  291. }
  292. free(listcentre);
  293. free(listray);
  294. listcentre=NULL;
  295. listray=NULL;
  296. a2ri_vf_bounding_ball_ritter(tomove,facestomove,tomove->nbface,&listcentre,&listray);
  297. glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
  298. for(int i=0;i<tomove->nbface;i++)
  299. {
  300. glTranslatef(listcentre[i].x,listcentre[i].y,listcentre[i].z);
  301. glutWireSphere(listray[i],10,10);
  302. glTranslatef(-listcentre[i].x,-listcentre[i].y,-listcentre[i].z);
  303. }
  304. free(listcentre);
  305. free(listray);
  306. }
  307. void
  308. transform (int i)
  309. {
  310. glTranslatef((modeltodisplay->xmin+modeltodisplay->xmax)/2.0,
  311. (modeltodisplay->ymin+modeltodisplay->ymax)/2.0,
  312. (modeltodisplay->zmin+modeltodisplay->zmax)/2.0);
  313. glRotatef (hist[i][0], 1.0, 0.0, 0.0);
  314. glRotatef (hist[i][1], 0.0, 1.0, 0.0);
  315. glRotatef (hist[i][2], 0.0, 0.0, 1.0);
  316. glTranslatef(-(modeltodisplay->xmin+modeltodisplay->xmax)/2.0,
  317. -(modeltodisplay->ymin+modeltodisplay->ymax)/2.0,
  318. -(modeltodisplay->zmin+modeltodisplay->zmax)/2.0);
  319. }
  320. void
  321. display ()
  322. {
  323. point3d ptmin,ptmax;
  324. if(modeltodisplay->xmin<tomove->xmin)
  325. ptmin.x=modeltodisplay->xmin;
  326. else
  327. ptmin.x=tomove->xmin;
  328. if(modeltodisplay->ymin<tomove->ymin)
  329. ptmin.y=modeltodisplay->ymin;
  330. else
  331. ptmin.y=tomove->ymin;
  332. if(modeltodisplay->zmin<tomove->zmin)
  333. ptmin.z=modeltodisplay->zmin;
  334. else
  335. ptmin.z=tomove->zmin;
  336. if(modeltodisplay->xmax>tomove->xmax)
  337. ptmax.x=modeltodisplay->xmax;
  338. else
  339. ptmax.x=tomove->xmax;
  340. if(modeltodisplay->ymax>tomove->ymax)
  341. ptmax.y=modeltodisplay->ymax;
  342. else
  343. ptmax.y=tomove->ymax;
  344. if(modeltodisplay->zmax>tomove->zmax)
  345. ptmax.z=modeltodisplay->zmax;
  346. else
  347. ptmax.z=tomove->zmax;
  348. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  349. glMatrixMode (GL_PROJECTION);
  350. glLoadIdentity ();
  351. gluPerspective (45, WIDTH/((float)HEIGHT), 0.1, 900);
  352. glMatrixMode (GL_MODELVIEW);
  353. glLoadIdentity ();
  354. if(modeltodisplay->zmax==0)
  355. gluLookAt (((modeltodisplay->xmin+modeltodisplay->xmax)/2.0)+tx,
  356. ((modeltodisplay->ymin+modeltodisplay->ymax)/2.0)+ty,
  357. 70+tz,
  358. ((modeltodisplay->xmin+modeltodisplay->xmax)/2.0)-tx,
  359. ((modeltodisplay->ymin+modeltodisplay->ymax)/2.0)+ty,
  360. ((modeltodisplay->zmin+modeltodisplay->zmax)/2.0)+tz,
  361. 0, 1, 0);
  362. else
  363. gluLookAt (((modeltodisplay->xmin+modeltodisplay->xmax)/2.0)+tx,
  364. ((modeltodisplay->ymin+modeltodisplay->ymax)/2.0)+ty,
  365. modeltodisplay->zmax*1.5+tz,
  366. ((modeltodisplay->xmin+modeltodisplay->xmax)/2.0)+tx,
  367. ((modeltodisplay->ymin+modeltodisplay->ymax)/2.0)+ty,
  368. ((modeltodisplay->zmin+modeltodisplay->zmax)/2.0)+tz,
  369. 0, 1, 0);
  370. transform (0);
  371. glTranslatef((ptmin.x+ptmax.x)/2.0,
  372. (ptmin.y+ptmax.y)/2.0,
  373. (ptmin.z+ptmax.z)/2.0);
  374. glRotatef(angle,0.0,1.0,0.0);
  375. glRotatef(angle2,1.0,0.0,0.0);
  376. glTranslatef(-(ptmin.x+ptmax.x)/2.0,
  377. -(ptmin.y+ptmax.y)/2.0,
  378. -(ptmin.z+ptmax.z)/2.0);
  379. display_triangles();
  380. if(affich_aabb)
  381. display_aabb();
  382. if(affich_ball_ritter)
  383. display_ball_ritter();
  384. glutSwapBuffers ();
  385. }
  386. void
  387. exitFunc ()
  388. {
  389. a2ri_vf_free(modeltodisplay);
  390. a2ri_vf_free(tomove);
  391. exit (EXIT_SUCCESS);
  392. }
  393. void mouse(int button, int state, int x, int y)
  394. {
  395. if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
  396. {
  397. rotation=1;
  398. initx=x;
  399. inity=y;
  400. }
  401. if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
  402. rotation=0;
  403. if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
  404. {
  405. deplacement=1;
  406. initx=x;
  407. inity=y;
  408. }
  409. if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
  410. deplacement=0;
  411. if(button==GLUT_WHEEL_MOUSE_UP)
  412. tz+=longdiag/50;
  413. if(button==GLUT_WHEEL_MOUSE_DOWN)
  414. tz-=longdiag/50;
  415. }
  416. void motion(int x, int y)
  417. {
  418. if(rotation)
  419. {
  420. angle+=(x-initx)*0.5;
  421. angle2+=(y-inity)*0.5;
  422. initx=x;
  423. inity=y;
  424. glutPostRedisplay();
  425. }
  426. if(deplacement)
  427. {
  428. tx-=(x-initx)*0.001*longdiag;
  429. ty+=(y-inity)*0.001*longdiag;
  430. initx=x;
  431. inity=y;
  432. glutPostRedisplay();
  433. }
  434. }
  435. void
  436. calcul_precision()
  437. {
  438. double *distance=NULL,longueurmin,longueur;
  439. int size=0,*distance_int=NULL;
  440. free(liste_vert);
  441. liste_vert=NULL;
  442. free(liste_rouge);
  443. liste_rouge=NULL;
  444. size_liste_couleur=0;
  445. for(int i=0;i<tomove->nbvertex;i++)
  446. {
  447. point3d M,A;
  448. point3d_init(&M,tomove->ve[i].x,tomove->ve[i].y,tomove->ve[i].z);
  449. longueurmin=-1;
  450. for(int j=0;j<modeltodisplay->nbvertex;j++)
  451. {
  452. point3d_init(&A,modeltodisplay->ve[j].x,modeltodisplay->ve[j].y,modeltodisplay->ve[j].z);
  453. longueur=point3d_length(&M,&A);
  454. if(longueur<longueurmin || longueurmin<0)
  455. longueurmin=longueur;
  456. }
  457. list_double_add(&distance,&size,sqrt(longueurmin),WITH_REDUNDANCE);
  458. }
  459. double min=list_double_min(distance,size);
  460. double max=list_double_max(distance,size);
  461. size=0;
  462. for(int i=0;i<tomove->nbvertex;i++)
  463. {
  464. list_int_add(&distance_int,&size,(int)(((distance[i]-min)/(max-min))*65535.0),WITH_REDUNDANCE);
  465. }
  466. size=0;
  467. liste_vert=NULL;
  468. liste_rouge=NULL;
  469. size_liste_couleur=0;
  470. for(int i=0;i<tomove->nbvertex;i++)
  471. {
  472. if(distance_int[i]<32767)
  473. {
  474. list_int_add(&liste_rouge,&size_liste_couleur,distance_int[i]/128,WITH_REDUNDANCE);
  475. size_liste_couleur--;
  476. list_int_add(&liste_vert,&size_liste_couleur,255,WITH_REDUNDANCE);
  477. }
  478. else
  479. {
  480. list_int_add(&liste_rouge,&size_liste_couleur,255,WITH_REDUNDANCE);
  481. size_liste_couleur--;
  482. list_int_add(&liste_vert,&size_liste_couleur,255-((distance_int[i]-32767)/128),WITH_REDUNDANCE);
  483. }
  484. }
  485. }
  486. void
  487. keyboard (unsigned char key, int x, int y)
  488. {
  489. int i, j;
  490. char fic[50];
  491. time_t dep,fin;
  492. point3d centre;
  493. centre.x=(tomove->xmin+tomove->xmax)/2.0;
  494. centre.y=(tomove->ymin+tomove->ymax)/2.0;
  495. centre.z=(tomove->zmin+tomove->zmax)/2.0;
  496. vector3d vtrans;
  497. switch (key)
  498. {
  499. case 'w':
  500. a2ri_vf_rotateX_degre_center(tomove,1,&centre);
  501. pertrotx+=1.0;
  502. break;
  503. case 'W':
  504. a2ri_vf_rotateX_degre_center(tomove,-1,&centre);
  505. pertrotx-=1.0;
  506. break;
  507. case 'x':
  508. a2ri_vf_rotateY_degre_center(tomove,1,&centre);
  509. pertroty+=1.0;
  510. break;
  511. case 'X':
  512. a2ri_vf_rotateY_degre_center(tomove,-1,&centre);
  513. pertroty-=1.0;
  514. break;
  515. case 'c':
  516. a2ri_vf_rotateZ_degre_center(tomove,1,&centre);
  517. pertrotz+=1.0;
  518. break;
  519. case 'C':
  520. a2ri_vf_rotateZ_degre_center(tomove,-1,&centre);
  521. pertrotz-=1.0;
  522. break;
  523. case 'v':
  524. vtrans.dx=(sqrt((tomove->xmin-tomove->xmax)*(tomove->xmin-tomove->xmax)+(tomove->ymin-tomove->ymax)*(tomove->ymin-tomove->ymax)+(tomove->zmin-tomove->zmax)*(tomove->zmin-tomove->zmax))/50.0);
  525. vtrans.dy=0;
  526. vtrans.dz=0;
  527. a2ri_vf_translate(tomove,&vtrans);
  528. perttx+=2.0;
  529. break;
  530. case 'V':
  531. vtrans.dx=-(sqrt((tomove->xmin-tomove->xmax)*(tomove->xmin-tomove->xmax)+(tomove->ymin-tomove->ymax)*(tomove->ymin-tomove->ymax)+(tomove->zmin-tomove->zmax)*(tomove->zmin-tomove->zmax))/50.0);
  532. vtrans.dy=0;
  533. vtrans.dz=0;
  534. a2ri_vf_translate(tomove,&vtrans);
  535. perttx-=2.0;
  536. break;
  537. case 'b':
  538. vtrans.dx=0;
  539. vtrans.dy=(sqrt((tomove->xmin-tomove->xmax)*(tomove->xmin-tomove->xmax)+(tomove->ymin-tomove->ymax)*(tomove->ymin-tomove->ymax)+(tomove->zmin-tomove->zmax)*(tomove->zmin-tomove->zmax))/50.0);
  540. vtrans.dz=0;
  541. a2ri_vf_translate(tomove,&vtrans);
  542. pertty+=2.0;
  543. break;
  544. case 'B':
  545. vtrans.dx=0;
  546. vtrans.dy=-(sqrt((tomove->xmin-tomove->xmax)*(tomove->xmin-tomove->xmax)+(tomove->ymin-tomove->ymax)*(tomove->ymin-tomove->ymax)+(tomove->zmin-tomove->zmax)*(tomove->zmin-tomove->zmax))/50.0);
  547. vtrans.dz=0;
  548. a2ri_vf_translate(tomove,&vtrans);
  549. pertty-=2.0;
  550. break;
  551. case 'n':
  552. vtrans.dx=0;
  553. vtrans.dy=0;
  554. vtrans.dz=(sqrt((tomove->xmin-tomove->xmax)*(tomove->xmin-tomove->xmax)+(tomove->ymin-tomove->ymax)*(tomove->ymin-tomove->ymax)+(tomove->zmin-tomove->zmax)*(tomove->zmin-tomove->zmax))/50.0);
  555. a2ri_vf_translate(tomove,&vtrans);
  556. perttz+=2.0;
  557. break;
  558. case 'N':
  559. vtrans.dx=0;
  560. vtrans.dy=0;
  561. vtrans.dz=-(sqrt((tomove->xmin-tomove->xmax)*(tomove->xmin-tomove->xmax)+(tomove->ymin-tomove->ymax)*(tomove->ymin-tomove->ymax)+(tomove->zmin-tomove->zmax)*(tomove->zmin-tomove->zmax))/50.0);
  562. a2ri_vf_translate(tomove,&vtrans);
  563. perttz-=2.0;
  564. break;
  565. case 's':
  566. case 'S' :
  567. printf("nom du fichier : ");
  568. a2ri_erreur_critique_si(!scanf("%s",fic),"erreur nom fichier");
  569. a2ri_vf_save_file(fic,tomove);
  570. break;
  571. case 'r':
  572. case 'R':
  573. printf("entrer le pourcentage de recouvrement (entre 0.0 et 1.0): ");
  574. a2ri_erreur_critique_si(!scanf("%lf",&recouvrement),"erreur recouvrement");
  575. break;
  576. case 'e':
  577. case 'E':
  578. printf("regler la sensibilite de l'algorithme automated_trimmed_cip (entre 0.0 et 1.0): ");
  579. a2ri_erreur_critique_si(!scanf("%lf",&sensibilite),"erreur sensibilite");
  580. break;
  581. case 'i':
  582. case 'I':
  583. dep=clock();
  584. a2ri_vf_icp(tomove,modeltodisplay,dkarret);
  585. fin=clock();
  586. printf("temps calcul recalage : %lf\n",((fin-dep)*1.0)/(CLOCKS_PER_SEC*1.0));
  587. break;
  588. case 't':
  589. case 'T':
  590. dep=clock();
  591. a2ri_vf_trimmed_icp(tomove,modeltodisplay,recouvrement,dkarret);
  592. fin=clock();
  593. printf("temps calcul recalage : %lf\n",((fin-dep)*1.0)/(CLOCKS_PER_SEC*1.0));
  594. break;
  595. case 'a':
  596. case 'A':
  597. dep=clock();
  598. a2ri_vf_automated_trimmed_icp(tomove,modeltodisplay,dkarret,sensibilite);
  599. fin=clock();
  600. printf("temps calcul recalage : %lf\n",((fin-dep)*1.0)/(CLOCKS_PER_SEC*1.0));
  601. break;
  602. case 'q':
  603. case 'Q':
  604. case 27:
  605. exitFunc ();
  606. break;
  607. }
  608. for (i=NB_VAL-1; i > 0; i--)
  609. for (j=0; j < 6; j++)
  610. hist [i][j] = hist [i-1][j];
  611. hist [0][0] = rx;
  612. hist [0][1] = ry;
  613. hist [0][2] = rz;
  614. hist [0][3] = tx;
  615. hist [0][4] = ty;
  616. hist [0][5] = tz;
  617. printf("vue:\nrotation : %lf , %lf , %lf / translation : %lf , %lf , %lf\n",rx,ry,rz,tx,ty,tz);
  618. printf("perturbation:\nrotation : %lf , %lf , %lf / translation : %lf , %lf , %lf\n",pertrotx,pertroty,pertrotz,perttx,pertty,perttz);
  619. }
  620. void
  621. idleFunc ()
  622. {
  623. int i, j;
  624. for (i=NB_VAL-1; i > 0; i--)
  625. for (j=0; j < 6; j++)
  626. hist [i][j] = hist [i-1][j];
  627. glutPostRedisplay ();
  628. }
  629. int
  630. go (int argc, char **argv, vf_model *b, vf_model *m, double dkarr)
  631. {
  632. point3d ptmin,ptmax;
  633. dkarret=dkarr;
  634. modeltodisplay=b;
  635. tomove=m;
  636. affich_aabb=0;
  637. affich_ball_ritter=0;
  638. recouvrement=1.0;
  639. sensibilite=0.3;
  640. facesmodeltodisplay=(int*)malloc(modeltodisplay->nbface*sizeof(int));
  641. for(int i=0;i<modeltodisplay->nbface;i++)
  642. facesmodeltodisplay[i]=i;
  643. facestomove=(int*)malloc(tomove->nbface*sizeof(int));
  644. for(int i=0;i<tomove->nbface;i++)
  645. facestomove[i]=i;
  646. rinc=R_INC;
  647. tinc=T_INC;
  648. if(modeltodisplay->zmax-modeltodisplay->zmin!=0)
  649. tinc=(modeltodisplay->zmax-modeltodisplay->zmin)/10;
  650. ptmin.x=modeltodisplay->xmin;
  651. ptmin.y=modeltodisplay->ymin;
  652. ptmin.z=modeltodisplay->zmin;
  653. ptmax.x=modeltodisplay->xmax;
  654. ptmax.y=modeltodisplay->ymax;
  655. ptmax.z=modeltodisplay->zmax;
  656. longdiag=point3d_length(&ptmin,&ptmax);
  657. affich_tomove=1;
  658. affich_display=1;
  659. init (argc, argv);
  660. glutDisplayFunc (display);
  661. glutKeyboardFunc (keyboard);
  662. glutMouseFunc(mouse);
  663. glutMotionFunc(motion);
  664. glutIdleFunc (idleFunc);
  665. glutMainLoop ();
  666. return EXIT_SUCCESS;
  667. }