Skip to content


# Mace

VTKEx/Cxx/Rendering/Mace

### Description

An example of multiple inputs and outputs.

Info

See Figure 4-21 in Chapter 4 the VTK Textbook.

Other languages

See (Python), (Java)

Question

If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

### Code

Mace.cxx

    #include <vtkConeSource.h>
    #include <vtkGlyph3D.h>
    #include <vtkNamedColors.h>
    #include <vtkPolyData.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>
    #include <vtkSphereSource.h>

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

      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();

      vtkSmartPointer<vtkRenderWindow> renWin =
        vtkSmartPointer<vtkRenderWindow>::New();
      renWin->AddRenderer(renderer);

      vtkSmartPointer<vtkRenderWindowInteractor> iren =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      iren->SetRenderWindow(renWin);

      vtkSmartPointer<vtkSphereSource> sphere =
        vtkSmartPointer<vtkSphereSource>::New();
      sphere->SetThetaResolution(8); sphere->SetPhiResolution(8);

      vtkSmartPointer<vtkPolyDataMapper> sphereMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      sphereMapper->SetInputConnection(sphere->GetOutputPort());

      vtkSmartPointer<vtkActor> sphereActor =
        vtkSmartPointer<vtkActor>::New();
      sphereActor->SetMapper(sphereMapper);

      vtkSmartPointer<vtkConeSource> cone =
        vtkSmartPointer<vtkConeSource>::New();
      cone->SetResolution(6);

      vtkSmartPointer<vtkGlyph3D> glyph =
        vtkSmartPointer<vtkGlyph3D>::New();
      glyph->SetInputConnection(sphere->GetOutputPort());
      glyph->SetSourceConnection(cone->GetOutputPort());
      glyph->SetVectorModeToUseNormal();
      glyph->SetScaleModeToScaleByVector();
      glyph->SetScaleFactor(0.25);

      vtkSmartPointer<vtkPolyDataMapper> spikeMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      spikeMapper->SetInputConnection(glyph->GetOutputPort());

      vtkSmartPointer<vtkActor> spikeActor =
        vtkSmartPointer<vtkActor>::New();
      spikeActor->SetMapper(spikeMapper);

      renderer->AddActor(sphereActor);
      renderer->AddActor(spikeActor);
      renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
      renWin->SetSize(640, 480);

      // interact with data
      renWin->Render();

      iren->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(Mace)

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

### Download and Build Mace

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

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

    ./Mace

WINDOWS USERS

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