From cc84d372331eef732763b7c0e442314e9b461699 Mon Sep 17 00:00:00 2001 From: Mathieu Westphal <mathieu.westphal@kitware.com> Date: Mon, 17 Feb 2025 14:49:13 +0100 Subject: [PATCH] PolyhedronAndHexahedron: Add a mixed grid example --- .../PolyhedronAndHexahedron.cxx | 127 ++++++++++++++++ .../PolyhedronAndHexahedron.py | 142 ++++++++++++++++++ 2 files changed, 269 insertions(+) create mode 100644 src/Cxx/GeometricObjects/PolyhedronAndHexahedron.cxx create mode 100644 src/Python/GeometricObjects/PolyhedronAndHexahedron.py diff --git a/src/Cxx/GeometricObjects/PolyhedronAndHexahedron.cxx b/src/Cxx/GeometricObjects/PolyhedronAndHexahedron.cxx new file mode 100644 index 00000000000..1abc040618b --- /dev/null +++ b/src/Cxx/GeometricObjects/PolyhedronAndHexahedron.cxx @@ -0,0 +1,127 @@ +#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); + + // Here we write out the cube. + vtkNew<vtkXMLUnstructuredGridWriter> writer; + writer->SetInputData(ugrid0); + writer->SetFileName("polyhedron.vtu"); + writer->SetDataModeToAscii(); + writer->Update(); + + // 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; +} diff --git a/src/Python/GeometricObjects/PolyhedronAndHexahedron.py b/src/Python/GeometricObjects/PolyhedronAndHexahedron.py new file mode 100644 index 00000000000..87ef2000791 --- /dev/null +++ b/src/Python/GeometricObjects/PolyhedronAndHexahedron.py @@ -0,0 +1,142 @@ +#!/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); + + # Here we write out the cube. + writer = vtkXMLUnstructuredGridWriter() + writer.SetInputData(ugrid0) + writer.SetFileName('polyhedron.vtu') + writer.SetDataModeToAscii() + writer.Update() + + # 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() -- GitLab