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 | #ifndef DETECTORFIELD_H
#define DETECTORFIELD_H
#include "Representations/Perception/MultiChannelIntegralImage.h"
#include "Representations/Infrastructure/CameraInfo.h"
#include "Representations/Perception/CameraMatrix.h"
#include "Representations/Motion/MotionStatus.h"
#include "Tools/CameraGeometry.h"
class DetectorImage
{
public:
std::vector<Vector2i> edges;
DetectorImage() : edges(4) {
edges.resize(4);
}
DetectorImage(Vector2i bottomLeft, Vector2i bottomRight, Vector2i topLeft, Vector2i topRight) : edges(4) {
edges = {bottomLeft, bottomRight, topLeft, topRight};<--- Variable 'edges' is assigned in constructor body. Consider performing initialization in initialization list. [+]When an object of a class is created, the constructors of all member variables are called consecutively in the order the variables are declared, even if you don't explicitly write them to the initialization list. You could avoid assigning 'edges' a value by passing the value to the constructor in the initialization list.
}
void rectify() {
// rectifies detector so that edges look like
// (0) 2 --- 3 (x)
// | |
// (y) 0 --- 1
edges[0] = Vector2i(minX(), maxY());
edges[1] = Vector2i(maxX(), maxY());
edges[2] = Vector2i(minX(), minY());
edges[3] = Vector2i(maxX(), minY());
}
bool limit(int maxX, int maxY, int minX, int minY) {
rectify();
// check if rectangle is at least partially inside limits
if(edges[0].x > maxX || edges[1].x < minX) {
return false;
}
else if(edges[2].y > maxY || edges[0].y < minY) {
return false;
}
edges[0].x = edges[2].x = std::max(edges[0].x, minX);
edges[1].x = edges[3].x = std::min(edges[1].x, maxX);
edges[0].y = edges[1].y = std::min(edges[0].y, maxY);
edges[2].y = edges[3].y = std::max(edges[2].y, minY);
return true;
}
int area() {
int x = maxX() - minX();
int y = maxY() - minY();
return x*y;
}
int pixels() {
int x = maxX() - minX() + 1;
int y = maxY() - minY() + 1;
return x*y;
}
int minX() {
int minX = edges[0].x;
for (size_t i=1; i<4; i++) {
minX = std::min(minX, edges[i].x);
}
return minX;
}
int minY() {
int minY = edges[0].y;
for (size_t i=1; i<4; i++) {
minY = std::min(minY, edges[i].y);
}
return minY;
}
int maxX() {
int maxX = edges[0].x;
for (size_t i=1; i<4; i++) {
maxX = std::max(maxX, edges[i].x);
}
return maxX;
}
int maxY() {
int maxY = edges[0].y;
for (size_t i=1; i<4; i++) {
maxY = std::max(maxY, edges[i].y);
}
return maxY;
}
double green_density(const BallDetectorIntegralImage& integralImage) {
return density(integralImage, 1);
}
double green_density_left(const BallDetectorIntegralImage& integralImage) {
return density_left(integralImage, 1);
}
double green_density_right(const BallDetectorIntegralImage& integralImage) {
return density_right(integralImage, 1);
}
double density(const BallDetectorIntegralImage& integralImage, uint32_t c) {
int minX = this->minX() / integralImage.FACTOR;
int minY = this->minY() / integralImage.FACTOR;
int maxX = this->maxX() / integralImage.FACTOR;
int maxY = this->maxY() / integralImage.FACTOR;
return integralImage.getDensityForRect(minX, minY, maxX, maxY, c);
}
double density_left(const BallDetectorIntegralImage& integralImage, uint32_t c) {
int minX = this->minX() / integralImage.FACTOR;
int minY = this->minY() / integralImage.FACTOR;
int maxX = this->maxX() / integralImage.FACTOR;
int maxY = this->maxY() / integralImage.FACTOR;
int halfX = (maxX - minX) / 2;
return integralImage.getDensityForRect(minX, minY, maxX - halfX, maxY, c);
}
double density_right(const BallDetectorIntegralImage& integralImage, uint32_t c) {
int minX = this->minX() / integralImage.FACTOR;
int minY = this->minY() / integralImage.FACTOR;
int maxX = this->maxX() / integralImage.FACTOR;
int maxY = this->maxY() / integralImage.FACTOR;
int halfX = (maxX - minX) / 2;
return integralImage.getDensityForRect(minX + halfX, minY, maxX, maxY, c);
}
};
class DetectorField
{
public:
std::vector<Vector2d> edges;
DetectorField() : edges(4) {
edges.resize(4);
}
// FIX: Order of indices is currently only important for debug drawings
DetectorField(Vector2d bottomLeft, Vector2d bottomRight, Vector2d topLeft, Vector2d topRight) : edges(4) {
edges = {bottomLeft, bottomRight, topLeft, topRight};<--- Variable 'edges' is assigned in constructor body. Consider performing initialization in initialization list. [+]When an object of a class is created, the constructors of all member variables are called consecutively in the order the variables are declared, even if you don't explicitly write them to the initialization list. You could avoid assigning 'edges' a value by passing the value to the constructor in the initialization list.
}
bool projectOnImage(DetectorImage& detectorImage, const CameraMatrix& cameraMatrix, const naoth::CameraInfo& cameraInfo) {
for(size_t i=0; i<4; ++i) {
Vector2d& fieldPoint = edges[i];
Vector2i& pointInImage = detectorImage.edges[i];
//Vector3d cameraMatrixOffset = Vector3d(getCameraMatrix().translation.x, getCameraMatrix().translation.y, 0);
bool projectionSuccess = CameraGeometry::relativePointToImage(
cameraMatrix, cameraInfo,
//RotationMatrix::getRotationZ(cameraMatrix.rotation.getZAngle()) * Vector3d(fieldPoint.x, fieldPoint.y, 0),
Vector3d(fieldPoint.x, fieldPoint.y, 0),
pointInImage);
if(!projectionSuccess) {
return false;
}
}
detectorImage.rectify();
return true;
}
void createPreview(DetectorField& detectorField, const MotionStatus& motionStatus) {<--- Technically the member function 'DetectorField::createPreview' can be const. [+]The member function 'DetectorField::createPreview' can be made a const function. Making this function 'const' should not cause compiler errors. Even though the function can be made const function technically it may not make sense conceptually. Think about your design and the task of the function first - is it a function that must not change object internal state?
for(size_t i=0; i<4; i++) {
detectorField.edges[i] = motionStatus.plannedMotion.hip * edges[i];
}
}
double minX() {
double minX = edges[0].x;
for (size_t i=1; i<4; i++) {
minX = std::min(minX, edges[i].x);
}
return minX;
}
double minY() {
double minY = edges[0].y;
for (size_t i=1; i<4; i++) {
minY = std::min(minY, edges[i].y);
}
return minY;
}
double maxX() {
double maxX = edges[0].x;
for (size_t i=1; i<4; i++) {
maxX = std::max(maxX, edges[i].x);
}
return maxX;
}
double maxY() {
double maxY = edges[0].y;
for (size_t i=1; i<4; i++) {
maxY = std::max(maxY, edges[i].y);
}
return maxY;
}
};
#endif // DETECTORFIELD_H
|