# How to deal with share points among multiple datasets?

**URL:** https://discourse.vtk.org/t/how-to-deal-with-share-points-among-multiple-datasets/12915
**Category:** Support
**Tags:** code
**Created:** [December 17, 2023, 4:49am UTC](https://discourse.vtk.org/t/how-to-deal-with-share-points-among-multiple-datasets/12915 "2023-12-17T04:49:12Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![rockydut](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rockydut/32/4261_2.png) [@rockydut](https://discourse.vtk.org/u/rockydut)
#### Post date: [December 17, 2023, 4:49am UTC](https://discourse.vtk.org/t/how-to-deal-with-share-points-among-multiple-datasets/12915/1 "2023-12-17T04:49:12Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![rockydut](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rockydut/32/4261_2.png) [@rockydut](https://discourse.vtk.org/u/rockydut)
#### Post date: [December 17, 2023, 4:52am UTC](https://discourse.vtk.org/t/how-to-deal-with-share-points-among-multiple-datasets/12915/2 "2023-12-17T04:52:21Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [December 18, 2023, 9:11am UTC](https://discourse.vtk.org/t/how-to-deal-with-share-points-among-multiple-datasets/12915/3 "2023-12-18T09:11:01Z")

</div>

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 ?

---

<div class="post-metadata">

### Author: ![rockydut](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rockydut/32/4261_2.png) [@rockydut](https://discourse.vtk.org/u/rockydut)
#### Post date: [December 22, 2023, 2:18am UTC](https://discourse.vtk.org/t/how-to-deal-with-share-points-among-multiple-datasets/12915/4 "2023-12-22T02:18:18Z")

</div>

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:

```auto
    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();

```

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [January 2, 2024, 7:50am UTC](https://discourse.vtk.org/t/how-to-deal-with-share-points-among-multiple-datasets/12915/5 "2024-01-02T07:50:16Z")

</div>

Use the composite poly data mapper ?

[https://vtk.org/doc/nightly/html/classvtkCompositePolyDataMapper2.html](https://vtk.org/doc/nightly/html/classvtkCompositePolyDataMapper2.html)

---

<div class="post-metadata">

### Author: ![rockydut](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rockydut/32/4261_2.png) [@rockydut](https://discourse.vtk.org/u/rockydut)
#### Post date: [January 3, 2024, 1:24am UTC](https://discourse.vtk.org/t/how-to-deal-with-share-points-among-multiple-datasets/12915/6 "2024-01-03T01:24:37Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [January 3, 2024, 8:21am UTC](https://discourse.vtk.org/t/how-to-deal-with-share-points-among-multiple-datasets/12915/7 "2024-01-03T08:21:42Z")

</div>

> [@rockydut](#):
>
> does not correctly show the results.

What do you get and why is it not correct ?

---

<div class="post-metadata">

### Author: ![rockydut](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rockydut/32/4261_2.png) [@rockydut](https://discourse.vtk.org/u/rockydut)
#### Post date: [January 5, 2024, 12:55am UTC](https://discourse.vtk.org/t/how-to-deal-with-share-points-among-multiple-datasets/12915/8 "2024-01-05T00:55:10Z")

</div>

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”

---

<div class="post-metadata">

### Author: ![rockydut](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rockydut/32/4261_2.png) [@rockydut](https://discourse.vtk.org/u/rockydut)
#### Post date: [January 5, 2024, 12:55am UTC](https://discourse.vtk.org/t/how-to-deal-with-share-points-among-multiple-datasets/12915/9 "2024-01-05T00:55:40Z")

</div>

Here is my code:

```auto
#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;
}

```

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [January 8, 2024, 8:40am UTC](https://discourse.vtk.org/t/how-to-deal-with-share-points-among-multiple-datasets/12915/10 "2024-01-08T08:40:12Z")

</div>

> [@rockydut](#):
>
> Expected a vtkDataObjectTree or vtkPolyData input. Got vtkUnstructuredGrid”

Here is your issue.

---

<div class="post-metadata">

### Author: ![rockydut](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rockydut/32/4261_2.png) [@rockydut](https://discourse.vtk.org/u/rockydut)
#### Post date: [January 8, 2024, 10:50am UTC](https://discourse.vtk.org/t/how-to-deal-with-share-points-among-multiple-datasets/12915/11 "2024-01-08T10:50:45Z")

</div>

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.
