Creating a new vtkDataSet from an existing vtkDataSet


I just met a question about creating a new vtkDataSet from an existing vtkDataSet. I have an original dataset A

vtkSmartPointer<vtkDataSet> A = CreateDataSetA();

And now I want to create a deepcopy of the DataSet A with all the PointData and CellData. I tried different ways and print the new Dataset B:

//***Not working in the compiling***
vtkSmartPointer<vtkDataSet> B = vtkSmartPointer<vtkDataSet>::New();

//***Working but probably Shallow copy***
vtkSmartPointer<vtkDataSet> B = vtkSmartPointer<vtkDataSet>(A);

//***Has coordinates of A but no Point Data***
vtkSmartPointer<vtkDataSet> B = vtkSmartPointer<vtkDataSet>(A);

vtkSmartPointer<vtkDataSet> B = vtkSmartPointer<vtkDataSet>::Newinstance(A);

//***Print function***
std::filebuf file_Buff;"./test.txt", std::ios::out);
std::ostream os(&file_Buff);

The last one is working and I guess that is a Deep Copy. But I wonder why thoese weird things happen.

I would be appreciate if someone could explain that.

The last one is indeed correct. If you want to copy object A to object B, you first have to create object B with same type than object A. The NewInstance() does that. Then, the DeepCopy() will deep copy (meaning allocate new memory, not sharing pointers) all data from object A into object B.

Thanks so much. I realize that after a while and the data structure in VTK is kind of different from the basic data type.