-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRadiation.cpp
128 lines (106 loc) · 3.2 KB
/
Radiation.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
#include <Rcpp.h>
using namespace Rcpp;
#include <cmath>
// Helper function to sum matrix elements within a specific range using a loop
NumericVector sumMatrixRange(NumericMatrix popMat, int col, int rowL, int rowU) {
int rowMax = popMat.nrow();
if (rowL < 1) rowL = 1;
if (rowU > rowMax) rowU = rowMax;
// Adjust 1-based indexing to 0-based for C++
col = col - 1;
rowL = rowL - 1;
rowU = rowU - 1;
int counter = rowU - rowL;
double total = 0.0;
// Loop through the y range and sum up the values in the matrix
for (int row = rowL; row <= rowU; ++row) {
total += popMat(row, col);
}
NumericVector result = NumericVector::create(total, counter);
return result;
}
// [[Rcpp::export]]
double computePop(int rowO, int colO, int r, NumericMatrix popMat) {
int colMax = popMat.ncol();
double t1 = r / 16.0;
double t2 = 0;
int x = r;
int y = 0;
IntegerVector p1 = {r, 0};
IntegerVector p2 = {0, r};
NumericVector done(r + 1, false);
NumericVector temp(2, 0.0);
double out = 0.0;
// Sum for the initial column at xO (the center of the circle)
int counter = 0;
temp = sumMatrixRange(popMat, colO, rowO - r, rowO + r);
out += temp[0];
counter += temp[1];
while (x >= y) {
IntegerVector p1New = {x, y};
IntegerVector p2New = {y, x};
if (p1New[0] != p1[0]) {
if ((colO + p1[0]) <= colMax) {
temp = sumMatrixRange(popMat, colO + p1[0], rowO - p1[1], rowO + p1[1]);
out += temp[0];
counter += temp[1];
}
if ((colO - p1[0]) > 0) {
temp = sumMatrixRange(popMat, colO - p1[0], rowO - p1[1], rowO + p1[1]);
out += temp[0];
counter += temp[1];
}
done[p1[0]] = true;
}
if ((colO + p2New[0]) <= colMax) {
temp = sumMatrixRange(popMat, colO + p2New[0], rowO - p2New[1], rowO + p2New[1]);
out += temp[0];
counter += temp[1];
}
if ((colO - p2New[0]) > 0) {
temp = sumMatrixRange(popMat, colO - p2New[0], rowO - p2New[1], rowO + p2New[1]);
out += temp[0];
counter += temp[1];
}
done[p2New[0]] = true;
p1 = p1New;
p2 = p2New;
y = y + 1;
t1 = t1 + y;
t2 = t1 - x;
if (t2 >= 0) {
t1 = t2;
x = x - 1;
}
}
// Final iteration to make sure no points are left unsummed
IntegerVector p1New = {x, y};
IntegerVector p2New = {y, x};
if (p1New[0] != p1[0] && !done[p1[0]]) {
if ((colO + p1[0]) <= colMax) {
temp = sumMatrixRange(popMat, colO + p1[0], rowO - p1[1], rowO + p1[1]);
out += temp[0];
counter += temp[1];
}
if ((colO - p1[0]) > 0) {
temp = sumMatrixRange(popMat, colO - p1[0], rowO - p1[1], rowO + p1[1]);
out += temp[0];
counter += temp[1];
}
done[p1[0]] = true;
}
if (!done[p2New[0]]) {
if ((colO + p2New[0]) <= colMax) {
temp = sumMatrixRange(popMat, colO + p2New[0], rowO - p2New[1], rowO + p2New[1]);
out += temp[0];
counter += temp[1];
}
if ((colO - p2New[0]) > 0) {
temp = sumMatrixRange(popMat, colO - p2New[0], rowO - p2New[1], rowO + p2New[1]);
out += temp[0];
counter += temp[1];
}
}
out = out / counter * r * r * M_PI;
return out;
}