muMECH  1.0
macros.h
Go to the documentation of this file.
1 //********************************************************************************************************
2 // code: ### ### ##### #### ## ##
3 // ## ## ######## ## ## ## ## ##
4 // ## ## ## ## ## ### ## ######
5 // ## ## ## ## ## ## ## ## ##
6 // ##### ## ## ##### #### ## ##
7 // ##
8 //
9 // name: macros.h
10 // author(s): Jan Novak, Jan Zeman
11 // language: C, C++
12 // license: This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU Lesser General Public License as published by
14 // the Free Software Foundation; either version 2 of the License, or
15 // (at your option) any later version.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 //********************************************************************************************************
31 #ifndef MUMECH_SHARE_H
32 #define MUMECH_SHARE_H
33 
34 // There is conflict name XMLDocument in msxml and tinyxml.
35 // Solution 1:
36 // This define have to be before first win32 include.
37 #define WIN32_LEAN_AND_MEAN
38 
39 
40 /* Default header files */
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <iostream> // Standard header defining std::cout
44 #include <float.h> //real number limits
45 #include <limits.h> //integer number limits
46 #include "gelib.h"
47 
48 using namespace gelibspace;
49 
50 namespace mumech {
51 
53 #define _NOCASE
54 
55 #ifdef _NOCASE
56 #define CASE false
57 #define _STRCMP _STRCASECMP
58 #define _STRNCMP strncmp
59 #else
60 #define CASE true
61 #define _STRCMP strcmp
62 #define _STRNCMP strncmp
63 #endif
64 
65 
66 
67 /* Symbolic constants */
68 #define MACHINE_EPS 1E-16 //alternatively FLT_EPSILON = 1e-5
69 #define MACHINE_EPS_PRN 1E-8 //alternatively FLT_EPSILON = 1e-5
70 
71 #define PI 3.14159265358979323846
72 
73 #define SQRT_2 1.41421356237309504880168872
74 #define _1_BY_SQRT_2 0.70710678118654752440084436
75 
76 /* File manipulating macros */
77 #define FileError(fmode,name) fprintf(stderr,"\033[31mCannot %s file %s in file %s line %d\033[0m\n", fmode, name, __FILE__,__LINE__); exit( 1 );
78 #define FOPEN(file, name, mode) if( ( file = fopen(name,mode) ) == NULL) {FileError("open", name)}
79 #define FCLOSE(file) if (( fclose(file) ) == EOF) {FileError("close","")}
80 
81 #define FREAD( var, number, file ) fread ( &var, sizeof ( var ), number, file )
82 #define FWRITE( var, number, file ) fwrite ( &var, sizeof ( var ), number, file )
83 
84 /* Error functions */
85 //#define Error(string,value) fprintf( stderr, "\033[31mError : " );fprintf(stderr,string,value); fprintf(stderr,"in file %s line %d.\033[0m\n", __FILE__, __LINE__ );exit(FALSE)
86 #define NotImplementedFunction( name ) _errorr2( "Function %s : not implemented yet\n", name );
87 #define Dprintf( format, var ) printf (" %s = ", #var ); printf ( format, var )
88 #define Dfprintf( file, format, var ) fprintf (file," %s = ", #var ); fprintf ( file, format, var )
89 //#define Warning(string,value) fprintf( stderr, "\033[31mWarning : " ); fprintf(stderr,string,value); fprintf(stderr,"in file %s line %d.\033[0m\n", __FILE__, __LINE__ )
90 #define NfoPrint(string,value) fprintf( stderr, "\033[31mNotice : \033[0m" ); fprintf(stderr,string,value);
91 
92 /* Other macros */
93 #ifndef SWAP
94 #define SWAP(a, b, dummy) dummy=a;a=b;b=dummy
95 #endif
96 
97 #define SQR( a ) ( ( a ) * ( a ) )
98 #define CUB( a ) ( ( a ) * ( a ) * ( a ) )
99 
100 #define MIN(a,b) ( ( ( a ) < ( b ) ) ? ( a ) : ( b ) )
101 #define MAX(a,b) ( ( ( a ) > ( b ) ) ? ( a ) : ( b ) )
102 
103 #define MID(a,b,c) MIN( MIN(MAX(a,b), MAX(b,c)), MIN(MAX(b,c), MAX(c,a)) )
104 
105 #ifndef CDMAX //collation of complex numbers' distances, requires <cmath> to be included
106 #define CDMAX(a,b) (((SQR(real(a))+SQR(imag(a)))>(SQR(real(b))+SQR(imag(b)))) ? (a) : (b))
107 #endif
108 
109 #ifndef CReMAX //collation of complex numbers' real parts, requires <cmath> to be included
110 #define CReMAX(a,b) ( ( real( a ) > real( b ) ) ? ( a ) : ( b ) )
111 #endif
112 
113 #ifndef CImMAX //collation of complex numbers' imaginary parts, requires <cmath> to be included
114 #define CImMAX(a,b) ( ( imag( a ) > imag( b ) ) ? ( a ) : ( b ) )
115 #endif
116 
117 #ifndef ABS
118 #define ABS( a ) ( ( ( a ) > 0 ) ? ( a ) : ( -1 * ( a ) ) )
119 #endif
120 
121 //#ifndef sgn
122 //#define sgn( a ) ( ( ( a ) < 0 ) ? -1 : 1 )
123 //#endif
124 
125 #ifndef odd_or_even
126 #define odd_or_even( a ) ( ( ( ( a ) % 2 ) > 0 ) ? ODD : EVEN )
127 #endif
128 
129 /* angle unit transformation*/
130 
131 #ifndef DEG2RAD
132 #define DEG2RAD( a ) ( ( a ) * PI / 180. )
133 #endif
134 
135 #ifndef RAD2DEG
136 #define RAD2DEG( a ) ( ( a ) * 180. / PI )
137 #endif
138 
139 /* infinity number constant */
140 
141 #ifndef INFTY
142 //#define INFTY FLT_MAX //alternatively DBL_MAX for douple precision
143 #define INFTY 1e+36 //alternatively DBL_MAX for douple precision
144 #endif
145 
146 
147 /* memory alocation macros */
148 /*-------------------------*/
149 //#define EXIT_SEQUENCE printf("\033[31mExiting\033[0m\n" ); printf( "\033[31mUnexpected exit!\033[0m\n" ); exit( 1 );
150 //extern void exit_throw( void );
151 
152 //#define AllocError(var) printf("\033[31mCannot allocate %s in file %s line %d, stack's capacity exceeded\033[0m\n", #var, __FILE__, __LINE__ ); EXIT_SEQUENCE;
153 //#define NotNULLError(var) printf("\033[31mYou tried to allocate not NULL variable %s in file %s line %d\033[0m\n",#var,__FILE__,__LINE__);
154 
155 /* Memory manipulating macros */
156 //#define MALLOC(var,number,type) if (var != NULL){NotNULLError(var)} else if( (var = (type*) calloc( (number), sizeof(type) ) ) == NULL ){AllocError(var)}
157 //#define FREE(var) if((var) != NULL) {free((void *) var); var = NULL;}
158 //#define REALLOC( var, number, type ) if ( ( var = ( type * ) realloc( var, (number) * sizeof( type ) ) ) == NULL ){AllocError(var)}
159 //#define MALLOC_IF_NULL(var,number,type) if (var == NULL) if( (var = (type*) calloc( (number), sizeof(type) ) ) == NULL ){AllocError(var)}
160 
161 /* "Safe" allocations */
162 //#define MALLOC_IFNOT_EMPTY(var, number, type) if ( (number) != 0 ) { MALLOC( var, number, type ); }
163 //#define FREE_IFNOT_EMPTY(var, number) if ( (number) != 0 ) { FREE ( var ); number = 0;}
164 
165 /* "Vector" memory allocation */
166 //#define _CreateVector(type,var,number) if (var != NULL) {NotNULLError(var)} else if ( (var = (type *) calloc((number),sizeof(type))) == NULL ){AllocError(var)}
167 //#define _DeleteVector(var) if ((var) != NULL) {free( (void*) var); var=NULL;}
168 
169 /* "Matrix" memory allocation */
170 //#define _CreateArrayStatic(type,name,rows,columns) {int _ii_; for(_ii_=0; _ii_<rows;_ii_++){ _CreateVector(type,name[_ii_],columns)}}
171 //#define _DeleteArrayStatic(name,rows) {int _ii_; for( _ii_=0; _ii_<rows;_ii_++) _DeleteVector(name[_ii_]);}
172 
173 /* "Matrix" memory allocation - pure dynamic */
174 //#define _CreateArray(type,name,rows,columns) {if (name) NotNULLError(name) else {MALLOC(name,rows,type*); for (int _ii_=0; _ii_<rows;_ii_++) {name[_ii_]=NULL;_CreateVector(type,name[_ii_],columns)}}}
175 //#define _DeleteArray(name,rows) if ( name != NULL ) {for(int _ii_=0; _ii_<rows;_ii_++) _DeleteVector(name[_ii_]); _DeleteVector( name );}
176 
177 /* "pointer" alocation */
178 //#define _CreatePointer(type,var) _CreateVector(type,var,1)
179 
180 /* delete pointer - the same as _DeleteVector */
181 //#define _DeletePointer(var) _DeleteVector(var)
182 
183 
184 
185 /* kronecker delta */
186 #ifndef delta
187 #define delta( i, j ) ( ( i == j ) ? 1. : 0. )
188 #endif
189 
190 /* continuous reading from string (as from the file) */
191 #ifndef jump_empties
192 #define jump_empties( p_str ) while( ( * p_str ) <= ' ' && ( * p_str ) != '\0' ){ p_str++; }
193 #endif
194 
195 #ifndef jump_word
196 #define jump_word( p_str ) while( ( * p_str ) > ' ' && ( * p_str ) != '\0' ){ p_str++; }
197 #endif
198 
199 #ifndef sscanff
200 #define sscanff( p_str, format, p_var ) jump_empties( p_str ); sscanf( p_str, format, p_var ); jump_word( p_str )
201 #endif
202 
203 //vector length
204 #define VEC_SQR_NORM_2D( x, y ) ( SQR( x ) + SQR( y ) )
205 
206 #define VEC_SQR_NORM_3D( x, y, z ) ( SQR( x ) + SQR( y ) + SQR( z ) )
207 #define VEC_NORM( x, y, z ) sqrt( VEC_SQR_NORM_3D( x, y, z ) )
208 
209 #define DIST_POINTS_SQR_2D( P1, P2 ) ( SQR( P1[0] - P2[0] ) + SQR( P1[1] - P2[1] ) )
210 #define DIST_POINTS_SQR_3D( P1, P2 ) ( SQR( P1[0] - P2[0] ) + SQR( P1[1] - P2[1] ) + SQR( P1[2] - P2[2] ) )
211 
212 
213 } // end of namespace mumech
214 
215 #endif
216 
217 /*end of file*/
General functions.