Reputation: 53
I am trying to create a 10 by 10 grid with uint8_t values. using some coordinates I am trying to place 255 values at those indexes. When I substitute 255 at those coordinates it works fine but as soon as I try to print out the values at those indexes it doesn't seem to be the values I wanted.
float grid_width = 10;
float grid_height = 10;
float grid_resolution = 0.05;
float rows = grid_width/grid_resolution;
float cols = grid_height/grid_resolution;
std::vector<std::vector<float>> landmarks = {{2,2},{8,8},{2,8},{8,2},{3,2},{3.5,2.1},{4.0,3.5},{4.5,2.0},{5.0,3.0}};
std::vector<std::vector<uint8_t>> grid(rows, std::vector<uint8_t>(cols, 0));
for(auto i: landmarks)
{
for(auto m = i[0]-(grid_resolution*2) ; m <= i[0] +(grid_resolution*2) ; m=m+grid_resolution )
{
for(auto n = i[1]-(grid_resolution*2); n <= i[1]+(grid_resolution*2); n=n+grid_resolution)
{
int m_co = m/grid_resolution;
int n_co = n/grid_resolution;
grid[m_co][n_co] = 255;
cout<<"m_co: "<< m_co <<" n_co:" << n_co <<endl;
cout<<"grid[m_co][n_co]: "<< unsigned(grid[m_co][n_co]) <<endl; //check the printout over here for index values of 18,22 you will see 255 but when you try it outside the for loops those values are rest to 0
}
}
}
// for(auto m: grid)
// {
// for(auto n: m)
// {
// cout<<unsigned(n)<<" ";
// }
// cout<<endl;
// }
cout<<unsigned(grid[18][18])<<endl; //expected 255 got 255
cout<<unsigned(grid[18][22])<<endl; //expected 255 got 0
cout<<unsigned(grid[22][22])<<endl; //expected 255 got 0
cout<<unsigned(grid[22][18])<<endl; //expected 255 got 0
cout<<unsigned(grid[17][17])<<endl; //expected 255 got 0
cout<<unsigned(grid[20][18])<<endl; //expected 255 got 255
So I am taking indexes using landmarks vector and trying to replace 0 from grid vector to 255 on the indexes of landmarks, but I also want to replace 0 on the indexes around that index. like if 2,2 is my coordinate I want to replace 1,1 and 1,2 and 1,3 and 2,1 and 2,2, and 2,3 and 3,1 and 3,2 and 3,3 to 255 but I do not get 1,3 and 2,3 and 3,3 255 i get 0 this is just an example I am trying to achive
Upvotes: 1
Views: 285
Reputation: 53
I was able to find the solution. the old logic was too complex and not working with different resolutions. So I made a simpler one that does need to take care of rounding up issues. first took each point and tried to go replace values index by index.
for(auto i: landmarks)
{
for(int m = (i[0]/grid_resolution)-2; m <= (i[0]/grid_resolution)+2; m++)
{
for(int n = (i[1]/grid_resolution)-2; n <= (i[1]/grid_resolution)+2; n++)
{
grid[m][n] = 255;
}
}
}
Upvotes: 0
Reputation: 3450
In the loop you are printing n/grid_resolution
. When it prints 22
, it is actually 21.99999809265, but because the default precision of iostream is 6, it is rounded to 22. On the other hand, when you assign it to int
, it is rounded down to 21
, so you actually store 255 in grid[18][21]
.
Upvotes: 2
Reputation: 1103
The problem is that m/grid_resolution has type float, while m_co has type int:
int main()
{
float grid_width = 10;
float grid_height = 10;
float grid_resolution = 0.1;
float rows = grid_width/grid_resolution;
float cols = grid_height/grid_resolution;
cout.precision(5);
std::vector<std::vector<float>> landmarks = {{2,2},{8,8},{2,8},{8,2},{3,2},{3.5,2.1},{4.0,3.5},{4.5,2.0},{5.0,3.0}};
std::vector<std::vector<uint8_t>> grid(rows, std::vector<uint8_t>(cols, 0));
for(auto i: landmarks)
{
for(auto m = i[0]-(grid_resolution*2) ; m <= i[0] +(grid_resolution*2) ; m=m+grid_resolution )
{
for(auto n = i[1]-(grid_resolution*2); n <= i[1]+(grid_resolution*2); n=n+grid_resolution)
{
int m_co = m/grid_resolution;
int n_co = n/grid_resolution;
grid[m_co][n_co] = 255;
// Uncomment the following two lines for showing that are different
//if (m_co != m/grid_resolution || n_co != n/grid_resolution)
// cout << "different" << endl;
cout << "grid["<<m_co<<"]["<<n_co<<"]: "<< unsigned(grid[m_co][n_co]) << endl;
}
}
}
// for(auto m: grid)
// {
// for(auto n: m)
// {
// cout<<unsigned(n)<<" ";
// }
// cout<<endl;
// }
cout<<"grid[18][18]" << unsigned(grid[18][18])<<endl;
cout<<"grid[18][22]" << unsigned(grid[18][22])<<endl;
cout<<"grid[22][22]" << unsigned(grid[22][22])<<endl;
cout<<"grid[22][18]" << unsigned(grid[22][18])<<endl;
cout<<"grid[17][17]" << unsigned(grid[17][17])<<endl;
cout<<"grid[20][18]" << unsigned(grid[20][18])<<endl;
return 0;
}
If you run that code, the right values of m_co
and n_co
will be printed.
Best regards.
Upvotes: 1