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 from enthought.mayavi import mlab 00031 import numpy as np, math 00032 00033 00034 color_list = [(1.,1.,1.),(1.,0.,0.),(0.,1.,0.),(0.,0.,1.),(1.,1.,0.),(1.,0.,1.),\ 00035 (0.,1.,1.),(0.5,1.,0.5),(1.,0.5,0.5)] 00036 00037 ## 00038 # make a figure with a white background. 00039 def white_bg(): 00040 mlab.figure(fgcolor = (0,0,0), bgcolor = (1,1,1)) 00041 00042 ## 00043 # save plot as a png 00044 # @param name - file name 00045 # size - (r,c) e.g. (1024, 768) 00046 def savefig(name, size): 00047 mlab.savefig(name, size=size) 00048 00049 ## plot 3D points connected to each other 00050 # Check mlab.points3d documentation for details. 00051 # @param pts - 3xN numpy matrix of points. 00052 # @param color - 3 tuple of color. (float b/w 0 and 1) 00053 # @param mode - how to display the points ('point','sphere','cube' etc.) 00054 # @param scale_fator - controls size of the spheres. not sure what it means. 00055 def plot(pts,color=(1.,1.,1.), scalar_list=None): 00056 if scalar_list != None: 00057 mlab.plot3d(pts[0,:].A1,pts[1,:].A1,pts[2,:].A1,scalar_list, 00058 representation = 'wireframe', tube_radius = None) 00059 mlab.colorbar() 00060 else: 00061 mlab.plot3d(pts[0,:].A1,pts[1,:].A1,pts[2,:].A1,color=color, 00062 representation = 'wireframe', tube_radius = None) 00063 00064 ## plot 3D points as a cloud. 00065 # Check mlab.points3d documentation for details. 00066 # @param pts - 3xN numpy matrix of points. 00067 # @param color - 3 tuple of color. (float b/w 0 and 1) 00068 # @param mode - how to display the points ('point','sphere','cube' etc.) 00069 # @param scale_fator - controls size of the spheres. not sure what it means. 00070 def plot_points(pts, color=(1.,1.,1.), scale_factor=0.02, mode='point', scalar_list=None): 00071 if scalar_list != None: 00072 mlab.points3d(pts[0,:].A1, pts[1,:].A1, pts[2,:].A1, scalar_list,mode=mode, scale_factor=scale_factor) 00073 mlab.colorbar() 00074 else: 00075 mlab.points3d(pts[0,:].A1, pts[1,:].A1, pts[2,:].A1, mode=mode, color=color, scale_factor=scale_factor) 00076 00077 00078 ## plot points and arrows. 00079 # @param pts - 3xN np matrix 00080 # @param vecs - 3xN np matrix of arrow vectors 00081 # @param color - of the arrows 00082 def plot_quiver(pts, vecs, color=(0.,1.,0.), arrow_scale = 0.05, 00083 point_mode='sphere', point_scale=0.002): 00084 x = pts[0,:].A1 00085 y = pts[1,:].A1 00086 z = pts[2,:].A1 00087 00088 u = vecs[0,:].A1 00089 v = vecs[1,:].A1 00090 w = vecs[2,:].A1 00091 00092 plot_points(pts, mode=point_mode, scale_factor=point_scale) 00093 mlab.quiver3d(x, y, z, u, v, w, scale_factor=arrow_scale, 00094 color=color, scale_mode='vector') 00095 00096 00097 ## Plot a yellow cuboid. 00098 # cuboid is defined by 12 tuples of corners that define the 12 edges, 00099 # as returned by occupancy_grig.grid_lines() function. 00100 def plot_cuboid(corner_tups): 00101 for tup in corner_tups: 00102 p1 = tup[0] 00103 p2 = tup[1] 00104 mlab.plot3d([p1[0,0],p2[0,0]],[p1[1,0],p2[1,0]], 00105 [p1[2,0],p2[2,0]],color=(1.,1.,0.), 00106 representation='wireframe',tube_radius=None) 00107 00108 ## show the plot. 00109 # call this function after plotting everything. 00110 def show(): 00111 mlab.show() 00112 00113 00114 if __name__ == '__main__': 00115 pts = np.matrix(np.random.uniform(size=(3,5000))) 00116 plot_points(pts) 00117 show() 00118 00119 00120 00121 00122