How to deal with share points among multiple datasets?

This is a problem related to sharing nodes among multiple datasets.

Suppose there are three datasets: one vtkUnstructuredGrid and two vtkPolyData.

The nodes in these three datasets are assigned unified global node numbers with a global node identifier. However, during storage, the topological structures are stored separately, resulting in each dataset having its own local node numbering. While global numbering storage can be achieved through SetGlobalId, a challenge arises when picking nodes from these three datasets. In the interface region of the datasets, nodes are picked twice, causing data duplication.

Even I use vtkMultiBlockDatasets or vtkPartitionedDataSetCollection for storage, the issue of duplicate nodes at the boundaries persists. How can this problem be resolved?

For example, there are three cubes (such as cube1, cube2, cube3) arranged consecutively, so, cube1 and cube2 share one face, and cube2 and cube3 share another face. The 12 nodes have unified numbering, and their coordinates are stored in a single vtkPoints (let’s assume it’s called points). The topological structures of these three cubes are stored in three vtkPolyData (Poly1, Poly2, and Poly3). When assigning coordinates to these three PolyData, the respective points can be extracted from the common points array for assignment. In this scenario, when making selections (area picking), if points, lines, or faces on the interface are chosen, there is a risk of inadvertently selecting duplicate points, or it may not be clear which line or face from which vtkPolyData is being selected.

Is there any way to deal with such problem directly?
In vtkMultiBlockDatasets or vtkPartitionedDataSetCollection, each block data have there local id, how to share global id?

You should set the data array pointer to same location using ShallowCopy in order to optimize for memory consumption.

You talk about storage though, which format are you using ?

I have another problem for showing vtkPartitionedDataSetCollection data.
Could you please tell me how to show vtkPartitionedDataSetCollection? I know one method is to show the data separately. Is there any method to show vtkPartitionedDataSetCollection at once (only one mapper and one actor)?

The method for separately show the data as following:

    vtkNew<vtkPoints> points1;
    points1->InsertNextPoint(0.0, 0.0, 0.0);
    points1->InsertNextPoint(1.0, 0.0, 0.0);
    points1->InsertNextPoint(1.0, 1.0, 0.0);
    points1->InsertNextPoint(0.0, 1.0, 0.0);
    points1->InsertNextPoint(0.0, 0.0, 1.0);
    points1->InsertNextPoint(1.0, 0.0, 1.0);
    points1->InsertNextPoint(1.0, 1.0, 1.0);
    points1->InsertNextPoint(0.0, 1.0, 1.0);

    vtkNew<vtkPoints> points2;
    points2->InsertNextPoint(1.0, 0.0, 0.0);
    points2->InsertNextPoint(2.0, 0.0, 0.0);
    points2->InsertNextPoint(2.0, 1.0, 0.0);
    points2->InsertNextPoint(1.0, 1.0, 0.0);
    points2->InsertNextPoint(1.0, 0.0, 1.0);
    points2->InsertNextPoint(2.0, 0.0, 1.0);
    points2->InsertNextPoint(2.0, 1.0, 1.0);
    points2->InsertNextPoint(1.0, 1.0, 1.0);

    vtkNew<vtkCellArray> cells1;
    vtkIdType hex1[8] = {0, 1, 2, 3, 4, 5, 6, 7};
    cells1->InsertNextCell(8, hex1);
    vtkNew<vtkUnstructuredGrid> grid1;
    grid1->SetPoints(points1);
    grid1->SetCells(VTK_HEXAHEDRON, cells1);

    vtkNew<vtkCellArray> cells2;
    vtkIdType hex2[8] = {0, 1, 2, 3, 4, 5, 6, 7};
    cells2->InsertNextCell(8, hex2);
    vtkNew<vtkUnstructuredGrid> grid2;
    grid2->SetPoints(points2);
    grid2->SetCells(VTK_HEXAHEDRON, cells2);

    vtkNew<vtkIdTypeArray> id1;
    id1->InsertNextValue(0);
    id1->InsertNextValue(1);
    id1->InsertNextValue(4);
    id1->InsertNextValue(3);
    id1->InsertNextValue(6);
    id1->InsertNextValue(7);
    id1->InsertNextValue(10);
    id1->InsertNextValue(9);
    grid1->GetPointData()->SetGlobalIds(id1);

    vtkNew<vtkIdTypeArray> id2;
    id2->InsertNextValue(1);
    id2->InsertNextValue(2);
    id2->InsertNextValue(5);
    id2->InsertNextValue(4);
    id2->InsertNextValue(7);
    id2->InsertNextValue(8);
    id2->InsertNextValue(11);
    id2->InsertNextValue(10);
    grid2->GetPointData()->SetGlobalIds(id2);

    vtkNew<vtkCylinderSource> cylinderSource;
    cylinderSource->SetCenter(5,0,0);
    cylinderSource->SetResolution(100);
    cylinderSource->SetHeight(2.0);
    cylinderSource->SetRadius(1.0);
    cylinderSource->Update();

    vtkNew<vtkSphereSource> sphereSource;
    sphereSource->SetCenter(10, 0,0);
    sphereSource->SetRadius(1.0);
    sphereSource->SetThetaResolution(100);
    sphereSource->SetPhiResolution(100);
    sphereSource->Update();

    vtkNew<vtkPartitionedDataSet> partionDataSet1;
    partionDataSet1->SetNumberOfPartitions(2);
    partionDataSet1->SetPartition(0, grid1);
    partionDataSet1->SetPartition(1, sphereSource->GetOutput());

    vtkNew<vtkPartitionedDataSet> partionDataSet2;
    partionDataSet2->SetNumberOfPartitions(2);
    partionDataSet2->SetPartition(0, grid2);
    partionDataSet2->SetPartition(1, cylinderSource->GetOutput());

    vtkNew<vtkPartitionedDataSetCollection> partionDataSetColl;
    partionDataSetColl->SetNumberOfPartitionedDataSets(2);
    partionDataSetColl->SetPartitionedDataSet(0, partionDataSet1);
    partionDataSetColl->SetPartitionedDataSet(1, partionDataSet2);
double r=1, g=0, b=0;
    vtkNew<vtkRenderer> renderer;
    for(vtkIdType i = 0; i<partionDataSetColl->GetNumberOfPartitionedDataSets(); i++)
    {
        vtkSmartPointer<vtkPartitionedDataSet> partion = partionDataSetColl->GetPartitionedDataSet(i);
        for(vtkIdType j=0; j<partion->GetNumberOfPartitions(); j++)
        {
            vtkSmartPointer<vtkDataSet> data = partionDataSetColl->GetPartition(i, j);
            vtkNew<vtkDataSetMapper> mapper;
            mapper->SetInputData(data);
            vtkNew<vtkActor> actor;
            actor->SetMapper(mapper);

            vtkNew<vtkProperty> property;
            property->SetColor(r, g, b);
            actor->SetProperty(property);
            r-=0.2;
            g+=0.2;
            b+=0.1;
            renderer->AddActor(actor);
        }
    }


    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->AddRenderer(renderer);
    vtkNew<vtkRenderWindowInteractor> interactor;
    interactor->SetRenderWindow(renderWindow);


    renderer->SetBackground(0.1, 0.1, 0.1);
    renderWindow->SetSize(800, 600);
    renderWindow->Render();

    interactor->Start();

Use the composite poly data mapper ?

https://vtk.org/doc/nightly/html/classvtkCompositePolyDataMapper2.html

vtkCompositePolyDataMapper2 does not correctly show the results.
The same for vtkCompositePolyDataMapper.
They only for vtkPolyData, not for unstructuredgrid, or mixed data.
In new version of VTk, vtkCompositePolyDataMapper2 is deprecated, vtkCompositePolyDataMapper instead.

What do you get and why is it not correct ?

It shows nothing, and quickly quit.

In new version of VTk, vtkCompositePolyDataMapper2 is deprecated. If replaced by vtkCompositePolyDataMapper, errors shows “vtkCompositePolyDataMapper (0x1aff17c3b70): Expected a vtkDataObjectTree or vtkPolyData input. Got vtkUnstructuredGrid”

Here is my code:

#include <vtkSmartPointer.h>
#include <vtkPartitionedDataSet.h>
#include <vtkPartitionedDataSetCollection.h>
#include <vtkUnstructuredGrid.h>
#include <vtkHexahedron.h>
#include <vtkPoints.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPointData.h>
#include <vtkCompositePolyDataMapper2.h>
#include <vtkCylinderSource.h>
#include <vtkSphereSource.h>
#include <vtkCompositePolyDataMapper.h>

int main(int argc, char* argv[])
{
    vtkNew<vtkRenderer> renderer;

    vtkNew<vtkPoints> points1;
    points1->InsertNextPoint(0.0, 0.0, 0.0);
    points1->InsertNextPoint(1.0, 0.0, 0.0);
    points1->InsertNextPoint(1.0, 1.0, 0.0);
    points1->InsertNextPoint(0.0, 1.0, 0.0);
    points1->InsertNextPoint(0.0, 0.0, 1.0);
    points1->InsertNextPoint(1.0, 0.0, 1.0);
    points1->InsertNextPoint(1.0, 1.0, 1.0);
    points1->InsertNextPoint(0.0, 1.0, 1.0);

    vtkNew<vtkPoints> points2;
    points2->InsertNextPoint(1.0, 0.0, 0.0);
    points2->InsertNextPoint(2.0, 0.0, 0.0);
    points2->InsertNextPoint(2.0, 1.0, 0.0);
    points2->InsertNextPoint(1.0, 1.0, 0.0);
    points2->InsertNextPoint(1.0, 0.0, 1.0);
    points2->InsertNextPoint(2.0, 0.0, 1.0);
    points2->InsertNextPoint(2.0, 1.0, 1.0);
    points2->InsertNextPoint(1.0, 1.0, 1.0);

    vtkNew<vtkCellArray> cells1;
    vtkIdType hex1[8] = {0, 1, 2, 3, 4, 5, 6, 7};
    cells1->InsertNextCell(8, hex1);
    vtkNew<vtkUnstructuredGrid> grid1;
    grid1->SetPoints(points1);
    grid1->SetCells(VTK_HEXAHEDRON, cells1);

    vtkNew<vtkCellArray> cells2;
    vtkIdType hex2[8] = {0, 1, 2, 3, 4, 5, 6, 7};
    cells2->InsertNextCell(8, hex2);
    vtkNew<vtkUnstructuredGrid> grid2;
    grid2->SetPoints(points2);
    grid2->SetCells(VTK_HEXAHEDRON, cells2);

    vtkNew<vtkIdTypeArray> id1;
    id1->InsertNextValue(0);
    id1->InsertNextValue(1);
    id1->InsertNextValue(4);
    id1->InsertNextValue(3);
    id1->InsertNextValue(6);
    id1->InsertNextValue(7);
    id1->InsertNextValue(10);
    id1->InsertNextValue(9);
    grid1->GetPointData()->SetGlobalIds(id1);

    vtkIdType cellIdToRemove = 0;
    grid1->GetCell(cellIdToRemove)->Initialize();
    grid1->Modified();

    vtkNew<vtkIdTypeArray> id2;
    id2->InsertNextValue(1);
    id2->InsertNextValue(2);
    id2->InsertNextValue(5);
    id2->InsertNextValue(4);
    id2->InsertNextValue(7);
    id2->InsertNextValue(8);
    id2->InsertNextValue(11);
    id2->InsertNextValue(10);
    grid2->GetPointData()->SetGlobalIds(id2);

    vtkNew<vtkCylinderSource> cylinderSource;
    cylinderSource->SetCenter(5,0,0);
    cylinderSource->SetResolution(100);
    cylinderSource->SetHeight(2.0);
    cylinderSource->SetRadius(1.0);
    cylinderSource->Update();

    vtkNew<vtkSphereSource> sphereSource;
    sphereSource->SetCenter(10, 0,0);
    sphereSource->SetRadius(1.0);
    sphereSource->SetThetaResolution(100);
    sphereSource->SetPhiResolution(100);
    sphereSource->Update();

    vtkNew<vtkPartitionedDataSet> partionDataSet1;
    partionDataSet1->SetNumberOfPartitions(2);
    partionDataSet1->SetPartition(0, grid1);
    partionDataSet1->SetPartition(1, sphereSource->GetOutput());

    vtkNew<vtkPartitionedDataSet> partionDataSet2;
    partionDataSet2->SetNumberOfPartitions(2);
    partionDataSet2->SetPartition(0, grid2);
    partionDataSet2->SetPartition(1, cylinderSource->GetOutput());

    vtkNew<vtkPartitionedDataSetCollection> partionDataSetColl;
    partionDataSetColl->SetNumberOfPartitionedDataSets(2);
    partionDataSetColl->SetPartitionedDataSet(0, partionDataSet1);
    partionDataSetColl->SetPartitionedDataSet(1, partionDataSet2);

    vtkNew<vtkCompositePolyDataMapper2> mapper;
    mapper->SetInputDataObject(partionDataSetColl);
    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);
    renderer->AddActor(actor);
    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->AddRenderer(renderer);
    vtkNew<vtkRenderWindowInteractor> interactor;
    interactor->SetRenderWindow(renderWindow);

    renderer->SetBackground(0.1, 0.1, 0.1);
    renderWindow->SetSize(800, 600);
    renderWindow->Render();

    interactor->Start();
    return 0;
}

Here is your issue.

Thank you for your reply. My trouble is that there are both vtkPolyData and vtkUnstructuredgrid in the data, and I didn’t want to convert vtkUnstructuredgrid to vtkpolydata to show.
That is to say, I hope to be able to display both vtkPolydata and vtkUnstructuredgrid simultaneously.