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

Fixing cube confusion.

Former-commit-id: e19ea378
parent 8ceefc63
No related branches found
No related tags found
No related merge requests found
......@@ -156,6 +156,8 @@ These are fully independent, compilable examples. There is significant overlap i
| -------------- | ---------------------- | ------------- | ------- |
[Axes](/Cxx/GeometricObjects/Axes) | vtkAxesActor |
[ColoredLines](/Cxx/GeometricObjects/ColoredLines) | vtkCellData vtkLine |
[Cube](/Cxx/GeometricObjects/Cube) | vtkPolyData | Manually build a polygonal cube.
[Cube1](/Cxx/GeometricObjects/Cube1) | vtkCubeSource | A nice simple example that demonstrates the operation of the VTK pipeline.
[Dodecahedron](/Cxx/GeometricObjects/Dodecahedron) | vtkPolyhedron | Create a dodecahedron using vtkPolyhedron.
[EllipticalCylinder](/Cxx/GeometricObjects/EllipticalCylinder) | vtkLinearExtrusionFilter | Create an elliptical cylinder using extrusion.
[EllipticalCylinderDemo](/Cxx/GeometricObjects/EllipticalCylinderDemo) | vtkLinearExtrusionFilter | Show the base and extrusion vector.
......
/*=========================================================================
Program: Visualization Toolkit
Module: Cube.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// This example shows how to manually create vtkPolyData.
// For a python version, please see:
// [Cube](https://lorensen.github.io/VTKExamples/site/Python/GeometricObjects/Cube/)
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCellArray.h>
#include <vtkFloatArray.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <array>
int main(int, char *[])
{
vtkNew<vtkNamedColors> colors;
std::array<std::array<double, 3>, 8> pts = {{{{0, 0, 0}},
{{1, 0, 0}},
{{1, 1, 0}},
{{0, 1, 0}},
{{0, 0, 1}},
{{1, 0, 1}},
{{1, 1, 1}},
{{0, 1, 1}}}};
// The ordering of the corner points on each face.
std::array<std::array<vtkIdType, 4>, 6> ordering = {{{{0, 1, 2, 3}},
{{4, 5, 6, 7}},
{{0, 1, 5, 4}},
{{1, 2, 6, 5}},
{{2, 3, 7, 6}},
{{3, 0, 4, 7}}}};
// We'll create the building blocks of polydata including data attributes.
vtkNew<vtkPolyData> cube;
vtkNew<vtkPoints> points;
vtkNew<vtkCellArray> polys;
vtkNew<vtkFloatArray> scalars;
// Load the point, cell, and data attributes.
for (auto i = 0ul; i < pts.size(); ++i)
{
points->InsertPoint(i, pts[i].data());
scalars->InsertTuple1(i, i);
}
for (auto&& i : ordering)
{
polys->InsertNextCell(vtkIdType(i.size()), i.data());
}
// We now assign the pieces to the vtkPolyData.
cube->SetPoints(points);
cube->SetPolys(polys);
cube->GetPointData()->SetScalars(scalars);
// Now we'll look at it.
vtkNew<vtkPolyDataMapper> cubeMapper;
cubeMapper->SetInputData(cube);
cubeMapper->SetScalarRange(cube->GetScalarRange());
vtkNew<vtkActor> cubeActor;
cubeActor->SetMapper(cubeMapper);
// The usual rendering stuff.
vtkNew<vtkCamera> camera;
camera->SetPosition(1, 1, 1);
camera->SetFocalPoint(0, 0, 0);
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renWin;
renWin->AddRenderer(renderer);
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);
renderer->AddActor(cubeActor);
renderer->SetActiveCamera(camera);
renderer->ResetCamera();
renderer->SetBackground(colors->GetColor3d("Cornsilk").GetData());
renWin->SetSize(600, 600);
// interact with data
renWin->Render();
iren->Start();
return EXIT_SUCCESS;
}
### Description
This is based on the C++ example [Examples/DataManipulation/Cxx/Cube.cxx](http://vtk.org/gitweb?p=VTK.git;a=blob;f=Examples/DataManipulation/Cxx/Cube.cxx) in the VTK source distribution.
It illustrates the manual use of vtkPolyData to construct a cube and differs from the Wiki examples [Cube1](/Cxx/GeometricObjects/Cube1) and [Cube1](/Python/GeometricObjects/Cube1), which use vtkCube.
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCubeSource.h>
#include <vtkNamedColors.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkNew.h>
#include <vtkProperty.h>
int main(int, char *[])
{
vtkNew<vtkNamedColors> colors;
// Create a rendering window and renderer.
vtkNew<vtkRenderer> ren;
vtkNew<vtkRenderWindow> renWin;
renWin->SetWindowName("Cube");
renWin->AddRenderer(ren);
// Create a renderwindowinteractor
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);
// Create a cube.
vtkNew<vtkCubeSource> cube;
cube->Update();
// mapper
vtkNew<vtkPolyDataMapper> cubeMapper;
cubeMapper->SetInputData(cube->GetOutput());
// Actor.
vtkNew<vtkActor> cubeActor;
cubeActor->SetMapper(cubeMapper);
cubeActor->GetProperty()->SetColor(colors->GetColor3d("Banana").GetData());
// Assign actor to the renderer.
ren->AddActor(cubeActor);
ren->ResetCamera();
ren->GetActiveCamera()->Azimuth(30);
ren->GetActiveCamera()->Elevation(30);
ren->ResetCameraClippingRange();
ren->SetBackground(colors->GetColor3d("Silver").GetData());
renWin->SetSize(300, 300);
// Enable user interface interactor.
iren->Initialize();
renWin->Render();
iren->Start();
return EXIT_SUCCESS;
}
### Description
Display a cube.
A nice simple example that demonstrates the operation of the VTK pipeline.
......@@ -120,7 +120,8 @@ It would be appreciated if there are any Python VTK experts who could convert an
| -------------- | ---------------------- | ------------- | ------- |
[Arrow](/Python/GeometricObjects/Arrow) | vtkArrowSource |
[Cone](/Python/GeometricObjects/Cone) | vtkConeSource |
[Cube](/Python/GeometricObjects/Cube) | vtkCubeSource |
[Cube](/Python/GeometricObjects/Cube) | vtkPolyData | Manually build a polygonal cube.
[Cube1](/Python/GeometricObjects/Cube1) | vtkCubeSource | A nice simple example that demonstrates the operation of the VTK pipeline.
[Cylinder](/Python/GeometricObjects/Cylinder) | vtkCylinderSource |
[Disk](/Python/GeometricObjects/Disk) | vtkDiskSource | A circle with a hole in it.
[Frustum](/Python/GeometricObjects/Frustum) | vtkFrustumSource |
......
### Description
This is a transliteration of the C++ example [Examples/DataManipulation/Cxx/Cube.cxx](http://vtk.org/gitweb?p=VTK.git;a=blob;f=Examples/DataManipulation/Cxx/Cube.cxx) in the VTK source distribution.
It illustrates the manual use of vtkPolyData to construct a cube and differs from the Wiki examples [Cube](/Cxx/GeometricObjects/Cube) and [Cube](/Python/GeometricObjects/Cube), which use vtkCube.
#!/usr/bin/env python
"""
This is (almost) a direct C++ to Python transliteration of
<VTK-root>/Examples/DataManipulation/Cxx/Cube.cxx from the VTK
source distribution, which "shows how to manually create vtkPolyData"
A convenience function, mkVtkIdList(), has been added and one if/else
so the example also works in version 6 or later.
If your VTK version is 5.x then remove the line: colors = vtk.vtkNamedColors()
and replace the set background parameters with (1.0, 0.9688, 0.8594)
"""
import vtk
def mkVtkIdList(it):
"""
Makes a vtkIdList from a Python iterable. I'm kinda surprised that
this is necessary, since I assumed that this kind of thing would
have been built into the wrapper and happen transparently, but it
seems not.
:param it: A python iterable.
:return: A vtkIdList
"""
vil = vtk.vtkIdList()
for i in it:
vil.InsertNextId(int(i))
return vil
def main():
colors = vtk.vtkNamedColors()
# x = array of 8 3-tuples of float representing the vertices of a cube:
x = [(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (1.0, 1.0, 0.0), (0.0, 1.0, 0.0),
(0.0, 0.0, 1.0), (1.0, 0.0, 1.0), (1.0, 1.0, 1.0), (0.0, 1.0, 1.0)]
# pts = array of 6 4-tuples of vtkIdType (int) representing the faces
# of the cube in terms of the above vertices
pts = [(0, 1, 2, 3), (4, 5, 6, 7), (0, 1, 5, 4),
(1, 2, 6, 5), (2, 3, 7, 6), (3, 0, 4, 7)]
# We'll create the building blocks of polydata including data attributes.
cube = vtk.vtkPolyData()
points = vtk.vtkPoints()
polys = vtk.vtkCellArray()
scalars = vtk.vtkFloatArray()
# Load the point, cell, and data attributes.
for i, xi in enumerate(x):
points.InsertPoint(i, xi)
for pt in pts:
polys.InsertNextCell(mkVtkIdList(pt))
for i, _ in enumerate(x):
scalars.InsertTuple1(i, i)
# We now assign the pieces to the vtkPolyData.
cube.SetPoints(points)
cube.SetPolys(polys)
cube.GetPointData().SetScalars(scalars)
# Now we'll look at it.
cubeMapper = vtk.vtkPolyDataMapper()
cubeMapper.SetInputData(cube)
cubeMapper.SetScalarRange(cube.GetScalarRange())
cubeActor = vtk.vtkActor()
cubeActor.SetMapper(cubeMapper)
# The usual rendering stuff.
camera = vtk.vtkCamera()
camera.SetPosition(1, 1, 1)
camera.SetFocalPoint(0, 0, 0)
renderer = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(renderer)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
renderer.AddActor(cubeActor)
renderer.SetActiveCamera(camera)
renderer.ResetCamera()
renderer.SetBackground(colors.GetColor3d("Cornsilk"))
# renderer.SetBackground(1.0, 0.9688, 0.8594)
renWin.SetSize(600, 600)
# interact with data
renWin.Render()
iren.Start()
if __name__ == "__main__":
main()
### Description
Display a cube.
A nice simple example that demonstrates the operation of the VTK pipeline.
This is a transliteration of the C++ example [Examples/DataManipulation/Cxx/Cube.cxx](http://vtk.org/gitweb?p=VTK.git;a=blob;f=Examples/DataManipulation/Cxx/Cube.cxx) in the VTK source distribution.
It illustrates the manual use of vtkPolyData to construct a cube and differs from the Wiki examples [Cube1](/Cxx/GeometricObjects/Cube1) and [Cube1](/Python/GeometricObjects/Cube1), which use vtkCube.
#!/usr/bin/env python
"""
This is (almost) a direct C++ to Python transliteration of
<VTK-root>/Examples/DataManipulation/Cxx/Cube.cxx from the VTK
source distribution, which "shows how to manually create vtkPolyData"
A convenience function, mkVtkIdList(), has been added and one if/else
so the example also works in version 6 or later.
If your VTK version is 5.x then remove the line: colors = vtk.vtkNamedColors()
and replace the set background parameters with (1.0, 0.9688, 0.8594)
"""
import vtk
def main():
colors = vtk.vtkNamedColors()
def mkVtkIdList(it):
"""
Makes a vtkIdList from a Python iterable. I'm kinda surprised that
this is necessary, since I assumed that this kind of thing would
have been built into the wrapper and happen transparently, but it
seems not.
# Create a rendering window and renderer.
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.SetWindowName("Cube")
renWin.AddRenderer(ren)
:param it: A python iterable.
:return: A vtkIdList
"""
vil = vtk.vtkIdList()
for i in it:
vil.InsertNextId(int(i))
return vil
# Create a renderwindowinteractor.
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# Create cube.
cube = vtk.vtkCubeSource()
cube.Update()
def main():
colors = vtk.vtkNamedColors()
# mapper
# x = array of 8 3-tuples of float representing the vertices of a cube:
x = [(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (1.0, 1.0, 0.0), (0.0, 1.0, 0.0),
(0.0, 0.0, 1.0), (1.0, 0.0, 1.0), (1.0, 1.0, 1.0), (0.0, 1.0, 1.0)]
# pts = array of 6 4-tuples of vtkIdType (int) representing the faces
# of the cube in terms of the above vertices
pts = [(0, 1, 2, 3), (4, 5, 6, 7), (0, 1, 5, 4),
(1, 2, 6, 5), (2, 3, 7, 6), (3, 0, 4, 7)]
# We'll create the building blocks of polydata including data attributes.
cube = vtk.vtkPolyData()
points = vtk.vtkPoints()
polys = vtk.vtkCellArray()
scalars = vtk.vtkFloatArray()
# Load the point, cell, and data attributes.
for i, xi in enumerate(x):
points.InsertPoint(i, xi)
for pt in pts:
polys.InsertNextCell(mkVtkIdList(pt))
for i, _ in enumerate(x):
scalars.InsertTuple1(i, i)
# We now assign the pieces to the vtkPolyData.
cube.SetPoints(points)
cube.SetPolys(polys)
cube.GetPointData().SetScalars(scalars)
# Now we'll look at it.
cubeMapper = vtk.vtkPolyDataMapper()
cubeMapper.SetInputData(cube.GetOutput())
# Actor.
cubeMapper.SetInputData(cube)
cubeMapper.SetScalarRange(cube.GetScalarRange())
cubeActor = vtk.vtkActor()
cubeActor.SetMapper(cubeMapper)
cubeActor.GetProperty().SetColor(colors.GetColor3d("Banana"))
# Assign actor to the renderer.
ren.AddActor(cubeActor)
# The usual rendering stuff.
camera = vtk.vtkCamera()
camera.SetPosition(1, 1, 1)
camera.SetFocalPoint(0, 0, 0)
ren.ResetCamera()
ren.GetActiveCamera().Azimuth(30)
ren.GetActiveCamera().Elevation(30)
ren.ResetCameraClippingRange()
ren.SetBackground(colors.GetColor3d("Silver"))
renderer = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(renderer)
# Enable user interface interactor.
iren.Initialize()
renWin.Render()
WriteImage('TestCube', renWin, rgba=False)
iren.Start()
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
def WriteImage(fileName, renWin, rgba=True):
"""
Write the render window view to an image file.
renderer.AddActor(cubeActor)
renderer.SetActiveCamera(camera)
renderer.ResetCamera()
renderer.SetBackground(colors.GetColor3d("Cornsilk"))
# renderer.SetBackground(1.0, 0.9688, 0.8594)
Image types supported are:
BMP, JPEG, PNM, PNG, PostScript, TIFF.
The default parameters are used for all writers, change as needed.
renWin.SetSize(600, 600)
:param fileName: The file name, if no extension then PNG is assumed.
:param renWin: The render window.
:param rgba: Used to set the buffer type.
:return:
"""
# interact with data
renWin.Render()
iren.Start()
import os
if fileName:
# Select the writer to use.
path, ext = os.path.splitext(fileName)
ext = ext.lower()
if not ext:
ext = '.png'
fileName = fileName + ext
if ext == '.bmp':
writer = vtk.vtkBMPWriter()
elif ext == '.jpg':
writer = vtk.vtkJPEGWriter()
elif ext == '.pnm':
writer = vtk.vtkPNMWriter()
elif ext == '.ps':
if rgba:
rgba = False
writer = vtk.vtkPostScriptWriter()
elif ext == '.tiff':
writer = vtk.vtkTIFFWriter()
else:
writer = vtk.vtkPNGWriter()
windowto_image_filter = vtk.vtkWindowToImageFilter()
windowto_image_filter.SetInput(renWin)
windowto_image_filter.SetScale(1) # image quality
if rgba:
windowto_image_filter.SetInputBufferTypeToRGBA()
else:
windowto_image_filter.SetInputBufferTypeToRGB()
# Read from the front buffer.
windowto_image_filter.ReadFrontBufferOff()
windowto_image_filter.Update()
writer.SetFileName(fileName)
writer.SetInputConnection(windowto_image_filter.GetOutputPort())
writer.Write()
else:
raise RuntimeError('Need a filename.')
if __name__ == "__main__":
main()
### Description
Display a cube.
A nice simple example that demonstrates the operation of the VTK pipeline.
#!/usr/bin/env python
import vtk
def main():
colors = vtk.vtkNamedColors()
# Create a rendering window and renderer.
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.SetWindowName("Cube")
renWin.AddRenderer(ren)
# Create a renderwindowinteractor.
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# Create cube.
cube = vtk.vtkCubeSource()
cube.Update()
# mapper
cubeMapper = vtk.vtkPolyDataMapper()
cubeMapper.SetInputData(cube.GetOutput())
# Actor.
cubeActor = vtk.vtkActor()
cubeActor.SetMapper(cubeMapper)
cubeActor.GetProperty().SetColor(colors.GetColor3d("Banana"))
# Assign actor to the renderer.
ren.AddActor(cubeActor)
ren.ResetCamera()
ren.GetActiveCamera().Azimuth(30)
ren.GetActiveCamera().Elevation(30)
ren.ResetCameraClippingRange()
ren.SetBackground(colors.GetColor3d("Silver"))
# Enable user interface interactor.
iren.Initialize()
renWin.Render()
iren.Start()
if __name__ == "__main__":
main()
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