Delaunator
2D Delaunay Triangulation in C++ with Python wrapper.
 All Classes Namespaces Functions
inlines.h
1 #include "utils.h"
2 
3 
4 
5 
6 /******************************************************************
7  * CONVERTION METHODS
8  ******************************************************************/
12 inline int str2int(std::string str) {
13  std::stringstream ss(str);
14  int n;
15  ss >> n;
16  return n;
17 }
18 
19 
20 
24 inline unsigned int str2uint(std::string str) {
25  std::stringstream ss(str);
26  unsigned int n;
27  ss >> n;
28  return n;
29 }
30 
31 
32 
37 inline std::vector<unsigned int> str2vec_uint(std::string line) {
38 // INITIALIZING
39  std::vector<unsigned int> values;
40  std::string val = "";
41 // TREATMENT
42  for(unsigned int i = 0, count = line.size(); i < count; i++) {
43  if(line[i] == ':') {
44  values.push_back(str2uint(val));
45  val = "";
46  } else val += line[i];
47  }
48 // ENDING
49  return values;
50 }
51 
52 
57 inline std::string vec_uint2str(std::vector<unsigned int> vec) {
58  std::string line = "";
59  for(auto v : vec) line += std::to_string(v) + ":";
60  return line;
61 }
62 
63 
64 
65 
66 
67 /******************************************************************
68  * TIME METHODS
69  ******************************************************************/
74 inline float timeSince(const clock_t t) {
75  return ((float)(clock()-t) / CLOCKS_PER_SEC);
76 }
77 
78 
79 
84 inline void waitFor(const int milliseconds) {
85 #ifdef __GNUC__
86  sleep((float)milliseconds / 1000.);
87 #elif __WIN32__
88  sleep(milliseconds);
89 #endif
90 }
91 
92 
93 
94 
95 
96 /******************************************************************
97  * LOGS MANAGEMENT METHODS
98  ******************************************************************/
103 inline void clogs(const std::string log) {
104  std::ofstream fl(FILE_LOGS, std::ios::app);
105  if(fl != NULL) {
106  fl << log << std::endl;
107  fl.close();
108  } else
109  std::cerr << "ERROR: FILE_LOGS can't opened !" << std::endl;
110 }
111 // modify clogs
118 inline void clogs(const int order) {
119  if(order == 0) { // ERASE ALL THE FILE CONTENT
120  std::ofstream fl(FILE_LOGS);
121  if(fl != NULL) fl.close();
122  else std::cerr << "ERROR: FILE_LOGS can't opened !" << std::endl;
123  }
124 }
125 
126 
127 
128 
129 
130 
131 /******************************************************************
132  * RANDOM METHODS
133  ******************************************************************/
138 inline int randN(const int N) {
139  return (int) (rand() / (double)RAND_MAX * (N));
140 }
141 
142 
143 
148 inline bool aChanceOn(const unsigned int n) {
149  return (randN(n) == 0); // true one time on n
150 }
151 
152 
153 
154 
155 
156 
157 /******************************************************************
158  * TERM METHODS
159  ******************************************************************/
160 
161 
176 enum termcolor { BLACK=0, RED=1, GREEN=2, ORANGE=3, BLUE=4, MAGENTA=5, CYAN=6, WHITE=7 };
177 inline void term_color(const int textc = 7, const int backc = 0) {
178  std::cout << "\033[" << textc+30 << ";" << backc+40 << "m";
179 }
180 
181 
185 inline void term_reset() {
186  std::cout << "\033[0m";
187 }
188 
189 
193 inline void term_clear() {
194  std::cout << "\033[H\033[2J";
195 }
196 
197 
198