MsgUtils.py
Go to the documentation of this file.
1 import array
2 
3 from nav_msgs.msg import OccupancyGrid
4 from nav_msgs.msg import MapMetaData
5 from std_msgs.msg import Header
6 from geometry_msgs.msg import Point
7 from geometry_msgs.msg import Quaternion
8 from geometry_msgs.msg import PolygonStamped
9 from geometry_msgs.msg import Polygon
10 from geometry_msgs.msg import Point32
11 from visualization_msgs.msg import Marker
12 from rose2.msg import Edge
13 from rose2.msg import Room
14 from rose2.msg import Contour
15 from rose2.msg import ExtendedLine
16 from scipy.spatial.transform import Rotation
17 import numpy as np
18 import random
19 import rospy
20 import pickle
21 from MyGridMap import MyGridMap
22 import rospy
23 from matplotlib import pyplot as plt
24 from PIL import Image
25 import cv2
26 #Utility file used to :
27 # - convert messages to data structures and viceversa
28 # - create messages
29 
30 """
31 OccupancyGrid -> np.array
32 Use this if the OccupancyGrid is in the standard form: values are 0,-1 or 100 (mode: trinary of yaml format)
33 """
34 def fromOccupancyGridToImg(occupancyGrid):
35  gridMap = MyGridMap(occupancyGrid)
36  map = np.zeros((gridMap.mapHeight, int(gridMap.mapWidth)))
37  height, width = map.shape
38  for i in range(height):
39  for j in range(width):
40  val = gridMap.getData(j, i)
41  if val == 0:
42  map[i,j] = 255
43  if val == -1:
44  map[i,j] = 200
45  if val == 100:
46  map[i,j] = 0
47  map = np.array(map)
48  map = (map).astype(np.uint8)
49  return map
50 """
51 OccupancyGrid -> np.array
52 Use this if the OccupancyGrid has pixel values between 0 and 255 (that corresponds to mode: raw of yaml format)
53 """
54 def fromOccupancyGridRawToImg(occupancyGrid):
55  gridMap = MyGridMap(occupancyGrid)
56  map = np.zeros((gridMap.mapHeight, int(gridMap.mapWidth)))
57  height, width = map.shape
58  for i in range(height):
59  for j in range(width):
60  map[i, j] = gridMap.getData(j, i)
61  map = np.array(map)
62  map = (map).astype(np.uint8)
63  return map
64 
65 # np.array -> OccupancyGrid wth raw values [0,255]
66 def fromImgMapToOccupancyGridRaw(imgMap, origin,res):
67  grid = OccupancyGrid()
68  grid.header = Header()
69  grid.header.frame_id = "map"
70  grid.info = MapMetaData()
71  grid.info.resolution = res
72  grid.info.height = imgMap.shape[0]
73  grid.info.width = imgMap.shape[1]
74  grid.data = list(imgMap.copy().ravel())
75  grid.info.origin = origin
76  return grid
77 
78 # ExtendedSegment -> ExtendedLine.msg
80  extLineMsg = ExtendedLine()
81  d = array.array('b', (pickle.dumps(line)))
82  extLineMsg.bytes = d
83  return extLineMsg
84 
85 # Segment -> Edge.msg
87  edgeMsg = Edge()
88  d = array.array('b', (pickle.dumps(e)))
89  edgeMsg.bytes = d
90  return edgeMsg
91 # room (shapely polygon) -> Room.msg
92 def make_room_msg(room):
93  roomMsg = Room()
94  d = array.array('b',(pickle.dumps(room)))
95  roomMsg.bytes = d
96  return roomMsg
97 # array of float32 -> Contour
98 def make_contour_msg(vertices):
99  contour = Contour()
100  contour.vertices = np.ravel(vertices)
101  return contour
102 
103 # Segment, id -> Marker
104 def make_edge_marker(edge, i, origin,res):
105  marker = Marker()
106  marker.header.frame_id = "map"
107  marker.header.stamp = rospy.Time.now()
108  marker.ns = "EDGES-%u" % 0
109  marker.id = i
110  marker.type = 5
111  marker.action = 0
112  marker.pose.position = origin.position
113  marker.scale.x = res
114  marker.color.a = 1
115  marker.color.r = random.uniform(0,1)
116  marker.color.g = random.uniform(0,1)
117  marker.color.b = random.uniform(0,1)
118  p1 = Point()
119  p2 = Point()
120  p1.x = edge.x1*res
121  p1.y = edge.y1*res
122  p2.x = edge.x2*res
123  p2.y = edge.y2*res
124  marker.points.append(p1)
125  marker.points.append(p2)
126  return marker
127 
128 def make_lines_marker(lines, origin,res):
129  marker = Marker()
130  marker.header.frame_id = "map"
131  marker.header.stamp = rospy.Time.now()
132  marker.ns = "LINES"
133  marker.id = 50
134  marker.type = 5
135  marker.action = 0
136  marker.pose.position = origin.position
137  marker.scale.x = res
138  marker.color.a = 1
139  marker.color.r = 1
140  for l in lines:
141  p1 = Point()
142  p2 = Point()
143  p1.x = l.x1*res
144  p1.y = l.y1*res
145  p2.x = l.x2*res
146  p2.y = l.y2*res
147  marker.points.append(p1)
148  marker.points.append(p2)
149  return marker
150 def make_room_polygon(room, origin, res):
151  origin_x = origin.position.x
152  origin_y = origin.position.y
153  points = []
154  points_x, points_y = room.exterior.coords.xy
155  for i in range(len(points_x)):
156  p = Point32()
157  p.x = points_x[i]*res + origin_x
158  p.y = points_y[i]*res + origin_y
159  points.append(p)
160  polygon = Polygon()
161  polygon.points = points
162  polygonStamped = PolygonStamped()
163  polygonStamped.polygon = polygon
164  polygonStamped.header.frame_id = "map"
165  polygonStamped.header.stamp = rospy.Time.now()
166  return polygonStamped
167 def make_direction_markers(main_directions, origin, res):
168  directions = []
169  markers = []
170  for i in range(0, len(main_directions), 2):
171  directions.append((main_directions[i], main_directions[i + 1]))
172  i = 0
173  i = 0
174  for d in directions:
175  marker = Marker()
176  marker.header.frame_id = "map"
177  marker.header.stamp = rospy.Time.now()
178  marker.ns = "DIR-%u" % i
179  marker.id = i
180  marker.type = 0
181  marker.action = 0
182  #marker.pose.position = origin.position
183 
184  rot = Rotation.from_euler('xyz', [d[0], 0, d[1]])
185  rot_quat = rot.as_quat()
186  marker.pose.orientation = Quaternion(rot_quat[0], rot_quat[1], rot_quat[2], rot_quat[3])
187  marker.scale.x = 5
188  marker.scale.y = res
189  marker.scale.z = res
190  marker.color.a = 1.0
191  marker.color.r = 0.0
192  marker.color.g = 1.0
193  marker.color.b = 0.0
194  i += 1
195  markers.append(marker)
196  return markers
MsgUtils.make_contour_msg
def make_contour_msg(vertices)
Definition: MsgUtils.py:98
MsgUtils.make_room_msg
def make_room_msg(room)
Definition: MsgUtils.py:92
MsgUtils.make_extendedline_msg
def make_extendedline_msg(line)
Definition: MsgUtils.py:79
MsgUtils.make_edge_msg
def make_edge_msg(e)
Definition: MsgUtils.py:86
MsgUtils.make_room_polygon
def make_room_polygon(room, origin, res)
Definition: MsgUtils.py:150
MsgUtils.make_edge_marker
def make_edge_marker(edge, i, origin, res)
Definition: MsgUtils.py:104
MsgUtils.fromImgMapToOccupancyGridRaw
def fromImgMapToOccupancyGridRaw(imgMap, origin, res)
Definition: MsgUtils.py:66
MsgUtils.fromOccupancyGridRawToImg
def fromOccupancyGridRawToImg(occupancyGrid)
Definition: MsgUtils.py:54
MsgUtils.make_direction_markers
def make_direction_markers(main_directions, origin, res)
Definition: MsgUtils.py:167
MsgUtils.make_lines_marker
def make_lines_marker(lines, origin, res)
Definition: MsgUtils.py:128
MsgUtils.fromOccupancyGridToImg
def fromOccupancyGridToImg(occupancyGrid)
Definition: MsgUtils.py:34


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