$search
00001 import vtk 00002 #from Numeric import * 00003 #from numpy import * 00004 #from numpy.linalg import * 00005 import numpy as np 00006 import pickle 00007 from tesselatedSphere import TesselatedSphere 00008 import array_handler 00009 import cv 00010 import math 00011 00012 def cv2array(im): 00013 depth2dtype = { 00014 cv.IPL_DEPTH_8U: 'uint8', 00015 cv.IPL_DEPTH_8S: 'int8', 00016 cv.IPL_DEPTH_16U: 'uint16', 00017 cv.IPL_DEPTH_16S: 'int16', 00018 cv.IPL_DEPTH_32S: 'int32', 00019 cv.IPL_DEPTH_32F: 'float32', 00020 cv.IPL_DEPTH_64F: 'float64', 00021 } 00022 00023 arrdtype=im.depth 00024 a = np.fromstring( 00025 im.tostring(), 00026 dtype=depth2dtype[im.depth], 00027 count=im.width*im.height*im.nChannels) 00028 a.shape = (im.height,im.width,im.nChannels) 00029 return a 00030 00031 def array2cv(a): 00032 dtype2depth = { 00033 'uint8': cv.IPL_DEPTH_8U, 00034 'int8': cv.IPL_DEPTH_8S, 00035 'uint16': cv.IPL_DEPTH_16U, 00036 'int16': cv.IPL_DEPTH_16S, 00037 'int32': cv.IPL_DEPTH_32S, 00038 'float32': cv.IPL_DEPTH_32F, 00039 'float64': cv.IPL_DEPTH_64F, 00040 } 00041 try: 00042 nChannels = a.shape[2] 00043 except: 00044 nChannels = 1 00045 cv_im = cv.CreateImageHeader((a.shape[1],a.shape[0]), 00046 dtype2depth[str(a.dtype)], 00047 nChannels) 00048 cv.SetData(cv_im, a.tostring(), 00049 a.dtype.itemsize*nChannels*a.shape[1]) 00050 return cv_im 00051 00052 class Visualizer: 00053 def visModelPly(self, plyFile): 00054 #----------------------------visualization-------------------------- 00055 00056 reader = vtk.vtkPLYReader() 00057 reader.SetFileName(plyFile) 00058 00059 lineMapper2 = vtk.vtkPolyDataMapper() 00060 lineMapper2.SetInputConnection(reader.GetOutputPort()) 00061 lineMapper2.Update() 00062 00063 lineActor = vtk.vtkActor() 00064 lineActor.GetProperty().SetColor(0,1,0) 00065 lineActor.GetProperty().SetOpacity(1) 00066 lineActor.SetMapper(lineMapper2) 00067 00068 # Create the usual graphics stuff. 00069 # Create the RenderWindow, Renderer and both Actors 00070 00071 camera = vtk.vtkCamera() 00072 #camera.SetPosition(0,3,-6) 00073 camera.SetFocalPoint(0,0,0) 00074 00075 ren = vtk.vtkRenderer() 00076 #ren.SetActiveCamera(camera) 00077 renWin = vtk.vtkRenderWindow() 00078 renWin.AddRenderer(ren) 00079 iren = vtk.vtkRenderWindowInteractor() 00080 iren.SetRenderWindow(renWin) 00081 00082 ren.AddActor(lineActor) 00083 00084 ren.SetBackground(1, 1, 1) 00085 renWin.SetSize(600, 600) 00086 00087 iren.Initialize() 00088 renWin.Render() 00089 iren.Start() 00090 00091 renWin.Finalize() 00092 00093 def visModelsPly(self, plyFile, plyFile2): 00094 #----------------------------visualization-------------------------- 00095 00096 reader = vtk.vtkPLYReader() 00097 reader.SetFileName(plyFile) 00098 00099 lineMapper2 = vtk.vtkPolyDataMapper() 00100 lineMapper2.SetInputConnection(reader.GetOutputPort()) 00101 lineMapper2.Update() 00102 00103 lineActor = vtk.vtkActor() 00104 lineActor.GetProperty().SetColor(0,1,0) 00105 lineActor.GetProperty().SetOpacity(1) 00106 lineActor.SetMapper(lineMapper2) 00107 00108 reader2 = vtk.vtkPLYReader() 00109 reader2.SetFileName(plyFile2) 00110 00111 lineMapper22 = vtk.vtkPolyDataMapper() 00112 lineMapper22.SetInputConnection(reader2.GetOutputPort()) 00113 lineMapper22.Update() 00114 00115 lineActor2 = vtk.vtkActor() 00116 lineActor2.GetProperty().SetColor(1,0,0) 00117 lineActor2.GetProperty().SetOpacity(1) 00118 lineActor2.SetMapper(lineMapper22) 00119 00120 # Create the usual graphics stuff. 00121 # Create the RenderWindow, Renderer and both Actors 00122 00123 camera = vtk.vtkCamera() 00124 #camera.SetPosition(0,3,-6) 00125 camera.SetFocalPoint(0,0,0) 00126 00127 ren = vtk.vtkRenderer() 00128 #ren.SetActiveCamera(camera) 00129 renWin = vtk.vtkRenderWindow() 00130 renWin.AddRenderer(ren) 00131 iren = vtk.vtkRenderWindowInteractor() 00132 iren.SetRenderWindow(renWin) 00133 00134 ren.AddActor(lineActor) 00135 ren.AddActor(lineActor2) 00136 00137 ren.SetBackground(1, 1, 1) 00138 renWin.SetSize(600, 600) 00139 00140 iren.Initialize() 00141 renWin.Render() 00142 iren.Start() 00143 00144 renWin.Finalize() 00145 00146 def visModelAndContactPoints(self, plyFile, contactPoints, scaleFac, CoM, sphere, modelMapper=None, anotherMapper=None): 00147 #----------------------------visualization-------------------------- 00148 00149 #print len(contactPoints), contactPoints 00150 00151 if modelMapper == None: 00152 reader = vtk.vtkPLYReader() 00153 reader.SetFileName(plyFile) 00154 00155 trans = vtk.vtkTransform() 00156 trans.Translate(-CoM[0],-CoM[1],-CoM[2]) 00157 tttf = vtk.vtkTransformFilter() 00158 tttf.SetTransform(trans) 00159 tttf.SetInputConnection(reader.GetOutputPort()) 00160 00161 lineMapper = vtk.vtkPolyDataMapper() 00162 lineMapper.SetInputConnection(tttf.GetOutputPort()) 00163 lineMapper.Update() 00164 00165 CoM = sphere.computeCenterOfMass(lineMapper.GetInput()) 00166 #print CoM 00167 00168 #scaleFac = 1 00169 #scaleFac /= 2.54 00170 #scaleFac = 25.4 00171 trans = vtk.vtkTransform() 00172 trans.Scale(scaleFac,scaleFac,scaleFac) 00173 00174 tf = vtk.vtkTransformFilter() 00175 #tf.SetTransform(trans) 00176 tf.SetInputConnection(reader.GetOutputPort()) 00177 00178 lineMapper2 = vtk.vtkPolyDataMapper() 00179 lineMapper2.SetInputConnection(reader.GetOutputPort()) 00180 lineMapper2.Update() 00181 else: 00182 lineMapper2 = modelMapper 00183 00184 lineActor = vtk.vtkActor() 00185 lineActor.GetProperty().SetColor(0,1,0) 00186 lineActor.GetProperty().SetOpacity(0.5) 00187 lineActor.SetMapper(lineMapper2) 00188 00189 coneSource = vtk.vtkConeSource() 00190 coneSource.SetResolution(6) 00191 00192 mypoints = vtk.vtkPoints() 00193 poly = vtk.vtkPolyData() 00194 conn = vtk.vtkCellArray() 00195 00196 for pp in contactPoints: 00197 mypoints.InsertNextPoint([pp[0],pp[1],pp[2]]) 00198 00199 for i in range(len(contactPoints)): 00200 a = conn.InsertNextCell(1) 00201 00202 poly.SetPoints(mypoints) 00203 poly.SetVerts(conn) 00204 00205 glyph = vtk.vtkGlyph3D() 00206 glyph.SetInput(poly) 00207 glyph.SetSourceConnection(coneSource.GetOutputPort()) 00208 glyph.SetScaleFactor(0.1) 00209 00210 spike = vtk.vtkPolyDataMapper() 00211 spike.SetInputConnection(glyph.GetOutputPort()) 00212 00213 spikeActor = vtk.vtkActor() 00214 spikeActor.SetMapper(spike) 00215 spikeActor.GetProperty().SetColor(1,0,0) 00216 00217 # Create the usual graphics stuff. 00218 # Create the RenderWindow, Renderer and both Actors 00219 00220 camera = vtk.vtkCamera() 00221 #camera.SetPosition(0,3,-6) 00222 camera.SetFocalPoint(0,0,0) 00223 00224 ren = vtk.vtkRenderer() 00225 #ren.SetActiveCamera(camera) 00226 renWin = vtk.vtkRenderWindow() 00227 renWin.AddRenderer(ren) 00228 iren = vtk.vtkRenderWindowInteractor() 00229 iren.SetRenderWindow(renWin) 00230 00231 ren.AddActor(lineActor) 00232 ren.AddActor(spikeActor) 00233 00234 if anotherMapper != None: 00235 anotherLineActor = vtk.vtkActor() 00236 anotherLineActor.GetProperty().SetColor(0,0,1) 00237 anotherLineActor.GetProperty().SetOpacity(0.5) 00238 anotherLineActor.SetMapper(anotherMapper) 00239 ren.AddActor(anotherLineActor) 00240 00241 ren.SetBackground(1, 1, 1) 00242 renWin.SetSize(600, 600) 00243 00244 iren.Initialize() 00245 renWin.Render() 00246 iren.Start() 00247 00248 renWin.Finalize() 00249 00250 def visModel(self, mapper1, normal=None): 00251 act1 = vtk.vtkActor() 00252 act1.GetProperty().SetColor(0,1,0) 00253 act1.GetProperty().SetOpacity(1) 00254 act1.SetMapper(mapper1) 00255 00256 ground = vtk.vtkPlaneSource() 00257 00258 if normal != None: 00259 ground.SetNormal(normal) 00260 else: 00261 ground.SetNormal([0, 1, 0]) 00262 00263 ground.SetCenter([0, 0, 0]) 00264 00265 planeMapper2 = vtk.vtkPolyDataMapper() 00266 planeMapper2.SetInputConnection(ground.GetOutputPort()) 00267 planeActor2 = vtk.vtkActor() 00268 planeActor2.SetMapper(planeMapper2) 00269 planeActor2.GetProperty().SetColor(1,0,0) 00270 planeActor2.GetProperty().SetOpacity(0.8) 00271 00272 cam = vtk.vtkCamera() 00273 ren = vtk.vtkRenderer() 00274 ren.SetActiveCamera(cam) 00275 renWin = vtk.vtkRenderWindow() 00276 renWin.AddRenderer(ren) 00277 iren = vtk.vtkRenderWindowInteractor() 00278 iren.SetRenderWindow(renWin) 00279 00280 axes = vtk.vtkAxes() 00281 axes.SetOrigin(0, 0, 0) 00282 axes.SetScaleFactor(0.1) 00283 00284 axes_colors = vtk.vtkFloatArray() 00285 axes_colors.InsertNextValue(0.0) 00286 axes_colors.InsertNextValue(0.0) 00287 axes_colors.InsertNextValue(0.5) 00288 axes_colors.InsertNextValue(0.5) 00289 axes_colors.InsertNextValue(1.0) 00290 axes_colors.InsertNextValue(1.0) 00291 00292 axes_data = axes.GetOutput() 00293 axes_data.Update() 00294 axes_data.GetPointData().SetScalars(axes_colors) 00295 00296 axes_tubes = vtk.vtkTubeFilter() 00297 axes_tubes.SetInput (axes_data) 00298 axes_tubes.SetRadius (axes.GetScaleFactor () / 50.0) 00299 axes_tubes.SetNumberOfSides (6) 00300 00301 axesMapper = vtk.vtkPolyDataMapper() 00302 axesMapper.SetScalarModeToUsePointData () 00303 axesMapper.SetInput(axes_tubes.GetOutput()) 00304 axesActor = vtk.vtkActor() 00305 axesActor.SetMapper(axesMapper) 00306 00307 ren.AddActor(axesActor) 00308 00309 ren.AddActor(act1) 00310 ren.AddActor(planeActor2) 00311 00312 ren.SetBackground(1, 1, 1) 00313 renWin.SetSize(600, 600) 00314 00315 iren.Initialize() 00316 renWin.Render() 00317 iren.Start() 00318 00319 renWin.Finalize() 00320 00321 def visModelCam(self, mapper1, camera, color=(0,1,0), points=[], displayModel=True): 00322 00323 act1 = vtk.vtkActor() 00324 act1.GetProperty().SetColor(color) 00325 act1.GetProperty().SetOpacity(1) 00326 act1.SetMapper(mapper1) 00327 00328 ren = vtk.vtkRenderer() 00329 renWin = vtk.vtkRenderWindow() 00330 renWin.AddRenderer(ren) 00331 iren = vtk.vtkRenderWindowInteractor() 00332 iren.SetRenderWindow(renWin) 00333 00334 if displayModel: 00335 ren.AddActor(act1) 00336 00337 if len(points) > 0: 00338 pointsActor1 = vtk.vtkActor() 00339 pointsMapper1 = self.getPointsActor3D(points,0.005) 00340 pointsActor1.SetMapper(pointsMapper1) 00341 pointsActor1.GetProperty().SetColor(1,0,0) 00342 pointsActor1.GetProperty().SetOpacity(0.5) 00343 00344 ren.AddActor(pointsActor1) 00345 00346 axes = vtk.vtkAxes() 00347 axes.SetOrigin(0, 0, 0) 00348 axes.SetScaleFactor(0.1) 00349 00350 axes_colors = vtk.vtkFloatArray() 00351 axes_colors.InsertNextValue(0.0) 00352 axes_colors.InsertNextValue(0.0) 00353 axes_colors.InsertNextValue(0.5) 00354 axes_colors.InsertNextValue(0.5) 00355 axes_colors.InsertNextValue(1.0) 00356 axes_colors.InsertNextValue(1.0) 00357 00358 axes_data = axes.GetOutput() 00359 axes_data.Update() 00360 axes_data.GetPointData().SetScalars(axes_colors) 00361 00362 axes_tubes = vtk.vtkTubeFilter() 00363 axes_tubes.SetInput (axes_data) 00364 axes_tubes.SetRadius (axes.GetScaleFactor () / 50.0) 00365 axes_tubes.SetNumberOfSides (6) 00366 00367 axesMapper = vtk.vtkPolyDataMapper() 00368 axesMapper.SetScalarModeToUsePointData () 00369 axesMapper.SetInput(axes_tubes.GetOutput()) 00370 axesActor = vtk.vtkActor() 00371 axesActor.SetMapper(axesMapper) 00372 00373 ren.AddActor(axesActor) 00374 ren.SetActiveCamera(camera) 00375 00376 ren.SetBackground(1, 1, 1) 00377 renWin.SetSize(600, 600) 00378 00379 iren.Initialize() 00380 renWin.Render() 00381 iren.Start() 00382 00383 renWin.Finalize() 00384 00385 def getPointsActor(self,points): 00386 coneSource = vtk.vtkConeSource() 00387 coneSource.SetResolution(6) 00388 00389 mypoints = vtk.vtkPoints() 00390 poly = vtk.vtkPolyData() 00391 conn = vtk.vtkCellArray() 00392 00393 for pp in points: 00394 mypoints.InsertNextPoint([pp[0],0,pp[1]]) 00395 00396 for i in range(len(points)): 00397 a = conn.InsertNextCell(1) 00398 00399 poly.SetPoints(mypoints) 00400 poly.SetVerts(conn) 00401 00402 glyph = vtk.vtkGlyph3D() 00403 glyph.SetInput(poly) 00404 glyph.SetSourceConnection(coneSource.GetOutputPort()) 00405 glyph.SetScaleFactor(0.025) 00406 00407 spike = vtk.vtkPolyDataMapper() 00408 spike.SetInputConnection(glyph.GetOutputPort()) 00409 00410 return spike 00411 00412 def getPointsActor3D(self,points,scale=0.05): 00413 coneSource = vtk.vtkConeSource() 00414 coneSource.SetResolution(6) 00415 00416 mypoints = vtk.vtkPoints() 00417 poly = vtk.vtkPolyData() 00418 conn = vtk.vtkCellArray() 00419 00420 for pp in points: 00421 #print pp 00422 mypoints.InsertNextPoint([pp[0],pp[1],pp[2]]) 00423 00424 for i in range(len(points)): 00425 a = conn.InsertNextCell(1) 00426 00427 poly.SetPoints(mypoints) 00428 poly.SetVerts(conn) 00429 00430 glyph = vtk.vtkGlyph3D() 00431 glyph.SetInput(poly) 00432 glyph.SetSourceConnection(coneSource.GetOutputPort()) 00433 glyph.SetScaleFactor(scale) 00434 00435 spike = vtk.vtkPolyDataMapper() 00436 spike.SetInputConnection(glyph.GetOutputPort()) 00437 00438 return spike 00439 00440 def getPlaneActor(self, normal): 00441 ground = vtk.vtkPlaneSource() 00442 ground.SetNormal(normal) 00443 ground.SetCenter([0, 0, 0]) 00444 00445 planeMapper2 = vtk.vtkPolyDataMapper() 00446 planeMapper2.SetInputConnection(ground.GetOutputPort()) 00447 planeActor2 = vtk.vtkActor() 00448 planeActor2.SetMapper(planeMapper2) 00449 return planeActor2 00450 00451 def displayMapperAndPoints(self, points, mapper): 00452 pointsActor1 = vtk.vtkActor() 00453 pointsMapper1 = self.getPointsActor3D(points) 00454 pointsActor1.SetMapper(pointsMapper1) 00455 pointsActor1.GetProperty().SetColor(1,0,0) 00456 pointsActor1.GetProperty().SetOpacity(0.5) 00457 00458 ren = vtk.vtkRenderer() 00459 renWin = vtk.vtkRenderWindow() 00460 renWin.AddRenderer(ren) 00461 iren = vtk.vtkRenderWindowInteractor() 00462 iren.SetRenderWindow(renWin) 00463 00464 act1 = vtk.vtkActor() 00465 act1.GetProperty().SetColor(1,0,0) 00466 act1.GetProperty().SetOpacity(0.5) 00467 act1.SetMapper(mapper) 00468 00469 ren.AddActor(pointsActor1) 00470 ren.AddActor(act1) 00471 00472 ren.SetBackground(1, 1, 1) 00473 renWin.SetSize(600, 600) 00474 00475 iren.Initialize() 00476 renWin.Render() 00477 iren.Start() 00478 00479 renWin.Finalize() 00480 00481 def VTKtoOpenCVImg(self, im): 00482 numPyArray = array_handler.vtk2array(im) 00483 size = int(math.sqrt(numPyArray.shape[0])) 00484 a = np.reshape(numPyArray, (size, size, 3)) 00485 00486 im = cv.CreateImage( (size, size) , 8, 3) 00487 im = array2cv(a) 00488 cv.Flip(im,im, 0) 00489 00490 #finally convert to binary image! 00491 binary = cv.CreateImage( cv.GetSize(im), cv.IPL_DEPTH_8U, 1) 00492 cv.CvtColor(im , binary, cv.CV_RGB2GRAY) 00493 return binary 00494 00495 def getViewUp(self, normal): 00496 if normal[1] == 1: 00497 return [1,0,0] 00498 else: 00499 return [0,1,0] 00500 00501 00502 def getImageProjectionFromMapperSettingNormal(self, mapper, normal, fileName, CoM, stereo=False): 00503 trans2scale = vtk.vtkTransform() 00504 trans2scale.Scale(1,1,1) 00505 transFilter22 = vtk.vtkTransformFilter() 00506 transFilter22.SetTransform(trans2scale) 00507 transFilter22.SetInput(mapper.GetInput()) 00508 mapper2Scaled = vtk.vtkPolyDataMapper() 00509 mapper2Scaled.SetInputConnection(transFilter22.GetOutputPort()) 00510 mapper2Scaled.Update() 00511 00512 actor = vtk.vtkActor() 00513 actor.GetProperty().SetColor(0,0,0) 00514 actor.SetMapper(mapper2Scaled) 00515 00516 camera = vtk.vtkCamera() 00517 camera.SetFocalPoint(CoM) 00518 camera.SetPosition(np.array(normal) * 3) 00519 camera.SetParallelProjection(1) 00520 camera.SetViewUp(self.getViewUp(normal)) 00521 00522 ren = vtk.vtkRenderer() 00523 ren.SetActiveCamera(camera) 00524 ren.AddActor(actor) 00525 renWin = vtk.vtkRenderWindow() 00526 renWin.AddRenderer(ren) 00527 ren.SetBackground(1, 1, 1) 00528 renWin.SetSize(128,128) 00529 ren.Render() 00530 00531 winToImageFilter = vtk.vtkWindowToImageFilter() 00532 winToImageFilter.SetInput(renWin); 00533 00534 renWin.Finalize() 00535 winToImageFilter.Update() 00536 00537 #transform vtk image to opencv image 00538 binary = self.VTKtoOpenCVImg(winToImageFilter.GetOutput().GetPointData().GetScalars()) 00539 00540 if stereo: 00541 #dilate image to make it cleaner 00542 cv.Not(binary,binary) 00543 cv.Threshold(binary, binary, 20, 255, cv.CV_THRESH_BINARY) 00544 cv.Dilate(binary,binary,None,2) 00545 #cv.Dilate(binary,binary,None,1) 00546 cv.Erode(binary,binary,None,1) 00547 cv.Erode(binary,binary,None,1) 00548 cv.Not(binary,binary) 00549 00550 cv.SaveImage(fileName + ".jpg", binary) 00551 #jw = vtk.vtkJPEGWriter(); 00552 #jw.SetInput(winToImageFilter.GetOutput()); 00553 00554 #jw.SetFileName(fileName + ".jpg"); 00555 #jw.Write(); 00556 00557 return binary 00558 00559 #kept in older versions!! 00560 #def displayProjectWithMainAxes(self, points1, points2, axes1, axes2, mapper1, mapper2): 00561 #def displayProjectedWithMainAxes(self, points1, axes1, mapper1): 00562 00563 def displayProjectedWithMapper(self, points1, mapper1): 00564 pointsActor1 = vtk.vtkActor() 00565 pointsMapper1 = self.getPointsActor(points1) 00566 pointsActor1.SetMapper(pointsMapper1) 00567 pointsActor1.GetProperty().SetColor(1,0,0) 00568 pointsActor1.GetProperty().SetOpacity(0.5) 00569 ren = vtk.vtkRenderer() 00570 renWin = vtk.vtkRenderWindow() 00571 renWin.AddRenderer(ren) 00572 iren = vtk.vtkRenderWindowInteractor() 00573 iren.SetRenderWindow(renWin) 00574 00575 act1 = vtk.vtkActor() 00576 act1.GetProperty().SetColor(1,1,0) 00577 act1.GetProperty().SetOpacity(0.5) 00578 act1.SetMapper(mapper1) 00579 00580 ren.AddActor(pointsActor1) 00581 #ren.AddActor(planeActor1) 00582 #ren.AddActor(planeActor11) 00583 ren.AddActor(act1) 00584 00585 ren.SetBackground(1, 1, 1) 00586 renWin.SetSize(600, 600) 00587 00588 iren.Initialize() 00589 renWin.Render() 00590 iren.Start() 00591 00592 renWin.Finalize() 00593 00594 def getPolygonMapper(self, points1, height): 00595 polygonPoints = vtk.vtkPoints() 00596 polygonPoints.SetNumberOfPoints(len(points1)) 00597 aPolygon = vtk.vtkPolygon() 00598 aPolygon.GetPointIds().SetNumberOfIds(len(points1)) 00599 i=0 00600 for p1 in points1: 00601 polygonPoints.InsertPoint(i, p1[0], height, p1[1]) 00602 aPolygon.GetPointIds().SetId(i, i) 00603 i+=1 00604 00605 aPolygonGrid = vtk.vtkUnstructuredGrid() 00606 aPolygonGrid.Allocate(1, 1) 00607 aPolygonGrid.InsertNextCell(aPolygon.GetCellType(), aPolygon.GetPointIds()) 00608 aPolygonGrid.SetPoints(polygonPoints) 00609 aPolygonMapper = vtk.vtkDataSetMapper() 00610 aPolygonMapper.SetInput(aPolygonGrid) 00611 return aPolygonMapper 00612 00613 def visualizePolygon(self, points1): 00614 00615 aPolygonMapper = self.getPolygonMapper(points1, 0.1) 00616 00617 aPolygonActor = vtk.vtkActor() 00618 aPolygonActor.SetMapper(aPolygonMapper) 00619 aPolygonActor.AddPosition(0, 0, 0) 00620 aPolygonActor.GetProperty().SetDiffuseColor(0, 0, 0) 00621 00622 ren = vtk.vtkRenderer() 00623 renWin = vtk.vtkRenderWindow() 00624 renWin.AddRenderer(ren) 00625 iren = vtk.vtkRenderWindowInteractor() 00626 iren.SetRenderWindow(renWin) 00627 00628 ren.AddActor(aPolygonActor) 00629 00630 ren.SetBackground(1, 1, 1) 00631 renWin.SetSize(600, 600) 00632 00633 iren.Initialize() 00634 renWin.Render() 00635 iren.Start() 00636 00637 renWin.Finalize() 00638 00639 def getMapperFromPointCloud(self, points): 00640 pointsMapper = self.getPointsActor3D(points, 0.005) 00641 return pointsMapper 00642 00643 def visStereoAnd3DModel(self, points, plyFile): 00644 pointsActor1 = vtk.vtkActor() 00645 pointsMapper1 = self.getPointsActor3D(points, 0.005) 00646 pointsActor1.SetMapper(pointsMapper1) 00647 pointsActor1.GetProperty().SetColor(1,0,0) 00648 pointsActor1.GetProperty().SetOpacity(0.5) 00649 00650 reader = vtk.vtkPLYReader() 00651 reader.SetFileName(plyFile) 00652 00653 lineMapper2 = vtk.vtkPolyDataMapper() 00654 lineMapper2.SetInputConnection(reader.GetOutputPort()) 00655 lineMapper2.Update() 00656 00657 lineActor = vtk.vtkActor() 00658 lineActor.GetProperty().SetColor(0,1,0) 00659 lineActor.GetProperty().SetOpacity(1) 00660 lineActor.SetMapper(lineMapper2) 00661 00662 ren = vtk.vtkRenderer() 00663 renWin = vtk.vtkRenderWindow() 00664 renWin.AddRenderer(ren) 00665 iren = vtk.vtkRenderWindowInteractor() 00666 iren.SetRenderWindow(renWin) 00667 00668 ren.AddActor(lineActor) 00669 ren.AddActor(pointsActor1) 00670 00671 ren.SetBackground(1, 1, 1) 00672 renWin.SetSize(600, 600) 00673 00674 iren.Initialize() 00675 renWin.Render() 00676 iren.Start() 00677 00678 renWin.Finalize() 00679 00680 def visModels(self, mapper1, mapper2): 00681 00682 bounds = mapper1.GetInput().GetBounds() #compute bounds again 00683 #print bounds 00684 00685 act1 = vtk.vtkActor() 00686 act1.GetProperty().SetColor(0,1,0) 00687 act1.GetProperty().SetOpacity(1) 00688 act1.SetMapper(mapper1) 00689 00690 act2 = vtk.vtkActor() 00691 act2.GetProperty().SetColor(1,0,0) 00692 act2.GetProperty().SetOpacity(0.5) 00693 act2.SetMapper(mapper2) 00694 00695 ground = vtk.vtkPlaneSource() 00696 ground.SetNormal([0, 1, 0]) 00697 ground.SetCenter([0, 0, 0]) 00698 00699 planeMapper2 = vtk.vtkPolyDataMapper() 00700 planeMapper2.SetInputConnection(ground.GetOutputPort()) 00701 planeActor2 = vtk.vtkActor() 00702 planeActor2.SetMapper(planeMapper2) 00703 planeActor2.GetProperty().SetColor(1,0,1) 00704 planeActor2.GetProperty().SetOpacity(0.5) 00705 planeActor2.SetScale(max(bounds)*3) 00706 00707 ren = vtk.vtkRenderer() 00708 camera = vtk.vtkCamera() 00709 camera.SetFocalPoint([0,0,0]) 00710 camera.SetPosition([0,4,0]) 00711 camera.SetParallelProjection(1) 00712 #camera.SetViewUp([1,0,0]) 00713 #ren.SetActiveCamera(camera) 00714 renWin = vtk.vtkRenderWindow() 00715 renWin.AddRenderer(ren) 00716 iren = vtk.vtkRenderWindowInteractor() 00717 iren.SetRenderWindow(renWin) 00718 00719 #act1.RotateX(180) 00720 #act2.RotateX(180) 00721 00722 ren.AddActor(act1) 00723 ren.AddActor(act2) 00724 ren.AddActor(planeActor2) 00725 00726 ren.SetBackground(1, 1, 1) 00727 renWin.SetSize(600, 600) 00728 00729 iren.Initialize() 00730 renWin.Render() 00731 iren.Start() 00732 00733 renWin.Finalize() 00734 00735 def visModelsAndPoints(self, mapper1, mapper2, points): 00736 00737 bounds = mapper1.GetInput().GetBounds() #compute bounds again 00738 #print bounds 00739 00740 act1 = vtk.vtkActor() 00741 act1.GetProperty().SetColor(0,1,0) 00742 act1.GetProperty().SetOpacity(1) 00743 act1.SetMapper(mapper1) 00744 00745 act2 = vtk.vtkActor() 00746 act2.GetProperty().SetColor(1,0,0) 00747 act2.GetProperty().SetOpacity(0.5) 00748 act2.SetMapper(mapper2) 00749 00750 ground = vtk.vtkPlaneSource() 00751 ground.SetNormal([0, 1, 0]) 00752 ground.SetCenter([0, 0, 0]) 00753 00754 planeMapper2 = vtk.vtkPolyDataMapper() 00755 planeMapper2.SetInputConnection(ground.GetOutputPort()) 00756 planeActor2 = vtk.vtkActor() 00757 planeActor2.SetMapper(planeMapper2) 00758 planeActor2.GetProperty().SetColor(1,0,1) 00759 planeActor2.GetProperty().SetOpacity(0.3) 00760 planeActor2.SetScale(max(bounds)*3) 00761 00762 ren = vtk.vtkRenderer() 00763 camera = vtk.vtkCamera() 00764 camera.SetFocalPoint([0,0,0]) 00765 camera.SetPosition([0,4,0]) 00766 camera.SetParallelProjection(1) 00767 #camera.SetViewUp([1,0,0]) 00768 #ren.SetActiveCamera(camera) 00769 renWin = vtk.vtkRenderWindow() 00770 renWin.AddRenderer(ren) 00771 iren = vtk.vtkRenderWindowInteractor() 00772 iren.SetRenderWindow(renWin) 00773 00774 pointActor = vtk.vtkActor() 00775 pointsMapper1 = self.getPointsActor3D(points, 0.05) 00776 pointActor.SetMapper(pointsMapper1) 00777 pointActor.GetProperty().SetColor(1,0,0) 00778 pointActor.GetProperty().SetOpacity(0.5) 00779 00780 ren.AddActor(act1) 00781 ren.AddActor(act2) 00782 ren.AddActor(planeActor2) 00783 ren.AddActor(pointActor) 00784 00785 ren.SetBackground(1, 1, 1) 00786 renWin.SetSize(600, 600) 00787 00788 iren.Initialize() 00789 renWin.Render() 00790 iren.Start() 00791 00792 renWin.Finalize() 00793 00794 def visModelsAndPointsAndDescriptor(self, mapper1, mapper2, points, descActor1,descActor2): 00795 00796 bounds = mapper1.GetInput().GetBounds() #compute bounds again 00797 #print bounds 00798 00799 act1 = vtk.vtkActor() 00800 act1.GetProperty().SetColor(0,1,0) 00801 act1.GetProperty().SetOpacity(0.5) 00802 act1.SetMapper(mapper1) 00803 00804 act2 = vtk.vtkActor() 00805 act2.GetProperty().SetColor(1,0,0) 00806 act2.GetProperty().SetOpacity(0.5) 00807 act2.SetMapper(mapper2) 00808 00809 ground = vtk.vtkPlaneSource() 00810 ground.SetNormal([0, 1, 0]) 00811 ground.SetCenter([0, 0, 0]) 00812 00813 planeMapper2 = vtk.vtkPolyDataMapper() 00814 planeMapper2.SetInputConnection(ground.GetOutputPort()) 00815 planeActor2 = vtk.vtkActor() 00816 planeActor2.SetMapper(planeMapper2) 00817 planeActor2.GetProperty().SetColor(1,0,1) 00818 planeActor2.GetProperty().SetOpacity(0.3) 00819 planeActor2.SetScale(max(bounds)*3) 00820 00821 ren = vtk.vtkRenderer() 00822 camera = vtk.vtkCamera() 00823 camera.SetFocalPoint([0,0,0]) 00824 camera.SetPosition([0,4,0]) 00825 camera.SetParallelProjection(1) 00826 #camera.SetViewUp([1,0,0]) 00827 #ren.SetActiveCamera(camera) 00828 renWin = vtk.vtkRenderWindow() 00829 renWin.AddRenderer(ren) 00830 iren = vtk.vtkRenderWindowInteractor() 00831 iren.SetRenderWindow(renWin) 00832 00833 pointActor = vtk.vtkActor() 00834 pointsMapper1 = self.getPointsActor3D(points, 0.05) 00835 pointActor.SetMapper(pointsMapper1) 00836 pointActor.GetProperty().SetColor(1,0,0) 00837 pointActor.GetProperty().SetOpacity(0.5) 00838 00839 #print descActor1.GetPosition() 00840 #print act2.GetPosition() 00841 descActor1.SetPosition(-0.75,0.25,0.75) 00842 act2.SetPosition(-0.75,0.25,0.75) 00843 00844 ren.AddActor(act1) 00845 ren.AddActor(act2) 00846 #ren.AddActor(planeActor2) 00847 #ren.AddActor(pointActor) 00848 ren.AddActor(descActor1) 00849 ren.AddActor(descActor2) 00850 00851 ren.SetBackground(1, 1, 1) 00852 renWin.SetSize(600, 600) 00853 00854 iren.Initialize() 00855 renWin.Render() 00856 iren.Start() 00857 00858 renWin.Finalize() 00859 00860 def visModelsWithOutPlane(self, mapper1, mapper2): 00861 00862 bounds = mapper1.GetInput().GetBounds() #compute bounds again 00863 #print bounds 00864 00865 act1 = vtk.vtkActor() 00866 act1.GetProperty().SetColor(0,1,0) 00867 act1.GetProperty().SetOpacity(1) 00868 act1.SetMapper(mapper1) 00869 00870 act2 = vtk.vtkActor() 00871 act2.GetProperty().SetColor(1,0,0) 00872 act2.GetProperty().SetOpacity(1) 00873 act2.SetMapper(mapper2) 00874 00875 ren = vtk.vtkRenderer() 00876 #ren.SetActiveCamera(camera) 00877 renWin = vtk.vtkRenderWindow() 00878 renWin.AddRenderer(ren) 00879 iren = vtk.vtkRenderWindowInteractor() 00880 iren.SetRenderWindow(renWin) 00881 00882 ren.AddActor(act1) 00883 ren.AddActor(act2) 00884 00885 ren.SetBackground(1, 1, 1) 00886 renWin.SetSize(600, 600) 00887 00888 iren.Initialize() 00889 renWin.Render() 00890 iren.Start() 00891 00892 renWin.Finalize() 00893 00894 def visModelsAndTwoPlanes(self, mapper1, mapper2, n1,n2): 00895 00896 bounds = mapper1.GetInput().GetBounds() #compute bounds again 00897 #print bounds 00898 00899 act1 = vtk.vtkActor() 00900 act1.GetProperty().SetColor(0,1,0) 00901 act1.GetProperty().SetOpacity(0.5) 00902 act1.SetMapper(mapper1) 00903 00904 act2 = vtk.vtkActor() 00905 act2.GetProperty().SetColor(1,0,0) 00906 act2.GetProperty().SetOpacity(0.5) 00907 act2.SetMapper(mapper2) 00908 00909 ground = vtk.vtkPlaneSource() 00910 ground.SetNormal([0, 1, 0]) 00911 ground.SetCenter([0, 0, 0]) 00912 00913 planeMapper2 = vtk.vtkPolyDataMapper() 00914 planeMapper2.SetInputConnection(ground.GetOutputPort()) 00915 planeActor2 = vtk.vtkActor() 00916 planeActor2.SetMapper(planeMapper2) 00917 planeActor2.GetProperty().SetColor(1,0,1) 00918 planeActor2.GetProperty().SetOpacity(0.5) 00919 planeActor2.SetScale(max(bounds)*3) 00920 00921 ren = vtk.vtkRenderer() 00922 renWin = vtk.vtkRenderWindow() 00923 renWin.AddRenderer(ren) 00924 iren = vtk.vtkRenderWindowInteractor() 00925 iren.SetRenderWindow(renWin) 00926 00927 act1.RotateX(180) 00928 act2.RotateX(180) 00929 00930 ren.AddActor(act1) 00931 ren.AddActor(act2) 00932 #ren.AddActor(planeActor2) 00933 00934 ground1 = vtk.vtkPlaneSource() 00935 ground1.SetNormal(n1) 00936 ground1.SetCenter([0, 0, 0]) 00937 00938 planeGround1 = vtk.vtkPolyDataMapper() 00939 planeGround1.SetInputConnection(ground1.GetOutputPort()) 00940 planeActorG1 = vtk.vtkActor() 00941 planeActorG1.SetMapper(planeGround1) 00942 planeActorG1.GetProperty().SetColor(0,1,0) 00943 planeActorG1.GetProperty().SetOpacity(0.5) 00944 00945 ground2 = vtk.vtkPlaneSource() 00946 ground2.SetNormal(n2) 00947 ground2.SetCenter([0, 0, 0]) 00948 00949 planeGround2 = vtk.vtkPolyDataMapper() 00950 planeGround2.SetInputConnection(ground2.GetOutputPort()) 00951 planeActorG2 = vtk.vtkActor() 00952 planeActorG2.SetMapper(planeGround2) 00953 planeActorG2.GetProperty().SetColor(1,0,0) 00954 planeActorG2.GetProperty().SetOpacity(0.5) 00955 00956 ren.AddActor(planeActorG1) 00957 ren.AddActor(planeActorG2) 00958 00959 ren.SetBackground(1, 1, 1) 00960 renWin.SetSize(600, 600) 00961 00962 iren.Initialize() 00963 renWin.Render() 00964 iren.Start() 00965 00966 renWin.Finalize()