[vtkusers] how to export a ply file with color infomation

luoyx luoyx08 at gmail.com
Mon Aug 8 23:01:30 EDT 2011


David:

The compilable code is shown below . Input file mesh.vtk and output file
mesh.ply is attached,too.
i just do not know how to add color information to a plywriter.
http://vtk.1045678.n5.nabble.com/file/n4680555/mesh.vtk mesh.vtk 
http://vtk.1045678.n5.nabble.com/file/n4680555/mesh.ply mesh.ply 

#include "stdafx.h"
#include <iostream>
#include <vtkPolyDataReader.h>
#include <vtkPolyData.h>
#include <vtkPLYWriter.h> 
#include <vtkTypedArray.h>
#include <vtkDenseArray.h> 
#include <vtkSmartPointer.h>
#include <vtkUnsignedCharArray.h> 
#include <vtkLookupTable.h> 
#include <vtkFieldData.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	//Import the vtk file
	 vtkSmartPointer<vtkPolyDataReader>
reader=vtkSmartPointer<vtkPolyDataReader>::New();
	 reader->SetFileName("mesh.vtk");
	 vtkSmartPointer<vtkDataObject>
vtkdata=vtkSmartPointer<vtkDataObject>::New();
	 vtkdata=reader->GetOutputDataObject(0);

	 vtkIdType  size=36; //SIZE OF POINTS (I also do not known how to extract
this number from reader)
	
	 // Set the color  infomation
	vtkSmartPointer<vtkUnsignedCharArray>   
color_array=vtkSmartPointer<vtkUnsignedCharArray>::New();
	color_array->SetName("RGB");
	color_array->SetNumberOfComponents(3);
	color_array->SetNumberOfTuples(size);
   
     for(vtkIdType j =0; j!=size; ++j)
	   for(vtkIdType i = 0; i!=3; ++i) 
 	 {
		 color_array->SetValue(j*3+i,255);
	 }

	// Convert vtk file to ply(add color info)
	vtkSmartPointer<vtkPLYWriter> writer=vtkSmartPointer<vtkPLYWriter>::New();
	writer->SetFileName("mesh.ply");
	writer->SetInput(vtkdata);
	writer->SetFileTypeToASCII();
	writer->SetColorModeToDefault();
	writer->SetArrayName("RGB");
	writer->Write();

	return 0;
}

--
View this message in context: http://vtk.1045678.n5.nabble.com/how-to-export-a-ply-file-with-color-infomation-tp4676437p4680555.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list