-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathUtilities.h
170 lines (134 loc) · 4.38 KB
/
Utilities.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
////////////////////////////////////////////////////////////////////////////////////
// Copyright © Charalambos "Charis" Poullis, [email protected] //
// This work can only be used under an exclusive license of the author. //
////////////////////////////////////////////////////////////////////////////////////
#ifndef __UTILITIES_H__
#define __UTILITIES_H__
#include <stdarg.h>
#include <malloc.h>
#include <string.h>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include "Eigen/Eigen"
using namespace Eigen;
#include <time.h>
#include <sys/types.h>
#include <sys/timeb.h>
#define _USE_MATH_DEFINES
#include <math.h>
///To convert from radians to degrees and back
#define RADIANS_TO_DEGREES 57.295779513082320876798
#define DEGREES_TO_RADIANS 0.0174532925199432957692
#define INVALID_VALUE 666
#define EPSILON 1e-06
#define _MAX_PATH 256
///Helper function for _format(...)
static std::string vformat(const char *fmt, va_list argPtr) {
/// We draw the line at a 1MB string.
const int maxSize = 1000000;
/// If the string is less than 161 characters,
/// allocate it on the stack because this saves
/// the malloc/free time.
const int bufSize = 512;
char stackBuffer[bufSize];
int attemptedSize = bufSize - 1;
int numChars = vsnprintf(stackBuffer, attemptedSize, fmt, argPtr);
if (numChars >= 0) {
/// Got it on the first try.
//printf("%s\n",stackBuffer);
return std::string(stackBuffer);
}
/// Now use the heap.
char* heapBuffer = NULL;
while ((numChars == -1 || numChars >= attemptedSize) && (attemptedSize < maxSize)) {
/// Try a bigger size
attemptedSize *= 2;
heapBuffer = (char*)realloc(heapBuffer, attemptedSize + 1);
numChars = vsnprintf(heapBuffer, attemptedSize, fmt, argPtr);
}
//printf("%s\n",heapBuffer);
std::string result = std::string(heapBuffer);
free(heapBuffer);
return result;
}
///Prints out a string given a set of parameters. Like printf but for strings.
static std::string _format(const char* fmt ...) {
va_list argList;
va_start(argList,fmt);
std::string result = vformat(fmt, argList);
va_end(argList);
return result;
}
///Function for timestamp. Returns the time in the format YMDHMS
static std::string timestamp() {
///TIMESTAMP prints the current YMDHMS date as a time stamp.
#define TIME_SIZE 40
static char time_buffer[TIME_SIZE];
time_t now = time ( NULL );
const struct tm *tm = localtime ( &now );
size_t len = strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
#undef TIME_SIZE
return _format("%s",time_buffer);
}
///Clamps a value between the range [a,b]
inline float clamp(float x, float a, float b)
{
return x < a ? a : (x > b ? b : x);
}
///Copies the string and returns a pointer to the copied string
inline void copy_string(char *input, char *&output) {
output = new char[strlen(input)+1];
for (unsigned int i=0;i<strlen(input)+1;i++) {
output[i] = input[i];
}
return;
}
inline void copy_strings(char **input, int number_of_strings, char **&output) {
output = new char*[number_of_strings];
for (unsigned int i=0;i<number_of_strings;i++) {
copy_string(input[i],output[i]);
}
return;
}
/// Function to return the current working directory
/// this is generally the application path
static void getCurrentPath(char* buffer) {
getcwd(buffer, _MAX_PATH);
return;
}
static std::string fullPath(const char *relative_path) {
// _MAX_PATH is the maximum length allowed for a path
char working_dir[_MAX_PATH];
// use the function to get the path
getCurrentPath(working_dir);
//std::cout << "Working directory: " << working_dir << std::endl;
std::string full_path = std::string(working_dir) + "/" + std::string(relative_path);
return full_path;
}
inline static unsigned short int float2short(float f) {
unsigned int bits = * (unsigned int *) &f;
return (bits >> 15);
}
inline static unsigned short int signedfloat2short(float f) {
unsigned int bits = * (unsigned int *) &f;
return (bits >> 16);
}
inline static float short2float(unsigned short int s) {
unsigned int bits = s << 15;
return * (float *) &bits;
}
inline static float short2signedfloat(unsigned short int s) {
unsigned int bits = s << 16;
return * (float *) &bits;
}
inline int _round(float num) {
if (ceil(num)-num > 0.5f) {
return int(floor(num));
}
else {
return int(ceil(num));
}
}
#endif