[vtkusers] vtkXMLPolyDataReader/Writer problem

madz madaramh at gmail.com
Thu Nov 7 23:27:17 EST 2013


Here is a simplified version of the program;

The construction of the grid and writing it,

	std::string filename = "ouputTest.vtu";

	vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
	points->InsertNextPoint(0, 0, 0);
	points->InsertNextPoint(1, 0, 0);
	points->InsertNextPoint(1, 1, 0);
	points->InsertNextPoint(0, 1, 1);

	vtkSmartPointer<vtkTetra> tetra = vtkSmartPointer<vtkTetra>::New();
	tetra->GetPointIds()->SetId(0, 0);
	tetra->GetPointIds()->SetId(1, 1);
	tetra->GetPointIds()->SetId(2, 2);
	tetra->GetPointIds()->SetId(3, 3);

	vtkSmartPointer<vtkCellArray> cellArray =
vtkSmartPointer<vtkCellArray>::New();
	cellArray->InsertNextCell(tetra);

	vtkSmartPointer<vtkUnstructuredGrid> grid =
vtkSmartPointer<vtkUnstructuredGrid>::New();
	grid->SetPoints(points);
	grid->SetCells(VTK_TETRA, cellArray);

	vtkSmartPointer<vtkGeometryFilter> geometryFilter =
vtkSmartPointer<vtkGeometryFilter>::New();
	vtkSmartPointer<vtkPolyData> polydata; 
	geometryFilter->SetInput(grid);
	geometryFilter->Update(); 
	polydata = geometryFilter->GetOutput();


	vtkSmartPointer<vtkDoubleArray> tempArray =
vtkSmartPointer<vtkDoubleArray>::New();

	for(int i=0;i<4;i++)
	{
		tempArray->InsertNextValue(i/2.5);
	}
	polydata->GetPointData()->SetScalars(tempArray);
	grid->GetPointData()->SetScalars(tempArray);


	vtkSmartPointer<vtkLookupTable> colorLookupTable =
vtkSmartPointer<vtkLookupTable>::New();

	colorLookupTable->SetTableRange(-100,500);
	double vmin,vmax; 
	colorLookupTable->GetHueRange(vmin,vmax);
	if(vmin!=vmax)
	{
		colorLookupTable->SetHueRange(0.6667,0.0);
	}
	colorLookupTable->Build();

	vtkSmartPointer<vtkContourFilter> contourFilter =
vtkSmartPointer<vtkContourFilter>::New();
	contourFilter->SetInputConnection(polydata->GetProducerPort()); 
	contourFilter->ComputeScalarsOn();
	contourFilter->GenerateValues(10,-100,500);

	vtkSmartPointer<vtkPolyDataMapper> mapper1 =
vtkSmartPointer<vtkPolyDataMapper>::New();
	mapper1->SetInputConnection(contourFilter->GetOutputPort());
	mapper1->SetScalarRange(-100,500);
	mapper1->SetLookupTable(colorLookupTable);
	mapper1->ScalarVisibilityOn();

	vtkSmartPointer<vtkActor> actor1 = vtkSmartPointer<vtkActor>::New();
	actor1->SetMapper(mapper1);

	vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
	mapper->SetInput(polydata);
	mapper->ScalarVisibilityOn();
	mapper->InterpolateScalarsBeforeMappingOn();
	mapper->SetLookupTable(colorLookupTable);
	mapper->SetScalarRange(-100,500);

	vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
	actor->SetMapper(mapper);

	// Visualize
	vtkSmartPointer<vtkRenderer> renderer =vtkSmartPointer<vtkRenderer>::New();
	vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
	renderWindow->AddRenderer(renderer);

	vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
	renderWindowInteractor->SetRenderWindow(renderWindow);

	renderer->AddActor(actor1);
	renderer->AddActor(actor);
	renderer->SetBackground(.3, .6, .3); // Background color green

	// Write file
	vtkSmartPointer<vtkXMLUnstructuredGridWriter> writer =
		vtkSmartPointer<vtkXMLUnstructuredGridWriter>::New();
	writer->SetFileName(filename.c_str());
#if VTK_MAJOR_VERSION <= 5
	writer->SetInput(grid);
#else
	writer->SetInputData(grid);
#endif
	writer->SetDataModeToAscii();
	writer->Write();
	renderWindow->Render();
	renderWindowInteractor->Start();

Reading the file,


 	std::string filename = "ouputTest.vtu";       
	vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();

	reader->SetFileName(filename.c_str());
	reader->Update();

	vtkSmartPointer<vtkDataSetMapper> mapper =
vtkSmartPointer<vtkDataSetMapper>::New();
	mapper->SetInputConnection(reader->GetOutputPort());

	vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
	actor->SetMapper(mapper);

	//vtkSmartPointer<vtkGeometryFilter> geometryFilter =
vtkSmartPointer<vtkGeometryFilter>::New();
	//vtkSmartPointer<vtkPolyData> polydata; 
	//geometryFilter->SetInput(reader->GetOutput());
	//geometryFilter->Update(); 
	//polydata = geometryFilter->GetOutput();

	//vtkSmartPointer<vtkContourFilter> contourFilter =
vtkSmartPointer<vtkContourFilter>::New();
	//contourFilter->SetInputConnection(polydata->GetProducerPort()); 
	//vtkSmartPointer<vtkDataSetMapper> mapper1 =
vtkSmartPointer<vtkDataSetMapper>::New();
	//mapper1->SetInput(polydata);
	//mapper1->ScalarVisibilityOn();

	//vtkSmartPointer<vtkActor> actor1 = vtkSmartPointer<vtkActor>::New();
	//actor1->SetMapper(mapper1);

	vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
	vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
	renderWindow->AddRenderer(renderer);

	vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
	renderWindowInteractor->SetRenderWindow(renderWindow);

	renderer->AddActor(actor);
	//renderer->AddActor(actor1);
	renderer->SetBackground(1, 1, 1); 
	renderWindow->Render();
	renderWindowInteractor->Start();

I think that the problem lies with the commented piece of code, they don't
seem to be working.
Any help would be appreciated.
Thanks.





--
View this message in context: http://vtk.1045678.n5.nabble.com/vtkXMLPolyDataReader-Writer-problem-tp5724345p5724354.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list