Skip to content
Snippets Groups Projects
Commit 7a7e14e7 authored by Mathieu Westphal (Kitware)'s avatar Mathieu Westphal (Kitware) :zap:
Browse files

Merge branch 'mixed_grid' into 'master'

PolyhedronAndHexahedron: Add a mixed grid example

See merge request !404
parents dd0ae37b a79926bd
Branches master
No related tags found
1 merge request!404PolyhedronAndHexahedron: Add a mixed grid example
Pipeline #437282 failed
......@@ -205,6 +205,7 @@ If you are new to VTK then these [tutorials](#tutorial) will help to get you sta
[PolyLine](/Cxx/GeometricObjects/PolyLine) |
[Polygon](/Cxx/GeometricObjects/Polygon) |
[Polyhedron](/Cxx/GeometricObjects/Polyhedron) | Create an unstructured grid representation of a polyhedron (cube) and write it out to a file.
[PolyhedronAndHexahedron](/Cxx/GeometricObjects/PolyhedronAndHexahedron) | Create an unstructured grid representation containing mixed cells, one polyhedron and one hexahedron.
[Pyramid](/Cxx/GeometricObjects/Pyramid) |
[Quad](/Cxx/GeometricObjects/Quad) |
[Tetrahedron](/Cxx/GeometricObjects/Tetrahedron) |
......
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkDataSetMapper.h>
#include <vtkIdList.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkUnstructuredGrid.h>
#include <vtkXMLUnstructuredGridWriter.h>
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
// create polyhedron (cube)
// Create points in an arbitrary order
vtkNew<vtkPoints> points;
points->InsertNextPoint(-5.0, -5.0, -10.0);
points->InsertNextPoint(-5.0, -5.0, 10.0);
points->InsertNextPoint(-5.0, 5.0, -10.0);
points->InsertNextPoint(-5.0, 5.0, 10.0);
points->InsertNextPoint(5.0, -5.0, -10.0);
points->InsertNextPoint(5.0, -5.0, 10.0);
points->InsertNextPoint(5.0, 5.0, -10.0);
points->InsertNextPoint(5.0, 5.0, 10.0);
// The ID of the points that compose the polyhedron cell
vtkIdType pointIds[8] = {0, 1, 2, 3, 4, 5, 6, 7};
// The ID of the points that compose each individual faces
vtkNew<vtkCellArray> faces;
vtkIdType face0[4] = { 0, 2, 6, 4 };
vtkIdType face1[4] = { 1, 3, 7, 5 };
vtkIdType face2[4] = { 0, 1, 3, 2 };
vtkIdType face3[4] = { 4, 5, 7, 6 };
vtkIdType face4[4] = { 0, 1, 5, 4 };
vtkIdType face5[4] = { 2, 3, 7, 6 };
// Insert each face
faces->InsertNextCell(4, face0);
faces->InsertNextCell(4, face1);
faces->InsertNextCell(4, face2);
faces->InsertNextCell(4, face3);
faces->InsertNextCell(4, face4);
faces->InsertNextCell(4, face5);
// Add the IDs of the faces for the polyhedron cell
vtkNew<vtkCellArray> faceLocations;
vtkIdType faceIds[6] = { 0, 1, 2, 3, 4, 5 };
faceLocations->InsertNextCell(6, faceIds);
// Insert the polyhedron cell
vtkNew<vtkCellArray> cells;
cells->InsertNextCell(8, pointIds);
// Insert the type of the cell
vtkNew<vtkUnsignedCharArray> cellTypes;
cellTypes->InsertNextValue(VTK_POLYHEDRON);
// Create hexahedron
// Create points in hexahedron order
points->InsertNextPoint(-5.0, -5.0, 15.0);
points->InsertNextPoint(5.0, -5.0, 15.0);
points->InsertNextPoint(5.0, 5.0, 15.0);
points->InsertNextPoint(-5.0, 5.0, 15.0);
points->InsertNextPoint(-5.0, -5.0, 35.0);
points->InsertNextPoint(5.0, -5.0, 35.0);
points->InsertNextPoint(5.0, 5.0, 35.0);
points->InsertNextPoint(-5.0, 5.0, 35.0);
// The ID of the points of the hexahedron
vtkIdType pointIdsh[8] = { 8, 9, 10, 11, 12, 13, 14, 15 };
// Add the ID of the "faces" for the hexahedon, its empty because hexahedron does not need faces
// But we still need faceLocations to have the same size as the number of cells
faceLocations->InsertNextCell(0);
// Insert the hexahedron
cells->InsertNextCell(8, pointIdsh);
// Insert the cell type
cellTypes->InsertNextValue(VTK_HEXAHEDRON);
// Set points and polyhedral cells
vtkNew<vtkUnstructuredGrid> ugrid0;
ugrid0->SetPoints(points);
ugrid0->SetPolyhedralCells(cellTypes, cells, faceLocations, faces);
// Create a mapper and actor
vtkNew<vtkDataSetMapper> mapper;
mapper->SetInputData(ugrid0);
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(colors->GetColor3d("Silver").GetData());
// Visualize.
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetWindowName("Polyhedron");
renderWindow->AddRenderer(renderer);
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderer->SetBackground(colors->GetColor3d("Salmon").GetData());
renderer->ResetCamera();
renderer->GetActiveCamera()->Azimuth(30);
renderer->GetActiveCamera()->Elevation(30);
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
### Description
This is a demonstration of how to construct an unstructured grid containing mixed cells types, polyhedron and hexahedron.
It rely on the `SetPolyhedralCells` API. The resulting object is then displayed.
......@@ -129,6 +129,7 @@ If you are new to VTK then these [tutorials](#tutorial) will help to get you sta
[PolyLine](/Python/GeometricObjects/PolyLine) |
[Polygon](/Python/GeometricObjects/Polygon) |
[Polyhedron](/Python/GeometricObjects/Polyhedron) | Create an unstructured grid representation of a polyhedron (cube) and write it out to a file.
[PolyhedronAndHexahedron](/Python/GeometricObjects/PolyhedronAndHexahedron) | Create an unstructured grid representation of mixed cells, one polyhedron and one hexahedron.
[Pyramid](/Python/GeometricObjects/Pyramid) |
[Quad](/Python/GeometricObjects/Quad) |
[Tetrahedron](/Python/GeometricObjects/Tetrahedron) |
......
### Description
This is a demonstration of how to construct an unstructured grid containing mixed cells types, polyhedron and hexahedron.
It rely on the `SetPolyhedralCells` API. The resulting object is then displayed.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonCore import (
vtkPoints,
vtkUnsignedCharArray
)
from vtkmodules.vtkCommonDataModel import (
VTK_HEXAHEDRON,
VTK_POLYHEDRON,
vtkCellArray,
vtkUnstructuredGrid
)
from vtkmodules.vtkIOXML import vtkXMLUnstructuredGridWriter
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkDataSetMapper,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer
)
def main():
colors = vtkNamedColors()
# create polyhedron (cube)
## Create points in an arbitrary order
points = vtkPoints()
points.InsertNextPoint(-5.0, -5.0, -10.0)
points.InsertNextPoint(-5.0, -5.0, 10.0)
points.InsertNextPoint(-5.0, 5.0, -10.0)
points.InsertNextPoint(-5.0, 5.0, 10.0)
points.InsertNextPoint(5.0, -5.0, -10.0)
points.InsertNextPoint(5.0, -5.0, 10.0)
points.InsertNextPoint(5.0, 5.0, -10.0)
points.InsertNextPoint(5.0, 5.0, 10.0)
## The ID of the points that compose the polyhedron cell
pointIds = [ 0, 1, 2, 3, 4, 5, 6, 7 ]
## The ID of the points that compose each individual faces
faces = vtkCellArray()
face0 = [ 0, 2, 6, 4 ]
face1 = [ 1, 3, 7, 5 ]
face2 = [ 0, 1, 3, 2 ]
face3 = [ 4, 5, 7, 6 ]
face4 = [ 0, 1, 5, 4 ]
face5 = [ 2, 3, 7, 6 ]
## Insert each face
faces.InsertNextCell(4, face0)
faces.InsertNextCell(4, face1)
faces.InsertNextCell(4, face2)
faces.InsertNextCell(4, face3)
faces.InsertNextCell(4, face4)
faces.InsertNextCell(4, face5)
## Add the IDs of the faces for the polyhedron cell
faceLocations = vtkCellArray()
faceIds = [ 0, 1, 2, 3, 4, 5 ]
faceLocations.InsertNextCell(6, faceIds)
## Insert the polyhedron cell
cells = vtkCellArray()
cells.InsertNextCell(8, pointIds)
## Insert the type of the cell
cellTypes = vtkUnsignedCharArray()
cellTypes.InsertNextValue(VTK_POLYHEDRON);
# Create hexahedron
## Create points in hexahedron order
points.InsertNextPoint(-5.0, -5.0, 15.0)
points.InsertNextPoint(5.0, -5.0, 15.0)
points.InsertNextPoint(5.0, 5.0, 15.0)
points.InsertNextPoint(-5.0, 5.0, 15.0)
points.InsertNextPoint(-5.0, -5.0, 35.0)
points.InsertNextPoint(5.0, -5.0, 35.0)
points.InsertNextPoint(5.0, 5.0, 35.0)
points.InsertNextPoint(-5.0, 5.0, 35.0)
## The ID of the points of the hexahedron
pointIds = [ 8, 9, 10, 11, 12, 13, 14, 15 ]
## Add the ID of the "faces" for the hexahedon, its empty because hexahedron does not need faces
## But we still need faceLocations to have the same size as the number of cells
faceLocations.InsertNextCell(0)
## Insert the hexahedron
cells.InsertNextCell(8, pointIds)
## Insert the cell type
cellTypes.InsertNextValue(VTK_HEXAHEDRON);
## Set points and polyhedral cells
ugrid0 = vtkUnstructuredGrid()
ugrid0.SetPoints(points);
ugrid0.SetPolyhedralCells(cellTypes, cells, faceLocations, faces);
# Create a mapper and actor
mapper = vtkDataSetMapper()
mapper.SetInputData(ugrid0)
actor = vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetColor(
colors.GetColor3d('Silver'))
# Visualize
renderer = vtkRenderer()
renderWindow = vtkRenderWindow()
renderWindow.SetWindowName('Polyhedron')
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
renderer.AddActor(actor)
renderer.SetBackground(colors.GetColor3d('Salmon'))
renderer.ResetCamera()
renderer.GetActiveCamera().Azimuth(30)
renderer.GetActiveCamera().Elevation(30)
renderWindow.Render()
renderWindowInteractor.Start()
if __name__ == '__main__':
main()
src/Testing/Baseline/Cxx/GeometricObjects/TestPolyhedronAndHexahedron.png

129 B

src/Testing/Baseline/Python/GeometricObjects/TestPolyhedronAndHexahedron.png

129 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