Skip to content

Commit 5ed1df9

Browse files
author
Dejan Trajkovic
committed
feat: Initial commit
0 parents  commit 5ed1df9

15 files changed

+1053
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Translate Python to C++
2+
In this project I applied my knowledge of C++ syntax by translating the Histogram Filter code from project 2 of 5 into C++.

a.out

130 KB
Binary file not shown.

debugging_helpers.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
debugging_helpers.cpp
3+
4+
Purpose: helper functions for debugging when working
5+
with grids of floats and chars.
6+
*/
7+
8+
#include <vector>
9+
#include "debugging_helpers.h"
10+
11+
using namespace std;
12+
13+
/**
14+
Displays a grid of beliefs. Does not return.
15+
16+
@param grid - a two dimensional grid (vector of
17+
vectors of floats) which will usually
18+
represent a robot's beliefs.
19+
*/
20+
void show_grid(vector < vector <float> > grid) {
21+
int i, j;
22+
float p;
23+
vector<float> row;
24+
for (i = 0; i < grid.size(); i++)
25+
{
26+
row = grid[i];
27+
for (j=0; j< row.size(); j++)
28+
{
29+
p = row[j];
30+
cout << p << ' ';
31+
}
32+
cout << endl;
33+
}
34+
}
35+
36+
/**
37+
Displays a grid map of the world
38+
*/
39+
void show_grid(vector < vector <char> > map) {
40+
int i, j;
41+
char p;
42+
vector<char> row;
43+
for (i = 0; i < map.size(); i++)
44+
{
45+
row = map[i];
46+
for (j=0; j< row.size(); j++)
47+
{
48+
p = row[j];
49+
cout << p << ' ';
50+
}
51+
cout << endl;
52+
}
53+
}
54+

debugging_helpers.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef DEBUGGING_HELPERS_H
2+
#define DEBUGGING_HELPERS_H
3+
4+
#include <vector>
5+
6+
// Displays a grid of beliefs. Does not return.
7+
void show_grid(std::vector < std::vector <float> > grid);
8+
9+
// Displays a grid map of the world
10+
void show_grid(std::vector < std::vector <char> > map);
11+
12+
#endif /* DEBUGGING_HELPERS_H */

helpers.cpp

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
/**
2+
helpers.cpp
3+
4+
Purpose: helper functions which are useful when
5+
implementing a 2-dimensional histogram filter.
6+
7+
This file is incomplete! Your job is to make the
8+
normalize and blur functions work. Feel free to
9+
look at helper.py for working implementations
10+
which are written in python.
11+
*/
12+
13+
#include <vector>
14+
#include <iostream>
15+
#include <cmath>
16+
#include <string>
17+
#include <fstream>
18+
#include "helpers.h"
19+
// #include "debugging_helpers.cpp"
20+
21+
using namespace std;
22+
23+
/**
24+
TODO - implement this function
25+
26+
Normalizes a grid of numbers.
27+
28+
@param grid - a two dimensional grid (vector of vectors of floats)
29+
where each entry represents the unnormalized probability
30+
associated with that grid cell.
31+
32+
@return - a new normalized two dimensional grid where the sum of
33+
all probabilities is equal to one.
34+
*/
35+
vector< vector <float> > normalize(vector< vector <float> > grid) {
36+
// todo - your code here
37+
vector< vector <float> > newGrid;
38+
int height = grid.size();
39+
int width = grid[0].size();
40+
newGrid = zeros(height, width);
41+
int total;
42+
total = height*width;
43+
for (int i = 0; i < grid.size(); i++) {
44+
for (int j = 0; j < grid[i].size(); j++) {
45+
newGrid[i][j] = grid[i][j] / total;
46+
}
47+
}
48+
return newGrid;
49+
}
50+
51+
/**
52+
TODO - implement this function.
53+
54+
Blurs (and normalizes) a grid of probabilities by spreading
55+
probability from each cell over a 3x3 "window" of cells. This
56+
function assumes a cyclic world where probability "spills
57+
over" from the right edge to the left and bottom to top.
58+
59+
EXAMPLE - After blurring (with blurring=0.12) a localized
60+
distribution like this:
61+
62+
0.00 0.00 0.00
63+
0.00 1.00 0.00
64+
0.00 0.00 0.00
65+
66+
would look like this:
67+
68+
0.01 0.02 0.01
69+
0.02 0.88 0.02
70+
0.01 0.02 0.01
71+
72+
@param grid - a two dimensional grid (vector of vectors of floats)
73+
where each entry represents the unnormalized probability
74+
associated with that grid cell.
75+
76+
@param blurring - a floating point number between 0.0 and 1.0
77+
which represents how much probability from one cell
78+
"spills over" to it's neighbors. If it's 0.0, then no
79+
blurring occurs.
80+
81+
@return - a new normalized two dimensional grid where probability
82+
has been blurred.
83+
*/
84+
vector < vector <float> > blur(vector < vector < float> > grid, float blurring) {
85+
// your code here
86+
int height;
87+
height = grid.size();
88+
int width;
89+
width = grid[0].size();
90+
91+
float center_prob;
92+
center_prob = 1.0-blurring;
93+
float corner_prob;
94+
corner_prob = blurring / 12.0;
95+
float adjacent_prob;
96+
adjacent_prob = blurring / 6.0;
97+
98+
vector < vector <float> > window;
99+
vector<float> firstrow;
100+
firstrow.push_back(corner_prob);
101+
firstrow.push_back(adjacent_prob);
102+
firstrow.push_back(corner_prob);
103+
vector<float> secondrow;
104+
secondrow.push_back(adjacent_prob);
105+
secondrow.push_back(center_prob);
106+
secondrow.push_back(adjacent_prob);
107+
vector<float> thirdrow;
108+
thirdrow.push_back(corner_prob);
109+
thirdrow.push_back(adjacent_prob);
110+
thirdrow.push_back(corner_prob);
111+
window.push_back(firstrow);
112+
window.push_back(secondrow);
113+
window.push_back(thirdrow);
114+
115+
vector < vector <float> > newGrid;
116+
vector<float> singlerow (width,0.0);
117+
for (int i = 0; i < height; i++) {
118+
newGrid.push_back(singlerow);
119+
}
120+
for (int i = 0; i < height; i++) {
121+
for (int j = 0; j < width; j++) {
122+
float grid_val;
123+
grid_val = grid[i][j];
124+
for (int dx = -1; dx < 2; dx++) {
125+
for (int dy = -1; dy < 2; dy++) {
126+
float mult;
127+
mult = window[dx+1][dy+1];
128+
int new_i;
129+
int new_j;
130+
new_i = (((i + dy) % height) + height) % height;
131+
new_j = (((j + dx) % width) + width) % width;
132+
newGrid[new_i][new_j] += mult * grid_val;
133+
}
134+
}
135+
}
136+
}
137+
return newGrid;
138+
}
139+
140+
/** -----------------------------------------------
141+
#
142+
#
143+
# You do not need to modify any code below here.
144+
#
145+
#
146+
# ------------------------------------------------- */
147+
148+
149+
/**
150+
Determines when two grids of floating point numbers
151+
are "close enough" that they should be considered
152+
equal. Useful for battling "floating point errors".
153+
154+
@param g1 - a grid of floats
155+
156+
@param g2 - a grid of floats
157+
158+
@return - A boolean (True or False) indicating whether
159+
these grids are (True) or are not (False) equal.
160+
*/
161+
bool close_enough(vector < vector <float> > g1, vector < vector <float> > g2) {
162+
int i, j;
163+
float v1, v2;
164+
if (g1.size() != g2.size()) {
165+
return false;
166+
}
167+
168+
if (g1[0].size() != g2[0].size()) {
169+
return false;
170+
}
171+
for (i=0; i<g1.size(); i++) {
172+
for (j=0; j<g1[0].size(); j++) {
173+
v1 = g1[i][j];
174+
v2 = g2[i][j];
175+
if (abs(v2-v1) > 0.0001 ) {
176+
return false;
177+
}
178+
}
179+
}
180+
return true;
181+
}
182+
183+
bool close_enough(float v1, float v2) {
184+
if (abs(v2-v1) > 0.0001 ) {
185+
return false;
186+
}
187+
return true;
188+
}
189+
190+
/**
191+
Helper function for reading in map data
192+
193+
@param s - a string representing one line of map data.
194+
195+
@return - A row of chars, each of which represents the
196+
color of a cell in a grid world.
197+
*/
198+
vector <char> read_line(string s) {
199+
vector <char> row;
200+
201+
size_t pos = 0;
202+
string token;
203+
string delimiter = " ";
204+
char cell;
205+
206+
while ((pos = s.find(delimiter)) != std::string::npos) {
207+
token = s.substr(0, pos);
208+
s.erase(0, pos + delimiter.length());
209+
210+
cell = token.at(0);
211+
row.push_back(cell);
212+
}
213+
214+
return row;
215+
}
216+
217+
/**
218+
Helper function for reading in map data
219+
220+
@param file_name - The filename where the map is stored.
221+
222+
@return - A grid of chars representing a map.
223+
*/
224+
vector < vector <char> > read_map(string file_name) {
225+
ifstream infile(file_name);
226+
vector < vector <char> > map;
227+
if (infile.is_open()) {
228+
229+
char color;
230+
vector <char> row;
231+
232+
string line;
233+
234+
while (std::getline(infile, line)) {
235+
row = read_line(line);
236+
map.push_back(row);
237+
}
238+
}
239+
return map;
240+
}
241+
242+
/**
243+
Creates a grid of zeros
244+
245+
For example:
246+
247+
zeros(2, 3) would return
248+
249+
0.0 0.0 0.0
250+
0.0 0.0 0.0
251+
252+
@param height - the height of the desired grid
253+
254+
@param width - the width of the desired grid.
255+
256+
@return a grid of zeros (floats)
257+
*/
258+
vector < vector <float> > zeros(int height, int width) {
259+
int i, j;
260+
vector < vector <float> > newGrid;
261+
vector <float> newRow;
262+
263+
for (i=0; i<height; i++) {
264+
newRow.clear();
265+
for (j=0; j<width; j++) {
266+
newRow.push_back(0.0);
267+
}
268+
newGrid.push_back(newRow);
269+
}
270+
return newGrid;
271+
}
272+
273+
// int main() {
274+
// vector < vector < char > > map = read_map("maps/m1.txt");
275+
// show_grid(map);
276+
// return 0;
277+
// }

helpers.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef HELPERS_H
2+
#define HELPERS_H
3+
4+
#include <vector>
5+
#include <string>
6+
7+
// Normalizes a grid of numbers.
8+
std::vector< std::vector<float> > normalize(std::vector< std::vector <float> > grid);
9+
10+
/**
11+
Blurs (and normalizes) a grid of probabilities by spreading
12+
probability from each cell over a 3x3 "window" of cells. This
13+
function assumes a cyclic world where probability "spills
14+
over" from the right edge to the left and bottom to top.
15+
*/
16+
std::vector < std::vector <float> > blur(std::vector < std::vector < float> > grid, float blurring);
17+
18+
/**
19+
Determines when two grids of floating point numbers
20+
are "close enough" that they should be considered
21+
equal. Useful for battling "floating point errors".
22+
*/
23+
bool close_enough(std::vector < std::vector <float> > g1, std::vector < std::vector <float> > g2);
24+
25+
bool close_enough(float v1, float v2);
26+
27+
// Helper function for reading in map data
28+
std::vector <char> read_line(std::string s);
29+
30+
// Helper function for reading in map data
31+
std::vector < std::vector <char> > read_map(std::string file_name);
32+
33+
// Creates a grid of zeros
34+
std::vector < std::vector <float> > zeros(int height, int width);
35+
36+
#endif /* HELPERS_H */

0 commit comments

Comments
 (0)