Mapping nodal scalar values to graded color

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

Mapping nodal scalar values to graded color

roobaru
Hello,
I am trying to display stress values on an FEM mesh as color grade using vtkColorTransferFunction. I am reading the nodal stress values from a file. I also added a scale widget on the right. The widget shows the colors correctly but the mesh doesn't display any graded color. I tried to achieve this using a single hexahedron mesh and it was displaying quite well. But the fileinput method somehow doesn't do anything. If anybody can see a glaring mistake and point it out, it will be a great help. I am attaching the code and the input file. Thanks for any help.


#!/usr/bin/env python
# This is a function, which takes a string parameter: file_path. This string is
# the path of the geometry txt file from which vtk will grab and display data.
import vtk
def displayMesh(file_path):
    import vtk
    from vtk import vtkHexahedron
    VTK_HEXAHEDRON = vtkHexahedron().GetCellType()
    with open(file_path, 'r') as f:
         aMeshGrid = vtk.vtkUnstructuredGrid()
         aMeshGrid.Allocate(1, 1)
# Get number of mesh points
         no_points = int(f.readline())
         print no_points
# Set number of points
         meshPoints = vtk.vtkPoints()
         meshPoints.SetNumberOfPoints(no_points)
# Iterate through point data
         for i in range(no_points):
             #print i
# Get coord info for each point
             point_info = f.readline().split() # I need to split, before I assign to point_coord
# else the whole thing is split into single numbers
             point_ID = (int(point_info[0])-1) # -1 because the IDs need to start with 0.
             point_x = float(point_info[1])
             point_y = float(point_info[2])
             point_z = float(point_info[3])
# Set coord info in mesh
             meshPoints.InsertPoint(point_ID, point_x, point_y, point_z)
# Get number of elements
         no_elements = int(f.readline())
# Set number of elements
         for i in range(no_elements):
             element_info = f.readline().split()
             element_ID = (int(element_info[0])-1)
             element_ID_list = vtk.vtkIdList()
             for j in range(8):
                 node_no = int(element_info[j+1])
                 element_ID_list.InsertNextId(node_no -1)
                 #print j, node_no
             cell_type = VTK_HEXAHEDRON
             aMeshGrid.InsertNextCell(cell_type, element_ID_list)
         stress = vtk.vtkFloatArray()
         for i in range(no_points):
             stress_value = f.readline()
             stress.InsertNextValue(float(stress_value))
         aMeshGrid.GetPointData().GetScalars()
         aMeshMapper = vtk.vtkDataSetMapper()

# # colorTransferFunction
         min=0.00001
         max=0.035
         avg=(min+max)/2.0
         aMeshMapper.SetInputData(aMeshGrid)
         colorTransferFunction = vtk.vtkColorTransferFunction()
         colorTransferFunction.AddRGBPoint(min, 0.0, 0.0, 1.0)
         colorTransferFunction.AddRGBPoint(avg, 1.0, 1.0, 0.0)
         colorTransferFunction.AddRGBPoint(max, 1.0, 0.0, 0.0)
         aMeshMapper.SetLookupTable(colorTransferFunction)

         scalar_bar = vtk.vtkScalarBarActor()
         scalar_bar.SetOrientationToHorizontal()
         scalar_bar.SetLookupTable(colorTransferFunction)
         scalar_bar.SetTitle("Stress")
         scalar_bar.GetTitleTextProperty().SetColor(1.0,1.0,1.0)

         aMeshGrid.SetPoints(meshPoints)
         aMeshActor = vtk.vtkActor()
         aMeshActor.AddPosition(0,0,0)
         aMeshActor.SetMapper(aMeshMapper)
         aMeshActor.GetProperty().SetDiffuseColor(1, 1, 0)
         aMeshActor.GetProperty().SetEdgeVisibility(1)
         aMeshActor.GetProperty().SetEdgeColor(1, 0, 0)

# Create the usual rendering stuff.
         ren = vtk.vtkRenderer()
         renWin = vtk.vtkRenderWindow()
         renWin.AddRenderer(ren)
         renWin.SetSize(1920, 1080)
         iren = vtk.vtkRenderWindowInteractor()
         iren.SetRenderWindow(renWin)

 # create the scalar_bar_widget
         scalar_bar_widget = vtk.vtkScalarBarWidget()
         scalar_bar_widget.SetInteractor(iren)
         scalar_bar_widget.SetScalarBarActor(scalar_bar)
         scalar_bar_widget.On()

         # CompassWidget
         compassRepresentation = vtk.vtkCompassRepresentation()
         compassWidget = vtk.vtkCompassWidget()
         compassWidget.SetInteractor(iren)
         compassWidget.SetRepresentation(compassRepresentation)

         ren.AddActor(aMeshActor)
         ren.SetBackground(0.323,0.341,0.431)
 # Add Axes
         transform = vtk.vtkTransform()
         transform.Translate(0.0, 0.0, 0.0)
         axes = vtk.vtkAxesActor()
#  The axes are positioned with a user transform
         axes.SetUserTransform(transform)
         ren.AddActor(axes)
         ren.GetActiveCamera().SetPosition(-0.6,-0.6,-0.5)
         ren.ResetCamera()
         renWin.Render()
         compassWidget.EnabledOn()
         style = vtk.vtkInteractorStyleTrackballCamera()
         iren.SetInteractorStyle(style)
## Render the scene and start interaction.
         iren.Initialize()
         iren.Start()
displayMesh('D:\VTK_Visualization\Beam_colorwidget.txt')

file: https://drive.google.com/file/d/0B1TsQHxIsXV8NWRNNnRBTE5BaWs/view?usp=sharing
Reply | Threaded
Open this post in threaded view
|

Re: Mapping nodal scalar values to graded color

Alex Malyushytskyy
I am not fluent with python,
but should not you replace
  aMeshGrid.GetPointData().GetScalars()
with something like
aMeshGrid.GetPointData().SetScalars(stress)
?


On Mon, Aug 3, 2015 at 2:15 PM, roobaru <[hidden email]> wrote:
Hello,
I am trying to display stress values on an FEM mesh as color grade using
vtkColorTransferFunction. I am reading the nodal stress values from a file.
I also added a scale widget on the right. The widget shows the colors
correctly but the mesh doesn't display any graded color. I tried to achieve
this using a single hexahedron mesh and it was displaying quite well. But
the fileinput method somehow doesn't do anything. If anybody can see a
glaring mistake and point it out, it will be a great help. I am attaching
the code and the input file. Thanks for any help.


#!/usr/bin/env python
# This is a function, which takes a string parameter: file_path. This string
is
# the path of the geometry txt file from which vtk will grab and display
data.
import vtk
def displayMesh(file_path):
    import vtk
    from vtk import vtkHexahedron
    VTK_HEXAHEDRON = vtkHexahedron().GetCellType()
    with open(file_path, 'r') as f:
         aMeshGrid = vtk.vtkUnstructuredGrid()
         aMeshGrid.Allocate(1, 1)
# Get number of mesh points
         no_points = int(f.readline())
         print no_points
# Set number of points
         meshPoints = vtk.vtkPoints()
         meshPoints.SetNumberOfPoints(no_points)
# Iterate through point data
         for i in range(no_points):
             #print i
# Get coord info for each point
             point_info = f.readline().split() # I need to split, before I
assign to point_coord
# else the whole thing is split into single numbers
             point_ID = (int(point_info[0])-1) # -1 because the IDs need to
start with 0.
             point_x = float(point_info[1])
             point_y = float(point_info[2])
             point_z = float(point_info[3])
# Set coord info in mesh
             meshPoints.InsertPoint(point_ID, point_x, point_y, point_z)
# Get number of elements
         no_elements = int(f.readline())
# Set number of elements
         for i in range(no_elements):
             element_info = f.readline().split()
             element_ID = (int(element_info[0])-1)
             element_ID_list = vtk.vtkIdList()
             for j in range(8):
                 node_no = int(element_info[j+1])
                 element_ID_list.InsertNextId(node_no -1)
                 #print j, node_no
             cell_type = VTK_HEXAHEDRON
             aMeshGrid.InsertNextCell(cell_type, element_ID_list)
         stress = vtk.vtkFloatArray()
         for i in range(no_points):
             stress_value = f.readline()
             stress.InsertNextValue(float(stress_value))
         aMeshGrid.GetPointData().GetScalars()
         aMeshMapper = vtk.vtkDataSetMapper()

# # colorTransferFunction
         min=0.00001
         max=0.035
         avg=(min+max)/2.0
         aMeshMapper.SetInputData(aMeshGrid)
         colorTransferFunction = vtk.vtkColorTransferFunction()
         colorTransferFunction.AddRGBPoint(min, 0.0, 0.0, 1.0)
         colorTransferFunction.AddRGBPoint(avg, 1.0, 1.0, 0.0)
         colorTransferFunction.AddRGBPoint(max, 1.0, 0.0, 0.0)
         aMeshMapper.SetLookupTable(colorTransferFunction)

         scalar_bar = vtk.vtkScalarBarActor()
         scalar_bar.SetOrientationToHorizontal()
         scalar_bar.SetLookupTable(colorTransferFunction)
         scalar_bar.SetTitle("Stress")
         scalar_bar.GetTitleTextProperty().SetColor(1.0,1.0,1.0)

         aMeshGrid.SetPoints(meshPoints)
         aMeshActor = vtk.vtkActor()
         aMeshActor.AddPosition(0,0,0)
         aMeshActor.SetMapper(aMeshMapper)
         aMeshActor.GetProperty().SetDiffuseColor(1, 1, 0)
         aMeshActor.GetProperty().SetEdgeVisibility(1)
         aMeshActor.GetProperty().SetEdgeColor(1, 0, 0)

# Create the usual rendering stuff.
         ren = vtk.vtkRenderer()
         renWin = vtk.vtkRenderWindow()
         renWin.AddRenderer(ren)
         renWin.SetSize(1920, 1080)
         iren = vtk.vtkRenderWindowInteractor()
         iren.SetRenderWindow(renWin)

 # create the scalar_bar_widget
         scalar_bar_widget = vtk.vtkScalarBarWidget()
         scalar_bar_widget.SetInteractor(iren)
         scalar_bar_widget.SetScalarBarActor(scalar_bar)
         scalar_bar_widget.On()

         # CompassWidget
         compassRepresentation = vtk.vtkCompassRepresentation()
         compassWidget = vtk.vtkCompassWidget()
         compassWidget.SetInteractor(iren)
         compassWidget.SetRepresentation(compassRepresentation)

         ren.AddActor(aMeshActor)
         ren.SetBackground(0.323,0.341,0.431)
 # Add Axes
         transform = vtk.vtkTransform()
         transform.Translate(0.0, 0.0, 0.0)
         axes = vtk.vtkAxesActor()
#  The axes are positioned with a user transform
         axes.SetUserTransform(transform)
         ren.AddActor(axes)
         ren.GetActiveCamera().SetPosition(-0.6,-0.6,-0.5)
         ren.ResetCamera()
         renWin.Render()
         compassWidget.EnabledOn()
         style = vtk.vtkInteractorStyleTrackballCamera()
         iren.SetInteractorStyle(style)
## Render the scene and start interaction.
         iren.Initialize()
         iren.Start()
displayMesh('D:\VTK_Visualization\Beam_colorwidget.txt')

file:
https://drive.google.com/file/d/0B1TsQHxIsXV8NWRNNnRBTE5BaWs/view?usp=sharing




--
View this message in context: http://vtk.1045678.n5.nabble.com/Mapping-nodal-scalar-values-to-graded-color-tp5733238.html
Sent from the VTK - Users mailing list archive at Nabble.com.
_______________________________________________
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:
http://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:
http://public.kitware.com/mailman/listinfo/vtkusers
Reply | Threaded
Open this post in threaded view
|

Re: Mapping nodal scalar values to graded color

roobaru
@Alex Thanks for the reply!
Doing "aMeshGrid.GetPointData().GetScalars(stress)" gives me an error
TypeError: GetScalars argument 1: string or None required
Reply | Threaded
Open this post in threaded view
|

Re: Mapping nodal scalar values to graded color

Bill Lorensen
Not
aMeshGrid.GetPointData().GetScalars(stress)
but
aMeshGrid.GetPointData().SetScalars(stress)

On Tue, Aug 4, 2015 at 12:08 PM, roobaru <[hidden email]> wrote:

> @Alex Thanks for the reply!
> Doing "aMeshGrid.GetPointData().GetScalars(stress)" gives me an error
> TypeError: GetScalars argument 1: string or None required
>
>
>
> --
> View this message in context: http://vtk.1045678.n5.nabble.com/Mapping-nodal-scalar-values-to-graded-color-tp5733238p5733254.html
> Sent from the VTK - Users mailing list archive at Nabble.com.
> _______________________________________________
> 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:
> http://public.kitware.com/mailman/listinfo/vtkusers



--
Unpaid intern in BillsBasement at noware dot com
_______________________________________________
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:
http://public.kitware.com/mailman/listinfo/vtkusers
Reply | Threaded
Open this post in threaded view
|

Re: Mapping nodal scalar values to graded color

roobaru
@Bill Thank you so much. That works. I really feel stupid for not noticing set->get.