1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
#include "QtVTKTouchscreenRenderWindows.h"
#include "ui_QtVTKTouchscreenRenderWindows.h"
// Available interactions:
// - Tap: Randomizes background color and moves the sphere actor to the location of the tap point
// = Touchscreen: 1 finger
// - Tap and hold: Switches camera between perspective and orthographic view and moves the cylinder
// to the location of the tap point
// = Touchscreen and MacOS trackpad: 1 finger
// - Swipe: Changes the color of the Square/Sphere/Cylinder based on the swipe angle. Angle -> Hue
// = Touchscreen: 3 fingers
// - Pinch : Zoom in and out the view, centered on the location of the pinch
// = Touchscreen and MacOS trackpad: 2 fingers
// - Rotate: Rotate the view, centered on the location of the pinch
// = Touchscreen and MacOS trackpad: 2 fingers
// - Pan: Translate the view
// = Touchscreen: 2+ fingers
// = MacOS trackpad: Long tap and move
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCollectionIterator.h>
#include <vtkCubeSource.h>
#include <vtkCylinderSource.h>
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkInteractorStyleMultiTouchCamera.h>
#include <vtkPlane.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkTransform.h>
vtkNew<vtkActor> cubeActor;
vtkNew<vtkActor> sphereActor;
vtkNew<vtkActor> cylinderActor;
vtkNew<vtkSphereSource> sphereSource;
vtkNew<vtkCubeSource> cubeSource;
vtkNew<vtkCylinderSource> cylinderSource;
vtkNew<vtkTransform> sphereTransform;
vtkNew<vtkTransform> cubeTransform;
vtkNew<vtkTransform> cylinderTransform;
class vtkInteractorStyleMultiTouchCameraExample : public vtkInteractorStyleMultiTouchCamera
{
public:
static vtkInteractorStyleMultiTouchCameraExample* New();
vtkTypeMacro(vtkInteractorStyleMultiTouchCameraExample, vtkInteractorStyleMultiTouchCamera);
void GetPickPosition(double pickPosition[4])
{
vtkCamera* camera = this->CurrentRenderer->GetActiveCamera();
if (!camera)
{
return;
}
int pointer = this->Interactor->GetPointerIndex();
this->FindPokedRenderer(this->Interactor->GetEventPositions(pointer)[0],
this->Interactor->GetEventPositions(pointer)[1]);
double* focalPointWorld = camera->GetFocalPoint();
double focalPointDisplay[3] = { 0, 0, 0 };
vtkInteractorObserver::ComputeWorldToDisplay(this->CurrentRenderer, focalPointWorld[0],
focalPointWorld[1], focalPointWorld[2], focalPointDisplay);
// New position at the center of the pinch gesture
int* touchPositionDisplay = this->Interactor->GetEventPositions(pointer);
double pickPoint[4] = { 0, 0, 0, 0 };
vtkInteractorObserver::ComputeDisplayToWorld(this->CurrentRenderer, touchPositionDisplay[0],
touchPositionDisplay[1], focalPointDisplay[2], pickPosition);
}
void OnLongTap() override
{
if (!this->CurrentRenderer)
{
return;
}
vtkCamera* camera = this->CurrentRenderer->GetActiveCamera();
if (!camera)
{
return;
}
camera->SetParallelProjection(!camera->GetParallelProjection());
double pickPoint[4] = { 0, 0, 0, 0 };
this->GetPickPosition(pickPoint);
cylinderTransform->Identity();
cylinderTransform->Translate(pickPoint);
this->CurrentRenderer->Render();
}
void OnTap() override
{
if (!this->CurrentRenderer)
{
return;
}
this->CurrentRenderer->SetBackground(
(double)rand() / RAND_MAX, (double)rand() / RAND_MAX, (double)rand() / RAND_MAX);
double pickPoint[4] = { 0, 0, 0, 0 };
this->GetPickPosition(pickPoint);
sphereTransform->Identity();
sphereTransform->Translate(pickPoint);
this->CurrentRenderer->Render();
}
bool IsSwiping = false;
void OnStartSwipe() override
{
this->IsSwiping = true;
this->StartGesture();
}
void OnEndSwipe() override
{
this->IsSwiping = false;
this->EndGesture();
}
void OnSwipe() override
{
if (!this->CurrentRenderer)
{
return;
}
double hsv[3] = { this->Interactor->GetRotation() / 360.0, 1.0, 1.0 };
double rgb[3];
vtkMath::HSVToRGB(hsv, rgb);
cubeActor->GetProperty()->SetColor(rgb);
sphereActor->GetProperty()->SetColor(rgb);
cylinderActor->GetProperty()->SetColor(rgb);
this->CurrentRenderer->Render();
}
void OnPinch() override
{
if (this->IsSwiping)
{
return;
}
Superclass::OnPinch();
}
void OnRotate() override
{
if (this->IsSwiping)
{
return;
}
Superclass::OnRotate();
}
void OnPan() override
{
if (this->IsSwiping)
{
return;
}
Superclass::OnPan();
}
};
vtkStandardNewMacro(vtkInteractorStyleMultiTouchCameraExample);
//------------------------------------------------------------------------------
QtVTKTouchscreenRenderWindows::QtVTKTouchscreenRenderWindows(int vtkNotUsed(argc), char* argv[])
{
this->ui = new Ui_QtVTKTouchscreenRenderWindows;
this->ui->setupUi(this);
vtkSmartPointer<vtkGenericOpenGLRenderWindow> renderWindow =
vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
this->ui->view->setRenderWindow(renderWindow);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
this->ui->view->renderWindow()->AddRenderer(renderer);
vtkRenderWindowInteractor* interactor = this->ui->view->interactor();
vtkSmartPointer<vtkInteractorStyleMultiTouchCameraExample> interactorStyle =
vtkSmartPointer<vtkInteractorStyleMultiTouchCameraExample>::New();
interactor->SetInteractorStyle(interactorStyle);
renderWindow->SetInteractor(interactor);
// Create a cube.
cubeSource->SetXLength(0.5);
cubeSource->SetYLength(0.5);
cubeSource->SetZLength(0.5);
// Create a mapper and actor.
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(cubeSource->GetOutputPort());
cubeActor->SetMapper(mapper);
renderer->AddActor(cubeActor);
// Create a sphere.
sphereSource->SetRadius(0.125);
// Create a mapper and actor.
vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
sphereActor->SetMapper(sphereMapper);
sphereActor->SetUserTransform(sphereTransform);
renderer->AddActor(sphereActor);
// Create a cylinder.
cylinderSource->SetRadius(0.125);
cylinderSource->SetHeight(0.25);
// Create a mapper and actor.
vtkSmartPointer<vtkPolyDataMapper> cylinderMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
cylinderMapper->SetInputConnection(cylinderSource->GetOutputPort());
cylinderActor->SetMapper(cylinderMapper);
cylinderActor->SetUserTransform(cylinderTransform);
renderer->AddActor(cylinderActor);
renderer->SetBackground(0.1, 0.2, 0.4);
};
|