-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistogramGrid.h
355 lines (302 loc) · 10.5 KB
/
HistogramGrid.h
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
/**
* HistogramGrid.h
* \author Swapnil Pande
*/
#ifndef VECTORFIELDHISTOGRAMTESTING_HISTOGRAMGRID_H
#define VECTORFIELDHISTOGRAMTESTING_HISTOGRAMGRID_H
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
#include "Utils.h"
/**
* \brief Represents the Histrogram Grid, which is a collection of obstacle likelihood
* scores.
*
* When a sensor detects an obstacle that lies inside a square in the Histogram grid,
* that square's "obstacle likelihood" value is incremented by 1. This likelihood value
* affects the robot's willingness to traverse over it.
*
* \author Swapnil Pande
*/
class HistogramGrid {
private:
int iMax; ///< Size in the i direction of the histogram grid
int jMax; ///< Size in the j direction of the histogram grid
double nodeSize; ///< Side dimension of each node. Each node is a square
double** histGrid; ///< Histogram grid object. Stores the certainty values for all nodes in grid
int** objectGrid; ///< Stores the type of object that exists at each node
int iSizeActiveRegion; ///< i-dimension size of the active region measured in number of nodes
int jSizeActiveRegion; ///< j-dimension size of the active region
discretePoint robotLoc; ///< The coordinates of the robot
discretePoint target; ///< The coordinates of the robot's destination
public:
/**
* Creates a new histogram grid object with no objects present in the grid
*
* \param histWidth Width of the entire histogram in meters
* \param histLength Length of the entire histogram in meters
* \param nodeSideLen Side dimension of each node. histWidth and histLength
* should be a multiple of this number
*/
HistogramGrid(int histWidth, int histLength, double nodeSideLen) :
iMax(int(double(histWidth)/nodeSideLen) - 1),
jMax(int(double(histLength)/nodeSideLen) - 1),
nodeSize(nodeSideLen),
histGrid(new double*[iMax]),
objectGrid(new int*[iMax]),
iSizeActiveRegion(10),
jSizeActiveRegion(10) // why are these 10???
{
std::cout << "Initializing grid: iMax = " << iMax << ". jMax = " << jMax << "\n";
//Initializing the histGrid and objectGrid
for(int i = 0; i < iMax; i++)
{
histGrid[i] = new double[jMax];
objectGrid[i] = new int[jMax];
for(int j = 0; j < jMax; j++)
{
histGrid[i][j] = 0;
objectGrid[i][j] = 0;
}
}
// DEBUG!!!!
// manually add obstacles
objectGrid[iMax/2][jMax/2] = 1;
objectGrid[iMax/2-1][jMax/2-1] = 1;
objectGrid[iMax/2][jMax/2-1] = 1;
objectGrid[iMax/2-1][jMax/2] = 1;
}
// DEBUG!!!
// HistogramGrid
// Alternate constructor used for reading grid from file. Used only for testing
HistogramGrid(std::string fName, discretePoint robotLocIn)
{
std::cout << std::endl
<< std::endl
<< "testing histogram grid initialization: "
<< std::endl;
robotLoc = robotLocIn;
std::string data; //Temporary string to store ingested data
int histWidth;
int histLength;
std::ifstream file(fName);
if (file.is_open())
{
file >> data;
histWidth = std::stoi(data);
file >> data;
histLength = std::stoi(data);
file >> data;
nodeSize = std::stod(data);
iMax = (int)(histWidth/nodeSize);
jMax = (int)(histLength/nodeSize);
histGrid = new double*[iMax];
objectGrid = new int*[iMax];
iSizeActiveRegion = 20;
jSizeActiveRegion = 20;
for(int i = 0; i < iMax; i++)
{
histGrid[i] = new double[jMax];
objectGrid[i] = new int[jMax];
for(int j = 0; j < jMax; j++)
{
file >> data;
if(data == "1")
{
histGrid[i][j] = 1;
objectGrid[i][j] = 1;
}
else if(data == "2")
{
histGrid[i][j] = 0;
objectGrid[i][j] = 0;
target.x = i;
target.y = j;
}
else
{
histGrid[i][j] = 0;
objectGrid[i][j] = 0;
}
//std::cout << i << " " << j << " " << histGrid[i][j] << " " << objectGrid[i][j] << "\n";
}
}
file.close();
}
else {
std::cout << "Unable to open file";
}
}
HistogramGrid(const HistogramGrid &rhs) :
iMax(rhs.iMax),
jMax(rhs.jMax),
nodeSize(rhs.nodeSize),
histGrid(new double*[iMax]),
objectGrid(new int*[iMax]),
iSizeActiveRegion(rhs.iSizeActiveRegion),
jSizeActiveRegion(rhs.jSizeActiveRegion)
{
//Initializing the histGrid and objectGrid
for(int i = 0; i < iMax; i++)
{
histGrid[i] = new double[jMax];
objectGrid[i] = new int[jMax];
for(int j = 0; j < jMax; j++)
{
histGrid[i][j] = rhs.histGrid[i][j];
objectGrid[i][j] = rhs.objectGrid[i][j];
}
}
}
~HistogramGrid()
{
for(int i = 0; i < iMax;i++)
{
delete[] histGrid[i];
histGrid[i] = nullptr;
delete[] objectGrid[i];
objectGrid[i] = nullptr;
}
delete[] histGrid;
delete[] objectGrid;
histGrid = nullptr;
objectGrid = nullptr;
}
const HistogramGrid& operator=(const HistogramGrid &rhs)
{
if(this == &rhs)
{
return *this;
}
HistogramGrid tmp(rhs); //make a copy of the right hand side via copy constructor
//next swap the data between *this and tmp
std::swap(iMax, tmp.iMax);
std::swap(jMax, tmp.jMax);
std::swap(nodeSize, tmp.nodeSize);
std::swap(histGrid, tmp.histGrid);
std::swap(objectGrid, tmp.objectGrid);
std::swap(iSizeActiveRegion, tmp.iSizeActiveRegion);
std::swap(jSizeActiveRegion, tmp.jSizeActiveRegion);
return *this;
}
// TODO: Consider making this private. - Josh
// TODO: Also, this assumes that obstacles are going to fall in exactly
// one grid space. What if the obstacles had a radius and covered multiple
// grid spaces, so you wouldn't even need this method? You would just need
// one method that transforms your collection of obstacles into an occupied
// grid. - Josh
//getDiscretePointFromCont
//Calculates the cell in which an object lies based on its continuous (exact) coordinates
//Returns a discrete point struct
discretePoint getDiscretePointFromCont(const contPoint& pos) const
{
discretePoint out;
out.x = int(pos.x / nodeSize);
out.y = int(pos.y / nodeSize);
//if(out.x < iMax && out.y < jMax) return out;
//TODO: Handle out-of-bounds arguments
//throw;
return out;
}
//updateCertainty
//Updates the certainty value for the node at which the object is located
//contPoint - the continuous point at which object is located
//certainty - certainty value to set
void updateCertainty(contPoint pos, double certainty)
{
discretePoint objLoc = getDiscretePointFromCont(pos);
histGrid[objLoc.x][objLoc.y] = certainty;
}
// getCertainty
// Returns the certainty of an object being present at the given node
double getCertainty(discretePoint pos) const
{
return histGrid[pos.x][pos.y];
}
// Consider making private. - Josh
//getDistance
//Returns scalar distance between two discretePoints (pos1 & pos2) on the histogram grid
double getDistance(discretePoint pos1, discretePoint pos2) const
{
return sqrt(pow(double(pos2.x - pos1.x), 2) + pow(double(pos2.y - pos1.y), 2));
}
// Private?
// getAngle
// Returns the angle between the line between pos2 and posRef and the horizontal along
// positive i direction.
double getAngle(discretePoint posRef, discretePoint pos2) const
{
double out = atan2(double(pos2.y) - posRef.y, double(pos2.x) - posRef.x)*180/M_PI;
if(out < 0) out += 360;
return out;
}
region getActiveRegion() const
{
region activeRegion;
activeRegion.min.x = (robotLoc.x - iSizeActiveRegion / 2);
if(activeRegion.min.x < 0)
{
activeRegion.min.x = 0;
}
activeRegion.min.y = (robotLoc.y - jSizeActiveRegion / 2);
if(activeRegion.min.y < 0)
{
activeRegion.min.y = 0;
}
activeRegion.max.x = (robotLoc.x + (iSizeActiveRegion - iSizeActiveRegion/2));
if(activeRegion.max.x >= iMax)
{
activeRegion.max.x = iMax;
}
activeRegion.max.y = (robotLoc.x + (jSizeActiveRegion - jSizeActiveRegion/2));
if(activeRegion.max.y >= jMax)
{
activeRegion.max.y = jMax;
}
return activeRegion;
}
// TODO: Consider making this return a 'continuous' point. - Josh
//getRobotLoc
//Returns a discretePoint describing the current position of the robot
discretePoint getRobotLoc() const
{
return robotLoc;
}
// TODO: See above.
// getTargetLoc
// Returns a discretePoint describing the current position of the target
discretePoint getTargetLoc() const
{
return target;
}
// TODO: See above.
//setTargetLoc
//Returns a discretePoint describing the current position of the target
void setTargetLoc(discretePoint targetLoc)
{
target = targetLoc;
}
// TODO: See above.
void setRobotLoc(discretePoint robotLocIn)
{
robotLoc = robotLocIn;
}
int** getObjectGrid() const
{
return objectGrid;
}
int getCellValue(int i, int j) const
{
return objectGrid[i][j];
}
int getIMax() const
{
return iMax;
}
int getJMax() const
{
return jMax;
}
};
#endif //VECTORFIELDHISTOGRAMTESTING_HISTOGRAMGRID_H