Salome HOME
Replace oe by ?
[modules/smesh.git] / src / Tools / MeshCut / MeshCut_Utils.cxx
1 #include "MeshCut_Utils.hxx"
2
3 #include <iostream>
4 #include <string>
5 #include <sstream>
6 #include <cstdlib>
7 #include <cstring>
8
9 using namespace std;
10 using namespace MESHCUT;
11
12 //string pathUsers = (string) "/var/www/XMeshLab/users/";
13
14 bool MESHCUT::estUnTypeMaille(std::string S)
15 {
16   if (S == (string) "POI1" || S == (string) "SEG2" || S == (string) "SEG3" || S == (string) "TRIA3" || S
17       == (string) "TRIA6" || S == (string) "QUAD4" || S == (string) "QUAD8" || S == (string) "QUAD9" || S
18       == (string) "TETRA4" || S == (string) "TETRA10" || S == (string) "PYRAM5" || S == (string) "PYRAM13" || S
19       == (string) "PENTA6" || S == (string) "PENTA15" || S == (string) "HEXA8" || S == (string) "HEXA20" || S
20       == (string) "HEXA27")
21     return true;
22   else
23     return false;
24 }
25
26 void MESHCUT::ERREUR(const char* msg)
27 {
28   cout << endl << "====== ERREUR ====== " << msg << endl << endl;
29   exit(-1);
30 }
31
32 char* MESHCUT::string2char(std::string str)
33 {
34   // créer le buffer pour copier la chaîne
35   size_t size = str.size() + 1;
36   char* buffer = new char[size];
37   // copier la chaîne
38   strncpy(buffer, str.c_str(), size);
39
40   // libérer la mémoire
41   //delete [] buffer;
42
43   return buffer;
44 }
45
46 std::string MESHCUT::int2string(int k)
47 {
48   std::stringstream oss;
49   oss << k;
50   return oss.str(); //  oss.seekp (ios_base::beg);
51 }
52
53 float MESHCUT::char2float(const char* ch)
54 {
55   return atof(ch);
56 }
57
58 std::string MESHCUT::float2string(float f)
59 {
60   stringstream buf;
61   buf << fixed << f;
62   string s = buf.str();
63   return s;
64 }
65
66 bool MESHCUT::appartient(std::string e, std::string tableau[], int taille)
67 {
68   for (int i = 0; i < taille; i++)
69     if (tableau[i] == e)
70       return true;
71   return false;
72 }
73
74 float MESHCUT::arrondi(float x)
75 {
76   if (x > 0 and x < 1.0e-5)
77     return 0;
78   else if (x < 0 and x > -1.0e-5)
79     return 0;
80   else
81     return x;
82 }
83
84 int MESHCUT::numNoeudPointe(std::string b1, std::string b2, std::string b3)
85 {
86   if (b1 == "1" && b2 == "1" && b3 == "2")
87     return 2;
88   else if (b1 == "1" && b2 == "2" && b3 == "1")
89     return 1;
90   else if (b1 == "1" && b2 == "2" && b3 == "2")
91     return 2;
92   else if (b1 == "2" && b2 == "1" && b3 == "1")
93     return 3;
94   else if (b1 == "2" && b2 == "1" && b3 == "2")
95     return 3;
96   else if (b1 == "2" && b2 == "2" && b3 == "1")
97     return 1;
98   else
99     return -1;
100 }
101
102 std::string MESHCUT::strip(std::string S)
103 {
104   if (S.empty())
105     return S;
106   int startIndex = S.find_first_not_of(" ");
107   int endIndex = S.find_last_not_of(" ");
108   return S.substr(startIndex, (endIndex - startIndex + 1));
109 }
110
111 std::string MESHCUT::entierSur10_g(int i)
112 {
113   if (i > 999999999)
114     ERREUR("Tentative d'ecriture sur 10 caracteres d'un entier plus grand que 999999999");
115   if (i < 10)
116     return int2string(i) + (string) "         ";
117   else if (i < 100)
118     return int2string(i) + (string) "        ";
119   else if (i < 1000)
120     return int2string(i) + (string) "       ";
121   else if (i < 10000)
122     return int2string(i) + (string) "      ";
123   else if (i < 100000)
124     return int2string(i) + (string) "     ";
125   else if (i < 1000000)
126     return int2string(i) + (string) "    ";
127   else if (i < 10000000)
128     return int2string(i) + (string) "   ";
129   else if (i < 100000000)
130     return int2string(i) + (string) "  ";
131   else if (i < 1000000000)
132     return int2string(i) + (string) " ";
133   else
134     return int2string(i);
135 }
136
137 std::string MESHCUT::entierSur10_d(int i)
138 {
139   if (i > 999999999)
140     ERREUR("Tentative d'ecriture sur 10 caracteres d'un entier plus grand que 999999999");
141   if (i < 10)
142     return (string) "         " + int2string(i);
143   else if (i < 100)
144     return (string) "        " + int2string(i);
145   else if (i < 1000)
146     return (string) "       " + int2string(i);
147   else if (i < 10000)
148     return (string) "      " + int2string(i);
149   else if (i < 100000)
150     return (string) "     " + int2string(i);
151   else if (i < 1000000)
152     return (string) "    " + int2string(i);
153   else if (i < 10000000)
154     return (string) "   " + int2string(i);
155   else if (i < 100000000)
156     return (string) "  " + int2string(i);
157   else if (i < 1000000000)
158     return (string) " " + int2string(i);
159   else
160     return int2string(i);
161 }
162
163 std::string MESHCUT::typeEnsight(std::string type)
164 {
165   if (type == (string) "POI1")
166     return (string) "point";
167   else if (type == (string) "SEG2")
168     return (string) "bar2";
169   else if (type == (string) "SEG3")
170     return (string) "bar2";// ATTENTION, triche!
171   else if (type == (string) "TRIA3")
172     return (string) "tria3";
173   else if (type == (string) "TRIA6")
174     return (string) "tria3";// ATTENTION, triche!
175   else if (type == (string) "QUAD4")
176     return (string) "quad4";
177   else if (type == (string) "QUAD8")
178     return (string) "quad4"; // ATTENTION, triche!
179   else if (type == (string) "QUAD9")
180     ERREUR("Type QUAD9 non supporte par Ensight");
181   else if (type == (string) "TETRA4")
182     return (string) "tetra4";
183   else if (type == (string) "TETRA10")
184     return (string) "tetra4"; // ATTENTION, triche!
185   else if (type == (string) "PYRAM5")
186     return (string) "pyramid5";
187   else if (type == (string) "PYRAM13")
188     return (string) "pyramid5"; // ATTENTION, triche!
189   else if (type == (string) "PENTA6")
190     return (string) "penta6";
191   else if (type == (string) "PENTA15")
192     return (string) "penta6"; // ATTENTION, triche!
193   else if (type == (string) "HEXA8")
194     return (string) "hexa8";
195   else if (type == (string) "HEXA20")
196     return (string) "hexa8"; // ATTENTION, triche!
197   else if (type == (string) "HEXA27")
198     ERREUR("Type HEXA27 non supporte par Ensight");
199   else
200     ERREUR("Type maille non reconnu (routine \"typeEnsight\"");
201   return (string) "";
202 }
203
204 int MESHCUT::Nnoeuds(TYPE_MAILLE type)
205 {
206   switch (type)
207   {
208     case POI1:
209       {
210         return 1;
211         break;
212       }
213     case SEG2:
214       {
215         return 2;
216         break;
217       }
218     case SEG3:
219       {
220         return 3;
221         break;
222       }
223     case TRIA3:
224       {
225         return 3;
226         break;
227       }
228     case TRIA6:
229       {
230         return 6;
231         break;
232       }
233     case QUAD4:
234       {
235         return 4;
236         break;
237       }
238     case QUAD8:
239       {
240         return 8;
241         break;
242       }
243       //case QUAD9:                   { return 9; break; }
244     case TETRA4:
245       {
246         return 4;
247         break;
248       }
249     case TETRA10:
250       {
251         return 10;
252         break;
253       }
254     case PYRAM5:
255       {
256         return 5;
257         break;
258       }
259     case PYRAM13:
260       {
261         return 13;
262         break;
263       }
264     case PENTA6:
265       {
266         return 6;
267         break;
268       }
269     case PENTA15:
270       {
271         return 15;
272         break;
273       }
274     case HEXA8:
275       {
276         return 8;
277         break;
278       }
279     case HEXA20:
280       {
281         return 20;
282         break;
283       }
284       //case HEXA27:                      { return 27;    break; }
285     default:
286       ERREUR("Type maille non reconnu (routine Nnoeuds)");
287   }
288   return 0;
289 }
290
291 int MESHCUT::NnoeudsGeom(TYPE_MAILLE type)
292 {
293   switch (type)
294   {
295     case POI1:
296       {
297         return 1;
298         break;
299       }
300     case SEG2:
301       {
302         return 2;
303         break;
304       }
305     case SEG3:
306       {
307         return 2;
308         break;
309       }
310     case TRIA3:
311       {
312         return 3;
313         break;
314       }
315     case TRIA6:
316       {
317         return 3;
318         break;
319       }
320     case QUAD4:
321       {
322         return 4;
323         break;
324       }
325     case QUAD8:
326       {
327         return 4;
328         break;
329       }
330       //case QUAD9:                   { return 9; break; }
331     case TETRA4:
332       {
333         return 4;
334         break;
335       }
336     case TETRA10:
337       {
338         return 4;
339         break;
340       }
341     case PYRAM5:
342       {
343         return 5;
344         break;
345       }
346     case PYRAM13:
347       {
348         return 5;
349         break;
350       }
351     case PENTA6:
352       {
353         return 6;
354         break;
355       }
356     case PENTA15:
357       {
358         return 6;
359         break;
360       }
361     case HEXA8:
362       {
363         return 8;
364         break;
365       }
366     case HEXA20:
367       {
368         return 8;
369         break;
370       }
371       //case HEXA27:                      { return 27;    break; }
372     default:
373       ERREUR("Type maille non reconnu (routine NnoeudsGeom)");
374   }
375   return 0;
376 }
377
378 int MESHCUT::codeGMSH(std::string type)
379 {
380   if (type == (string) "POI1")
381     ERREUR("POI1 non pris en charge par GMSH");
382   else if (type == (string) "SEG2")
383     return 1;
384   else if (type == (string) "SEG3")
385     return 8;
386   else if (type == (string) "TRIA3")
387     return 2;
388   else if (type == (string) "TRIA6")
389     return 9;
390   else if (type == (string) "QUAD4")
391     return 3;
392   else if (type == (string) "QUAD8")
393     return 16;
394   else if (type == (string) "QUAD9")
395     return 10;
396   else if (type == (string) "TETRA4")
397     return 4;
398   else if (type == (string) "TETRA10")
399     return 11;
400   else if (type == (string) "PYRAM5")
401     return 7;
402   else if (type == (string) "PENTA6")
403     return 6;
404   else if (type == (string) "PENTA15")
405     return 18;
406   else if (type == (string) "HEXA8")
407     return 5;
408   else if (type == (string) "HEXA20")
409     return 17;
410   else if (type == (string) "HEXA27")
411     return 12;
412   else
413     ERREUR("Type maille non reconnu (routine codeGMSH)");
414   return 0;
415 }
416
417 std::string MESHCUT::floatEnsight(float x)
418 {
419   char buf[12];
420   string s;
421   if (x < 0.0)
422     sprintf(buf, "%1.5E", x);
423   else
424     sprintf(buf, " %1.5E", x);
425   s = (string) buf;
426   s.erase(10, 1);
427   return s;
428 }
429
430 bool MESHCUT::typeComplexe(std::string type)
431 {
432   if (type == (string) "SEG3")
433     return true;
434   else if (type == (string) "TRIA6")
435     return true;
436   else if (type == (string) "QUAD8")
437     return true;
438   else if (type == (string) "QUAD9")
439     return true;
440   else if (type == (string) "TETRA10")
441     return true;
442   else if (type == (string) "PYRAM13")
443     return true;
444   else if (type == (string) "PENTA15")
445     return true;
446   else if (type == (string) "HEXA20")
447     return true;
448   else if (type == (string) "HEXA27")
449     return true;
450   else
451     return false;
452 }
453
454 std::string MESHCUT::ASTER8(std::string s)
455 {
456   if (s.size() == 0)
457     return (s + (string) "        ");
458   else if (s.size() == 1)
459     return (s + (string) "       ");
460   else if (s.size() == 2)
461     return (s + (string) "      ");
462   else if (s.size() == 3)
463     return (s + (string) "     ");
464   else if (s.size() == 4)
465     return (s + (string) "    ");
466   else if (s.size() == 5)
467     return (s + (string) "   ");
468   else if (s.size() == 6)
469     return (s + (string) "  ");
470   else if (s.size() == 7)
471     return (s + (string) " ");
472   else if (s.size() == 8)
473     return (s);
474   else
475     ERREUR("Depassement de la longueur max 8 pour une chaine ASTER");
476   return (s);
477 }
478
479 /*!
480  *  Distance à laquelle doit se tenir l'observateur sur un axe
481  *  pour voir sous 90° un objet centré de dimensions a et b selon les deux autres axes.
482  *  Si on ne tient pas compte de la dimension de l'objet selon l'axe choisi,
483  *  la formule d_obs=max(a,b)/2 donne la cote
484  *  qui permet de voir l'objet plat dans un angle de 90°.
485  *  A cela il faut ajouter la dimension de l'objet selon l'axe d'observation = c.
486  *
487  *  @param a dimensions de l'objet selon un des axes normal à l'axe d'observation
488  *  @param b dimensions de l'objet selon l'autre axe normal à l'axe d'observation
489  *  @param c est la dimension de l'objet selon l'axe d'observation
490  */
491 float MESHCUT::dObservateur(float a, float b, float c)
492 {
493   return (max(a, b) / 2.0 + c);
494 }
495
496 int MESHCUT::copieFichier(std::string source, std::string cible)
497 {
498   FILE *fsource, *fcible;
499   char buffer[512];
500   int NbLu;
501   if ((fsource = fopen(string2char(source), "rb")) == NULL)
502     return -1;
503   if ((fcible = fopen(string2char(cible), "wb")) == NULL)
504     {
505       fclose(fsource);
506       return -2;
507     }
508   while ((NbLu = fread(buffer, 1, 512, fsource)) != 0)
509     fwrite(buffer, 1, NbLu, fcible);
510   fclose(fcible);
511   fclose(fsource);
512   return 0;
513 }
514
515 med_geometry_type MESHCUT::InstanceMGE(TYPE_MAILLE TYPE)
516 {
517   med_geometry_type typeBanaliseMED;
518
519   switch (TYPE)
520   {
521     case POI1:
522       typeBanaliseMED = MED_POINT1;
523       break; // Attention, piège !
524     case SEG2:
525       typeBanaliseMED = MED_SEG2;
526       break;
527     case SEG3:
528       typeBanaliseMED = MED_SEG3;
529       break;
530     case TRIA3:
531       typeBanaliseMED = MED_TRIA3;
532       break;
533     case TRIA6:
534       typeBanaliseMED = MED_TRIA6;
535       break;
536     case QUAD4:
537       typeBanaliseMED = MED_QUAD4;
538       break;
539     case QUAD8:
540       typeBanaliseMED = MED_QUAD8;
541       break;
542     case TETRA4:
543       typeBanaliseMED = MED_TETRA4;
544       break;
545     case TETRA10:
546       typeBanaliseMED = MED_TETRA10;
547       break;
548     case PYRAM5:
549       typeBanaliseMED = MED_PYRA5;
550       break; // Attention, piège !
551     case PYRAM13:
552       typeBanaliseMED = MED_PYRA13;
553       break; // Attention, piège !
554     case PENTA6:
555       typeBanaliseMED = MED_PENTA6;
556       break;
557     case PENTA15:
558       typeBanaliseMED = MED_PENTA15;
559       break;
560     case HEXA8:
561       typeBanaliseMED = MED_HEXA8;
562       break;
563     case HEXA20:
564       typeBanaliseMED = MED_HEXA20;
565       break;
566     default:
567       ERREUR("Routine InstanceMGE, type inconnu ");
568   }
569   return typeBanaliseMED;
570 }
571
572 int MESHCUT::chrono()
573 {
574   return clock() / CLOCKS_PER_SEC;
575 }
576
577 TYPE_MAILLE MESHCUT::typeMaille(std::string type)
578 {
579   if (type == (string) "POI1")
580     return POI1;
581   else if (type == (string) "SEG2")
582     return SEG2;
583   else if (type == (string) "SEG3")
584     return SEG3;
585   else if (type == (string) "TRIA3")
586     return TRIA3;
587   else if (type == (string) "TRIA6")
588     return TRIA6;
589   else if (type == (string) "QUAD4")
590     return QUAD4;
591   else if (type == (string) "QUAD8")
592     return QUAD8;
593   else if (type == (string) "TETRA4")
594     return TETRA4;
595   else if (type == (string) "TETRA10")
596     return TETRA10;
597   else if (type == (string) "PYRAM5")
598     return PYRAM5;
599   else if (type == (string) "PYRAM13")
600     return PYRAM13;
601   else if (type == (string) "PENTA6")
602     return PENTA6;
603   else if (type == (string) "PENTA15")
604     return PENTA15;
605   else if (type == (string) "HEXA8")
606     return HEXA8;
607   else if (type == (string) "HEXA20")
608     return HEXA20;
609   else
610     ERREUR("ERREUR procédure typeMaille, type non reconnu");
611   return POI1;
612 }
613
614 std::string MESHCUT::MGE2string(med_geometry_type MGE)
615 {
616   if (MGE == MED_NONE)
617     return (string) "NOEUD";
618   else if (MGE == MED_POINT1)
619     return (string) "POI1";
620   else if (MGE == MED_SEG2)
621     return (string) "SEG2";
622   else if (MGE == MED_SEG3)
623     return (string) "SEG3";
624   else if (MGE == MED_TRIA3)
625     return (string) "TRIA3";
626   else if (MGE == MED_TRIA6)
627     return (string) "TRIA6";
628   else if (MGE == MED_QUAD4)
629     return (string) "QUAD4";
630   else if (MGE == MED_QUAD8)
631     return (string) "QUAD8";
632   else if (MGE == MED_TETRA4)
633     return (string) "TETRA4";
634   else if (MGE == MED_TETRA10)
635     return (string) "TETRA10";
636   else if (MGE == MED_PYRA5)
637     return (string) "PYRAM5";
638   else if (MGE == MED_PYRA13)
639     return (string) "PYRAM13";
640   else if (MGE == MED_PENTA6)
641     return (string) "PENTA6";
642   else if (MGE == MED_PENTA15)
643     return (string) "PENTA15";
644   else if (MGE == MED_HEXA8)
645     return (string) "HEXA8";
646   else if (MGE == MED_HEXA20)
647     return (string) "HEXA20";
648   else
649     ERREUR("ERREUR procédure MGE2string, type non reconnu");
650   return (string) "NOEUD";
651 }
652
653 std::string MESHCUT::TM2string(TYPE_MAILLE MGE)
654 {
655   if (MGE == POI1)
656     return (string) "POI1";
657   else if (MGE == SEG2)
658     return (string) "SEG2";
659   else if (MGE == SEG3)
660     return (string) "SEG3";
661   else if (MGE == TRIA3)
662     return (string) "TRIA3";
663   else if (MGE == TRIA6)
664     return (string) "TRIA6";
665   else if (MGE == QUAD4)
666     return (string) "QUAD4";
667   else if (MGE == QUAD8)
668     return (string) "QUAD8";
669   else if (MGE == TETRA4)
670     return (string) "TETRA4";
671   else if (MGE == TETRA10)
672     return (string) "TETRA10";
673   else if (MGE == PYRAM5)
674     return (string) "PYRAM5";
675   else if (MGE == PYRAM13)
676     return (string) "PYRAM13";
677   else if (MGE == PENTA6)
678     return (string) "PENTA6";
679   else if (MGE == PENTA15)
680     return (string) "PENTA15";
681   else if (MGE == HEXA8)
682     return (string) "HEXA8";
683   else if (MGE == HEXA20)
684     return (string) "HEXA20";
685   else
686     ERREUR("ERREUR procédure TM2string, type non reconnu");
687   return (string) "POI1";
688 }
689
690 TYPE_MAILLE MESHCUT::string2TM(std::string stm)
691 {
692   if (stm == (string) "POI1")
693     return POI1;
694   else if (stm == (string) "SEG2")
695     return SEG2;
696   else if (stm == (string) "SEG3")
697     return SEG3;
698   else if (stm == (string) "TRIA3")
699     return TRIA3;
700   else if (stm == (string) "TRIA6")
701     return TRIA6;
702   else if (stm == (string) "QUAD4")
703     return QUAD4;
704   else if (stm == (string) "QUAD8")
705     return QUAD8;
706   else if (stm == (string) "TETRA4")
707     return TETRA4;
708   else if (stm == (string) "TETRA10")
709     return TETRA10;
710   else if (stm == (string) "PYRAM5")
711     return PYRAM5;
712   else if (stm == (string) "PYRAM13")
713     return PYRAM13;
714   else if (stm == (string) "PENTA6")
715     return PENTA6;
716   else if (stm == (string) "PENTA15")
717     return PENTA15;
718   else if (stm == (string) "HEXA8")
719     return HEXA8;
720   else if (stm == (string) "HEXA20")
721     return HEXA20;
722   else
723     ERREUR("ERREUR procédure string2TM, type non reconnu");
724   return POI1;
725 }
726
727 std::string MESHCUT::coordIndex_ILS(TYPE_MAILLE tm)
728 {
729   if (tm == SEG2)
730     return (string) " 0,1 ";
731   else if (tm == SEG3)
732     return (string) " 0,1 "; // Idem SEG2
733   else if (tm == TRIA3)
734     return (string) " 0,1,2,0 ";
735   else if (tm == TRIA6)
736     return (string) " 0,1,2,0 ";
737   else if (tm == QUAD4)
738     return (string) " 0,1,2,3,0 ";
739   else if (tm == QUAD8)
740     return (string) " 0,1,2,3,0 ";
741   else if (tm == TETRA4)
742     return (string) " 0,1,2,0,-1, 0,3,-1, 1,3,-1, 2,3,-1 ";
743   else if (tm == TETRA10)
744     return (string) " 0,1,2,0,-1, 0,3,-1, 1,3,-1, 2,3,-1 ";
745   else if (tm == PYRAM5)
746     return (string) " 0,1,2,3,0,-1, 0,4,-1, 1,4,-1, 2,4,-1, 3,4,-1 ";
747   else if (tm == PYRAM13)
748     return (string) " 0,1,2,3,0,-1, 0,4,-1, 1,4,-1, 2,4,-1, 3,4,-1 ";
749   else if (tm == PENTA6)
750     return (string) " 0,1,2,0,-1, 3,4,5,3,-1, 0,3,-1, 1,4,-1, 2,5,-1 ";
751   else if (tm == PENTA15)
752     return (string) " 0,1,2,0,-1, 3,4,5,3,-1, 0,3,-1, 1,4,-1, 2,5,-1 ";
753   else if (tm == HEXA8)
754     return (string) " 0,1,2,3,0,-1, 4,5,6,7,4,-1, 0,4,-1, 1,5,-1, 2,6,-1, 3,7,-1 ";
755   else if (tm == HEXA20)
756     return (string) " 0,1,2,3,0,-1, 4,5,6,7,4,-1, 0,4,-1, 1,5,-1, 2,6,-1, 3,7,-1 ";
757   else
758     return (string) "";
759 }
760
761 std::string MESHCUT::coordIndex_IFS(TYPE_MAILLE tm)
762 {
763   if (tm == SEG2)
764     return (string) "  ";
765   else if (tm == SEG3)
766     return (string) "  "; // Idem SEG2
767   else if (tm == TRIA3)
768     return (string) " 0,1,2,0,-1, 0,2,1,0,-1 ";
769   else if (tm == TRIA6)
770     return (string) " 0,1,2,0,-1, 0,2,1,0,-1 ";
771   else if (tm == QUAD4)
772     return (string) " 0,1,2,3,0,-1, 0,3,2,1,0,-1 ";
773   else if (tm == QUAD8)
774     return (string) " 0,1,2,3,0,-1, 0,3,2,1,0,-1 ";
775   else if (tm == TETRA4)
776     return (string) " 0,1,2,0,-1, 0,2,1,0,-1, 0,3,1,0,-1, 0,1,3,0,-1, 1,3,2,1,-1, 1,2,3,1,-1, 0,2,3,0,-1, 0,3,2,0,-1 ";
777   else if (tm == TETRA10)
778     return (string) " 0,1,2,0,-1, 0,2,1,0,-1, 0,3,1,0,-1, 0,1,3,0,-1, 1,3,2,1,-1, 1,2,3,1,-1, 0,2,3,0,-1, 0,3,2,0,-1 ";
779   else if (tm == PYRAM5)
780     return (string) " 0,1,2,3,0,-1, 0,3,2,1,0,-1, 0,1,4,0,-1, 0,4,1,0,-1, 1,2,4,1,-1, 1,4,2,1,-1, 2,4,3,2,-1, 2,3,4,2,-1, 3,4,0,3,-1, 3,0,4,3,-1 ";
781   else if (tm == PYRAM13)
782     return (string) " 0,1,2,3,0,-1, 0,3,2,1,0,-1, 0,1,4,0,-1, 0,4,1,0,-1, 1,2,4,1,-1, 1,4,2,1,-1, 2,4,3,2,-1, 2,3,4,2,-1, 3,4,0,3,-1, 3,0,4,3,-1 ";
783   else if (tm == PENTA6)
784     return (string) " 0,1,2,0,-1, 0,2,1,0,-1, 3,4,5,3,-1, 3,5,4,3,-1, 0,1,4,3,0,-1, 0,3,4,1,0,-1, 1,4,5,2,1,-1, 1,2,5,4,1,-1, 0,3,5,2,0,-1, 0,2,5,3,0,-1 ";
785   else if (tm == PENTA15)
786     return (string) " 0,1,2,0,-1, 0,2,1,0,-1, 3,4,5,3,-1, 3,5,4,3,-1, 0,1,4,3,0,-1, 0,3,4,1,0,-1, 1,4,5,2,1,-1, 1,2,5,4,1,-1, 0,3,5,2,0,-1, 0,2,5,3,0,-1 ";
787   else if (tm == HEXA8)
788     return (string) " 0,1,2,3,0,-1, 0,3,2,1,0,-1, 1,5,6,2,1,-1, 1,2,6,5,1,-1, 5,4,7,6,5,-1, 5,6,7,4,5,-1, 4,0,3,7,4,-1, 4,7,3,0,4,-1, 0,4,5,1,0,-1, 0,1,5,4,0,-1, 3,7,6,2,3,-1, 3,2,6,7,3,-1 ";
789   else if (tm == HEXA20)
790     return (string) " 0,1,2,3,0,-1, 0,3,2,1,0,-1, 1,5,6,2,1,-1, 1,2,6,5,1,-1, 5,4,7,6,5,-1, 5,6,7,4,5,-1, 4,0,3,7,4,-1, 4,7,3,0,4,-1, 0,4,5,1,0,-1, 0,1,5,4,0,-1, 3,7,6,2,3,-1, 3,2,6,7,3,-1 ";
791   else
792     return (string) "";
793 }
794
795 std::string MESHCUT::SIGNE(double x)
796 {
797   if (x < 0)
798     return "-";
799   else if (x > 0)
800     return "+";
801   else
802     return "0";
803 }
804
805 void MESHCUT::champType(std::string type, med_entity_type MEM, med_geometry_type MGE, med_idt fid, med_idt fidout,
806                         char *maa, char *nomChamp, char *nomChampMoy, med_field_type typeChamp, char *compChamp,
807                         char *unitChamp, med_int nCompChamp, std::map<std::string, int> REFGAUSS, int ichamp)
808 {
809
810   bool debug = true;
811   int ipt, nmailles, ngauss, imaille, igauss, icomp;
812   //  int ival, ngpdt;
813   med_int nval, numdt, numo, nPasTemps;
814   char dtunit[MED_SNAME_SIZE + 1] = "";
815   char locname[MED_NAME_SIZE + 1] = "";
816   char nomprofil[MED_NAME_SIZE + 1] = "";
817   med_float dt = 0.0;
818   med_float *valr = NULL;
819   med_float *valr2 = NULL;
820   med_bool local;
821   //  med_int nbrefmaa;
822   med_field_type fieldType;
823
824   if (MEDfieldInfo(fid, ichamp, nomChamp, maa, &local, &fieldType, compChamp, unitChamp, dtunit, &nPasTemps) < 0)
825     ERREUR("Erreur MEDfieldInfo");
826   cout << type << " : " << (int) nPasTemps << " pas de temps  " << endl;
827
828   for (ipt = 1; ipt <= nPasTemps; ipt++)
829     {
830       //for (ipt=1; ipt<=min(nPasTemps,1); ipt++) {
831       if (debug)
832         cout << endl;
833       if (debug)
834         cout << "************************************************************" << endl;
835       if (debug)
836         cout << "                    CHAMP " << ichamp << endl;
837       if (debug)
838         cout << "          " << nomChamp << endl;
839       if (debug)
840         cout << "          " << type << "   ---   Pas de temps " << ipt << endl;
841       if (debug)
842         cout << "************************************************************" << endl;
843       if (debug)
844         cout << endl;
845
846       if (MEDfieldComputingStepInfo(fid, nomChamp, ipt, &numdt, &numo, &dt) < 0)
847         {
848           cout << endl;
849           cout << endl << "####################################################################" << endl;
850           cout << "                   ERREUR MEDpasdetempsInfo                         " << endl;
851           cout << endl << "####################################################################" << endl;
852           cout << "                  Champ: " << (string) nomChamp << endl;
853           cout << "                  Géométrie: " << MGE2string(MGE) << endl;
854           cout << "                  Pas de temps " << ipt << " ignoré" << endl;
855
856           continue;
857         }
858
859       med_int profilesize, nintegrationpoint;
860       nval = MEDfieldnValueWithProfile(fid, nomChamp, numdt, numo, MEM, MGE, ipt, MED_COMPACT_PFLMODE, nomprofil,
861                                        &profilesize, locname, &nintegrationpoint);
862       if (debug)
863         cout << "     Nombre de valeurs dans ce pas de temps : " << (int) nval << endl;
864
865       if (typeChamp == MED_FLOAT64)
866         valr = (med_float*) calloc(nCompChamp * nval, sizeof(med_float));
867       else
868         ERREUR("Type de champ non pris en compte");
869
870       if (MEDfieldValueWithProfileRd(fid, maa, numdt, numo, MEM, MGE, MED_COMPACT_PFLMODE, nomprofil,
871                                      MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, (unsigned char*) valr) < 0)
872         {
873           cout << endl;
874           cout << endl << "####################################################################" << endl;
875           cout << "                         ERREUR MEDchampLire                        " << endl;
876           cout << endl << "####################################################################" << endl;
877           cout << endl;
878           cout << "   Champ: " << (string) nomChamp << endl;
879           cout << "   Géométrie: " << MGE2string(MGE) << endl;
880           cout << "   Pas de temps " << ipt << " ignoré" << endl;
881           cout << endl << endl;
882           continue;
883         }
884
885       if (debug)
886         cout << "       profil  = " << (string) nomprofil << endl;
887       // Localisation du champ aux points de Gauss
888       if (debug)
889         cout << "       locname = " << (string) locname << endl;
890
891       if (REFGAUSS[(string) locname])
892         {
893           ngauss = REFGAUSS[(string) locname];
894           if (debug)
895             cout << "       " << ngauss << " points de Gauss par élément)" << endl;
896         }
897       else
898         ngauss = 1;
899
900       nmailles = nval / ngauss;
901       if (debug)
902         cout << "      Nbre de mailles: " << nmailles << endl;
903
904       if (debug)
905         {
906           cout << endl << "       Liste des valeurs du champ brut aux 3 premiers éléments:" << endl;
907           for (imaille = 0; imaille < min(nmailles, 3); imaille++)
908             {
909               cout << "         Maille " << imaille << endl;
910               for (igauss = 0; igauss < ngauss; igauss++)
911                 {
912                   cout << "             PG " << igauss << " : ";
913                   for (icomp = 0; icomp < nCompChamp; icomp++)
914                     cout << " " << *(valr + imaille * ngauss * nCompChamp + igauss * nCompChamp + icomp);
915                   cout << endl;
916                 }
917               cout << endl;
918             }
919           cout << endl;
920         }
921
922       if (ngauss > 1)
923         {
924
925           valr2 = (med_float*) calloc(nCompChamp * nmailles, sizeof(med_float));
926
927           if (debug)
928             cout << endl << "       Moyenne sur les PG des mailles" << endl;
929           for (imaille = 0; imaille < nmailles; imaille++)
930             {
931               for (icomp = 0; icomp < nCompChamp; icomp++)
932                 {
933                   float valCompMaille = 0.0;
934                   for (igauss = 0; igauss < ngauss; igauss++)
935                     valCompMaille += *(valr + imaille * ngauss * nCompChamp + igauss * nCompChamp + icomp);
936                   *(valr2 + imaille * nCompChamp + icomp) = valCompMaille / ngauss;
937
938                 }
939             }
940
941           //cout << endl << "Nom champ moy = " <<  (string)nomChampMoy << endl;
942           //cout << endl << "Type champ = " <<  typeChamp << endl;
943           //cout << endl << "Comp champ = " <<  (string)compChamp << endl;
944           //cout << endl << "Unit champ = " <<  (string)unitChamp << endl;
945           //cout << endl << "N comp champ = " <<  nCompChamp << endl;
946
947           if (MEDfieldValueWithProfileWr(fidout, nomChampMoy, numdt, numo, dt, MEM, MGE, MED_COMPACT_PFLMODE,
948                                          nomprofil, MED_NO_LOCALIZATION, MED_FULL_INTERLACE, MED_ALL_CONSTITUENT,
949                                          (med_int) nmailles, (unsigned char*) valr2) < 0)
950             {
951               cout << endl;
952               cout << endl << "********************************************************************" << endl;
953               cout << "********************                         ***********************" << endl;
954               cout << "********************   ERREUR MEDchampEcr    ***********************" << endl;
955               cout << "********************                         ***********************" << endl;
956               cout << "********************************************************************" << endl;
957               cout << endl;
958               cout << "   Champ: " << (string) nomChampMoy << endl;
959               cout << "   Géométrie: " << MGE2string(MGE) << endl;
960               cout << "   Pas de temps " << ipt << " ignoré" << endl;
961               cout << endl << endl;
962               continue;
963             }
964
965           if (debug)
966             cout << "    Ecriture des valeurs moyennées dans le nouveau champ: OK " << endl;
967
968           // Restitution du champ moyenné
969           if (debug)
970             {
971               cout << endl << "       Liste des valeurs du champ moyenné aux 3 premiers éléments:" << endl;
972               for (imaille = 0; imaille < min(nmailles, 3); imaille++)
973                 {
974                   cout << "         Maille " << imaille << endl;
975                   for (icomp = 0; icomp < nCompChamp; icomp++)
976                     cout << " " << *(valr2 + imaille * nCompChamp + icomp);
977                   cout << endl;
978                 }
979               cout << endl;
980             }
981
982         }
983
984       free(valr);
985       free(valr2);
986
987     } // boucle sur les pas de temps
988
989   cout << endl;
990 }
991
992 std::string MESHCUT::nomMaille(TYPE_MAILLE tm, int nl)
993 {
994   return (TM2string(tm) + (string) "_" + int2string(nl));
995 }
996
997 bool MESHCUT::appartientVN(int n, std::vector<int> V)
998 {
999   bool app = false;
1000   for (unsigned int i = 0; i < V.size(); i++)
1001     if (n == V[i])
1002       {
1003         app = true;
1004         break;
1005       }
1006   return app;
1007 }
1008
1009 float MESHCUT::distance2(float x1, float y1, float z1, float x2, float y2, float z2)
1010 {
1011   return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1);
1012 }
1013
1014 /*!
1015  *  Conversion HL-MED d'une table de connectivités
1016  */
1017 void MESHCUT::conversionCNX(int *CNXtm, TYPE_MAILLE tm, int N)
1018 {
1019
1020   int n = Nnoeuds(tm);
1021
1022   if (tm == TETRA4)
1023     {
1024       for (int i = 0; i < N; i++)
1025         {
1026           int i1 = CNXtm[i * n + 1];
1027           int i2 = CNXtm[i * n + 2];
1028           CNXtm[i * n + 1] = i2;
1029           CNXtm[i * n + 2] = i1;
1030         }
1031     }
1032   else if (tm == PYRAM5)
1033     {
1034       for (int i = 0; i < N; i++)
1035         {
1036           int i1 = CNXtm[i * n + 1];
1037           int i3 = CNXtm[i * n + 3];
1038           CNXtm[i * n + 1] = i3;
1039           CNXtm[i * n + 3] = i1;
1040         }
1041     }
1042   else if (tm == PENTA6)
1043     {
1044       for (int i = 0; i < N; i++)
1045         {
1046           int i0 = CNXtm[i * n + 0];
1047           int i1 = CNXtm[i * n + 1];
1048           int i2 = CNXtm[i * n + 2];
1049           int i3 = CNXtm[i * n + 3];
1050           int i4 = CNXtm[i * n + 4];
1051           int i5 = CNXtm[i * n + 5];
1052           CNXtm[i * n + 0] = i3;
1053           CNXtm[i * n + 1] = i4;
1054           CNXtm[i * n + 2] = i5;
1055           CNXtm[i * n + 3] = i0;
1056           CNXtm[i * n + 4] = i1;
1057           CNXtm[i * n + 5] = i2;
1058         }
1059     }
1060
1061   else if (tm == HEXA8)
1062     {
1063       for (int i = 0; i < N; i++)
1064         {
1065           int i0 = CNXtm[i * n + 0];
1066           int i1 = CNXtm[i * n + 1];
1067           int i2 = CNXtm[i * n + 2];
1068           int i3 = CNXtm[i * n + 3];
1069           int i4 = CNXtm[i * n + 4];
1070           int i5 = CNXtm[i * n + 5];
1071           int i6 = CNXtm[i * n + 6];
1072           int i7 = CNXtm[i * n + 7];
1073           CNXtm[i * n + 0] = i4;
1074           CNXtm[i * n + 1] = i5;
1075           CNXtm[i * n + 2] = i6;
1076           CNXtm[i * n + 3] = i7;
1077           CNXtm[i * n + 4] = i0;
1078           CNXtm[i * n + 5] = i1;
1079           CNXtm[i * n + 6] = i2;
1080           CNXtm[i * n + 7] = i3;
1081         }
1082     }
1083 }
1084