[vtkusers] Animation performance with cell color

Flagada flagada15 at gmail.com
Sat Nov 17 14:20:32 EST 2018


Hi all !
I am trying to animate a mesh deformation with quite a large number of
nodes (about 300000).
For now the best solution I have found is to make a list containing the
vtkpoints for each animation frame and loop by changing the vtkpoints of
the displayed polydata (or unstructuredgrid).
On a simple model without applying any color to the cells I can obtain an
animation rate of about 50fps wich is a good frame rate for me.
But if I apply colors with a PolyData.GetCellData().SetScalars() the
framerate goes down to 13fps.
Does anybody knows if it could be a better method to display mesh animation
or to improve the display rate of a mesh with colors ?

Here below is the python code I use.
It draws a disk of 800x400 nodes and if you press on F1 it animates a kind
of sinus wave.
First it calculate each frame, then it display the animation without color
and finally it display the animation with random color on cells.

Thanks !

############################

import vtk
import math
from time import time
import random

############################
# Creating disk mesh
disk = vtk.vtkDiskSource()
disk.SetInnerRadius(0.1)
disk.SetOuterRadius(2.0)
disk.SetRadialResolution(400)
disk.SetCircumferentialResolution(800)
disk.Update()
PolyData = disk.GetOutput()

print("%d nodes" % PolyData.GetNumberOfPoints())
print("%d elms" % PolyData.GetNumberOfCells())

# Setup actor and mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(PolyData)
actor = vtk.vtkActor()
actor.SetMapper(mapper)

# Setup render window, renderer, interactor and camera
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
style = vtk.vtkInteractorStyleTrackballCamera()
renderWindowInteractor.SetInteractorStyle(style)
camera = vtk.vtkCamera()
camera.Elevation(-45)
renderer.SetActiveCamera(camera)

#################################

def KeyPress(object, event):
   if object.GetInteractor().GetKeySym() == "F1":
       # Calculate each frame and store the vtkPoints into vtkPointsList
       PolyData = disk.GetOutput()
       vtkPointsList = {}

       print("Calculate...")

       for ph in range(0, 360, 20):
         start_time = time()
         vtkPointsList[ph] = vtk.vtkPoints()
         vtkPointsList[ph].DeepCopy(PolyData.GetPoints())
         for i in range(0, PolyData.GetNumberOfPoints()):
           NodePos = vtkPointsList[ph].GetPoint(i)
           vtkPointsList[ph].SetPoint(i, NodePos[0], NodePos[1], 0.3 *
math.sin(NodePos[0] * 2 + ph * math.pi / 180))
         PolyData.SetPoints(vtkPointsList[ph])
         renderWindow.Render()

       print("Done. Animate.")

       # First animation without color
       start_time = time()
       for ph in range(0, 360, 20):
         PolyData.SetPoints(vtkPointsList[ph])
         renderWindow.Render()
       print("%.2f FPS" % (18 / (time() - start_time)))

       # Activate cells colors
       ELMcolors = vtk.vtkUnsignedCharArray()
       ELMcolors.SetNumberOfComponents(3)
       ELMcolors.SetName("Colors")
       for i in range(0, PolyData.GetNumberOfCells()):
         ELMcolors.InsertNextTuple([random.random() * 255, random.random()
* 255, random.random() * 255])
       PolyData.GetCellData().SetScalars(ELMcolors)

       # Second animation with color
       start_time = time()
       for ph in range(0, 360, 20):
         PolyData.SetPoints(vtkPointsList[ph])
         renderWindow.Render()
       print("%.2f FPS" % (18 / (time() - start_time)))

####################################

style.AddObserver("KeyPressEvent", KeyPress)

renderer.AddActor(actor)
renderWindow.Render()
renderer.ResetCamera()
renderWindowInteractor.Start()

############################
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://public.kitware.com/pipermail/vtkusers/attachments/20181117/1c1b47c9/attachment.html>


More information about the vtkusers mailing list