# How to set colors for PointData of vtkStructuredGrid example

**URL:** https://discourse.vtk.org/t/how-to-set-colors-for-pointdata-of-vtkstructuredgrid-example/3159
**Category:** Support
**Created:** [April 26, 2020, 12:07am UTC](https://discourse.vtk.org/t/how-to-set-colors-for-pointdata-of-vtkstructuredgrid-example/3159 "2020-04-26T00:07:58Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![kerim](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kerim/32/1175_2.png) [@kerim](https://discourse.vtk.org/u/kerim)
#### Post date: [April 26, 2020, 12:07am UTC](https://discourse.vtk.org/t/how-to-set-colors-for-pointdata-of-vtkstructuredgrid-example/3159/1 "2020-04-26T00:07:58Z")

</div>

Hi,

I’m beginner. I read tutorial and watch examples but some basic questions still actual.  
So [here is simple example](https://lorensen.github.io/VTKExamples/site/Cxx/StructuredGrid/StructuredGrid/) 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

---

<div class="post-metadata">

### Author: ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)
#### Post date: [April 27, 2020, 1:58am UTC](https://discourse.vtk.org/t/how-to-set-colors-for-pointdata-of-vtkstructuredgrid-example/3159/2 "2020-04-27T01:58:15Z")

</div>

Here is a simplified solution that you can use [StructuredGrid.cxx](https://discourse.vtk.org/uploads/short-url/gEG8lAxfmkxhEh5OxWraLsLAydo.cxx) (3.1 KB) . Here you have the choice of colouring either by point data or cell data.

If you need it, this is the CMake file to use[CMakeLists.txt](https://discourse.vtk.org/uploads/short-url/j5AkIkSEmTAEVNoHydyTuAYex1C.txt) (2.7 KB) :

---

<div class="post-metadata">

### Author: ![kerim](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kerim/32/1175_2.png) [@kerim](https://discourse.vtk.org/u/kerim)
#### Post date: [April 27, 2020, 12:39pm UTC](https://discourse.vtk.org/t/how-to-set-colors-for-pointdata-of-vtkstructuredgrid-example/3159/3 "2020-04-27T12:39:55Z")

</div>

Thank for working example! It really helped
