效果如下:

代码:
#include <vtkActor.h>
#include <vtkCellArray.h>
#include <vtkCellData.h>
#include <vtkLine.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkUnsignedCharArray.h>
#ifndef INITIAL_OPENGL
#define INITIAL_OPENGL
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
#endif
#ifdef vtkGenericDataArray_h
#define InsertNextTupleValue InsertNextTypedTuple
#endif
int main(void)
{
vtkNew<vtkPolyData> linesPolyData;
double origin[3] = { 0.0, 0.0, 0.0 };
double p0[3] = { 1.0, 0.0, 0.0 };
double p1[3] = { 0.0, 1.0, 0.0 };
vtkNew<vtkPoints> pts;
pts->InsertNextPoint(origin);
pts->InsertNextPoint(p0);
pts->InsertNextPoint(p1);
linesPolyData->SetPoints(pts);
vtkNew<vtkLine> line0;
line0->GetPointIds()->SetId(0, 0);
line0->GetPointIds()->SetId(1, 1);
vtkNew<vtkLine> line1;
line1->GetPointIds()->SetId(0, 0);
line1->GetPointIds()->SetId(1, 2);
vtkNew<vtkCellArray> lines;
lines->InsertNextCell(line0);
lines->InsertNextCell(line1);
linesPolyData->SetLines(lines);
vtkNew<vtkNamedColors> namedColors;
vtkNew<vtkUnsignedCharArray> colors;
colors->SetNumberOfComponents(3);
colors->InsertNextTupleValue(namedColors->GetColor3ub("Tomato").GetData());
colors->InsertNextTupleValue(namedColors->GetColor3ub("Mint").GetData());
linesPolyData->GetCellData()->SetScalars(colors);
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputData(linesPolyData);
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
actor->GetProperty()->SetLineWidth(4);
vtkNew<vtkRenderer> renderer;
renderer->AddActor(actor);
renderer->SetBackground(namedColors->GetColor3d("SlateGray").GetData());
vtkNew<vtkRenderWindow> window;
window->SetWindowName("Colored Lines");
window->AddRenderer(renderer);
vtkNew<vtkRenderWindowInteractor> interactor;
interactor->SetRenderWindow(window);
window->Render();
interactor->Start();
system("pause");
return EXIT_SUCCESS;
}
893

被折叠的 条评论
为什么被折叠?



