Hi,
I’m beginner. I read tutorial and watch examples but some basic questions still actual.
So here is simple example of vtkStructuredGrid. I’m trying to add data to the points and display this data as color. The problem that this modified example doesn’t work. As a result my displayed data is the same as in the picture from the example link. Please, give me a hint what I’am doing wrong?
Here is my modified code:
#include <vtkSmartPointer.h>
#include <vtkStructuredGrid.h>
#include <vtkXMLStructuredGridWriter.h>
#include <vtkDataSetMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkDoubleArray.h>
#include <vtkFloatArray.h>
#include <vtkLookupTable.h>
#include <vtkScalarBarActor.h>
#include <vtkPointData.h>
int main(int, char *[])
{
// Create a grid
vtkSmartPointer<vtkStructuredGrid> structuredGrid =
vtkSmartPointer<vtkStructuredGrid>::New();
vtkSmartPointer<vtkPoints> points =
vtkSmartPointer<vtkPoints>::New();
vtkDoubleArray *array = vtkDoubleArray::New();
size_t nx = 2, ny = 3, nz = 2;
double x, y, z;
x = 0.0;
y = 0.0;
z = 0.0;
for (size_t i = 0; i < nx*ny*nz; i++){
array->InsertNextValue(i);
}
for(unsigned int k = 0; k < nz; k++)
{
z += 2.0;
for(unsigned int j = 0; j < ny; j++)
{
y += 1.0;
for(unsigned int i = 0; i < nx; i++)
{
x += .5;
points->InsertNextPoint(x, y, z);
}
}
}
// Specify the dimensions of the grid
structuredGrid->SetDimensions(nx,ny,nz);
structuredGrid->SetPoints(points);
structuredGrid->GetPointData()->AddArray(array);
int* dims = structuredGrid->GetDimensions();
// Retrieve the entries from the grid and print them to the screen
unsigned int counter = 0;
for (int k = 0; k < dims[2]; k++)
{
for (int j = 0; j < dims[1]; j++)
{
for (int i = 0; i < dims[0]; i++)
{
double p[3];
structuredGrid->GetPoint(counter, p);
double pNew[3];
structuredGrid->GetPoint(i, j, k, pNew);
std::cout << "P : "
<< p[0] << " "
<< p[1] << " "
<< p[2] << std::endl;
std::cout << "PNew: "
<< pNew[0] << " "
<< pNew[1] << " "
<< pNew[2] << std::endl;
counter++;
}
}
}
// Map the scalar values in the image to colors with a lookup table:
vtkSmartPointer<vtkLookupTable> lookupTable =
vtkSmartPointer<vtkLookupTable>::New();
//lookupTable->SetRange(0.0, nx*ny*nz); // image intensity range
//lookupTable->SetValueRange(0.0, nx*ny*nz); // from black to white
lookupTable->SetTableRange(0, nx*ny*nz);
//lookupTable->SetSaturationRange(0.0, nx*ny*nz); // no color saturation
//lookupTable->SetNumberOfTableValues(256);
lookupTable->SetRampToLinear();
lookupTable->Build();
// Create a mapper and actor
vtkSmartPointer<vtkDataSetMapper> mapper =
vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputData(structuredGrid);
mapper->SetLookupTable(lookupTable);
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// Create a renderer, render window, and interactor
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// Add the actor to the scene
renderer->AddActor(actor);
renderer->SetBackground(.3, .6, .3); // Background color green
// Render and interact
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
Best regards,
kerim