<div dir="ltr">Hi all,<div><br></div><div style>I am creating a vtkUnstructuredGrid and extract its faces using vtkUnstructuredGridGeometryFilter.</div><div style>I want to add colors to each face of the resulting unstructured grid. </div>
<div style>Is there a way that we can assign scalar data to faces?</div><div style>I tried with getCellData method but it did not give any output.</div><div style>Below is my code:</div><div style><br></div><div style><div>
#include <vtkVersion.h></div><div>#include <vtkCellArray.h></div><div>#include <vtkPoints.h></div><div>#include <vtkHexahedron.h></div><div>#include <vtkUnstructuredGrid.h></div><div> </div><div>
#include <vtkSmartPointer.h></div><div>#include <vtkDataSetMapper.h></div><div>#include <vtkActor.h></div><div>#include <vtkRenderWindow.h></div><div>#include <vtkRenderer.h></div><div>#include <vtkRenderWindowInteractor.h></div>
<div>#include <vtkCellData.h></div><div>#include <vtkCell.h></div><div>#include <vtkLookupTable.h></div><div>#include <vtkFloatArray.h></div><div>#include <vtkUnstructuredGridGeometryFilter.h></div>
<div>#include <vtkPointData.h></div><div> </div><div>int main(int, char *[])</div><div>{</div><div> // Setup the coordinates of eight points </div><div> // (the two faces must be in counter clockwise order as viewd from the outside)</div>
<div> double P0[3] = {0.0, 0.0, 0.0};</div><div> double P1[3] = {1.0, 0.0, 0.0};</div><div> double P2[3] = {1.0, 1.0, 0.0};</div><div> double P3[3] = {0.0, 1.0, 0.0};</div><div> double P4[3] = {0.0, 0.0, 1.0};</div><div>
double P5[3] = {1.0, 0.0, 1.0};</div><div> double P6[3] = {1.0, 1.0, 1.0};</div><div> double P7[3] = {0.0, 1.0, 1.0};</div><div> </div><div> </div><div> // Create the points</div><div> vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();</div>
<div> points->InsertNextPoint(P0);</div><div> points->InsertNextPoint(P1);</div><div> points->InsertNextPoint(P2);</div><div> points->InsertNextPoint(P3);</div><div> points->InsertNextPoint(P4);</div><div>
points->InsertNextPoint(P5);</div><div> points->InsertNextPoint(P6);</div><div> points->InsertNextPoint(P7);</div><div> </div><div> // Create a hexahedron from the points</div><div> vtkSmartPointer<vtkHexahedron> hex = </div>
<div> vtkSmartPointer<vtkHexahedron>::New();</div><div> hex->GetPointIds()->SetId(0,0);</div><div> hex->GetPointIds()->SetId(1,1);</div><div> hex->GetPointIds()->SetId(2,2);</div><div> hex->GetPointIds()->SetId(3,3);</div>
<div> hex->GetPointIds()->SetId(4,4);</div><div> hex->GetPointIds()->SetId(5,5);</div><div> hex->GetPointIds()->SetId(6,6);</div><div> hex->GetPointIds()->SetId(7,7);</div><div> </div><div> // Add the hexahedron to a cell array</div>
<div> vtkSmartPointer<vtkCellArray> hexs = </div><div> vtkSmartPointer<vtkCellArray>::New();</div><div> hexs->InsertNextCell(hex);</div><div> </div><div> vtkSmartPointer<vtkFloatArray> cellData =</div>
<div> vtkSmartPointer<vtkFloatArray>::New();</div><div><span class="" style="white-space:pre">        </span>for (int i = 0; i < 6; i++)</div><div> {</div><div><span class="" style="white-space:pre">                </span> cellData->InsertNextValue(i + 1);</div>
<div> }</div><div><span class="" style="white-space:pre">        </span></div><div> // Add the points and hexahedron to an unstructured grid</div><div> vtkSmartPointer<vtkUnstructuredGrid> uGrid =</div><div> vtkSmartPointer<vtkUnstructuredGrid>::New();</div>
<div> uGrid->SetPoints(points);</div><div> uGrid->InsertNextCell(hex->GetCellType(), hex->GetPointIds());</div><div><br></div><div> vtkSmartPointer<vtkUnstructuredGridGeometryFilter> geometryFilter = </div>
<div> vtkSmartPointer<vtkUnstructuredGridGeometryFilter>::New();</div><div><br></div><div> geometryFilter->SetInput(uGrid);</div><div> geometryFilter->Update(); </div><div> </div><div> vtkUnstructuredGrid* grid2 = geometryFilter->GetOutput();</div>
<div> /grid2->GetCellData()->SetScalars(cellData);</div><div> // Create a lookup table to map cell data to colors</div><div> grid2->se</div><div> vtkSmartPointer<vtkLookupTable> lut =</div><div> vtkSmartPointer<vtkLookupTable>::New();</div>
<div> int tableSize = 1;</div><div> lut->SetNumberOfTableValues(tableSize);</div><div> lut->Build();</div><div> </div><div> </div><div> // Visualize</div><div> vtkSmartPointer<vtkDataSetMapper> mapper = </div>
<div> vtkSmartPointer<vtkDataSetMapper>::New();</div><div>#if VTK_MAJOR_VERSION <= 5</div><div> mapper->SetInputConnection(grid2->GetProducerPort());</div><div>#else</div><div> mapper->SetInputData(uGrid);</div>
<div>#endif</div><div> mapper->SetScalarRange(0, tableSize - 1);</div><div> mapper->SetLookupTable(lut);</div><div> </div><div> vtkSmartPointer<vtkActor> actor = </div><div> vtkSmartPointer<vtkActor>::New();</div>
<div> actor->SetMapper(mapper);</div><div> </div><div> vtkSmartPointer<vtkRenderer> renderer = </div><div> vtkSmartPointer<vtkRenderer>::New();</div><div> vtkSmartPointer<vtkRenderWindow> renderWindow = </div>
<div> vtkSmartPointer<vtkRenderWindow>::New();</div><div> renderWindow->AddRenderer(renderer);</div><div> vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = </div><div> vtkSmartPointer<vtkRenderWindowInteractor>::New();</div>
<div> renderWindowInteractor->SetRenderWindow(renderWindow);</div><div> </div><div> renderer->AddActor(actor);</div><div> //renderer->SetBackground(.2, .3, .4);</div><div> </div><div> renderWindow->Render();</div>
<div> renderWindowInteractor->Start();</div><div> </div><div> return EXIT_SUCCESS;</div><div>}</div><div><br></div><div style>Thanks in advance.</div></div></div>