ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/REPOS_ERICCA/magic/lib/CAD4FE/src/CAD4FE_mailleur2d.cpp
Revision: 27
Committed: Thu Jul 5 15:26:40 2007 UTC (17 years, 10 months ago) by foucault
Original Path: magic/lib/CAD4FE/CAD4FE/src/CAD4FE_mailleur2d.cpp
File size: 36011 byte(s)
Log Message:

File Contents

# User Rev Content
1 foucault 27 //------------------------------------------------------------
2     //------------------------------------------------------------
3     // MAGiC
4     // Jean Christophe Cuillière et Vincent FRANCOIS
5     // Département de Génie Mécanique - UQTR
6     //------------------------------------------------------------
7     // Le projet MAGIC est un projet de recherche du département
8     // de génie mécanique de l'Université du Québec à
9     // Trois Rivières
10     // Les librairies ne peuvent être utilisées sans l'accord
11     // des auteurs (contact : francois@uqtr.ca)
12     //------------------------------------------------------------
13     //------------------------------------------------------------
14     //
15     // mailleur2d.cpp
16     //
17     //------------------------------------------------------------
18     //------------------------------------------------------------
19     // COPYRIGHT 2000
20     // Version du 02/03/2006 à 11H23
21     //------------------------------------------------------------
22     //------------------------------------------------------------
23    
24    
25     #include "gestionversion.h"
26     #include "CAD4FE_mailleur2d.h"
27     #include "ot_mathematique.h"
28     //#include "message.h"
29     //#include "affiche.h"
30     #include <fstream>
31     #include <math.h>
32     #include <time.h>
33    
34     #include <CAD4FE_MCEdge.h>
35     #include <CAD4FE_MCVertex.h>
36     #include "CAD4FE_geometric_tools.h"
37     #include "CAD4FE_InventorText_MG_MAILLAGE.h"
38     #include "CAD4FE_InventorText_MCSegment.h"
39     #include "CAD4FE_OptimizeEdgeSwap.h"
40    
41     using namespace CAD4FE;
42    
43     MAILLEUR2D::MAILLEUR2D(MG_MAILLAGE* mgmai,MG_GEOMETRIE *mggeo,MCFace* __mcFace,FCT_TAILLE* fct_taille):MAILLEUR(),mg_maillage(mgmai),mg_geometrie(mggeo),_mcFace(__mcFace),metrique(fct_taille)
44     {
45     strcpy(filenameNbTriangles,getenv("TEMP"));
46     strcat(filenameNbTriangles,"CAD4FE_mailleur2d.txt");
47     }
48    
49    
50    
51     MAILLEUR2D::~MAILLEUR2D()
52     {
53     }
54    
55    
56    
57    
58     void MAILLEUR2D::maille(void)
59     {
60     if (_mcFace!=NULL) maille(_mcFace);
61     else
62     {
63     // Il faudrait mailler les MC Face de la géométrie :
64     // A faire : une fonction qui parcours les MCFace de la géométrie
65     /* int nb_face=mg_geometrie->get_nb_mg_face();
66     for (int i=0;i<nb_face;i++)
67     {
68     MG_FACE* mgface=mg_geometrie->get_mg_face(i);
69     maille(mgface);
70     } */
71     }
72     }
73    
74     void MAILLEUR2D::maille(MCFace * __mcFace)
75     {
76     creation_metrique=0;
77     lst_tri_qual.clear();
78     //afficheur << MAILLAGEFACE << mgface->get_id() << endaff;
79     initialise_frontiere(__mcFace);
80     cree_grille(__mcFace);
81     initialise_front(__mcFace);
82    
83     if (metrique==NULL)
84     {
85     printf("Mailleur2D::Maille --> Abandon: \nLa fonction carte de taille doit être spécifiée !\n");
86     return;
87     }
88     progresse_front(__mcFace);
89     if (debug){
90     char filename1[5000];
91     strcpy(filename1,getenv("TEMP"));
92     strcat(filename1,"\\void_2D.mai");
93     ofstream o4(filename1,ios::out|ios::trunc);
94     o4.precision(16);
95     o4.setf(ios::showpoint);
96     mg_maillage->enregistrer_sous_mesh_2D(o4);
97    
98     std::ofstream file;
99     char filename2[5000];
100     strcpy(filename2,getenv("TEMP"));
101     strcat(filename2,"\\CAD4FE_Mesher.iv");
102     file.open(filename2);
103     CAD4FE::InventorText_MG_MAILLAGE ivTextRefTess (mg_maillage);
104     ivTextRefTess.ShowFaceSegments = true;
105     ivTextRefTess.ShowSegmentId = false;
106     ivTextRefTess.ShowTriangleId = false;
107     ivTextRefTess.ShowNodeId = false;
108     ivTextRefTess.InverseTriangleNormals = true;
109     ivTextRefTess.TriangleColor=1;
110     file << ivTextRefTess.GetText();
111     file << "\n}\n";
112     file.close();
113     }
114     if ( 0 )
115     {
116     std::ofstream file;
117     char filename2[5000];
118     strcpy(filename2,getenv("TEMP"));
119     strcat(filename2,"\\CAD4FE_Mesher_MCSegments.iv");
120     file.open(filename2);
121     InventorText_MG_MAILLAGE meshIvText (mg_maillage);
122     meshIvText.ShowFaceSegments = true;
123     meshIvText.ShowSegmentId = true;
124     meshIvText.ShowTriangleId = true;
125     meshIvText.ShowNodeId = true;
126     meshIvText.InverseTriangleNormals = true;
127     meshIvText.TriangleColor=1;
128     file<<meshIvText.GetText();
129    
130     file << "DrawStyle {\nstyle LINES\nlineWidth 2\npointSize 3\n}\n";
131     LISTE_MG_SEGMENT::iterator itSegment;
132     for (MG_SEGMENT * seg = mg_maillage->get_premier_segment(itSegment); seg; seg = mg_maillage->get_suivant_segment(itSegment))
133     {
134     if (seg->get_lien_topologie()->get_dimension() == 1)
135     file << "BaseColor { \n rgb 0.0 0.0 0.9\n }\n";
136     if (seg->get_lien_topologie()->get_dimension() == 2)
137     file << "BaseColor { \n rgb 0.0 0.0 0.0\n }\n";
138     MCSegment * mcSeg = (MCSegment *) seg;
139     InventorText_MCSegment mcSegTxt (mcSeg);
140     file << mcSegTxt.GetText();
141     }
142    
143     file << "\n}\n";
144     file.close();
145     }
146    
147     optimise_bascule_segment(__mcFace);
148     optimise(__mcFace);
149    
150     {
151     std::ofstream file(filenameNbTriangles);
152     file.clear();
153     file.flush();
154     file.close();
155     }
156    
157    
158     if (creation_metrique==1)
159     {
160     delete metrique;
161     metrique=NULL;
162     }
163     delete grille_de_segment;
164     delete grille_de_frontiere;
165     delete grille_de_front;
166     segment_frontiere.vide();
167     noeud_frontiere.vide();
168     //ofstream o3("test_2D.mai",ios::out|ios::trunc);
169     //o3.precision(16);
170     //o3.setf(ios::showpoint);
171     //mg_maillage->enregistrer_sous_mesh_2D(o3);
172     }
173    
174    
175    
176    
177    
178     void MAILLEUR2D::initialise_frontiere(MCFace* __mcFace)
179     {
180     int nb_boucle=__mcFace->get_nb_mg_boucle();
181     for (int i=0;i<nb_boucle;i++)
182     {
183     MG_BOUCLE* mgboucle=__mcFace->get_mg_boucle(i);
184     int nb_coarete=mgboucle->get_nb_mg_coarete();
185     for (int j=0;j<nb_coarete;j++)
186     {
187     MG_COARETE* coarete=mgboucle->get_mg_coarete(j);
188     MCEdge* mgarete=(MCEdge*)coarete->get_arete();
189     int nb_segment=mgarete->get_lien_maillage()->get_nb();
190     for (int k=0;k<nb_segment;k++)
191     {
192     refresh();
193     MG_SEGMENT* mgsegment=(MG_SEGMENT*)mgarete->get_lien_maillage()->get(k);
194     MG_SEGMENT* mgsegtemp=(MG_SEGMENT*)mg_maillage->get_mg_segmentid(mgsegment->get_id());
195     if (mgsegtemp==NULL) continue;
196     MCSegment * mcSegment = (MCSegment*) mgsegment;
197     MCNode* mcNode=(MCNode*)mcSegment->get_noeud1();
198     segment_frontiere.ajouter(mcSegment);
199     noeud_frontiere.ajouter(mcNode);
200     mcNode=(MCNode*)mcSegment->get_noeud2();
201     if (!noeud_frontiere.est_dans_la_liste(mcNode));
202     {
203     noeud_frontiere.ajouter(mcNode);
204     }
205     }
206    
207     }
208     }
209    
210    
211     }
212    
213     void MAILLEUR2D::cree_grille(MCFace* __mcFace)
214     {
215     double bounding_box[6]={1E308,1E308,1E308,-1E308,-1E308,-1E308};
216     TPL_SET<MG_ELEMENT_MAILLAGE*>::ITERATEUR itElem;
217     for (MG_ELEMENT_MAILLAGE* elem = __mcFace->get_lien_maillage()->get_premier(itElem);
218     elem; elem = __mcFace->get_lien_maillage()->get_suivant(itElem))
219     {
220     MG_TRIANGLE * triang = (MG_TRIANGLE *) elem;
221     MG_NOEUD * n[3];
222     n[0] = triang->get_noeud1();
223     n[1] = triang->get_noeud2();
224     n[2] = triang->get_noeud3();
225     for (unsigned int i=0; i<3; i++)
226     {
227     for (unsigned int j=0; j<3; j++)
228     {
229     if (n[i]->get_coord()[j] < bounding_box[j])
230     bounding_box[j] = n[i]->get_coord()[j];
231     if (n[i]->get_coord()[j] > bounding_box[j+3])
232     bounding_box[j+3] = n[i]->get_coord()[j];
233     }
234     }
235     }
236    
237     double xyz_taille [3];
238     for (unsigned int i=0; i<3; i++)
239     xyz_taille[i] = (bounding_box[i]+bounding_box[i+3])*.5;
240    
241     double taille;
242     double density_tensor[9];
243     metrique->evaluer(xyz_taille, density_tensor);
244     taille = pow(density_tensor[0],-0.5);
245    
246     int nb_cellule[3];
247     for (unsigned i = 0; i < 3; i++)
248     {
249     double taille_cellule = taille;
250    
251     if ( taille_cellule != 0 )
252     nb_cellule[i] = (bounding_box[i+3]-bounding_box[i])/taille_cellule;
253     else
254     nb_cellule[i] = 1;
255    
256     if (nb_cellule[i] == 0)
257     nb_cellule[i] ++;
258     }
259    
260     const double NB_MAX_CELL = 200000;
261     double alpha = NB_MAX_CELL / (nb_cellule[0]*nb_cellule[1]*nb_cellule[2]);
262     if (alpha < 1)
263     {
264     for (unsigned i = 0; i < 3; i++)
265     {
266     nb_cellule[i] = (int)((double)nb_cellule[i])*pow(alpha,.33);
267     if ( nb_cellule[i] == 0 )
268     nb_cellule[i] = 1;
269     }
270     printf("Nb cells = %d\n", nb_cellule[0]*nb_cellule[1]*nb_cellule[2]);
271     }
272    
273     // increase grid's size by 100% of mesh size
274     for (int i=0; i<3; i++)
275     bounding_box[i] -= taille*1.0;
276     for (int i=0; i<3; i++)
277     bounding_box[i+3] += taille*1.0;
278    
279     grille_de_segment = new TPL_GRILLE<MCSegment*>;
280     grille_de_segment->initialiser(bounding_box[0], bounding_box[1], bounding_box[2], bounding_box[3], bounding_box[4], bounding_box[5], nb_cellule[0], nb_cellule[1], nb_cellule[2]);
281     grille_de_frontiere = new TPL_GRILLE<MCSegment*>;
282     grille_de_frontiere->initialiser(bounding_box[0], bounding_box[1], bounding_box[2], bounding_box[3], bounding_box[4], bounding_box[5], nb_cellule[0], nb_cellule[1], nb_cellule[2]);
283     grille_de_front = new TPL_GRILLE<MG_FRONT_2D*>;
284     grille_de_front->initialiser(bounding_box[0], bounding_box[1], bounding_box[2], bounding_box[3], bounding_box[4], bounding_box[5], nb_cellule[0], nb_cellule[1], nb_cellule[2]);
285     }
286    
287     void MAILLEUR2D::initialise_front(MCFace* __mcFace)
288     {
289     MG_FRONT_2D::initialise_compteur_id();
290     int nb_boucle=__mcFace->get_nb_mg_boucle();
291     for (int iboucle=0;iboucle<nb_boucle;iboucle++)
292     {
293     unsigned int nb_front_avant_cette_boucle=get_nb_front(front_courant);
294     MG_BOUCLE* mgboucle=__mcFace->get_mg_boucle(iboucle);
295     unsigned int nbcoaretetotale=mgboucle->get_nb_mg_coarete();
296     MG_COARETE* mgcoarete=mgboucle->get_mg_coarete(0);
297     MCNode* noeud_de_depart;
298     int bon_noeud=0;
299     int numnoeud=0;
300     while (bon_noeud==0)
301     {
302     if (mgcoarete->get_orientation()==MEME_SENS) noeud_de_depart=(MCNode*)mgcoarete->get_arete()->get_cosommet1()->get_sommet()->get_lien_maillage()->get(numnoeud);
303     else noeud_de_depart=(MCNode*)mgcoarete->get_arete()->get_cosommet2()->get_sommet()->get_lien_maillage()->get(numnoeud);
304     MG_NOEUD* noeudtemp=mg_maillage->get_mg_noeudid(noeud_de_depart->get_id());
305     if (noeudtemp==NULL) numnoeud++; else bon_noeud=1;
306     }
307     MG_FRONT_2D* premier_front;
308     MCNode* noeud_courant=noeud_de_depart;
309     MCSegment* segment_courant=NULL;
310     MG_FRONT_2D* front_precedent=NULL;
311     unsigned int nbcoaretetraite=0;
312     do
313     {
314     nbcoaretetraite++;
315     MCNode* noeud_d_arrivee;
316     int bon_noeud=0;
317     int numnoeud=0;
318     while (bon_noeud==0)
319     {
320     if (mgcoarete->get_orientation()==MEME_SENS) noeud_d_arrivee=(MCNode*)mgcoarete->get_arete()->get_cosommet2()->get_sommet()->get_lien_maillage()->get(numnoeud);
321     else noeud_d_arrivee=(MCNode*)mgcoarete->get_arete()->get_cosommet1()->get_sommet()->get_lien_maillage()->get(numnoeud);
322     MG_NOEUD* noeudtemp=mg_maillage->get_mg_noeudid(noeud_d_arrivee->get_id());
323     if (noeudtemp==NULL) numnoeud++; else bon_noeud=1;
324     }
325     MCEdge* mgarete=(MCEdge*)mgcoarete->get_arete();
326     segment_courant=NULL;
327     int passe_aretefermee=0;
328     do
329     {
330     int trouve=0;
331     int i;
332     if ((mgcoarete->get_arete()->get_cosommet1()->get_sommet()==mgcoarete->get_arete()->get_cosommet2()->get_sommet())&&(passe_aretefermee==0))
333     {
334     passe_aretefermee=1;
335     MCSegment* mgsegment_depart1=NULL;
336     MCSegment* mgsegment_depart2=NULL;
337     TPL_SET<MG_ELEMENT_MAILLAGE*>::ITERATEUR it;
338     MCSegment* mgsegment=(MCSegment*)mgarete->get_lien_maillage()->get_premier(it);
339     do
340     {
341     if ((mgsegment->get_noeud1()==noeud_courant) || (mgsegment->get_noeud2()==noeud_courant)) mgsegment_depart1=mgsegment;
342     mgsegment=(MCSegment*)mgarete->get_lien_maillage()->get_suivant(it);
343     }
344     while (mgsegment_depart1==NULL);
345     do
346     {
347     if ((mgsegment->get_noeud1()==noeud_courant) || (mgsegment->get_noeud2()==noeud_courant)) mgsegment_depart2=mgsegment;
348     mgsegment=(MCSegment*)mgarete->get_lien_maillage()->get_suivant(it);
349     }
350     while (mgsegment_depart2==NULL);
351     MCNode* noeud1_2;
352     MCNode* noeud2_2;
353     if (mgsegment_depart1->get_noeud1()==noeud_courant) noeud1_2=(MCNode*)mgsegment_depart1->get_noeud2(); else noeud1_2=(MCNode*)mgsegment_depart1->get_noeud1();
354     if (mgsegment_depart2->get_noeud1()==noeud_courant) noeud2_2=(MCNode*)mgsegment_depart2->get_noeud2(); else noeud2_2=(MCNode*)mgsegment_depart2->get_noeud1();
355     OT_VECTEUR_3D vecteur1(noeud1_2->get_x()-noeud_courant->get_x(),noeud1_2->get_y()-noeud_courant->get_y(),noeud1_2->get_z()-noeud_courant->get_z());
356     double coo[3];
357     mgarete->deriver(mgarete->get_tmin(),coo);
358     OT_VECTEUR_3D tangeante(coo[0]*mgcoarete->get_orientation(),coo[1]*mgcoarete->get_orientation(),coo[2]*mgcoarete->get_orientation());
359     MCNode* noeud_suivant;
360     if (vecteur1*tangeante>0.)
361     {
362     mgsegment=mgsegment_depart1;
363     noeud_suivant=noeud1_2;
364     }
365     else
366     {
367     mgsegment=mgsegment_depart2;
368     noeud_suivant=noeud2_2;
369     }
370     MG_FRONT_2D* front=ajouter_front(front_courant,noeud_courant,noeud_suivant,mgsegment);
371     grille_de_segment->inserer(mgsegment);
372     if (get_nb_front(front_courant)==nb_front_avant_cette_boucle+1) premier_front=front;
373     front->changer_front_precedent(front_precedent);
374     front_precedent=front;
375     noeud_courant=noeud_suivant;
376     segment_courant=mgsegment;
377     }
378     else
379     {
380     trouve=0;
381     TPL_SET<MG_ELEMENT_MAILLAGE*>::ITERATEUR it;
382     MG_SEGMENT* mgsegment=(MG_SEGMENT*)mgarete->get_lien_maillage()->get_premier(it);
383     do
384     {
385     if ((mgsegment->get_noeud1()==noeud_courant) || (mgsegment->get_noeud2()==noeud_courant))
386     if (mgsegment!=segment_courant)
387     {
388     trouve=1;
389     MCNode* noeud_suivant;
390     if (mgsegment->get_noeud1()==noeud_courant) noeud_suivant=(MCNode*)mgsegment->get_noeud2();
391     else if (mgsegment->get_noeud2()==noeud_courant) noeud_suivant=(MCNode*)mgsegment->get_noeud1();
392     MG_FRONT_2D* front=ajouter_front(front_courant,noeud_courant,noeud_suivant,(MCSegment*)mgsegment);
393     grille_de_segment->inserer((MCSegment*)mgsegment);
394     if (get_nb_front(front_courant)==nb_front_avant_cette_boucle+1) premier_front=front;
395     else front_precedent->changer_front_suivant(front);
396     front->changer_front_precedent(front_precedent);
397     front_precedent=front;
398     noeud_courant=noeud_suivant;
399     segment_courant=(MCSegment*)mgsegment;
400     }
401     mgsegment=(MG_SEGMENT*)mgarete->get_lien_maillage()->get_suivant(it);
402     }
403     while (trouve==0);
404     }
405     }
406     while (noeud_courant!=noeud_d_arrivee);
407    
408     int trouve=0;
409    
410     if (nbcoaretetraite != nbcoaretetotale)
411     mgcoarete = mgboucle->get_mg_coarete(nbcoaretetraite);
412     else
413     mgcoarete = NULL;
414     }
415     while ( mgcoarete );
416     front_precedent->changer_front_suivant(premier_front);
417     premier_front->changer_front_precedent(front_precedent);
418     }
419    
420     }
421    
422     void MAILLEUR2D::progresse_front(MCFace * __mcFace)
423     {
424     int compteur=0;
425     int nbpas_compteur=100;
426     int compteur_min=100;
427     int compteur_min_oiv1=100;
428     int compteur_min_oiv2=9000000;
429     _nbTriangles[__mcFace] = 0;
430     clock_t compteur_temps, compteur_temps2;
431     compteur_temps = clock();
432    
433     while (get_nb_front(front_courant)!=0)
434     {
435    
436     compteur++;
437    
438     compteur_temps2 = clock();
439     if (compteur_temps2-compteur_temps > CLOCKS_PER_SEC*.1)
440     {
441     std::ofstream file(filenameNbTriangles);
442    
443     file << " id = " << __mcFace->get_id()<< " ";
444     file << "nb triangles = "<<_nbTriangles[__mcFace]<< " ";
445     file << "nb fronts = "<< get_nb_front(front_courant)+get_nb_front(front_attente);
446     file.flush();
447     file.close();
448     compteur_temps = compteur_temps2;
449     }
450    
451     if (debug && _nbTriangles[__mcFace] >= compteur_min && (_nbTriangles[__mcFace]%nbpas_compteur==0))
452     {
453     if(0)
454     {
455     MG_FRONT_2D *f1=get_front(front_courant,0), *fc=f1;
456     std::set < MG_FRONT_2D * > visited;
457     unsigned cpt_front=0;
458     while (cpt_front < get_nb_front(front_courant))
459     {
460     visited.insert (fc);
461     unsigned id = fc->get_noeud1()->get_id();
462     if (id == 60101 || id==60099)
463     printf("* %d *, ",id);
464     else
465     printf("%d, ", id);
466     fc = fc->get_front_suivant();
467     if (visited.find(fc) != visited.end())
468     for (std::multimap<double,MG_FRONT_2D*>::iterator itFr=front_courant.begin();
469     itFr != front_courant.end();
470     itFr++)
471     if ( visited.find(itFr->second) == visited.end())
472     {
473     fc = itFr->second;
474     printf("--\n");
475     break;
476     }
477    
478     cpt_front++;
479     } ;
480     printf("\n\n");
481     }
482    
483     {
484     ofstream o4("c:\\temp\\void_2D.mai",ios::out|ios::trunc);
485     o4.precision(16);
486     o4.setf(ios::showpoint);
487     mg_maillage->enregistrer_sous_mesh_2D(o4);
488     }
489    
490     if ( _nbTriangles[__mcFace]>=compteur_min_oiv1)
491     {
492     char filename2[5000];
493     sprintf(filename2,"%s\\CAD4FE_Mesher_%d.iv",getenv("TEMP"),_nbTriangles[__mcFace]);
494     std::ofstream file(filename2);
495     CAD4FE::InventorText_MG_MAILLAGE ivTextRefTess (mg_maillage);
496     ivTextRefTess.ShowFaceSegments = true;
497     ivTextRefTess.ShowSegmentId = false;
498     ivTextRefTess.ShowTriangleId = false;
499     ivTextRefTess.ShowNodeId = true;
500     ivTextRefTess.InverseTriangleNormals = true;
501     ivTextRefTess.TriangleColor=1;
502     file << ivTextRefTess.GetText();
503     file << "\n}\n";
504     file.close();
505     }
506    
507     if (compteur>=compteur_min_oiv2 )
508     {
509     char filename2[5000];
510     strcpy(filename2,getenv("TEMP"));
511     strcat(filename2,"\\CAD4FE_Mesher_MCSegments.iv");
512     std::ofstream file(filename2);
513    
514     InventorText_MG_MAILLAGE meshIvText (mg_maillage);
515     meshIvText.ShowFaceSegments = true;
516     meshIvText.ShowSegmentId = true;
517     meshIvText.ShowTriangleId = true;
518     meshIvText.ShowNodeId = true;
519     meshIvText.InverseTriangleNormals = true;
520     meshIvText.TriangleColor=1;
521     file<<meshIvText.GetText();
522    
523     file << "DrawStyle {\nstyle LINES\nlineWidth 2\npointSize 3\n}\n";
524     LISTE_MG_SEGMENT::iterator itSegment;
525     for (MG_SEGMENT * seg = mg_maillage->get_premier_segment(itSegment); seg; seg = mg_maillage->get_suivant_segment(itSegment))
526     {
527     if (seg->get_lien_topologie()->get_dimension() == 1)
528     file << "BaseColor { \n rgb 0.0 0.0 0.9\n }\n";
529     if (seg->get_lien_topologie()->get_dimension() == 2)
530     file << "BaseColor { \n rgb 0.0 0.0 0.0\n }\n";
531     MCSegment * mcSeg = (MCSegment *) seg;
532     InventorText_MCSegment mcSegTxt (mcSeg);
533     file << mcSegTxt.GetText();
534     }
535    
536     file << "\n}\n";
537     file.close();
538     }
539     }
540    
541    
542     refresh();
543     MG_FRONT_2D* ft=get_front(front_courant,0);
544     MG_FRONT_2D* ftp=ft->get_front_precedent();
545     MG_FRONT_2D* fts=ft->get_front_suivant();
546     double eps=0.0001*ft->get_segment()->get_longueur();
547     MCNode* nop=ftp->get_noeud1();
548     MCNode* no1=ft->get_noeud1();
549     MCNode* no2=ft->get_noeud2();
550     MCNode* nos=fts->get_noeud2();
551     /* angle precedent et angle suivant */
552     OT_VECTEUR_3D n1n2(no1->get_coord(),no2->get_coord());
553     OT_VECTEUR_3D n2s(no2->get_coord(),nos->get_coord());
554     OT_VECTEUR_3D pn1(nop->get_coord(),no1->get_coord());
555     n1n2.norme();
556     n2s.norme();
557     pn1.norme();
558     double coo[3];
559     int nbRefFaces;
560     __mcFace->calcul_normale_unitaire(no1->GetRefFaceMapping(), coo, &nbRefFaces);
561     // MCFace_calcul_normale_unitaire(__mcFace,no1,coo);
562     OT_VECTEUR_3D nplan(coo);
563     OT_VECTEUR_3D n=nplan&n1n2;
564     n.norme();
565     double pcp=(-1)*(n1n2*pn1);
566     double psp=(-1)*(n*pn1);
567     int anglep;
568     if ((pcp>=0.1786481777)&&(psp>eps)) anglep=1; else anglep=0;
569     // MCFace_calcul_normale_unitaire(__mcFace,no2,coo);
570     __mcFace->calcul_normale_unitaire(no2->GetRefFaceMapping(), coo, &nbRefFaces);
571     OT_VECTEUR_3D nplan2(coo);
572     n=nplan2&n1n2;
573     n.norme();
574     double pcs=(-1.)*(n1n2*n2s);
575     double pss=n*n2s;
576     int angles;
577     if ((pcs>=0.1786481777)&&(pss>eps)) angles=1; else angles=0;
578     int type_cas_front[10];
579     if ( (ftp==fts->get_front_suivant()) && (no1!=nos) && (no2!=nop) ) type_cas_front[CAS_FRONT_3]=1; else type_cas_front[CAS_FRONT_3]=0;
580     if (identifie_front_cas_front_3x(ftp,ft,fts)) type_cas_front[CAS_FRONT_3X]=1; else type_cas_front[CAS_FRONT_3X]=0;
581     if ( ftp->get_front_precedent()==fts->get_front_suivant() && (nos!=no1) && (no2!=nop) ) type_cas_front[CAS_FRONT_4]=1; else type_cas_front[CAS_FRONT_4]=0;
582     if ( (anglep==1) && (angles==1) ) type_cas_front[CAS_FERME_CAVITE]=1; else type_cas_front[CAS_FERME_CAVITE]=0;
583     if ( anglep==1 ) type_cas_front[CAS_FERME_CAVITE_P]=1; else type_cas_front[CAS_FERME_CAVITE_P]=0;
584     if ( angles==1 ) type_cas_front[CAS_FERME_CAVITE_S]=1; else type_cas_front[CAS_FERME_CAVITE_S]=0;
585     type_cas_front[CAS_GENERAL]=1;
586     int solution=PASTROUVE;
587     if (type_cas_front[CAS_FRONT_3]) solution=traite_front(CAS_FRONT_3,__mcFace,ftp,ft,fts);
588     if ((solution==PASTROUVE) && (type_cas_front[CAS_FRONT_3X])) solution=traite_front(CAS_FRONT_3X,__mcFace,ftp,ft,fts);
589     if ((solution==PASTROUVE) && (type_cas_front[CAS_FRONT_4])) solution=traite_front(CAS_FRONT_4,__mcFace,ftp,ft,fts);
590     if ((solution==PASTROUVE) && (type_cas_front[CAS_FERME_CAVITE])) solution=traite_front(CAS_FERME_CAVITE,__mcFace,ftp,ft,fts);
591     if ((solution==PASTROUVE) && (type_cas_front[CAS_FERME_CAVITE_P])) solution=traite_front(CAS_FERME_CAVITE_P,__mcFace,ftp,ft,fts);
592     if ((solution==PASTROUVE) && (type_cas_front[CAS_FERME_CAVITE_S])) solution=traite_front(CAS_FERME_CAVITE_S,__mcFace,ftp,ft,fts);
593     if ((solution==PASTROUVE) && (type_cas_front[CAS_GENERAL]))
594     {
595     solution=traite_front(CAS_GENERAL,__mcFace,ftp,ft,fts);
596     if ((solution!=PASTROUVE)&&(solution!=TROUVE))
597     if (!type_cas_front[solution]) solution=traite_front(solution,__mcFace,ftp,ft,fts); else solution=PASTROUVE;
598     if (solution==PASTROUVE)
599     {
600     echange_de_front(front_courant,front_attente,ft);
601     ft->incremente_ifail();
602     solution=TROUVE;
603     }
604     }
605     if (get_nb_front(front_courant)==0) front_courant.swap(front_attente);
606     }
607    
608     {
609     std::ofstream file(filenameNbTriangles);
610     file << " id = " << __mcFace->get_id()<< " ";
611     file << "nb triangles = "<<_nbTriangles[__mcFace]<< " ";
612     file << "nb fronts = "<< get_nb_front(front_courant)+get_nb_front(front_attente);
613     file.flush();
614     file.close();
615     }
616    
617     }
618    
619     void MAILLEUR2D::optimise_bascule_segment(MCFace* __mcFace)
620     {
621     OptimizeEdgeSwap edgeSwapper(mg_maillage);
622     edgeSwapper.Optimize(__mcFace);
623     initialise_lst_tri_qual(__mcFace);
624     }
625    
626     void MAILLEUR2D::initialise_lst_tri_qual(MCFace* __mcFace)
627     {
628     lst_tri_qual.clear();
629     lst_tri_qual2.clear();
630     LISTE_MG_TRIANGLE::iterator itTri;
631     for (M3D_MCTriangle * mtri = (M3D_MCTriangle *)mg_maillage->get_premier_triangle(itTri);mtri; mtri = (M3D_MCTriangle *)mg_maillage->get_suivant_triangle(itTri))
632     {
633     double qual=OPERATEUR::qualite_triangle(mtri->get_noeud1()->get_coord(),mtri->get_noeud2()->get_coord(),mtri->get_noeud3()->get_coord());
634     mtri->change_qualite(qual);
635     lst_tri_qual.insert(std::make_pair(mtri->get_qualite(),mtri));
636     }
637     }
638    
639     void MAILLEUR2D::optimise(MCFace* __mcFace)
640     {
641     initialise_lst_tri_qual(__mcFace);
642     std::set<MCSegment *> lstSegToUpdate;
643     for (int phase=0;phase<2;phase++)
644     {
645     ORDRE_TRIANGLE& lst=lst_tri_qual;
646     if (phase==1)
647     lst=lst_tri_qual2;
648     int ok=0;
649     do
650     {
651     ORDRE_TRIANGLE::iterator i=lst.begin();
652     if (i==lst.end())
653     {
654     ok=1;
655     continue;
656     }
657     M3D_MCTriangle* tri=(*i).second;
658     if (tri->get_qualite()<0.5)
659     {
660     MCNode* no[3];
661     no[0]=(MCNode*)tri->get_noeud1();
662     no[1]=(MCNode*)tri->get_noeud2();
663     no[2]=(MCNode*)tri->get_noeud3();
664     double crit[3];//,u[3],v[3],x[3],y[3],z[3];
665     MCNode *noeud_optimal[3]={0,0,0};
666     int ierr=bouge_point(__mcFace,no[0],crit[0],&(noeud_optimal[0]));
667     if (ierr==0) crit[0]=0.;
668     ierr=bouge_point(__mcFace,no[1],crit[1],&(noeud_optimal[1]));
669     if (ierr==0) crit[1]=0.;
670     ierr=bouge_point(__mcFace,no[2],crit[2],&(noeud_optimal[2]));
671     if (ierr==0) crit[2]=0.;
672     double critopt=std::max(crit[0],crit[1]);
673     critopt=std::max(critopt,crit[2]);
674     int num=-1;
675     if (critopt>tri->get_qualite())
676     {
677     if (critopt==crit[0]) num=0;
678     if (critopt==crit[1]) num=1;
679     if (critopt==crit[2]) num=2;
680     }
681     if (num!=-1)
682     {
683     no[num]->CopyGeometry(*noeud_optimal[num]);
684     int nb_seg=no[num]->get_lien_segment()->get_nb();
685     // the geometry of adjacent segment (path in the reference BRep)
686     // must be updated according to the new position of the node
687     for (int i=0;i<nb_seg;i++)
688     lstSegToUpdate.insert((MCSegment*)no[num]->get_lien_segment()->get(i));
689     int nb_tri=no[num]->get_lien_triangle()->get_nb();
690     for (int i=0;i<nb_tri;i++)
691     {
692     M3D_MCTriangle* mtri=(M3D_MCTriangle*)no[num]->get_lien_triangle()->get(i);
693     double qual=OPERATEUR::qualite_triangle(mtri->get_noeud1()->get_coord(),mtri->get_noeud2()->get_coord(),mtri->get_noeud3()->get_coord());
694     mtri->change_qualite(qual);
695     }
696     }
697     if (noeud_optimal[0]) delete noeud_optimal[0];
698     if (noeud_optimal[1]) delete noeud_optimal[1];
699     if (noeud_optimal[2]) delete noeud_optimal[2];
700     }
701     else ok=1;
702     if ((tri->get_qualite()<0.5) && (phase==0))
703     {
704     std::pair<const double,M3D_MCTriangle*> tmp(1./tri->get_qualite(),tri);
705     lst_tri_qual2.insert(tmp);
706     }
707     lst.erase(i);
708     }
709     while (ok==0);
710     }
711    
712     // update the geometry of segments which nodes have been moved
713     for (std::set<MCSegment*>::iterator itSeg=lstSegToUpdate.begin();
714     itSeg!=lstSegToUpdate.end();
715     itSeg++)
716     {
717     MCSegment * seg = * itSeg;
718     seg->UpdateGeometry();
719     }
720     }
721    
722    
723     /*void MAILLEUR2D::optimise(MG_FACE* mgface)
724     {
725     for (int phase=0;phase<2;phase++)
726     {
727     ORDRE_TRIANGLE& lst=lst_tri_qual;
728     if (phase==1)
729     lst=lst_tri_qual2;
730     int ok=0;
731     do
732     {
733     ORDRE_TRIANGLE::iterator i=lst.begin();
734     if (i==lst.end())
735     {
736     ok=1;
737     continue;
738     }
739     M3D_MCTriangle* tri=(*i).second;
740     if (tri->get_qualite()<0.5)
741     {
742     MG_NOEUD* no[3];
743     no[0]=tri->get_noeud1();
744     no[1]=tri->get_noeud2();
745     no[2]=tri->get_noeud3();
746     double crit[3],u[3],v[3],x[3],y[3],z[3];
747     int ierr=bouge_point(mgface,no[0],crit[0],u[0],v[0],x[0],y[0],z[0]);
748     if (ierr==0) crit[0]=0.;
749     ierr=bouge_point(mgface,no[1],crit[1],u[1],v[1],x[1],y[1],z[1]);
750     if (ierr==0) crit[1]=0.;
751     ierr=bouge_point(mgface,no[2],crit[2],u[2],v[2],x[2],y[2],z[2]);
752     if (ierr==0) crit[2]=0.;
753     double critopt=std::max(crit[0],crit[1]);
754     critopt=std::max(critopt,crit[2]);
755     int num=-1;
756     if (critopt>tri->get_qualite())
757     {
758     if (critopt==crit[0]) num=0;
759     if (critopt==crit[1]) num=1;
760     if (critopt==crit[2]) num=2;
761     }
762     if (num!=-1)
763     {
764     no[num]->change_u(u[num]);
765     no[num]->change_v(v[num]);
766     no[num]->change_x(x[num]);
767     no[num]->change_y(y[num]);
768     no[num]->change_z(z[num]);
769     int nb_tri=no[num]->get_lien_triangle()->get_nb();
770     for (int i=0;i<nb_tri;i++)
771     {
772     M3D_MCTriangle* mtri=(M3D_MCTriangle*)no[num]->get_lien_triangle()->get(i);
773     double qual=OPERATEUR::qualite_triangle(mtri->get_noeud1()->get_coord(),mtri->get_noeud2()->get_coord(),mtri->get_noeud3()->get_coord());
774     mtri->change_qualite(qual);
775     }
776     }
777     }
778     else ok=1;
779     if ((tri->get_qualite()<0.5) && (phase==0))
780     {
781     std::pair<const double,M3D_MCTriangle*> tmp(1./tri->get_qualite(),tri);
782     lst_tri_qual2.insert(tmp);
783     }
784     lst.erase(i);
785     }
786     while (ok==0);
787     }
788     } */
789    
790    
791    
792    
793     // FONCTIONS GERANT L ENTITE FRONT (ajout suppression et tout le bordel)
794     MG_FRONT_2D* MAILLEUR2D::ajouter_front(FRONT& front,MCNode* noeud1,MCNode* noeud2,MCSegment* segment)
795     {
796     MG_FRONT_2D* ft=new MG_FRONT_2D(noeud1,noeud2,segment);
797     ajouter_front(front,ft);
798     grille_de_front->inserer(ft);
799     return ft;
800     }
801    
802     void MAILLEUR2D::ajouter_front(FRONT& front,MG_FRONT_2D *ft)
803     {
804     std::pair<const double,MG_FRONT_2D*> tmp(ft->get_segment()->get_longueur(),ft);
805     front.insert(tmp);
806     }
807    
808     MG_FRONT_2D* MAILLEUR2D::get_front(FRONT& front,unsigned int num)
809     {
810     FRONT::iterator i=front.begin();
811     for (unsigned long j=0;j<num;j++) i++;
812     return ((*i).second);
813     }
814    
815    
816     unsigned int MAILLEUR2D::get_nb_front(FRONT& front)
817     {
818     return front.size();
819     }
820    
821    
822     MG_FRONT_2D * MAILLEUR2D::trouver_front(MG_NOEUD * no1, MG_NOEUD * no2)
823     {
824     FRONT::iterator j;
825     MG_FRONT_2D *ft = 0;
826     for (j=front_courant.begin(); j!=front_courant.end(); j++)
827     {
828     MG_FRONT_2D * ftj = j->second;
829     if ( ftj->get_noeud1() == no1 && ftj->get_noeud2() == no2 )
830     {
831     ft = ftj;
832     break;
833     }
834     }
835     if (ft == 0)
836     for (j=front_attente.begin(); j!=front_attente.end(); j++)
837     {
838     MG_FRONT_2D * ftj = j->second;
839     if ( ftj->get_noeud1() == no1 && ftj->get_noeud2() == no2 )
840     {
841     ft = ftj;
842     break;
843     }
844     }
845     return ft;
846     }
847    
848     void MAILLEUR2D::supprimer_front(MG_FRONT_2D* ft)
849     {
850     grille_de_front->supprimer(ft);
851    
852     FRONT::iterator j=front_courant.lower_bound(ft->get_segment()->get_longueur());
853     int ok=0;
854     if (j==front_courant.end()) ok=2;
855     while (ok==0&&(j!=front_courant.end()))
856     {
857     MG_FRONT_2D* fttmp=(*j).second;
858     if (ft==fttmp) {ok=1;front_courant.erase(j);}
859     if (fttmp->get_segment()->get_longueur()>ft->get_segment()->get_longueur()) ok=2;
860     j++;
861     }
862     if (ok!=1)
863     {
864     j=front_attente.lower_bound(ft->get_segment()->get_longueur());
865     while (ft!=(*j).second) j++;
866     front_attente.erase(j);
867     }
868    
869     delete ft;
870     }
871    
872     void MAILLEUR2D::echange_de_front(FRONT& front_original,FRONT& front_destination,MG_FRONT_2D* ft)
873     {
874     FRONT::iterator j=front_original.lower_bound(ft->get_segment()->get_longueur());
875     while (ft!=(*j).second) j++;
876     front_original.erase(j);
877     ajouter_front(front_destination,ft);
878     }