Removing and Re-Adding Cells to vtkPolyData:

Hello All,

I have a vtkPolyData object where the user will select several input points. Using these points the connected cells will then be deleted from the mesh. I want to make a copy of the removed cells so they can be re-added back to the mesh at a latter point.

I am able to delete the cells using vtkRemovePolyData, but I run into problems when I am trying to copy the cells that are marked for deletion into another vtkPolyData object.

The smallest example I can post of how I am trying to do this is shown bellow where path array is a list of nodes that forms a closed path on the input mesh a sphere in this case.

   vtkNew<vtkSphereSource> Sphere;
   Sphere->SetCenter(0, 0, 0);
   Sphere->SetRadius(1.10);
	Sphere->Update();
    vtkNew<vtkRemovePolyData> RemovePoints;
	vtkSmartPointer<vtkCellArray> RemovedCells = vtkSmartPointer<vtkCellArray>::New();
	vtkSmartPointer<vtkPoints> RemovedPoints = vtkSmartPointer<vtkPoints>::New();
	vtkSmartPointer<vtkPolyData> RemovedPolyData = vtkSmartPointer<vtkPolyData>::New();
	
	vtkSmartPointer<vtkIdList> ConnectedCellIds = vtkSmartPointer<vtkIdList>::New();
	vtkSmartPointer<vtkIdList> RemovedCellIds = vtkSmartPointer<vtkIdList>::New();
	vtkSmartPointer<vtkIdList> RemovedPointIds = vtkSmartPointer<vtkIdList>::New();
	vtkSmartPointer<vtkIdList> CellPoints = vtkSmartPointer<vtkIdList>::New();

	ConnectedCellIds->Reset();
	Sphere->GetOutput()->BuildLinks();

	for(int i = 0; i<PathARRAY->GetNumberOfValues(); i++){
		Sphere->GetOutput()->GetPointCells(PathARRAY->GetValue(i), ConnectedCellIds);
	}

	RemovePoints->SetInputData(Sphere->GetOutput());
	RemovePoints->SetPointIds(PathARRAY);
	RemovePoints->Update();
	
	// Get vtkIdList of cells that will be removed from the mesh
	for(int i = 0; i<PathARRAY->GetNumberOfValues(); i++){
		ConnectedCellIds->Reset();
		Sphere->GetOutput()->GetPointCells(PathARRAY->GetValue(i), ConnectedCellIds);
		for(int j = 0; j<ConnectedCellIds->GetNumberOfIds(); j++){
			RemovedCellIds->InsertUniqueId(ConnectedCellIds->GetId(j));
		}
	}

	// Get vtkIdList of points that make up the cells that make up the mesh
	vtkIdType PointId;
	for(int i = 0; i<RemovedCellIds->GetNumberOfIds(); i++){
		for(int j = 0; j<Sphere->GetOutput()->GetCell(RemovedCellIds->GetId(i))->GetPointIds()->GetNumberOfIds(); j++){
			PointId = Sphere->GetOutput()->GetCell(RemovedCellIds->GetId(i))->GetPointIds()->GetId(j);
			RemovedPointIds->InsertUniqueId(PointId);
			RemovedPoints->InsertNextPoint(Sphere->GetOutput()->GetCell(RemovedCellIds->GetId(i))->GetPoints()->GetPoint(PointId));
		}
	}

	// Construct the polydata object that makes up the region of the mesh that is being removed
	// Add the points 
	for(int i = 0; i<RemovedPointIds->GetNumberOfIds(); i++){
		RemovedPoints->InsertNextPoint(Sphere->GetOutput()->GetPoint(RemovedPointIds->GetId(i)));
	}
	// Add the cells
	for(int i = 0; i<RemovedCellIds->GetNumberOfIds(); i++){
		RemovedCells->InsertNextCell(3);
		RemovedCells->InsertCellPoint(i*3 + i);
	}

	for(int i = 0; i<RemovedCellIds->GetNumberOfIds(); i++){
		RemovedCells->InsertNextCell(3);
		CellPoints->Reset();
		CellPoints = Sphere->GetOutput()->GetCell(RemovedCellIds->GetId(i))->GetPointIds();
		for(int j = 0; j<CellPoints->GetNumberOfIds(); j++){
			RemovedCells->InsertCellPoint(CellPoints->GetId(j));
		}
	}

Any help or suggestions on how to make this functional would be greatly appreciated.

you can take a look at this example: https://examples.vtk.org/site/Cxx/Meshes/AddCell/ that shows exactly how to add triangle to vtkpolydata