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 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkOpenXRRenderWindowInteractor.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 "vtkOpenXRRenderWindowInteractor.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLState.h"
#include "vtkOpenXRInteractorStyle.h"
#include "vtkOpenXRRenderWindow.h"
#include "vtkOpenXRUtilities.h"
#include "vtk_jsoncpp.h"
#include <vtksys/FStream.hxx>
#include <vtksys/SystemTools.hxx>
VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkOpenXRRenderWindowInteractor);
//------------------------------------------------------------------------------
// Construct object so that light follows camera motion.
vtkOpenXRRenderWindowInteractor::vtkOpenXRRenderWindowInteractor()
{
// This will create the actions name and store it in ActionMap
vtkNew<vtkOpenXRInteractorStyle> style;
this->SetInteractorStyle(style);
for (int i = 0; i < vtkEventDataNumberOfDevices; i++)
{
this->DeviceInputDownCount[i] = 0;
}
this->ActionManifestFileName = "./vtk_openxr_actions.json";
// OpenXR can't have slashes in the action set name (as well as action names)
this->ActionSetName = "vtk-actions";
}
//------------------------------------------------------------------------------
vtkOpenXRRenderWindowInteractor::~vtkOpenXRRenderWindowInteractor()
{
MapAction::iterator it;
for (it = this->MapActionStruct_Name.begin(); it != this->MapActionStruct_Name.end(); ++it)
{
ActionData* actionData = it->second;
delete actionData;
}
MapActionStruct_Name.clear();
}
//------------------------------------------------------------------------------
void vtkOpenXRRenderWindowInteractor::DoOneEvent(
vtkVRRenderWindow* renWin, vtkRenderer* vtkNotUsed(ren))
{
this->ProcessXrEvents();
if (this->Done || !vtkOpenXRManager::GetInstance().IsSessionRunning())
{
return;
}
this->PollXrActions();
if (this->RecognizeGestures)
{
this->RecognizeComplexGesture(nullptr);
}
// Start a render
this->InvokeEvent(vtkCommand::RenderEvent);
renWin->Render();
}
//------------------------------------------------------------------------------
void vtkOpenXRRenderWindowInteractor::ProcessXrEvents()
{
vtkOpenXRManager& xrManager = vtkOpenXRManager::GetInstance();
XrEventDataBuffer eventData{};
while (xrManager.PollEvent(eventData))
{
switch (eventData.type)
{
// We lost some data
case XR_TYPE_EVENT_DATA_EVENTS_LOST:
{
const auto stateEvent = *reinterpret_cast<const XrEventDataEventsLost*>(&eventData);
vtkDebugMacro(<< "OpenXR event [XR_TYPE_EVENT_DATA_EVENTS_LOST] : "
<< stateEvent.lostEventCount << " events data lost!");
// do we care if the runtime loses events?
break;
}
//
case XR_TYPE_EVENT_DATA_INSTANCE_LOSS_PENDING:
{
vtkWarningMacro(
<< "OpenXR event [XR_TYPE_EVENT_DATA_INSTANCE_LOSS_PENDING] : exit render loop.");
this->Done = true;
return;
}
//
case XR_TYPE_EVENT_DATA_SESSION_STATE_CHANGED:
{
const auto stateEvent =
*reinterpret_cast<const XrEventDataSessionStateChanged*>(&eventData);
if (stateEvent.session != xrManager.GetSession())
{
vtkErrorMacro(<< "OpenXR event [XR_TYPE_EVENT_DATA_SESSION_STATE_CHANGED] : session is "
"different than this->Session. Aborting.");
this->Done = true;
return;
}
switch (stateEvent.state)
{
case XR_SESSION_STATE_READY:
{
vtkDebugMacro(<< "OpenXR event [XR_SESSION_STATE_READY] : Begin session");
xrManager.BeginSession();
break;
}
case XR_SESSION_STATE_STOPPING:
vtkDebugMacro(<< "OpenXR event [XR_SESSION_STATE_STOPPING]");
VTK_FALLTHROUGH;
case XR_SESSION_STATE_LOSS_PENDING:
// Session was lost, so start over and poll for new systemId.
vtkDebugMacro(<< "OpenXR event [XR_SESSION_STATE_LOSS_PENDING]");
VTK_FALLTHROUGH;
case XR_SESSION_STATE_EXITING:
{
// Do not attempt to restart, because user closed this session.
vtkDebugMacro(<< "OpenXR event [XR_SESSION_STATE_EXITING]");
vtkDebugMacro(<< "Exit render loop.");
this->Done = true;
break;
}
default:
break;
}
break;
}
case XR_TYPE_EVENT_DATA_REFERENCE_SPACE_CHANGE_PENDING:
{
vtkDebugMacro(<< "OpenXR event [XR_TYPE_EVENT_DATA_REFERENCE_SPACE_CHANGE_PENDING]");
const auto stateEvent =
*reinterpret_cast<const XrEventDataReferenceSpaceChangePending*>(&eventData);
(void)stateEvent;
break;
}
case XR_TYPE_EVENT_DATA_INTERACTION_PROFILE_CHANGED:
{
vtkDebugMacro(<< "OpenXR event [XR_TYPE_EVENT_DATA_INTERACTION_PROFILE_CHANGED]");
const auto stateEvent =
*reinterpret_cast<const XrEventDataInteractionProfileChanged*>(&eventData);
(void)stateEvent;
XrInteractionProfileState state{ XR_TYPE_INTERACTION_PROFILE_STATE };
for (uint32_t hand :
{ vtkOpenXRManager::ControllerIndex::Left, vtkOpenXRManager::ControllerIndex::Right })
{
if (!xrManager.XrCheckWarn(xrGetCurrentInteractionProfile(xrManager.GetSession(),
xrManager.GetSubactionPaths()[hand], &state),
"Failed to get interaction profile for hand " + hand))
{
continue;
}
XrPath interactionProfile = state.interactionProfile;
if (!interactionProfile)
{
vtkDebugMacro(<< "No interaction profile set");
continue;
}
uint32_t strLength;
char profileString[XR_MAX_PATH_LENGTH];
if (!xrManager.XrCheckWarn(
xrPathToString(xrManager.GetXrRuntimeInstance(), interactionProfile,
XR_MAX_PATH_LENGTH, &strLength, profileString),
"Failed to get interaction profile path string for hand " + hand))
{
continue;
}
vtkDebugMacro(<< "Interaction profile changed for " << hand << ": " << profileString);
}
break;
}
default:
{
// Give a chance to the manager to handle connection events
if (!xrManager.GetConnectionStrategy()->HandleXrEvent(eventData))
{
vtkWarningMacro(<< "Unhandled event type "
<< vtkOpenXRUtilities::GetStructureTypeAsString(eventData.type));
}
break;
}
}
}
}
//------------------------------------------------------------------------------
void vtkOpenXRRenderWindowInteractor::ConvertOpenXRPoseToWorldCoordinates(const XrPosef& xrPose,
double pos[3], // Output world position
double wxyz[4], // Output world orientation quaternion
double ppos[3], // Output physical position
double wdir[3]) // Output world view direction (-Z)
{
vtkOpenXRUtilities::SetMatrixFromXrPose(this->PoseToWorldMatrix, xrPose);
this->ConvertPoseToWorldCoordinates(this->PoseToWorldMatrix, pos, wxyz, ppos, wdir);
}
//------------------------------------------------------------------------------
void vtkOpenXRRenderWindowInteractor::PollXrActions()
{
// Update the action states by syncing using the active action set
vtkOpenXRManager::GetInstance().SyncActions();
// Iterate over all actions and update their data
MapAction::iterator it;
for (it = this->MapActionStruct_Name.begin(); it != this->MapActionStruct_Name.end(); ++it)
{
ActionData* actionData = it->second;
// Update the state of the actions for left and right hands separately.
for (uint32_t hand :
{ vtkOpenXRManager::ControllerIndex::Left, vtkOpenXRManager::ControllerIndex::Right })
{
vtkOpenXRManager::GetInstance().UpdateActionData(actionData->ActionStruct, hand);
}
}
// Construct the event data that contains position and orientation of each hand
double pos[3] = { 0.0 };
double ppos[3] = { 0.0 };
double wxyz[4] = { 0.0 };
double wdir[3] = { 0.0 };
std::array<vtkSmartPointer<vtkEventDataDevice3D>, 2> eventDatas;
for (const uint32_t hand :
{ vtkOpenXRManager::ControllerIndex::Left, vtkOpenXRManager::ControllerIndex::Right })
{
XrPosef* handPose = this->GetHandPose(hand);
if (handPose)
{
this->ConvertOpenXRPoseToWorldCoordinates(*handPose, pos, wxyz, ppos, wdir);
auto edHand = vtkEventDataDevice3D::New();
edHand->SetDevice(hand == vtkOpenXRManager::ControllerIndex::Right
? vtkEventDataDevice::RightController
: vtkEventDataDevice::LeftController);
edHand->SetWorldPosition(pos);
edHand->SetWorldOrientation(wxyz);
edHand->SetWorldDirection(wdir);
eventDatas[hand].TakeReference(edHand);
// We should remove this and use event data directly
int pointerIndex = static_cast<int>(edHand->GetDevice());
this->SetPhysicalEventPosition(ppos[0], ppos[1], ppos[2], pointerIndex);
this->SetWorldEventPosition(pos[0], pos[1], pos[2], pointerIndex);
this->SetWorldEventOrientation(wxyz[0], wxyz[1], wxyz[2], wxyz[3], pointerIndex);
}
}
// All actions are now updated, handle them now
for (it = this->MapActionStruct_Name.begin(); it != this->MapActionStruct_Name.end(); ++it)
{
ActionData* actionData = it->second;
for (uint32_t hand :
{ vtkOpenXRManager::ControllerIndex::Left, vtkOpenXRManager::ControllerIndex::Right })
{
vtkEventDataDevice3D* eventData = eventDatas[hand];
if (eventData)
{
eventData->SetInput(actionData->DeviceInput);
eventData->SetType(actionData->EventId);
this->HandleAction(*actionData, hand, eventData);
}
}
}
}
//------------------------------------------------------------------------------
XrPosef* vtkOpenXRRenderWindowInteractor::GetHandPose(uint32_t hand)
{
if (this->MapActionStruct_Name.count("handpose") == 0)
{
return nullptr;
}
ActionData* adHandPose = this->MapActionStruct_Name["handpose"];
return &(adHandPose->ActionStruct.PoseLocations[hand].pose);
}
//------------------------------------------------------------------------------
void vtkOpenXRRenderWindowInteractor::HandleAction(
const ActionData& actionData, const int hand, vtkEventDataDevice3D* ed)
{
const Action_t& actionT = actionData.ActionStruct;
switch (actionT.ActionType)
{
/*case XR_ACTION_TYPE_FLOAT_INPUT:
actionT.States[hand]._float.type = XR_TYPE_ACTION_STATE_FLOAT;
actionT.States[hand]._float.next = nullptr;
if (!this->XrCheckError(xrGetActionStateFloat(Session, &info, &action_t.States[hand]._float),
"Failed to get float value"))
{
return false;
}
break;*/
case XR_ACTION_TYPE_BOOLEAN_INPUT:
this->HandleBooleanAction(actionData, hand, ed);
break;
case XR_ACTION_TYPE_VECTOR2F_INPUT:
this->HandleVector2fAction(actionData, hand, ed);
break;
case XR_ACTION_TYPE_POSE_INPUT:
this->HandlePoseAction(actionData, hand, ed);
break;
default:
break;
}
}
//------------------------------------------------------------------------------
void vtkOpenXRRenderWindowInteractor::ApplyAction(
const ActionData& actionData, vtkEventDataDevice3D* ed)
{
this->SetPointerIndex(static_cast<int>(ed->GetDevice()));
if (actionData.UseFunction)
{
actionData.Function(ed);
}
else
{
this->InvokeEvent(actionData.EventId, ed);
}
}
//------------------------------------------------------------------------------
void vtkOpenXRRenderWindowInteractor::HandleBooleanAction(
const ActionData& actionData, const int hand, vtkEventDataDevice3D* ed)
{
XrActionStateBoolean value = actionData.ActionStruct.States[hand]._boolean;
// Set the active state of the model
vtkOpenXRRenderWindow::SafeDownCast(this->RenderWindow)
->SetModelActiveState(hand, value.isActive);
// Do nothing if the controller is inactive
if (!value.isActive)
{
return;
}
if (value.changedSinceLastSync)
{
vtkDebugMacro(<< "Boolean action \"" << actionData.Name << "\" is triggered with value "
<< value.currentState << " for hand " << hand);
if (value.currentState == 1)
{
ed->SetAction(vtkEventDataAction::Press);
}
else
{
ed->SetAction(vtkEventDataAction::Release);
}
this->ApplyAction(actionData, ed);
}
}
//------------------------------------------------------------------------------
void vtkOpenXRRenderWindowInteractor::HandlePoseAction(
const ActionData& actionData, const int hand, vtkEventDataDevice3D* ed)
{
XrActionStatePose pose = actionData.ActionStruct.States[hand]._pose;
// Set the active state of the model
vtkOpenXRRenderWindow::SafeDownCast(this->RenderWindow)->SetModelActiveState(hand, pose.isActive);
// Do nothing if the controller is inactive
if (!pose.isActive)
{
return;
}
this->ApplyAction(actionData, ed);
}
//------------------------------------------------------------------------------
void vtkOpenXRRenderWindowInteractor::HandleVector2fAction(
const ActionData& actionData, const int hand, vtkEventDataDevice3D* ed)
{
XrActionStateVector2f vec2f = actionData.ActionStruct.States[hand]._vec2f;
// Set the active state of the model
vtkOpenXRRenderWindow::SafeDownCast(this->RenderWindow)
->SetModelActiveState(hand, vec2f.isActive);
// Do nothing if the controller is inactive
if (!vec2f.isActive)
{
return;
}
if (vec2f.changedSinceLastSync)
{
vtkDebugMacro(<< "Vector2f : " << actionData.Name << ", x = " << vec2f.currentState.x
<< " / y = " << vec2f.currentState.y);
ed->SetTrackPadPosition(vec2f.currentState.x, vec2f.currentState.y);
this->ApplyAction(actionData, ed);
}
}
//------------------------------------------------------------------------------
void vtkOpenXRRenderWindowInteractor::AddAction(
const std::string& path, const vtkCommand::EventIds& eid)
{
if (this->MapActionStruct_Name.count(path) == 0)
{
ActionData* am = new ActionData();
this->MapActionStruct_Name[path] = am;
}
auto* am = this->MapActionStruct_Name[path];
am->EventId = eid;
am->UseFunction = false;
}
//------------------------------------------------------------------------------
void vtkOpenXRRenderWindowInteractor::AddAction(
const std::string& path, const std::function<void(vtkEventData*)>& func)
{
if (this->MapActionStruct_Name.count(path) == 0)
{
ActionData* am = new ActionData();
this->MapActionStruct_Name[path] = am;
}
auto* am = this->MapActionStruct_Name[path];
am->UseFunction = true;
am->Function = func;
}
//------------------------------------------------------------------------------
void vtkOpenXRRenderWindowInteractor::Initialize()
{
if (this->Initialized)
{
return;
}
// Start with superclass initialization
this->Superclass::Initialize();
vtkOpenXRRenderWindow* renWin = vtkOpenXRRenderWindow::SafeDownCast(this->RenderWindow);
// Make sure the render window is initialized
renWin->Initialize();
if (!renWin->GetInitialized())
{
return;
}
// Grip actions are handled by the interactor directly (why?)
this->AddAction("leftgripaction", [this](vtkEventData* ed) { this->HandleGripEvents(ed); });
this->AddAction("rightgripaction", [this](vtkEventData* ed) { this->HandleGripEvents(ed); });
// Create an entry for pose actions that are used to retrieve
// Orientation and locations of trackers
this->AddAction("handpose", vtkCommand::Move3DEvent);
// Prevent unbound action warning
this->AddAction("handposegrip", [](vtkEventData*) {});
std::string fullpath =
vtksys::SystemTools::CollapseFullPath(this->ActionManifestFileName.c_str());
if (!this->LoadActions(fullpath))
{
vtkErrorMacro(<< "Failed to load actions.");
this->Initialized = false;
return;
}
// All action sets have been created, so
// We can now attach the action sets to the session
if (!vtkOpenXRManager::GetInstance().AttachSessionActionSets())
{
this->Initialized = false;
return;
}
}
//------------------------------------------------------------------------------
bool vtkOpenXRRenderWindowInteractor::LoadActions(const std::string& actionFilename)
{
vtkDebugMacro(<< "LoadActions from file : " << actionFilename);
// As OpenXR does not yet have a way to pass a file to create actions
// We need to create them programmatically, so we parse it with JsonCpp
Json::Value root;
// Open the file
vtksys::ifstream file;
file.open(actionFilename.c_str());
if (!file.is_open())
{
vtkErrorMacro(<< "Unable to open openXR action file : " << actionFilename);
return false;
}
Json::CharReaderBuilder builder;
std::string formattedErrors;
// parse the entire data into the Json::Value root
if (!Json::parseFromStream(builder, file, &root, &formattedErrors))
{
// Report failures and their locations in the document
vtkErrorMacro(<< "Failed to parse action file with errors :" << endl << formattedErrors);
return false;
}
// Create an action set
std::string localizedActionSetName = "VTK actions";
vtkOpenXRManager::GetInstance().CreateActionSet(this->ActionSetName, localizedActionSetName);
// We must select an action set to create actions
// For instance only one action set so select it
// Improvement: select each action set and create all actions
// that belong to it
vtkOpenXRManager::GetInstance().SelectActiveActionSet(0);
// Create actions
Json::Value actions = root["actions"];
if (actions.isNull())
{
vtkErrorMacro(<< "Parse openxr_actions: Missing actions node");
return false;
}
Json::Value localization = root["localization"];
if (localization.isNull())
{
vtkErrorMacro(<< "Parse openxr_actions: Missing localization node");
return false;
}
localization = localization[0];
for (Json::Value::ArrayIndex i = 0; i < actions.size(); ++i)
{
// Create one action per json value
Json::Value action = actions[i];
std::string name = action["name"].asString();
std::string localizedName = localization[name].asString();
std::string type = action["type"].asString();
// If the action is an output, add it so that it will
// connect to its binding without user having to specify.
// Vibration is the only supported output
if (type == "vibration")
{
if (this->MapActionStruct_Name.count(name) == 0)
{
ActionData* am = new ActionData();
this->MapActionStruct_Name[name] = am;
}
}
// Check if the action is used by the interactor style
// or ourself. If that's the case, create it
// Else do nothing
if (this->MapActionStruct_Name.count(name) == 0)
{
vtkWarningMacro(
<< "An action with name " << name
<< " is available but the interactor style or the interactor does not use it.");
continue;
}
vtkDebugMacro(<< "Creating an action of type=" << type << ", with name=" << name
<< ", localizedName=" << localizedName);
XrActionType xrActionType = this->GetActionTypeFromString(type);
if (!xrActionType)
{
return false;
}
// Create the action using the selected action set
Action_t actionStruct;
actionStruct.ActionType = xrActionType;
if (!vtkOpenXRManager::GetInstance().CreateOneAction(actionStruct, name, localizedName))
{
return false;
}
// Store it to retrieve actions by their name
this->MapActionStruct_Name[name]->ActionStruct = actionStruct;
this->MapActionStruct_Name[name]->Name = name;
}
Json::Value defaultBindings = root["default_bindings"];
if (defaultBindings.isNull())
{
vtkErrorMacro(<< "Parse openxr_actions: Missing default_bindings node");
return false;
}
// look in the same directory as the actionFilename
std::string path = vtksys::SystemTools::GetFilenamePath(actionFilename);
for (Json::Value::ArrayIndex i = 0; i < defaultBindings.size(); ++i)
{
Json::Value binding = defaultBindings[i];
std::string bindingUrl = binding["binding_url"].asString();
std::string bindingFilename = vtksys::SystemTools::CollapseFullPath(path + "/" + bindingUrl);
if (!this->LoadDefaultBinding(bindingFilename))
{
return false;
}
}
return true;
}
//------------------------------------------------------------------------------
XrActionType vtkOpenXRRenderWindowInteractor::GetActionTypeFromString(const std::string& type)
{
if (type == "boolean")
{
return XR_ACTION_TYPE_BOOLEAN_INPUT;
}
else if (type == "float")
{
return XR_ACTION_TYPE_FLOAT_INPUT;
}
else if (type == "vector2")
{
return XR_ACTION_TYPE_VECTOR2F_INPUT;
}
else if (type == "pose")
{
return XR_ACTION_TYPE_POSE_INPUT;
}
else if (type == "vibration")
{
return XR_ACTION_TYPE_VIBRATION_OUTPUT;
}
else
{
vtkErrorMacro(<< "Unrecognized action type: " << type);
return (XrActionType)0;
}
}
//------------------------------------------------------------------------------
bool vtkOpenXRRenderWindowInteractor::LoadDefaultBinding(const std::string& bindingFilename)
{
Json::Value root;
// Open the file
vtksys::ifstream file;
file.open(bindingFilename.c_str());
if (!file.is_open())
{
vtkErrorMacro(<< "Unable to open openXR binding file : " << bindingFilename);
return false;
}
Json::CharReaderBuilder builder;
std::string formattedErrors;
// parse the entire data into the Json::Value root
if (!Json::parseFromStream(builder, file, &root, &formattedErrors))
{
// Report failures and their locations in the document
vtkErrorMacro(<< "Failed to parse binding file with errors :" << endl << formattedErrors);
return false;
}
// Get the interaction profile name
std::string interactionProfile = root["interaction_profile"].asString();
Json::Value bindings = root["bindings"];
Json::Value actionSet = bindings[this->ActionSetName];
if (actionSet.isNull())
{
vtkErrorMacro(<< "Selected action set : " << this->ActionSetName
<< " is not in binding file : " << bindingFilename);
return false;
}
// We need to fill this vector to suggest interaction profile bindings
std::vector<XrActionSuggestedBinding> actionSuggestedBindings;
// Get the XrPath from path string,
// Get the XrAction from the string jsonValue["output"]
// Store in the actionData the device input guessed from the path
// And fill actionSuggestedBindings
auto fillActionSuggestedBindings = [&](const std::string& path, const Json::Value& jsonValue) {
// Get the action
std::string action = jsonValue["output"].asString();
// Only suggest a binding for an action that is used by the interactor style
// or ourself
if (this->MapActionStruct_Name.count(action) == 0)
{
return;
}
vtkDebugMacro(<< "Add action : " << action << ", with path : " << path);
ActionData* actionData = this->GetActionDataFromName(action);
if (actionData != nullptr)
{
// Use the path to guess the device input
if (path.find("trigger") != std::string::npos)
{
actionData->DeviceInput = vtkEventDataDeviceInput::Trigger;
}
else if (path.find("trackpad") != std::string::npos)
{
actionData->DeviceInput = vtkEventDataDeviceInput::TrackPad;
}
else if (path.find("grip") != std::string::npos)
{
actionData->DeviceInput = vtkEventDataDeviceInput::Grip;
}
else if (path.find("thumbstick") != std::string::npos)
{
actionData->DeviceInput = vtkEventDataDeviceInput::Joystick;
}
Action_t& actionT = actionData->ActionStruct;
if (actionT.Action == XR_NULL_HANDLE)
{
vtkErrorMacro(<< "Action " << action << ", with path : " << path << " has a null handle !");
return;
}
XrPath xrPath = vtkOpenXRManager::GetInstance().GetXrPath(path);
actionSuggestedBindings.push_back({ actionT.Action, xrPath });
}
};
// First, look after all sources inputs, ie. boolean/float/vector2f actions
Json::Value sources = actionSet["sources"];
for (Json::Value::ArrayIndex i = 0; i < sources.size(); ++i)
{
Json::Value source = sources[i];
// The path for this action
std::string path = source["path"].asString();
// Iterate over all inputs and append to the path the selected input
// For example, if the input is "click", then append click to the path
// if the input is "position", add nothing as openxr binds the position as a vector2f
// (for example if we want to retrieve the position of the trackpad as a vector2f directly)
Json::Value inputs = source["inputs"];
for (auto const& inputStr : inputs.getMemberNames())
{
Json::Value action = inputs[inputStr];
if (inputStr == "position")
{
fillActionSuggestedBindings(path, action);
}
else
{
fillActionSuggestedBindings(path + "/" + inputStr, action);
}
}
}
// Look under haptics for any outputs
Json::Value haptics = actionSet["haptics"];
for (Json::Value::ArrayIndex i = 0; i < haptics.size(); i++)
{
Json::Value haptic = haptics[i];
// The path for this action
std::string path = haptic["path"].asString();
// Iterate over all outputs
fillActionSuggestedBindings(path, haptic);
}
// Submit all suggested bindings
return vtkOpenXRManager::GetInstance().SuggestActions(
interactionProfile, actionSuggestedBindings);
}
//------------------------------------------------------------------------------
vtkOpenXRRenderWindowInteractor::ActionData* vtkOpenXRRenderWindowInteractor::GetActionDataFromName(
const std::string& actionName)
{
// Check if action data exists
if (this->MapActionStruct_Name.count(actionName) == 0)
{
vtkWarningMacro(<< "vtkOpenXRRenderWindowInteractor: Attempt to get an action data with name "
<< actionName << " that does not exist in the map.");
return nullptr;
}
return this->MapActionStruct_Name[actionName];
}
//------------------------------------------------------------------------------
void vtkOpenXRRenderWindowInteractor::PrintSelf(ostream& os, vtkIndent indent)
{
os << indent << "vtkOpenXRRenderWindowInteractor"
<< "\n";
this->Superclass::PrintSelf(os, indent);
}
//------------------------------------------------------------------------------
bool vtkOpenXRRenderWindowInteractor::ApplyVibration(const std::string& actionName, const int hand,
const float amplitude, const float duration, const float frequency)
{
ActionData* actionData = GetActionDataFromName(actionName);
if (actionData == nullptr)
{
vtkWarningMacro(
<< "vtkOpenXRRenderWindowInteractor: Attempt to ApplyVibration using action data with name"
<< actionName << " that does not exist.");
return false;
}
return vtkOpenXRManager::GetInstance().ApplyVibration(
actionData->ActionStruct, hand, amplitude, duration, frequency);
}
VTK_ABI_NAMESPACE_END
|