change ploydata color using vtk.vtkUnsignedCharArray dynamically not re-rendering

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

change ploydata color using vtk.vtkUnsignedCharArray dynamically not re-rendering

Abhishek-2
Greetings,
I am new to VTK so may be my question is very basic.

I have following code, where the aim is to be able to change the lines colors using dynamically based on data we get from QColorDialog. if I apply the color directly to vtkActor and re-render the window then it just works fine. But eventually I wan to be able to change individual line Points color and thats why I am setting self.linesPolyData.GetCellData().SetScalars(self.colors) when I change the color using QPushButton in function def select_color(self): it doesn't re-render the lines with new color. What am I doing wrong?

from Analysis.Channel import *
import vtk
import numpy as np
import itertools
import sys
import math

from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QScrollArea, QPushButton, QColorDialog
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor



# Qt Class which will render the objects
class MainWindow(QWidget):
    def __init__(self, parent = None):
        super().__init__()
        self.layout = QGridLayout(self)
        self.color_button = QPushButton('Select Color')

        self.layout.addWidget(self.color_button)
        self.color_button.clicked.connect(self.select_color)

        self.color = [255, 255, 0, 255]
        self.colors = vtk.vtkUnsignedCharArray()
        self.colors.SetNumberOfComponents(4)

        # Create the polydata where we will store all the geometric data
        self.linesPolyData = vtk.vtkPolyData()

        # Create three points
        origin = [0.0, 0.0, 0.0]
        p0 = [1.0, 0.0, 0.0]
        p1 = [0.0, 1.0, 0.0]

        # Create a vtkPoints container and store the points in it
        pts = vtk.vtkPoints()
        pts.InsertNextPoint(origin)
        pts.InsertNextPoint(p0)
        pts.InsertNextPoint(p1)

        self.linesPolyData.SetPoints(pts)

        # Create the first line (between Origin and P0)
        line0 = vtk.vtkLine()
        line0.GetPointIds().SetId(0, 0)  # the second 0 is the index of the Origin in linesPolyData's points
        line0.GetPointIds().SetId(1, 1)  # the second 1 is the index of P0 in linesPolyData's points

        # Create the second line (between Origin and P1)
        line1 = vtk.vtkLine()
        line1.GetPointIds().SetId(0, 0)  # the second 0 is the index of the Origin in linesPolyData's points
        line1.GetPointIds().SetId(1, 2)  # 2 is the index of P1 in linesPolyData's points

        self.lines = vtk.vtkCellArray()
        self.lines.InsertNextCell(line0)
        self.lines.InsertNextCell(line1)
        # Add the lines to the polydata container
        self.linesPolyData.SetLines(self.lines)

        for i in range(0, self.lines.GetNumberOfCells()):
            self.colors.InsertNextTuple(self.color)

        self.linesPolyData.GetCellData().SetScalars(self.colors)

        self.mapper = vtk.vtkPolyDataMapper()
        self.mapper.SetInputData(self.linesPolyData)

        self.actor = vtk.vtkActor()
        self.actor.SetMapper(self.mapper)
        self.actor.GetProperty().SetLineWidth(4)

        self.vtk_widget = QVTKRenderWindowInteractor()
        # vtk_widget.setMinimumSize(round(1024/3), round(1024/3))
        ren = vtk.vtkRenderer()
        self.vtk_widget.GetRenderWindow().AddRenderer(ren)

        ren.AddActor(self.actor)

        ren.ResetCamera()
        interactor = self.vtk_widget.GetRenderWindow().GetInteractor()
        interactor.Initialize()
        interactor.Start()

        self.layout.addWidget(self.vtk_widget)

    def select_color(self):
        color = QColorDialog.getColor(QtCore.Qt.green, self)
        if color.isValid():
            self.color = color.getRgb()
            for i in range(0, self.lines.GetNumberOfCells()):
                self.colors.InsertNextTuple(self.color)

            self.linesPolyData.GetCellData().SetScalars(self.colors)
            self.mapper.SetInputData(self.linesPolyData)
            self.linesPolyData.Modified()
            self.mapper.Modified()
            # self.actor.GetProperty().Modified()
            self.vtk_widget.Render()



if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())

--
Abhishek
zeroth.me
--
Abhishek Patil
zeroth.me

_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers
Reply | Threaded
Open this post in threaded view
|

Re: change ploydata color using vtk.vtkUnsignedCharArray dynamically not re-rendering

David E DeMarle
Hi Ahishek,

It looks to me like you are appending to the end of the self.colors. Call self.colors.GetNumberOfTuples() to verify that.
If you fix (do replacement with Insertvalue(id, value) instead of InserNextValue(value)) I think it will do what you are trying to do.

hth




David E DeMarle
Kitware, Inc.
Principal Engineer
21 Corporate Drive
Clifton Park, NY 12065-8662
Phone: 518-881-4909

On Mon, Jun 4, 2018 at 8:35 PM, Abhishek <[hidden email]> wrote:
Greetings,
I am new to VTK so may be my question is very basic.

I have following code, where the aim is to be able to change the lines colors using dynamically based on data we get from QColorDialog. if I apply the color directly to vtkActor and re-render the window then it just works fine. But eventually I wan to be able to change individual line Points color and thats why I am setting self.linesPolyData.GetCellData().SetScalars(self.colors) when I change the color using QPushButton in function def select_color(self): it doesn't re-render the lines with new color. What am I doing wrong?

from Analysis.Channel import *
import vtk
import numpy as np
import itertools
import sys
import math

from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QScrollArea, QPushButton, QColorDialog
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor



# Qt Class which will render the objects
class MainWindow(QWidget):
    def __init__(self, parent = None):
        super().__init__()
        self.layout = QGridLayout(self)
        self.color_button = QPushButton('Select Color')

        self.layout.addWidget(self.color_button)
        self.color_button.clicked.connect(self.select_color)

        self.color = [255, 255, 0, 255]
        self.colors = vtk.vtkUnsignedCharArray()
        self.colors.SetNumberOfComponents(4)

        # Create the polydata where we will store all the geometric data
        self.linesPolyData = vtk.vtkPolyData()

        # Create three points
        origin = [0.0, 0.0, 0.0]
        p0 = [1.0, 0.0, 0.0]
        p1 = [0.0, 1.0, 0.0]

        # Create a vtkPoints container and store the points in it
        pts = vtk.vtkPoints()
        pts.InsertNextPoint(origin)
        pts.InsertNextPoint(p0)
        pts.InsertNextPoint(p1)

        self.linesPolyData.SetPoints(pts)

        # Create the first line (between Origin and P0)
        line0 = vtk.vtkLine()
        line0.GetPointIds().SetId(0, 0)  # the second 0 is the index of the Origin in linesPolyData's points
        line0.GetPointIds().SetId(1, 1)  # the second 1 is the index of P0 in linesPolyData's points

        # Create the second line (between Origin and P1)
        line1 = vtk.vtkLine()
        line1.GetPointIds().SetId(0, 0)  # the second 0 is the index of the Origin in linesPolyData's points
        line1.GetPointIds().SetId(1, 2)  # 2 is the index of P1 in linesPolyData's points

        self.lines = vtk.vtkCellArray()
        self.lines.InsertNextCell(line0)
        self.lines.InsertNextCell(line1)
        # Add the lines to the polydata container
        self.linesPolyData.SetLines(self.lines)

        for i in range(0, self.lines.GetNumberOfCells()):
            self.colors.InsertNextTuple(self.color)

        self.linesPolyData.GetCellData().SetScalars(self.colors)

        self.mapper = vtk.vtkPolyDataMapper()
        self.mapper.SetInputData(self.linesPolyData)

        self.actor = vtk.vtkActor()
        self.actor.SetMapper(self.mapper)
        self.actor.GetProperty().SetLineWidth(4)

        self.vtk_widget = QVTKRenderWindowInteractor()
        # vtk_widget.setMinimumSize(round(1024/3), round(1024/3))
        ren = vtk.vtkRenderer()
        self.vtk_widget.GetRenderWindow().AddRenderer(ren)

        ren.AddActor(self.actor)

        ren.ResetCamera()
        interactor = self.vtk_widget.GetRenderWindow().GetInteractor()
        interactor.Initialize()
        interactor.Start()

        self.layout.addWidget(self.vtk_widget)

    def select_color(self):
        color = QColorDialog.getColor(QtCore.Qt.green, self)
        if color.isValid():
            self.color = color.getRgb()
            for i in range(0, self.lines.GetNumberOfCells()):
                self.colors.InsertNextTuple(self.color)

            self.linesPolyData.GetCellData().SetScalars(self.colors)
            self.mapper.SetInputData(self.linesPolyData)
            self.linesPolyData.Modified()
            self.mapper.Modified()
            # self.actor.GetProperty().Modified()
            self.vtk_widget.Render()



if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())

--
Abhishek
zeroth.me
--
Abhishek Patil
zeroth.me

_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers



_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers
Reply | Threaded
Open this post in threaded view
|

Re: change ploydata color using vtk.vtkUnsignedCharArray dynamically not re-rendering

Abhishek-2
Hi David,
Thanks for pointing out,
Just to be clear InsertValue(id, val) trows error because val should be int,  I tried InsertTuple(id, tuple) it didn't throw an error but didn't work either.  End up re-initializing the vtk.vtkUnsignedCharArray  in def select_color(self) as
           self.colors = vtk.vtkUnsignedCharArray()
            self.colors.SetNumberOfComponents(4)

and it works smooth. :)

Thanks,


On Tue, Jun 5, 2018 at 11:41 PM David E DeMarle <[hidden email]> wrote:
Hi Ahishek,

It looks to me like you are appending to the end of the self.colors. Call self.colors.GetNumberOfTuples() to verify that.
If you fix (do replacement with Insertvalue(id, value) instead of InserNextValue(value)) I think it will do what you are trying to do.

hth




David E DeMarle
Kitware, Inc.
Principal Engineer
21 Corporate Drive
Clifton Park, NY 12065-8662
Phone: 518-881-4909

On Mon, Jun 4, 2018 at 8:35 PM, Abhishek <[hidden email]> wrote:
Greetings,
I am new to VTK so may be my question is very basic.

I have following code, where the aim is to be able to change the lines colors using dynamically based on data we get from QColorDialog. if I apply the color directly to vtkActor and re-render the window then it just works fine. But eventually I wan to be able to change individual line Points color and thats why I am setting self.linesPolyData.GetCellData().SetScalars(self.colors) when I change the color using QPushButton in function def select_color(self): it doesn't re-render the lines with new color. What am I doing wrong?

from Analysis.Channel import *
import vtk
import numpy as np
import itertools
import sys
import math

from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QScrollArea, QPushButton, QColorDialog
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor



# Qt Class which will render the objects
class MainWindow(QWidget):
    def __init__(self, parent = None):
        super().__init__()
        self.layout = QGridLayout(self)
        self.color_button = QPushButton('Select Color')

        self.layout.addWidget(self.color_button)
        self.color_button.clicked.connect(self.select_color)

        self.color = [255, 255, 0, 255]
        self.colors = vtk.vtkUnsignedCharArray()
        self.colors.SetNumberOfComponents(4)

        # Create the polydata where we will store all the geometric data
        self.linesPolyData = vtk.vtkPolyData()

        # Create three points
        origin = [0.0, 0.0, 0.0]
        p0 = [1.0, 0.0, 0.0]
        p1 = [0.0, 1.0, 0.0]

        # Create a vtkPoints container and store the points in it
        pts = vtk.vtkPoints()
        pts.InsertNextPoint(origin)
        pts.InsertNextPoint(p0)
        pts.InsertNextPoint(p1)

        self.linesPolyData.SetPoints(pts)

        # Create the first line (between Origin and P0)
        line0 = vtk.vtkLine()
        line0.GetPointIds().SetId(0, 0)  # the second 0 is the index of the Origin in linesPolyData's points
        line0.GetPointIds().SetId(1, 1)  # the second 1 is the index of P0 in linesPolyData's points

        # Create the second line (between Origin and P1)
        line1 = vtk.vtkLine()
        line1.GetPointIds().SetId(0, 0)  # the second 0 is the index of the Origin in linesPolyData's points
        line1.GetPointIds().SetId(1, 2)  # 2 is the index of P1 in linesPolyData's points

        self.lines = vtk.vtkCellArray()
        self.lines.InsertNextCell(line0)
        self.lines.InsertNextCell(line1)
        # Add the lines to the polydata container
        self.linesPolyData.SetLines(self.lines)

        for i in range(0, self.lines.GetNumberOfCells()):
            self.colors.InsertNextTuple(self.color)

        self.linesPolyData.GetCellData().SetScalars(self.colors)

        self.mapper = vtk.vtkPolyDataMapper()
        self.mapper.SetInputData(self.linesPolyData)

        self.actor = vtk.vtkActor()
        self.actor.SetMapper(self.mapper)
        self.actor.GetProperty().SetLineWidth(4)

        self.vtk_widget = QVTKRenderWindowInteractor()
        # vtk_widget.setMinimumSize(round(1024/3), round(1024/3))
        ren = vtk.vtkRenderer()
        self.vtk_widget.GetRenderWindow().AddRenderer(ren)

        ren.AddActor(self.actor)

        ren.ResetCamera()
        interactor = self.vtk_widget.GetRenderWindow().GetInteractor()
        interactor.Initialize()
        interactor.Start()

        self.layout.addWidget(self.vtk_widget)

    def select_color(self):
        color = QColorDialog.getColor(QtCore.Qt.green, self)
        if color.isValid():
            self.color = color.getRgb()
            for i in range(0, self.lines.GetNumberOfCells()):
                self.colors.InsertNextTuple(self.color)

            self.linesPolyData.GetCellData().SetScalars(self.colors)
            self.mapper.SetInputData(self.linesPolyData)
            self.linesPolyData.Modified()
            self.mapper.Modified()
            # self.actor.GetProperty().Modified()
            self.vtk_widget.Render()



if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())

--
Abhishek
zeroth.me
--
Abhishek Patil
zeroth.me

_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers


--
Abhishek Patil
zeroth.me

_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers