Salome HOME
Merge branch V7_3_1_BR
[modules/hexablock.git] / src / HEXABLOCK / hexa_utils.cxx
1
2 // C++ : Fonctions utilitaires de Hexa
3
4 // Copyright (C) 2009-2014  CEA/DEN, EDF R&D
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 <sys/stat.h>
29
30 BEGIN_NAMESPACE_HEXA
31
32
33 // ========================================================= niv_debug
34 int niv_debug ()
35 {
36    static int debug_level = NOTHING;
37    if (debug_level == NOTHING)
38       {
39       cpchar rep = getenv ("HEXA_DB");
40       if (rep!=NULL)
41           debug_level = atoi (rep);
42       debug_level = std::max (debug_level, 0);
43       }
44    return debug_level;
45 }
46 // ========================================================= on_debug
47 bool on_debug ()
48 {
49    return niv_debug () > 0;
50 }
51 // ========================================================= in_test
52 bool in_test ()
53 {
54    static int test_level = NOTHING;
55    if (test_level == NOTHING)
56       {
57       cpchar rep = getenv ("HEXA_TEST");
58       if (rep!=NULL)
59           test_level = atoi (rep);
60       test_level = std::max (test_level, 0);
61       }
62    return test_level > 0;
63
64 }
65 // ======================================================== set_minus
66 void set_minus (string& chaine)
67 {
68    int lg = chaine.size();
69    for (int nc=0 ; nc<lg ; nc++)
70        chaine [lg] = tolower (chaine[lg]);
71 }
72 // ======================================================== get_temp_name
73 pchar get_temp_name (cpchar format, pchar nomfic)
74 {
75    int nro = 0;
76    while (true)
77          {
78          nro ++;
79          sprintf (nomfic, format, nro);
80
81          struct stat buffer;
82          int rep = stat (nomfic, &buffer);
83          if (rep<0)
84             return nomfic;
85          }
86 }
87 // ========================================================= prod_scalaire
88 double prod_scalaire (double v1[], double v2[])
89 {
90    double prod = v1[dir_x]*v2[dir_x] + v1[dir_y]*v2[dir_y]
91                                      + v1[dir_z]*v2[dir_z];
92    return prod;
93 }
94 // ========================================================= calc_norme
95 double calc_norme (double vect[])
96 {
97     double norme = vect[dir_x]*vect[dir_x] + vect[dir_y]*vect[dir_y]
98                                            + vect[dir_z]*vect[dir_z];
99     return sqrt (norme);
100 }
101 // ======================================================== calc_distance
102 double calc_distance  (double v1[], double v2[])
103 {
104    Real3 vv = { v2[dir_x]-v1[dir_x], v2[dir_y]-v1[dir_y], v2[dir_z]-v1[dir_z] };
105    return calc_norme (vv);
106 }
107 // ======================================================== calc_d2
108 double calc_d2  (double v1[], double v2[])
109 {
110    double dd = carre (v2[dir_x]-v1[dir_x]) + carre (v2[dir_y]-v1[dir_y]) 
111              + carre (v2[dir_z]-v1[dir_z]);
112    return dd;
113 }
114 // ========================================================= calc_vecteur
115 void calc_vecteur  (double pta[], double ptb[], double vab[])
116 {
117    vab [dir_x] = ptb [dir_x] - pta [dir_x];
118    vab [dir_y] = ptb [dir_y] - pta [dir_y];
119    vab [dir_z] = ptb [dir_z] - pta [dir_z];
120 }
121 // ========================================================= copy_vecteur
122 void copy_vecteur  (double va[], double vb[])
123 {
124    vb [dir_x] = va [dir_x];
125    vb [dir_y] = va [dir_y];
126    vb [dir_z] = va [dir_z];
127 }
128 // ========================================================= calc_milieu
129 void calc_milieu  (double pta[], double ptb[], double milieu[])
130 {
131    milieu [dir_x] = (ptb [dir_x] + pta [dir_x])/2.0;
132    milieu [dir_y] = (ptb [dir_y] + pta [dir_y])/2.0;
133    milieu [dir_z] = (ptb [dir_z] + pta [dir_z])/2.0;
134 }
135 // ========================================================= normer_vecteur
136 int normer_vecteur (double vect[])
137 {
138    double dn = calc_norme (vect);
139    if (dn < 1e-30)
140       return HERR;
141
142    vect [dir_x] /= dn;
143    vect [dir_y] /= dn;
144    vect [dir_z] /= dn;
145    return HOK;
146 }
147 // ========================================================= prod_vectoriel
148 double* prod_vectoriel (double v1[], double v2[], double prod[])
149 {
150    prod [dir_x] = v1[dir_y] * v2[dir_z] - v2[dir_y] * v1[dir_z];
151    prod [dir_y] = v1[dir_z] * v2[dir_x] - v2[dir_z] * v1[dir_x];
152    prod [dir_z] = v1[dir_x] * v2[dir_y] - v2[dir_x] * v1[dir_y];
153
154    return prod;
155 }
156 // ====================================================== carre
157 double carre (double val)
158 {
159    return val*val;
160 }
161 // ====================================================== same_coords
162 bool same_coords (double* pa, double* pb, double epsilon2)
163 {
164    double d2 = carre (pb[dir_x]-pa[dir_x]) + carre (pb[dir_y]-pa[dir_y])
165              + carre (pb[dir_z]-pa[dir_z]);
166    return d2 < epsilon2;
167 }
168 // ========================================================= prod_mixte
169 double prod_mixte (double vi[], double vj[], double vk[])
170 {
171    double pmixte = 0;
172
173    for (int nc=0 ; nc<DIM3 ; nc++)
174        {
175        int nc1 = (nc + 1) MODULO DIM3;
176        int nc2 = (nc + 2) MODULO DIM3;
177        pmixte +=  vk[nc] * (vi [nc1] * vj [nc2] - vj [nc1] * vi [nc2]);
178        }
179
180    return pmixte;
181 }
182 // ========================================================= fatal_error
183 void fatal_error (cpchar format, cpchar info1, cpchar info2)
184 {
185    char     buffer [240];
186    sprintf (buffer, format, info1, info2);
187    printf ("****\n");
188    printf ("**** %s\n", buffer);
189    printf ("****\n");
190 #ifdef NO_CASCADE
191    printf ("                ............ Erreur fatale\n");
192    exit (94);
193 #endif
194    printf ("\n");
195 }
196 static int current_option = NOTHING;
197 // ====================================================== special_option
198 bool special_option ()
199 {
200    if (current_option == NOTHING)
201       {
202       cpchar rep = getenv ("HEXA_OPTION");
203       if (rep!=NULL)
204           current_option = atoi (rep);
205       current_option = std::max (current_option, 0);
206       }
207    return current_option > 0;
208 }
209 // ====================================================== set_special_option
210 void set_special_option (bool opt)
211 {
212    current_option = opt ? 1 : 0;
213 }
214 // ====================================================== sizeof_file
215 int sizeof_file (cpchar filename)
216 {
217    struct stat status;
218
219    int ier = stat  (filename, &status);
220    if (ier == HOK)
221       return status.st_size;
222    else
223       return NOTHING;
224 }
225 // ====================================================== read_file
226 char* read_file (cpchar filename, int& size)
227 {
228    size = 0;
229
230    struct stat status;
231    int ier = stat  (filename, &status);
232    if (ier != HOK)
233       return  NULL;
234
235    FILE* fic = fopen (filename, "r");
236    if (fic == NULL)
237       return  NULL;
238
239    int   lgalloc = status.st_size;
240    char* buffer  = (char*) malloc (lgalloc+1);
241    if (buffer == NULL)
242       return  NULL;
243
244    for (int car = fgetc (fic) ; car!=EOF && size<lgalloc ; car = fgetc(fic))
245        buffer [size++] = car;
246    fclose (fic);
247
248    printf ("read_file : lgalloc=%d, size=%d\n", lgalloc, size);
249    buffer [size++] = EOS;
250    return buffer;
251 }
252 // ====================================================== get_time
253 cpchar get_time (string& buffer)
254 {
255    char   quand[24];
256    time_t tps;
257    time (&tps);
258    struct tm *temps = localtime (&tps);
259
260    sprintf (quand, "%d/%02d/%02d %02d:%02d:%02d",
261             1900 + temps->tm_year, temps->tm_mon+1, temps->tm_mday,
262             temps->tm_hour, temps->tm_min, temps->tm_sec);
263
264     buffer = quand;
265     return buffer.c_str();
266 }
267 // ======================================================== requals
268 bool requals (const double v1, const double v2)
269 {
270    static const double Epsilon = 1e-6;
271    return v1 >= v2 - Epsilon && v1 <= v2 + Epsilon;
272 }
273 // ======================================================== requals
274 bool requals (const double* lun, const double* lautre)
275 {
276    return    lun!=NULL && lautre!=NULL     && requals (lun [0], lautre [0])
277           && requals (lun [1], lautre [1]) && requals (lun [2], lautre [2]) ;
278 }
279 // ======================================================== make_basename
280 int make_basename (cpchar filename, string& base)
281 {
282    const char slash = '/';
283    const char antis = '\\';
284    int  lg   = strlen (filename);
285    int  ifin = -1;
286    int  ideb = 0;
287    bool more = true;
288
289    for (int nc = lg-1 ; more && nc>=0 ; --nc)
290        {
291        char car = filename[nc];
292        if (car==slash || car==antis)
293           {
294           if (ifin >0) 
295              {
296              ideb = nc + 1;
297              more = false;
298              }
299           }
300        else if (ifin<0)
301           ifin = nc;
302        } 
303
304    if (ifin  <0)
305       {
306       base = "undefined";
307       return HERR;
308       }
309
310    base = "";
311    for (int nc=ideb ; nc <= ifin ; ++nc)
312        base += filename[nc];
313    return HOK;
314 }
315 END_NAMESPACE_HEXA