File: vtkOpenVRRenderWindowInteractor.cxx

package info (click to toggle)
vtk7 7.1.1%2Bdfsg2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 127,396 kB
  • sloc: cpp: 1,539,584; ansic: 124,382; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 126; objc: 83
file content (393 lines) | stat: -rw-r--r-- 12,168 bytes parent folder | download | duplicates (2)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkOpenVRRenderWindowInteractor.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

#include "vtkOpenVRRenderWindowInteractor.h"
#include "vtkOpenVRRenderWindow.h"
#include "vtkRendererCollection.h"


#include "vtkActor.h"
#include "vtkObjectFactory.h"
#include "vtkCommand.h"
#include "vtkOpenVRCamera.h"
#include "vtkNew.h"
#include "vtkInteractorStyle3D.h"
#include "vtkPropPicker3D.h"

vtkStandardNewMacro(vtkOpenVRRenderWindowInteractor);

void (*vtkOpenVRRenderWindowInteractor::ClassExitMethod)(void *) = (void (*)(void *))NULL;
void *vtkOpenVRRenderWindowInteractor::ClassExitMethodArg = (void *)NULL;
void (*vtkOpenVRRenderWindowInteractor::ClassExitMethodArgDelete)(void *) = (void (*)(void *))NULL;

//----------------------------------------------------------------------------
// Construct object so that light follows camera motion.
vtkOpenVRRenderWindowInteractor::vtkOpenVRRenderWindowInteractor()
{
}

//----------------------------------------------------------------------------
vtkOpenVRRenderWindowInteractor::~vtkOpenVRRenderWindowInteractor()
{
}

void vtkOpenVRRenderWindowInteractor::SetPhysicalTranslation(vtkCamera *camin, double t1, double t2, double t3)
{
  vtkOpenVRCamera *cam =
    static_cast<vtkOpenVRCamera *>(camin);
  cam->SetTranslation(t1,t2,t3);
}

double *vtkOpenVRRenderWindowInteractor::GetPhysicalTranslation(vtkCamera *camin)
{
  vtkOpenVRCamera *cam =
    static_cast<vtkOpenVRCamera *>(camin);
  return cam->GetTranslation();
}

void vtkOpenVRRenderWindowInteractor::ConvertPoseToWorldCoordinates(
  vtkRenderer *ren,
  vr::TrackedDevicePose_t &tdPose,
  double pos[3],
  double wxyz[4])
{
  // get the position and orientation of the button press
  for (int i = 0; i < 3; i++)
  {
    pos[i] = tdPose.mDeviceToAbsoluteTracking.m[i][3];
  }

  vtkOpenVRCamera *cam =
    static_cast<vtkOpenVRCamera *>(ren->GetActiveCamera());
  double distance = cam->GetDistance();
  double *trans = cam->GetTranslation();

  for (int i = 0; i < 3; i++)
  {
    pos[i] = pos[i]*distance - trans[i];
  }

  double ortho[3][3];
  for (int i = 0; i < 3; i++)
  {
    ortho[0][i] = tdPose.mDeviceToAbsoluteTracking.m[0][i];
    ortho[1][i] = tdPose.mDeviceToAbsoluteTracking.m[1][i];
    ortho[2][i] = tdPose.mDeviceToAbsoluteTracking.m[2][i];
  }
  if (vtkMath::Determinant3x3(ortho) < 0)
  {
    ortho[0][2] = -ortho[0][2];
    ortho[1][2] = -ortho[1][2];
    ortho[2][2] = -ortho[2][2];
  }
  vtkMath::Matrix3x3ToQuaternion(ortho, wxyz);

  // calc the return value wxyz
 double mag = sqrt( wxyz[1] * wxyz[1] + wxyz[2] * wxyz[2] + wxyz[3] * wxyz[3] );

  if ( mag != 0.0 )
  {
    wxyz[0] = 2.0 * vtkMath::DegreesFromRadians( atan2( mag, wxyz[0] ) );
    wxyz[1] /= mag;
    wxyz[2] /= mag;
    wxyz[3] /= mag;
  }
  else
  {
    wxyz[0] = 0.0;
    wxyz[1] = 0.0;
    wxyz[2] = 0.0;
    wxyz[3] = 1.0;
  }
}

void vtkOpenVRRenderWindowInteractor::UpdateTouchPadPosition(
  vr::IVRSystem *pHMD,
   vr::TrackedDeviceIndex_t tdi)
{
  vr::VRControllerState_t cstate;
  pHMD->GetControllerState(tdi,&cstate);

  for (unsigned int i = 0; i < vr::k_unControllerStateAxisCount; i++)
  {
    if (pHMD->GetInt32TrackedDeviceProperty(tdi,
      static_cast<vr::ETrackedDeviceProperty>(vr::ETrackedDeviceProperty::Prop_Axis0Type_Int32 + i))
      == vr::EVRControllerAxisType::k_eControllerAxis_TrackPad)
    {
      this->SetTouchPadPosition(cstate.rAxis[i].x,cstate.rAxis[i].y);
    }
  }
}

//----------------------------------------------------------------------------
void  vtkOpenVRRenderWindowInteractor::StartEventLoop()
{
  this->StartedMessageLoop = 1;
  this->Done = false;

  vtkOpenVRRenderWindow *renWin =
    vtkOpenVRRenderWindow::SafeDownCast(this->RenderWindow);

  vtkCollectionSimpleIterator rit;
  renWin->GetRenderers()->InitTraversal(rit);
  vtkRenderer *ren = renWin->GetRenderers()->GetNextRenderer(rit);

  while (!this->Done)
  {
    this->DoOneEvent(renWin, ren);
  }
}

void vtkOpenVRRenderWindowInteractor::DoOneEvent(vtkOpenVRRenderWindow *renWin, vtkRenderer *ren)
{
  vr::IVRSystem *pHMD = renWin->GetHMD();

  if (!pHMD)
  {
    return;
  }

  vr::VREvent_t event;

  bool result =
    pHMD->PollNextEvent(
      &event,
      sizeof(vr::VREvent_t));

  if (result)
  {
    vr::TrackedDeviceIndex_t tdi = event.trackedDeviceIndex;

    // is it a controller button action?
    if (pHMD->GetTrackedDeviceClass(tdi) ==
        vr::ETrackedDeviceClass::TrackedDeviceClass_Controller &&
          (event.eventType == vr::VREvent_ButtonPress ||
           event.eventType == vr::VREvent_ButtonUnpress))
    {
      vr::ETrackedControllerRole role = pHMD->GetControllerRoleForTrackedDeviceIndex(tdi);

      this->UpdateTouchPadPosition(pHMD,tdi);

      // 0 = right hand 1 = left
      int pointerIndex =
        (role == vr::ETrackedControllerRole::TrackedControllerRole_RightHand ? 0 : 1);
      this->PointerIndexLookup[pointerIndex] = tdi;

      vr::TrackedDevicePose_t &tdPose = renWin->GetTrackedDevicePose(tdi);
      double pos[3];
      double wxyz[4];
      this->ConvertPoseToWorldCoordinates(ren, tdPose, pos, wxyz);

      // so even though we have world coordinates we have to convert them to
      // screen coordinates because all of VTKs picking code is currently
      // based on screen coordinates
      ren->SetWorldPoint(pos[0],pos[1],pos[2],1.0);
      ren->WorldToDisplay();
      double *displayCoords = ren->GetDisplayPoint();

      this->SetEventPosition(displayCoords[0],displayCoords[1],pointerIndex);
      this->SetWorldEventPosition(pos[0],pos[1],pos[2],pointerIndex);
      this->SetPhysicalEventPosition(
        tdPose.mDeviceToAbsoluteTracking.m[0][3],
        tdPose.mDeviceToAbsoluteTracking.m[1][3],
        tdPose.mDeviceToAbsoluteTracking.m[2][3],
        pointerIndex);
      this->SetWorldEventOrientation(wxyz[0],wxyz[1],wxyz[2],wxyz[3],pointerIndex);
      this->SetPointerIndex(pointerIndex);

      if (event.eventType == vr::VREvent_ButtonPress)
      {
        if (event.data.controller.button == vr::EVRButtonId::k_EButton_Axis1)
        {
          this->LeftButtonPressEvent();
        }
        if (event.data.controller.button == vr::EVRButtonId::k_EButton_Axis0)
        {
          this->RightButtonPressEvent();
        }
        if (event.data.controller.button == vr::EVRButtonId::k_EButton_ApplicationMenu)
        {
          this->Done = true;
        }
      }
      if (event.eventType == vr::VREvent_ButtonUnpress)
      {
        if (event.data.controller.button == vr::EVRButtonId::k_EButton_Axis1)
        {
          this->LeftButtonReleaseEvent();
        }
        if (event.data.controller.button == vr::EVRButtonId::k_EButton_Axis0)
        {
          this->RightButtonReleaseEvent();
        }
      }
    }
  }
  else
  {
    // if pointers are down track the movement
    if (this->PointersDownCount)
    {
      for (int i = 0; i < VTKI_MAX_POINTERS; i++)
      {
        if (this->PointersDown[i])
        {
          this->UpdateTouchPadPosition(pHMD,
           static_cast<vr::TrackedDeviceIndex_t>(this->PointerIndexLookup[i]));
          vr::TrackedDevicePose_t &tdPose =
            renWin->GetTrackedDevicePose(
            static_cast<vr::TrackedDeviceIndex_t>(this->PointerIndexLookup[i]));
          double pos[3];
          double wxyz[4];
          this->ConvertPoseToWorldCoordinates(ren, tdPose, pos, wxyz);

          // so even though we have world coordinates we have to convert them to
          // screen coordinates because all of VTKs picking code is currently
          // based on screen coordinates
          ren->SetWorldPoint(pos[0],pos[1],pos[2],1.0);
          ren->WorldToDisplay();
          double *displayCoords = ren->GetDisplayPoint();
          this->SetEventPosition(displayCoords[0], displayCoords[1], i);
          this->SetWorldEventPosition(pos[0],pos[1],pos[2],i);
          this->SetWorldEventOrientation(wxyz[0],wxyz[1],wxyz[2],wxyz[3],i);
          this->SetPhysicalEventPosition(
            tdPose.mDeviceToAbsoluteTracking.m[0][3],
            tdPose.mDeviceToAbsoluteTracking.m[1][3],
            tdPose.mDeviceToAbsoluteTracking.m[2][3],
            i);
        }
      }
      this->MouseMoveEvent();
    }
  }
  renWin->Render();
}


//----------------------------------------------------------------------------
void vtkOpenVRRenderWindowInteractor::Initialize()
{
  // make sure we have a RenderWindow and camera
  if ( ! this->RenderWindow)
  {
    vtkErrorMacro(<<"No renderer defined!");
    return;
  }
  if (this->Initialized)
  {
    return;
  }

  vtkOpenVRRenderWindow *ren =
    vtkOpenVRRenderWindow::SafeDownCast(this->RenderWindow);
  int *size;

  this->Initialized = 1;
  // get the info we need from the RenderingWindow

  size = ren->GetSize();
  ren->GetPosition();
  this->Enable();
  this->Size[0] = size[0];
  this->Size[1] = size[1];
}

//----------------------------------------------------------------------------
void vtkOpenVRRenderWindowInteractor::TerminateApp(void)
{
  this->Done = true;
}

//----------------------------------------------------------------------------
int vtkOpenVRRenderWindowInteractor::InternalCreateTimer(
  int vtkNotUsed(timerId),
  int vtkNotUsed(timerType),
  unsigned long vtkNotUsed(duration))
{
  // todo
  return 0;
}

//----------------------------------------------------------------------------
int vtkOpenVRRenderWindowInteractor::InternalDestroyTimer(
  int vtkNotUsed(platformTimerId))
{
  // todo
  return 0;
}


//----------------------------------------------------------------------------
// Specify the default function to be called when an interactor needs to exit.
// This callback is overridden by an instance ExitMethod that is defined.
void
vtkOpenVRRenderWindowInteractor::SetClassExitMethod(void (*f)(void *),void *arg)
{
  if ( f != vtkOpenVRRenderWindowInteractor::ClassExitMethod
       || arg != vtkOpenVRRenderWindowInteractor::ClassExitMethodArg)
  {
    // delete the current arg if there is a delete method
    if ((vtkOpenVRRenderWindowInteractor::ClassExitMethodArg)
        && (vtkOpenVRRenderWindowInteractor::ClassExitMethodArgDelete))
    {
      (*vtkOpenVRRenderWindowInteractor::ClassExitMethodArgDelete)
        (vtkOpenVRRenderWindowInteractor::ClassExitMethodArg);
    }
    vtkOpenVRRenderWindowInteractor::ClassExitMethod = f;
    vtkOpenVRRenderWindowInteractor::ClassExitMethodArg = arg;

    // no call to this->Modified() since this is a class member function
  }
}

//----------------------------------------------------------------------------
// Set the arg delete method.  This is used to free user memory.
void
vtkOpenVRRenderWindowInteractor::SetClassExitMethodArgDelete(void (*f)(void *))
{
  if (f != vtkOpenVRRenderWindowInteractor::ClassExitMethodArgDelete)
  {
    vtkOpenVRRenderWindowInteractor::ClassExitMethodArgDelete = f;

    // no call to this->Modified() since this is a class member function
  }
}

//----------------------------------------------------------------------------
void vtkOpenVRRenderWindowInteractor::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
  os << indent << "StartedMessageLoop: " << this->StartedMessageLoop << endl;
}

//----------------------------------------------------------------------------
void vtkOpenVRRenderWindowInteractor::ExitCallback()
{
  if (this->HasObserver(vtkCommand::ExitEvent))
  {
    this->InvokeEvent(vtkCommand::ExitEvent,NULL);
  }
  else if (this->ClassExitMethod)
  {
    (*this->ClassExitMethod)(this->ClassExitMethodArg);
  }

  this->TerminateApp();
}