VTK/Examples/Cxx/Visualization/RandomProbe

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Revision as of 16:12, 14 March 2013 by Lorensen (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
VTK Examples Baseline Visualization TestRandomProbe.png

RandomProbe.cxx

#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkSampleFunction.h>
#include <vtkSphereSource.h>
#include <vtkCone.h>
#include <vtkGlyph3D.h>
#include <vtkProbeFilter.h>
#include <vtkPointSource.h>
#include <vtkThreshold.h>
#include <vtkThresholdPoints.h>
#include <vtkImageData.h>
#include <vtkPointData.h>
 
#include <vtkDataSetMapper.h>
#include <vtkProperty.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkUnsignedCharArray.h>
 
int main(int argc, char *argv[])
{
  int resolution = 50;
  if (argc > 1)
    {
    resolution = atoi(argv[1]);
    }
 
  // Create a sampled cone
  vtkSmartPointer<vtkCone> implicitCone =
    vtkSmartPointer<vtkCone>::New();
  implicitCone->SetAngle(30.0);
 
  double radius = 1.0;
  vtkSmartPointer<vtkSampleFunction> sampledCone =
    vtkSmartPointer<vtkSampleFunction>::New();
  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);
 
  vtkSmartPointer<vtkThreshold> thresholdCone =
    vtkSmartPointer<vtkThreshold>:: New();
  thresholdCone->SetInputConnection(sampledCone->GetOutputPort());
  thresholdCone->ThresholdByLower(0);
 
  vtkSmartPointer<vtkPointSource> randomPoints =
    vtkSmartPointer<vtkPointSource>::New();
  randomPoints->SetCenter (0.0, 0.0, 0.0);
  randomPoints->SetNumberOfPoints(10000);
  randomPoints->SetDistributionToUniform();
  randomPoints->SetRadius(xMax);
 
  // Probe the cone dataset with random points
  vtkSmartPointer<vtkProbeFilter> randomProbe =
    vtkSmartPointer<vtkProbeFilter>::New();
  randomProbe->SetInputConnection(0, randomPoints->GetOutputPort());
  randomProbe->SetInputConnection(1, thresholdCone->GetOutputPort());
  randomProbe->Update();
  randomProbe->GetOutput()->GetPointData()->SetActiveScalars("vtkValidPointMask");
 
  vtkSmartPointer<vtkThresholdPoints> selectPoints =
    vtkSmartPointer<vtkThresholdPoints>::New();
  selectPoints->SetInputConnection(randomProbe->GetOutputPort());
  selectPoints->ThresholdByUpper(1.0);
 
  vtkSmartPointer<vtkSphereSource> sphere =
    vtkSmartPointer<vtkSphereSource>::New();
  sphere->SetRadius(.05);
 
  vtkSmartPointer<vtkGlyph3D> glyph =
    vtkSmartPointer<vtkGlyph3D>::New();
#if VTK_MAJOR_VERSION <= 5
  glyph->SetSource(sphere->GetOutput());
  glyph->SetInput(selectPoints->GetOutput());
#else
  glyph->SetSourceConnection(sphere->GetOutputPort());
  glyph->SetInputConnection(selectPoints->GetOutputPort());
#endif
 
  // Create a mapper and actor
  vtkSmartPointer<vtkDataSetMapper> mapper =
    vtkSmartPointer<vtkDataSetMapper>::New();
  mapper->SetInputConnection(glyph->GetOutputPort());
  mapper->ScalarVisibilityOff();
  vtkSmartPointer<vtkActor> actor =
    vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);
 
  // Visualize
  vtkSmartPointer<vtkRenderer> renderer = 
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow = 
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = 
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);
 
  renderer->AddActor(actor);
  renderer->SetBackground(.1, .5, .8);
 
  renderWindow->Render();
  renderWindowInteractor->Start();
 
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
 
PROJECT(RandomProbe)
 
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
 
add_executable(RandomProbe MACOSX_BUNDLE RandomProbe.cxx)
 
if(VTK_LIBRARIES)
  target_link_libraries(RandomProbe ${VTK_LIBRARIES})
else()
  target_link_libraries(RandomProbe vtkHybrid vtkWidgets)
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 PLEASE NOTE: Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.