[vtkusers] Color the faces of vtkUnstructuredGridGeometryFilter
Bill Lorensen
bill.lorensen at gmail.com
Mon Jan 21 10:18:19 EST 2013
This example colors the faces of polydata. It might be useful:
http://vtk.org/Wiki/VTK/Examples/Cxx/PolyData/ColorCells
On Thu, Jan 17, 2013 at 5:19 AM, Gishara Indeewarie <gish.777 at gmail.com> wrote:
> Hi all,
>
> I am creating a vtkUnstructuredGrid and extract its faces using
> vtkUnstructuredGridGeometryFilter.
> I want to add colors to each face of the resulting unstructured grid.
> Is there a way that we can assign scalar data to faces?
> I tried with getCellData method but it did not give any output.
> Below is my code:
>
> #include <vtkVersion.h>
> #include <vtkCellArray.h>
> #include <vtkPoints.h>
> #include <vtkHexahedron.h>
> #include <vtkUnstructuredGrid.h>
>
> #include <vtkSmartPointer.h>
> #include <vtkDataSetMapper.h>
> #include <vtkActor.h>
> #include <vtkRenderWindow.h>
> #include <vtkRenderer.h>
> #include <vtkRenderWindowInteractor.h>
> #include <vtkCellData.h>
> #include <vtkCell.h>
> #include <vtkLookupTable.h>
> #include <vtkFloatArray.h>
> #include <vtkUnstructuredGridGeometryFilter.h>
> #include <vtkPointData.h>
>
> int main(int, char *[])
> {
> // Setup the coordinates of eight points
> // (the two faces must be in counter clockwise order as viewd from the
> outside)
> double P0[3] = {0.0, 0.0, 0.0};
> double P1[3] = {1.0, 0.0, 0.0};
> double P2[3] = {1.0, 1.0, 0.0};
> double P3[3] = {0.0, 1.0, 0.0};
> double P4[3] = {0.0, 0.0, 1.0};
> double P5[3] = {1.0, 0.0, 1.0};
> double P6[3] = {1.0, 1.0, 1.0};
> double P7[3] = {0.0, 1.0, 1.0};
>
>
> // Create the points
> vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
> points->InsertNextPoint(P0);
> points->InsertNextPoint(P1);
> points->InsertNextPoint(P2);
> points->InsertNextPoint(P3);
> points->InsertNextPoint(P4);
> points->InsertNextPoint(P5);
> points->InsertNextPoint(P6);
> points->InsertNextPoint(P7);
>
> // Create a hexahedron from the points
> vtkSmartPointer<vtkHexahedron> hex =
> vtkSmartPointer<vtkHexahedron>::New();
> hex->GetPointIds()->SetId(0,0);
> hex->GetPointIds()->SetId(1,1);
> hex->GetPointIds()->SetId(2,2);
> hex->GetPointIds()->SetId(3,3);
> hex->GetPointIds()->SetId(4,4);
> hex->GetPointIds()->SetId(5,5);
> hex->GetPointIds()->SetId(6,6);
> hex->GetPointIds()->SetId(7,7);
>
> // Add the hexahedron to a cell array
> vtkSmartPointer<vtkCellArray> hexs =
> vtkSmartPointer<vtkCellArray>::New();
> hexs->InsertNextCell(hex);
>
> vtkSmartPointer<vtkFloatArray> cellData =
> vtkSmartPointer<vtkFloatArray>::New();
> for (int i = 0; i < 6; i++)
> {
> cellData->InsertNextValue(i + 1);
> }
> // Add the points and hexahedron to an unstructured grid
> vtkSmartPointer<vtkUnstructuredGrid> uGrid =
> vtkSmartPointer<vtkUnstructuredGrid>::New();
> uGrid->SetPoints(points);
> uGrid->InsertNextCell(hex->GetCellType(), hex->GetPointIds());
>
> vtkSmartPointer<vtkUnstructuredGridGeometryFilter> geometryFilter =
> vtkSmartPointer<vtkUnstructuredGridGeometryFilter>::New();
>
> geometryFilter->SetInput(uGrid);
> geometryFilter->Update();
>
> vtkUnstructuredGrid* grid2 = geometryFilter->GetOutput();
> /grid2->GetCellData()->SetScalars(cellData);
> // Create a lookup table to map cell data to colors
> grid2->se
> vtkSmartPointer<vtkLookupTable> lut =
> vtkSmartPointer<vtkLookupTable>::New();
> int tableSize = 1;
> lut->SetNumberOfTableValues(tableSize);
> lut->Build();
>
>
> // Visualize
> vtkSmartPointer<vtkDataSetMapper> mapper =
> vtkSmartPointer<vtkDataSetMapper>::New();
> #if VTK_MAJOR_VERSION <= 5
> mapper->SetInputConnection(grid2->GetProducerPort());
> #else
> mapper->SetInputData(uGrid);
> #endif
> mapper->SetScalarRange(0, tableSize - 1);
> mapper->SetLookupTable(lut);
>
> vtkSmartPointer<vtkActor> actor =
> vtkSmartPointer<vtkActor>::New();
> actor->SetMapper(mapper);
>
> 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(.2, .3, .4);
>
> renderWindow->Render();
> renderWindowInteractor->Start();
>
> return EXIT_SUCCESS;
> }
>
> Thanks in advance.
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
--
Unpaid intern in BillsBasement at noware dot com
More information about the vtkusers
mailing list