I want to show a simple polygon using vtkPolyData / vtkPolygon , but seems the result is strange
Points
points->InsertNextPoint(0, -1, 2);
points->InsertNextPoint(1, -0.5, 2);
points->InsertNextPoint(2, -3, 2);
points->InsertNextPoint(2, 3, 2);
points->InsertNextPoint(1, 0.5, 2);
points->InsertNextPoint(0, 1, 2);
Results:
#include <vtkActor.h>
#include <vtkCellArray.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTriangle.h>
#include <vtkUnsignedCharArray.h>
// For compatibility with new VTK generic data arrays.
#ifdef vtkGenericDataArray_h
#define InsertNextTupleValue InsertNextTypedTuple
#endif
#include <vtkPolygon.h>
int main(int, char*[])
{
vtkNew<vtkNamedColors> nc;
// Setup points.
vtkNew<vtkPoints> points;
points->InsertNextPoint(0, -1, 2);
points->InsertNextPoint(1, -0.5, 2);
points->InsertNextPoint(2, -3, 2);
points->InsertNextPoint(2, 3, 2);
points->InsertNextPoint(1, 0.5, 2);
points->InsertNextPoint(0, 1, 2);
// Define some colors.
unsigned char red[3] = {255, 0, 0};
unsigned char green[3] = {0, 255, 0};
unsigned char blue[3] = {0, 0, 255};
// Setup the colors array.
vtkNew<vtkUnsignedCharArray> colors;
colors->SetNumberOfComponents(3);
colors->SetName("Colors");
// Add the three colors we have created to the array.
colors->InsertNextTupleValue(red);
colors->InsertNextTupleValue(green);
colors->InsertNextTupleValue(blue);
colors->InsertNextTupleValue(red);
colors->InsertNextTupleValue(green);
colors->InsertNextTupleValue(blue);
// Create a triangle.
vtkNew<vtkCellArray> triangles;
vtkNew<vtkPolygon> triangle;
triangle->GetPointIds()->SetNumberOfIds(6);
triangle->GetPointIds()->SetId(0, 0);
triangle->GetPointIds()->SetId(1, 1);
triangle->GetPointIds()->SetId(2, 2);
triangle->GetPointIds()->SetId(3, 3);
triangle->GetPointIds()->SetId(4, 4);
triangle->GetPointIds()->SetId(5, 5);
triangles->InsertNextCell(triangle);
// Create a polydata object and add everything to it.
vtkNew<vtkPolyData> polydata;
polydata->SetPoints(points);
polydata->SetPolys(triangles);
polydata->GetPointData()->SetScalars(colors);
// Visualize
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputData(polydata);
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("TriangleColoredPoints");
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderer->SetBackground(nc->GetColor3d("SlateGray").GetData());
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}