[vtkusers] Why does this example not work ?

De Boer Ingo I.deBoer at polytec.de
Wed Apr 23 07:04:06 EDT 2003


Hi,

I made the code lines below. It should compile and work. Actually, I wanted
something like a X,Y,Z and XYZ-View. But all the cubes look the same although
I have set the vectors differently. Where is the problem/wrong thinking or .. ?

greets
  Ingo


/////////////////////////////////////////////////////////////////////////////

#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"
#include "vtkWarpVector.h"
#include "vtkPolyDataNormals.h"
#include "vtkVectorDot.h"
#include "vtkDataSetMapper.h"

void createobject(vtkFloatArray  *vectors, vtkActor *actor)
{
   // 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();

   // Load the point, cell, and data attributes.
   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}};
   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);

   // We now assign the pieces to the vtkPolyData.
   cube->SetPoints(points);
   points->Delete();
   cube->SetPolys(polys);
   polys->Delete();
   cube->GetPointData()->SetScalars(scalars);
   scalars->Delete();
   cube->GetPointData()->SetVectors(vectors);

   // set warp vector, normals
   vtkWarpVector *warpvector = vtkWarpVector::New();
   warpvector->SetInput(cube);
   warpvector->SetScaleFactor(1.0);
   vtkPolyDataNormals *normals = vtkPolyDataNormals::New();
   normals->SetInput(warpvector->GetPolyDataOutput());
   vtkVectorDot *vectordot = vtkVectorDot::New();
   vectordot->SetInput(normals->GetOutput());
   normals->Delete();

   // Now we'll look at it.
   vtkDataSetMapper *cubeMapper = vtkDataSetMapper::New();
   cubeMapper->SetInput(vectordot->GetOutput());
   cubeMapper->SetScalarRange(0,7);

   actor->SetMapper(cubeMapper);

   // delete 
   vectordot->Delete();
   cube->Delete();
   cubeMapper->Delete();
   warpvector->Delete();
}

int main( int argc, char *argv[] )
{
   static float v[8][3]={{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0},
      {-6.850900e-04, 1.131900e-01, -4.838000e-04}, 
      {-3.533400e-02, 1.260400e-01, 6.314500e-02}, 
      {-5.096200e-02, 8.419700e-02, 6.382000e-02}, 
      {-1.532600e-03, 2.888500e-01, -7.648200e-04}};

   vtkCamera *camera = vtkCamera::New();
   camera->SetPosition(1,1,1);
   camera->SetFocalPoint(0,0,0);

   vtkRenderer *rendererX     = vtkRenderer::New();
   vtkRenderer *rendererY     = vtkRenderer::New();
   vtkRenderer *rendererZ     = vtkRenderer::New();
   vtkRenderer *rendererXYZ   = vtkRenderer::New();

   vtkRenderWindow *renWin = vtkRenderWindow::New();

   renWin->AddRenderer(rendererX);
   renWin->AddRenderer(rendererY);
   renWin->AddRenderer(rendererZ);
   renWin->AddRenderer(rendererXYZ);

   vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
   iren->SetRenderWindow(renWin);

   vtkActor       *actorX     = vtkActor::New();
   vtkActor       *actorY     = vtkActor::New();
   vtkActor       *actorZ     = vtkActor::New();
   vtkActor       *actorXYZ   = vtkActor::New();
   vtkFloatArray  *vectors    = vtkFloatArray::New();

   vectors->SetNumberOfComponents(3);
   for (int i=0; i<8; i++) vectors->InsertTuple3(i, v[i][0], 0.0, 0.0);
   ::createobject(vectors, actorX);
   for (int i=0; i<8; i++) vectors->InsertTuple3(i, 0.0, v[i][1], 0.0);
   ::createobject(vectors, actorY);
   for (int i=0; i<8; i++) vectors->InsertTuple3(i, 0.0, 0.0, v[i][2]);
   ::createobject(vectors, actorZ);
   for (int i=0; i<8; i++) vectors->InsertTuple3(i, v[i][0], v[i][1], v[i][2]);
   ::createobject(vectors, actorXYZ);

   rendererX->AddActor(actorX);
   rendererX->SetViewport(0.0, 0.5, 0.5, 1.0);
   rendererX->SetBackground(1, 0, 0);
   rendererX->SetActiveCamera(camera);
   rendererX->ResetCamera();

   rendererY->AddActor(actorY);
   rendererY->SetViewport(0.5, 0.5, 1.0, 1.0);
   rendererY->SetBackground(0, 1, 0);
   rendererY->SetActiveCamera(camera);
   rendererY->ResetCamera();

   rendererZ->AddActor(actorZ);
   rendererZ->SetViewport(0.0, 0.0, 0.5, 0.5);
   rendererZ->SetBackground(0, 0, 1);
   rendererZ->SetActiveCamera(camera);
   rendererZ->ResetCamera();

   rendererXYZ->AddActor(actorXYZ);
   rendererXYZ->SetViewport(0.5, 0.0, 1.0, 0.5);
   rendererXYZ->SetBackground(0.1, 0.2, 0.4);
   rendererXYZ->SetActiveCamera(camera);
   rendererXYZ->ResetCamera();

   renWin->SetSize(512, 512);

   // interact with data
   renWin->Render();
   iren->Start();

   // Clean up
   actorX->Delete();
   actorY->Delete();
   actorZ->Delete();
   actorXYZ->Delete();
   vectors->Delete();
   camera->Delete();
   rendererX->Delete();
   rendererY->Delete();
   rendererZ->Delete();
   rendererXYZ->Delete();
   renWin->Delete();
   iren->Delete();

   return 0;
}




More information about the vtkusers mailing list