Difference between revisions of "VTK/Examples/Cxx/PolyData/ColorCellsWithRGB"

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to: navigation, search
(Created page with "<div class="floatright">File:VTK_Examples_Baseline_PolyData_TestColorCellsWithRGB.png</div> This example shows how to assign specific colors to individual cells of a polydata...")
 
(Replaced content with "= '''See [https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/ColorCellsWithRGB ColorCellsWithRGB] on the new [https://lorensen.github.io/VTKExamples/site/ VTKExample...")
 
Line 1: Line 1:
<div class="floatright">[[File:VTK_Examples_Baseline_PolyData_TestColorCellsWithRGB.png]]</div>
+
= '''See [https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/ColorCellsWithRGB ColorCellsWithRGB] on the new [https://lorensen.github.io/VTKExamples/site/ VTKExamples website].''' =
This example shows how to assign specific colors to individual cells of a polydata. This example stores the cell data as rgb triples.
 
 
 
==ColorCellsWithRGB.cxx==
 
<source lang="cpp">
 
#include <vtkSmartPointer.h>
 
#include <vtkPolyDataMapper.h>
 
#include <vtkActor.h>
 
#include <vtkRenderWindow.h>
 
#include <vtkRenderer.h>
 
#include <vtkRenderWindowInteractor.h>
 
 
 
#include <vtkMath.h>
 
#include <vtkUnsignedCharArray.h>
 
#include <vtkCellData.h>
 
#include <vtkPolyData.h>
 
#include <vtkPlaneSource.h>
 
 
int main(int , char *[])
 
{
 
  // Provide some geometry
 
  int resolutionX = 5;
 
  int resolutionY = 3;
 
 
 
  vtkSmartPointer<vtkPlaneSource> aPlane =
 
    vtkSmartPointer<vtkPlaneSource>::New();
 
  aPlane->SetXResolution(resolutionX);
 
  aPlane->SetYResolution(resolutionY);
 
  aPlane->Update();
 
 
 
  // Create cell data
 
  vtkMath::RandomSeed(8775070); // for reproducibility
 
  vtkSmartPointer<vtkUnsignedCharArray> cellData =
 
    vtkSmartPointer<vtkUnsignedCharArray>::New();
 
  cellData->SetNumberOfComponents(3);
 
  cellData->SetNumberOfTuples(aPlane->GetOutput()->GetNumberOfCells());
 
  for (int i = 0; i < aPlane->GetOutput()->GetNumberOfCells(); i++)
 
    {
 
    float rgb[3];
 
    rgb[0] = vtkMath::Random(64, 255);
 
    rgb[1] = vtkMath::Random(64, 255);
 
    rgb[2] = vtkMath::Random(64, 255);
 
    cellData->InsertTuple(i, rgb);
 
    }
 
 
 
  aPlane->GetOutput()->GetCellData()->SetScalars(cellData);
 
 
 
  // Setup actor and mapper
 
  vtkSmartPointer<vtkPolyDataMapper> mapper =
 
    vtkSmartPointer<vtkPolyDataMapper>::New();
 
  mapper->SetInputConnection(aPlane->GetOutputPort());
 
 
 
  vtkSmartPointer<vtkActor> actor =
 
    vtkSmartPointer<vtkActor>::New();
 
  actor->SetMapper(mapper);
 
 
  // Setup render window, renderer, 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);
 
  renderer->AddActor(actor);
 
  renderer->SetBackground(.1,.2,.3);
 
  renderWindow->Render();
 
  renderWindowInteractor->Start();
 
 
  return EXIT_SUCCESS;
 
}
 
</source>
 
 
 
{{VTKCMakeLists|{{SUBPAGENAME}}}}
 

Latest revision as of 16:31, 9 September 2019

See ColorCellsWithRGB on the new VTKExamples website.