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