visualization_utils.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 # Author: Yuki Furuta <furushchev@jsk.imi.i.u-tokyo.ac.jp>
00004 
00005 import rospy
00006 from std_msgs.msg import Header, ColorRGBA
00007 from geometry_msgs.msg import Point, Vector3
00008 from visualization_msgs.msg import Marker, MarkerArray
00009 from copy import deepcopy
00010 from transform_utils import TransformationUtils as T
00011 import colorsys
00012 import math
00013 
00014 def distanceOfVector3(v1,v2):
00015     return math.sqrt((v1.x - v2.x) * (v1.x - v2.x)
00016                      + (v1.y - v2.y) * (v1.y - v2.y)
00017                      + (v1.z - v2.z) * (v1.z - v2.z))
00018 
00019 class VisualizationUtils():
00020     marker_id = 0
00021 
00022     @classmethod
00023     def transformStampedArrayToLabeledArrayMarker(cls, tsdata_lst, fmt="%Y-%m-%d %H:%M:%S", zoffset=0.05, label_downsample=1, discrete=False):
00024         "[[transformStamped, meta],...] -> LineStrip / String"
00025         h = Header()
00026         h.stamp = rospy.Time(0) #rospy.Time.now()
00027         h.frame_id = "eng2" #t_first.child_frame_id
00028         res = []
00029 
00030         t_first = tsdata_lst[0][0]
00031         prev_t = t_first.transform.translation
00032         for ts, _ in tsdata_lst:
00033             m = Marker(type=Marker.ARROW,
00034                        action=Marker.ADD,
00035                        header=h,
00036                        id=cls.marker_id)
00037             cls.marker_id += 1
00038             t = ts.transform.translation
00039             dist = distanceOfVector3(prev_t, t) * 0.65
00040             rgb = colorsys.hsv_to_rgb(dist, 1.0, 1.0)
00041             m.points = [Point(x=prev_t.x,y=prev_t.y,z=prev_t.z),
00042                         Point(x=(prev_t.x + t.x) / 2.,y=(prev_t.y + t.y) /2.,z=(prev_t.z + t.z) /2.)]
00043             m.ns = "labeled_line"
00044             m.lifetime = rospy.Time(3000)
00045             m.scale.x, m.scale.y, m.scale.z = 0.02, 0.06, 0.1
00046             m.color = ColorRGBA(rgb[0],rgb[1],rgb[2],1.0)
00047             if dist < 0.65:
00048                 res.append(m)
00049             prev_t = t
00050 
00051         for t, t_meta in tsdata_lst[::label_downsample]:
00052             text = Marker(type=Marker.TEXT_VIEW_FACING,
00053                           action=Marker.ADD,
00054                           header=h,
00055                           id=cls.marker_id)
00056             cls.marker_id += 1
00057             text.scale.z = 0.1
00058             text.pose = T.poseFromTransform(t.transform)
00059             text.pose.position.z += zoffset
00060             text.color = ColorRGBA(0.0,0.0,1.0,1.0)
00061             text.text = t_meta["inserted_at"].strftime(fmt)
00062             text.ns = "labeled_line_text"
00063             text.lifetime = rospy.Time(3000)
00064             res.append(text)
00065         return res
00066 
00067     @classmethod
00068     def transformStampedArrayToLabeledLineStripMarker(cls, tsdata_lst, fmt="%Y-%m-%d %H:%M:%S", zoffset=0.05, label_downsample=1, discrete=False):
00069         "[[transformStamped, meta],...] -> LineStrip / String"
00070         res = []
00071         # make line strip
00072         points = []
00073         colors = []
00074         t_first = tsdata_lst[0][0]
00075         prev_t = t_first.transform.translation
00076         for ts, _ in tsdata_lst:
00077             t = ts.transform.translation
00078             dist = distanceOfVector3(prev_t, t) * 0.65
00079             rgb = colorsys.hsv_to_rgb(dist, 1.0, 1.0)
00080             points.append(Point(x=t.x, y=t.y, z=t.z))
00081             colors.append(ColorRGBA(rgb[0],rgb[1],rgb[2],1.0))
00082             prev_t = t
00083 
00084         h = Header()
00085         h.stamp = rospy.Time(0) #rospy.Time.now()
00086         h.frame_id = "eng2" #t_first.child_frame_id
00087         if discrete:
00088             m_type = Marker.POINTS
00089         else:
00090             m_type = Marker.LINE_STRIP
00091         m = Marker(type=m_type,
00092                    action=Marker.ADD,
00093                    header=h,
00094                    id=cls.marker_id)
00095         cls.marker_id += 1
00096         m.scale.x = 0.1
00097         m.scale.y = 0.1
00098         m.points = points
00099         m.colors = colors
00100         m.ns = "labeled_line"
00101         m.lifetime = rospy.Time(3000)
00102         res.append(m)
00103 
00104         for t, t_meta in tsdata_lst[::label_downsample]:
00105             text = Marker(type=Marker.TEXT_VIEW_FACING,
00106                           action=Marker.ADD,
00107                           header=h,
00108                           id=cls.marker_id)
00109             cls.marker_id += 1
00110             text.scale.z = 0.1
00111             text.pose = T.poseFromTransform(t.transform)
00112             text.pose.position.z += zoffset
00113             text.color = ColorRGBA(0.0,0.0,1.0,1.0)
00114             text.text = t_meta["inserted_at"].strftime(fmt)
00115             text.ns = "labeled_line_text"
00116             text.lifetime = rospy.Time(3000)
00117             res.append(text)
00118         return res
00119 
00120     @classmethod
00121     def poseStampedToLabeledSphereMarker(cls, psdata, label, fmt="{label} %Y-%m-%d %H:%M:%S", zoffset=0.05):
00122         "[poseStamped, meta] -> sphereMarker"
00123         ps, meta = psdata
00124         res = []
00125         h = Header()
00126         h.stamp = rospy.Time.now()
00127         h.frame_id = ps.header.frame_id
00128         sphere = Marker(type=Marker.SPHERE,
00129                    action=Marker.ADD,
00130                    header=h,
00131                    id=cls.marker_id)
00132         sphere.scale.x = 0.07
00133         sphere.scale.y = 0.07
00134         sphere.scale.z = 0.07
00135         sphere.pose = ps.pose
00136         sphere.color = ColorRGBA(1.0,0,0,1.0)
00137         sphere.ns = "db_play"
00138         sphere.lifetime = rospy.Time(3)
00139         cls.marker_id += 1
00140         res.append(sphere)
00141 
00142         text = Marker(type=Marker.TEXT_VIEW_FACING,
00143                    action=Marker.ADD,
00144                    header=h,
00145                    id=cls.marker_id)
00146         text.scale.z = 0.1
00147         text.pose = deepcopy(ps.pose)
00148         text.pose.position.z += zoffset
00149         text.color = ColorRGBA(1.0,1.0,1.0,1.0)
00150         text.text = meta["inserted_at"].strftime(fmt).format(label=label)
00151         text.ns = "db_play_text"
00152         text.lifetime = rospy.Time(300)
00153         cls.marker_id += 1
00154         res.append(text)
00155         return res
00156 


jsk_pr2_startup
Author(s):
autogenerated on Wed Sep 16 2015 10:32:17