Skip to content

Instantly share code, notes, and snippets.

@agirault
Created September 4, 2017 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agirault/83d100c22a7f9e9c567e39b2e6935d8a to your computer and use it in GitHub Desktop.
Save agirault/83d100c22a7f9e9c567e39b2e6935d8a to your computer and use it in GitHub Desktop.
auto picker = vtkSmartPointer<vtkPropPicker>::New();
picker->Pick(screen_x, screen_y, 0, renderer);
// Check if pick is in bounds
vtkAssemblyPath* path = picker->GetPath();
bool validPick = false;
if (path) {
vtkCollectionSimpleIterator sit;
path->InitTraversal(sit);
vtkAssemblyNode *node;
for (int i = 0; i < path->GetNumberOfItems() && !validPick; ++i) {
node = path->GetNextNode(sit);
if (vtkImageSlice::SafeDownCast(node->GetViewProp()) == labels_actor) {
validPick = true;
break;
}
}
}
// Out of bounds
if (!validPick) {
return -1;
}
// Convert from world coordinates to image coordinates
double p[3] = { 0. };
picker->GetPickPosition(p);
double origin[3] = { 0. };
labels_image->GetOrigin(origin);
double spacing[3] = { 0. };
labels_image->GetSpacing(spacing);
int x = (p[0] - origin[0])/spacing[0];
int y = (p[1] - origin[1])/spacing[1];
int z = (p[2] - origin[2])/spacing[2];
// Retrieving scalar value
return labels_image->GetScalarComponentAsFloat(x, y, z, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment