# Multi geometry interaction

**URL:** https://discourse.vtk.org/t/multi-geometry-interaction/9276
**Category:** Support
**Tags:** proposal
**Created:** [September 4, 2022, 9:02am UTC](https://discourse.vtk.org/t/multi-geometry-interaction/9276 "2022-09-04T09:02:31Z")
**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: [September 4, 2022, 9:02am UTC](https://discourse.vtk.org/t/multi-geometry-interaction/9276/1 "2022-09-04T09:02:31Z")

</div>

I have multiple geometries, each of which is stored in vtkunstructuredGrid/vtkPolyData. I hope that when the mouse clicks on a geometry during interaction, the geometry can be selected.  
Of course, I also want to be able to select facets, points and so on in the geometry.  
I used a vtkActor to display each geometry currently.  
That is, there are as many vtkActors as there are geometry, so the mouse can select the actor.  
But this operation is very inconvenient.  
Is it possible to display only one vtkActor, but it is convenient to select geometry.

---

<div class="post-metadata">

### Author: ![tmnku](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/e95f7d/32.png) [@tmnku](https://discourse.vtk.org/u/tmnku)
#### Post date: [September 4, 2022, 9:26am UTC](https://discourse.vtk.org/t/multi-geometry-interaction/9276/2 "2022-09-04T09:26:36Z")

</div>

Hi Rockydut, what you’re trying sounds a bit like what I’m currently working on. For the selection part I had success with vtkMultiBlockDataSet and vtkCompositePolyDataMapper2. Then vtkHardwarePicker will return a blockId and a cellId which you can use to find out which geometry was selected and which cell of that geometry.

---

<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: [September 4, 2022, 10:04am UTC](https://discourse.vtk.org/t/multi-geometry-interaction/9276/3 "2022-09-04T10:04:15Z")

</div>

@tmnku could you please paste an example to me?  
This problem has puzzled me for a long time.  
Thanks a lot.

---

<div class="post-metadata">

### Author: ![tmnku](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/e95f7d/32.png) [@tmnku](https://discourse.vtk.org/u/tmnku)
#### Post date: [September 4, 2022, 10:13am UTC](https://discourse.vtk.org/t/multi-geometry-interaction/9276/4 "2022-09-04T10:13:27Z")

</div>

```auto
vtkNew<vtkMultiBlockDataSet> root;
root->SetNumberOfBlocks(2);
root->SetBlock(0, createPolyData());
root->SetBlock(1, createPolyData());

vtkNew<vtkCompositePolyDataMapper2> mapper;
mapper->SetInputDataObject(root);

vtkNew<vtkActor> actor;
actor->SetMapper(mapper);

```

`createPolyData()` is your custom function which returns the polydata you want to display. Then in the interactor use something like

```auto
void OnLeftButtonDown() {
    int* pos = this->GetInteractor()->GetEventPosition();
    vtkNew<vtkHardwarePicker> picker;
    int res = picker->Pick(pos[0], pos[1], 0, this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer());
    if (res == 1) {
      std::cout << "picked polydata " << picker->GetFlatBlockIndex() << ", cell " << picker->GetCellId() << std::endl;
    }
  }

```

---

<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: [September 4, 2022, 10:15am UTC](https://discourse.vtk.org/t/multi-geometry-interaction/9276/5 "2022-09-04T10:15:10Z")

</div>

Thank you very much. I’ll try it then give you a response.

---

<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: [September 4, 2022, 1:44pm UTC](https://discourse.vtk.org/t/multi-geometry-interaction/9276/6 "2022-09-04T13:44:28Z")

</div>

I have a new problem.  
How to set color for each geometry?

---

<div class="post-metadata">

### Author: ![tmnku](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/e95f7d/32.png) [@tmnku](https://discourse.vtk.org/u/tmnku)
#### Post date: [September 5, 2022, 5:47am UTC](https://discourse.vtk.org/t/multi-geometry-interaction/9276/7 "2022-09-05T05:47:54Z")

</div>

I actually had the same problem. `SetBlockColor()` of the mapper did not seem to have any effect neither did `SetBlockVisibility()`. I found an example in the unittests and it turns out that you have to set

```auto
vtkNew<vtkCompositeDataDisplayAttributes> cdsa;
mapper->SetCompositeDataDisplayAttributes(cdsa);

```

only then setting block attributes will work fine. When setting per block colors and visibility make sure to use its flat index which is not the same as the index when you use `SetBlock()`. In my case I had n polyDatas which I added to the root using `SetBlock(0), SetBlock(1)...` and then changed their color `SetBlockColor(1), SetBlockColor(2),...`.

You can also set colors within each polyData object as an alternative.

```auto
  vtkNew<vtkUnsignedCharArray> colors;
  colors->SetNumberOfComponents(3);
  for (int i = 0; i < polyData->GetNumberOfCells(); ++i) {
    colors->InsertNextTuple3(255, 0, 0);
  }
  polyData->GetCellData()->SetScalars(colors);

```

---

<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: [September 6, 2022, 12:30pm UTC](https://discourse.vtk.org/t/multi-geometry-interaction/9276/8 "2022-09-06T12:30:26Z")

</div>

I’m confuse for the color setting for each block.  
The following code show the method for setting color of each block. However, the final color is not show as the setting.

vtkSmartPointer CreateSphereDataSet(const vtkVector3d& center,  
double radius)  
{  
vtkNew leaf;  
leaf-\>SetCenter(center.GetData());  
leaf-\>SetRadius(radius);  
leaf-\>Update();  
return leaf-\>GetOutput();  
}

int main(int, char\*[])  
{

```
vtkNew<vtkNamedColors> colors;
vtkNew<vtkMultiBlockDataSet> root;

root->SetBlock(0, CreateSphereDataSet({0, 0, 0}, 1.0));
root->SetBlock(1, CreateSphereDataSet({5, 0, 0}, 2));
root->SetBlock(3, CreateSphereDataSet({10, 0, 0.0}, 3));

vtkNew<vtkCompositePolyDataMapper2> mapper;
mapper->SetInputDataObject(root);
vtkNew<vtkCompositeDataDisplayAttributes> cdsa;
mapper->SetCompositeDataDisplayAttributes(cdsa);

double color[3];
color[0] = 1; color[1] = 0; color[2] = 0;
cdsa->SetBlockColor(root->GetBlock(0), color);
color[0] = 0; color[1] = 1; color[2] = 0;
cdsa->SetBlockColor(root->GetBlock(1), color);
color[0] = 0; color[1] = 0; color[2] = 1;
cdsa->SetBlockColor(root->GetBlock(2), color);

//Another method for color setting
// mapper->SetBlockColor(0, 1, 0, 0);
// mapper->SetBlockColor(1, 0, 1, 0);

```

// mapper-\>SetBlockColor(2, 0, 0, 1);

```
// display the data
vtkNew<vtkRenderer> aren;
vtkNew<vtkRenderWindow> renWin;
renWin->AddRenderer(aren);
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);

vtkNew<vtkActor> actor;
actor->SetMapper(mapper);

aren->AddActor(actor);
aren->SetBackground(colors->GetColor3d("CornflowerBlue").GetData());

renWin->SetWindowName("MultiBLockDataSet");
renWin->Render();
iren->Start();
return 0;

```

}

---

<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: [September 6, 2022, 12:35pm UTC](https://discourse.vtk.org/t/multi-geometry-interaction/9276/9 "2022-09-06T12:35:13Z")

</div>

![P1](https://discourse.vtk.org/uploads/default/original/2X/8/833da8fb5e5dc94afba1230fb796db9222df0083.png)  
 ![P2](https://discourse.vtk.org/uploads/default/original/2X/6/6ec78816c4143b06f82400aecf962214202ca241.png)

The two method to color the ball is not same as that of the setting.

---

<div class="post-metadata">

### Author: ![tmnku](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/e95f7d/32.png) [@tmnku](https://discourse.vtk.org/u/tmnku)
#### Post date: [September 7, 2022, 9:03am UTC](https://discourse.vtk.org/t/multi-geometry-interaction/9276/10 "2022-09-07T09:03:32Z")

</div>

As I said before, you have to use the flat index in `SetBlockColor()` which starts at 1. Index 0 which you are using does not exist.

---

<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: [September 26, 2022, 10:04am UTC](https://discourse.vtk.org/t/multi-geometry-interaction/9276/11 "2022-09-26T10:04:20Z")

</div>

Do you test the vtkMultiBlockDataSet sorted the vtkUnstructuredGrid data and show it?  
When I do it, the data can not display. I don’t know why.  
When I transform ugrid to vtkPolyData, it can display correctly.  
as the below showing an example.

#include \<vtkActor.h\>  
#include \<vtkCompositeDataGeometryFilter.h\>  
#include \<vtkExtractEdges.h\>  
#include \<vtkMultiBlockDataSet.h\>  
#include \<vtkNamedColors.h\>  
#include \<vtkNew.h\>  
#include \<vtkPolyDataMapper.h\>  
#include \<vtkProperty.h\>  
#include \<vtkRenderWindow.h\>  
#include \<vtkRenderWindowInteractor.h\>  
#include \<vtkRenderer.h\>  
#include \<vtkSmartPointer.h\>  
#include \<vtkSphereSource.h\>  
#include \<vtkVector.h\>  
#include \<vtkCompositePolyDataMapper2.h\>  
#include \<vtkSTLReader.h\>  
#include \<vtkCompositeDataDisplayAttributes.h\>  
#include \<vtkUnstructuredGrid.h\>

vtkSmartPointer MakeHexahedron()  
{  
int numberOfVertices = 8;

```
// Create the points
vtkSmartPointer<vtkPoints> points =
        vtkSmartPointer<vtkPoints>::New();
points->InsertNextPoint(0.0, 0.0, 0.0);
points->InsertNextPoint(1.0, 0.0, 0.0);
points->InsertNextPoint(1.0, 1.0, 0.0);
points->InsertNextPoint(0.0, 1.0, 0.0);
points->InsertNextPoint(0.0, 0.0, 1.0);
points->InsertNextPoint(1.0, 0.0, 1.0);
points->InsertNextPoint(1.0, 1.0, 1.0);
points->InsertNextPoint(0.0, 1.0, 1.0);

// Create a hexahedron from the points
vtkSmartPointer<vtkHexahedron> hex =
        vtkSmartPointer<vtkHexahedron>::New();
for (int i = 0; i < numberOfVertices; ++i)
{
    hex->GetPointIds()->SetId(i, i);
}

// Add the points and hexahedron to an unstructured grid
vtkSmartPointer<vtkUnstructuredGrid> uGrid =
        vtkSmartPointer<vtkUnstructuredGrid>::New();
uGrid->SetPoints(points);
uGrid->InsertNextCell(hex->GetCellType(), hex->GetPointIds());

return uGrid;

```

}

int main(int, char\*[])  
{  
vtkNew root;

```
vtkSmartPointer<vtkUnstructuredGrid> grid = MakeHexahedron();
vtkSmartPointer<vtkDataSetSurfaceFilter> surfaceFilter = vtkSmartPointer<vtkDataSetSurfaceFilter>::New();
surfaceFilter->SetInputData(grid);
surfaceFilter->Update();
vtkPolyData* poly = surfaceFilter->GetOutput();

//root->SetBlock(0, poly);
root->SetBlock(0, grid);

vtkNew<vtkCompositePolyDataMapper2> mapper;
mapper->SetInputDataObject(root);
vtkNew<vtkCompositeDataDisplayAttributes> cdsa;
mapper->SetCompositeDataDisplayAttributes(cdsa);

mapper->SetBlockColor(0, 1, 0, 0);

vtkNew<vtkRenderer> aren;
vtkNew<vtkRenderWindow> renWin;
renWin->AddRenderer(aren);
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);

vtkNew<vtkActor> actor;
actor->SetMapper(mapper);

aren->AddActor(actor);
vtkNew<vtkNamedColors> colors;
aren->SetBackground(colors->GetColor3d("CornflowerBlue").GetData());

renWin->SetWindowName("MultiBLockDataSet");
renWin->Render();
iren->Start();
return 0;

```

}
