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