OpenMEEG
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
om_utils.h
Go to the documentation of this file.
1 /*
2 Project Name : OpenMEEG
3 
4 © INRIA and ENPC (contributors: Geoffray ADDE, Maureen CLERC, Alexandre
5 GRAMFORT, Renaud KERIVEN, Jan KYBIC, Perrine LANDREAU, Théodore PAPADOPOULO,
6 Emmanuel OLIVI
7 Maureen.Clerc.AT.inria.fr, keriven.AT.certis.enpc.fr,
8 kybic.AT.fel.cvut.cz, papadop.AT.inria.fr)
9 
10 The OpenMEEG software is a C++ package for solving the forward/inverse
11 problems of electroencephalography and magnetoencephalography.
12 
13 This software is governed by the CeCILL-B license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL-B
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's authors, the holders of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL-B license and that you accept its terms.
38 */
39 
40 #pragma once
41 
42 #if WIN32
43 #define _USE_MATH_DEFINES
44 #endif
45 
46 #include <string>
47 #include <cmath>
48 #include <iostream>
49 #include <algorithm>
50 #include <cctype>
51 
52 #include "OpenMEEGConfigure.h"
53 
54 #ifdef USE_PROGRESSBAR
55  #define PROGRESSBAR(a,b) progressbar((a),(b))
56 #else
57  #define PROGRESSBAR(a,b)
58 #endif
59 
60 namespace OpenMEEG {
61 
62  #ifndef M_PI
63  #define M_PI 3.14159265358979323846
64  #endif
65 
66  #define MU0 (4*M_PI*1e-7)
67 
68  inline std::string getNameExtension ( const std::string& name )
69  {
70  std::string::size_type idx = name.find('.');
71  if (idx == std::string::npos) {
72  return std::string("");
73  } else if (name.substr(idx+1).find('.') != std::string::npos) {
74  return getNameExtension( name.substr(idx+1) );
75  } else {
76  return name.substr(idx+1);
77  }
78  };
79 
80  inline void init_random(int seed) {
81  static bool first=true;
82  if (seed==-1 && !first)
83  return;
84  first=false;
85  // srand((unsigned int)((seed==-1)?time(0):seed));
86  srand(0);
87  rand(); // the first is biased!
88  }
89 
90  inline double drandom()
91  {
92  init_random(-1);
93  return double(rand())/RAND_MAX;
94  }
95 
96  inline double gaussian()
97  {
98  double x;
99  do
100  x=drandom();
101  while (x==0);
102  return (double)(sqrt(-2*log(x))*cos(2*M_PI*drandom()));
103  }
104 
105  inline void disp_argv(int argc, char **argv) {
106  std::cout << std::endl << "| ------ " << argv[0] << std::endl;
107  for( int i = 1; i < argc; i += 1 )
108  {
109  std::cout << "| " << argv[i] << std::endl;
110  }
111  std::cout << "| -----------------------" << std::endl;
112  }
113 
114 #ifdef USE_PROGRESSBAR
115  inline void progressbar(unsigned n, unsigned N, unsigned w = 20) {
116  // w : nb of steps
117  const char* cprog = ".";
118  const char* cprog1 = "*";
119  const char* cbeg = "[";
120  const char* cend = "]";
121  unsigned p = (unsigned)std::min( (unsigned)floor(1.f*n*(w+1)/N), w);
122 
123  static unsigned pprev = -1;
124  if (N>1) {
125  if (n == 0) {
126  pprev = -1;
127  }
128 
129  if (p != pprev) {
130  if (n>1) {
131  // clear previous string
132  for(unsigned i = 0; i < (w+2); ++i)
133  std::cout<< "\b";
134 
135  std::cout<< cbeg;
136  for(unsigned i = 0; i < p; ++i) {
137  std::cout<< cprog1;
138  }
139  for(unsigned i = p; i < w; ++i) {
140  std::cout<< cprog;
141  }
142  std::cout<< cend;
143  }
144  }
145  pprev = p;
146  if (n >= (N-1)) {
147  std::cout<<"\n";
148  }
149  std::cout.flush();
150  }
151  }
152 #endif
153 
154  inline void warning(std::string message) {
155  std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
156  std::cout << "!!!!!!!!!!! WARNING !!!!!!!!!!!" << std::endl;
157  std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
158  std::cout << message << std::endl;
159  }
160 
161  inline void print_version(const char* cmd) {
162  std::cout << cmd << " version " << version << " compiled at " << __DATE__ << " " << __TIME__ << std::endl << std::endl;
163  }
164 }
void disp_argv(int argc, char **argv)
Definition: om_utils.h:105
void init_random(int seed)
Definition: om_utils.h:80
#define M_PI
Definition: om_utils.h:63
double gaussian()
Definition: om_utils.h:96
void warning(std::string message)
Definition: om_utils.h:154
std::string getNameExtension(const std::string &name)
Definition: om_utils.h:68
void print_version(const char *cmd)
Definition: om_utils.h:161
double drandom()
Definition: om_utils.h:90