Skip to content

RandomProbe

vtk-examples/Cxx/Visualization/RandomProbe


Other languages

See (Java)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

RandomProbe.cxx

#include <vtkActor.h>
#include <vtkCone.h>
#include <vtkDataSetMapper.h>
#include <vtkGlyph3D.h>
#include <vtkImageData.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPointSource.h>
#include <vtkProbeFilter.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSampleFunction.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkThreshold.h>
#include <vtkThresholdPoints.h>
#include <vtkUnsignedCharArray.h>

int main(int argc, char* argv[])
{
  int resolution = 50;
  if (argc > 1)
  {
    resolution = atoi(argv[1]);
  }

  vtkNew<vtkNamedColors> colors;

  // Create a sampled cone
  vtkNew<vtkCone> implicitCone;
  implicitCone->SetAngle(30.0);

  double radius = 1.0;
  vtkNew<vtkSampleFunction> sampledCone;
  sampledCone->SetSampleDimensions(resolution, resolution, resolution);
  double xMin = -radius * 2.0;
  double xMax = radius * 2.0;
  sampledCone->SetModelBounds(xMin, xMax, xMin, xMax, xMin, xMax);
  sampledCone->SetImplicitFunction(implicitCone);

  vtkNew<vtkThreshold> thresholdCone;
  thresholdCone->SetInputConnection(sampledCone->GetOutputPort());
  thresholdCone->ThresholdByLower(0);

  vtkNew<vtkPointSource> randomPoints;
  randomPoints->SetCenter(0.0, 0.0, 0.0);
  randomPoints->SetNumberOfPoints(10000);
  randomPoints->SetDistributionToUniform();
  randomPoints->SetRadius(xMax);

  // Probe the cone dataset with random points
  vtkNew<vtkProbeFilter> randomProbe;
  randomProbe->SetInputConnection(0, randomPoints->GetOutputPort());
  randomProbe->SetInputConnection(1, thresholdCone->GetOutputPort());
  randomProbe->Update();
  randomProbe->GetOutput()->GetPointData()->SetActiveScalars(
      "vtkValidPointMask");

  vtkNew<vtkThresholdPoints> selectPoints;
  selectPoints->SetInputConnection(randomProbe->GetOutputPort());
  selectPoints->ThresholdByUpper(1.0);

  vtkNew<vtkSphereSource> sphere;
  sphere->SetRadius(0.025);

  vtkNew<vtkGlyph3D> glyph;
  glyph->SetSourceConnection(sphere->GetOutputPort());
  glyph->SetInputConnection(selectPoints->GetOutputPort());

  // Create a mapper and actor
  vtkNew<vtkDataSetMapper> mapper;
  mapper->SetInputConnection(glyph->GetOutputPort());
  mapper->ScalarVisibilityOff();
  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());

  // Visualize
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("RandomProbe");

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("RoyalBlue").GetData());

  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(RandomProbe)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersCore
  vtkFiltersSources
  vtkImagingHybrid
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping RandomProbe: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(RandomProbe MACOSX_BUNDLE RandomProbe.cxx )
  target_link_libraries(RandomProbe PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(RandomProbe MACOSX_BUNDLE RandomProbe.cxx )
  target_link_libraries(RandomProbe PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS RandomProbe
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build RandomProbe

Click here to download RandomProbe and its CMakeLists.txt file. Once the tarball RandomProbe.tar has been downloaded and extracted,

cd RandomProbe/build

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./RandomProbe

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.