SampleFunction
Repository source: SampleFunction
Other languages
See (Python), (PythonicAPI), (Java), (CSharp)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
SampleFunction.cxx
#include <vtkActor.h>
#include <vtkContourFilter.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkOutlineFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSampleFunction.h>
#include <vtkSuperquadric.h>
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
vtkNew<vtkSuperquadric> implicitFunction;
implicitFunction->SetPhiRoundness(2.5);
implicitFunction->SetThetaRoundness(0.5);
// Sample the function.
vtkNew<vtkSampleFunction> sample;
sample->SetSampleDimensions(50, 50, 50);
sample->SetImplicitFunction(implicitFunction);
double value = 2.0;
double xmin = -value, xmax = value, ymin = -value, ymax = value,
zmin = -value, zmax = value;
sample->SetModelBounds(xmin, xmax, ymin, ymax, zmin, zmax);
// Create the 0 isosurface.
vtkNew<vtkContourFilter> contours;
contours->SetInputConnection(sample->GetOutputPort());
contours->GenerateValues(1, 2.0, 2.0);
// Map the contours to graphical primitives.
vtkNew<vtkPolyDataMapper> contourMapper;
contourMapper->SetInputConnection(contours->GetOutputPort());
contourMapper->SetScalarRange(0.0, 1.2);
// Create an actor for the contours.
vtkNew<vtkActor> contourActor;
contourActor->SetMapper(contourMapper);
// -- create a box around the function to indicate the sampling volume --
// Create outline.
vtkNew<vtkOutlineFilter> outline;
outline->SetInputConnection(sample->GetOutputPort());
// Map it to graphics primitives.
vtkNew<vtkPolyDataMapper> outlineMapper;
outlineMapper->SetInputConnection(outline->GetOutputPort());
// Create an actor for it.
vtkNew<vtkActor> outlineActor;
outlineActor->SetMapper(outlineMapper);
outlineActor->GetProperty()->SetColor(colors->GetColor3d("Black").GetData());
// Visualize
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("Samplefunction");
vtkNew<vtkRenderWindowInteractor> interactor;
interactor->SetRenderWindow(renderWindow);
renderer->AddActor(contourActor);
renderer->AddActor(outlineActor);
renderer->SetBackground(colors->GetColor3d("Tan").GetData());
renderWindow->Render();
interactor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(SampleFunction)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
FiltersCore
FiltersModeling
ImagingHybrid
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "SampleFunction: Unable to find the VTK build folder.")
endif()
# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(SampleFunction MACOSX_BUNDLE SampleFunction.cxx )
target_link_libraries(SampleFunction PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS SampleFunction
MODULES ${VTK_LIBRARIES}
)
Download and Build SampleFunction¶
Click here to download SampleFunction and its CMakeLists.txt file. Once the tarball SampleFunction.tar has been downloaded and extracted,
cd SampleFunction/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:
./SampleFunction
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.