generator.py
Go to the documentation of this file.
1 import tf
2 import rospy
3 from visualization_msgs.msg import Marker
4 from geometry_msgs.msg import Point, Quaternion
5 from std_msgs.msg import ColorRGBA
6 
7 
8 def get_point(position, scale=1.0):
9  pt = Point()
10  if position is None:
11  pt.x = 0.0
12  pt.y = 0.0
13  pt.z = 0.0
14  elif('x' in dir(position)):
15  pt.x = position.x
16  pt.y = position.y
17  pt.z = position.z
18  else:
19  pt.x = position[0]
20  pt.y = position[1]
21  pt.z = position[2]
22 
23  pt.x /= scale
24  pt.y /= scale
25  pt.z /= scale
26 
27  return pt
28 
29 
30 def get_quat(orientation):
31  quat = Quaternion()
32  if orientation is None:
33  quat.x = 0.0
34  quat.y = 0.0
35  quat.z = 0.0
36  quat.w = 1.0
37  elif('x' in dir(orientation)):
38  quat.w = orientation.w
39  quat.x = orientation.x
40  quat.y = orientation.y
41  quat.z = orientation.z
42  elif len(orientation) == 4:
43  quat.x = orientation[0]
44  quat.y = orientation[1]
45  quat.z = orientation[2]
46  quat.w = orientation[3]
47  else:
48  q2 = tf.transformations.quaternion_from_euler(orientation[0], orientation[1], orientation[2])
49  quat.x = q2[0]
50  quat.y = q2[1]
51  quat.z = q2[2]
52  quat.w = q2[3]
53  return quat
54 
55 
56 def get_color(color):
57  rgba = ColorRGBA()
58  if color is None:
59  color = [1.0] * 4
60 
61  if hasattr(color, 'x'):
62  rgba.r = getattr(color, 'r', 1.0)
63  rgba.g = getattr(color, 'g', 1.0)
64  rgba.b = getattr(color, 'b', 1.0)
65  rgba.a = getattr(color, 'a', 1.0)
66  elif len(color) == 4:
67  rgba.r = color[0]
68  rgba.g = color[1]
69  rgba.b = color[2]
70  rgba.a = color[3]
71  else:
72  rgba.r = color[0]
73  rgba.g = color[1]
74  rgba.b = color[2]
75  rgba.a = 1.0
76  return rgba
77 
78 
80  def __init__(self):
81  self.reset()
82 
83  def reset(self):
84  self.counter = 0
85  self.frame_id = ''
86  self.ns = 'marker'
87  self.type = 0
88  self.action = Marker.ADD
89  self.scale = [1.0] * 3
90  self.color = None
91  self.points = []
92  self.colors = []
93  self.text = ''
94  self.lifetime = 0.0
95 
96  def marker(self, position=None, orientation=None, points=None, colors=None, scale=1.0, color=None):
97  mark = Marker()
98  mark.header.stamp = rospy.Time.now()
99  mark.header.frame_id = self.frame_id
100  mark.ns = self.ns
101  mark.type = self.type
102  mark.id = self.counter
103  mark.action = self.action
104  mark.scale.x = self.scale[0]
105  mark.scale.y = self.scale[1]
106  mark.scale.z = self.scale[2]
107  if color:
108  mark.color = get_color(color)
109  else:
110  mark.color = get_color(self.color)
111  mark.lifetime = rospy.Duration(self.lifetime)
112 
113  if points is not None:
114  mark.points = []
115  for point in points:
116  mark.points.append(get_point(point, scale))
117  if colors is not None:
118  mark.colors = colors
119 
120  if position is not None or orientation is not None:
121  mark.pose.position = get_point(position, scale)
122  mark.pose.orientation = get_quat(orientation)
123  else:
124  mark.pose.orientation.w = 1.0
125 
126  self.counter += 1
127  return mark
def get_color(color)
Definition: generator.py:56
def get_quat(orientation)
Definition: generator.py:30
def marker(self, position=None, orientation=None, points=None, colors=None, scale=1.0, color=None)
Definition: generator.py:96
def get_point(position, scale=1.0)
Definition: generator.py:8


easy_markers
Author(s): David V. Lu!!
autogenerated on Tue Mar 1 2022 00:06:55