# picking 1 cube out of many in an actor

**URL:** https://discourse.vtk.org/t/picking-1-cube-out-of-many-in-an-actor/5470
**Category:** Development
**Created:** [March 30, 2021, 10:22pm UTC](https://discourse.vtk.org/t/picking-1-cube-out-of-many-in-an-actor/5470 "2021-03-30T22:22:21Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![unclejunior](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/u/ee59a6/32.png) [@unclejunior](https://discourse.vtk.org/u/unclejunior)
#### Post date: [March 30, 2021, 10:22pm UTC](https://discourse.vtk.org/t/picking-1-cube-out-of-many-in-an-actor/5470/1 "2021-03-30T22:22:21Z")

</div>

Hi,

I have 1 actor with 100s of cubes (just a starter project). I create the cubes like this:

```auto
  // create points
  int bias=0;
  for (const QRect & rect : layer->rects()) {
	QRectF r = rect;
	points->InsertNextPoint(r.left(), r.bottom(), layer->z());
	points->InsertNextPoint(r.right(), r.bottom(), layer->z());
	points->InsertNextPoint(r.right(), r.top(), layer->z());
	points->InsertNextPoint(r.left(), r.top(), layer->z());

	// Cell Array
	polys->InsertNextCell({bias+0,bias+1,bias+2,bias+3});
	bias += 4;
  }

  // create polydata
  auto polydata = vtkSmartPointer<vtkPolyData>::New();
  polydata->SetPoints(points);
  polydata->SetPolys(polys);

  // Linear extrusion
  auto extrude = vtkSmartPointer<vtkLinearExtrusionFilter>::New();
  extrude->SetInputData(polydata);
  extrude->SetVector(0.0, 0.0, layer->layerThick()+grow);

  // Mapper
  layer->mapper()->SetInputConnection(extrude->GetOutputPort());
  layer->mapper()->SetColorModeToDefault();

  // Actor
  layer->actor()->SetMapper(layer->mapper());
  layer->actor()->GetProperty()->SetColor(layer->color().r(), layer->color().g(), layer->color().b());
```

I implemented a Cell Picker like this: ( Copied from the example: [https://lorensen.github.io/VTKExamples/site/Cxx/Picking/CellPicking/](https://lorensen.github.io/VTKExamples/site/Cxx/Picking/CellPicking/) )

```auto
  auto picker = vtkSmartPointer<vtkCellPicker>::New();
  if (!picker) { return; }

  int success = picker->Pick(_rbEndPt[0], _rbEndPt[1], 5000., CurrentRenderer);
  if (!success) {
    this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->RemoveActor(_selectedActor);
    return;
  }

  double* worldPosition = picker->GetPickPosition();
  vtkSmartPointer<vtkNamedColors> colors = vtkSmartPointer<vtkNamedColors>::New();

  qDebug() << "Cell id is: " << picker->GetCellId();
  if (picker->GetCellId() != -1)
  {
    qDebug() << "Pick position is: " << worldPosition[0] << " " << worldPosition[1] << " " << worldPosition[2];

    vtkSmartPointer<vtkIdTypeArray> ids = vtkSmartPointer<vtkIdTypeArray>::New();
    ids->SetNumberOfComponents(1);
    ids->InsertNextValue(picker->GetCellId());

    vtkSmartPointer<vtkSelectionNode> selectionNode = vtkSmartPointer<vtkSelectionNode>::New();
    selectionNode->SetFieldType(vtkSelectionNode::CELL);
    selectionNode->SetContentType(vtkSelectionNode::INDICES);
    selectionNode->SetSelectionList(ids);

    vtkSmartPointer<vtkSelection> selection = vtkSmartPointer<vtkSelection>::New();
    selection->AddNode(selectionNode);

    vtkSmartPointer<vtkExtractSelection> extractSelection = vtkSmartPointer<vtkExtractSelection>::New();
    extractSelection->SetInputData(0, picker->GetActor()->GetMapper()->GetInput());
    extractSelection->SetInputData(1, selection);
    extractSelection->Update();

    // In selection
    vtkSmartPointer<vtkUnstructuredGrid> selected = vtkSmartPointer<vtkUnstructuredGrid>::New();
    selected->ShallowCopy(extractSelection->GetOutput());

    qDebug() << "There are " << selected->GetNumberOfPoints() << " points in the selection.";
    qDebug() << "There are " << selected->GetNumberOfCells() << " cells in the selection.";

    _selectedMapper->SetInputData(selected);
    _selectedActor->SetMapper(_selectedMapper);
    _selectedActor->GetProperty()->EdgeVisibilityOn();
    _selectedActor->GetProperty()->SetColor(colors->GetColor3d("Red").GetData());

    _selectedActor->GetProperty()->SetLineWidth(3);

    this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(_selectedActor);
  }
```

I was expecting the selected cell to have 8 points (4 for the bottom square, 4 for the top square), but  
I get a 4 point plane of one of the sides, or the top of the cube instead.

Is there a way I can get all the points of the selected cube?

Thanks,  
UJ
