viz.py
Go to the documentation of this file.
00001 
00002 import numpy as np
00003 
00004 import roslib; roslib.load_manifest('hrl_lib')
00005 import rospy
00006 import visualization_msgs.msg as vm
00007 import geometry_msgs.msg as gm
00008 import std_msgs.msg as sdm
00009 
00010 import hrl_lib.transforms as tr
00011 
00012 ##
00013 # Creates a dictionary containing marker constants indexed by friendly names
00014 def create_mdict():
00015     mdict = {}
00016     mdict['arrow']            = vm.Marker.ARROW
00017     mdict['cube']             = vm.Marker.CUBE
00018     mdict['sphere']           = vm.Marker.SPHERE
00019     mdict['cylinder']         = vm.Marker.CYLINDER
00020     mdict['line_strip']       = vm.Marker.LINE_STRIP
00021     mdict['line_list']        = vm.Marker.LINE_LIST
00022     mdict['cube_list']        = vm.Marker.CUBE_LIST
00023     mdict['sphere_list']      = vm.Marker.SPHERE_LIST
00024     mdict['points']           = vm.Marker.POINTS
00025     mdict['text_view_facing'] = vm.Marker.TEXT_VIEW_FACING
00026     mdict['mesh_resource']    = vm.Marker.MESH_RESOURCE
00027     mdict['triangle_list']    = vm.Marker.TRIANGLE_LIST
00028     return mdict
00029 
00030 
00031 def text_marker(text, position, scale, color, frame, m_id=0):
00032     m = vm.Marker()
00033     m.header.frame_id = frame
00034     m.id = m_id
00035     m.type = create_mdict()['text_view_facing']
00036 
00037     m.pose.position.x = position[0,0]
00038     m.pose.position.y = position[1,0]              
00039     m.pose.position.z = position[2,0]              
00040     m.scale.z = scale
00041     m.color.r = color[0,0]
00042     m.color.g = color[1,0]
00043     m.color.b = color[2,0]
00044     m.color.a = color[3,0]
00045 
00046     return m
00047 
00048 ##
00049 # Create a visualization_msgs.Marker message containing all the given points
00050 def list_marker(points, colors, scale, mtype, mframe, duration=10.0, m_id=0):
00051     m = vm.Marker()
00052     m.header.frame_id = mframe
00053     m.id = m_id
00054     m.type = create_mdict()[mtype]
00055     m.action = vm.Marker.ADD
00056     m.points = [gm.Point(points[0,i], points[1,i], points[2,i]) for i in range(points.shape[1])]
00057     #pdb.set_trace()
00058     m.colors = [sdm.ColorRGBA(colors[0,i], colors[1,i], colors[2,i], colors[3,i]) for i in range(colors.shape[1])]
00059 
00060     m.color.r = 1.
00061     m.color.g = 0.
00062     m.color.b = 0.
00063     m.color.a = 1.
00064     m.scale.x = scale[0]
00065     m.scale.y = scale[1]
00066     m.scale.z = scale[2]
00067 
00068     m.lifetime = rospy.Duration(duration)
00069 
00070     return m
00071 
00072 
00073 def text_marker(text, center, color, scale, mframe, 
00074         duration=10.0, m_id=0):
00075 
00076     m = vm.Marker()
00077     m.header.frame_id = mframe
00078     m.id = m_id
00079     m.type = create_mdict()['text_view_facing']
00080     m.action = vm.Marker.ADD
00081     m.text = text
00082     m.scale.z = scale
00083     m.pose.position.x = center[0,0]
00084     m.pose.position.y = center[1,0]
00085     m.pose.position.z = center[2,0]
00086     m.lifetime = rospy.Duration(duration)
00087 
00088     return m
00089 
00090 
00091 def circle_marker(center, radius, scale, color, mframe, z=.03, duration=10.0, m_id=0, resolution=1.):
00092     angles = np.matrix(np.arange(0, 360., resolution)).T
00093     xs = center[0,0] + np.cos(angles) * radius
00094     ys = center[1,0] + np.sin(angles) * radius
00095     z = .03
00096 
00097     m = vm.Marker()
00098     m.header.frame_id = mframe
00099     m.id = m_id
00100     m.type = create_mdict()['line_strip']
00101     m.action = vm.Marker.ADD
00102     m.points = [gm.Point(xs[i,0], ys[i,0], z) for i in range(angles.shape[0])]
00103     m.colors = [sdm.ColorRGBA(color[0,0], color[1,0], color[2,0], color[3,0]) for i in range(angles.shape[0])]
00104     m.scale.x = scale
00105     m.lifetime = rospy.Duration(duration)
00106     return m
00107 
00108     
00109 
00110 ##
00111 # Create a visualization_msgs.Marker message given a point
00112 # @param point - 3x1 np matrix
00113 # @param orientation - 4x1 np matrix (quaternion)
00114 # @param mtype - string (see the function create_mdict)
00115 def single_marker(point, orientation, mtype, mframe, scale=[.2,.2,.2], color=[1.0, 0, 0,.5], duration=10.0, m_id=0):
00116     m = vm.Marker()
00117     m.header.frame_id = mframe
00118     m.id = m_id
00119     m.type = create_mdict()[mtype]
00120     m.action = vm.Marker.ADD
00121 
00122     m.pose.position.x = point[0,0]
00123     m.pose.position.y = point[1,0]              
00124     m.pose.position.z = point[2,0]              
00125     m.pose.orientation.x = orientation[0,0]
00126     m.pose.orientation.y = orientation[1,0]
00127     m.pose.orientation.z = orientation[2,0]
00128     m.pose.orientation.w = orientation[3,0]
00129 
00130     m.scale.x = scale[0]
00131     m.scale.y = scale[1]
00132     m.scale.z = scale[2]
00133     m.color.r = color[0]
00134     m.color.g = color[1]
00135     m.color.b = color[2]
00136     m.color.a = color[3]
00137     m.lifetime = rospy.Duration(duration)
00138     return m
00139 
00140 ##
00141 # Creates a Marker message to display a coordinate frame
00142 def create_frame_marker(center, frame, line_len, frame_id, m_id=0):
00143     clist = []
00144     plist = []
00145     alpha = line_len
00146     for i in range(3):
00147         colors = np.matrix(np.zeros((4,2)))
00148         colors[i,:] = 1.0
00149         colors[3,:] = 1.0
00150         clist.append(colors)
00151         plist.append(np.column_stack([center, center+ alpha * frame[:,i]]))
00152     return list_marker(np.column_stack(plist), np.column_stack(clist),
00153                        [.01, 0, 0], 'line_list', frame_id, m_id=m_id)
00154 
00155 
00156 ## make a quaternion from an arrow direction.
00157 # can use this quaternion as the pose of the arrow marker to vizualise
00158 # in rviz.
00159 # @param direc - 3x1 np matrix.
00160 # @return quaternion as a 4x1 np matrix
00161 def arrow_direction_to_quat(direc):
00162     direc = direc / np.linalg.norm(direc)
00163     t = np.matrix([1.,0.,0.]).T
00164     if abs((t.T*direc)[0,0]) > 0.866:
00165         t = np.matrix([0.,1.,0.]).T
00166 
00167     v1 = direc
00168     v2 = t - (t.T*v1)[0,0] * v1
00169     v2 = v2 / np.linalg.norm(v2)
00170     v3 = np.matrix(np.cross(v1.A1, v2.A1)).T
00171     rot_mat = np.column_stack([v1, v2, v3])
00172     q = np.matrix(tr.matrix_to_quaternion(rot_mat)).T
00173     return q
00174 
00175 
00176 
00177 
00178 
00179 
00180 
00181 


hrl_lib
Author(s): Cressel Anderson, Travis Deyle, Advait Jain, Hai Nguyen, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:34:06