Add colored lines to a Polydata
This examples adds two colored lines to a polydata. Be sure to changed "Color by" to "Colors" and uncheck "Map Scalars" to see these lines correctly in Paraview! <source lang="cpp">
- include <vtkCellArray.h>
- include <vtkCellData.h>
- include <vtkDoubleArray.h>
- include <vtkPoints.h>
- include <vtkLine.h>
- include <vtkPolyData.h>
- include <vtkXMLPolyDataWriter.h>
int main(int argc, char *argv[]) { //create three points. Will will join (Origin and P0) with a red line and (Origin and P1) with a green line double Origin[3]; Origin[0] = 0.0; Origin[1] = 0.0; Origin[2] = 0.0;
double P0[3]; P0[0] = 1.0; P0[1] = 0.0; P0[2] = 0.0;
double P1[3]; P1[0] = 0.0; P1[1] = 1.0; P1[2] = 0.0;
//create a vtkPoints object and store the points in it vtkPoints* pts = vtkPoints::New(); pts->InsertNextPoint(Origin); pts->InsertNextPoint(P0); pts->InsertNextPoint(P1);
//setup two colors - one for each line unsigned char red[3]; red[0] = 255; red[1] = 0; red[2] = 0;
unsigned char green[3]; green[0] = 0; green[1] = 255; green[2] = 0;
//setup the colors array vtkUnsignedCharArray* Colors = vtkUnsignedCharArray::New(); Colors->SetNumberOfComponents(3); Colors->SetName("Colors");
//add the colors we created to the colors array Colors->InsertNextTupleValue(red); Colors->InsertNextTupleValue(green);
//Create the first line (between Origin and P0) vtkLine* line0 = vtkLine::New(); line0->GetPointIds()->SetId(0,0); //the second 0 is the index of the Origin in the vtkPoints line0->GetPointIds()->SetId(1,1); //the second 1 is the index of P0 in the vtkPoints
//Create the second line (between Origin and P1) vtkLine* line1 = vtkLine::New(); line1->GetPointIds()->SetId(0,0); //the second 0 is the index of the Origin in the vtkPoints line1->GetPointIds()->SetId(1,2); //2 is the index of P1 in the vtkPoints
//create a cell array to store the lines in and add the lines to it vtkCellArray* lines = vtkCellArray::New(); lines->InsertNextCell(line0); lines->InsertNextCell(line1);
//create a polydata to store everything in vtkPolyData* pdata = vtkPolyData::New();
//add the points to the dataset pdata->SetPoints(pts);
//add the lines to the dataset pdata->SetLines(lines);
//color the lines - associate the first component (red) of the colors array with the first component of the cell array (line 0) and the second component (green) of the colors array with the second component of the cell array (line 1) pdata->GetCellData()->AddArray(Colors);
//write the file vtkXMLPolyDataWriter* writer = vtkXMLPolyDataWriter::New(); writer->SetInput(pdata); writer->SetFileName("ColoredLines.vtp"); writer->Write();
return 0; }
</source>