-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGrid.cpp
More file actions
3319 lines (3082 loc) · 124 KB
/
Grid.cpp
File metadata and controls
3319 lines (3082 loc) · 124 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
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//---------------------------------------------------------------------------
#include <fstream>
#include <sstream>
#include "Grid.h"
#include "Plot.h"
#include "SimManager.h"
//---------------------------------------------------------------------------
using namespace std;
//////////////////////////////////////////////////////////////////////////////
// Constructor
//////////////////////////////////////////////////////////////////////////////
clGrid::clGrid(clSimManager *p_oSimManager, const std::string sGridName,
short int iNumIntVals, short int iNumdoubleVals, short int iNumStringVals,
short int iNumBoolVals, float fXCellLength, float fYCellLength) :
clWorkerBase(p_oSimManager) {
try {
clPlot *p_oPlot; //pointer to plot object, for querying plot dimensions
int i, j, k; //loop counters
//Allowed file types
m_iNumAllowedTypes = 4;
mp_iAllowedFileTypes = new int[m_iNumAllowedTypes];
mp_iAllowedFileTypes[0] = parfile;
mp_iAllowedFileTypes[1] = map;
mp_iAllowedFileTypes[2] = detailed_output_timestep;
mp_iAllowedFileTypes[3] = detailed_output;
//Check the data - grid name can't be empty, grid lengths and numbers of
//variables can't be negative. Throw errors if any of these things is true.
if (sGridName.length() == 0) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::clGrid";
stcErr.sMoreInfo = "Grid name must be supplied.";
throw(stcErr);
}
if (fXCellLength < 0 || fYCellLength < 0) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::clGrid";
std::stringstream s;
s << "Either X or Y grid cell length is less than 0. X = " <<
fXCellLength << " Y = " << fYCellLength;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
if (iNumIntVals < 0 || iNumdoubleVals < 0 || iNumStringVals < 0 ||
iNumBoolVals < 0) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::clGrid";
stcErr.sMoreInfo = "Data member numbers cannot be negative.";
throw(stcErr);
}
//Take the data we were passed and assign it
p_oPlot = mp_oSimManager->GetPlotObject();
m_sNameString = sGridName;
if (0 == fXCellLength)
fXCellLength = p_oPlot->GetXCellLength();
m_fXCellSize = fXCellLength;
if (0 == fYCellLength) m_fYCellSize = fXCellLength;
else m_fYCellSize = fYCellLength;
//Figure out the number of grids in X and Y directions
m_fXPlotLength = p_oPlot->GetXPlotLength();
m_fYPlotLength = p_oPlot->GetYPlotLength();
m_iNumXCells = (int)ceil(m_fXPlotLength / m_fXCellSize);
m_iNumYCells = (int)ceil(m_fYPlotLength / m_fYCellSize);
//Assign the data member numbers
m_iNumIntVals = iNumIntVals;
m_iNumFloatVals = iNumdoubleVals;
m_iNumStringVals = iNumStringVals;
m_iNumBoolVals = iNumBoolVals;
//Size and initialize the arrays
mp_gridVals = new stcRecords*[m_iNumXCells];
for (i = 0; i < m_iNumXCells; i++) {
mp_gridVals[i] = new stcRecords[m_iNumYCells];
for (j = 0; j < m_iNumYCells; j++) {
//Integer data members
if (m_iNumIntVals == 0) mp_gridVals[i][j].p_iIntVals = NULL;
else {
mp_gridVals[i][j].p_iIntVals = new int[m_iNumIntVals];
for (k = 0; k < m_iNumIntVals; k++)
mp_gridVals[i][j].p_iIntVals[k] = 0;
}
//Float data members
if (m_iNumFloatVals == 0) mp_gridVals[i][j].p_fFloatVals = NULL;
else {
mp_gridVals[i][j].p_fFloatVals = new float[m_iNumFloatVals];
for (k = 0; k < m_iNumFloatVals; k++)
mp_gridVals[i][j].p_fFloatVals[k] = 0;
}
//String data members
if (m_iNumStringVals == 0) mp_gridVals[i][j].p_sStringVals = NULL;
else {
mp_gridVals[i][j].p_sStringVals = new string[m_iNumStringVals];
for (k = 0; k < m_iNumStringVals; k++)
mp_gridVals[i][j].p_sStringVals[k] = "";
}
//Bool data members
if (m_iNumBoolVals == 0) mp_gridVals[i][j].p_bBoolVals = NULL;
else {
mp_gridVals[i][j].p_bBoolVals = new bool[m_iNumBoolVals];
for (k = 0; k < m_iNumBoolVals; k++)
mp_gridVals[i][j].p_bBoolVals[k] = false;
}
mp_gridVals[i][j].p_oPackage = NULL;
} //end of for (j = 0; j < m_iNumYCells; j++)
} //end of for (i = 0; i < m_iNumXCells; i++)
//Label arrays
if (0 == m_iNumIntVals) mp_sIntLabels = NULL;
else {
mp_sIntLabels = new string[m_iNumIntVals];
for (i = 0; i < m_iNumIntVals; i++) {
mp_sIntLabels[i] = "";
}
}
if (0 == m_iNumFloatVals) mp_sFloatLabels = NULL;
else {
mp_sFloatLabels = new string[m_iNumFloatVals];
for (i = 0; i < m_iNumFloatVals; i++) {
mp_sFloatLabels[i] = "";
}
}
if (0 == m_iNumStringVals) mp_sStringLabels = NULL;
else {
mp_sStringLabels = new string[m_iNumStringVals];
for (i = 0; i < m_iNumStringVals; i++) {
mp_sStringLabels[i] = "";
}
}
if (0 == m_iNumBoolVals) mp_sBoolLabels = NULL;
else {
mp_sBoolLabels = new string[m_iNumBoolVals];
for (i = 0; i < m_iNumBoolVals; i++) {
mp_sBoolLabels[i] = "";
}
}
//******************************************************************
// Packages
//******************************************************************
//Set up the number of data members and label lists to be identical to the
//grids
m_bPackageChangeAllowed = true;
m_bPackageDataChanged = false;
m_iNumPackageIntVals = m_iNumIntVals;
m_iNumPackageFloatVals = m_iNumFloatVals;
m_iNumPackageStringVals = m_iNumStringVals;
m_iNumPackageBoolVals = m_iNumBoolVals;
if (0 == m_iNumIntVals) mp_sPackageIntLabels = NULL;
else {
mp_sPackageIntLabels = new string[m_iNumIntVals];
for (i = 0; i < m_iNumIntVals; i++) {
mp_sPackageIntLabels[i] = "";
}
}
if (0 == m_iNumFloatVals) mp_sPackageFloatLabels = NULL;
else {
mp_sPackageFloatLabels = new string[m_iNumFloatVals];
for (i = 0; i < m_iNumFloatVals; i++) {
mp_sPackageFloatLabels[i] = "";
}
}
if (0 == m_iNumStringVals) mp_sPackageStringLabels = NULL;
else {
mp_sPackageStringLabels = new string[m_iNumStringVals];
for (i = 0; i < m_iNumStringVals; i++) {
mp_sPackageStringLabels[i] = "";
}
}
if (0 == m_iNumBoolVals) mp_sPackageBoolLabels = NULL;
else {
mp_sPackageBoolLabels = new string[m_iNumBoolVals];
for (i = 0; i < m_iNumBoolVals; i++) {
mp_sPackageBoolLabels[i] = "";
}
}
} //end of try block
catch (modelErr&err) {throw(err);}
catch (modelMsg &msg) {throw(msg);} //non-fatal error
catch (...) {
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clGrid::clGrid";
throw(stcErr);
}
}
/////////////////////////////////////////////////////////////////////////////
// ChangePackageDataStructure()
/////////////////////////////////////////////////////////////////////////////
void clGrid::ChangePackageDataStructure(short int iNumIntVals,
short int iNumdoubleVals, short int iNumCharVals, short int iNumBoolVals) {
try {
short int i;
//If packages have been created, this is not allowed - throw an error
if (!m_bPackageChangeAllowed) {
modelErr stcErr;
stcErr.iErrorCode = ILLEGAL_OP;
stcErr.sFunction = "clGrid::ChangePackageDataStructure";
stcErr.sMoreInfo = "Package data structure may not be changed after packages have been created.";
throw(stcErr);
}
//Delete all the old label arrays
if (mp_sPackageIntLabels) delete[] mp_sPackageIntLabels;
mp_sPackageIntLabels = NULL;
if (mp_sPackageFloatLabels) delete[] mp_sPackageFloatLabels;
mp_sPackageFloatLabels = NULL;
if (mp_sPackageStringLabels) delete[] mp_sPackageStringLabels;
mp_sPackageStringLabels = NULL;
if (mp_sPackageBoolLabels) delete[] mp_sPackageBoolLabels;
mp_sPackageBoolLabels = NULL;
//Assign the new variables
m_iNumPackageIntVals = iNumIntVals;
m_iNumPackageFloatVals = iNumdoubleVals;
m_iNumPackageStringVals = iNumCharVals;
m_iNumPackageBoolVals = iNumBoolVals;
//Declare the arrays
if (0 == m_iNumPackageIntVals) mp_sPackageIntLabels = NULL;
else {
mp_sPackageIntLabels = new string[iNumIntVals];
for (i = 0; i < iNumIntVals; i++) {
mp_sPackageIntLabels[i] = "";
}
}
if (0 == m_iNumPackageFloatVals) mp_sPackageFloatLabels = NULL;
else {
mp_sPackageFloatLabels = new string[iNumdoubleVals];
for (i = 0; i < iNumdoubleVals; i++) {
mp_sPackageFloatLabels[i] = "";
}
}
if (0 == m_iNumPackageStringVals) mp_sPackageStringLabels = NULL;
else {
mp_sPackageStringLabels = new string[iNumCharVals];
for (i = 0; i < iNumCharVals; i++) {
mp_sPackageStringLabels[i] = "";
}
}
if (0 == m_iNumPackageBoolVals) mp_sPackageBoolLabels = NULL;
else {
mp_sPackageBoolLabels = new string[iNumBoolVals];
for (i = 0; i < iNumBoolVals; i++) {
mp_sPackageBoolLabels[i] = "";
}
}
m_bPackageDataChanged = true;
} //end of try block
catch (modelErr&err) {throw(err);}
catch (modelMsg &msg) {throw(msg);} //non-fatal error
catch (...) {
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clGrid::ChangePackageDataStructure";
throw(stcErr);
}
}
/////////////////////////////////////////////////////////////////////////////
// CreatePackage()
/////////////////////////////////////////////////////////////////////////////
clPackage* clGrid::CreatePackage(
clPackage *p_oPreviousPackage) {
clPackage *p_oNewPackage, *p_oNextPackage;
//Verify that the pointer isn't NULL - if it is, throw an error
if (NULL == p_oPreviousPackage) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::CreatePackage(clPackage)";
stcErr.sMoreInfo = "Pointer passed cannot be NULL.";
throw(stcErr);
}
p_oNewPackage = new clPackage(this, p_oPreviousPackage->mp_parentCell);
//Put this new variable in between the previous pointer passed and any
//"next" it might have
p_oNextPackage = p_oPreviousPackage->mp_oNext;
p_oPreviousPackage->mp_oNext = p_oNewPackage;
p_oNewPackage->mp_oNext = p_oNextPackage;
return p_oNewPackage;
}
/////////////////////////////////////////////////////////////////////////////
// CreatePackageOfCell()
/////////////////////////////////////////////////////////////////////////////
clPackage* clGrid::CreatePackageOfCell(int iX, int iY) {
clPackage *p_oNewPackage, *p_oNextPackage;
//Verify that the grid numbers are valid - if they aren't, throw an error
if (iX < 0 || iX >= m_iNumXCells || iY < 0 || iY >= m_iNumYCells) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::CreatePackageOfGrid";
std::stringstream s;
s << "Invalid grid numbers. X = " << iX << ", Y = " << iY;
stcErr.sMoreInfo = s.str();
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
p_oNewPackage = new clPackage(this, &mp_gridVals[iX][iY]);
//Put this new variable in between the previous pointer passed and any
//"next" it might have
p_oNextPackage = mp_gridVals[iX][iY].p_oPackage;
mp_gridVals[iX][iY].p_oPackage = p_oNewPackage;
p_oNewPackage->mp_oNext = p_oNextPackage;
return p_oNewPackage;
}
/////////////////////////////////////////////////////////////////////////////
// CreatePackageAtPoint()
/////////////////////////////////////////////////////////////////////////////
clPackage* clGrid::CreatePackageAtPoint(float fX, float fY) {
clPackage *p_oNewPackage, *p_oNextPackage;
int iXGrid = 0, iYGrid = 0; //the X and Y grid indexes for the values array
//Verify that the grid numbers are valid - if they aren't, throw an error
if (fX >= m_fXPlotLength || fX < 0
|| fY >= m_fYPlotLength || fY < 0) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::CreatePackageAtPoint";
std::stringstream s;
s << "At least one grid coordinate is outside of grid - X = " << fX
<< " & Y = " << fY;
stcErr.sMoreInfo = s.str();
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
//Divide the X and Y values by the size of the grid divisions to get grid
//cell indexes
iXGrid = (int)floor(fX / m_fXCellSize);
iYGrid = (int)floor(fY / m_fYCellSize);
p_oNewPackage = new clPackage(this, &mp_gridVals[iXGrid][iYGrid]);
//Put this new variable in between the previous pointer passed and any
//"next" it might have
p_oNextPackage = mp_gridVals[iXGrid][iYGrid].p_oPackage;
mp_gridVals[iXGrid][iYGrid].p_oPackage = p_oNewPackage;
p_oNewPackage->mp_oNext = p_oNextPackage;
return p_oNewPackage;
}
/////////////////////////////////////////////////////////////////////////////
// GetFirstPackageOfCell()
/////////////////////////////////////////////////////////////////////////////
clPackage* clGrid::GetFirstPackageOfCell(int iX, int iY) {
//Verify that the grid numbers are valid - if they aren't, throw an error
if (iX < 0 || iX >= m_iNumXCells || iY < 0 || iY >= m_iNumYCells) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetFirstPackageOfCell";
std::stringstream s;
s << "Invalid grid numbers for grid " << m_sNameString << ". X = "
<< iX << ", Y = " << iY;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
return mp_gridVals[iX][iY].p_oPackage;
}
/////////////////////////////////////////////////////////////////////////////
// GetFirstPackageAtPoint()
/////////////////////////////////////////////////////////////////////////////
clPackage* clGrid::GetFirstPackageAtPoint(float fX, float fY) {
int iXGrid = 0, iYGrid = 0; //the X and Y grid indexes for the values array
//Verify that the grid numbers are valid - if they aren't, throw an error
if (fX >= m_fXPlotLength || fX < 0
|| fY >= m_fYPlotLength || fY < 0) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetFirstPackageAtPoint";
std::stringstream s;
s << "At least one grid coordinate is outside of grid " << m_sNameString
<< " - X = " << fX << " & Y = " << fY;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
//Divide the X and Y values by the size of the grid divisions to get grid
//cell indexes
iXGrid = (int)floor(fX / m_fXCellSize);
iYGrid = (int)floor(fY / m_fYCellSize);
return mp_gridVals[iXGrid][iYGrid].p_oPackage;
}
//////////////////////////////////////////////////////////////////////////////
// GetPointOfCell
//////////////////////////////////////////////////////////////////////////////
void clGrid::GetPointOfCell(short int iCellX, short int iCellY,
float *fX, float *fY) {
try {
float fTemp;
//Make sure the cell points are valid
if (iCellX < 0 || iCellX >= m_iNumXCells ||
iCellY < 0 || iCellY >= m_iNumYCells) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetPointOfCell";
std::stringstream s;
s << "Invalid grid numbers for grid " << m_sNameString << ". X = "
<< iCellX << ", Y = " << iCellY;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
//If this is not the last cell in a row in either direction, the calculation
//is straightforward
if (iCellX < (m_iNumXCells - 1) && iCellY < (m_iNumYCells - 1)) {
*fX = (iCellX * m_fXCellSize) + (m_fXCellSize / 2);
*fY = (iCellY * m_fYCellSize) + (m_fYCellSize / 2);
} else {
//We have to correct for the possibility that the last grid cells are short
//Find half of the length of the last cell
if (iCellX == m_iNumXCells - 1)
fTemp = (m_fXPlotLength -
((m_iNumXCells - 1) * m_fXCellSize)) / 2;
else fTemp = m_fXCellSize / 2;
*fX = (iCellX * m_fXCellSize) + fTemp;
if (iCellY == m_iNumYCells - 1)
fTemp = (m_fYPlotLength - ((m_iNumYCells - 1) * m_fYCellSize)) / 2;
else fTemp = m_fYCellSize / 2;
*fY = (iCellY * m_fYCellSize) + fTemp;
}
}
catch (modelErr&err) {throw(err);}
catch (modelMsg &msg) {throw(msg);} //non-fatal error
catch (...) {
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clGrid::GetPointOfCell";
throw(stcErr);
}
}
/////////////////////////////////////////////////////////////////////////////
// GetCellOfPoint()
/////////////////////////////////////////////////////////////////////////////
void clGrid::GetCellOfPoint(float fX, float fY, short int *iCellX,
short int *iCellY) {
try {
//Make sure the cell points are valid
if (fX >= m_fXPlotLength || fX < 0 || fY >= m_fYPlotLength || fY < 0) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetPointOfCell";
std::stringstream s;
s << "At least one grid coordinate is outside of grid - X = " << fX
<< " & Y = " << fY;
stcErr.sMoreInfo = s.str();
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
//Divide the X and Y values by the size of the grid divisions to get grid
//cell indexes
*iCellX = (int)floor(fX / m_fXCellSize);
*iCellY = (int)floor(fY / m_fYCellSize);
}
catch (modelErr&err) {throw(err);}
catch (modelMsg &msg) {throw(msg);} //non-fatal error
catch (...) {
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clGrid::GetCellOfPoint";
throw(stcErr);
}
}
/////////////////////////////////////////////////////////////////////////////
// DeletePackage()
/////////////////////////////////////////////////////////////////////////////
void clGrid::DeletePackage(clPackage *p_oOldPackage){
try {
clPackage *p_oPreviousPackage;
//If the pointer to the package is NULL, do nothing; exit
if (NULL == p_oOldPackage) return;
//If this was the first pointer of the cell, have the parent point to
//the next package
if (p_oOldPackage == p_oOldPackage->mp_parentCell->p_oPackage) {
p_oOldPackage->mp_parentCell->p_oPackage = p_oOldPackage->mp_oNext;
//If not the first, find the package or grid cell that points to this so we
//can NULL out its "next" pointer
} else {
p_oPreviousPackage = p_oOldPackage->mp_parentCell->p_oPackage;
while (p_oPreviousPackage != NULL &&
p_oPreviousPackage->mp_oNext != p_oOldPackage)
p_oPreviousPackage = p_oPreviousPackage->mp_oNext;
if (p_oPreviousPackage)
p_oPreviousPackage->mp_oNext = p_oOldPackage->mp_oNext;
}
delete p_oOldPackage;
}
catch (modelErr&err) {throw(err);}
catch (modelMsg &msg) {throw(msg);} //non-fatal error
catch (...) {
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clGrid::DeletePackage";
throw(stcErr);
}
}
/////////////////////////////////////////////////////////////////////////////
// Destructor()
/////////////////////////////////////////////////////////////////////////////
clGrid::~clGrid() {
clPackage *p_oPackage, *p_oNextPackage;
int i, j; //loop counters
//Delete the values array
for (i = 0; i < m_iNumXCells; i++) {
for (j = 0; j < m_iNumYCells; j++) {
//delete data arrays
delete[] mp_gridVals[i][j].p_iIntVals;
delete[] mp_gridVals[i][j].p_fFloatVals;
delete[] mp_gridVals[i][j].p_sStringVals;
delete[] mp_gridVals[i][j].p_bBoolVals;
//delete any linked packages
p_oPackage = mp_gridVals[i][j].p_oPackage;
while (p_oPackage) {
p_oNextPackage = p_oPackage->mp_oNext;
delete p_oPackage;
p_oPackage = p_oNextPackage;
} //end of while (p_record)
} //end of for (j = 0; j < m_iNumYCells; j++)
delete[] mp_gridVals[i];
} //end of for (i = 0; i < m_iNumXCells; i++)
delete[] mp_gridVals; mp_gridVals = NULL;
//Delete the label arrays
delete[] mp_sIntLabels; mp_sIntLabels = NULL;
delete[] mp_sFloatLabels; mp_sFloatLabels = NULL;
delete[] mp_sStringLabels; mp_sStringLabels = NULL;
delete[] mp_sBoolLabels; mp_sBoolLabels = NULL;
//Delete the package label arrays
delete[] mp_sPackageIntLabels; mp_sPackageIntLabels = NULL;
delete[] mp_sPackageFloatLabels; mp_sPackageFloatLabels = NULL;
delete[] mp_sPackageStringLabels; mp_sPackageStringLabels = NULL;
delete[] mp_sPackageBoolLabels; mp_sPackageBoolLabels = NULL;
//Set the various values to zero - this way if this has been called before
//there won't be an error trying to delete the values array again - i.e.
//erroring out followed by destructor
m_iNumXCells = 0;
m_iNumYCells = 0;
m_fXCellSize = 0;
m_fYCellSize = 0;
m_iNumIntVals = 0;
m_iNumFloatVals = 0;
m_iNumStringVals = 0;
m_iNumBoolVals = 0;
m_iNumPackageIntVals = 0;
m_iNumPackageFloatVals = 0;
m_iNumPackageStringVals = 0;
m_iNumPackageBoolVals = 0;
}
/////////////////////////////////////////////////////////////////////////////
// GetData()
/////////////////////////////////////////////////////////////////////////////
void clGrid::GetData(DOMDocument *p_oDoc) {
try {
if (NULL == p_oDoc) { //throw error
modelErr stcErr;
stcErr.iErrorCode = BAD_FILE;
stcErr.sFunction = "clGrid::GetData";
stcErr.sMoreInfo = "DOM document pointer passed was NULL.";
throw(stcErr);
}
//Read a map file, if present
ReadMapFile(p_oDoc);
} //end of try block
catch (modelErr&err) {throw(err);}
catch (modelMsg &msg) {throw(msg);} //non-fatal error
catch (...) {
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clGrid::GetData";
throw(stcErr);
}
}
/////////////////////////////////////////////////////////////////////////////
// GetValueAtPoint()
/////////////////////////////////////////////////////////////////////////////
void clGrid::GetValueAtPoint(float fX, float fY, short int iCode,
int *p_iValHolder) {
try {
int iXGrid = 0, iYGrid = 0; //the X and Y grid indexes for the values array
//Make sure the code is valid and the coordinates are within the grid - if
//not throw an error
if (iCode < 0 || iCode >= m_iNumIntVals) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetValueAtPoint (int)";
std::stringstream s;
s << "Invalid data code - " << iCode;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
if (fX >= m_fXPlotLength || fX < 0
|| fY >= m_fYPlotLength || fY < 0) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetValueAtPoint (int)";
std::stringstream s;
s << "At least one grid coordinate is outside of grid - X = " << fX
<< " & Y = " << fY;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
//Divide the X and Y values by the size of the grid divisions to get grid
//cell indexes
iXGrid = (int)floor(fX / m_fXCellSize);
iYGrid = (int)floor(fY / m_fYCellSize);
//Return the value pointed to by the indexes
*p_iValHolder = mp_gridVals[iXGrid][iYGrid].p_iIntVals[iCode];
}
catch (modelErr&err) {throw(err);}
catch (modelMsg &msg) {throw(msg);} //non-fatal error
catch (...) {
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clGrid::GetValueAtPoint (int)";
throw(stcErr);
}
}
/////////////////////////////////////////////////////////////////////////////
// GetValueAtPoint()
/////////////////////////////////////////////////////////////////////////////
void clGrid::GetValueAtPoint(float fX, float fY, short int iCode,
float *p_fValHolder) {
try {
int iXGrid = 0, iYGrid = 0; //the X and Y grid indexes for the values array
//Make sure the code is valid and the coordinates are within the grid - if
//not throw an error
if (iCode < 0 || iCode >= m_iNumFloatVals) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetValueAtPoint (float)";
std::stringstream s;
s << "Invalid data code - " << iCode;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
if (fX >= m_fXPlotLength || fX < 0
|| fY >= m_fYPlotLength || fY < 0) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetValueAtPoint (float)";
std::stringstream s;
s << "At least one grid coordinate is outside of grid - X = " << fX
<< " & Y = " << fY;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
//Divide the X and Y values by the size of the grid divisions to get grid
//cell indexes
iXGrid = (int)floor(fX / m_fXCellSize);
iYGrid = (int)floor(fY / m_fYCellSize);
//Return the value pointed to by the indexes
*p_fValHolder = mp_gridVals[iXGrid][iYGrid].p_fFloatVals[iCode];
}
catch (modelErr&err) {throw(err);}
catch (modelMsg &msg) {throw(msg);} //non-fatal error
catch (...) {
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clGrid::GetValueAtPoint (float)";
throw(stcErr);
}
}
/////////////////////////////////////////////////////////////////////////////
// GetValueAtPoint()
/////////////////////////////////////////////////////////////////////////////
void clGrid::GetValueAtPoint(float fX, float fY, short int iCode,
string *p_sValHolder) {
try {
int iXGrid = 0, iYGrid = 0; //the X and Y grid indexes for the values array
//Make sure the code is valid and the coordinates are within the grid - if
//not throw an error
if (iCode < 0 || iCode >= m_iNumIntVals) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetValueAtPoint (char)";
std::stringstream s;
s << "Invalid data code - " << iCode;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
if (fX >= m_fXPlotLength || fX < 0
|| fY >= m_fYPlotLength || fY < 0) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetValueAtPoint (char)";
std::stringstream s;
s << "At least one grid coordinate is outside of grid - X = " << fX
<< " & Y = " << fY;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
//Divide the X and Y values by the size of the grid divisions to get grid
//cell indexes
iXGrid = (int)floor(fX / m_fXCellSize);
iYGrid = (int)floor(fY / m_fYCellSize);
//Return the value pointed to by the indexes
*p_sValHolder = mp_gridVals[iXGrid][iYGrid].p_sStringVals[iCode];
}
catch (modelErr&err) {throw(err);}
catch (modelMsg &msg) {throw(msg);} //non-fatal error
catch (...) {
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clGrid::GetValueAtPoint (char)";
throw(stcErr);
}
}
/////////////////////////////////////////////////////////////////////////////
// GetValueAtPoint()
/////////////////////////////////////////////////////////////////////////////
void clGrid::GetValueAtPoint(float fX, float fY, short int iCode,
bool *p_bValHolder) {
try {
int iXGrid = 0, iYGrid = 0; //the X and Y grid indexes for the values array
//Make sure the code is valid and the coordinates are within the grid - if
//not throw an error
if (iCode < 0 || iCode >= m_iNumBoolVals) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetValueAtPoint (bool)";
std::stringstream s;
s << "Invalid data code - " << iCode;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
if (fX >= m_fXPlotLength || fX < 0
|| fY >= m_fYPlotLength || fY < 0) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetValueAtPoint (bool)";
std::stringstream s;
s << "At least one grid coordinate is outside of grid - X = " << fX
<< " & Y = " << fY;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
//Divide the X and Y values by the size of the grid divisions to get grid
//cell indexes
iXGrid = (int)floor(fX / m_fXCellSize);
iYGrid = (int)floor(fY / m_fYCellSize);
//Return the value pointed to by the indexes
*p_bValHolder = mp_gridVals[iXGrid][iYGrid].p_bBoolVals[iCode];
}
catch (modelErr&err) {throw(err);}
catch (modelMsg &msg) {throw(msg);} //non-fatal error
catch (...) {
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clGrid::GetValueAtPoint (bool)";
throw(stcErr);
}
}
/////////////////////////////////////////////////////////////////////////////
// GetValueOfCell()
/////////////////////////////////////////////////////////////////////////////
void clGrid::GetValueOfCell(int iX, int iY, short int iCode,
int *p_iValHolder) {
try {
//Make sure that the code is valid and that the grid coordinates are OK -
//if not throw error
if (iCode < 0 || iCode >= m_iNumIntVals) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetValueOfCell (int)";
std::stringstream s;
s << "Invalid data code - " << iCode;
stcErr.sMoreInfo = s.str();
throw(stcErr);
} else if (iX >= m_iNumXCells || iX < 0 || iY >= m_iNumYCells || iY < 0) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetValueOfCell (int)";
std::stringstream s;
s << "At least one grid coordinate is outside of grid - X = " << iX
<< " & Y = " << iY;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
*p_iValHolder = mp_gridVals[iX][iY].p_iIntVals[iCode];
} //end of try block
catch (modelErr&err) {throw(err);}
catch (modelMsg &msg) {throw(msg);} //non-fatal error
catch (...) {
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clGrid::GetValueOfCell (int)";
throw(stcErr);
}
}
/////////////////////////////////////////////////////////////////////////////
// GetValueOfCell()
/////////////////////////////////////////////////////////////////////////////
void clGrid::GetValueOfCell(int iX, int iY, short int iCode,
float *p_fValHolder) {
try {
//Make sure that the code is valid and that the grid coordinates are OK -
//if not throw error
if (iCode < 0 || iCode >= m_iNumFloatVals) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetValueOfCell (float)";
std::stringstream s;
s << "Invalid data code - " << iCode;
stcErr.sMoreInfo = s.str();
throw(stcErr);
} else if (iX >= m_iNumXCells || iX < 0 || iY >= m_iNumYCells || iY < 0) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetValueOfCell (float)";
std::stringstream s;
s << "At least one grid coordinate is outside of grid - X = " << iX
<< " & Y = " << iY;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
*p_fValHolder = mp_gridVals[iX][iY].p_fFloatVals[iCode];
} //end of try block
catch (modelErr&err) {throw(err);}
catch (modelMsg &msg) {throw(msg);} //non-fatal error
catch (...) {
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clGrid::GetValueOfCell (float)";
throw(stcErr);
}
}
/////////////////////////////////////////////////////////////////////////////
// GetValueOfCell()
/////////////////////////////////////////////////////////////////////////////
void clGrid::GetValueOfCell(int iX, int iY, short int iCode,
string *p_sValHolder) {
try {
//Make sure that the code is valid and that the grid coordinates are OK -
//if not throw error
if (iCode < 0 || iCode >= m_iNumStringVals) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetValueOfCell (char)";
std::stringstream s;
s << "Invalid data code - " << iCode;
stcErr.sMoreInfo = s.str();
throw(stcErr);
} else if (iX >= m_iNumXCells || iX < 0 || iY >= m_iNumYCells || iY < 0) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetValueOfCell (char)";
std::stringstream s;
s << "At least one grid coordinate is outside of grid - X = " << iX
<< " & Y = " << iY;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
*p_sValHolder = mp_gridVals[iX][iY].p_sStringVals[iCode];
} //end of try block
catch (modelErr&err) {throw(err);}
catch (modelMsg &msg) {throw(msg);} //non-fatal error
catch (...) {
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clGrid::GetValueOfCell (char)";
throw(stcErr);
}
}
/////////////////////////////////////////////////////////////////////////////
// GetValueOfCell()
/////////////////////////////////////////////////////////////////////////////
void clGrid::GetValueOfCell(int iX, int iY, short int iCode,
bool *p_bValHolder) {
try {
//Make sure that the code is valid and that the grid coordinates are OK -
//if not throw error
if (iCode < 0 || iCode >= m_iNumBoolVals) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetValueOfCell (bool)";
std::stringstream s;
s << "Invalid data code - " << iCode;
stcErr.sMoreInfo = s.str();
throw(stcErr);
} else if (iX >= m_iNumXCells || iX < 0 || iY >= m_iNumYCells || iY < 0) {
modelErr stcErr;
stcErr.iErrorCode = BAD_DATA;
stcErr.sFunction = "clGrid::GetValueOfCell (bool)";
std::stringstream s;
s << "At least one grid coordinate is outside of grid - X = " << iX
<< " & Y = " << iY;
stcErr.sMoreInfo = s.str();
throw(stcErr);
}
*p_bValHolder = mp_gridVals[iX][iY].p_bBoolVals[iCode];
} //end of try block
catch (modelErr&err) {throw(err);}
catch (modelMsg &msg) {throw(msg);} //non-fatal error
catch (...) {
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clGrid::GetValueOfCell (bool)";
throw(stcErr);
}
}
/////////////////////////////////////////////////////////////////////////////
// GetAverageIntValue()
/////////////////////////////////////////////////////////////////////////////
float clGrid::GetAverageIntValue(float fX, float fY, short int iCode,
float fRadius) {
return GetAverageValue(fX, fY, iCode, fRadius, false);
}
/////////////////////////////////////////////////////////////////////////////
// GetAveragedoubleValue()
/////////////////////////////////////////////////////////////////////////////
float clGrid::GetAverageFloatValue(float fX, float fY, short int iCode,
float fRadius) {
return GetAverageValue(fX, fY, iCode, fRadius, true);
}
/////////////////////////////////////////////////////////////////////////////
// GetAverageValue()
/////////////////////////////////////////////////////////////////////////////