2
'''Displays the points in an xyz file the name of which is supplied on the
3
command line. The file should have a format like
11
#Copyright 2010-2011, Julian Ryde and Nicholas Hillier.
13
#This program is distributed in the hope that it will be useful,
14
#but WITHOUT ANY WARRANTY; without even the implied warranty of
15
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
#GNU Lesser General Public License for more details.
18
#You should have received a copy of the GNU Lesser General Public License
19
#along with this program. If not, see <http://www.gnu.org/licenses/>.
21
# TODO circular references between dispxyz and robotvisualiser
22
# fixed for now with delayed import statements, however should really fix
33
import mrol_mapping.pointcloud as pointcloud
34
import mrol_mapping.poseutil as poseutil
36
from threading import Thread
38
class EnableMouseThread(Thread):
39
def __init__(self, scene):
41
self.mouse_thread_enabled = True
47
while self.mouse_thread_enabled:
48
if self.scene.mouse.events:
49
m1 = self.scene.mouse.getevent()
50
if m1.click == 'left':
51
if m1.pickpos != None:
52
self.scene.center = m1.pickpos
53
if m1.drag == 'left' and self.dragging == False:
54
self.drag_pos = np.asarray(self.scene.mouse.pos)
59
new_pos = np.asarray(self.scene.mouse.pos)
60
if (new_pos + delta != self.drag_pos).all(): # if mouse has moved
61
delta = (self.drag_pos - new_pos)
62
self.scene.center = self.scene.center + delta
63
self.drag_pos = new_pos + delta # update drag position
66
self.mouse_thread_enabled = False
70
""" function used externally by other programs to show continuosly rotating
76
visual.scene.up = (0, 0, 1)
80
visual.scene.forward = (np.sin(az), np.cos(az), -1)
82
def colors(X, bounds=None):
91
mcm = matplotlib.cm.jet
92
#mcm = matplotlib.cm.GnBu
93
#mcm = matplotlib.cm.gray
94
return mcm((X-minx)/(maxx-minx))[:, :3]
96
def showpts(xyzs, pose=None, size=2, color=None):
98
xyzs = pose.transformPoints(xyzs)
100
import mrol_mapping.visualiser.robotvisualiser as robotvisualiser
101
robotvisualiser._draw_axes((0,0,0))
104
C = colors(xyzs[:, 2])
107
P = visual.points(pos=xyzs, color=C, shape='square', size=size)
109
#visual.scene.visible = False
113
def plotgrid(xyzs, separation=1, pointsize=2, grid_pt_size=1):
115
mins = np.amin(xyzs, 0).astype(int)
116
maxs = np.amax(xyzs, 0).astype(int)
117
minz = np.amin(xyzs[:, 2])
119
XX, YY = np.meshgrid(range(mins[0]/d, maxs[0]/d), range(mins[1]/d, maxs[1]/d))
122
ZZ = minz*np.ones_like(XX)
123
gridpts = np.vstack((XX, YY, ZZ)).T
124
visual.scene.background = (1, 1, 1)
125
vis_gridpts = visual.points(pos=gridpts, size=grid_pt_size, color=(0, 0, 0))
126
vis_pts = showpts(xyzs, size=pointsize)
129
def plot2ptlines(pts1, pts2, color=None):
130
pts1 = np.asarray(pts1)
131
pts2 = np.asarray(pts2)
132
assert pts1.shape == pts2.shape, 'pts1 and pts2 must have the sime dimensions'
133
assert pts1.shape[1] == 3, 'pts must be Nx3'
134
assert pts2.shape[1] == 3, 'pts must be Nx3'
136
dist = np.zeros(pts1.shape[0])
138
for i, p1 in enumerate(pts1):
140
dist[i] = np.sqrt(np.abs(np.sum(p2**2 - p1**2)))
141
assert not np.isnan(dist[i])
145
C = np.asarray(color)
146
for i in range(pts1.shape[0]):
147
handles.append(visual.curve(pos=[pts1[i,:],pts2[i,:]], color=C[i,:]))
150
def plotaxis(length=1.):
151
return plot2ptlines([(0,0,0),(0,0,0),(0,0,0)],[(length,0,0),(0,length,0),(0,0,length)],color=[(1,0,0),(0,1,0),(0,0,1)])
153
# TODO merge the rotating code below with the rotating function
154
if __name__ == '__main__':
155
if len(sys.argv) == 1:
159
rotate = '--rotating' in sys.argv[1:]
160
grid = '--grid' in sys.argv[1:]
163
visual.scene.select()
164
visual.scene.up = (0, 0, 1)
165
visual.scene.forward = (np.sin(az), np.cos(az), -1)
166
M = pointcloud.load(fname)
168
# approximately center the view on the point cloud
169
visual.scene.center = M.points.mean(axis=0)
171
MT = EnableMouseThread(visual.scene)
175
pc = pointcloud.load(fname)
178
vis_pts = plotgrid(M)
182
# wait for file to change
183
lastmodified = os.path.getmtime(fname)
184
print 'Displaying ', len(M), 'points'
185
while lastmodified == os.path.getmtime(fname):
189
visual.scene.forward = (np.sin(az), np.cos(az), -1)
190
vis_pts.visible = False
191
vis_gridpts.visible=False