diff --git a/vtk/README.md b/vtk/README.md index 72ef54b..6332205 100644 --- a/vtk/README.md +++ b/vtk/README.md @@ -5,5 +5,39 @@ VTK was initially created in 1993 as companion software to the book The Visualization Toolkit: An Object-Oriented Approach to 3D Graphics.[5] The book and software were written by three researchers (Will Schroeder, Ken Martin and Bill Lorensen) on their own time and with permission from General Electric (thus the ownership of the software resided with, and continues to reside with, the authors). After the core of VTK was written, users and developers around the world began to improve and apply the system to real-world problems.[3] https://en.wikipedia.org/wiki/VTK +## Visualisation pipeline (OpenGL pipeline) +VTK made use of the following pipeline, which seems to be inspired by OpenGL pipeline, to contruct geometric representations that are rendendered by the graphics pipeline: +1. SOURCE/READER: Load data, use source classes (vtkSphereSource, vtkCubeSource, vtkConeSource), or read data from files (Polydata, StructuredGrid). + * VTK Data objects: – vtkImageData, vtkRectilinearGrid, vtkStructuredGrid, vtkPolyData, vtkUnstructuredGrid + * Usually you create an iterator: `cube = vtk.vtkCubeSource()` and you can accesss vtkPolyData instance `polyData = cube.GetOutput()` +2. FILTER: Takes data, modify it and returns the modified data. + * Select data of particular size, strenght, intensity, + * Process 2D/3D images or polygon messhes + * Generate geometric objects from data +3. MAPPER: Maps data to graphics primitives that can be displayed by the renderer. + * Multiple mappers may share the same input but render in different way + * Usual mapper: vtkPolyDataMapper + * Set connection between source and mapper + * create mapper: `cubeMapper = vtk.vtkPolyDataMapper()` + * stablish connection: `cubeMapper.SetInputConnection(cube.GetOutputPort())` +4. ACTOR: represents and object in a rendenring scene + * Rendering properties is generated by the properties configuation (scale, orientation, textures) +5. RENDERER: controls the rendering process for actors and scenes + * Perform coordinate transformation between multiplate coordinates (world, view and camera). + * `vtkRenderWindow` class creates a window for renderers to draw into +6. INTERACTORS: + * The `vtkRenderWindowInteractor` class provides platform-independent window interaction via the mouse and keyboard + +* Launch and see further details in [cube-vtk.py](cube-vtk.py) +``` +conda activate bareVE +python cube-vtk.py +``` +![fig](vtk_pipeline.png) +See https://mt4sd.ulpgc.es/slicer-int/images/7/71/VTK.pdf for further reference. + + ## References -https://kitware.github.io/vtk-examples/site/Python/#tutorial +* https://kitware.github.io/vtk-examples/site/Python/#tutorial +* https://www.toptal.com/data-science/3d-data-visualization-with-open-source-tools-an-example-using-vtk + diff --git a/vtk/cube-vtk.py b/vtk/cube-vtk.py new file mode 100644 index 0000000..7f1389a --- /dev/null +++ b/vtk/cube-vtk.py @@ -0,0 +1,76 @@ +## References +### PAGE15 https://mt4sd.ulpgc.es/slicer-int/images/7/71/VTK.pdf +### step5.py https://kitware.github.io/vtk-examples/site/Python/#tutorial + +import vtkmodules.vtkRenderingOpenGL2 +from vtkmodules.vtkFiltersSources import vtkCubeSource +from vtkmodules.vtkInteractionStyle import vtkInteractorStyleTrackballCamera +from vtkmodules.vtkRenderingCore import ( + vtkActor, + vtkPolyDataMapper, + vtkRenderWindow, + vtkRenderWindowInteractor, + vtkRenderer +) + +def main(): + ##SOURCE/READER + # Create an interactor + cube = vtkCubeSource() + polyData = cube.GetOutput() + #print(polyData) + + ##MAPPER + cubeMapper = vtkPolyDataMapper() + cubeMapper.SetInputConnection(cube.GetOutputPort()) + + ##ACTOR + cubeActor = vtkActor() + cubeActor.SetMapper(cubeMapper) + cubeActor.GetProperty().SetColor(1.0, 0.0, 0.0) # make the cube red + + ##RENDERER + ### Create a renderer and add the cube actor to it + renderer = vtkRenderer() + renderer.AddActor(cubeActor) + renderer.SetBackground(0.0, 0.0, 0.0) # make the background black + + ##Render window + renderWindow = vtkRenderWindow() + renderWindow.AddRenderer(renderer) + renderWindow.SetSize(500, 500) + renderWindow.SetWindowName('Tutorial_Step1') + + # The vtkRenderWindowInteractor class watches for events (e.g., keypress, + # mouse) in the vtkRenderWindow. These events are translated into + # event invocations that VTK understands (see VTK/Common/vtkCommand.h + # for all events that VTK processes). Then observers of these VTK + # events can process them as appropriate. + iterator_renderer = vtkRenderWindowInteractor() + iterator_renderer.SetRenderWindow(renderWindow) + + # By default the vtkRenderWindowInteractor instantiates an instance + # of vtkInteractorStyle. vtkInteractorStyle translates a set of events + # it observes into operations on the camera, actors, and/or properties + # in the vtkRenderWindow associated with the vtkRenderWinodwInteractor. + # Here we specify a particular interactor style. + style = vtkInteractorStyleTrackballCamera() + iterator_renderer.SetInteractorStyle(style) + + + # The user can use the mouse + # and keyboard to perform the operations on the scene according to the + # current interaction style. When the user presses the 'e' key, by default + # an ExitEvent is invoked by the vtkRenderWindowInteractor which is caught + # and drops out of the event loop (triggered by the Start() method that + # follows. + # You can also use + # q to quit + # w to show lines + # 3 to swap colours + iterator_renderer.Initialize() + iterator_renderer.Start() + + +if __name__ == '__main__': + main() diff --git a/vtk/vtk_pipeline.png b/vtk/vtk_pipeline.png new file mode 100644 index 0000000..e566b0f Binary files /dev/null and b/vtk/vtk_pipeline.png differ