Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 46.34 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Imagina
  3. // ----------------------------------------------------------------------------
  4. // IN - Synthèse d'images - Modélisation géométrique
  5. // Auteur : Gilles Gesquière
  6. // ----------------------------------------------------------------------------
  7. // Base du TP 1
  8. // programme permettant de créer des formes de bases.
  9. // La forme représentée ici est un polygone blanc dessiné sur un fond rouge
  10. ///////////////////////////////////////////////////////////////////////////////  
  11. #include <stdio.h>      
  12. #include <stdlib.h>    
  13. #include <math.h>
  14. #include <string>
  15. #include "glad/glad.h"
  16. #include "headers/Point.h"
  17. #include "headers/Vector.h"
  18. #include "headers/Voxel.h"
  19.  
  20. #include "headers/Octree.h"
  21.  
  22. #include <fstream>
  23.  
  24. #include <vector>
  25. #include <iostream>
  26. #include <iomanip>
  27.  
  28. /* Dans les salles de TP, vous avez généralement accès aux glut dans C:\Dev. Si ce n'est pas le cas, téléchargez les .h .lib ...
  29. Vous pouvez ensuite y faire référence en spécifiant le chemin dans visual. Vous utiliserez alors #include <glut.h>.
  30. Si vous mettez glut dans le répertoire courant, on aura alors #include "glut.h"
  31. */
  32.  
  33. #include <algorithm>
  34.  
  35.  
  36. #include <GL/glut.h>
  37.  
  38. // Définition de la taille de la fenêtre
  39. #define WIDTH  480
  40.  
  41. #define HEIGHT 480
  42.  
  43. // Définition de la couleur de la fenêtre
  44. #define RED   1
  45. #define GREEN 0
  46. #define BLUE  0
  47. #define ALPHA 1
  48.  
  49. #define IMG_GAUSS 5689
  50. #define FILL 1
  51. #define RAFINEMENT 56765343
  52.  
  53. #define BUTTERFLY 5676156
  54.  
  55.  
  56. // Touche echap (Esc) permet de sortir du programme
  57. #define KEY_ESC 27
  58.  
  59.  
  60. // Entêtes de fonctions
  61. void init_scene();
  62. void render_scene();
  63. GLvoid initGL();
  64. GLvoid window_display();
  65. GLvoid window_reshape(GLsizei width, GLsizei height);
  66. GLvoid window_key(unsigned char key, int x, int y);
  67.  
  68. float angle = 0.0f;
  69. float lx=-1.0f,lz=-1.0f;
  70. float x=1.0f, z=1.0f;
  71.  
  72. float lxp=0.0f,lzp=0.5f;
  73. float xp=0.0f, zp=-0.5f;
  74.  
  75. float deltaAngle = 0.0f;
  76. float deltaMove = 0;
  77. int xOrigin = -1;
  78. float* TabVertices;
  79. int* TabIndices;
  80. float* TabNormals;
  81. int* TabVoisins;
  82. GLubyte* TabColors;
  83. float* TabNormalsSom;
  84. int nbPoints, nbPolys, nbEdges;
  85. int image_gaussienne = 0;
  86. int testFill = 0;
  87. int testSeg = 0;
  88. int testRafinement = 0;
  89. int testButterfly = 0;
  90.  
  91. float maxX=0, maxY=0, maxZ=0;
  92.  
  93. float sizefenetre = 10;
  94. float sizefenetreX = 10;
  95. float sizefenetreY = 10;
  96. float sizefenetreZ = 10;
  97.  
  98. double searchSizeOrtho(float* TabVertices) {
  99.   float min = TabVertices[0], max = TabVertices[0];
  100.   for (int i = 0; i<nbPoints; i++) {
  101.     if (TabVertices[i]>max)
  102.       max = TabVertices[i];
  103.     if (TabVertices[i]<min)
  104.       min = TabVertices[i];
  105.   }
  106.   float ret = sqrt(max*max+min*min);
  107.   std::cout<< " RET : "<< ret <<std::endl;
  108.   return ret;
  109. }
  110.  
  111. void searchSizeOrthoXYZ(float* TabVertices) {
  112.   float Xmin = TabVertices[0], Xmax = TabVertices[0];
  113.   float Ymin = TabVertices[1], Ymax = TabVertices[1];
  114.   float Zmin = TabVertices[2], Zmax = TabVertices[2];
  115.   for (int i = 0; i<nbPoints; i++) {
  116.  
  117.     if (TabVertices[3*i]>Xmax)
  118.       Xmax = TabVertices[3*i];
  119.     if (TabVertices[3*i]<Xmin)
  120.       Xmin = TabVertices[3*i];
  121.  
  122.     if (TabVertices[3*i+1]>Ymax)
  123.       Ymax = TabVertices[3*i+1];
  124.     if (TabVertices[3*i+1]<Ymin)
  125.       Ymin = TabVertices[3*i+1];
  126.  
  127.     if (TabVertices[3*i+2]>Zmax)
  128.       Zmax = TabVertices[3*i+2];
  129.     if (TabVertices[3*i+2]<Zmin)
  130.       Zmin = TabVertices[3*i+2];
  131.   }
  132.  
  133.   sizefenetreX =  sqrt(Xmax*Xmax+Xmin*Xmin);
  134.   sizefenetreY =  sqrt(Ymax*Ymax+Ymin*Ymin);
  135.   sizefenetreZ =  sqrt(Zmax*Zmax+Zmin*Zmin);
  136. }
  137.  
  138. int indiceEquals(int* TabIndices, int i, int j, int k, int l) { // return 0 if tab[i] est voisin avec tab[j]
  139.  
  140.   // k et l sont les positions des deux triangles à tester dans le tableau d'indice par exemple 0 et 1 ou 1 et 2 ou 2 et 0
  141.  
  142.   if(
  143.       (
  144.         ((TabIndices[i*3+k] == TabIndices[j*3]) || (TabIndices[i*3+k] == TabIndices[j*3+1]) || (TabIndices[i*3+k] == TabIndices[j*3+2]))
  145.           &&  
  146.         ((TabIndices[i*3+l] == TabIndices[j*3]) || (TabIndices[i*3+l] == TabIndices[j*3+1]) || (TabIndices[i*3+l] == TabIndices[j*3+2]))
  147.       )
  148.     )
  149.     return 0;
  150.   else
  151.     return 1;
  152. }
  153.      
  154.      
  155.  
  156. int fillVoisinTab(int* TabIndices, int* TabVoisins, int nbPolys) { // Pour chaque triangle, si il a deux indices en commun avec un autre triangle on ajoute cet autre triangle
  157.                                                                    // au tableau des voisin du triangle en question
  158.  
  159.   std::ofstream fichierEcriture("TabVoisins.txt", std::ios::out | std::ios::trunc);
  160.   int ownVoisin = 0, tr1 = 0, tr2 = 0, tr3 = 0;
  161.  
  162.   if(fichierEcriture)
  163.   {
  164.     for(int i = 0; i<nbPolys; i++)
  165.     {
  166.       //std::cout<<i<<" : ";
  167.       fichierEcriture<<i<<" : ";
  168.       for(int j = 0; j<nbPolys; j++)
  169.       {
  170.         //std::cout<<" compare "<<TabIndices[ i*3 ]<<" et "<<TabIndices[j*3]<<std::endl;
  171.         if( i != j)
  172.         {
  173.  
  174.           if(indiceEquals(TabIndices, i, j, 0, 1) == 0 && tr1 == 0)
  175.           {
  176.             TabVoisins[i*3] = j;
  177.             fichierEcriture<<(int)j<<" "; // ecriture fichier
  178.             ownVoisin++;
  179.             tr1++;
  180.           }
  181.           else if(indiceEquals(TabIndices, i, j, 0, 1) != 0 && j==nbPolys-1 && tr1 == 0)
  182.           {
  183.             TabVoisins[i*3] = -1;
  184.             fichierEcriture<<(int)-1<<" ";
  185.             ownVoisin++;
  186.             tr1++;
  187.           }
  188.  
  189.  
  190.           if(indiceEquals(TabIndices, i, j, 1, 2) == 0 && tr2 == 0)
  191.           {
  192.             TabVoisins[i*3+1] = j;
  193.             fichierEcriture<<(int)j<<" "; // ecriture fichier
  194.             ownVoisin++;
  195.             tr2++;
  196.           }
  197.           else if(indiceEquals(TabIndices, i, j, 1, 2) != 0 && j==nbPolys-1 && tr2 == 0)
  198.           {
  199.             TabVoisins[i*3+1] = -1;
  200.             fichierEcriture<<(int)-1<<" ";
  201.             ownVoisin++;
  202.             tr2++;
  203.           }
  204.  
  205.           if(indiceEquals(TabIndices, i, j, 2, 0) == 0 && tr3 == 0)
  206.           {
  207.             TabVoisins[i*3+2] = j;
  208.             fichierEcriture<<(int)j<<" "; // ecriture fichier
  209.             ownVoisin++;
  210.             tr3++;
  211.           }
  212.           else if(indiceEquals(TabIndices, i, j, 2, 0) != 0 && j==nbPolys-1 && tr3 == 0)
  213.           {
  214.             TabVoisins[i*3+2] = -1;
  215.             fichierEcriture<<(int)-1<<" ";
  216.             ownVoisin++;
  217.             tr3++;
  218.           }
  219.         }
  220.         if(ownVoisin==3)
  221.           j=nbPolys;
  222.       }
  223. /*      if(ownVoisin != 3)
  224.       {
  225.         //std::cout<<"pas de voisins ta race"<<std::endl;
  226.         TabVoisins[i] = -1;
  227.         fichierEcriture<<(int)-1<<" ";
  228.       }*/
  229.       ownVoisin = 0;
  230.       tr1 = 0;
  231.       tr2 = 0;
  232.       tr3 = 0;
  233.       //std::cout<<std::endl;
  234.       fichierEcriture<<"\n";
  235.     }
  236.     fichierEcriture.close();  //
  237.   }
  238.   else
  239.   {
  240.     std::cerr << "Impossible d'ouvrir le fichier !" << std::endl;
  241.     return -1;
  242.   }
  243.   return 0;
  244. }
  245.  
  246. void traceSegment(Point *A, Point *B,Vector color) {
  247.   glColor3f(color.getX(),color.getY(),color.getZ());
  248.   glBegin(GL_LINES);
  249.       glVertex3f(A->getX(),A->getY(),A->getZ());
  250.       glVertex3f(B->getX(),B->getY(),B->getZ());
  251.     glEnd();
  252. }
  253.  
  254. void tracePoint(Point *A,Vector color) {
  255.   glColor3f(color.getX(),color.getY(),color.getZ());
  256.   glBegin(GL_POINTS);
  257.     glVertex3f(A->getX(),A->getY(),A->getZ());
  258.     glEnd();
  259. }
  260.  
  261. void traceVector(Vector *v,Vector color) {
  262.   glColor3f(color.getX(),color.getY(),color.getZ());
  263.   glBegin(GL_LINES);
  264.       glVertex3f(0,0,0);
  265.       glVertex3f(v->getX(),v->getY(),v->getZ());
  266.     glEnd();
  267. }
  268.  
  269. void traceQuad(Point* A, Point* B,Point* C,Point* D, Vector color) {
  270.   glColor3f(color.getX(),color.getY(),color.getZ());
  271.   glBegin(GL_QUADS);
  272.     glVertex3f(A->getX(),A->getY(),A->getZ());
  273.     glVertex3f(B->getX(),B->getY(),B->getZ());
  274.     glVertex3f(C->getX(),C->getY(),C->getZ());
  275.     glVertex3f(D->getX(),D->getY(),D->getZ());
  276.   glEnd();
  277. }
  278.  
  279. void traceTri(Point* A, Point* B,Point* C, Vector color) {
  280.   glColor3f(color.getX(),color.getY(),color.getZ());
  281.   glBegin(GL_TRIANGLES);
  282.     glVertex3f(A->getX(),A->getY(),A->getZ());
  283.     glVertex3f(B->getX(),B->getY(),B->getZ());
  284.     glVertex3f(C->getX(),C->getY(),C->getZ());
  285.     glEnd();
  286. }
  287.  
  288.  
  289. int main(int argc, char **argv)
  290. {  
  291.   char fichierLu[250];
  292.   if (argc != 2)
  293.   {
  294.     printf("Usage: file.off \n");
  295.     exit (1);
  296.   }
  297.   sscanf (argv[1],"%s",fichierLu);
  298.  
  299.   //std::ifstream fichier("MeshSegmentation.off", std::ios::in);
  300.   std::ifstream fichier(fichierLu, std::ios::in);
  301.   float xIn = 0;
  302.   std::string chaine1, chaine2;
  303.   if(fichier)
  304.   {
  305.     fichier >> chaine1;/* >> entier1 >> entier2;*/  /*on lit jusqu'à l'espace et on stocke ce qui est lu dans la variable indiquée */
  306.     if(chaine1 == "OFF")
  307.     {
  308.       std::cout << "Ouverture fichier OFF" << std::endl;
  309.       fichier >> nbPoints >> nbPolys >> nbEdges;
  310.       std::cout << "nbPoints = " <<nbPoints<<", nbPolys = "<<nbPolys<<", nbEdges = "<<nbEdges<<std::endl;
  311.       int i = 0;
  312.       TabVertices = (float*) malloc(sizeof(float)*3*nbPoints);
  313.       TabIndices = (int*) malloc(sizeof(int)*3*nbPolys);
  314.       TabVoisins = (int*) malloc(sizeof(int)*3*nbPolys);
  315.       TabNormals = (float*) malloc(sizeof(float)*3*nbPolys);
  316.       TabNormalsSom = (float*) malloc(sizeof(float)*3*nbPoints);
  317.       TabColors = (GLubyte*) malloc(sizeof(GLubyte)*3*nbPoints);
  318.  
  319.       while(i<nbPoints*3)
  320.       {
  321.         fichier>>xIn;
  322.         TabVertices[i] = xIn;
  323.         i++;
  324.       }
  325.       i=0;
  326.        while(i<nbPolys*3)
  327.       {
  328.         fichier>>xIn;
  329.         xIn = 0.0;
  330.         fichier>>xIn;
  331.         TabIndices[i] = xIn;
  332.         i++;
  333.         fichier>>xIn;
  334.         TabIndices[i] = xIn;
  335.         i++;
  336.         fichier>>xIn;
  337.         TabIndices[i] = xIn;
  338.         i++;
  339.       }
  340.     }
  341.     fichier.close();
  342.   }
  343.   else
  344.     std::cerr << "Impossible d'ouvrir le fichier !" << std::endl;
  345.  
  346.  
  347.   for(int i = 0; i<nbPolys; i++) // Trace les normales aux triangles dans TabNormals
  348.   {
  349.     Point* p1 = new Point(TabVertices[3*(TabIndices[i*3])], TabVertices[3*(TabIndices[i*3])+1], TabVertices[3*(TabIndices[i*3])+2]);
  350.     Point* p2 = new Point(TabVertices[3*(TabIndices[i*3+1])], TabVertices[3*(TabIndices[i*3+1])+1], TabVertices[3*(TabIndices[i*3+1])+2]);
  351.     Point* p3 = new Point(TabVertices[3*(TabIndices[i*3+2])], TabVertices[3*(TabIndices[i*3+2])+1], TabVertices[3*(TabIndices[i*3+2])+2]);
  352.  
  353.     Vector* v1 = new Vector(p2->getX()-p1->getX(), p2->getY()-p1->getY(), p2->getZ()-p1->getZ());
  354.     Vector* v2 = new Vector(p3->getX()-p1->getX(), p3->getY()-p1->getY(), p3->getZ()-p1->getZ());
  355.     v1->normalize();
  356.     v2->normalize();
  357.  
  358.     Vector v3 =  v1->Vectoriel(v2);
  359.  
  360.     TabNormals[i*3] = v3.getX();
  361.     TabNormals[i*3+1] = v3.getY();
  362.     TabNormals[i*3+2] = v3.getZ();
  363.   }
  364.  
  365.  
  366.   if(fillVoisinTab(TabIndices, TabVoisins, nbPolys) == -1)
  367.   {
  368.     std::cout<<"Erreur ecriture tab voisins"<<std::endl;
  369.   }
  370.   std::cout<<"Ecriture tab voisin réussie"<<std::endl;
  371.  
  372.     // initialisation  des paramètres de GLUT en fonction
  373.   // des arguments sur la ligne de commande
  374.   glutInit(&argc, argv);
  375.   glutInitDisplayMode(GLUT_RGBA);
  376.  
  377.   // définition et création de la fenêtre graphique, ainsi que son titre
  378.   glutInitWindowSize(WIDTH, HEIGHT);
  379.   glutInitWindowPosition(0, 0);
  380.   glutCreateWindow("TP8 - SEGMENTATION");
  381.  
  382.  
  383.   if(!gladLoadGL()) {
  384.     printf("Something went wrong!\n");
  385.     exit(-1);
  386.   }
  387.   // initialisation de OpenGL et de la scène
  388.   initGL();  
  389.   init_scene();
  390.  
  391.   // choix des procédures de callback pour
  392.   // le tracé graphique
  393.   glutDisplayFunc(&window_display);
  394.   // le redimensionnement de la fenêtre
  395.   sizefenetre = searchSizeOrtho(TabVertices);
  396.   searchSizeOrthoXYZ(TabVertices);
  397.   //sizefenetre*=1.2;
  398.   glutReshapeFunc(&window_reshape);
  399.   // la gestion des événements clavier
  400.   glutKeyboardFunc(&window_key);
  401.  
  402.   // la boucle prinicipale de gestion des événements utilisateur
  403.   glutMainLoop();
  404.  
  405.   return 1;
  406. }
  407.  
  408. // initialisation du fond de la fenêtre graphique : noir opaque
  409. GLvoid initGL()
  410. {
  411.   glClearColor(0, 0, 0, ALPHA);
  412.   glEnable(GL_LIGHTING);
  413.   glEnable(GL_LIGHT0);
  414.   glEnable(GL_LIGHT1);
  415.   glEnable(GL_DEPTH_TEST);        
  416. }
  417.  
  418. // Initialisation de la scene. Peut servir à stocker des variables de votre programme
  419. // à initialiser
  420. void init_scene()
  421. {
  422. }
  423.  
  424. // fonction de call-back pour l´affichage dans la fenêtre
  425.  
  426. GLvoid window_display()
  427. {
  428.   glClear(GL_COLOR_BUFFER_BIT);
  429.   glLoadIdentity();
  430.  
  431.   // C'est l'endroit où l'on peut dessiner. On peut aussi faire appel
  432.   // à une fonction (render_scene() ici) qui contient les informations
  433.   // que l'on veut dessiner
  434.   render_scene();
  435.  
  436.   // trace la scène grapnique qui vient juste d'être définie
  437.   glFlush();
  438. }
  439.  
  440. // fonction de call-back pour le redimensionnement de la fenêtre
  441.  
  442. GLvoid window_reshape(GLsizei width, GLsizei height)
  443. {  
  444.   glViewport(0, 0, width, height);
  445.  
  446.   glMatrixMode(GL_PROJECTION);
  447.   glLoadIdentity();
  448.   // ici, vous verrez pendant le cours sur les projections qu'en modifiant les valeurs, il est
  449.   // possible de changer la taille de l'objet dans la fenêtre. Augmentez ces valeurs si l'objet est
  450.   // de trop grosse taille par rapport à la fenêtre.
  451.   //glOrtho(-0.5, 0.5, -0.5, 0.5, -0.5, 0.5);
  452.   /*glOrtho(-10,10, -10, 10, -10, 10);*/
  453.   //glOrtho(-2*sizefenetre, 2*sizefenetre, -2*sizefenetre, 2*sizefenetre, -2*sizefenetre, 2*sizefenetre);
  454.   //glOrtho(-1*sizefenetre, 1*sizefenetre, -1*sizefenetre, 1*sizefenetre, -1*sizefenetre, 1*sizefenetre);
  455.   glOrtho(-1*sizefenetreX, 1*sizefenetreX, -1*sizefenetreY, 1*sizefenetreY, -1*sizefenetreZ, 1*sizefenetreZ);
  456.  
  457.   // toutes les transformations suivantes s´appliquent au modèle de vue
  458.   glMatrixMode(GL_MODELVIEW);
  459. }
  460.  
  461.  
  462. GLvoid window_key(unsigned char key, int x, int y)
  463. {  
  464.   float fraction = 0.05f;
  465.   switch (key) {    
  466.   case KEY_ESC:
  467.     free(TabVertices );
  468.     free(TabIndices );
  469.     free(TabVoisins );
  470.     free(TabNormals );
  471.     free(TabNormalsSom);
  472.     free(TabColors);
  473.     exit(1);                    
  474.     break;
  475.   case 113:
  476.     angle -= 0.1f;
  477.     lx = sin(angle);
  478.     lz = -cos(angle);
  479.     lxp = sin(angle);
  480.     lzp = -cos(angle);
  481.     break;
  482.   case 100 :
  483.     angle += 0.1f;
  484.     lx = sin(angle);
  485.     lz = -cos(angle);
  486.     lxp = sin(angle);
  487.     lzp = -cos(angle);
  488.     break;
  489.  
  490.   case 122 :
  491.     x += lx * fraction;
  492.     z += lz * fraction;
  493.     xp += lxp * fraction;
  494.     zp += lzp * fraction;
  495.     break;
  496.   case 115 :
  497.     x -= lx * fraction;
  498.     z -= lz * fraction;
  499.     xp -= lxp * fraction;
  500.     zp -= lzp * fraction;
  501.     break;
  502.   case 'g' :
  503.     std::cout<<"Construction image gaussienne"<<std::endl;
  504.     if(image_gaussienne == IMG_GAUSS)
  505.       image_gaussienne = 0;
  506.     else
  507.     {
  508.       image_gaussienne = IMG_GAUSS;
  509.     }
  510.     break;
  511.   case 'f' :
  512.     if(testFill == FILL)
  513.       testFill = -1;
  514.     else
  515.       testFill = FILL;
  516.     break;
  517.   case 'h' :
  518.   std::cout<<"Segmentation !"<<std::endl;
  519.     if(testSeg >6)
  520.       testSeg = 0;
  521.     else
  522.       testSeg++;
  523.     break;
  524.   case 'r' :
  525.   std::cout<<"Rafinement"<<std::endl;
  526.     if(testRafinement == RAFINEMENT)
  527.       testRafinement = -1;
  528.     else
  529.       testRafinement = RAFINEMENT;
  530.     break;
  531.   case 'b' :
  532.   std::cout<<"Subdivision schema Butterfly"<<std::endl;
  533.     if(testButterfly == BUTTERFLY)
  534.       testButterfly = -1;
  535.     else
  536.       testButterfly = BUTTERFLY;
  537.     break;
  538.   default:
  539.     printf ("La touche %d n´est pas active.\n", key);
  540.     break;
  541.   }
  542.    glutPostRedisplay();
  543. }
  544.  
  545. void text(double x, double y, std::string menu)
  546. {
  547.     int len = 80;
  548.     len = menu.length();
  549.     glColor3f(1,1,1);
  550.  
  551.     glMatrixMode( GL_PROJECTION );
  552.     glPushMatrix();
  553.     glLoadIdentity();
  554.  
  555.     gluOrtho2D( 0, 600, 0, 600 );
  556.  
  557.     glMatrixMode( GL_MODELVIEW );
  558.     glPushMatrix();
  559.  
  560.     glLoadIdentity();
  561.  
  562.     glRasterPos2i(265, 300);
  563.  
  564.  
  565.     for ( int i = 0; i < len; ++i )
  566.     {
  567.         glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, menu[i]);
  568.     }
  569.  
  570.     glPopMatrix();
  571.  
  572.     glMatrixMode( GL_PROJECTION );
  573.     glPopMatrix();
  574.     glMatrixMode( GL_MODELVIEW );
  575. }
  576.  
  577. Point* Approximate(double t, Point* pt1, Point* pt2)
  578. {
  579.     double x = pt1->getX() * (1-t) + pt2->getX()* t;
  580.     double y = pt1->getY() * (1-t) + pt2->getY() * t;
  581.  
  582.     return new Point(x, y, 0);
  583. }
  584.  
  585. void constructImgGauss () { // On fait la moyenne des normales au sommet pour tous les triangles qui disposent de l'indice du sommet à évaluer
  586.     int n = 0;
  587.     float X = 0, Y=0, Z =0;
  588.     for(int i = 0 ; i<nbPoints; i++)
  589.     {
  590.       for(int j = 0; j<nbPolys; j++)
  591.       {
  592.  
  593.         if((TabIndices[3*j] == i) || (TabIndices[3*j+1] == i) || (TabIndices[3*j+2] == i)) // si le point appartient à un triangle
  594.         {
  595.           x+=TabNormals[3*j];
  596.           Y+=TabNormals[3*j+1];
  597.           Z+=TabNormals[3*j+2];
  598.           n++;
  599.         }                                                   // On fait la moyenne des normales des triangles qui possedent ce point i dans TabNormalsSom[i]
  600.       }
  601.       TabNormalsSom[3*i] = (float)(X/n);
  602.       TabNormalsSom[3*i+1] =  (float)(Y/n);
  603.       TabNormalsSom[3*i+2] =  (float)(Z/n);
  604.       n=0; X=0; Y=0; Z=0;
  605.     }
  606.  
  607.     for(int i = 0; i<nbPoints; i++) // On trace les normales à chaque sommets
  608.     {
  609.       Point* A = new Point(TabVertices[3*i]+TabNormalsSom[3*i],TabVertices[3*i+1]+TabNormalsSom[3*i+1],TabVertices[3*i+2]+TabNormalsSom[3*i+2]);
  610.       Point* B = new Point(TabVertices[3*i],TabVertices[3*i+1],TabVertices[3*i+2]);
  611.       traceSegment(A, B, Vector(255, 255, 255));
  612.       delete(A);
  613.       delete(B);    
  614.     }
  615. }
  616.  
  617. int sontVoisins(int pos1, int pos2) {
  618.   if((pos1 == TabVoisins[3*pos2]) || (pos1 == TabVoisins[3*pos2+1]) || (pos1 == TabVoisins[3*pos2+2]))
  619.   {
  620.     //std::cout<<"les deux triangles sont bien voisins"<<std::endl;
  621.     return 0;
  622.   }
  623.   else
  624.   {
  625.    // std::cout<<"les deux triangles ne sont pas voisins"<<std::endl;
  626.     return -1;
  627.   }
  628. }
  629.  
  630. float computeDiedreAngle (int posAngle1, int posAngle2) { // supposés voisins
  631.  
  632.     Vector* v1 = new Vector(TabNormals[posAngle1*3],TabNormals[posAngle1*3+1],TabNormals[posAngle1*3+2]);
  633.     Vector* v2 = new Vector(TabNormals[posAngle2*3],TabNormals[posAngle2*3+1],TabNormals[posAngle2*3+2]);
  634.     //std::cout<<"L'angle entre le triangle "<<posAngle1<< " : "<<TabNormals[posAngle1*3]<<" "<<TabNormals[posAngle1*3+1]<<" "<<TabNormals[posAngle1*3+2]<<std::endl;
  635.     //std::cout<<"et le triangle "<<posAngle2<< " : "<<TabNormals[posAngle2*3]<<" "<<TabNormals[posAngle2*3+1]<<" "<<TabNormals[posAngle2*3+2]<<std::endl;
  636.     v1->normalize();
  637.     v2->normalize();
  638.     float ret =  180-(acos(v1->scalar(v2)/(v1->norme(),v2->norme()))*180.0 / M_PI );
  639.     delete(v1);
  640.     delete(v2);
  641.    // std::cout<<"Vaut  : "<<ret<<std::endl;  
  642.     return ret;
  643. }
  644.  
  645. void coloreAreteCommune(int pos1, int pos2, Vector c) {
  646.  /* std::cout<<"====> "<<pos1<< "  : "<< TabIndices[3*pos1]<< ", "<<TabIndices[3*pos1+1]<<", "<<TabIndices[3*pos1+2]<<std::endl;
  647.   std::cout<<"====> "<<pos2<< "  : "<< TabIndices[3*pos2]<< ", "<<TabIndices[3*pos2+1]<<", "<<TabIndices[3*pos2+2]<<std::endl;
  648.   std::cout<<"====> "<<pos1<< " et "<<pos2<<" ont les sommets suivants en commun : ";
  649. */
  650.  
  651.   if((TabIndices[3*pos1] == TabIndices[3*pos2]) || (TabIndices[3*pos1] ==TabIndices[3*pos2+1]) || (TabIndices[3*pos1] ==TabIndices[3*pos2+2])) {
  652. /*    TabColors[3*pos1] = 255;
  653.     TabColors[3*pos1+1] = 255;
  654.     TabColors[3*pos1+2] = 0;*/
  655.   /*  std::cout<<" Premier " <<TabIndices[3*pos1]<<std::endl;
  656.     std::cout<<" Premier " <<TabVertices[3*TabIndices[3*pos1]]<<" "<<TabVertices[3*TabIndices[3*pos1]+1]<<" "<<TabVertices[3*TabIndices[3*pos1]+2]<<std::endl;*/
  657.     Point* p = new Point(TabVertices[3*TabIndices[3*pos1]],TabVertices[3*TabIndices[3*pos1]+1],TabVertices[3*TabIndices[3*pos1]+2]);
  658.     //tracePoint(p, Vector(0, 0, 255));
  659.     if( (TabIndices[3*pos1+1] == TabIndices[3*pos2]) || (TabIndices[3*pos1+1] ==TabIndices[3*pos2+1]) || (TabIndices[3*pos1+1] == TabIndices[3*pos2+2])) {
  660.       Point* p2 = new Point(TabVertices[3*TabIndices[3*pos1+1]],TabVertices[3*TabIndices[3*pos1+1]+1],TabVertices[3*TabIndices[3*pos1+1]+2]);
  661.       //tracePoint(p2, Vector(0, 0, 255));
  662.       traceSegment(p, p2, c);    
  663.       delete(p2);
  664.     }
  665.  
  666.     if((TabIndices[3*pos1+2] == TabIndices[3*pos2]) || (TabIndices[3*pos1+2] == TabIndices[3*pos2+1]) ||(TabIndices[3*pos1+2] == TabIndices[3*pos2+2])) {
  667.       Point* p2 = new Point(TabVertices[3*TabIndices[3*pos1+2]],TabVertices[3*TabIndices[3*pos1+2]+1],TabVertices[3*TabIndices[3*pos1+2]+2]);
  668.       //tracePoint(p2, Vector(0, 0, 255));
  669.       traceSegment(p, p2, c);  
  670.       delete(p2);
  671.     }
  672.     delete(p);
  673.   }
  674.  
  675.   if( (TabIndices[3*pos1+1] == TabIndices[3*pos2]) || (TabIndices[3*pos1+1] ==TabIndices[3*pos2+1]) || (TabIndices[3*pos1+1] == TabIndices[3*pos2+2])) {
  676. /*    TabColors[3*(pos1+1)] = 255;
  677.     TabColors[3*(pos1+1)+1] = 255;
  678.     TabColors[3*(pos1+1)+2] = 0;*/
  679.     // std::cout<<" Deuxieme " <<TabIndices[3*pos1+1]<<std::endl;
  680.     // std::cout<<" Deuxieme " <<TabVertices[3*TabIndices[3*pos1+1]]<<" "<<TabVertices[3*TabIndices[3*pos1+1]+1]<<" "<<TabVertices[3*TabIndices[3*pos1+1]+2]<<std::endl;
  681.     Point* p = new Point(TabVertices[3*TabIndices[3*pos1+1]],TabVertices[3*TabIndices[3*pos1+1]+1],TabVertices[3*TabIndices[3*pos1+1]+2]);
  682.     //tracePoint(p, Vector(0, 0, 255));
  683.  
  684.     if((TabIndices[3*pos1+2] == TabIndices[3*pos2]) || (TabIndices[3*pos1+2] == TabIndices[3*pos2+1]) ||(TabIndices[3*pos1+2] == TabIndices[3*pos2+2])) {
  685.       Point* p2 = new Point(TabVertices[3*TabIndices[3*pos1+2]],TabVertices[3*TabIndices[3*pos1+2]+1],TabVertices[3*TabIndices[3*pos1+2]+2]);
  686.       //tracePoint(p2, Vector(0, 0, 255));
  687.       traceSegment(p, p2, c);  
  688.       delete(p2);
  689.     }
  690.     delete(p);
  691.   }
  692.  
  693.   //if((TabIndices[3*pos1+2] == TabIndices[3*pos2]) || (TabIndices[3*pos1+2] == TabIndices[3*pos2+1]) ||(TabIndices[3*pos1+2] == TabIndices[3*pos2+2])) {
  694. /*    TabColors[3*(pos1+2)] = 255;
  695.     TabColors[3*(pos1+2)+1] = 255;
  696.     TabColors[3*(pos1+2)+2] = 0;*/
  697.  
  698.   /*  std::cout<<" Troisieme "<<TabIndices[3*pos1+2]<<std::endl;
  699.      std::cout<<" Troisieme "<<TabVertices[3*TabIndices[3*pos1+2]]<<" "<<TabVertices[3*TabIndices[3*pos1+2]+1]<<" "<<TabVertices[3*TabIndices[3*pos1+2]+2]<<std::endl;*/
  700.   //  Point* p = new Point(TabVertices[3*TabIndices[3*pos1+2]],TabVertices[3*TabIndices[3*pos1+2]+1],TabVertices[3*TabIndices[3*pos1+2]+2]);
  701.   //  tracePoint(p, Vector(0, 0, 255));
  702.   //  delete(p);
  703.   //}
  704.    // std::cout<<std::endl<<std::endl;
  705. }
  706.  
  707. void testAretesVives(float angle, Vector c) {
  708.   for(int i = 0; i<nbPolys; i++)
  709.   {
  710.     if(sontVoisins(i, TabVoisins[3*i])==0) {
  711.       //std::cout<<"les triangles "<<i<< " et "<< TabVoisins[3*i]<<" sont voisins"<<std::endl;
  712.       //std::cout<<"Angles entre leurs normales = "<<computeDiedreAngle(i,TabVoisins[3*i])<<std::endl;
  713.       if(computeDiedreAngle(i,TabVoisins[3*i])<angle) {
  714.         coloreAreteCommune(i,TabVoisins[3*i], c);
  715.       }
  716.     }
  717.     if(sontVoisins(i, TabVoisins[3*i+1])==0) {
  718.       //std::cout<<"les triangles "<<i<< " et "<< TabVoisins[3*i+1]<<" sont voisins"<<std::endl;
  719.       //std::cout<<"Angles entre leurs normales = "<<computeDiedreAngle(i,TabVoisins[3*i+1])<<std::endl;
  720.       if(computeDiedreAngle(i,TabVoisins[3*i+1])<angle) {
  721.         coloreAreteCommune(i,TabVoisins[3*i+1], c);
  722.       }
  723.     }
  724.     if(sontVoisins(i, TabVoisins[3*i+2])==0) {
  725.      // std::cout<<"les triangles "<<i<< " et "<< TabVoisins[3*i+2]<<" sont voisins"<<std::endl;
  726.       //std::cout<<"Angles entre leurs normales = "<<computeDiedreAngle(i,TabVoisins[3*i+2])<<std::endl;
  727.       if(computeDiedreAngle(i,TabVoisins[3*i+2])<angle) {
  728.         coloreAreteCommune(i,TabVoisins[3*i+2], c);
  729.       }
  730.     }
  731.     //std::cout<<"********"<<std::endl;
  732.   }
  733. }
  734.  
  735. void coloreTriaangle(int i, Vector c) {
  736.     Point* p1 = new Point(TabVertices[3*(TabIndices[i*3])], TabVertices[3*(TabIndices[i*3])+1], TabVertices[3*(TabIndices[i*3])+2]);
  737.     Point* p2 = new Point(TabVertices[3*(TabIndices[i*3+1])], TabVertices[3*(TabIndices[i*3+1])+1], TabVertices[3*(TabIndices[i*3+1])+2]);
  738.     Point* p3 = new Point(TabVertices[3*(TabIndices[i*3+2])], TabVertices[3*(TabIndices[i*3+2])+1], TabVertices[3*(TabIndices[i*3+2])+2]);
  739.  
  740.  
  741.   traceTri(p1, p2, p3, c);
  742.   delete(p1);
  743.   delete(p2);
  744.   delete(p3);
  745. }
  746.  
  747. void segmentationByProgagation(std::vector<int>* triangleTraites, int indice, Vector c, int x, int prof) {
  748.  
  749.    coloreTriaangle(indice, c);
  750.    triangleTraites->push_back(indice);
  751.   /* std::cout<<"Angle entre le triangle et le premier voisin : "<<computeDiedreAngle(indice,TabVoisins[3*indice])<<std::endl;
  752.    std::cout<<"Angle entre le triangle et le deuxième voisin : "<<computeDiedreAngle(indice,TabVoisins[3*indice+1])<<std::endl;
  753.    std::cout<<"Angle entre le triangle et le troisième voisin : "<<computeDiedreAngle(indice,TabVoisins[3*indice+2])<<std::endl;*/
  754.    if(!(std::find(triangleTraites->begin(), triangleTraites->end(), TabVoisins[3*indice])!= triangleTraites->end()))
  755.     if(computeDiedreAngle(indice,TabVoisins[3*indice])>=160 && computeDiedreAngle(indice,TabVoisins[3*indice])<=180 && x<prof)
  756.       segmentationByProgagation(triangleTraites, TabVoisins[3*indice], c, x+1, prof);
  757.  
  758.  
  759.    if(!(std::find(triangleTraites->begin(), triangleTraites->end(), TabVoisins[3*indice+1])!=triangleTraites->end()))
  760.     if(computeDiedreAngle(indice,TabVoisins[3*indice+1])>=160 && computeDiedreAngle(indice,TabVoisins[3*indice+1])<=180 && x<prof)
  761.       segmentationByProgagation(triangleTraites, TabVoisins[3*indice+1], c, x+1, prof);
  762.  
  763.  
  764.     if(!(std::find(triangleTraites->begin(), triangleTraites->end(), TabVoisins[3*indice+2]) != triangleTraites->end()))
  765.       if(computeDiedreAngle(indice,TabVoisins[3*indice+2])>=160 && computeDiedreAngle(indice,TabVoisins[3*indice+2])<=180 && x<prof)
  766.         segmentationByProgagation(triangleTraites, TabVoisins[3*indice+2], c, x+1, prof);
  767. }
  768.  
  769. /* Ici on veut faire une fonction découpe tous les triangles en 4.
  770.  
  771. Pour chaque triangle de tabIndice,
  772. pour le 1er et le 2e point
  773.    on calcule les coordonées du point entre
  774.    on incrémente le nombre de sommets
  775.    on augmente la taille de TabVertice
  776.    on ajoute ce point à tabvertice
  777. pour le 1er et le 3e point
  778.    on calcule les coordonées du point entre
  779.    on incrémente le nombre de sommets
  780.    on augmente la taille de TabVertice
  781.    on ajoute ce point à tabvertice
  782. pour le 2e et le 3e point
  783.    on calcule les coordonées du point entre
  784.    on incrémente le nombre de sommets
  785.    on augmente la taille de TabVertice
  786.    on ajoute ce point à tabvertice
  787.  
  788. */
  789.  
  790.  
  791. void calculNormalesAuxTriangles(int nbPolys) {
  792.   // Reajustement des normales
  793.   for(int i = 0; i<nbPolys; i++) // Trace les normales aux triangles dans TabNormals
  794.   {
  795.     Point* p1 = new Point(TabVertices[3*(TabIndices[i*3])], TabVertices[3*(TabIndices[i*3])+1], TabVertices[3*(TabIndices[i*3])+2]);
  796.     Point* p2 = new Point(TabVertices[3*(TabIndices[i*3+1])], TabVertices[3*(TabIndices[i*3+1])+1], TabVertices[3*(TabIndices[i*3+1])+2]);
  797.     Point* p3 = new Point(TabVertices[3*(TabIndices[i*3+2])], TabVertices[3*(TabIndices[i*3+2])+1], TabVertices[3*(TabIndices[i*3+2])+2]);
  798.  
  799.     Vector* v1 = new Vector(p2->getX()-p1->getX(), p2->getY()-p1->getY(), p2->getZ()-p1->getZ());
  800.     Vector* v2 = new Vector(p3->getX()-p1->getX(), p3->getY()-p1->getY(), p3->getZ()-p1->getZ());
  801.     v1->normalize();
  802.     v2->normalize();
  803.  
  804.     Vector v3 =  v1->Vectoriel(v2);
  805.  
  806.     TabNormals[i*3] = v3.getX();
  807.     TabNormals[i*3+1] = v3.getY();
  808.     TabNormals[i*3+2] = v3.getZ();
  809.   }
  810. }
  811.  
  812.  
  813. void rafinementMaillage() {
  814.  
  815.   int oldNbPolys = nbPolys;
  816.   nbPolys=nbPolys + 4*nbPolys;
  817.  
  818.   int oldNbPoints = nbPoints;
  819.   nbPoints= nbPoints + 3*oldNbPolys;
  820.  
  821.   TabVertices = (float*) realloc(TabVertices, sizeof(float) * (3*nbPoints));
  822.   TabIndices =  (int*)   realloc(TabIndices,  sizeof(int) * (3*nbPolys));
  823.   TabNormals =  (float*) realloc(TabNormals,  sizeof(int) * (3*nbPolys));
  824.  
  825.   // Crée les sous triangles du centre
  826.   int k = oldNbPolys;
  827.   for(int i = 0; i< oldNbPolys; i++) {
  828.  
  829.     TabIndices[3*k] = oldNbPoints+3*i;
  830.     TabIndices[3*k+1] = oldNbPoints+3*i+1;
  831.     TabIndices[3*k+2] = oldNbPoints+3*i+2;
  832.  
  833.    // std::cout<< TabIndices[3*k] <<std::endl;
  834.    // std::cout<< TabIndices[3*k+1]<<std::endl;
  835.    // std::cout<< TabIndices[3*k+2]<<std::endl<<std::endl;
  836.     k++;
  837.   }
  838.  
  839.  
  840.   double x1, x2, x3;
  841.   double y1, y2, y3;
  842.   double z1, z2, z3;
  843.  
  844.   k = 2* oldNbPolys;
  845.  
  846.   for(int i = 0 ; i< oldNbPolys; i++) {
  847.  
  848.    // premier sommet
  849.   x1 = (TabVertices[3*(TabIndices[3*i])] +   TabVertices[3*(TabIndices[3*i+1])  ]) / 2;
  850.   y1 = (TabVertices[3*(TabIndices[3*i])+1] + TabVertices[3*(TabIndices[3*i+1])+1]) / 2;
  851.   z1 = (TabVertices[3*(TabIndices[3*i])+2] + TabVertices[3*(TabIndices[3*i+1])+2]) / 2;
  852.  
  853.   TabVertices[3*TabIndices[3*(oldNbPolys+i)]] =   x1;
  854.   TabVertices[3*TabIndices[3*(oldNbPolys+i)]+1] = y1;
  855.   TabVertices[3*TabIndices[3*(oldNbPolys+i)]+2] = z1;
  856.  
  857.     // second sommet
  858.   x2 = (TabVertices[3*(TabIndices[3*i+1])]  +  TabVertices[3*(TabIndices[3*i+2])  ]) / 2;
  859.   y2 = (TabVertices[3*(TabIndices[3*i+1])+1] + TabVertices[3*(TabIndices[3*i+2])+1]) / 2;
  860.   z2 = (TabVertices[3*(TabIndices[3*i+1])+2] + TabVertices[3*(TabIndices[3*i+2])+2]) / 2;
  861.  
  862.   TabVertices[3*TabIndices[3*(oldNbPolys+i)+1]] =   x2;
  863.   TabVertices[3*TabIndices[3*(oldNbPolys+i)+1]+1] = y2;
  864.   TabVertices[3*TabIndices[3*(oldNbPolys+i)+1]+2] = z2;
  865.  
  866.   // troisieme sommet
  867.  
  868.   x3 = (TabVertices[3*(TabIndices[3*i+2])  ] + TabVertices[3*(TabIndices[3*i])  ]) / 2;
  869.   y3 = (TabVertices[3*(TabIndices[3*i+2])+1] + TabVertices[3*(TabIndices[3*i])+1]) / 2;
  870.   z3 = (TabVertices[3*(TabIndices[3*i+2])+2] + TabVertices[3*(TabIndices[3*i])+2]) / 2;
  871.  
  872.   TabVertices[3*TabIndices[3*(oldNbPolys+i)+2]] =   x3;
  873.   TabVertices[3*TabIndices[3*(oldNbPolys+i)+2]+1] = y3;
  874.   TabVertices[3*TabIndices[3*(oldNbPolys+i)+2]+2] = z3;
  875.  
  876.  
  877.   // premier triangle
  878.     TabIndices[3*k]   = TabIndices[3*(oldNbPolys+i)];
  879.     TabIndices[3*k+1] = TabIndices[3*(oldNbPolys+i)+1];
  880.     TabIndices[3*k+2] = TabIndices[3*i+1];
  881. /*    std::cout<<"premier triangle"<<std::endl;
  882.     std::cout<< TabIndices[3*k] <<std::endl;
  883.     std::cout<< TabIndices[3*k+1]<<std::endl;
  884.     std::cout<< TabIndices[3*k+2]<<std::endl<<std::endl;*/
  885.     k++;
  886.  
  887.   // deuxieme triangle
  888.  
  889.     TabIndices[3*k]   = TabIndices[3*(oldNbPolys+i)];
  890.     TabIndices[3*k+1] = TabIndices[3*i];
  891.     TabIndices[3*k+2] = TabIndices[3*(oldNbPolys+i)+2];
  892. /*    std::cout<<"deuxieme triangle"<<std::endl;
  893.     std::cout<< TabIndices[3*k] <<std::endl;
  894.     std::cout<< TabIndices[3*k+1]<<std::endl;
  895.     std::cout<< TabIndices[3*k+2]<<std::endl<<std::endl;*/
  896.     k++;
  897.  
  898.   // troisieme triangle
  899.     TabIndices[3*k]   = TabIndices[3*i+2];
  900.     TabIndices[3*k+1] = TabIndices[3*(oldNbPolys+i)+1];
  901.     TabIndices[3*k+2] = TabIndices[3*(oldNbPolys+i)+2];
  902. /*    std::cout<<"troisieme triangle"<<std::endl;
  903.     std::cout<< TabIndices[3*k] <<std::endl;
  904.     std::cout<< TabIndices[3*k+1]<<std::endl;
  905.     std::cout<< TabIndices[3*k+2]<<std::endl<<std::endl;*/
  906.     k++;
  907.  
  908. }
  909.  
  910.   // Reajustement des normales
  911.   calculNormalesAuxTriangles(nbPolys);
  912.  
  913. }
  914.  
  915.  
  916. // OK
  917. int pointsEgaux(int P1, int P2) { // Retourne 0 si deux triangles on deux points en communs
  918.   if( (TabVertices[3*TabIndices[3*P1]]   == TabVertices[3*TabIndices[3*P2]])   &&
  919.       (TabVertices[3*TabIndices[3*P1]+1] == TabVertices[3*TabIndices[3*P2]+1]) &&
  920.       (TabVertices[3*TabIndices[3*P1]+2] == TabVertices[3*TabIndices[3*P2]+2]))
  921.     return 0;
  922.  
  923.   else
  924.     if(( TabVertices[3*TabIndices[3*P1+1]] == TabVertices[3*TabIndices[3*P2]])   &&
  925.       (TabVertices[3*TabIndices[3*P1+1]+1] == TabVertices[3*TabIndices[3*P2]+1]) &&
  926.       (TabVertices[3*TabIndices[3*P1+1]+2] == TabVertices[3*TabIndices[3*P2]+2]))
  927.     return 0;
  928.  
  929.   else
  930.     if( (TabVertices[3*TabIndices[3*P1+2]] == TabVertices[3*TabIndices[3*P2]])   &&
  931.       (TabVertices[3*TabIndices[3*P1+2]+1] == TabVertices[3*TabIndices[3*P2]+1]) &&
  932.       (TabVertices[3*TabIndices[3*P1+2]+2] == TabVertices[3*TabIndices[3*P2]+2]))
  933.     return 0;
  934.   else
  935.     if( (TabVertices[3*TabIndices[3*P1+2]] == TabVertices[3*TabIndices[3*P2]])   &&
  936.       (TabVertices[3*TabIndices[3*P1+2]+1] == TabVertices[3*TabIndices[3*P2]+1]) &&
  937.       (TabVertices[3*TabIndices[3*P1+2]+2] == TabVertices[3*TabIndices[3*P2]+2]))
  938.     return 0;
  939.   else
  940.     // ..........................
  941.     return -1;
  942. }
  943.  
  944.  
  945.  
  946. double deuxTrianglesPtsCommuns(int A, int B, int indice1, int indice2) {
  947.     for(int i =0; i<nbPolys; i++) {
  948.      for(int j =0; j<nbPolys; j++) {
  949.         if(pointsEgaux(i,TabVoisins[3*j])==0){
  950.           indice1 = i;
  951.           indice2 = TabVoisins[3*j];
  952.         }
  953.         else if(pointsEgaux(i,TabVoisins[3*j+1])==0) {
  954.           indice1 = i;
  955.           indice2 = TabVoisins[3*j+1];
  956.         }
  957.         else if(pointsEgaux(i,TabVoisins[3*j+2])==0) {
  958.           indice1 = i;
  959.           indice2 = TabVoisins[3*j+2];
  960.         }
  961.       }  
  962.     }
  963.     return 0;
  964. }
  965.  
  966.  
  967. /*  void indicePointRestantTriangle(int A, int B, int oldNbPolys, int &C, int &D) {
  968.     int passe = 1;
  969.  
  970.      for(int i = 0 ; i< oldNbPolys; i++) {
  971.         if( (TabIndices[3*i] == A) && (TabIndices[3*i+1] ==B)) // Si triangle  (A,B,X)
  972.         {
  973.           if(passe==1) {
  974.             C = TabIndices[3*i+2] ;
  975.             passe++;   //  on attribue X à C pour le premier triangle
  976.  
  977.           }
  978.           else if(passe == 2)
  979.             D = TabIndices[3*i+2] ;  // sinon on attribue X à D pour le 2e triangle (cf schéma)
  980.  
  981.         }
  982.  
  983.         if( (TabIndices[3*i] == A) && (TabIndices[3*i+2] == B)) // Si triangle  (A,X,B)
  984.         {
  985.           if(passe==1) {
  986.             C = TabIndices[3*i+1];
  987.             passe++;   //  on attribue X à C pour le premier triangle
  988.  
  989.           }
  990.           else if(passe == 2)
  991.             D = TabIndices[3*i+1];  // sinon on attribue X à D pour le 2e triangle (cf schéma)
  992.  
  993.         }
  994.  
  995.         if( (TabIndices[3*i+1] == A) && (TabIndices[3*i+2] == B)) // Si triangle  (X,A,B)
  996.         {
  997.           if(passe==1) {
  998.             C = TabIndices[3*i];
  999.             passe++;   //  on attribue X à C pour le premier triangle
  1000.  
  1001.           }
  1002.           else if(passe == 2)
  1003.             D = TabIndices[3*i];  // sinon on attribue X à D pour le 2e triangle (cf schéma)
  1004.  
  1005.         }
  1006.  
  1007.         if( (TabIndices[3*i+2] == A) && (TabIndices[3*i+1] == B)) // Si triangle  (X,B,A)
  1008.         {
  1009.           if(passe==1) {
  1010.             C = TabIndices[3*i];
  1011.             passe++;   //  on attribue X à C pour le premier triangle
  1012.  
  1013.           }
  1014.           else if(passe == 2)
  1015.             D = TabIndices[3*i];  // sinon on attribue X à D pour le 2e triangle (cf schéma)
  1016.  
  1017.         }
  1018.  
  1019.         if( (TabIndices[3*i+2] == A) && (TabIndices[3*i] == B)) // Si triangle  (B,X,A)
  1020.         {
  1021.           if(passe==1) {
  1022.             C = TabIndices[3*i+1];
  1023.             passe++;   //  on attribue X à C pour le premier triangle
  1024.  
  1025.           }
  1026.           else if(passe == 2)
  1027.             D = TabIndices[3*i+1];  // sinon on attribue X à D pour le 2e triangle (cf schéma)
  1028.  
  1029.         }
  1030.  
  1031.         if( (TabIndices[3*i+1] == A) && (TabIndices[3*i] == B)) // Si triangle  (B,A,X)
  1032.         {
  1033.           if(passe==1) {
  1034.             C = TabIndices[3*i+2];
  1035.             passe++;   //  on attribue X à C pour le premier triangle
  1036.  
  1037.           }
  1038.           else if(passe == 2)
  1039.             D = TabIndices[3*i+2];  // sinon on attribue X à D pour le 2e triangle (cf schéma)
  1040.         }
  1041.  
  1042.      }
  1043.   }
  1044. */
  1045.  
  1046.   int indicePointRestantTriangle(int A, int B, int oldNbPolys, int C) {
  1047.  
  1048.      for(int i = 0 ; i< oldNbPolys; i++) {
  1049.         if( (TabIndices[3*i] == A) && (TabIndices[3*i+1] ==B) && (TabIndices[3*i+2]!=C)) // Si triangle  (A,B,X)
  1050.           return TabIndices[3*i+2];
  1051.  
  1052.         if( (TabIndices[3*i] == A) && (TabIndices[3*i+2] == B) && (TabIndices[3*i+1]!=C)) // Si triangle  (A,X,B)
  1053.           return TabIndices[3*i+1];
  1054.  
  1055.         if( (TabIndices[3*i+1] == A) && (TabIndices[3*i+2] == B) && (TabIndices[3*i]!=C)) // Si triangle  (X,A,B)
  1056.           return TabIndices[3*i];
  1057.  
  1058.         if( (TabIndices[3*i+2] == A) && (TabIndices[3*i+1] == B) && (TabIndices[3*i]!=C)) // Si triangle  (X,B,A)
  1059.           return TabIndices[3*i];
  1060.  
  1061.         if( (TabIndices[3*i+2] == A) && (TabIndices[3*i] == B) && (TabIndices[3*i+1]!=C)) // Si triangle  (B,X,A)
  1062.           return TabIndices[3*i+1];
  1063.  
  1064.         if( (TabIndices[3*i+1] == A) && (TabIndices[3*i] == B) && (TabIndices[3*i+2]!=C)) // Si triangle  (B,A,X)
  1065.           return TabIndices[3*i+2];
  1066.      }
  1067.      return -1;
  1068.   }
  1069.  
  1070. double calculCoordButterFlyX(int A, int B) {
  1071.  
  1072.   return (TabVertices[A] + TabVertices[B]) / 2;
  1073. }
  1074.  
  1075. double calculCoordButterFly(int A, int B, int C, int D, int E, int F, int G, int H) {
  1076.  
  1077.   return ((TabVertices[A]/2) + TabVertices[B]/2 + TabVertices[C]/8 + TabVertices[D]/8 - TabVertices[G]/16 - TabVertices[H]/16 - TabVertices[F]/16 - TabVertices[E]/16);
  1078. }
  1079.  
  1080.  
  1081. void butterFly() {
  1082.  
  1083.   int oldNbPolys = nbPolys;
  1084.   nbPolys=nbPolys + 7*nbPolys;
  1085.  
  1086.   int oldNbPoints = nbPoints;
  1087.   nbPoints= nbPoints + 3*oldNbPolys;
  1088.  
  1089.   TabVertices = (float*) realloc(TabVertices, sizeof(float) * (3*nbPoints));
  1090.   TabIndices =  (int*)   realloc(TabIndices,  sizeof(int) * (3*nbPolys));
  1091.   TabNormals =  (float*) realloc(TabNormals,  sizeof(int) * (3*nbPolys));
  1092.   TabVoisins = (int*)   realloc(TabVoisins, sizeof(int)*(3*nbPolys));
  1093.  
  1094.   // Crée les sous triangles du centre
  1095.   int k = oldNbPolys;
  1096.   for(int i = 0; i< oldNbPolys; i++) {
  1097.  
  1098.     TabIndices[3*k] = oldNbPoints+3*i;
  1099.     TabIndices[3*k+1] = oldNbPoints+3*i+1;
  1100.     TabIndices[3*k+2] = oldNbPoints+3*i+2;
  1101.  
  1102.    /* std::cout<< TabIndices[3*k] <<std::endl;
  1103.     std::cout<< TabIndices[3*k+1]<<std::endl;
  1104.     std::cout<< TabIndices[3*k+2]<<std::endl<<std::endl;*/
  1105.     k++;
  1106.   }
  1107.  
  1108.  
  1109.   double x1, x2, x3;
  1110.   double y1, y2, y3;
  1111.   double z1, z2, z3;
  1112.  
  1113.   k = 2* oldNbPolys;
  1114.  
  1115.   for(int i = 0 ; i< oldNbPolys; i++) {
  1116.  
  1117.     // Pour chaque arrête du poly en question il faut faire le calcule suivant :
  1118.     // Chercher les deux sommets qui font partie des triangles qui contiennent A et B en sommets
  1119.  
  1120.  
  1121.    // premier sommet
  1122.     int A = TabIndices[3*i];
  1123.     int B = TabIndices[3*i+1];
  1124.     int C = TabIndices[3*i+2];
  1125.    // std::cout<<"Point A :"<<A<<" point B : "<<B<<" point C : "<<C<<"\n";
  1126.  
  1127.     int D = -1;
  1128.     int G = -1;
  1129.     int H = -1;
  1130.     int F = -1;
  1131.     int E = -1;
  1132.  
  1133.     D = indicePointRestantTriangle(A,B, oldNbPolys, C); // Point D, symétrique de C par rapport à AB
  1134.     G = indicePointRestantTriangle(D,B, oldNbPolys, A); // Point G, symétrique de A par rapport à DB
  1135.     H = indicePointRestantTriangle(A,D, oldNbPolys, B); // Point H, symétrique de B par rapport à AD
  1136.     F = indicePointRestantTriangle(A,C, oldNbPolys, B); // Point F, symétrique de B par rapport à AC
  1137.     E = indicePointRestantTriangle(C,B, oldNbPolys, A); // Point E, symétrique de A par rapport à CB
  1138.  
  1139.   x1 = calculCoordButterFly(3*(A) , 3*(B), 3*C, 3*D, 3*E, 3*F, 3*G, 3*H);
  1140.   y1 = calculCoordButterFly(3*(A)+1, 3*(B)+1, 3*C+1, 3*D+1, 3*E+1, 3*F+1, 3*G+1, 3*H+1);
  1141.   z1 = calculCoordButterFly(3*(A)+2, 3*(B)+2, 3*C+2, 3*D+2, 3*E+2, 3*F+2, 3*G+2, 3*H+2);
  1142.  
  1143.   TabVertices[3*TabIndices[3*(oldNbPolys+i)]] =   x1;
  1144.   TabVertices[3*TabIndices[3*(oldNbPolys+i)]+1] = y1;
  1145.   TabVertices[3*TabIndices[3*(oldNbPolys+i)]+2] = z1;
  1146.  
  1147.  
  1148.   TabIndices[3*k]   = TabIndices[3*(oldNbPolys+i)]; // Ajout triangle (A,B,MILIEU)
  1149.   TabIndices[3*k+1] = A;
  1150.   TabIndices[3*k+2] = B;
  1151.   k++;
  1152.  
  1153.     // second sommet
  1154.     A = TabIndices[3*i];
  1155.     B = TabIndices[3*i+2];
  1156.     C = TabIndices[3*i+1];
  1157.     //std::cout<<"Point A :"<<A<<" point B : "<<B<<" point C : "<<C<<"\n";
  1158.  
  1159.     D = -1;
  1160.     G = -1;
  1161.     H = -1;
  1162.     F = -1;
  1163.     E = -1;
  1164.  
  1165.     D = indicePointRestantTriangle(A,B, oldNbPolys, C); // Point D, symétrique de C par rapport à AB
  1166.     G = indicePointRestantTriangle(D,B, oldNbPolys, A); // Point G, symétrique de A par rapport à DB
  1167.     H = indicePointRestantTriangle(A,D, oldNbPolys, B); // Point H, symétrique de B par rapport à AD
  1168.     F = indicePointRestantTriangle(A,C, oldNbPolys, B); // Point F, symétrique de B par rapport à AC
  1169.     E = indicePointRestantTriangle(C,B, oldNbPolys, A); // Point E, symétrique de A par rapport à CB
  1170.  
  1171.  
  1172.   x2 = calculCoordButterFly(3*(A) , 3*(B), 3*C, 3*D, 3*E, 3*F, 3*G, 3*H);
  1173.   y2 = calculCoordButterFly(3*(A)+1, 3*(B)+1, 3*C+1, 3*D+1, 3*E+1, 3*F+1, 3*G+1, 3*H+1);  
  1174.   z2 = calculCoordButterFly(3*(A)+2, 3*(B)+2, 3*C+2, 3*D+2, 3*E+2, 3*F+2, 3*G+2, 3*H+2);
  1175.  
  1176.   TabVertices[3*TabIndices[3*(oldNbPolys+i)+1]] =   x2;
  1177.   TabVertices[3*TabIndices[3*(oldNbPolys+i)+1]+1] = y2;
  1178.   TabVertices[3*TabIndices[3*(oldNbPolys+i)+1]+2] = z2;
  1179.  
  1180.   TabIndices[3*k]   = TabIndices[3*(oldNbPolys+i)+1]; // Ajout triangle (A,B,MILIEU)
  1181.   TabIndices[3*k+1] = A;
  1182.   TabIndices[3*k+2] = B;
  1183.   k++;
  1184.  
  1185.   // troisieme sommet
  1186.  
  1187.     A = TabIndices[3*i+2];
  1188.     B = TabIndices[3*i+1];
  1189.     C = TabIndices[3*i];
  1190. //    std::cout<<"Point A :"<<A<<" point B : "<<B<<" point C : "<<C<<"\n";
  1191.  
  1192.     D = -1;
  1193.     G = -1;
  1194.     H = -1;
  1195.     F = -1;
  1196.     E = -1;
  1197.  
  1198.     D = indicePointRestantTriangle(A,B, oldNbPolys, C); // Point D, symétrique de C par rapport à AB
  1199.     G = indicePointRestantTriangle(D,B, oldNbPolys, A); // Point G, symétrique de A par rapport à DB
  1200.     H = indicePointRestantTriangle(A,D, oldNbPolys, B); // Point H, symétrique de B par rapport à AD
  1201.     F = indicePointRestantTriangle(A,C, oldNbPolys, B); // Point F, symétrique de B par rapport à AC
  1202.     E = indicePointRestantTriangle(C,B, oldNbPolys, A); // Point E, symétrique de A par rapport à CB
  1203.  
  1204.  
  1205.  
  1206.   x3 = calculCoordButterFly(3*(A) , 3*(B), 3*C, 3*D, 3*E, 3*F, 3*G, 3*H);
  1207.   y3 = calculCoordButterFly(3*(A)+1, 3*(B)+1, 3*C+1, 3*D+1, 3*E+1, 3*F+1, 3*G+1, 3*H+1);  
  1208.   z3 = calculCoordButterFly(3*(A)+2, 3*(B)+2, 3*C+2, 3*D+2, 3*E+2, 3*F+2, 3*G+2, 3*H+2);
  1209.  
  1210.   TabVertices[3*TabIndices[3*(oldNbPolys+i)+2]] =   x3;
  1211.   TabVertices[3*TabIndices[3*(oldNbPolys+i)+2]+1] = y3;
  1212.   TabVertices[3*TabIndices[3*(oldNbPolys+i)+2]+2] = z3;
  1213.  
  1214.   TabIndices[3*k]   = TabIndices[3*(oldNbPolys+i)+2]; // Ajout triangle (A,B,MILIEU)
  1215.   TabIndices[3*k+1] = A;
  1216.   TabIndices[3*k+2] = B;
  1217.   k++;
  1218.  
  1219.  
  1220.  
  1221.   // premier triangle
  1222.     TabIndices[3*k]   = TabIndices[3*(oldNbPolys+i)];
  1223.     TabIndices[3*k+1] = TabIndices[3*(oldNbPolys+i)+1];
  1224.     TabIndices[3*k+2] = TabIndices[3*i+1];
  1225. /*    std::cout<<"premier triangle"<<std::endl;
  1226.     std::cout<< TabIndices[3*k] <<std::endl;
  1227.     std::cout<< TabIndices[3*k+1]<<std::endl;
  1228.     std::cout<< TabIndices[3*k+2]<<std::endl<<std::endl;*/
  1229.     k++;
  1230.  
  1231.   // deuxieme triangle
  1232.  
  1233.     TabIndices[3*k]   = TabIndices[3*(oldNbPolys+i)];
  1234.     TabIndices[3*k+1] = TabIndices[3*i];
  1235.     TabIndices[3*k+2] = TabIndices[3*(oldNbPolys+i)+2];
  1236. /*    std::cout<<"deuxieme triangle"<<std::endl;
  1237.     std::cout<< TabIndices[3*k] <<std::endl;
  1238.     std::cout<< TabIndices[3*k+1]<<std::endl;
  1239.     std::cout<< TabIndices[3*k+2]<<std::endl<<std::endl;*/
  1240.     k++;
  1241.  
  1242.   // troisieme triangle
  1243.     TabIndices[3*k]   = TabIndices[3*i+2];
  1244.     TabIndices[3*k+1] = TabIndices[3*(oldNbPolys+i)+1];
  1245.     TabIndices[3*k+2] = TabIndices[3*(oldNbPolys+i)+2];
  1246. /*    std::cout<<"troisieme triangle"<<std::endl;
  1247.     std::cout<< TabIndices[3*k] <<std::endl;
  1248.     std::cout<< TabIndices[3*k+1]<<std::endl;
  1249.     std::cout<< TabIndices[3*k+2]<<std::endl<<std::endl;*/
  1250.     k++;
  1251.  
  1252. }
  1253.   // Reajustement des normales
  1254.   calculNormalesAuxTriangles(nbPolys);
  1255.   // Recalcul des voisins
  1256.   fillVoisinTab(TabIndices, TabVoisins, nbPolys);
  1257.  
  1258. }
  1259.  
  1260. //////////////////////////////////////////////////////////////////////////////////////////
  1261. // Fonction que vous allez modifier afin de dessiner
  1262. /////////////////////////////////////////////////////////////////////////////////////////
  1263.  
  1264. void render_scene()
  1265. {
  1266.  
  1267.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1268.   glLoadIdentity();
  1269.  
  1270.   glColor3f(1.0, 1.0, 0.0); // RED
  1271.  
  1272.   //computeDiedreAngle(0, 78);
  1273.  
  1274.   //computeDiedreAngle(0,1);
  1275.  
  1276.    if(testRafinement == RAFINEMENT)
  1277.     rafinementMaillage();
  1278.   testRafinement = -1;
  1279.  
  1280.    if(testButterfly == BUTTERFLY)
  1281.     butterFly();
  1282.  
  1283.  
  1284.  
  1285.   testButterfly = -1;
  1286. /*  Point* ct = new Point(sizefenetreX/2,sizefenetreY/2,sizefenetreZ/2);
  1287.   double l = sizefenetreX ;
  1288.   Octree* o = new Octree(ct, l);
  1289.   o->displayV();*/
  1290.  
  1291. /*
  1292.   std::cout<<"Taille TabVertices : "<<sizeof(TabVertices) / sizeof(TabVertices[0]);
  1293.   std::cout<<"Taille TabIndices : "<<sizeof(TabIndices) / sizeof(TabIndices[0]);*/
  1294.  
  1295.   // gluLookAt(0,0,-1,0,0,0,0,1,0);
  1296.   gluLookAt(  xp, 0.0f, zp, xp+lxp, 0.0f,  zp+lzp, 0.0f, 0.25f, 0.0f);
  1297.  
  1298.   GLfloat lightColor0[] = {1.0f, 1.0f, 1.0f, 1.0f};
  1299.   GLfloat lightPos0[] = {3.0f, 10.0f, 3.0f, 1.0f};
  1300.   GLfloat Lt0spec[] = {1.0, 1.0, 1.0, 1.0};
  1301.   GLfloat ambient0[] = {0.1, 0.1, 0.1, 1.0};
  1302.   glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
  1303.   glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
  1304.   glLightfv(GL_LIGHT0, GL_SPECULAR, Lt0spec);
  1305.   glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);
  1306.  
  1307.   GLfloat lightColor1[] = {1.0f, 1.0f, 1.0f, 1.0f};
  1308.   GLfloat lightPos1[] = {-3.0f, 10.0f, -3.0f, 1.0f};
  1309.   glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
  1310.   glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
  1311.   glLightfv(GL_LIGHT1, GL_SPECULAR, Lt0spec);
  1312.   glLightfv(GL_LIGHT1, GL_AMBIENT, ambient0);
  1313.  
  1314.   glColorMaterial (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
  1315. /*  glColorMaterial (GL_FRONT_AND_BACK, GL_SPECULAR);*/
  1316.   glEnable (GL_COLOR_MATERIAL);
  1317.  
  1318.  
  1319.  
  1320.   glEnableClientState (GL_VERTEX_ARRAY);
  1321.   glEnableClientState (GL_NORMAL_ARRAY);
  1322.  
  1323.   glVertexPointer (3, GL_FLOAT, 0, TabVertices);
  1324.   glNormalPointer(GL_FLOAT, 0, TabNormals);
  1325.  
  1326.   if(testFill == FILL)
  1327.     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  1328.   else
  1329.     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  1330.   glDrawElements (GL_TRIANGLES, 3*(nbPolys), GL_UNSIGNED_INT,  TabIndices);
  1331.   glDisableClientState (GL_VERTEX_ARRAY);
  1332.   glDisableClientState (GL_NORMAL_ARRAY);
  1333.  
  1334. /*  if(image_gaussienne == IMG_GAUSS)
  1335.     constructImgGauss();*/
  1336.  
  1337.   glPointSize(6);
  1338.   //testAretesVives(91, Vector (0, 0, 255));
  1339.   int profondeur = 1500;
  1340.   std::vector<int>* triangleTraites = new std::vector<int>();
  1341.   // segmentationByProgagation(triangleTraites, 3, Vector (0, 255, 255), 0, profondeur); // quart du côté du socle en bleu clair
  1342.   if(testSeg>0)
  1343.     segmentationByProgagation(triangleTraites, 1, Vector (0, 255, 0), 0, profondeur); // cercle du dessus du socle en vert
  1344.   if(testSeg>1)
  1345.     segmentationByProgagation(triangleTraites, 0, Vector (0, 255, 255), 0, profondeur); // cercle du dessous du socle en jaune
  1346.   if(testSeg>2)
  1347.     segmentationByProgagation(triangleTraites, 1000, Vector (0, 0, 255), 0, profondeur); // sphere en haut en bleu
  1348.   if(testSeg>3)
  1349.     segmentationByProgagation(triangleTraites, 103, Vector (255, 0, 255), 0, profondeur); // premier creux
  1350.   if(testSeg>4)
  1351.     segmentationByProgagation(triangleTraites, 1753, Vector (255, 0, 255), 0, profondeur); // deuxieme creux
  1352.   if(testSeg>5)
  1353.     segmentationByProgagation(triangleTraites, 1700, Vector (255, 255, 0), 0, profondeur); // tube
  1354.  
  1355.   glutSwapBuffers();
  1356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement