-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDgBoundedRFS2D.cpp
244 lines (194 loc) · 6.83 KB
/
DgBoundedRFS2D.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
////////////////////////////////////////////////////////////////////////////////
//
// DgBoundedRFS2D.cpp: DgBoundedRFS2D class implementation
//
// Version 6.1 - Kevin Sahr, 5/23/13
//
////////////////////////////////////////////////////////////////////////////////
#include <limits>
#include <cstdint>
#include "DgBoundedRFS2D.h"
#include "DgBoundedHexC2RF2D.h"
#include "DgDiscRFS2D.h"
#include "DgTriGrid2DS.h"
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
DgBoundedRFS2D::DgBoundedRFS2D (const DgDiscRFS2D& rf,
const DgIVec2D& lowerLeft0,
const DgIVec2D& upperRight0)
: DgBoundedRF< DgResAdd<DgIVec2D>, DgDVec2D, long double > (rf,
DgResAdd<DgIVec2D> (DgIVec2D(0, 0), 0),
DgResAdd<DgIVec2D> (DgIVec2D(0, 0), 0), rf.undefAddress()),
discRFS_ (rf)
{
// check to see if current limitations are met
if (lowerLeft0 != DgIVec2D(0, 0))
{
report("DgBoundedRFS2DS::DgBoundedRFS2DS() only lowerLeft0 = (0, 0) "
"supported", DgBase::Fatal);
}
// allocate the grids
grids_ = new vector<DgBoundedRF2D*>(discRFS().nRes());
int totTicks = 1;
std::int64_t numI = upperRight0.i() + 1;
std::int64_t numJ = upperRight0.j() + 1;
if (rf.aperture() == 3) // better be hex!
{
for (int i = 0; i < discRFS().nRes(); i++)
{
bool isClassI = !(i % 2);
if (isClassI)
{
(*grids_)[i] = new DgBoundedRF2D(*rf.grids()[i], DgIVec2D(0, 0),
DgIVec2D(totTicks * numI - 1, totTicks * numJ - 1));
totTicks *= 3;
}
else
{
(*grids_)[i] = new DgBoundedHexC2RF2D(*rf.grids()[i],
DgIVec2D(0, 0), DgIVec2D(totTicks * numI - 1,
totTicks * numJ - 1));
}
}
}
else
{
// check that the aperture is a perfect square
int sqrtApp = static_cast<int>(sqrt(static_cast<float>(rf.aperture())));
if (static_cast<unsigned int>(sqrtApp * sqrtApp) != rf.aperture())
{
report("DgBoundedRFS2DS::DgBoundedRFS2DS() aperture " +
dgg::util::to_string(rf.aperture()) + " is not a perfect square",
DgBase::Fatal);
}
for (int i = 0; i < discRFS().nRes(); i++)
{
(*grids_)[i] = new DgBoundedRF2D(*rf.grids()[i], DgIVec2D(0, 0),
DgIVec2D(totTicks * numI - 1, totTicks * numJ - 1));
/*
cout << "grid " << i << endl;
for (DgIVec2D c = (*grids_)[i]->lowerLeft();
c != (*grids_)[i]->invalidAdd();
c = (*grids_)[i]->incrementAddress(c)) cout << c << endl;
*/
totTicks *= sqrtApp;
}
}
// set the correct last address
int maxRes = discRFS().nRes() - 1;
DgResAdd<DgIVec2D> last((*grids_)[maxRes]->lastAdd(), maxRes);
setLastAdd(last);
// set the size
size_ = 0;
for (int i = 0; i < discRFS().nRes(); i++)
{
std::uint64_t lastSize = size_;
const DgBoundedRF2D* g = (*grids_)[i];
if (g->validSize()) size_ += g->size();
if (!g->validSize() || size() < lastSize)
{
report("DgBoundedRFS2D::DgBoundedRFS2D() invalid size setting due to "
"possible overflow", DgBase::Warning);
validSize_ = false;
}
}
validSize_ = true;
} // DgBoundedRFS2D::DgBoundedRFS2D
////////////////////////////////////////////////////////////////////////////////
DgResAdd<DgIVec2D>&
DgBoundedRFS2D::incrementAddress (DgResAdd<DgIVec2D>& add) const
{
if (!validAddress(add)) return add = invalidAdd();
else if (add == lastAdd()) return add = endAdd();
else if (add == endAdd()) return add;
const DgBoundedRF2D& grid = *(grids()[add.res()]);
DgIVec2D v(add.address());
add = DgResAdd<DgIVec2D>(grid.incrementAddress(v), add.res());
if (add.address() == grid.endAdd())
{
if (add.res() == (discRFS().nRes() - 1)) return add = endAdd();
else
{
int newRes = add.res() + 1;
add = DgResAdd<DgIVec2D>(grids()[newRes]->firstAdd(), newRes);
}
}
return add;
/*
if (add.address() == grid.lastAdd() ||
add.address() == grid.endAdd())
{
if (add.res() == (discRFS().nRes() - 1)) return add = endAdd();
else
{
int newRes = add.res() + 1;
return add = DgResAdd<DgIVec2D>(grids()[newRes]->firstAdd(), newRes);
}
}
else
{
DgIVec2D v(add.address());
return add = DgResAdd<DgIVec2D>(grid.incrementAddress(v), add.res());
}
*/
} // DgResAdd<DgIVec2D>& DgBoundedRFS2D::incrementAddress
////////////////////////////////////////////////////////////////////////////////
DgResAdd<DgIVec2D>&
DgBoundedRFS2D::decrementAddress (DgResAdd<DgIVec2D>& add) const
{
if (!validAddress(add) || add == firstAdd()) return add = invalidAdd();
const DgBoundedRF2D& grid = *(grids()[add.res()]);
if (add.address() == grid.firstAdd())
{
int newRes = add.res() - 1;
return add = DgResAdd<DgIVec2D>(grids()[newRes]->lastAdd(), newRes);
}
else
{
DgIVec2D v(add.address());
return add = DgResAdd<DgIVec2D>(grid.decrementAddress(v), add.res());
}
} // DgResAdd<DgIVec2D>& DgBoundedRFS2D::decrementAddress
////////////////////////////////////////////////////////////////////////////////
std::uint64_t
DgBoundedRFS2D::seqNumAddress (const DgResAdd<DgIVec2D>& add) const
{
if (!validSize())
{
report("DgBoundedRFS2D::seqNumAddress() valid size required",
DgBase::Fatal);
return 0;
}
std::uint64_t sNum = 0;
if (!zeroBased()) sNum++;
for (int i = 0; i < add.res(); i++) sNum += (*grids_)[i]->size();
sNum += (*grids_)[add.res()]->seqNumAddress(add.address());
return sNum;
} // std::uint64_t DgBoundedRFS2D::seqNumAddress
////////////////////////////////////////////////////////////////////////////////
DgResAdd<DgIVec2D>
DgBoundedRFS2D::addFromSeqNum (std::uint64_t sNum) const
{
if (!validSize())
{
report("DgBoundedRFS2D::seqNumAddress() valid size required",
DgBase::Fatal);
return DgResAdd<DgIVec2D>();
}
if (!zeroBased()) sNum--;
DgResAdd<DgIVec2D> tmp;
std::uint64_t n = sNum;
tmp.setRes(0);
int r;
for (r = 0; r < discRFS().nRes(); r++)
{
tmp.setRes(r);
if (n < (*grids_)[r]->size()) break;
n -= (*grids_)[r]->size();
}
if (r == discRFS().nRes()) return invalidAdd();
tmp.setAddress((*grids_)[tmp.res()]->addFromSeqNum(n));
return tmp;
} // DgResAdd<DgIVec2D> DgBoundedRFS2D::addFromSeqNum
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////