Skip to content

SphereWidget2

Repository source: SphereWidget2


Description

The spherical coordinates of the widget's handle match the coordinates that it displays in the window. The world coordinates of the handle are also shown.

Question

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

Code

SphereWidget2.cxx

#include <vtkCommand.h>
#include <vtkMath.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphere.h>
#include <vtkSphereRepresentation.h>
#include <vtkSphereWidget2.h>

namespace {
// Callback that displays the sphere widget's spherical handle postion
// in both sphercial (relative to the widget's center) and cartesian global
// coordinates
class vtkSphereCallback : public vtkCommand
{
public:
  static vtkSphereCallback* New()
  {
    return new vtkSphereCallback;
  }

  virtual void Execute(vtkObject* caller, unsigned long, void*)
  {
    vtkSphereWidget2* sphereWidget =
        reinterpret_cast<vtkSphereWidget2*>(caller);

    double center[3], handlePosition[3];
    vtkSphereRepresentation* sphereRepresentation =
        dynamic_cast<vtkSphereRepresentation*>(
            sphereWidget->GetRepresentation());
    sphereRepresentation->GetHandlePosition(handlePosition);
    sphereRepresentation->GetSphere(this->Sphere);

    this->Sphere->GetCenter(center);

    double radius = sqrt(static_cast<double>(
        vtkMath::Distance2BetweenPoints(center, handlePosition)));
    radius = (radius <= 0.0 ? 1.0 : radius);
    double theta = vtkMath::DegreesFromRadians(atan2(
        (handlePosition[1] - center[1]), (handlePosition[0] - center[0])));
    double phi = vtkMath::DegreesFromRadians(
        acos((handlePosition[2] - center[2]) / radius));

    std::cout << "r, theta, phi: (" << std::setprecision(3) << radius << ", "
              << theta << ", " << phi << ") " << "x, y, z: ("
              << handlePosition[0] << ", " << handlePosition[1] << ", "
              << handlePosition[2] << ") " << std::endl;
  }

  vtkSphereCallback()
  {
    this->Sphere = vtkSphere::New();
  }
  ~vtkSphereCallback()
  {
    this->Sphere->Delete();
  }

  vtkSphere* Sphere;
};

} // namespace

int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;

  // Create a renderer and a render window
  vtkNew<vtkRenderer> renderer;
  renderer->SetBackground(colors->GetColor3d("MidnightBlue").GetData());

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("SphereWidget2");

  // Create an interactor
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Create a sphere widget
  vtkNew<vtkSphereWidget2> sphereWidget;
  sphereWidget->SetInteractor(renderWindowInteractor);
  sphereWidget->CreateDefaultRepresentation();

  vtkSphereRepresentation* sphereRepresentation =
      dynamic_cast<vtkSphereRepresentation*>(sphereWidget->GetRepresentation());
  sphereRepresentation->HandleVisibilityOn();

  vtkNew<vtkSphereCallback> sphereCallback;

  sphereWidget->AddObserver(vtkCommand::InteractionEvent, sphereCallback);

  renderWindow->Render();

  renderWindowInteractor->Initialize();
  renderWindow->Render();
  sphereWidget->On();

  // Begin mouse interaction
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(SphereWidget2)

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

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

Download and Build SphereWidget2

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

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

./SphereWidget2

WINDOWS USERS

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