I'm working on an robot that would be able to navigate through a maze, avoid obstacles and identify some of the objects (Boxes in which it has to pot the balls) in it. I have a monochromatic bitmap of the maze, that is supposed to be used in the robot navigation.
Up till now, I have converted/read the bitmap image of the maze into a 2D array of bits. Right now I am writing a code that should convert the 2D array (that represents the maze) into a connectivity map so that I could apply a path planning algorithm on it. Mr. @Chuck has helped me by providing a code in MATLAB. i have converted that code into C++, however the code isn't providing the right output. Kindly see the code and tell me what I am doing wrong.
I am sharing the link to the 2D array that has been made, the MATLAB code, and my code in C++ to convert the array into a connectivity map.
Link to the 2D array:-
https://drive.google.com/file/d/0BwUKS98DxycUZDZwTVYzY0lueFU/view?usp=sharing
MATLAB CODE:-
Map = load(map.mat);
nRows = size(Map,1);
nCols = size(Map,2);
mapSize = size(Map);
N = numel(Map);
Digraph = zeros(N, N);
for i = 1:nRows
for j = 1:nCols
currentPos = sub2ind(mapSize,i,j);
% left neighbor, if it exists
if (j-1)> 0
destPos = sub2ind (mapSize,i,j-1);
Digraph(currentPos,destPos) = Map(currentPos)*Map(destPos);
end
% right neighbor, if it exists
if (j+1)<=nCols
destPos = sub2ind (mapSize,i,j+1);
Digraph(currentPos,destPos) = Map(currentPos)*Map(destPos);
end
% top neighbor, if it exists
if (i-1)> 0
destPos = sub2ind (mapSize,i-1,j);
Digraph(currentPos,destPos) = Map(currentPos)*Map(destPos);
end
% bottom neighbor, if it exists
if (i+1)<=nRows
destPos = sub2ind (mapSize,i+1,j);
Digraph(currentPos,destPos) = Map(currentPos)*Map(destPos);
end
end
end
Code in C++:-
int **digraph = NULL;
digraph = new int *[6144];
for (int i = 0; i < 6144; i++)
{
digraph[i] = new int[6144];
}
for (j = 0; j < 96; j++)
{
for (z = 0; z < 64; z++)
{
currentPos = sub2ind[j][z];
digraph[currentPos][currentPos] = 0; //------NEW ADDITION-----------
if ((z - 1) >= 0)
{
destPos = sub2ind[j][z - 1];
digraph[currentPos][destPos] = bitarray[j][z] * bitarray[j][z - 1];
}
if ((z + 1) < 64)
{
destPos = sub2ind[j][z + 1];
digraph[currentPos][destPos] = bitarray[j][z] * bitarray[j][z + 1];
}
if ((j - 1) >= 0)
{
destPos = sub2ind[j - 1][z];
digraph[currentPos][destPos] = bitarray[j][z] * bitarray[j - 1][z];
}
if ((j + 1) < 96)
{
destPos = sub2ind[j + 1][z];
digraph[currentPos][destPos] = bitarray[j][z] * bitarray[j + 1][z];
}
}
}
ofstream connectivityMap;
connectivityMap.open("diGraph.txt");
for (int l = 0; j < 100; l++) // printing only 100 elements
{
for (int k = 0; k < 100; k++)
{
connectivityMap << digraph[l][k] << " ";
}
}