Skip to content
Snippets Groups Projects
Commit 64b1700f authored by Andrew Maclean's avatar Andrew Maclean
Browse files

Adding StructuredGrid examples

parent a1430b6e
No related branches found
No related tags found
1 merge request!327Add pythonic api examples 1
......@@ -170,6 +170,11 @@ This section includes examples of manipulating meshes.
#### ?vtkStructuredGrid?
| Example Name | Description | Image |
| -------------- | ------------- | ------- |
[BlankPoint](/PythonicAPI/StructuredGrid/BlankPoint) | Blank a point of a vtkStructuredGrid.
[SGrid](/PythonicAPI/StructuredGrid/SGrid) | Creating a structured grid dataset of a semi-cylinder. Vectors are created whose magnitude is proportional to radius and oriented in tangential direction.
#### ?vtkStructuredPoints?
#### ?vtkRectilinearGrid?
......
#!/usr/bin/env python3
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonCore import vtkPoints
from vtkmodules.vtkCommonDataModel import vtkStructuredGrid
from vtkmodules.vtkFiltersGeometry import vtkStructuredGridGeometryFilter
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkDataSetMapper,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer
)
def main():
colors = vtkNamedColors()
points = vtkPoints()
grid_size = 8
counter = 0
pt_idx = 0
# Create a 5x5 grid of points
for j in range(0, grid_size):
for i in range(0, grid_size):
if i == 3 and j == 3: # Make one point higher than the rest.
points.InsertNextPoint(i, j, 2)
print(f'The different point is number {counter}.')
pt_idx = counter
else:
# Make most of the points the same height.
points.InsertNextPoint(i, j, 0)
counter += 1
# Specify the dimensions of the grid.
structured_grid = vtkStructuredGrid(dimensions=(grid_size, grid_size, 1), points=points)
structured_grid.BlankPoint(pt_idx)
# Check.
def is_visible(pt_num):
if structured_grid.IsPointVisible(pt_num):
return f'Point {pt_num:2d} is visible.'
else:
return f'Point {pt_num:2d} is not visible.'
# Should not be visible.
print(f'{is_visible(pt_idx)}')
# Should be visible.
print(f'{is_visible(7)}')
# We need the geometry filter to ensure that the
# blanked point and surrounding faces is missing.
geometry_filter = vtkStructuredGridGeometryFilter()
# Create a mapper and actor.
grid_mapper = vtkDataSetMapper()
structured_grid >> geometry_filter >> grid_mapper
grid_actor = vtkActor(mapper=grid_mapper)
grid_actor.property.edge_visibility = True
grid_actor.property.edge_color = colors.GetColor3d('Blue')
# Visualize
renderer = vtkRenderer(background=colors.GetColor3d('ForestGreen'))
ren_win = vtkRenderWindow(window_name='BlankPoint')
ren_win.AddRenderer(renderer)
iren = vtkRenderWindowInteractor()
iren.render_window = ren_win
renderer.AddActor(grid_actor)
ren_win.Render()
iren.Start()
if __name__ == '__main__':
main()
### Description
Creating a structured grid dataset of a semicylinder. Vectors are created whose magnitude is proportional to radius and oriented in tangential direction.
!!! info
See [Figure 5-19](../../../VTKBook/05Chapter5/#Figure%205-19) in [Chapter 5](../../../VTKBook/05Chapter5) the [VTK Textbook](../../../VTKBook/01Chapter1).
#!/usr/bin/env python3
"""
This example shows how to manually create a structured grid.
The basic idea is to instantiate vtkStructuredGrid, set its dimensions,
and then assign points defining the grid coordinate. The number of
points must equal the number of points implicit in the dimensions
(i.e., dimX*dimY*dimZ). Also, data attributes (either point or cell)
can be added to the dataset.
"""
import math
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonCore import (
vtkDoubleArray,
vtkMath,
vtkPoints
)
from vtkmodules.vtkCommonDataModel import vtkStructuredGrid
from vtkmodules.vtkFiltersCore import vtkHedgeHog
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkPolyDataMapper,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer
)
def main():
colors = vtkNamedColors()
r_min = 0.5
r_max = 1.0
dims = (13, 11, 11)
# We also create the points and vectors. The points
# form a hemi-cylinder of data.
vectors = vtkDoubleArray()
vectors.SetNumberOfComponents(3)
vectors.SetNumberOfTuples(dims[0] * dims[1] * dims[2])
points = vtkPoints()
points.Allocate(dims[0] * dims[1] * dims[2])
delta_z = 2.0 / (dims[2] - 1)
delta_rad = (r_max - r_min) / (dims[1] - 1)
x = [0.0] * 3
v = [0.0] * 3
for k in range(0, dims[2]):
x[2] = -1.0 + k * delta_z
k_offset = k * dims[0] * dims[1]
for j in range(0, dims[1]):
radius = r_min + j * delta_rad
j_offset = j * dims[0]
for i in range(0, dims[0]):
theta = i * vtkMath.RadiansFromDegrees(15.0)
x[0] = radius * math.cos(theta)
x[1] = radius * math.sin(theta)
v[0] = -x[1]
v[1] = x[0]
offset = i + j_offset + k_offset
points.InsertPoint(offset, x)
vectors.InsertTuple(offset, v)
# Create the structured grid.
sgrid = vtkStructuredGrid(dimensions=dims, points=points)
sgrid.point_data.SetVectors(vectors)
# We create a simple pipeline to display the data.
hedgehog = vtkHedgeHog(scale_factor=0.1)
sgrid_mapper = vtkPolyDataMapper()
sgrid >> hedgehog >> sgrid_mapper
sgrid_actor = vtkActor(mapper=sgrid_mapper)
sgrid_actor.property.color = colors.GetColor3d('Gold')
# Create the usual rendering stuff.
renderer = vtkRenderer(background=colors.GetColor3d('MidnightBlue'))
ren_win = vtkRenderWindow(size=(640, 480), window_name='SGrid')
ren_win.AddRenderer(renderer)
iren = vtkRenderWindowInteractor()
iren.render_window = ren_win
renderer.AddActor(sgrid_actor)
renderer.ResetCamera()
renderer.active_camera.Elevation(60.0)
renderer.active_camera.Azimuth(30.0)
renderer.active_camera.Dolly(1.0)
# Interact with the data.
ren_win.Render()
iren.Start()
if __name__ == '__main__':
main()
src/Testing/Baseline/PythonicAPI/StructuredGrid/TestBlankPoint.png

129 B

src/Testing/Baseline/PythonicAPI/StructuredGrid/TestSGrid.png

130 B

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment