Salome HOME
Replace oe by ?
[modules/smesh.git] / src / Tools / MeshCut / MeshCut_DC.cxx
1 // Classes et fonctions XMeshLab
2
3 #include "MeshCut_Utils.hxx"
4 #include "MeshCut_Maillage.hxx"
5
6 #include "MeshCut_Carre.hxx"
7 #include "MeshCut_Cube.hxx"
8
9 #include "MeshCut_Fonctions.hxx"
10 #include "MeshCut_Cas.hxx"
11
12 #include "MeshCut_Globals.hxx"
13
14 #include <iostream>
15 #include <cmath>
16 #include <cstdlib>
17 #include <cstring>
18
19 using namespace MESHCUT;
20 using namespace std;
21
22 // ==================================  DECLARATION DES VARIABLES GLOBALES  ==================================================
23
24 std::map<std::string, int> MESHCUT::intersections;
25
26 int MESHCUT::indexNouvellesMailles, MESHCUT::indexNouveauxNoeuds, MESHCUT::offsetMailles;
27 std::string MESHCUT::str_id_GMplus, MESHCUT::str_id_GMmoins;
28 Maillage *MESHCUT::MAILLAGE1, *MESHCUT::MAILLAGE2;
29
30 std::vector<float> MESHCUT::newXX, MESHCUT::newYY, MESHCUT::newZZ;
31 std::map<TYPE_MAILLE, std::vector<int> > MESHCUT::newCNX;
32 std::map<TYPE_MAILLE, int> MESHCUT::cptNouvellesMailles;
33 std::map<TYPE_MAILLE, std::vector<int> > MESHCUT::GMplus, MESHCUT::GMmoins;
34
35 float *MESHCUT::DNP;
36 int *MESHCUT::POSN;
37
38 std::string MESHCUT::str_id_maillagenew;
39
40 float MESHCUT::normale[3], MESHCUT::pointPlan[3];
41 float MESHCUT::d;
42 float MESHCUT::epsilon;
43
44 bool MESHCUT::debug;
45 int MESHCUT::Naretes;
46
47 // ==================================   PROGRAMME PRINCIPAL  ==================================================
48
49 int main(int argc, char *argv[])
50 {
51
52   debug = false;
53   string ficMEDin;
54   string ficMEDout;
55   float xNormal;
56   float yNormal;
57   float zNormal;
58   float xm;
59   float ym;
60   float zm;
61   float tolerance;
62   try
63     {
64       if (argc != 13)
65         throw std::exception();
66       char *ficMEDin0 = argv[1];
67       ficMEDin = (string) ficMEDin0;
68       char *ficMEDout0 = argv[2];
69       ficMEDout = (string) ficMEDout0;
70       char *id_maillagenew = argv[3];
71       str_id_maillagenew = (string) id_maillagenew;
72
73       // Groupes créés
74       char *id_GMplus = argv[4];
75       str_id_GMplus = (string) id_GMplus;
76       char *id_GMmoins = argv[5];
77       str_id_GMmoins = (string) id_GMmoins;
78
79       // Vecteur normal au plan de coupe
80       char *charxn = argv[6];
81       xNormal = char2float(charxn);
82       char *charyn = argv[7];
83       yNormal = char2float(charyn);
84       char *charzn = argv[8];
85       zNormal = char2float(charzn);
86
87       // Point du plan de coupe
88       char *charxm = argv[9];
89       xm = char2float(charxm);
90       char *charym = argv[10];
91       ym = char2float(charym);
92       char *charzm = argv[11];
93       zm = char2float(charzm);
94
95       // Tolérance :  epsilon = tolérance * longueur arête moyenne - où epsilon est la tolérance absolue (distance)
96       char *chtolerance = argv[12];
97       tolerance = char2float(chtolerance);
98     }
99   catch (...)
100     {
101       cout << endl;
102       cout << "                 Cut a tetrahedron mesh by a plane" << endl;
103       cout << "                 ---------------------------------" << endl;
104       cout << "Syntax:" << endl << endl;
105       cout << argv[0] << " input.med output.med resuMeshName aboveGroup belowGroup nx ny nz px py pz T " << endl;
106       cout << endl << "where:" << endl;
107       cout << "  input.med    = name of the original mesh file in med format" << endl;
108       cout << "  output.med   = name of the result mesh file in med format" << endl;
109       cout << "  resuMeshName = name of the result mesh" << endl;
110       cout << "  aboveGroup   = name of the group of volumes above the cut plane" << endl;
111       cout << "  belowGroups  = name of the group of volumes below the cut plane" << endl;
112       cout << "  nx ny nz     = vector normal to the cut plane" << endl;
113       cout << "  px py pz     = a point of the cut plane" << endl;
114       cout << "  T            = 0 < T < 1 : vertices of a tetrahedron are considered as belonging" << endl;
115       cout << "                 the cut plane if their distance to the plane is inferior to L*T" << endl;
116       cout << "                 where L is the mean edge size of the tetrahedron" << endl;
117       ERREUR("--> check arguments!");
118     }
119
120   cout << "Découpe plane :" << endl;
121   cout << "\t" << "Maillage source: " << ficMEDin << endl;
122   cout << "\t" << "Maillage cible:  " << ficMEDout << endl;
123   cout << "\t" << "ID nouveau maillage:  " << str_id_maillagenew << endl;
124   cout << "\t" << "Nom GMplus:  " << str_id_GMplus << endl;
125   cout << "\t" << "Nom GMmoins: " << str_id_GMmoins << endl;
126   cout << "\t" << "Vecteur normal du plan de coupe: xn=" << xNormal << " yn=" << yNormal << " zn=" << zNormal << endl;
127   cout << "\t" << "Point du plan de coupe: xm=" << xm << " ym=" << ym << " zm=" << zm << endl;
128   cout << "\t" << "Tolérance: " << tolerance << endl;
129   cout << endl;
130
131   if (tolerance <= 0.0)
132     ERREUR("L'argument tolérance doit être strictement positif");
133
134   // Il faut normer la normale
135   float normeNormal = sqrt(xNormal * xNormal + yNormal * yNormal + zNormal * zNormal);
136   if (normeNormal == 0.0)
137     ERREUR("Vecteur normal nul");
138   normale[0] = xNormal / normeNormal;
139   normale[1] = yNormal / normeNormal;
140   normale[2] = zNormal / normeNormal;
141
142   pointPlan[0] = xm;
143   pointPlan[1] = ym;
144   pointPlan[2] = zm;
145
146   // Calcul du coefficient d de l'équation du plan  xn x + yn y + zn n + d = 0
147   d = -normale[0] * xm - normale[1] * ym - normale[2] * zm;
148
149   intersections.clear();
150
151   // Initialisation des compteurs de nouvelles mailles
152   for (int itm = (int) POI1; itm <= (int) HEXA20; itm++)
153     {
154       TYPE_MAILLE tm = (TYPE_MAILLE) itm;
155       cptNouvellesMailles[tm] = 0;
156     }
157
158   int V[6];
159   int S[4]; // Signature du T4 courant
160   int NG[4]; // Num. globaux des sommets
161
162   // Acquisition maillage initial
163   //cout << chrono() << " - Acquisition du maillage initial" << endl;
164   MAILLAGE1 = new Maillage((string) "TEMP");
165   MAILLAGE1->inputMED(ficMEDin);
166   cout << chrono() << " - Fin d'acquisition maillage" << endl;
167   indexNouveauxNoeuds = MAILLAGE1->nombreNoeudsMaillage;
168
169   // Le maillage ne contient aucun TETRA4 : on rend le maillage initial sans modification
170   if (!MAILLAGE1->EFFECTIFS_TYPES[TETRA4])
171     {
172       cout << "WARNING : le maillage ne contient aucun élément TETRA4, il ne sera donc pas modifié" << endl;
173       MAILLAGE1->ID = str_id_maillagenew;
174       MAILLAGE1->outputMED(ficMEDout);
175       cout << chrono() << " - Terminé" << endl << endl;
176       exit(0);
177     }
178   // A partir de cet instant le maillage contient forcément des TETRA4
179
180
181   // Chargement des distances noeud-plan DNP
182   DNP = (float*) malloc(sizeof(float) * MAILLAGE1->nombreNoeudsMaillage);
183   for (int k = 0; k < MAILLAGE1->nombreNoeudsMaillage; k++)
184     DNP[k] = distanceNoeudPlan(k + 1);
185   cout << chrono() << " - Fin de chargement des distances noeud-plan" << endl;
186
187   // Longueur d'arête moyenne des T4 intersectant le plan de coupe
188   float LONGUEURS = 0.0;
189   int cptLONGUEURS = 0;
190   for (int it4 = 0; it4 < MAILLAGE1->EFFECTIFS_TYPES[TETRA4]; it4++)
191     {
192       bool plus = false;
193       bool moins = false;
194       int *offset = MAILLAGE1->CNX[TETRA4] + 4 * it4;
195       for (int is = 0; is < 4; is++)
196         {
197           int ng = *(offset + is);
198           if (DNP[ng - 1] > 0.0)
199             plus = true;
200           else if (DNP[ng - 1] < 0.0)
201             moins = true;
202         }
203       if (plus && moins)
204         {
205           // Ce tetra est à cheval sur le plan de coupe: on calcule ses longueurs d'arêtes
206           LONGUEURS += longueurSegment(*(offset + 0), *(offset + 1));
207           cptLONGUEURS++;
208           LONGUEURS += longueurSegment(*(offset + 0), *(offset + 2));
209           cptLONGUEURS++;
210           LONGUEURS += longueurSegment(*(offset + 0), *(offset + 3));
211           cptLONGUEURS++;
212           LONGUEURS += longueurSegment(*(offset + 1), *(offset + 2));
213           cptLONGUEURS++;
214           LONGUEURS += longueurSegment(*(offset + 1), *(offset + 3));
215           cptLONGUEURS++;
216           LONGUEURS += longueurSegment(*(offset + 2), *(offset + 3));
217           cptLONGUEURS++;
218         }
219     }
220
221   // Aucun TETRA4 intercepté par le plan de coupe : on rend MAILLAGE1
222   if (cptLONGUEURS == 0)
223     {
224       cout
225           << "WARNING : le plan de coupe n'intersecte aucun élément TETRA4, le maillage initial ne sera donc pas modifié"
226           << endl;
227       MAILLAGE1->ID = str_id_maillagenew;
228       MAILLAGE1->outputMED(ficMEDout);
229       cout << chrono() << " - Terminé" << endl << endl;
230       exit(0);
231     }
232   // A partir de cet instant le maillage contient forcément des TETRA4 intersectant le plan de coupe
233
234
235   float longueurMoyenne = LONGUEURS / cptLONGUEURS;
236   epsilon = tolerance * longueurMoyenne;
237
238   int nT4coupe = cptLONGUEURS / 6;
239   cout << chrono() << " - Fin du calcul de la longueur moyenne des arêtes TETRA4 au voisinage du plan de coupe" << endl;
240
241   cout << "Nombre de TETRA4 impliqués dans la coupe = " << nT4coupe << endl;
242   cout << "Longueur moyenne = " << longueurMoyenne << endl;
243   cout << "Tolérance = " << tolerance << endl;
244   cout << "Epsilon = " << epsilon << endl;
245
246   // Détermination des positions de noeuds par rapport au plan de coupe - POSN
247   POSN = (int*) malloc(sizeof(int) * MAILLAGE1->nombreNoeudsMaillage);
248   for (int k = 0; k < MAILLAGE1->nombreNoeudsMaillage; k++)
249     {
250       if (DNP[k] > epsilon)
251         POSN[k] = 1;
252       else if (DNP[k] < -epsilon)
253         POSN[k] = -1;
254       else
255         POSN[k] = 0;
256     }
257   cout << chrono() << " - Fin du positionnement des points par rapport au plan de coupe" << endl;
258   cout << "Début de la boucle sur les TETRA4" << endl;
259
260   for (int it4 = 0; it4 < MAILLAGE1->EFFECTIFS_TYPES[TETRA4]; it4++)
261     {
262
263       for (int is = 0; is < 4; is++)
264         {
265           int ng = *(MAILLAGE1->CNX[TETRA4] + 4 * it4 + is);
266           NG[is] = ng;
267           S[is] = *(POSN + ng - 1);
268         }
269
270       // -------------------------------------------------------------------
271
272       if (S[0] == -1 && S[1] == -1 && S[2] == -1 && S[3] == -1)
273         GMmoins[TETRA4].push_back(it4);
274
275       else if (S[0] == -1 && S[1] == -1 && S[2] == -1 && S[3] == 0)
276         GMmoins[TETRA4].push_back(it4);
277
278       else if (S[0] == -1 && S[1] == -1 && S[2] == -1 && S[3] == 1)
279         { // Cas 3 - Arêtes 2 4 5
280           V[0] = -1;
281           V[1] = -1;
282           V[2] = intersectionSegmentPlan(it4, 2);
283           V[3] = -1;
284           V[4] = intersectionSegmentPlan(it4, 4);
285           V[5] = intersectionSegmentPlan(it4, 5);
286           cas3(V, it4);
287         }
288
289       // -------------------------------------------------------------------
290
291       else if (S[0] == -1 && S[1] == -1 && S[2] == 0 && S[3] == -1)
292         GMmoins[TETRA4].push_back(it4);
293
294       else if (S[0] == -1 && S[1] == -1 && S[2] == 0 && S[3] == 0)
295         GMmoins[TETRA4].push_back(it4);
296
297       else if (S[0] == -1 && S[1] == -1 && S[2] == 0 && S[3] == 1)
298         { // Cas 2, arêtes 2 4
299           V[0] = -1;
300           V[1] = -1;
301           V[2] = intersectionSegmentPlan(it4, 2);
302           V[3] = -1;
303           V[4] = intersectionSegmentPlan(it4, 4);
304           V[5] = -1;
305           cas2(V, it4);
306         }
307
308       // -------------------------------------------------------------------
309
310       else if (S[0] == -1 && S[1] == -1 && S[2] == 1 && S[3] == -1)
311         { // Cas 3, arêtes 1 3 5
312           V[0] = -1;
313           V[1] = intersectionSegmentPlan(it4, 1);
314           V[2] = -1;
315           V[3] = intersectionSegmentPlan(it4, 3);
316           V[4] = -1;
317           V[5] = intersectionSegmentPlan(it4, 5);
318           cas3(V, it4);
319         }
320
321       else if (S[0] == -1 && S[1] == -1 && S[2] == 1 && S[3] == 0)
322         { // Cas 2, arêtes 1 3
323           V[0] = -1;
324           V[1] = intersectionSegmentPlan(it4, 1);
325           V[2] = -1;
326           V[3] = intersectionSegmentPlan(it4, 3);
327           V[4] = -1;
328           V[5] = -1;
329           cas2(V, it4);
330         }
331
332       else if (S[0] == -1 && S[1] == -1 && S[2] == 1 && S[3] == 1)
333         { // Cas 4, arêtes 1 2 3 4
334           V[0] = -1;
335           V[1] = intersectionSegmentPlan(it4, 1);
336           V[2] = intersectionSegmentPlan(it4, 2);
337           V[3] = intersectionSegmentPlan(it4, 3);
338           V[4] = intersectionSegmentPlan(it4, 4);
339           V[5] = -1;
340           cas4(V, it4);
341         }
342
343       // -------------------------------------------------------------------
344
345       else if (S[0] == -1 && S[1] == 0 && S[2] == -1 && S[3] == -1)
346         GMmoins[TETRA4].push_back(it4);
347
348       else if (S[0] == -1 && S[1] == 0 && S[2] == -1 && S[3] == 0)
349         GMmoins[TETRA4].push_back(it4);
350
351       else if (S[0] == -1 && S[1] == 0 && S[2] == -1 && S[3] == 1)
352         { // Cas 2, arêtes 2 5
353           V[0] = -1;
354           V[1] = -1;
355           V[2] = intersectionSegmentPlan(it4, 2);
356           V[3] = -1;
357           V[4] = -1;
358           V[5] = intersectionSegmentPlan(it4, 5);
359           cas2(V, it4);
360         }
361
362       // -------------------------------------------------------------------
363
364       else if (S[0] == -1 && S[1] == 0 && S[2] == 0 && S[3] == -1)
365         GMmoins[TETRA4].push_back(it4);
366
367       else if (S[0] == -1 && S[1] == 0 && S[2] == 0 && S[3] == 0)
368         GMmoins[TETRA4].push_back(it4);
369
370       else if (S[0] == -1 && S[1] == 0 && S[2] == 0 && S[3] == 1)
371         { // Cas 1, arête 2
372           V[0] = -1;
373           V[1] = -1;
374           V[2] = intersectionSegmentPlan(it4, 2);
375           V[3] = -1;
376           V[4] = -1;
377           V[5] = -1;
378           cas1(V, it4);
379         }
380
381       // -------------------------------------------------------------------
382
383       else if (S[0] == -1 && S[1] == 0 && S[2] == 1 && S[3] == -1)
384         { // Cas 2, arêtes 1 5
385           V[0] = -1;
386           V[1] = intersectionSegmentPlan(it4, 1);
387           V[2] = -1;
388           V[3] = -1;
389           V[4] = -1;
390           V[5] = intersectionSegmentPlan(it4, 5);
391           cas2(V, it4);
392         }
393
394       else if (S[0] == -1 && S[1] == 0 && S[2] == 1 && S[3] == 0)
395         { // Cas 1, arête 1
396           V[0] = -1;
397           V[1] = intersectionSegmentPlan(it4, 1);
398           V[2] = -1;
399           V[3] = -1;
400           V[4] = -1;
401           V[5] = -1;
402           cas1(V, it4);
403         }
404
405       else if (S[0] == -1 && S[1] == 0 && S[2] == 1 && S[3] == 1)
406         { // Cas 2, arêtes 1 2
407           V[0] = -1;
408           V[1] = intersectionSegmentPlan(it4, 1);
409           V[2] = intersectionSegmentPlan(it4, 2);
410           V[3] = -1;
411           V[4] = -1;
412           V[5] = -1;
413           cas2(V, it4);
414         }
415
416       // -------------------------------------------------------------------
417
418       else if (S[0] == -1 && S[1] == 1 && S[2] == -1 && S[3] == -1)
419         { // Cas 3, arêtes 0 3 4
420           V[0] = intersectionSegmentPlan(it4, 0);
421           V[1] = -1;
422           V[2] = -1;
423           V[3] = intersectionSegmentPlan(it4, 3);
424           V[4] = intersectionSegmentPlan(it4, 4);
425           V[5] = -1;
426           cas3(V, it4);
427         }
428
429       else if (S[0] == -1 && S[1] == 1 && S[2] == -1 && S[3] == 0)
430         { // Cas 2, arêtes 0 3
431           V[0] = intersectionSegmentPlan(it4, 0);
432           V[1] = -1;
433           V[2] = -1;
434           V[3] = intersectionSegmentPlan(it4, 3);
435           V[4] = -1;
436           V[5] = -1;
437           cas2(V, it4);
438         }
439
440       else if (S[0] == -1 && S[1] == 1 && S[2] == -1 && S[3] == 1)
441         { // Cas 4, arêtes 0 2 3 5
442           V[0] = intersectionSegmentPlan(it4, 0);
443           V[1] = -1;
444           V[2] = intersectionSegmentPlan(it4, 2);
445           V[3] = intersectionSegmentPlan(it4, 3);
446           V[4] = -1;
447           V[5] = intersectionSegmentPlan(it4, 5);
448           cas4(V, it4);
449         }
450
451       // -------------------------------------------------------------------
452
453       else if (S[0] == -1 && S[1] == 1 && S[2] == 0 && S[3] == -1)
454         { // Cas 2, arêtes 0 4
455           V[0] = intersectionSegmentPlan(it4, 0);
456           V[1] = -1;
457           V[2] = -1;
458           V[3] = -1;
459           V[4] = intersectionSegmentPlan(it4, 4);
460           V[5] = -1;
461           cas2(V, it4);
462         }
463
464       else if (S[0] == -1 && S[1] == 1 && S[2] == 0 && S[3] == 0)
465         { // Cas 1, arête 0
466           V[0] = intersectionSegmentPlan(it4, 0);
467           V[1] = -1;
468           V[2] = -1;
469           V[3] = -1;
470           V[4] = -1;
471           V[5] = -1;
472           cas1(V, it4);
473         }
474
475       else if (S[0] == -1 && S[1] == 1 && S[2] == 0 && S[3] == 1)
476         { // Cas 2, arêtes 0 2
477           V[0] = intersectionSegmentPlan(it4, 0);
478           V[1] = -1;
479           V[2] = intersectionSegmentPlan(it4, 2);
480           V[3] = -1;
481           V[4] = -1;
482           V[5] = -1;
483           cas2(V, it4);
484         }
485
486       // -------------------------------------------------------------------
487
488       else if (S[0] == -1 && S[1] == 1 && S[2] == 1 && S[3] == -1)
489         { // Cas 4, arêtes 0 1 4 5
490           V[0] = intersectionSegmentPlan(it4, 0);
491           V[1] = intersectionSegmentPlan(it4, 1);
492           V[2] = -1;
493           V[3] = -1;
494           V[4] = intersectionSegmentPlan(it4, 4);
495           V[5] = intersectionSegmentPlan(it4, 5);
496           cas4(V, it4);
497         }
498
499       else if (S[0] == -1 && S[1] == 1 && S[2] == 1 && S[3] == 0)
500         { // Cas 2, arêtes 0 1
501           V[0] = intersectionSegmentPlan(it4, 0);
502           V[1] = intersectionSegmentPlan(it4, 1);
503           V[2] = -1;
504           V[3] = -1;
505           V[4] = -1;
506           V[5] = -1;
507           cas2(V, it4);
508         }
509
510       else if (S[0] == -1 && S[1] == 1 && S[2] == 1 && S[3] == 1)
511         { // Cas 3, arêtes 0 1 2
512           V[0] = intersectionSegmentPlan(it4, 0);
513           V[1] = intersectionSegmentPlan(it4, 1);
514           V[2] = intersectionSegmentPlan(it4, 2);
515           V[3] = -1;
516           V[4] = -1;
517           V[5] = -1;
518           cas3(V, it4);
519         }
520
521       // -------------------------------------------------------------------
522
523       else if (S[0] == 0 && S[1] == -1 && S[2] == -1 && S[3] == -1)
524         GMmoins[TETRA4].push_back(it4);
525
526       else if (S[0] == 0 && S[1] == -1 && S[2] == -1 && S[3] == 0)
527         GMmoins[TETRA4].push_back(it4);
528
529       else if (S[0] == 0 && S[1] == -1 && S[2] == -1 && S[3] == 1)
530         { // Cas 2, arêtes 4 5
531           V[0] = -1;
532           V[1] = -1;
533           V[2] = -1;
534           V[3] = -1;
535           V[4] = intersectionSegmentPlan(it4, 4);
536           V[5] = intersectionSegmentPlan(it4, 5);
537           cas2(V, it4);
538         }
539
540       // -------------------------------------------------------------------
541
542       else if (S[0] == 0 && S[1] == -1 && S[2] == 0 && S[3] == -1)
543         GMmoins[TETRA4].push_back(it4);
544
545       else if (S[0] == 0 && S[1] == -1 && S[2] == 0 && S[3] == 0)
546         GMmoins[TETRA4].push_back(it4);
547
548       else if (S[0] == 0 && S[1] == -1 && S[2] == 0 && S[3] == 1)
549         { // Cas 1, arête 4
550           V[0] = -1;
551           V[1] = -1;
552           V[2] = -1;
553           V[3] = -1;
554           V[4] = intersectionSegmentPlan(it4, 4);
555           V[5] = -1;
556           cas1(V, it4);
557         }
558
559       // -------------------------------------------------------------------
560
561       else if (S[0] == 0 && S[1] == -1 && S[2] == 1 && S[3] == -1)
562         { // Cas 2, arêtes 3 5
563           V[0] = -1;
564           V[1] = -1;
565           V[2] = -1;
566           V[3] = intersectionSegmentPlan(it4, 3);
567           V[4] = -1;
568           V[5] = intersectionSegmentPlan(it4, 5);
569           cas2(V, it4);
570         }
571
572       else if (S[0] == 0 && S[1] == -1 && S[2] == 1 && S[3] == 0)
573         { // Cas 1, arête 3
574           V[0] = -1;
575           V[1] = -1;
576           V[2] = -1;
577           V[3] = intersectionSegmentPlan(it4, 3);
578           V[4] = -1;
579           V[5] = -1;
580           cas1(V, it4);
581         }
582
583       else if (S[0] == 0 && S[1] == -1 && S[2] == 1 && S[3] == 1)
584         { // Cas 2, arêtes 3 4
585           V[0] = -1;
586           V[1] = -1;
587           V[2] = -1;
588           V[3] = intersectionSegmentPlan(it4, 3);
589           V[4] = intersectionSegmentPlan(it4, 4);
590           V[5] = -1;
591           cas2(V, it4);
592         }
593
594       // -------------------------------------------------------------------
595
596       else if (S[0] == 0 && S[1] == 0 && S[2] == -1 && S[3] == -1)
597         GMmoins[TETRA4].push_back(it4);
598
599       else if (S[0] == 0 && S[1] == 0 && S[2] == -1 && S[3] == 0)
600         GMmoins[TETRA4].push_back(it4);
601
602       else if (S[0] == 0 && S[1] == 0 && S[2] == -1 && S[3] == 1)
603         { // Cas 1, arête 5
604           V[0] = -1;
605           V[1] = -1;
606           V[2] = -1;
607           V[3] = -1;
608           V[4] = -1;
609           V[5] = intersectionSegmentPlan(it4, 5);
610           cas1(V, it4);
611         }
612
613       // -------------------------------------------------------------------
614
615       else if (S[0] == 0 && S[1] == 0 && S[2] == 0 && S[3] == -1)
616         GMmoins[TETRA4].push_back(it4);
617
618       else if (S[0] == 0 && S[1] == 0 && S[2] == 0 && S[3] == 0)
619         {
620           cout << "WARNING : TETRA4 numéro " << it4
621               << " entièrement inclus dans la zone de tolérance autour du plan de coupe" << endl;
622           cout << "   --> affecté au groupe " << str_id_GMmoins << endl;
623           GMmoins[TETRA4].push_back(it4);
624         }
625
626       else if (S[0] == 0 && S[1] == 0 && S[2] == 0 && S[3] == 1)
627         GMplus[TETRA4].push_back(it4);
628
629       // -------------------------------------------------------------------
630
631       else if (S[0] == 0 && S[1] == 0 && S[2] == 1 && S[3] == -1)
632         { // Cas 1, arête 5
633           V[0] = -1;
634           V[1] = -1;
635           V[2] = -1;
636           V[3] = -1;
637           V[4] = -1;
638           V[5] = intersectionSegmentPlan(it4, 5);
639           cas1(V, it4);
640         }
641
642       else if (S[0] == 0 && S[1] == 0 && S[2] == 1 && S[3] == 0)
643         GMplus[TETRA4].push_back(it4);
644
645       else if (S[0] == 0 && S[1] == 0 && S[2] == 1 && S[3] == 1)
646         GMplus[TETRA4].push_back(it4);
647
648       // -------------------------------------------------------------------
649
650       else if (S[0] == 0 && S[1] == 1 && S[2] == -1 && S[3] == -1)
651         { // Cas 2, arêtes 3 4
652           V[0] = -1;
653           V[1] = -1;
654           V[2] = -1;
655           V[3] = intersectionSegmentPlan(it4, 3);
656           V[4] = intersectionSegmentPlan(it4, 4);
657           V[5] = -1;
658           cas2(V, it4);
659         }
660
661       else if (S[0] == 0 && S[1] == 1 && S[2] == -1 && S[3] == 0)
662         { // Cas 1, arête 3
663           V[0] = -1;
664           V[1] = -1;
665           V[2] = -1;
666           V[3] = intersectionSegmentPlan(it4, 3);
667           V[4] = -1;
668           V[5] = -1;
669           cas1(V, it4);
670         }
671
672       else if (S[0] == 0 && S[1] == 1 && S[2] == -1 && S[3] == 1)
673         { // Cas 2, arêtes 3 5
674           V[0] = -1;
675           V[1] = -1;
676           V[2] = -1;
677           V[3] = intersectionSegmentPlan(it4, 3);
678           V[4] = -1;
679           V[5] = intersectionSegmentPlan(it4, 5);
680           cas2(V, it4);
681         }
682
683       // -------------------------------------------------------------------
684
685       else if (S[0] == 0 && S[1] == 1 && S[2] == 0 && S[3] == -1)
686         { // Cas 1, arête 4
687           V[0] = -1;
688           V[1] = -1;
689           V[2] = -1;
690           V[3] = -1;
691           V[4] = intersectionSegmentPlan(it4, 4);
692           V[5] = -1;
693           cas1(V, it4);
694         }
695
696       else if (S[0] == 0 && S[1] == 1 && S[2] == 0 && S[3] == 0)
697         GMplus[TETRA4].push_back(it4);
698
699       else if (S[0] == 0 && S[1] == 1 && S[2] == 0 && S[3] == 1)
700         GMplus[TETRA4].push_back(it4);
701
702       // -------------------------------------------------------------------
703
704       else if (S[0] == 0 && S[1] == 1 && S[2] == 1 && S[3] == -1)
705         { // Cas 2, arêtes 4 5
706           V[0] = -1;
707           V[1] = -1;
708           V[2] = -1;
709           V[3] = -1;
710           V[4] = intersectionSegmentPlan(it4, 4);
711           V[5] = intersectionSegmentPlan(it4, 5);
712           cas2(V, it4);
713         }
714
715       else if (S[0] == 0 && S[1] == 1 && S[2] == 1 && S[3] == 0)
716         GMplus[TETRA4].push_back(it4);
717
718       else if (S[0] == 0 && S[1] == 1 && S[2] == 1 && S[3] == 1)
719         GMplus[TETRA4].push_back(it4);
720
721       // -------------------------------------------------------------------
722
723       else if (S[0] == 1 && S[1] == -1 && S[2] == -1 && S[3] == -1)
724         { // Cas 3, arêtes 0 1 2
725           V[0] = intersectionSegmentPlan(it4, 0);
726           V[1] = intersectionSegmentPlan(it4, 1);
727           V[2] = intersectionSegmentPlan(it4, 2);
728           V[3] = -1;
729           V[4] = -1;
730           V[5] = -1;
731           cas3(V, it4);
732         }
733
734       else if (S[0] == 1 && S[1] == -1 && S[2] == -1 && S[3] == 0)
735         { // Cas 2, arêtes 0 1
736           V[0] = intersectionSegmentPlan(it4, 0);
737           V[1] = intersectionSegmentPlan(it4, 1);
738           V[2] = -1;
739           V[3] = -1;
740           V[4] = -1;
741           V[5] = -1;
742           cas2(V, it4);
743         }
744
745       else if (S[0] == 1 && S[1] == -1 && S[2] == -1 && S[3] == 1)
746         { // Cas 4, arêtes 0 1 4 5
747           V[0] = intersectionSegmentPlan(it4, 0);
748           V[1] = intersectionSegmentPlan(it4, 1);
749           V[2] = -1;
750           V[3] = -1;
751           V[4] = intersectionSegmentPlan(it4, 4);
752           V[5] = intersectionSegmentPlan(it4, 5);
753           cas4(V, it4);
754         }
755
756       // -------------------------------------------------------------------
757
758       else if (S[0] == 1 && S[1] == -1 && S[2] == 0 && S[3] == -1)
759         { // Cas 2, arêtes 0 2
760           V[0] = intersectionSegmentPlan(it4, 0);
761           V[1] = -1;
762           V[2] = intersectionSegmentPlan(it4, 2);
763           V[3] = -1;
764           V[4] = -1;
765           V[5] = -1;
766           cas2(V, it4);
767         }
768
769       else if (S[0] == 1 && S[1] == -1 && S[2] == 0 && S[3] == 0)
770         { // Cas 1, arête 0
771           V[0] = intersectionSegmentPlan(it4, 0);
772           V[1] = -1;
773           V[2] = -1;
774           V[3] = -1;
775           V[4] = -1;
776           V[5] = -1;
777           cas1(V, it4);
778         }
779
780       else if (S[0] == 1 && S[1] == -1 && S[2] == 0 && S[3] == 1)
781         { // Cas 2, arêtes 0 4
782           V[0] = intersectionSegmentPlan(it4, 0);
783           V[1] = -1;
784           V[2] = -1;
785           V[3] = -1;
786           V[4] = intersectionSegmentPlan(it4, 4);
787           V[5] = -1;
788           cas2(V, it4);
789         }
790
791       // -------------------------------------------------------------------
792
793       else if (S[0] == 1 && S[1] == -1 && S[2] == 1 && S[3] == -1)
794         { // Cas 4, arêtes 0 2 3 5
795           V[0] = intersectionSegmentPlan(it4, 0);
796           V[1] = -1;
797           V[2] = intersectionSegmentPlan(it4, 2);
798           V[3] = intersectionSegmentPlan(it4, 3);
799           V[4] = -1;
800           V[5] = intersectionSegmentPlan(it4, 5);
801           cas4(V, it4);
802         }
803
804       else if (S[0] == 1 && S[1] == -1 && S[2] == 1 && S[3] == 0)
805         { // Cas 2, arêtes 0 3
806           V[0] = intersectionSegmentPlan(it4, 0);
807           V[1] = -1;
808           V[2] = -1;
809           V[3] = intersectionSegmentPlan(it4, 3);
810           V[4] = -1;
811           V[5] = -1;
812           cas2(V, it4);
813         }
814
815       else if (S[0] == 1 && S[1] == -1 && S[2] == 1 && S[3] == 1)
816         { // Cas 3, arêtes 0 3 4
817           V[0] = intersectionSegmentPlan(it4, 0);
818           V[1] = -1;
819           V[2] = -1;
820           V[3] = intersectionSegmentPlan(it4, 3);
821           V[4] = intersectionSegmentPlan(it4, 4);
822           V[5] = -1;
823           cas3(V, it4);
824         }
825
826       // -------------------------------------------------------------------
827
828       else if (S[0] == 1 && S[1] == 0 && S[2] == -1 && S[3] == -1)
829         { // Cas 2, arêtes 1 2
830           V[0] = -1;
831           V[1] = intersectionSegmentPlan(it4, 1);
832           V[2] = intersectionSegmentPlan(it4, 2);
833           V[3] = -1;
834           V[4] = -1;
835           V[5] = -1;
836           cas2(V, it4);
837         }
838
839       else if (S[0] == 1 && S[1] == 0 && S[2] == -1 && S[3] == 0)
840         { // Cas 1, arête 1
841           V[0] = -1;
842           V[1] = intersectionSegmentPlan(it4, 1);
843           V[2] = -1;
844           V[3] = -1;
845           V[4] = -1;
846           V[5] = -1;
847           cas1(V, it4);
848         }
849
850       else if (S[0] == 1 && S[1] == 0 && S[2] == -1 && S[3] == 1)
851         { // Cas 2, arêtes 1 5
852           V[0] = -1;
853           V[1] = intersectionSegmentPlan(it4, 1);
854           V[2] = -1;
855           V[3] = -1;
856           V[4] = -1;
857           V[5] = intersectionSegmentPlan(it4, 5);
858           cas2(V, it4);
859         }
860
861       // -------------------------------------------------------------------
862
863       else if (S[0] == 1 && S[1] == 0 && S[2] == 0 && S[3] == -1)
864         { // Cas 1, arête 2
865           V[0] = -1;
866           V[1] = -1;
867           V[2] = intersectionSegmentPlan(it4, 2);
868           V[3] = -1;
869           V[4] = -1;
870           V[5] = -1;
871           cas1(V, it4);
872         }
873
874       else if (S[0] == 1 && S[1] == 0 && S[2] == 0 && S[3] == 0)
875         GMplus[TETRA4].push_back(it4);
876
877       else if (S[0] == 1 && S[1] == 0 && S[2] == 0 && S[3] == 1)
878         GMplus[TETRA4].push_back(it4);
879
880       // -------------------------------------------------------------------
881
882       else if (S[0] == 1 && S[1] == 0 && S[2] == 1 && S[3] == -1)
883         { // Cas 2, arêtes 2 5
884           V[0] = -1;
885           V[1] = -1;
886           V[2] = intersectionSegmentPlan(it4, 2);
887           V[3] = -1;
888           V[4] = -1;
889           V[5] = intersectionSegmentPlan(it4, 5);
890           cas2(V, it4);
891         }
892
893       else if (S[0] == 1 && S[1] == 0 && S[2] == 1 && S[3] == 0)
894         GMplus[TETRA4].push_back(it4);
895
896       else if (S[0] == 1 && S[1] == 0 && S[2] == 1 && S[3] == 1)
897         GMplus[TETRA4].push_back(it4);
898
899       // -------------------------------------------------------------------
900
901       else if (S[0] == 1 && S[1] == 1 && S[2] == -1 && S[3] == -1)
902         { // Cas 4, arêtes 1 2 3 4
903           V[0] = -1;
904           V[1] = intersectionSegmentPlan(it4, 1);
905           V[2] = intersectionSegmentPlan(it4, 2);
906           V[3] = intersectionSegmentPlan(it4, 3);
907           V[4] = intersectionSegmentPlan(it4, 4);
908           V[5] = -1;
909           cas4(V, it4);
910         }
911
912       else if (S[0] == 1 && S[1] == 1 && S[2] == -1 && S[3] == 0)
913         { // Cas 2, arêtes 1 3
914           V[0] = -1;
915           V[1] = intersectionSegmentPlan(it4, 1);
916           V[2] = -1;
917           V[3] = intersectionSegmentPlan(it4, 3);
918           V[4] = -1;
919           V[5] = -1;
920           cas2(V, it4);
921         }
922
923       else if (S[0] == 1 && S[1] == 1 && S[2] == -1 && S[3] == 1)
924         { // Cas 3, arêtes 1 3 5
925           V[0] = -1;
926           V[1] = intersectionSegmentPlan(it4, 1);
927           V[2] = -1;
928           V[3] = intersectionSegmentPlan(it4, 3);
929           V[4] = -1;
930           V[5] = intersectionSegmentPlan(it4, 5);
931           cas3(V, it4);
932         }
933
934       // -------------------------------------------------------------------
935
936       else if (S[0] == 1 && S[1] == 1 && S[2] == 0 && S[3] == -1)
937         { // Cas 2, arêtes 2 4
938           V[0] = -1;
939           V[1] = -1;
940           V[2] = intersectionSegmentPlan(it4, 2);
941           V[3] = -1;
942           V[4] = intersectionSegmentPlan(it4, 4);
943           V[5] = -1;
944           cas2(V, it4);
945         }
946
947       else if (S[0] == 1 && S[1] == 1 && S[2] == 0 && S[3] == 0)
948         GMplus[TETRA4].push_back(it4);
949
950       else if (S[0] == 1 && S[1] == 1 && S[2] == 0 && S[3] == 1)
951         GMplus[TETRA4].push_back(it4);
952
953       // -------------------------------------------------------------------
954
955       else if (S[0] == 1 && S[1] == 1 && S[2] == 1 && S[3] == -1)
956         { // Cas 3, arêtes 2 4 5
957           V[0] = -1;
958           V[1] = -1;
959           V[2] = intersectionSegmentPlan(it4, 2);
960           V[3] = -1;
961           V[4] = intersectionSegmentPlan(it4, 4);
962           V[5] = intersectionSegmentPlan(it4, 5);
963           cas3(V, it4);
964         }
965
966       else if (S[0] == 1 && S[1] == 1 && S[2] == 1 && S[3] == 0)
967         GMplus[TETRA4].push_back(it4);
968
969       else if (S[0] == 1 && S[1] == 1 && S[2] == 1 && S[3] == 1)
970         GMplus[TETRA4].push_back(it4);
971
972       else
973         ERREUR("Cas non prévu");
974
975     }
976   cout << chrono() << " - Fin de la boucle sur les TETRA4" << endl;
977
978   // cout << "indexNouveauxNoeuds = " << indexNouveauxNoeuds << endl;
979   newXX.resize(indexNouveauxNoeuds - MAILLAGE1->nombreNoeudsMaillage);
980   newYY.resize(indexNouveauxNoeuds - MAILLAGE1->nombreNoeudsMaillage);
981   newZZ.resize(indexNouveauxNoeuds - MAILLAGE1->nombreNoeudsMaillage);
982
983   if (cptNouvellesMailles[TETRA4])
984     newCNX[TETRA4].resize(4 * cptNouvellesMailles[TETRA4]);
985   if (cptNouvellesMailles[PYRAM5])
986     newCNX[PYRAM5].resize(5 * cptNouvellesMailles[PYRAM5]);
987   if (cptNouvellesMailles[PENTA6])
988     newCNX[PENTA6].resize(6 * cptNouvellesMailles[PENTA6]);
989
990   // =========================================================================================
991   //                          2. Constitution du maillage final
992   // =========================================================================================
993
994   cout << chrono() << " - Constitution du maillage final" << endl;
995
996   MAILLAGE2 = new Maillage(str_id_maillagenew);
997   MAILLAGE2->dimensionMaillage = MAILLAGE1->dimensionMaillage;
998   MAILLAGE2->dimensionEspace = MAILLAGE1->dimensionEspace;
999   strcpy(MAILLAGE2->axisname, MAILLAGE1->axisname);
1000   strcpy(MAILLAGE2->unitname, MAILLAGE1->unitname);
1001   MAILLAGE2->nombreNoeudsMaillage = indexNouveauxNoeuds;
1002   MAILLAGE2->nombreMaillesMaillage = MAILLAGE1->nombreMaillesMaillage + cptNouvellesMailles[TETRA4]
1003       + cptNouvellesMailles[PYRAM5] + cptNouvellesMailles[PENTA6];
1004
1005   // ---------- Coordonnées
1006   // Optimisation de la mémoire au détriment du temps
1007
1008   // Héritage des coordonnées MAILLAGE1
1009   MAILLAGE2->XX = (float*) malloc(sizeof(float) * MAILLAGE2->nombreNoeudsMaillage);
1010   for (int i = 0; i < MAILLAGE1->nombreNoeudsMaillage; i++)
1011     *(MAILLAGE2->XX + i) = *(MAILLAGE1->XX + i);
1012   free(MAILLAGE1->XX);
1013   MAILLAGE2->YY = (float*) malloc(sizeof(float) * MAILLAGE2->nombreNoeudsMaillage);
1014   for (int i = 0; i < MAILLAGE1->nombreNoeudsMaillage; i++)
1015     *(MAILLAGE2->YY + i) = *(MAILLAGE1->YY + i);
1016   free(MAILLAGE1->YY);
1017   MAILLAGE2->ZZ = (float*) malloc(sizeof(float) * MAILLAGE2->nombreNoeudsMaillage);
1018   for (int i = 0; i < MAILLAGE1->nombreNoeudsMaillage; i++)
1019     *(MAILLAGE2->ZZ + i) = *(MAILLAGE1->ZZ + i);
1020   free(MAILLAGE1->ZZ);
1021
1022   // Coordonnées des noeuds créés
1023   for (int i = 0; i < MAILLAGE2->nombreNoeudsMaillage - MAILLAGE1->nombreNoeudsMaillage; i++)
1024     {
1025       *(MAILLAGE2->XX + MAILLAGE1->nombreNoeudsMaillage + i) = newXX[i];
1026       *(MAILLAGE2->YY + MAILLAGE1->nombreNoeudsMaillage + i) = newYY[i];
1027       *(MAILLAGE2->ZZ + MAILLAGE1->nombreNoeudsMaillage + i) = newZZ[i];
1028       // cout << "Nouveaux noeuds, indice " << i << " : " << newXX[i] << " " << newYY[i] << " " << newZZ[i] << " " << endl;
1029     }
1030
1031   // Legacy mailles maillage 1 +
1032   for (int itm = (int) POI1; itm <= (int) HEXA20; itm++)
1033     {
1034       TYPE_MAILLE tm = (TYPE_MAILLE) itm;
1035       if (tm != TETRA4 && tm != PYRAM5 && tm != PENTA6)
1036         {
1037           // Pour les types autres que TETRA4 PYRAM5 PENTA6 on fait seulement pointer CNX2 vers CNX1
1038           if (MAILLAGE1->EFFECTIFS_TYPES[tm])
1039             MAILLAGE2->CNX[tm] = MAILLAGE1->CNX[tm];
1040           MAILLAGE2->EFFECTIFS_TYPES[tm] = MAILLAGE1->EFFECTIFS_TYPES[tm];
1041         }
1042       else
1043         {
1044           // Pour les types TETRA4 PYRAM5 PENTA6 on recopie CNX1 et on ajoute à la suite les newCNX
1045           // cout << "Legacy " << tm << " effectif " << MAILLAGE1->EFFECTIFS_TYPES[tm] << endl;
1046           int tailleType = Nnoeuds(tm);
1047
1048           MAILLAGE2->CNX[tm] = (int*) malloc(sizeof(int) * tailleType * (MAILLAGE1->EFFECTIFS_TYPES[tm]
1049               + cptNouvellesMailles[tm]));
1050           for (int i = 0; i < MAILLAGE1->EFFECTIFS_TYPES[tm]; i++)
1051             for (int j = 0; j < tailleType; j++)
1052               *(MAILLAGE2->CNX[tm] + tailleType * i + j) = *(MAILLAGE1->CNX[tm] + tailleType * i + j);
1053
1054           for (int i = 0; i < cptNouvellesMailles[tm]; i++)
1055             for (int j = 0; j < tailleType; j++)
1056               *(MAILLAGE2->CNX[tm] + tailleType * (MAILLAGE1->EFFECTIFS_TYPES[tm] + i) + j) = newCNX[tm][i * tailleType
1057                   + j];
1058
1059           MAILLAGE2->EFFECTIFS_TYPES[tm] = MAILLAGE1->EFFECTIFS_TYPES[tm] + cptNouvellesMailles[tm];
1060         }
1061     }
1062
1063   // Restit CNX
1064
1065   //   cout << "Maillage 2 - CNX TETRA4 : " << endl;
1066   //  ;
1067   //  for (int i = 0; i < MAILLAGE2->EFFECTIFS_TYPES[TETRA4]; i++)
1068   //    {
1069   //      cout << "Maille " << i << " : ";
1070   //      for (int j = 0; j < 4; j++)
1071   //        cout << MAILLAGE2->CNX[TETRA4][i * 4 + j] << " ";
1072   //      cout << endl;
1073   //    }
1074   //  cout << endl;
1075   //  cout << "Maillage 2 - CNX PENTA6 : " << endl;
1076   //  ;
1077   //  for (int i = 0; i < MAILLAGE2->EFFECTIFS_TYPES[PENTA6]; i++)
1078   //    {
1079   //      cout << "Maille " << i << " : ";
1080   //      for (int j = 0; j < 6; j++)
1081   //        cout << MAILLAGE2->CNX[PENTA6][i * 6 + j] << " ";
1082   //      cout << endl;
1083   //    }
1084   //  cout << endl;
1085
1086   // Groupes de mailles
1087   MAILLAGE2->GM = MAILLAGE1->GM;
1088   MAILLAGE2->GM[str_id_GMplus] = GMplus;
1089   MAILLAGE2->GM[str_id_GMmoins] = GMmoins;
1090
1091   MAILLAGE2->GN = MAILLAGE1->GN;
1092
1093   cout << chrono() << " - Ecriture du fichier MED" << endl;
1094
1095   MAILLAGE2->outputMED(ficMEDout);
1096   cout << chrono() << " - Terminé" << endl << endl;
1097
1098   return 0;
1099
1100 }