OpenMEEG
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 <sstream>
50 #include <algorithm>
51 #include <cctype>
52 #ifdef USE_OMP
53 #include <omp.h>
54 #endif
55 
56 #include "OpenMEEGConfigure.h"
57 
58 #ifdef USE_PROGRESSBAR
59  #define PROGRESSBAR(a,b) progressbar((a),(b))
60 #else
61  #define PROGRESSBAR(a,b)
62 #endif
63 
64 namespace OpenMEEG {
65 
66  #ifndef M_PI
67  #define M_PI 3.14159265358979323846
68  #endif
69 
70  #define MU0 (4*M_PI*1e-7)
71 
72  inline std::string getNameExtension ( const std::string& name )
73  {
74  std::string::size_type idx = name.find('.');
75  if (idx == std::string::npos) {
76  return std::string("");
77  } else if (name.substr(idx+1).find('.') != std::string::npos) {
78  return getNameExtension( name.substr(idx+1) );
79  } else {
80  return name.substr(idx+1);
81  }
82  };
83 
84  inline void init_random(int seed) {
85  static bool first=true;
86  if (seed==-1 && !first)
87  return;
88  first=false;
89  // srand((unsigned int)((seed==-1)?time(0):seed));
90  srand(0);
91  rand(); // the first is biased!
92  }
93 
94  inline double drandom()
95  {
96  init_random(-1);
97  return double(rand())/RAND_MAX;
98  }
99 
100  inline double gaussian()
101  {
102  double x;
103  do
104  x=drandom();
105  while (x==0);
106  return (double)(sqrt(-2*log(x))*cos(2*M_PI*drandom()));
107  }
108 
109  inline void disp_argv(int argc, char **argv) {
110  std::cout << std::endl << "| ------ " << argv[0] << std::endl;
111  for( int i = 1; i < argc; i += 1 )
112  {
113  std::cout << "| " << argv[i] << std::endl;
114  }
115  std::cout << "| -----------------------" << std::endl;
116  }
117 
118 #ifdef USE_PROGRESSBAR
119  inline void progressbar(unsigned n, unsigned N, unsigned w = 20) {
120  // w : nb of steps
121  const char* cprog = ".";
122  const char* cprog1 = "*";
123  const char* cbeg = "[";
124  const char* cend = "]";
125  unsigned p = (unsigned)std::min( (unsigned)floor(1.f*n*(w+1)/N), w);
126 
127  static unsigned pprev = -1;
128  if (N>1) {
129  if (n == 0) {
130  pprev = -1;
131  }
132 
133  if (p != pprev) {
134  if (n>1) {
135  // clear previous string
136  for(unsigned i = 0; i < (w+2); ++i)
137  std::cout<< "\b";
138 
139  std::cout<< cbeg;
140  for(unsigned i = 0; i < p; ++i) {
141  std::cout<< cprog1;
142  }
143  for(unsigned i = p; i < w; ++i) {
144  std::cout<< cprog;
145  }
146  std::cout<< cend;
147  }
148  }
149  pprev = p;
150  if (n >= (N-1)) {
151  std::cout<<"\n";
152  }
153  std::cout.flush();
154  }
155  }
156 #endif
157 
158  inline void warning(std::string message) {
159  std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
160  std::cout << "!!!!!!!!!!! WARNING !!!!!!!!!!!" << std::endl;
161  std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
162  std::cout << message << std::endl;
163  }
164 
165  inline void print_version(const char* cmd) {
166  #ifdef USE_OMP
167  std::string omp_support = " using OpenMP\n Executing using " + std::to_string(omp_get_max_threads()) + " threads.";
168  #else
169  std::string omp_support = "";
170  #endif
171 
172  std::ostringstream display_info;
173  display_info << cmd;
174  display_info << " version " << version;
175  display_info << " compiled at " << __DATE__ << " " << __TIME__;
176  display_info << omp_support;
177  std::cout << display_info.str() << std::endl << std::endl;
178  }
179 }
OpenMEEG::print_version
void print_version(const char *cmd)
Definition: om_utils.h:165
OpenMEEG::init_random
void init_random(int seed)
Definition: om_utils.h:84
OpenMEEG::gaussian
double gaussian()
Definition: om_utils.h:100
M_PI
#define M_PI
Definition: om_utils.h:67
OpenMEEG::getNameExtension
std::string getNameExtension(const std::string &name)
Definition: om_utils.h:72
OpenMEEG
Definition: analytics.h:45
OpenMEEG::drandom
double drandom()
Definition: om_utils.h:94
OpenMEEG::warning
void warning(std::string message)
Definition: om_utils.h:158
OpenMEEG::disp_argv
void disp_argv(int argc, char **argv)
Definition: om_utils.h:109