-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
219 lines (197 loc) · 4.81 KB
/
Utils.cpp
File metadata and controls
219 lines (197 loc) · 4.81 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "Utils.h"
/**
Updates:
23/08/2011
- Removed tidy and moved it to another GlobalOptimisationUtils
**/
using namespace std;
void Tokenize(const string& str, vector<string>& tokens, const string& delimiters)
// Tokenize a line of text (with a stupid American spelling)
// Input: string(str) - Input string
// vector(tokens) - Location of output
// string(delimiters) - Delimiters like spaces and tabs
{
string::size_type lastPos = str.find_first_not_of(delimiters, 0); // Skip delimiters at beginning.
string::size_type pos = str.find_first_of(delimiters, lastPos); // Find first "non-delimiter".
while (string::npos != pos || string::npos != lastPos)
{
tokens.push_back(str.substr(lastPos, pos - lastPos)); // Found a token, add it to the vector.
lastPos = str.find_first_not_of(delimiters, pos); // Skip delimiters. Note the "not_of"
pos = str.find_first_of(delimiters, lastPos); // Find next "non-delimiter"
}
}
void point_on_3D_line(const vec3d& b_vec, const vec3d& p2, vec3d& new_point, double nu)
// Go to point on 3D line
// Inputs: vec3d(b_vec) - 3D start information
// vec3d(p2) - End point
// vec3d(new_point) - Point on line with new information
// double(nu) - Displacement along line
{
vec3d d_vec; // Work out displacement
d_vec.x = p2.x - b_vec.x;
d_vec.y = p2.y - b_vec.y;
d_vec.z = p2.z - b_vec.z;
new_point.x = b_vec.x + nu * d_vec.x;
new_point.y = b_vec.y + nu * d_vec.y;
new_point.z = b_vec.z + nu * d_vec.z;
}
void point_on_2D_line(const DataPoint2D &p1, const DataPoint2D &p2, DataPoint2D &new_point, double nu)
// Go to point on @D line
// Inputs: vec2d(p1) - 2D start information
// vec2d(p2) - End point
// vec2d(new_point) - Point on line with new information
// double(nu) - Displacement along line
{
DataPoint2D p3; // Get displacement
p3.x = p2.x - p1.x;
p3.y = p2.y - p1.y;
new_point.x = p1.x + nu * p3.x;
new_point.y = p1.y + nu * p3.y;
}
void find_white_space_end(const string &s, unsigned int &i)
//Does what it says on the tin
// Inputs: string(s) - String
// int(i) - Current int. Returns new white space locator at end
{
if(i >= s.size()) return;
bool isWhiteSpace = false;
if(s[i] == ' ' || s[i] == '\t')
{
isWhiteSpace = true;
i++;
}
else
{
isWhiteSpace = false;
return;
}
while(isWhiteSpace)
if(s[i] == ' ' || s[i] == '\t'){
isWhiteSpace = true;
i++;
}
else if( i >= s.size())
{
isWhiteSpace = false;
return;
}
else
{
isWhiteSpace = false;
return;
}
}
void find_character_end(const string &s, unsigned int &i)
// Find last location of charcters
// Inputs: string(s) - String
// int(i) - Current int. Returns new white space locator at end
{
if(i >= s.size()) return;
bool isChar = false;
if(s[i] == ' ' || s[i] == '\t')
{
isChar = false;
return;
}
else
{
isChar = true;
i++;
}
while(isChar)
if(s[i] == ' ' || s[i] == '\t')
{
isChar = false;
return;
}
else if( i >= s.size())
{
isChar = false; //as its the end of the line
}
else
{
isChar = true;
i++;
}
}
bool cmpStr(const string &s1, const string &s2)
// Compares two strings and returns true or false
// Inputs: s1 - String 1
// s2 - String 2
{
if (strcmp(s1.c_str(),s2.c_str()) == 0)
{
return true;
}
else
{
return false;
}
}
int randomNumber(int hi, int *idum)
// Scales random number to max possible
// Input: int (hi) - max possible
// Output: int - answer
{
// return range [0..hi-1]
return int(randomNumber(idum)*hi); // implicit cast and truncation in return
}
float randomNumberF(float hi, int *idum)
// Scales random number to max possible
// Input: int (hi) - max possible
// Output: int - answer
{
// return range [0..hi-1]
return float(randomNumber(idum)*hi); // implicit cast and truncation in return
}
// This has been copied from Numerical Recipes in C
#define MBIG 1000000000
#define MSEED 161803398
#define MZ 0
#define FAC (1.0/MBIG)
float randomNumber(int *idum)
// Get random number between 0 and 1.
// As implemented in Numerical Recipes in C
// Inputs: int seed value (must be negative)
// Returns: float
// int *idum;
{
static int inext,inextp;
static long ma[56];
static int iff=0;
long mj,mk;
int i,ii,k;
if (*idum < 0 || iff == 0) {
iff=1;
mj=MSEED-(*idum < 0 ? -*idum : *idum);
mj %= MBIG;
ma[55]=mj;
mk=1;
for (i=1;i<=54;i++) {
ii=(21*i) % 55;
ma[ii]=mk;
mk=mj-mk;
if (mk < MZ) mk += MBIG;
mj=ma[ii];
}
for (k=1;k<=4;k++)
for (i=1;i<=55;i++) {
ma[i] -= ma[1+(i+30) % 55];
if (ma[i] < MZ) ma[i] += MBIG;
}
inext=0;
inextp=31;
*idum=1;
}
if (++inext == 56) inext=1;
if (++inextp == 56) inextp=1;
mj=ma[inext]-ma[inextp];
if (mj < MZ) mj += MBIG;
ma[inext]=mj;
// cout << "Random Number Generated: " << mj*FAC << endl;
return mj*FAC;
}
#undef MBIG
#undef MSEED
#undef MZ
#undef FAC