ProjectSphere
Repository source: ProjectSphere
Other languages
See (Java)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
ProjectSphere.cxx
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkElevationFilter.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkParametricFunctionSource.h>
#include <vtkParametricSuperEllipsoid.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProjectSphereFilter.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
vtkNew<vtkParametricSuperEllipsoid> surface;
surface->SetN1(2.0);
surface->SetN2(0.5);
vtkNew<vtkParametricFunctionSource> source;
source->SetParametricFunction(surface);
vtkNew<vtkElevationFilter> elevationFilter;
elevationFilter->SetInputConnection(source->GetOutputPort());
elevationFilter->SetLowPoint(0.0, 0.0, -4.0);
elevationFilter->SetHighPoint(0.0, 0.0, 4.0);
elevationFilter->Update();
// Deep copy the point data since in some versions of VTK,
// the ProjectSphereFilter modifies the input point data.
vtkNew<vtkPolyData> pd1;
pd1->DeepCopy(elevationFilter->GetOutput());
vtkNew<vtkProjectSphereFilter> sphereProject1;
sphereProject1->SetInputConnection(elevationFilter->GetOutputPort());
sphereProject1->Update();
vtkNew<vtkPolyDataMapper> mapper1;
mapper1->SetInputConnection(sphereProject1->GetOutputPort());
mapper1->SetScalarRange(
sphereProject1->GetOutput()->GetPointData()->GetScalars()->GetRange());
vtkNew<vtkActor> actor1;
actor1->SetMapper(mapper1);
vtkNew<vtkPolyDataMapper> mapper2;
mapper2->SetInputData(pd1);
mapper2->SetScalarRange(pd1->GetPointData()->GetScalars()->GetRange());
vtkNew<vtkActor> actor2;
actor2->SetMapper(mapper2);
// A render window.
vtkNew<vtkRenderWindow> renderWindow;
// Define viewport ranges.
// (xmin, ymin, xmax, ymax)
double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};
// Setup both renderers.
vtkNew<vtkRenderer> leftRenderer;
renderWindow->AddRenderer(leftRenderer);
leftRenderer->SetViewport(leftViewport);
leftRenderer->SetBackground(colors->GetColor3d("RosyBrown").GetData());
vtkNew<vtkRenderer> rightRenderer;
renderWindow->AddRenderer(rightRenderer);
rightRenderer->SetViewport(rightViewport);
rightRenderer->SetBackground(colors->GetColor3d("CadetBlue").GetData());
leftRenderer->AddActor(actor2);
rightRenderer->AddActor(actor1);
// An interactor.
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
leftRenderer->GetActiveCamera()->Azimuth(30);
leftRenderer->GetActiveCamera()->Elevation(-30);
leftRenderer->ResetCamera();
// Render an image (lights and cameras are created automatically).
renderWindow->SetSize(640, 480);
renderWindow->SetWindowName("ProjectSphere");
renderWindow->Render();
// Begin mouse interaction.
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(ProjectSphere)
find_package(VTK COMPONENTS
CommonColor
CommonComputationalGeometry
CommonCore
CommonDataModel
FiltersCore
FiltersGeometry
FiltersSources
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "ProjectSphere: 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(ProjectSphere MACOSX_BUNDLE ProjectSphere.cxx )
target_link_libraries(ProjectSphere PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ProjectSphere
MODULES ${VTK_LIBRARIES}
)
Download and Build ProjectSphere¶
Click here to download ProjectSphere and its CMakeLists.txt file. Once the tarball ProjectSphere.tar has been downloaded and extracted,
cd ProjectSphere/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:
./ProjectSphere
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.