2 // C++ : Fonctions utilitaires de Hexa
4 // Copyright (C) 2009-2013 CEA/DEN, EDF R&D
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "hexa_base.hxx"
33 // ========================================================= niv_debug
36 static int debug_level = NOTHING;
37 if (debug_level == NOTHING)
39 cpchar rep = getenv ("HEXA_DB");
41 debug_level = atoi (rep);
42 debug_level = std::max (debug_level, 0);
46 // ========================================================= on_debug
49 return niv_debug () > 0;
51 // ========================================================= in_test
54 static int test_level = NOTHING;
55 if (test_level == NOTHING)
57 cpchar rep = getenv ("HEXA_TEST");
59 test_level = atoi (rep);
60 test_level = std::max (test_level, 0);
62 return test_level > 0;
65 // ======================================================== set_minus
66 void set_minus (string& chaine)
68 int lg = chaine.size();
69 for (int nc=0 ; nc<lg ; nc++)
70 chaine [lg] = tolower (chaine[lg]);
72 // ======================================================== get_temp_name
73 pchar get_temp_name (cpchar format, pchar nomfic)
79 sprintf (nomfic, format, nro);
82 int rep = stat (nomfic, &buffer);
87 // ========================================================= prod_scalaire
88 double prod_scalaire (double v1[], double v2[])
90 double prod = v1[dir_x]*v2[dir_x] + v1[dir_y]*v2[dir_y]
91 + v1[dir_z]*v2[dir_z];
94 // ========================================================= calc_norme
95 double calc_norme (double vect[])
97 double norme = vect[dir_x]*vect[dir_x] + vect[dir_y]*vect[dir_y]
98 + vect[dir_z]*vect[dir_z];
101 // ======================================================== calc_distance
102 double calc_distance (double v1[], double v2[])
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);
107 // ========================================================= calc_vecteur
108 void calc_vecteur (double pta[], double ptb[], double vab[])
110 vab [dir_x] = ptb [dir_x] - pta [dir_x];
111 vab [dir_y] = ptb [dir_y] - pta [dir_y];
112 vab [dir_z] = ptb [dir_z] - pta [dir_z];
114 // ========================================================= calc_milieu
115 void calc_milieu (double pta[], double ptb[], double milieu[])
117 milieu [dir_x] = (ptb [dir_x] + pta [dir_x])/2.0;
118 milieu [dir_y] = (ptb [dir_y] + pta [dir_y])/2.0;
119 milieu [dir_z] = (ptb [dir_z] + pta [dir_z])/2.0;
121 // ========================================================= normer_vecteur
122 int normer_vecteur (double vect[])
124 double dn = calc_norme (vect);
133 // ========================================================= prod_vectoriel
134 double* prod_vectoriel (double v1[], double v2[], double prod[])
136 prod [dir_x] = v1[dir_y] * v2[dir_z] - v2[dir_y] * v1[dir_z];
137 prod [dir_y] = v1[dir_z] * v2[dir_x] - v2[dir_z] * v1[dir_x];
138 prod [dir_z] = v1[dir_x] * v2[dir_y] - v2[dir_x] * v1[dir_y];
142 // ====================================================== carre
143 double carre (double val)
147 // ====================================================== same_coords
148 bool same_coords (double* pa, double* pb, double epsilon2)
151 double d2 = carre (pb[dir_x]-pa[dir_x]) + carre (pb[dir_y]-pa[dir_y])
152 + carre (pb[dir_z]-pa[dir_z]);
153 return d2 < epsilon2;
155 // ========================================================= prod_mixte
156 double prod_mixte (double vi[], double vj[], double vk[])
160 for (int nc=0 ; nc<DIM3 ; nc++)
162 int nc1 = (nc + 1) MODULO DIM3;
163 int nc2 = (nc + 2) MODULO DIM3;
164 pmixte += vk[nc] * (vi [nc1] * vj [nc2] - vj [nc1] * vi [nc2]);
169 // ========================================================= fatal_error
170 void fatal_error (cpchar format, cpchar info1, cpchar info2)
173 sprintf (buffer, format, info1, info2);
175 printf ("**** %s\n", buffer);
178 printf (" ............ Erreur fatale\n");
183 static int current_option = NOTHING;
184 // ====================================================== special_option
185 bool special_option ()
187 if (current_option == NOTHING)
189 cpchar rep = getenv ("HEXA_OPTION");
191 current_option = atoi (rep);
192 current_option = std::max (current_option, 0);
194 return current_option > 0;
196 // ====================================================== set_special_option
197 void set_special_option (bool opt)
199 current_option = opt ? 1 : 0;
201 // ====================================================== sizeof_file
202 int sizeof_file (cpchar filename)
206 int ier = stat (filename, &status);
208 return status.st_size;
212 // ====================================================== read_file
213 char* read_file (cpchar filename, int& size)
218 int ier = stat (filename, &status);
222 FILE* fic = fopen (filename, "r");
226 int lgalloc = status.st_size;
227 char* buffer = (char*) malloc (lgalloc+1);
231 for (int car = fgetc (fic) ; car!=EOF && size<lgalloc ; car = fgetc(fic))
232 buffer [size++] = car;
235 printf ("read_file : lgalloc=%d, size=%d\n", lgalloc, size);
236 buffer [size++] = EOS;
239 // ====================================================== get_time
240 cpchar get_time (string& buffer)
245 struct tm *temps = localtime (&tps);
247 sprintf (quand, "%d/%02d/%02d %02d:%02d:%02d",
248 1900 + temps->tm_year, temps->tm_mon+1, temps->tm_mday,
249 temps->tm_hour, temps->tm_min, temps->tm_sec);
252 return buffer.c_str();