Salome HOME
0974d2f4773c8feb1c50f928f9821e09624dc540
[modules/hexablock.git] / src / HEXABLOCK / hexa_utils.cxx
1
2 // C++ : Fonctions utilitaires de Hexa
3
4 // Copyright (C) 2009-2023  CEA, EDF
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "hexa_base.hxx"
24
25 #include <cstdlib>
26 #include <cmath>
27 #include <ctime>
28 #include <algorithm>
29 #include <sys/stat.h>
30
31 BEGIN_NAMESPACE_HEXA
32
33
34 // ========================================================= niv_debug
35 int niv_debug ()
36 {
37    static int debug_level = NOTHING;
38    if (debug_level == NOTHING)
39       {
40       cpchar rep = getenv ("HEXA_DB");
41       if (rep!=NULL)
42           debug_level = atoi (rep);
43       debug_level = std::max (debug_level, 0);
44       }
45    return debug_level;
46 }
47 // ========================================================= on_debug
48 bool on_debug ()
49 {
50    return niv_debug () > 0;
51 }
52 // ========================================================= in_test
53 bool in_test ()
54 {
55    static int test_level = NOTHING;
56    if (test_level == NOTHING)
57       {
58       cpchar rep = getenv ("HEXA_TEST");
59       if (rep!=NULL)
60           test_level = atoi (rep);
61       test_level = std::max (test_level, 0);
62       }
63    return test_level > 0;
64
65 }
66 // ======================================================== set_minus
67 void set_minus (std::string& chaine)
68 {
69    int lg = chaine.size();
70    for (int nc=0 ; nc<lg ; nc++)
71        chaine [lg] = tolower (chaine[lg]);
72 }
73 // ======================================================== get_temp_name
74 pchar get_temp_name (cpchar format, pchar nomfic)
75 {
76    int nro = 0;
77    while (true)
78          {
79          nro ++;
80          sprintf (nomfic, format, nro);
81
82          struct stat buffer;
83          int rep = stat (nomfic, &buffer);
84          if (rep<0)
85             return nomfic;
86          }
87 }
88 // ========================================================= prod_scalaire
89 double prod_scalaire (double v1[], double v2[])
90 {
91    double prod = v1[dir_x]*v2[dir_x] + v1[dir_y]*v2[dir_y]
92                                      + v1[dir_z]*v2[dir_z];
93    return prod;
94 }
95 // ========================================================= calc_norme
96 double calc_norme (double vect[])
97 {
98     double norme = vect[dir_x]*vect[dir_x] + vect[dir_y]*vect[dir_y]
99                                            + vect[dir_z]*vect[dir_z];
100     return sqrt (norme);
101 }
102 // ======================================================== calc_distance
103 double calc_distance  (double v1[], double v2[])
104 {
105    Real3 vv = { v2[dir_x]-v1[dir_x], v2[dir_y]-v1[dir_y], v2[dir_z]-v1[dir_z] };
106    return calc_norme (vv);
107 }
108 // ======================================================== calc_d2
109 double calc_d2  (double v1[], double v2[])
110 {
111    double dd = carre (v2[dir_x]-v1[dir_x]) + carre (v2[dir_y]-v1[dir_y]) 
112              + carre (v2[dir_z]-v1[dir_z]);
113    return dd;
114 }
115 // ========================================================= calc_vecteur
116 void calc_vecteur  (double pta[], double ptb[], double vab[])
117 {
118    vab [dir_x] = ptb [dir_x] - pta [dir_x];
119    vab [dir_y] = ptb [dir_y] - pta [dir_y];
120    vab [dir_z] = ptb [dir_z] - pta [dir_z];
121 }
122 // ========================================================= copy_vecteur
123 void copy_vecteur  (double va[], double vb[])
124 {
125    vb [dir_x] = va [dir_x];
126    vb [dir_y] = va [dir_y];
127    vb [dir_z] = va [dir_z];
128 }
129 // ========================================================= calc_milieu
130 void calc_milieu  (double pta[], double ptb[], double milieu[])
131 {
132    milieu [dir_x] = (ptb [dir_x] + pta [dir_x])/2.0;
133    milieu [dir_y] = (ptb [dir_y] + pta [dir_y])/2.0;
134    milieu [dir_z] = (ptb [dir_z] + pta [dir_z])/2.0;
135 }
136 // ========================================================= normer_vecteur
137 int normer_vecteur (double vect[])
138 {
139    double dn = calc_norme (vect);
140    if (dn < 1e-30)
141       return HERR;
142
143    vect [dir_x] /= dn;
144    vect [dir_y] /= dn;
145    vect [dir_z] /= dn;
146    return HOK;
147 }
148 // ========================================================= prod_vectoriel
149 double* prod_vectoriel (double v1[], double v2[], double prod[])
150 {
151    prod [dir_x] = v1[dir_y] * v2[dir_z] - v2[dir_y] * v1[dir_z];
152    prod [dir_y] = v1[dir_z] * v2[dir_x] - v2[dir_z] * v1[dir_x];
153    prod [dir_z] = v1[dir_x] * v2[dir_y] - v2[dir_x] * v1[dir_y];
154
155    return prod;
156 }
157 // ====================================================== carre
158 double carre (double val)
159 {
160    return val*val;
161 }
162 // ====================================================== same_coords
163 bool same_coords (double* pa, double* pb, double epsilon2)
164 {
165    double d2 = carre (pb[dir_x]-pa[dir_x]) + carre (pb[dir_y]-pa[dir_y])
166              + carre (pb[dir_z]-pa[dir_z]);
167    return d2 < epsilon2;
168 }
169 // ========================================================= prod_mixte
170 double prod_mixte (double vi[], double vj[], double vk[])
171 {
172    double pmixte = 0;
173
174    for (int nc=0 ; nc<DIM3 ; nc++)
175        {
176        int nc1 = (nc + 1) MODULO DIM3;
177        int nc2 = (nc + 2) MODULO DIM3;
178        pmixte +=  vk[nc] * (vi [nc1] * vj [nc2] - vj [nc1] * vi [nc2]);
179        }
180
181    return pmixte;
182 }
183 // ========================================================= fatal_error
184 void fatal_error (cpchar format, cpchar info1, cpchar info2)
185 {
186    char     buffer [240];
187    sprintf (buffer, format, info1, info2);
188    printf ("****\n");
189    printf ("**** %s\n", buffer);
190    printf ("****\n");
191 #ifdef NO_CASCADE
192    printf ("                ............ Erreur fatale\n");
193    exit (94);
194 #endif
195    printf ("\n");
196 }
197 static int current_option = NOTHING;
198 // ====================================================== special_option
199 bool special_option ()
200 {
201    if (current_option == NOTHING)
202       {
203       cpchar rep = getenv ("HEXA_OPTION");
204       if (rep!=NULL)
205           current_option = atoi (rep);
206       current_option = std::max (current_option, 0);
207       }
208    return current_option > 0;
209 }
210 // ====================================================== set_special_option
211 void set_special_option (bool opt)
212 {
213    current_option = opt ? 1 : 0;
214 }
215 // ====================================================== sizeof_file
216 int sizeof_file (cpchar filename)
217 {
218    struct stat status;
219
220    int ier = stat  (filename, &status);
221    if (ier == HOK)
222       return status.st_size;
223    else
224       return NOTHING;
225 }
226 // ====================================================== read_file
227 char* read_file (cpchar filename, int& size)
228 {
229    size = 0;
230
231    struct stat status;
232    int ier = stat  (filename, &status);
233    if (ier != HOK)
234       return  NULL;
235
236    FILE* fic = fopen (filename, "r");
237    if (fic == NULL)
238       return  NULL;
239
240    int   lgalloc = status.st_size;
241    char* buffer  = (char*) malloc (lgalloc+1);
242    if (buffer == NULL)
243       return  NULL;
244
245    for (int car = fgetc (fic) ; car!=EOF && size<lgalloc ; car = fgetc(fic))
246        buffer [size++] = car;
247    fclose (fic);
248
249    printf ("read_file : lgalloc=%d, size=%d\n", lgalloc, size);
250    buffer [size++] = EOS;
251    return buffer;
252 }
253 // ====================================================== get_time
254 cpchar get_time (std::string& buffer)
255 {
256    char   quand[24];
257    time_t tps;
258    time (&tps);
259    struct tm *temps = localtime (&tps);
260
261    sprintf (quand, "%d/%02d/%02d %02d:%02d:%02d",
262             1900 + temps->tm_year, temps->tm_mon+1, temps->tm_mday,
263             temps->tm_hour, temps->tm_min, temps->tm_sec);
264
265     buffer = quand;
266     return buffer.c_str();
267 }
268 // ======================================================== requals
269 bool requals (const double v1, const double v2)
270 {
271    static const double Epsilon = 1e-6;
272    return v1 >= v2 - Epsilon && v1 <= v2 + Epsilon;
273 }
274 // ======================================================== requals
275 bool requals (const double* lun, const double* lautre)
276 {
277    return    lun!=NULL && lautre!=NULL     && requals (lun [0], lautre [0])
278           && requals (lun [1], lautre [1]) && requals (lun [2], lautre [2]) ;
279 }
280 // ======================================================== make_basename
281 int make_basename (cpchar filename, std::string& base)
282 {
283    const char slash = '/';
284    const char antis = '\\';
285    int  lg   = strlen (filename);
286    int  ifin = -1;
287    int  ideb = 0;
288    bool more = true;
289
290    for (int nc = lg-1 ; more && nc>=0 ; --nc)
291        {
292        char car = filename[nc];
293        if (car==slash || car==antis)
294           {
295           if (ifin >0) 
296              {
297              ideb = nc + 1;
298              more = false;
299              }
300           }
301        else if (ifin<0)
302           ifin = nc;
303        } 
304
305    if (ifin  <0)
306       {
307       base = "undefined";
308       return HERR;
309       }
310
311    base = "";
312    for (int nc=ideb ; nc <= ifin ; ++nc)
313        base += filename[nc];
314    return HOK;
315 }
316 END_NAMESPACE_HEXA