vtk view settings

Problem Description

The vtk displays various views of the model, such as the front view, the back view, the left view, the right view, and so on.

analysis

We achieve each view by adjusting the camera position, focus position, and upward direction.

  • Camera location: that is, where the camera is located, set with method vtkCamera::SetPosition();
  • Camera focus: The point to which the camera is looking, set with method vtkCamera::SetFocusPoint();
  • Up: The camera is up.It's like when we look upright, head up, and what we see is upright. If we look upside down at something, then head down, of course, what we see is upside down.The actual orientation of the camera is determined by three factors, camera position, camera focus and upward orientation, that is, the view of the camera.

Place several models:

Views:

Realization

#include <vtkSmartPointer.h>
#include <vtkCameraActor.h>
#include <vtkNamedColors.h>
#include <vtkSphereSource.h>
#include <vtkConeSource.h>
#include <vtkCubeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkCamera.h>
#include <vtkMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>

void ViewDirection(vtkRenderer *renderer,
                   double lookX, double lookY, double lookZ,
                   double upX, double upY, double upZ)
{
    renderer->GetActiveCamera()->SetPosition(0, 0, 0);    //Camera position
    renderer->GetActiveCamera()->SetFocalPoint(lookX, lookY, lookZ);    //Focus position
    renderer->GetActiveCamera()->SetViewUp(upX, upY, upZ);    //Upward
    renderer->ResetCamera();
}

void ViewPositiveX(vtkRenderer *renderer)
{
    ViewDirection(renderer, 1, 0, 0, 0, 0, 1);
}

void ViewNegativeX(vtkRenderer *renderer)
{
    ViewDirection(renderer, -1, 0, 0, 0, 0, 1);
}

void ViewPositiveY(vtkRenderer *renderer)
{
    ViewDirection(renderer, 0, 1, 0, 0, 0, 1);
}

void ViewNegativeY(vtkRenderer *renderer)
{
    ViewDirection(renderer, 0, -1, 0, 0, 0, 1);
}

void ViewPositiveZ(vtkRenderer *renderer)
{
    ViewDirection(renderer, 0, 0, 1, 0, 1, 0);
}
void ViewNegativeZ(vtkRenderer *renderer)
{
    ViewDirection(renderer, 0, 0, -1, 0, 1, 0);
}

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

    // Place cube on X axis
    auto cubeSource = vtkSmartPointer<vtkCubeSource>::New();
    cubeSource->SetCenter(1000, 0, 0);
    cubeSource->SetXLength(1000);
    cubeSource->SetYLength(1000);
    cubeSource->SetZLength(1000);
    cubeSource->Update();

    auto cubeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    cubeMapper->SetInputConnection(cubeSource->GetOutputPort());
    auto cubeActor = vtkSmartPointer<vtkActor>::New();
    cubeActor->SetMapper(cubeMapper);
    cubeActor->GetProperty()->SetDiffuseColor(
        namedColors->GetColor3d("Tomato").GetData());

    // Y-Axis Placement Ball
    auto sphereSource = vtkSmartPointer<vtkSphereSource>::New();
    sphereSource->SetCenter(0, 1000, 0);
    sphereSource->SetRadius(500);
    sphereSource->SetThetaResolution(64);
    sphereSource->SetPhiResolution(64);
    sphereSource->Update();

    auto sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
    auto sphereActor = vtkSmartPointer<vtkActor>::New();
    sphereActor->SetMapper(sphereMapper);
    sphereActor->GetProperty()->SetDiffuseColor(
        namedColors->GetColor3d("Tomato").GetData());

    // Place cone on Z axis
    auto coneSource = vtkSmartPointer<vtkConeSource>::New();
    coneSource->SetCenter(0, 0, 1000);
    coneSource->SetRadius(500);
    coneSource->SetHeight(1000);
    coneSource->SetDirection(0, 0, 1);
    coneSource->SetResolution(128);
    coneSource->Update();

    auto coneMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    coneMapper->SetInputConnection(coneSource->GetOutputPort());
    auto coneActor = vtkSmartPointer<vtkActor>::New();
    coneActor->SetMapper(coneMapper);
    coneActor->GetProperty()->SetDiffuseColor(
        namedColors->GetColor3d("Tomato").GetData());

    // Visualize
    auto renderer = vtkSmartPointer<vtkRenderer>::New();
    auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->SetSize(500, 500);
    renderWindow->AddRenderer(renderer);

    auto renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderer->SetBackground(namedColors->GetColor3d("SlateGray").GetData());
    renderer->AddActor(cubeActor);
    renderer->AddActor(sphereActor);
    renderer->AddActor(coneActor);
    renderer->ResetCamera();

    //ViewPositiveX(renderer);
    //ViewNegativeX(renderer);
    //ViewPositiveY(renderer);
    //ViewNegativeY(renderer);
    //ViewPositiveZ(renderer);
    ViewNegativeZ(renderer);

    renderWindow->Render();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

Posted on Fri, 17 Jul 2020 10:34:39 -0400 by bluebyyou