Advertisement
Guest User

Untitled

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