16 #ifndef POPS_SPREAD_RATE_HPP
17 #define POPS_SPREAD_RATE_HPP
30 template<
typename Raster>
37 double west_east_resolution;
39 double north_south_resolution;
41 std::vector<BBoxInt> boundaries;
42 std::vector<BBoxFloat> rates;
52 bool bn =
false, bs =
false, be =
false, bw =
false;
53 std::tie(n, s, e, w) = bbox;
56 if (s == (height - 1))
63 return std::make_tuple(bn, bs, be, bw);
71 const Raster& raster,
const std::vector<std::vector<int>>& suitable_cells)
78 for (
auto indices : suitable_cells) {
81 auto value = raster(i, j);
95 return std::make_tuple(n, s, e, w);
97 return std::make_tuple(-1, -1, -1, -1);
104 bool is_boundary_valid(
const BBoxInt bbox)
107 std::tie(n, s, e, w) = bbox;
119 const std::vector<std::vector<int>>& suitable_cells)
120 : width(raster.cols()),
121 height(raster.rows()),
122 west_east_resolution(ew_res),
123 north_south_resolution(ns_res),
124 num_steps(num_steps),
125 boundaries(num_steps + 1, std::make_tuple(0, 0, 0, 0)),
128 std::make_tuple(std::nan(
""), std::nan(
""), std::nan(
""), std::nan(
"")))
130 boundaries.at(0) = infection_boundary(raster, suitable_cells);
154 const std::vector<std::vector<int>>& suitable_cells)
156 BBoxInt bbox = infection_boundary(raster, suitable_cells);
157 boundaries.at(step + 1) = bbox;
158 if (!is_boundary_valid(bbox)) {
160 std::make_tuple(std::nan(
""), std::nan(
""), std::nan(
""), std::nan(
""));
163 int n1, n2, s1, s2, e1, e2, w1, w2;
164 std::tie(n1, s1, e1, w1) = boundaries.at(step);
165 std::tie(n2, s2, e2, w2) = bbox;
166 double n_rate = ((n1 - n2) * north_south_resolution);
167 double s_rate = ((s2 - s1) * north_south_resolution);
168 double e_rate = ((e2 - e1) * west_east_resolution);
169 double w_rate = ((w1 - w2) * west_east_resolution);
172 std::tie(bn, bs, be, bw) = is_out_of_bounds(bbox);
173 if (n_rate == 0 && bn)
174 n_rate = std::nan(
"");
175 if (s_rate == 0 && bs)
176 s_rate = std::nan(
"");
177 if (e_rate == 0 && be)
178 e_rate = std::nan(
"");
179 if (w_rate == 0 && bw)
180 w_rate = std::nan(
"");
182 rates.at(step) = std::make_tuple(n_rate, s_rate, e_rate, w_rate);
191 template<
typename Raster>
197 int size_n = 0, size_s = 0, size_e = 0, size_w = 0;
198 double avg_n = 0, avg_s = 0, avg_e = 0, avg_w = 0;
199 for (
unsigned i = 0; i < rates.size(); i++) {
200 std::tie(n, s, e, w) = rates[i].step_rate(step);
201 if (!std::isnan(n)) {
205 if (!std::isnan(s)) {
209 if (!std::isnan(e)) {
213 if (!std::isnan(w)) {
218 avg_n = size_n ? avg_n / size_n : std::nan(
"");
219 avg_s = size_s ? avg_s / size_s : std::nan(
"");
220 avg_e = size_e ? avg_e / size_e : std::nan(
"");
221 avg_w = size_w ? avg_w / size_w : std::nan(
"");
223 return std::make_tuple(avg_n, avg_s, avg_e, avg_w);
227 #endif // POPS_SPREAD_RATE_HPP