# TextureCutSphere

VTKExamples/Cxx/Texture/TextureCutSphere

### Description

Sphere cut with transparent texture.

Info

See Figure 9-43b in Chapter 9 The VTK Textbook.

Other Languages

See (Python)

### Code

TextureCutSphere.cxx

    //
    // Cut an outer sphere to reveal an inner sphere.
    //
    #include <vtkActor.h>
    #include <vtkCamera.h>
    #include <vtkDataSetMapper.h>
    #include <vtkDoubleArray.h>
    #include <vtkImplicitTextureCoords.h>
    #include <vtkNamedColors.h>
    #include <vtkPlanes.h>
    #include <vtkPoints.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkProperty.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>
    #include <vtkSmartPointer.h>
    #include <vtkSphereSource.h>
    #include <vtkStructuredPointsReader.h>
    #include <vtkTexture.h>

    int main(int /* argc */, char* argv[])
    {
      vtkSmartPointer<vtkNamedColors> colors =
        vtkSmartPointer<vtkNamedColors>::New();

      // Hidden sphere.
      vtkSmartPointer<vtkSphereSource> sphere1 =
        vtkSmartPointer<vtkSphereSource>::New();
      sphere1->SetRadius(0.5);

      vtkSmartPointer<vtkPolyDataMapper> innerMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      innerMapper->SetInputConnection(sphere1->GetOutputPort());

      vtkSmartPointer<vtkActor> innerSphere = vtkSmartPointer<vtkActor>::New();
      innerSphere->SetMapper(innerMapper);
      innerSphere->GetProperty()->SetColor(
        colors->GetColor3d("BlanchedAlmond").GetData());

      // Sphere to texture.
      vtkSmartPointer<vtkSphereSource> sphere2 =
        vtkSmartPointer<vtkSphereSource>::New();
      sphere2->SetRadius(1.0);
      sphere2->SetPhiResolution(21);
      sphere2->SetThetaResolution(21);

      vtkSmartPointer<vtkPlanes> planes = vtkSmartPointer<vtkPlanes>::New();

      double pts[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
      vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
      points->SetNumberOfPoints(2);
      points->SetPoint(0, pts);
      points->SetPoint(1, pts + 3);

      double nrms[6] = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0};
      vtkSmartPointer<vtkDoubleArray> normals =
        vtkSmartPointer<vtkDoubleArray>::New();
      normals->SetNumberOfComponents(3);
      normals->SetNumberOfTuples(2);
      normals->SetTuple(0, nrms);
      normals->SetTuple(1, nrms + 3);

      planes->SetPoints(points);
      planes->SetNormals(normals);

      vtkSmartPointer<vtkImplicitTextureCoords> tcoords =
        vtkSmartPointer<vtkImplicitTextureCoords>::New();
      tcoords->SetInputConnection(sphere2->GetOutputPort());
      tcoords->SetRFunction(planes);

      vtkSmartPointer<vtkDataSetMapper> outerMapper =
        vtkSmartPointer<vtkDataSetMapper>::New();
      outerMapper->SetInputConnection(tcoords->GetOutputPort());

      vtkSmartPointer<vtkStructuredPointsReader> tmap =
        vtkSmartPointer<vtkStructuredPointsReader>::New();
      tmap->SetFileName(argv[1]);

      vtkSmartPointer<vtkTexture> texture = vtkSmartPointer<vtkTexture>::New();
      texture->SetInputConnection(tmap->GetOutputPort());
      texture->InterpolateOff();
      texture->RepeatOff();

      vtkSmartPointer<vtkActor> outerSphere = vtkSmartPointer<vtkActor>::New();
      outerSphere->SetMapper(outerMapper);
      outerSphere->SetTexture(texture);
      outerSphere->GetProperty()->SetColor(
        colors->GetColor3d("LightSalmon").GetData());

      vtkSmartPointer<vtkRenderWindow> renWin =
        vtkSmartPointer<vtkRenderWindow>::New();
      vtkSmartPointer<vtkRenderWindowInteractor> iren =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      vtkSmartPointer<vtkRenderer> aren = vtkSmartPointer<vtkRenderer>::New();
      iren->SetRenderWindow(renWin);
      renWin->AddRenderer(aren);

      aren->AddActor(innerSphere);
      aren->AddActor(outerSphere);
      aren->SetBackground(colors->GetColor3d("SlateGray").GetData());
      aren->GetActiveCamera()->Azimuth(-20);
      aren->GetActiveCamera()->Elevation(-10);
      aren->ResetCamera();

      renWin->SetSize(500, 500);

      // Interact with the data.
      renWin->Render();

      iren->Initialize();
      iren->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(TextureCutSphere)

    find_package(VTK COMPONENTS
      vtkCommonColor
      vtkCommonCore
      vtkCommonDataModel
      vtkFiltersSources
      vtkFiltersTexture
      vtkIOLegacy
      vtkInteractionStyle
      vtkRenderingCore
      vtkRenderingFreeType
      vtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping TextureCutSphere: ${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(TextureCutSphere MACOSX_BUNDLE TextureCutSphere.cxx )
      target_link_libraries(TextureCutSphere PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(TextureCutSphere MACOSX_BUNDLE TextureCutSphere.cxx )
      target_link_libraries(TextureCutSphere PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS TextureCutSphere
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build TextureCutSphere

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

    cd TextureCutSphere/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:

    ./TextureCutSphere

WINDOWS USERS

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