forked from nepomukotte/CARE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GOrderedGrid.cpp
463 lines (363 loc) · 10.8 KB
/
GOrderedGrid.cpp
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*! GOrderedGrid.cpp
Version 1.0
June 17, 2011
Charlie Duke
Grinnell College
*/
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <cmath>
#include <map>
#include <deque>
#include <list>
#include <iterator>
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <limits>
using namespace std;
#include "TMatrixD.h"
#include "TMath.h"
#include "Math/Vector3D.h"
#include "Math/RotationX.h"
#include "Math/RotationY.h"
#include "Math/RotationZ.h"
#include "Math/Rotation3D.h"
#include "GOrderedGrid.h"
#define DEBUG(x) cout << #x << " = " << x << endl
#define DEBUGS(x) cout << " "<< #x << " = " << x << endl;
// GridElem constructor
GridElem::GridElem(const int &elemNum, const double &dist)
: elemNum(elemNum),dist(dist) {};
/*************** end of GridElem::GridElem *********/
// used in stl sort algorithm
bool gridBinSort(const GridElem &s1, const GridElem &s2) {
return (s1.dist < s2.dist);
}
/**************** end of gridBinSort ************/
GOrderedGrid::GOrderedGrid( const vector<double> &xe,const vector<double> &ye,
const vector<double> &re,
const int &nbinsx, const int &nbinsy,
const int &option,
const string &filename) :vmx(xe),vmy(ye),vmr(re),
nbinsx(nbinsx),
nbinsy(nbinsy),
iGridOption(option),
sFileName(filename)
{
bool debug = false;
bGridOption = true;
if (iGridOption==0 ) bGridOption = false;
if (debug) {
cout << " -- GOrderedGrid::GOrderedGrid " << endl;
cout << " nbinsx " << nbinsx << endl;
cout << " nbinsy " << nbinsy << endl;
cout << " iOption " << iGridOption << endl;
cout << " gridfilename " << sFileName << endl;
}
if ( (nbinsx == 0) || (nbinsy == 0) ) {
cout << " returning from GOrderedGrid constructor: " << endl;
cout << " nbinsx == 0 or nbinsy == 0. " << endl;
cout << " full element loop search forced " << endl;
}
else {
if (iGridOption == 0) {
return;
}
else if (iGridOption==1) {
initialize();
makeGridParameters();
makeGrid();
}
else if (iGridOption==2) {
initialize();
makeGridParameters();
makeGrid();
printGrid();
}
else if (iGridOption==3) {
if (!readGrid()) {
initialize();
makeGridParameters();
makeGrid();
printGrid();
}
}
else {
cout << "unknown iGridOption in GOrderGrid constructor: "
<< iGridOption << endl;
exit(0);
}
}
};
/********************* end of GOrderedGrid ***********/
GOrderedGrid::~GOrderedGrid() {
for (unsigned i = 0;i<vGrid.size();i++) {
delete vGrid[i];
}
};
/********************* end of ~GOrderedGrid ***********/
GOrderedGrid::GOrderedGrid(const GOrderedGrid &orderedGrid) {
};
/********************* end of GOrderedGrid ***********/
void GOrderedGrid::initialize() {
fXmin = 99999999.9;
fXmax = -99999999.9;
fYmin = 99999999.9;
fYmax = -99999999.9;
fDelX = 0.0;
fDelY = 0.0;
};
/********************* end of initialize ***********************/
bool GOrderedGrid::makeGridParameters() {
bool debug = false;
if (debug) {
cout << " -- GOrderedGrid::makeGridParameters " << endl;
}
// find fXmin,fXmax,fYmin,fYmax
for (unsigned i = 0;i<vmx.size();i++) {
double diff;
diff = vmx[i] - vmr[i];
if ( diff < fXmin ) fXmin = diff;
diff = vmx[i] + vmr[i];
if (diff > fXmax) fXmax = diff;
diff = vmy[i] - vmr[i];
if ( diff < fYmin ) fYmin = diff;
diff = vmy[i] + vmr[i];
if (diff > fYmax) fYmax = diff;
}
fDelX = (fXmax-fXmin)/nbinsx;
fDelY = (fYmax-fYmin)/nbinsy;
if (debug) {
DEBUGS(fXmin);DEBUGS(fXmax);
DEBUGS(fYmin);DEBUGS(fYmax);
DEBUGS(fDelX);DEBUGS(fDelY);
}
return true;
};
/******************** end of makeGridParameters **********************/
bool GOrderedGrid::makeGrid() {
bool debug = false;
if (debug) {
cout << " -- GOrderedGrid::makeGrid " << endl;
}
int numBins = nbinsx*nbinsy;
// prepare the grid since we know the number of bins
for (int i = 0;i<numBins;i++) {
vGrid.push_back(new list<GridElem> );
}
if (debug) {
cout << "vGrid size " << vGrid.size() << endl;
}
double grid_dia = sqrt(fDelX*fDelX + fDelY*fDelY) / 2.0;
// loop over grid bins
for (int gb = 0;gb < numBins; ++gb) {
// get bin center location
int ybin = gb/nbinsx;
int xbin = gb % nbinsx;
double xc = (fXmin + xbin*fDelX) + (fDelX/2);
double yc = (fYmin + ybin*fDelY) + (fDelY/2);
// loop over elements
for (unsigned elem=0;elem<vmx.size();++elem) {
// get x,y,radius for this element
double xe = vmx[elem];
double ye = vmy[elem];
double ra = vmr[elem];
// get distance from bin center to element location
double dst = sqrt( ((xe-xc)*(xe-xc)) + ((ye-yc)*(ye-yc)) );
// test distance
if (dst < (ra + grid_dia) ) {
if (debug) {
cout << "dst " << dst << endl;
}
//cout << " gb elem dst " << gb << " " << elem
// << " " << dst << endl;
// insert this element into the grid
GridElem grdf(elem,dst);
vGrid[gb]->push_back(grdf);
}
}
}
// sort the lists
for (int gb = 0;gb < numBins; ++gb) {
vGrid[gb]->sort(gridBinSort);
}
return true;
};
/********************* end of makeGrid ************************/
bool GOrderedGrid::getGridBinList(const double &x, const double &y,
list<GridElem> *&gridList) {
bool debug = false;
if (debug) {
cout << " -- GOrderedGrid::getGridBinList" << endl;
}
if ( (nbinsx==0) || (nbinsy==0) ||
(!bGridOption) ) return false;
bool gridFound = true;
int xbin,ybin;
xbin = (int)( floor( (x - fXmin)/fDelX ));
ybin = (int)( floor( (y - fYmin)/fDelY ));
int gridkey = (xbin + (nbinsx*ybin) );
if (debug) {
DEBUGS(x);DEBUGS(xbin);
DEBUGS(y);DEBUGS(ybin);
DEBUGS(gridkey);
}
if ( (gridkey < 0) || (gridkey > (nbinsx*nbinsy) - 1) ) {
gridFound = false;
}
else {
gridList = vGrid[gridkey];
if (debug) {
cout << " gridList->size() " << gridList->size() << endl;
}
}
if (debug) {
cout << " gridFound " << gridFound << endl;
}
return gridFound;
};
/********************* end of getGridBinList ************************/
bool GOrderedGrid::readGrid() {
bool debug = false;
if (debug) {
cout << " -- GOrderedGrid::readGrid" << endl;
}
ifstream inFile(sFileName.c_str(),ios::in);
if (! inFile) {
return false; // no such file
}
string fileRec;
vector<string> tokens;
// skip the first three documentation records
getline(inFile,fileRec,'\n');
getline(inFile,fileRec,'\n');
getline(inFile,fileRec,'\n');
// read grid parameters
getline(inFile,fileRec,'\n');
tokenizer(fileRec,tokens);
nbinsx = atoi(tokens[0].c_str());
fDelX = atof(tokens[1].c_str());
fXmin = atof(tokens[2].c_str());
nbinsy = atoi(tokens[3].c_str());
fDelY = atof(tokens[4].c_str());
fYmin = atof(tokens[5].c_str());
if (debug) {
DEBUGS(nbinsx);DEBUGS(fDelX);DEBUGS(fXmin);
DEBUGS(nbinsy);DEBUGS(fDelY);DEBUGS(fYmin);
}
int numBins = nbinsx*nbinsy;
// start with a new grid if it's already filled
if (vGrid.size() > 0) {
for (unsigned i = 0;i<vGrid.size();i++) {
delete vGrid[i];
}
vGrid.clear();
}
// prepare the grid since we know the number of bins
for (int i = 0;i<numBins;i++) {
vGrid.push_back(new list<GridElem> );
}
for (int i = 0;i<nbinsx*nbinsy;i++) {
//for (int i = 0;i<10;i++) {
string elements;
string distances;
vector<string> tokElem;
vector<string> tokDist;
// read element list
getline(inFile,elements,'\n');
tokenizer(elements,tokElem);
int gridkey = atoi(tokElem[0].c_str());
int numElem = atoi(tokElem[3].c_str());
// read distance list
getline(inFile,distances,'\n');
tokenizer(distances,tokDist);
// loop over elements
for (int el=0;el<numElem;++el) {
int elem = atoi(tokElem[4+el].c_str());
double dist = atof(tokDist[4+el].c_str());
GridElem grdf(elem-1,dist);
vGrid[gridkey]->push_back(grdf);
}
}
return true;
};
/********************* end of readGrid ************************/
bool GOrderedGrid::printGrid() {
bool debug = false;
if (debug) {
cout << " -- GOrderedGrid::printGrid " << endl;
cout << " opening file " << sFileName << endl;
}
if (sFileName=="") {
return false;
}
FILE *fg;
if ( (fg= fopen(sFileName.c_str(),"w")) == NULL) {
cerr << " failed to open element grid file "
<< sFileName << endl;
exit(0);
}
fprintf(fg,"first data record: nbinsx delx xmin nbinsy dely ymin\n");
fprintf(fg,"listing 1: gridKey,xbin,ybin,numElements,list of elements\n");
fprintf(fg," followed by same format, list of distances\n");
fprintf(fg,"%d %lf %lf %d %lf %lf\n",nbinsx,fDelX,fXmin,
nbinsy,fDelY,fYmin);
int numBins = nbinsx*nbinsy;
for (int i = 0;i<numBins;++i) {
int xb,yb;
yb = i / nbinsx;
xb = i % nbinsx;
// get list for this element
list<GridElem> *lst = vGrid[i];
list<GridElem>::iterator iterlst;
unsigned numElem = vGrid[i]->size();
fprintf(fg,"%4d %3d %3d %2d ",i, xb,yb,numElem);
if (numElem == 0) {
fprintf(fg,"\n");
}
else {
for (iterlst = lst->begin();
iterlst!= lst->end(); ++iterlst) {
int elemN = iterlst->elemNum;
fprintf(fg,"%3d ",elemN + 1);
}
fprintf(fg,"\n");
}
fprintf(fg,"%4d %3d %3d %2d ",i, xb,yb,numElem);
if (numElem == 0) {
fprintf(fg,"\n");
}
else {
for (iterlst = lst->begin();
iterlst!= lst->end(); ++iterlst) {
double distg = iterlst->dist;
fprintf(fg,"%5.2f ",distg);
}
fprintf(fg,"\n");
}
}
fclose(fg);
return true;
};
/********************* end of printGrid ************************/
void GOrderedGrid::tokenizer(const string& str,
vector<string>& tokens) {
tokens.clear();
// tokenizer with multiple delimiters
char delim[] = " ,";
string::size_type lgt = 2;
string::size_type idx = 0;
string::size_type start;
// find first non-delimiter character for start
while ( (idx = str.find_first_not_of(delim,idx,lgt) ) !=string::npos ) {
start = idx; // start of string
idx = str.find_first_of(delim,idx,lgt);
tokens.push_back(str.substr(start,idx - start));
}
};
/******************** end of tokenizer ************/