def __updatePlatform(self): qDebug('RendererHelper::__updatePlatform()') #* Platform Model if self.__m_platformModel: self.__m_platformModel.SetXLength(self.__m_platformWidth) self.__m_platformModel.SetYLength(self.__m_platformDepth) self.__m_platformModel.SetZLength(self.__m_platformThickness) self.__m_platformModel.SetCenter(0.0, 0.0, -self.__m_platformThickness / 2) #* Platform Grid gridPoints = vtk.vtkPoints() gridCells = vtk.vtkCellArray() i = -self.__m_platformWidth / 2 while i <= self.__m_platformWidth / 2: self.__createLine(i, -self.__m_platformDepth / 2, self.__m_gridBottomHeight, i, self.__m_platformDepth / 2, self.__m_gridBottomHeight, gridPoints, gridCells) i += self.__m_gridSize i = -self.__m_platformDepth / 2 while i <= self.__m_platformDepth / 2: self.__createLine(-self.__m_platformWidth / 2, i, self.__m_gridBottomHeight, self.__m_platformWidth / 2, i, self.__m_gridBottomHeight, gridPoints, gridCells) i += self.__m_gridSize self.__m_platformGrid.SetPoints(gridPoints) self.__m_platformGrid.SetLines(gridCells)
def __init__(self): qDebug('RendererHelper::__init__()') super().__init__() self.__m_vtkFboItem = None #self.gl = QOpenGLFunctions() self.gl = gl self.__m_mouseLeftButton: QMouseEvent = None self.__m_mouseEvent: QMouseEvent = None self.__m_moveEvent: QMouseEvent = None self.__m_wheelEvent: QWheelEvent = None self.__m_firstRender: bool = True self.renderWindow: vtk.vtkGenericOpenGLRenderWindow = vtk.vtkGenericOpenGLRenderWindow( ) self.renderer: vtk.vtkRenderer = vtk.vtkRenderer() self.renderWindow.AddRenderer(self.renderer) #* Interactor self.interactor: vtk.vtkGenericRenderWindowInteractor = vtk.vtkGenericRenderWindowInteractor( ) self.interactor.EnableRenderOff() self.renderWindow.SetInteractor(self.interactor) #* Initialize the OpenGL context for the renderer self.renderWindow.OpenGLInitContext() #* Interactor Style style = vtk.vtkInteractorStyleTrackballCamera() style.SetDefaultRenderer(self.renderer) style.SetMotionFactor(10.0) self.interactor.SetInteractorStyle(style)
def initScene(self): qDebug('RendererHelper::initScene()') #* Top background color r2 = 245.0 / 255.0 g2 = 245.0 / 255.0 b2 = 245.0 / 255.0 #* Bottom background color r1 = 170.0 / 255.0 g1 = 170.0 / 255.0 b1 = 170.0 / 255.0 self.renderer.SetBackground(r2, g2, b2) self.renderer.SetBackground2(r1, g1, b1) self.renderer.GradientBackgroundOn() # #* Axes axes = vtk.vtkAxesActor() axes_length = 50.0 axes_label_font_size = np.int16(20) axes.SetTotalLength(axes_length, axes_length, axes_length) axes.SetCylinderRadius(0.01) axes.SetShaftTypeToCylinder() axes.GetXAxisCaptionActor2D().GetTextActor().SetTextScaleModeToNone() axes.GetYAxisCaptionActor2D().GetTextActor().SetTextScaleModeToNone() axes.GetZAxisCaptionActor2D().GetTextActor().SetTextScaleModeToNone() axes.GetXAxisCaptionActor2D().GetCaptionTextProperty().SetFontSize(axes_label_font_size) axes.GetYAxisCaptionActor2D().GetCaptionTextProperty().SetFontSize(axes_label_font_size) axes.GetZAxisCaptionActor2D().GetCaptionTextProperty().SetFontSize(axes_label_font_size) self.renderer.AddActor(axes) # Platform self.__generatePlatform()
def __generatePlatform(self): qDebug('RendererHelper::__generatePlatform()') #* Platform Model platformModelMapper = vtk.vtkPolyDataMapper() self.__m_platformModel = vtk.vtkCubeSource() platformModelMapper.SetInputConnection(self.__m_platformModel.GetOutputPort()) self.__m_platformModelActor = vtk.vtkActor() self.__m_platformModelActor.SetMapper(platformModelMapper) self.__m_platformModelActor.GetProperty().SetColor(1, 1, 1) self.__m_platformModelActor.GetProperty().LightingOn() self.__m_platformModelActor.GetProperty().SetOpacity(1) self.__m_platformModelActor.GetProperty().SetAmbient(0.45) self.__m_platformModelActor.GetProperty().SetDiffuse(0.4) self.__m_platformModelActor.PickableOff() self.renderer.AddActor(self.__m_platformModelActor) #* Platform Grid self.__m_platformGrid = vtk.vtkPolyData() platformGridMapper = vtk.vtkPolyDataMapper() platformGridMapper.SetInputData(self.__m_platformGrid) self.__m_platformGridActor = vtk.vtkActor() self.__m_platformGridActor.SetMapper(platformGridMapper) self.__m_platformGridActor.GetProperty().LightingOff() self.__m_platformGridActor.GetProperty().SetColor(0.45, 0.45, 0.45) self.__m_platformGridActor.GetProperty().SetOpacity(1) self.__m_platformGridActor.PickableOff() self.renderer.AddActor(self.__m_platformGridActor) self.__updatePlatform()
def onWidgetRegistered(self, rot, normal, p): global myDebug qDebug('onWidgetRegisterede') U = rot[1] sgn = np.sign(np.dot(U, normal)) udotn = float("{:.5f}".format(np.abs(np.dot(U, normal)))) self.leUdotNErr.setText(str(udotn)) trans = float("{:.5f}".format(np.sqrt(np.sum(p**2)))) self.leTransDistErr.setText(str(trans)) arg = rot[0] arg = np.fmod(arg + 180.0, 360.0) - 180.0 arg = float("{:.5f}".format(arg * sgn)) self.leAngleErr.setText(str(arg))
def initVessels(self): qDebug('initVessels()') if os.name == 'nt': filename = os.path.join(filedir, '../../data/Abdomen/Connected.vtp') if IOUSFAN: #filename = 'e:/analogic/TrialVTK/data/VesselMeshData.vtk' filename = 'e:/analogic/TrialVTK/data/LiverVesselMeshData.vtk' else: filename = '/home/jmh/bkmedical/data/CT/Connected.vtp' # read data if IOUSFAN: reader = vtk.vtkGenericDataObjectReader() else: reader = vtk.vtkXMLPolyDataReader() reader.SetFileName(filename) reader.Update() connectFilter = vtk.vtkPolyDataConnectivityFilter() connectFilter.SetInputConnection(reader.GetOutputPort()) connectFilter.SetExtractionModeToLargestRegion() connectFilter.Update() self.vesselPolyData = self.scale(connectFilter.GetOutput()) # compute normals self.vesselNormals = vtk.vtkPolyDataNormals() self.vesselNormals.SetInputData(self.vesselPolyData) # mapper mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(self.vesselNormals.GetOutputPort()) # actor for vessels self.vessels = vtk.vtkActor() self.vessels.SetMapper(mapper) prop = self.vessels.GetProperty() if self.brighter25: prop.SetColor(vtk.vtkColor3d(hexCol("#517487"))) # 25% lighter else: prop.SetColor(vtk.vtkColor3d(hexCol("#415d6c"))) # assign actor to the renderer self.renderer.AddActor(self.vessels)
def initScene(self): qDebug('initScene()') self.renderer = vtk.vtkOpenGLRenderer() self.render_window = self.interactor.GetRenderWindow() self.render_window.AddRenderer(self.renderer) self.cellPicker = vtk.vtkCellPicker() self.cellPicker.SetTolerance(30.0) self.interactor.SetPicker(self.cellPicker) #* Top background color bg_t = np.ones(3) * 245.0 / 255.0 #* Bottom background color bg_b = np.ones(3) * 170.0 / 255.0 self.renderer.SetBackground(bg_t) self.renderer.SetBackground2(bg_b) self.renderer.GradientBackgroundOn()
def initLiver(self): qDebug('initLiver()') if os.name == 'nt': filename = os.path.join( filedir, '../../data/Abdomen/Liver_3D-interpolation.vtp') if IOUSFAN: filename = 'e:/analogic/TrialVTK/data/segmented_liver_ITK_snap.vtk' else: filename = '/home/jmh/bkmedical/data/CT/Liver_3D-interpolation.vtp' if IOUSFAN: reader = vtk.vtkGenericDataObjectReader() else: reader = vtk.vtkXMLPolyDataReader() reader.SetFileName(filename) reader.Update() connectFilter = vtk.vtkPolyDataConnectivityFilter() connectFilter.SetInputConnection(reader.GetOutputPort()) connectFilter.SetExtractionModeToLargestRegion() connectFilter.Update() surfNormals = vtk.vtkPolyDataNormals() surfNormals.SetInputData(self.scale(connectFilter.GetOutput())) #Create a mapper and actor mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(surfNormals.GetOutputPort()) # was reader self.liver = vtk.vtkActor() self.liver.SetMapper(mapper) prop = self.liver.GetProperty() if self.brighter25: prop.SetColor(vtk.vtkColor3d(hexCol("#873927"))) else: prop.SetColor(vtk.vtkColor3d(hexCol("#6c2e1f"))) prop.SetOpacity(self.opacity) self.renderer.AddActor(self.liver)
def selectModel(self, x, y): qDebug('QVTKFramebufferObjectRenderer::selectModel()')
def __init__(self, sys_argv): super().__init__() self.__m_vtkFboItem = None #sys_argv += ['--style', 'Material'] #! MUST HAVE #sys_argv += ['--style', 'Fusion'] #! MUST HAVE sys_argv += ['--style', 'Windows'] #! MUST HAVE QApplication.setAttribute(Qt.AA_UseDesktopOpenGL) QtGui.QSurfaceFormat.setDefaultFormat( defaultFormat(False)) # from vtk 8.2.0 app = QApplication(sys_argv) app.setApplicationName("QtQuickVTK") app.setWindowIcon(QIcon(":/resources/bq.ico")) app.setOrganizationName("Sexy Soft") app.setOrganizationDomain("www.sexysoft.com") engine = QQmlApplicationEngine() app.setApplicationName('QtVTK-Py') # Register QML Types qmlRegisterType(FboItem, 'QtVTK', 1, 0, 'VtkFboItem') # Expose/Bind Python classes (QObject) to QML ctxt = engine.rootContext() # returns QQmlContext ctxt.setContextProperty('canvasHandler', self) self.dataProvider = ChartDataProvider() ctxt.setContextProperty('chartDataProvider', self.dataProvider) # Load main QML file engine.load(QUrl.fromLocalFile('resources/main.qml')) # Get reference to the QVTKFramebufferObjectItem in QML rootObject = engine.rootObjects()[0] # returns QObject self.__m_vtkFboItem = rootObject.findChild(FboItem, 'vtkFboItem') # Give the vtkFboItem reference to the CanvasHandler if (self.__m_vtkFboItem): qDebug( 'CanvasHandler::CanvasHandler: setting vtkFboItem to CanvasHandler' ) self.__m_vtkFboItem.rendererInitialized.connect( self.startApplication) else: qCritical( 'CanvasHandler::CanvasHandler: Unable to get vtkFboItem instance' ) return MySettings = QSettings() print("load Settings") self.fileDialog = rootObject.findChild(QObject, "myFileDialog") if (self.fileDialog is not None): tmp = MySettings.value(CanvasHandler.DEFAULT_MODEL_DIR_KEY) print(tmp) self.fileDialog.setProperty("folder", QUrl.fromLocalFile(tmp)) rc = app.exec_() qDebug( f'CanvasHandler::CanvasHandler: Execution finished with return code: {rc}' )
def startApplication(self): qDebug('CanvasHandler::startApplication()') self.__m_vtkFboItem.rendererInitialized.disconnect( self.startApplication)
def mouseReleaseEvent(self, button: int, screenX: int, screenY: int): qDebug('CanvasHandler::mouseReleaseEvent()')
def initPlaneWidgets(self, index): qDebug('initPlaneWidgets()') center, htrans = self.getReferencePosition(index) hw = self.imgWidth shw = self.worldScale * hw source = vtk.vtkPlaneSource() source.SetOrigin(0, 0, 0) source.SetPoint1(shw, 0, 0) source.SetPoint2(0, shw, 0) transform = vtk.vtkTransform() mat = vtk.vtkMatrix4x4() for i in range(4): for j in range(4): mat.SetElement(i, j, htrans[i, j]) # Should transformation also be scaled?? for i in range(3): mat.SetElement(i, 3, self.worldScale * mat.GetElement(i, 3)) transform.SetMatrix(mat) transform.Update() origin = source.GetOrigin() origin = transform.TransformPoint(origin) source.SetOrigin(origin) p1 = source.GetPoint1() p1 = transform.TransformPoint(p1) source.SetPoint1(p1) p2 = source.GetPoint2() p2 = transform.TransformPoint(p2) source.SetPoint2(p2) source.Update() source.SetCenter(self.worldScale * center) source.Update() # Test position good for slice 17 (HACK) if (IOUSFAN): source.SetOrigin(-36.00039424299387, 58.447421532729656, 116.93018531955384) source.SetPoint1(13.731795848152041, 54.203001711976306, 119.87877296847647) source.SetPoint2(-40.18599847580337, 8.635225461941415, 115.82300881527104) source.Update() self.planeSources.append(source) ##################################### # Blue reference plane ##################################### # mapper if self.showReferencePlane: mapper0 = vtk.vtkPolyDataMapper() mapper0.SetInputConnection(source.GetOutputPort()) # actor refActor = vtk.vtkActor() refActor.SetMapper(mapper0) prop = refActor.GetProperty() prop.SetColor(blue) prop.SetOpacity(self.opacity) self.refplanes.append(refActor) self.renderer.AddActor(refActor) else: self.refplanes.append(None) ##################################### # Compute contours, tubes and clipping ##################################### tubes, oldContours = self.computeContoursAndTubes(source) edgeMapper = vtk.vtkPolyDataMapper() edgeMapper.ScalarVisibilityOff() edgeMapper.SetInputConnection(tubes.GetOutputPort()) planes = self.computeClippingPlanes(source) edgeMapper.SetClippingPlanes(planes) actor = vtk.vtkActor() actor.SetMapper(edgeMapper) prop = actor.GetProperty() prop.SetColor(green) prop.SetLineWidth(3) self.contours.append(actor) self.renderer.AddActor(actor) ################################################### # Plane widget for interaction ################################################### planeWidget = vtk.vtkPlaneWidget() planeWidget.SetInteractor(self.interactor) planeWidget.SetOrigin(source.GetOrigin()) planeWidget.SetPoint1(source.GetPoint1()) planeWidget.SetPoint2(source.GetPoint2()) prop = planeWidget.GetHandleProperty() prop.SetColor(QLiverViewer.colors.GetColor3d("Red")) prop = planeWidget.GetPlaneProperty() prop.SetColor(QLiverViewer.colors.GetColor3d("Red")) # Original position and orientation of reference plane self.lastPositions['reset'][index] = [ planeWidget.GetOrigin(), planeWidget.GetPoint1(), planeWidget.GetPoint2(), planeWidget.GetCenter(), np.array(planeWidget.GetNormal()) ] # Redundant print('normal') print(planeWidget.GetNormal()) planeWidget.SetEnabled(1) planeWidget.AddObserver(vtk.vtkCommand.EndInteractionEvent, self.onWidgetMoved, 1.0) attempt = vtk.vtkActor() self.fullcontours.append(oldContours) mapper = vtk.vtkPolyDataMapper() mapper.SetInputData(oldContours) attempt.SetMapper(mapper) attempt.GetProperty().SetColor(red) self.userAttempts.append(attempt) self.renderer.AddActor(attempt) lastNormal = planeWidget.GetNormal() lastAxis1 = vtk.vtkVector3d() vtk.vtkMath.Subtract(planeWidget.GetPoint1(), planeWidget.GetOrigin(), lastAxis1) lastOrigin = planeWidget.GetCenter() self.lastPositions['origin'][index] = lastOrigin self.lastPositions['normal'][index] = lastNormal self.lastPositions['axis1'][index] = lastAxis1 self.planeWidgets.append(planeWidget) self.render_window.Render()
def resetCamera(self): qDebug('resetCamera') self.renderer.ResetCamera()