[vtkusers] how to convert AppendFilter output to polyData?

vinmean iamdpak at gmail.com
Wed Sep 13 01:12:09 EDT 2017


Below is an c++ code I wrote for combining two polygon source. I tweaked an
existing example so my code is at the end. Problem is, after combining two
polygon data (using appendFilter),  I am not able to access the polyData of
the combined actor. It throws a segmentation fault when I access the
polyData points using GetPoints() function



#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyData.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkDataSetMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkAssembly.h>
#include <vtkAppendFilter.h>
#include <vtkUnstructuredGrid.h>
#include <vtkRenderWindowInteractor.h>




void merge_polyData_in_viz(int actor_strt_ID,vtkSmartPointer<vtkRenderer>
renderer, vtkSmartPointer<vtkAppendFilter> appendFilter
,vtkSmartPointer<vtkAssembly> assembly)
{
	vtkSmartPointer<vtkActorCollection> actors =
vtkSmartPointer<vtkActorCollection>::New();
	actors = renderer->GetActors();
	actors->InitTraversal();

	//skip the first actor_strt_ID actors
	for (vtkIdType a = 0; a < actor_strt_ID; ++a)
	{
	  vtkActor * actor = actors->GetNextActor();
	}

	for (vtkIdType a = actor_strt_ID; a < actors->GetNumberOfItems(); ++a)
	{
	  vtkActor * actor = actors->GetNextActor();
	  assembly->AddPart(actor);
	  vtkSmartPointer<vtkPolyData>  pd =
vtkPolyData::SafeDownCast(actor->GetMapper()->GetInput());
	  vtkSmartPointer<vtkUnstructuredGrid> ug =
vtkSmartPointer<vtkUnstructuredGrid>::New();
	  ug->SetPoints(pd->GetPoints());
	  appendFilter->AddInputData(pd);
	  appendFilter->AddInputData(ug);
	  appendFilter->Update();
	}


}


int main(int, char *[])
{

	// Create the polydata geometry

  vtkSmartPointer<vtkSphereSource> sphereSource =
    vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->Update();

  // Set up the actor to display the untransformed polydata

  vtkSmartPointer<vtkPolyDataMapper> originalMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  originalMapper->SetInputConnection(sphereSource->GetOutputPort());

  vtkSmartPointer<vtkActor> originalActor =
    vtkSmartPointer<vtkActor>::New();
  originalActor->SetMapper(originalMapper);
  originalActor->GetProperty()->SetColor(1,0,0);

  // Set up the transform filter

  vtkSmartPointer<vtkTransform> translation =
    vtkSmartPointer<vtkTransform>::New();
  translation->Translate(1.0, 2.0, 3.0);

  vtkSmartPointer<vtkTransformPolyDataFilter> transformFilter =
    vtkSmartPointer<vtkTransformPolyDataFilter>::New();
  transformFilter->SetInputConnection(sphereSource->GetOutputPort());
  transformFilter->SetTransform(translation);
  transformFilter->Update();

  // Set up the actor to display the transformed polydata

  vtkSmartPointer<vtkPolyDataMapper> transformedMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  transformedMapper->SetInputConnection(transformFilter->GetOutputPort());

  vtkSmartPointer<vtkActor> transformedActor =
    vtkSmartPointer<vtkActor>::New();
  transformedActor->SetMapper(transformedMapper);
  transformedActor->GetProperty()->SetColor(0,1,0);


  // Set up the rest of the visualization pipeline
  vtkSmartPointer<vtkRenderer> renderer =
     vtkSmartPointer<vtkRenderer>::New();
   renderer->AddActor(originalActor);
   renderer->AddActor(transformedActor);
   renderer->SetBackground(.3, .6, .3); // Set renderer's background color
to green

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

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

   renderWindowInteractor->Start();

   /////////////////////////////////////////addional code
/////////////////////////////////////////////////

   //merge two actors to create a new actor
   vtkSmartPointer<vtkActor> merged_actor =
vtkSmartPointer<vtkActor>::New();
   vtkSmartPointer<vtkAppendFilter> appendFilter =
vtkSmartPointer<vtkAppendFilter>::New();
   vtkSmartPointer<vtkAssembly>	asmbly =
vtkSmartPointer<vtkAssembly>::New();
   merge_polyData_in_viz(0,renderer, appendFilter,asmbly);

  	//new actor and mapper
  	vtkSmartPointer<vtkDataSetMapper> merged_mapper =
vtkSmartPointer<vtkDataSetMapper>::New();
  	merged_mapper->SetInputConnection(appendFilter->GetOutputPort());
  	merged_actor->SetMapper(merged_mapper);

  	//trying to get points of new polyData
  	vtkSmartPointer<vtkPolyData>  pd =
vtkPolyData::SafeDownCast(merged_actor->GetMapper()->GetInput());
  	pd->GetPoints();




   return EXIT_SUCCESS;

}



--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html


More information about the vtkusers mailing list