Commit 95364e36 authored by Andrew J. P. Maclean's avatar Andrew J. P. Maclean
Browse files

Upgrading these examples to C++11.

parent e02313e7
......@@ -16,29 +16,33 @@
// data as well as field data. It creates geometry (vtkPolyData) as
// well as attribute data explicitly.
// first include the required header files for the vtk classes we are using
#include "vtkActor.h"
#include "vtkCellArray.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkIntArray.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
// First include the required header files for the vtk classes we are using.
#include <vtkActor.h>
#include <vtkCellArray.h>
#include <vtkDoubleArray.h>
#include <vtkFloatArray.h>
#include <vtkIntArray.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 i;
vtkNew<vtkNamedColors> colors;
// Create a float array which represents the points.
vtkFloatArray* pcoords = vtkFloatArray::New();
vtkNew<vtkDoubleArray> pcoords;
// Note that by default, an array has 1 component.
// We have to change it to 3 for points
// We have to change it to 3 for points.
pcoords->SetNumberOfComponents(3);
// We ask pcoords to allocate room for at least 4 tuples
// and set the number of tuples to 4.
......@@ -46,20 +50,22 @@ int main()
// Assign each tuple. There are 5 specialized versions of SetTuple:
// SetTuple1 SetTuple2 SetTuple3 SetTuple4 SetTuple9
// These take 1, 2, 3, 4 and 9 components respectively.
float pts[4][3] = { {0.0, 0.0, 0.0}, {0.0, 1.0, 0.0},
{1.0, 0.0, 0.0}, {1.0, 1.0, 0.0} };
for (i=0; i<4; i++)
std::array<std::array<double, 3>, 4> pts = {{{{0.0, 0.0, 0.0}},
{{0.0, 1.0, 0.0}},
{{1.0, 0.0, 0.0}},
{{1.0, 1.0, 0.0}}}};
for (auto i = 0ul; i < pts.size(); ++i)
{
pcoords->SetTuple(i, pts[i]);
pcoords->SetTuple(i, pts[i].data());
}
// Create vtkPoints and assign pcoords as the internal data array.
vtkPoints* points = vtkPoints::New();
vtkNew<vtkPoints> points;
points->SetData(pcoords);
// Create the cells. In this case, a triangle strip with 2 triangles
// (which can be represented by 4 points)
vtkCellArray* strips = vtkCellArray::New();
// (which can be represented by 4 points).
vtkNew<vtkCellArray> strips;
strips->InsertNextCell(4);
strips->InsertCellPoint(0);
strips->InsertCellPoint(1);
......@@ -68,8 +74,8 @@ int main()
// Create an integer array with 4 tuples. Note that when using
// InsertNextValue (or InsertNextTuple1 which is equivalent in
// this situation), the array will expand automatically
vtkIntArray* temperature = vtkIntArray::New();
// this situation), the array will expand automatically.
vtkNew<vtkIntArray> temperature;
temperature->SetName("Temperature");
temperature->InsertNextValue(10);
temperature->InsertNextValue(20);
......@@ -77,7 +83,7 @@ int main()
temperature->InsertNextValue(40);
// Create a double array.
vtkDoubleArray* vorticity = vtkDoubleArray::New();
vtkNew<vtkDoubleArray> vorticity;
vorticity->SetName("Vorticity");
vorticity->InsertNextValue(2.7);
vorticity->InsertNextValue(4.1);
......@@ -85,7 +91,7 @@ int main()
vorticity->InsertNextValue(3.4);
// Create the dataset. In this case, we create a vtkPolyData
vtkPolyData* polydata = vtkPolyData::New();
vtkNew<vtkPolyData> polydata;
// Assign points and cells
polydata->SetPoints(points);
polydata->SetStrips(strips);
......@@ -97,38 +103,28 @@ int main()
// Create the mapper and set the appropriate scalar range
// (default is (0,1)
vtkPolyDataMapper* mapper = vtkPolyDataMapper::New();
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputData(polydata);
mapper->SetScalarRange(0, 40);
// Create an actor.
vtkActor* actor = vtkActor::New();
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
// Create the rendering objects.
vtkRenderer* ren = vtkRenderer::New();
vtkNew<vtkRenderer> ren;
ren->AddActor(actor);
ren->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());
vtkRenderWindow* renWin = vtkRenderWindow::New();
vtkNew<vtkRenderWindow> renWin;
renWin->AddRenderer(ren);
renWin->SetSize(600, 600);
vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);
iren->Initialize();
iren->Start();
pcoords->Delete();
points->Delete();
strips->Delete();
temperature->Delete();
vorticity->Delete();
polydata->Delete();
mapper->Delete();
actor->Delete();
ren->Delete();
renWin->Delete();
iren->Delete();
return 0;
return EXIT_SUCCESS;
}
......@@ -14,84 +14,96 @@
=========================================================================*/
// This example shows how to manually create vtkPolyData.
#include "vtkActor.h"
#include "vtkCamera.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
// For a python version, please see:
// [Cube](https://lorensen.github.io/VTKExamples/site/Python/DataManipulation/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 i;
static float x[8][3]={{0,0,0}, {1,0,0}, {1,1,0}, {0,1,0},
{0,0,1}, {1,0,1}, {1,1,1}, {0,1,1}};
static vtkIdType pts[6][4]={{0,1,2,3}, {4,5,6,7}, {0,1,5,4},
{1,2,6,5}, {2,3,7,6}, {3,0,4,7}};
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.
vtkPolyData *cube = vtkPolyData::New();
vtkPoints *points = vtkPoints::New();
vtkCellArray *polys = vtkCellArray::New();
vtkFloatArray *scalars = vtkFloatArray::New();
vtkNew<vtkPolyData> cube;
vtkNew<vtkPoints> points;
vtkNew<vtkCellArray> polys;
vtkNew<vtkFloatArray> scalars;
// Load the point, cell, and data attributes.
for (i=0; i<8; i++) points->InsertPoint(i,x[i]);
for (i=0; i<6; i++) polys->InsertNextCell(4,pts[i]);
for (i=0; i<8; i++) scalars->InsertTuple1(i,i);
for (auto i = 0ul; i < pts.size(); ++i)
{
points->InsertPoint(i, pts[i].data());
scalars->InsertTuple1(i, i);
}
for (auto&& i : ordering)
{
polys->InsertNextCell(i.size(), i.data());
}
// We now assign the pieces to the vtkPolyData.
cube->SetPoints(points);
points->Delete();
cube->SetPolys(polys);
polys->Delete();
cube->GetPointData()->SetScalars(scalars);
scalars->Delete();
// Now we'll look at it.
vtkPolyDataMapper *cubeMapper = vtkPolyDataMapper::New();
cubeMapper->SetInputData(cube);
cubeMapper->SetScalarRange(0,7);
vtkActor *cubeActor = vtkActor::New();
cubeActor->SetMapper(cubeMapper);
vtkNew<vtkPolyDataMapper> cubeMapper;
cubeMapper->SetInputData(cube);
cubeMapper->SetScalarRange(cube->GetScalarRange());
vtkNew<vtkActor> cubeActor;
cubeActor->SetMapper(cubeMapper);
// The usual rendering stuff.
vtkCamera *camera = vtkCamera::New();
camera->SetPosition(1,1,1);
camera->SetFocalPoint(0,0,0);
vtkNew<vtkCamera> camera;
camera->SetPosition(1, 1, 1);
camera->SetFocalPoint(0, 0, 0);
vtkRenderer *renderer = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(renderer);
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renWin;
renWin->AddRenderer(renderer);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);
renderer->AddActor(cubeActor);
renderer->SetActiveCamera(camera);
renderer->ResetCamera();
renderer->SetBackground(1,1,1);
renderer->SetActiveCamera(camera);
renderer->ResetCamera();
renderer->SetBackground(colors->GetColor3d("Cornsilk").GetData());
renWin->SetSize(300,300);
renWin->SetSize(600, 600);
// interact with data
renWin->Render();
iren->Start();
// Clean up
cube->Delete();
cubeMapper->Delete();
cubeActor->Delete();
camera->Delete();
renderer->Delete();
renWin->Delete();
iren->Delete();
return 0;
return EXIT_SUCCESS;
}
......@@ -15,117 +15,108 @@
// This example shows how to create a rectilinear grid.
//
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkFloatArray.h"
#include "vtkRectilinearGrid.h"
#include "vtkRectilinearGridGeometryFilter.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkProperty.h"
#include "vtkCamera.h"
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkFloatArray.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRectilinearGrid.h>
#include <vtkRectilinearGridGeometryFilter.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <array>
int main()
{
int i;
static double x[47]={
-1.22396, -1.17188, -1.11979, -1.06771, -1.01562, -0.963542,
-0.911458, -0.859375, -0.807292, -0.755208, -0.703125, -0.651042,
-0.598958, -0.546875, -0.494792, -0.442708, -0.390625, -0.338542,
-0.286458, -0.234375, -0.182292, -0.130209, -0.078125, -0.026042,
0.0260415, 0.078125, 0.130208, 0.182291, 0.234375, 0.286458,
0.338542, 0.390625, 0.442708, 0.494792, 0.546875, 0.598958,
0.651042, 0.703125, 0.755208, 0.807292, 0.859375, 0.911458,
0.963542, 1.01562, 1.06771, 1.11979, 1.17188};
static double y[33]={-1.25, -1.17188, -1.09375, -1.01562, -0.9375, -0.859375,
-0.78125, -0.703125, -0.625, -0.546875, -0.46875, -0.390625,
-0.3125, -0.234375, -0.15625, -0.078125, 0, 0.078125,
0.15625, 0.234375, 0.3125, 0.390625, 0.46875, 0.546875,
0.625, 0.703125, 0.78125, 0.859375, 0.9375, 1.01562,
1.09375, 1.17188, 1.25};
static double z[44]={0, 0.1, 0.2, 0.3, 0.4, 0.5,
0.6, 0.7, 0.75, 0.8, 0.9, 1,
1.1, 1.2, 1.3, 1.4, 1.5, 1.6,
1.7, 1.75, 1.8, 1.9, 2, 2.1,
2.2, 2.3, 2.4, 2.5, 2.6, 2.7,
2.75, 2.8, 2.9, 3, 3.1, 3.2,
3.3, 3.4, 3.5, 3.6, 3.7, 3.75,
3.8, 3.9};
vtkNew<vtkNamedColors> colors;
std::array<double, 47> x = {
{-1.22396, -1.17188, -1.11979, -1.06771, -1.01562, -0.963542,
-0.911458, -0.859375, -0.807292, -0.755208, -0.703125, -0.651042,
-0.598958, -0.546875, -0.494792, -0.442708, -0.390625, -0.338542,
-0.286458, -0.234375, -0.182292, -0.130209, -0.078125, -0.026042,
0.0260415, 0.078125, 0.130208, 0.182291, 0.234375, 0.286458,
0.338542, 0.390625, 0.442708, 0.494792, 0.546875, 0.598958,
0.651042, 0.703125, 0.755208, 0.807292, 0.859375, 0.911458,
0.963542, 1.01562, 1.06771, 1.11979, 1.17188}};
std::array<double, 33> y = {
{-1.25, -1.17188, -1.09375, -1.01562, -0.9375, -0.859375,
-0.78125, -0.703125, -0.625, -0.546875, -0.46875, -0.390625,
-0.3125, -0.234375, -0.15625, -0.078125, 0, 0.078125,
0.15625, 0.234375, 0.3125, 0.390625, 0.46875, 0.546875,
0.625, 0.703125, 0.78125, 0.859375, 0.9375, 1.01562,
1.09375, 1.17188, 1.25}};
std::array<double, 44> z = {{0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.75,
0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6,
1.7, 1.75, 1.8, 1.9, 2, 2.1, 2.2, 2.3, 2.4,
2.5, 2.6, 2.7, 2.75, 2.8, 2.9, 3, 3.1, 3.2,
3.3, 3.4, 3.5, 3.6, 3.7, 3.75, 3.8, 3.9}};
// Create a rectilinear grid by defining three arrays specifying the
// coordinates in the x-y-z directions.
vtkFloatArray *xCoords = vtkFloatArray::New();
for (i=0; i<47; i++)
vtkNew<vtkFloatArray> xCoords;
for (auto&& i : x)
{
xCoords->InsertNextValue(x[i]);
xCoords->InsertNextValue(i);
}
vtkFloatArray *yCoords = vtkFloatArray::New();
for (i=0; i<33; i++)
vtkNew<vtkFloatArray> yCoords;
for (auto&& i : y)
{
yCoords->InsertNextValue(y[i]);
yCoords->InsertNextValue(i);
}
vtkFloatArray *zCoords = vtkFloatArray::New();
for (i=0; i<44; i++)
vtkNew<vtkFloatArray> zCoords;
for (auto&& i : z)
{
zCoords->InsertNextValue(z[i]);
zCoords->InsertNextValue(i);
}
// The coordinates are assigned to the rectilinear grid. Make sure that
// the number of values in each of the XCoordinates, YCoordinates,
// and ZCoordinates is equal to what is defined in SetDimensions().
//
vtkRectilinearGrid *rgrid = vtkRectilinearGrid::New();
rgrid->SetDimensions(47,33,44);
rgrid->SetXCoordinates(xCoords);
rgrid->SetYCoordinates(yCoords);
rgrid->SetZCoordinates(zCoords);
vtkNew<vtkRectilinearGrid> rgrid;
rgrid->SetDimensions(int(x.size()), int(y.size()), int(z.size()));
rgrid->SetXCoordinates(xCoords);
rgrid->SetYCoordinates(yCoords);
rgrid->SetZCoordinates(zCoords);
// Extract a plane from the grid to see what we've got.
vtkRectilinearGridGeometryFilter *plane = vtkRectilinearGridGeometryFilter::New();
plane->SetInputData(rgrid);
plane->SetExtent(0,46, 16,16, 0,43);
// Extract a plane from the grid to see what we've got.
vtkNew<vtkRectilinearGridGeometryFilter> plane;
plane->SetInputData(rgrid);
plane->SetExtent(0, 46, 16, 16, 0, 43);
vtkPolyDataMapper *rgridMapper = vtkPolyDataMapper::New();
rgridMapper->SetInputConnection(plane->GetOutputPort());
vtkNew<vtkPolyDataMapper> rgridMapper;
rgridMapper->SetInputConnection(plane->GetOutputPort());
vtkActor *wireActor = vtkActor::New();
wireActor->SetMapper(rgridMapper);
wireActor->GetProperty()->SetRepresentationToWireframe();
wireActor->GetProperty()->SetColor(0,0,0);
vtkNew<vtkActor> wireActor;
wireActor->SetMapper(rgridMapper);
wireActor->GetProperty()->SetRepresentationToWireframe();
wireActor->GetProperty()->SetColor(colors->GetColor3d("Indigo").GetData());
// Create the usual rendering stuff.
vtkRenderer *renderer = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(renderer);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renWin;
renWin->AddRenderer(renderer);
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);
renderer->AddActor(wireActor);
renderer->SetBackground(1,1,1);
renderer->SetBackground(1, 1, 1);
renderer->ResetCamera();
renderer->GetActiveCamera()->Elevation(60.0);
renderer->GetActiveCamera()->Azimuth(30.0);
renderer->GetActiveCamera()->Zoom(1.0);
renderer->SetBackground(colors->GetColor3d("Cornsilk").GetData());
renWin->SetSize(300,300);
renWin->SetSize(600, 600);
// interact with data
renWin->Render();
iren->Start();
// Clean up
plane->Delete();
renderer->Delete();
renWin->Delete();
iren->Delete();
xCoords->Delete();
yCoords->Delete();
zCoords->Delete();
rgrid->Delete();
rgridMapper->Delete();
wireActor->Delete();
return 0;
return EXIT_SUCCESS;
}
......@@ -20,106 +20,101 @@
// can be added to the dataset.
//
//
#include "vtkActor.h"
#include "vtkCamera.h"
#include "vtkFloatArray.h"
#include "vtkHedgeHog.h"
#include "vtkMath.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkStructuredGrid.h"
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkFloatArray.h>
#include <vtkHedgeHog.h>
#include <vtkMath.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkStructuredGrid.h>
#include <array>
int main()
{
int i, j, k, kOffset, jOffset, offset;
float x[3], v[3], rMin=0.5, rMax=1.0, deltaRad, deltaZ;
float radius, theta;
static const int dims[3]={13,11,11};
vtkNew<vtkNamedColors> colors;
float rMin = 0.5, rMax = 1.0, deltaRad, deltaZ;
std::array<int, 3> dims = {{13, 11, 11}};
// Create the structured grid.
vtkStructuredGrid *sgrid = vtkStructuredGrid::New();
sgrid->SetDimensions(dims);
vtkNew<vtkStructuredGrid> sgrid;
sgrid->SetDimensions(dims.data());
// We also create the points and vectors. The points
// form a hemi-cylinder of data.
vtkFloatArray *vectors = vtkFloatArray::New();
vectors->SetNumberOfComponents(3);
vectors->SetNumberOfTuples(dims[0]*dims[1]*dims[2]);
vtkPoints *points = vtkPoints::New();
points->Allocate(dims[0]*dims[1]*dims[2]);
deltaZ = 2.0 / (dims[2]-1);
deltaRad = (rMax-rMin) / (dims[1]-1);
v[2]=0.0;
for ( k=0; k<dims[2]; k++)
vtkNew<vtkFloatArray> vectors;
vectors->SetNumberOfComponents(3);
vectors->SetNumberOfTuples(dims[0] * dims[1] * dims[2]);
vtkNew<vtkPoints> points;
points->Allocate(dims[0] * dims[1] * dims[2]);
deltaZ = 2.0 / (dims[2] - 1);
deltaRad = (rMax - rMin) / (dims[1] - 1);
float x[3], v[3];
v[2] = 0.0;
for (auto k = 0; k < dims[2]; k++)
{
x[2] = -1.0 + k*deltaZ;
kOffset = k * dims[0] * dims[1];
for (j=0; j<dims[1]; j++)
x[2] = -1.0 + k * deltaZ;
int kOffset = k * dims[0] * dims[1];
for (auto j = 0; j < dims[1]; j++)
{
radius = rMin + j*deltaRad;
jOffset = j * dims[0];
for (i=0; i<dims[0]; i++)
float radius = rMin + j * deltaRad;
int jOffset = j * dims[0];
for (auto i = 0; i < dims[0]; i++)
{