Skip to content

EllipticalButton

vtk-examples/Cxx/Interaction/EllipticalButton


Description

This example creates a 3D button using vtkEllipsoidalButton. Interaction is done by assigning the button to a vtkProp3DButtonRepresentation. Then a callback for a vtkButtonWidget controls the color of the button and the displayed geometry. Click on the button to see the color of both the button and geometry change.

Warning

There is a bug in vtkEllipticalButtonSource. If CircumferentialResolution is odd, improper geometry is created.

Warning

Values close to 1.0 for RadialRatio can cause shading artifacts at the corners of the button.

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

EllipticalButton.cxx

#include <vtkActor.h>
#include <vtkButtonWidget.h>
#include <vtkCommand.h>
#include <vtkEllipticalButtonSource.h>
#include <vtkImageData.h>
#include <vtkImageReader2.h>
#include <vtkImageReader2Factory.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProp3DButtonRepresentation.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkSuperquadricSource.h>
#include <vtkTexture.h>

// Callback for the interaction.
namespace {

class vtkButtonCallback : public vtkCommand
{
public:
  static vtkButtonCallback* New()
  {
    return new vtkButtonCallback;
  }

  virtual void Execute(vtkObject* caller, unsigned long, void*)
  {
    auto buttonWidget = reinterpret_cast<vtkButtonWidget*>(caller);
    auto rep = reinterpret_cast<vtkProp3DButtonRepresentation*>(
        buttonWidget->GetRepresentation());
    int state = rep->GetState();
    this->Actor->GetProperty()->SetColor(
        reinterpret_cast<vtkActor*>(rep->GetButtonProp(state))
            ->GetProperty()
            ->GetColor());
    std::cout << "State: " << state << "\n";
  }

  vtkButtonCallback() : Actor(0)
  {
  }

  vtkActor* Actor;
};

vtkSmartPointer<vtkActor> CreateButtonActor(const char* textureFile);

} // namespace

int main(int argc, char* argv[])
{
  vtkNew<vtkNamedColors> color;

  if (argc < 2)
  {
    std::cout << "Usage: " << argv[0] << " imageFilename e.g. YinYang.png"
              << std::endl;
    return EXIT_FAILURE;
  }

  vtkNew<vtkSuperquadricSource> source;
  source->SetPhiResolution(64);
  source->SetThetaResolution(64);
  source->SetThetaRoundness(1.5);
  source->SetThickness(1.5);
  source->SetSize(2);
  source->Update();

  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(source->GetOutputPort());

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);

  vtkNew<vtkRenderer> renderer;
  renderer->AddActor(actor);
  renderer->SetBackground(color->GetColor3d("Burlywood").GetData());

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->SetSize(200, 200);
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("EllipticalButton");

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Create the widget and its representation
  vtkSmartPointer<vtkActor> buttonActor1 = CreateButtonActor(argv[1]);
  buttonActor1->GetProperty()->SetColor(color->GetColor3d("Tomato").GetData());
  vtkSmartPointer<vtkActor> buttonActor2 = CreateButtonActor(argv[1]);
  buttonActor2->GetProperty()->SetColor(color->GetColor3d("Banana").GetData());

  vtkNew<vtkProp3DButtonRepresentation> buttonRepresentation;
  buttonRepresentation->SetNumberOfStates(2);
  buttonRepresentation->SetButtonProp(0, buttonActor1);
  buttonRepresentation->SetButtonProp(1, buttonActor2);
  buttonRepresentation->FollowCameraOn();

  vtkNew<vtkButtonCallback> callback;
  callback->Actor = actor;
  actor->GetProperty()->SetColor(
      reinterpret_cast<vtkActor*>(buttonRepresentation->GetButtonProp(0))
          ->GetProperty()
          ->GetColor());

  renderWindowInteractor->Initialize();

  vtkNew<vtkButtonWidget> buttonWidget;
  buttonWidget->SetInteractor(renderWindowInteractor);
  buttonWidget->SetRepresentation(buttonRepresentation);
  buttonWidget->AddObserver(vtkCommand::StateChangedEvent, callback);
  buttonWidget->SetEnabled(1);

  renderer->ResetCamera();
  renderer->Render();

  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

namespace {

vtkSmartPointer<vtkActor> CreateButtonActor(const char* textureFile)
{
  vtkNew<vtkImageReader2Factory> readerFactory;
  vtkSmartPointer<vtkImageReader2> imageReader;
  imageReader.TakeReference(readerFactory->CreateImageReader2(textureFile));
  imageReader->SetFileName(textureFile);
  imageReader->Update();

  // Aspect ratio of image
  int dims[3];
  imageReader->GetOutput()->GetDimensions(dims);
  double aspect = static_cast<double>(dims[0]) / static_cast<double>(dims[1]);

  vtkNew<vtkTexture> texture;
  texture->SetInputConnection(imageReader->GetOutputPort());

  vtkNew<vtkEllipticalButtonSource> ellipticalButtonSource;
  ellipticalButtonSource->SetCircumferentialResolution(50);
  ellipticalButtonSource->SetShoulderResolution(10);
  ellipticalButtonSource->SetTextureResolution(10);
  ellipticalButtonSource->SetRadialRatio(1.05);
  ellipticalButtonSource->SetShoulderTextureCoordinate(0.0, 0.0);
  ellipticalButtonSource->SetTextureDimensions(dims[0], dims[1]);
  ellipticalButtonSource->SetTextureStyleToProportional();
  ellipticalButtonSource->TwoSidedOn();
  ellipticalButtonSource->SetWidth(aspect);
  ellipticalButtonSource->SetHeight(1.0);
  ellipticalButtonSource->SetDepth(.15);
  ellipticalButtonSource->SetCenter(2, 2, 0);

  ellipticalButtonSource->SetOutputPointsPrecision(
      vtkAlgorithm::SINGLE_PRECISION);

  vtkNew<vtkPolyDataMapper> buttonMapper;
  buttonMapper->SetInputConnection(ellipticalButtonSource->GetOutputPort());

  vtkNew<vtkActor> buttonActor;
  buttonActor->SetMapper(buttonMapper);
  buttonActor->SetTexture(texture);

  return buttonActor;
}

} // namespace

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(EllipticalButton)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  FiltersSources
  IOImage
  InteractionStyle
  InteractionWidgets
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "EllipticalButton: 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(EllipticalButton MACOSX_BUNDLE EllipticalButton.cxx )
  target_link_libraries(EllipticalButton PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS EllipticalButton
  MODULES ${VTK_LIBRARIES}
)

Download and Build EllipticalButton

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

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

./EllipticalButton

WINDOWS USERS

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