MyGridMap.py
Go to the documentation of this file.
1 #ros library
2 import roslib
3 import rospy
4 import copy
5 from nav_msgs.msg import OccupancyGrid
6 
7 #Other Libary
8 
9 import copy
10 import io
11 from PIL import Image
12 
13 class MyGridMap:
14  def __init__(self,grid):
15  self.occupancyGrid = grid
16  self.mapWidth = grid.info.width
17  self.mapHeight = grid.info.height
18  self.lethalCost = 100 #Value for the cell with an obstacle. 0 Free cell, -1 unknown
19 
20  def getWidth(self):
21  return self.mapWidth
22 
23  def getHeight(self):
24  return self.mapHeight
25 
26  def getSize(self):
27  return self.mapHeight * self.mapWidth
28 
29  def getResolution(self):
30  return self.occupancyGrid.info.resolution
31 
32  def getOriginX(self):
33  return self.occupancyGrid.info.origin.position.x
34 
35  def getOriginY(self):
36  return self.occupancyGrid.info.origin.position.y
37 
38  def getOccupancyGrid(self):
39  return self.occupancyGrid
40 
41  def getCoordinates(self,index):
42  if (index > (self.mapWidth * self.mapHeight)):
43  print ("Error, index too big!")
44  return -1, -1
45  else:
46  x = index/ self.mapWidth
47  y = index % self.mapWidth
48  return x,y
49 
50  def getIndex(self,x,y):
51  if(x > self.mapWidth or y > self.mapHeight):
52  print ("Error, get index failed")
53  return -1
54  else:
55  return (y * self.mapWidth + x)
56 
57  def getData(self,index):
58  if(index < (self.mapWidth * self.mapHeight) and index > 0):
59  return self.occupancyGrid.data[index]
60  else:
61  print ("Error, wrong index")
62  return -1
63 
64  def getData(self,x,y):
65  if(x < 0 or x > self.mapWidth or y < 0 or y > self.mapHeight):
66  return -1
67  return self.occupancyGrid.data[y * self.mapWidth + x]
68 
69  def isFree(self,index):
70  value = self.getData(index)
71  if(value >= 0 and value < self.lethalCost):
72  return True
73  else:
74  return False
75 
76 
77  def isFree(self,x,y):
78  if(x < 0 or x > self.mapWidth or y < 0 or y > self.mapHeight):
79  return False
80  else:
81  value = self.getData(x,y)
82  if(value >= 0 and value < self.lethalCost):
83  return True
84  else:
85  return False
86 
87  def isFrontier(self,index):
88  x = index / self.mapWidth
89  y = index % self.mapWidth
90  if(self.getData(x-1,y-1) == -1):
91  return True
92  if(self.getData(x-1,y ) == -1):
93  return True
94  if(self.getData(x-1,y+1) == -1):
95  return True
96  if(self.getData(x ,y-1) == -1):
97  return True
98  if(self.getData(x ,y+1) == -1):
99  return True
100  if(self.getData(x+1,y-1) == -1):
101  return True
102  if(self.getData(x+1,y ) == -1):
103  return True
104  if(self.getData(x+1,y+1) == -1):
105  return True
106  return False
107 
108  def isFrontier(self,x,y):
109  i = self.getIndex(x,y)
110  if( i == -1):
111  print("Wrong coordinate")
112  return False
113  else:
114  return self.isFrontier(i)
115 
116  def createPGM(self):
117  my_file = io.BytesIO()
118  pgmHeader ='P5' + '\n' + ' ' + str(self.mapWidth) + ' ' + str(self.mapHeight) + '\n' + ' ' + str(255) + '\n'
119  pgmHeader_byte = bytearray(pgmHeader,'utf-8')
120  my_file.write(pgmHeader_byte)
121  for y in range (0,self.mapHeight):
122  for x in range (0,self.mapWidth):
123  index = x + (self.mapHeight-y-1)*self.mapWidth
124  value = self.occupancyGrid.data[index]
125  if value == +100:
126  my_file.write((0).to_bytes(1,'big'))
127  elif value >= 0 and value <= self.lethalCost:
128  my_file.write((254).to_bytes(1,'big'))
129  else:
130  my_file.write((205).to_bytes(1,'big'))
131  my_file.truncate()
132  my_file.seek(0)
133  t_image = Image.open(my_file)
134  image = t_image.copy()
135  my_file.close()
136  return image
137 
138 
139  def areNeighbour(self,index1,index2):
140  x1,y1 = self.getCoordinates(index1)
141  x2,y2 = self.getCoordinates(index2)
142  if( x1 == -1 or x2 == -1):
143  print("Error, wrong Index!")
144  return False
145  if(x1 == x2 and y1 == y2):
146  return True
147  if(x1 == (x2-1) and y1 == (y2-1)):
148  return True
149  if(x1 == x2 and y1 == (y2-1)):
150  return True
151  if(x1 == (x2+1) and y1 == (y2-1)):
152  return True
153  if(x1 == (x2-1) and y1 == y2):
154  return True
155  if(x1 == (x2+1) and y1 == y2):
156  return True
157  if(x1 == (x2-1) and y1 == (y2+1)):
158  return True
159  if(x1 == x2 and y1 == (y2+1)):
160  return True
161  if(x1 == (x2+1) and y1 ==(y2+1)):
162  return True
163  return False
164 
src.MyGridMap.MyGridMap.getSize
def getSize(self)
Definition: MyGridMap.py:26
src.MyGridMap.MyGridMap.getData
def getData(self, index)
Definition: MyGridMap.py:57
src.MyGridMap.MyGridMap.getResolution
def getResolution(self)
Definition: MyGridMap.py:29
src.MyGridMap.MyGridMap.getOriginX
def getOriginX(self)
Definition: MyGridMap.py:32
src.MyGridMap.MyGridMap.occupancyGrid
occupancyGrid
Definition: MyGridMap.py:15
src.MyGridMap.MyGridMap.lethalCost
lethalCost
Definition: MyGridMap.py:18
src.MyGridMap.MyGridMap.isFrontier
def isFrontier(self, index)
Definition: MyGridMap.py:87
src.MyGridMap.MyGridMap.getHeight
def getHeight(self)
Definition: MyGridMap.py:23
src.MyGridMap.MyGridMap.getOccupancyGrid
def getOccupancyGrid(self)
Definition: MyGridMap.py:38
src.MyGridMap.MyGridMap.areNeighbour
def areNeighbour(self, index1, index2)
Definition: MyGridMap.py:139
src.MyGridMap.MyGridMap.getCoordinates
def getCoordinates(self, index)
Definition: MyGridMap.py:41
src.MyGridMap.MyGridMap.isFree
def isFree(self, index)
Definition: MyGridMap.py:69
src.MyGridMap.MyGridMap.getIndex
def getIndex(self, x, y)
Definition: MyGridMap.py:50
src.MyGridMap.MyGridMap.createPGM
def createPGM(self)
Definition: MyGridMap.py:116
src.MyGridMap.MyGridMap
Definition: MyGridMap.py:13
src.MyGridMap.MyGridMap.getOriginY
def getOriginY(self)
Definition: MyGridMap.py:35
src.MyGridMap.MyGridMap.__init__
def __init__(self, grid)
Definition: MyGridMap.py:14
src.MyGridMap.MyGridMap.getWidth
def getWidth(self)
Definition: MyGridMap.py:20
src.MyGridMap.MyGridMap.mapHeight
mapHeight
Definition: MyGridMap.py:17
src.MyGridMap.MyGridMap.mapWidth
mapWidth
Definition: MyGridMap.py:16


rose2
Author(s): Gabriele Somaschini, Matteo Luperto
autogenerated on Wed Jun 28 2023 02:21:53