display_3d_mayavi.py
Go to the documentation of this file.
00001 #
00002 # Copyright (c) 2009, Georgia Tech Research Corporation
00003 # All rights reserved.
00004 #
00005 # Redistribution and use in source and binary forms, with or without
00006 # modification, are permitted provided that the following conditions are met:
00007 #     * Redistributions of source code must retain the above copyright
00008 #       notice, this list of conditions and the following disclaimer.
00009 #     * Redistributions in binary form must reproduce the above copyright
00010 #       notice, this list of conditions and the following disclaimer in the
00011 #       documentation and/or other materials provided with the distribution.
00012 #     * Neither the name of the Georgia Tech Research Corporation nor the
00013 #       names of its contributors may be used to endorse or promote products
00014 #       derived from this software without specific prior written permission.
00015 #
00016 # THIS SOFTWARE IS PROVIDED BY GEORGIA TECH RESEARCH CORPORATION ''AS IS'' AND
00017 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 # DISCLAIMED. IN NO EVENT SHALL GEORGIA TECH BE LIABLE FOR ANY DIRECT, INDIRECT,
00020 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00021 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00022 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00023 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00024 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00025 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 #
00027 
00028 #  \author Advait Jain (Healthcare Robotics Lab, Georgia Tech.)
00029 
00030 import roslib; roslib.load_manifest('hrl_tilting_hokuyo')
00031 
00032 from enthought.mayavi import mlab
00033 
00034 import sys,os,time
00035 import optparse
00036 import hrl_lib.util as ut
00037 import numpy as np, math
00038 
00039 
00040 color_list = [(1.,1.,1.),(1.,0.,0.),(0.,1.,0.),(0.,0.,1.),(1.,1.,0.),(1.,0.,1.),\
00041               (0.,1.,1.),(0.5,1.,0.5),(1.,0.5,0.5)]
00042 
00043 
00044 
00045 ##
00046 # make a figure with a white background.
00047 def white_bg():
00048     mlab.figure(fgcolor = (0,0,0), bgcolor = (1,1,1))
00049 
00050 ##
00051 # save plot as a png
00052 # @param name - file name
00053 # size - (r,c) e.g. (1024, 768)
00054 def savefig(name, size):
00055     mlab.savefig(name, size=size)
00056 
00057 ## plot 3D points connected to each other
00058 #
00059 # Check mlab.points3d documentation for details.
00060 # @param pts - 3xN numpy matrix of points.
00061 # @param color - 3 tuple of color. (float b/w 0 and 1)
00062 # @param mode - how to display the points ('point','sphere','cube' etc.)
00063 # @param scale_fator - controls size of the spheres. not sure what it means.
00064 def plot(pts,color=(1.,1.,1.), scalar_list=None):
00065     if scalar_list != None:
00066         mlab.plot3d(pts[0,:].A1,pts[1,:].A1,pts[2,:].A1,scalar_list,
00067                     representation = 'wireframe', tube_radius = None)
00068         mlab.colorbar()
00069     else:
00070         mlab.plot3d(pts[0,:].A1,pts[1,:].A1,pts[2,:].A1,color=color,
00071                     representation = 'wireframe', tube_radius = None)
00072 
00073 ## plot 3D points as a cloud.
00074 #
00075 # Check mlab.points3d documentation for details.
00076 # @param pts - 3xN numpy matrix of points.
00077 # @param color - 3 tuple of color. (float b/w 0 and 1)
00078 # @param mode - how to display the points ('point','sphere','cube' etc.)
00079 # @param scale_fator - controls size of the spheres. not sure what it means.
00080 def plot_points(pts,color=(1.,1.,1.),mode='point',scale_factor=0.01,scalar_list=None):
00081     if scalar_list != None:
00082         mlab.points3d(pts[0,:].A1,pts[1,:].A1,pts[2,:].A1,scalar_list,mode=mode,scale_factor=scale_factor)
00083         mlab.colorbar()
00084     else:
00085         mlab.points3d(pts[0,:].A1,pts[1,:].A1,pts[2,:].A1,mode=mode,color=color,scale_factor=scale_factor)
00086 
00087 
00088 ## Use mayavi2 to plot normals, and curvature of a point cloud.
00089 # @param pts - 3xN np matrix
00090 # @param normals - 3xN np matrix of surface normals at the points in pts.
00091 # @param curvature - list of curvatures.
00092 # @param mask_points - how many point to skip while drawint the normals
00093 # @param color - of the arrows
00094 # @param scale_factor - modulate size of arrows.
00095 #
00096 # Surface normals are plotted as arrows at the pts, curvature is colormapped and
00097 # shown as spheres. The radius of the sphere also indicates the magnitude
00098 # of the curvature. If curvature is None then it is not plotted. The pts
00099 # are then displayed as pixels.
00100 def plot_normals(pts, normals, curvature=None, mask_points=1,
00101                  color=(0.,1.,0.), scale_factor = 0.1):
00102     x = pts[0,:].A1
00103     y = pts[1,:].A1
00104     z = pts[2,:].A1
00105 
00106     u = normals[0,:].A1
00107     v = normals[1,:].A1
00108     w = normals[2,:].A1
00109 
00110     if curvature != None:
00111         curvature = np.array(curvature)
00112         #idxs = np.where(curvature>0.03)
00113         #mlab.points3d(x[idxs],y[idxs],z[idxs],curvature[idxs],mode='sphere',scale_factor=0.1,mask_points=1)
00114         mlab.points3d(x,y,z,curvature,mode='sphere',scale_factor=0.1,mask_points=1, color=color)
00115 #        mlab.points3d(x,y,z,mode='point')
00116         mlab.colorbar()
00117     else:
00118         mlab.points3d(x,y,z,mode='point')
00119     mlab.quiver3d(x, y, z, u, v, w, mask_points=mask_points,
00120                   scale_factor=scale_factor, color=color)
00121 #    mlab.axes()
00122 
00123 ## Plot a yellow cuboid.
00124 # cuboid is defined by 12 tuples of corners that define the 12 edges,
00125 # as returned by occupancy_grig.grid_lines() function.
00126 def plot_cuboid(corner_tups):
00127     for tup in corner_tups:
00128         p1 = tup[0]
00129         p2 = tup[1]
00130         mlab.plot3d([p1[0,0],p2[0,0]],[p1[1,0],p2[1,0]],
00131                     [p1[2,0],p2[2,0]],color=(1.,1.,0.),
00132                     representation='wireframe',tube_radius=None)
00133 
00134 ## show the plot.
00135 # call this function after plotting everything.
00136 def show():
00137     mlab.show()
00138 
00139 
00140 
00141 if __name__ == '__main__':
00142     p = optparse.OptionParser()
00143 
00144     p.add_option('-c', action='store', type='string', dest='pts_pkl',
00145                  help='pkl file with 3D points')
00146     p.add_option('-f', action='store', type='string', dest='dict_pkl',
00147                  help='pkl file with 3D dict')
00148     p.add_option('--save_cloud', action='store_true', dest='save_cloud',
00149                  help='pickle the point cloud (3xN matrix)')
00150     p.add_option('--pan_angle', action='store', type='float',
00151                  dest='max_pan_angle', default=60.0,
00152                  help='angle in DEGREES. points b/w (-pan_angle and +pan_angle) are displayed. [default=60.]')
00153     p.add_option('--max_dist', action='store', type='float',
00154                  dest='max_dist', default=3.0,
00155                  help='maximum distance (meters). Points further than this are discarded. [default=3.]')
00156 
00157     opt, args = p.parse_args()
00158 
00159     pts_pkl_fname = opt.pts_pkl
00160     dict_pkl_fname = opt.dict_pkl
00161     save_cloud_flag = opt.save_cloud
00162     max_pan_angle = opt.max_pan_angle
00163     max_dist = opt.max_dist
00164 
00165     if pts_pkl_fname != None:
00166         pts = ut.load_pickle(pts_pkl_fname)
00167     elif dict_pkl_fname != None:
00168         import tilting_hokuyo.processing_3d as p3d
00169         dict = ut.load_pickle(dict_pkl_fname)
00170         pts = p3d.generate_pointcloud(dict['pos_list'],dict['scan_list'],
00171                                       math.radians(-max_pan_angle),
00172                                       math.radians(max_pan_angle),
00173                                       dict['l1'],dict['l2'],
00174                                       min_tilt=math.radians(-90),max_tilt=math.radians(90))
00175     else:
00176         print 'Specify either a pts pkl or a dict pkl (-c or -f)'
00177         print 'Exiting...'
00178         sys.exit()
00179 
00180     dist_mat = ut.norm(pts)
00181     idxs = np.where(dist_mat<max_dist)[1]
00182     print 'pts.shape', pts.shape
00183     pts = pts[:,idxs.A1]
00184     print 'pts.shape', pts.shape
00185 
00186     if save_cloud_flag:
00187         ut.save_pickle(pts,'numpy_pc_'+ut.formatted_time()+'.pkl')
00188 
00189     plot_points(pts)
00190     mlab.show()
00191 
00192 


hrl_tilting_hokuyo
Author(s): Advait Jain, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:56:18