-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathoxr_simple_api_demo.cpp
More file actions
120 lines (98 loc) · 4.7 KB
/
oxr_simple_api_demo.cpp
File metadata and controls
120 lines (98 loc) · 4.7 KB
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
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
#include <deviceio_session/deviceio_session.hpp>
#include <deviceio_trackers/hand_tracker.hpp>
#include <deviceio_trackers/head_tracker.hpp>
#include <oxr/oxr_session.hpp>
#include <oxr_utils/os_time.hpp>
#include <iostream>
#include <memory>
/**
* Simple API Demo - demonstrates the clean public API
*
* External users only see:
* - get_head() / get_left_hand() / get_right_hand()
* - get_name()
* - DeviceIOSession::get_required_extensions(trackers) before creating OpenXRSession
*
* Internal lifecycle methods (initialize, update, cleanup) are hidden!
*/
int main(int argc, char** argv)
try
{
std::cout << "OpenXR Simple API Demo" << std::endl;
std::cout << "======================" << std::endl;
std::cout << std::endl;
// Step 1: External user creates trackers (only public API visible)
std::cout << "[Step 1] Creating trackers..." << std::endl;
auto hand_tracker = std::make_shared<core::HandTracker>();
auto head_tracker = std::make_shared<core::HeadTracker>();
std::cout << " ✓ Created " << hand_tracker->get_name() << std::endl;
std::cout << " ✓ Created " << head_tracker->get_name() << std::endl;
// Note: At this point, external users CANNOT call:
// - hand_tracker->initialize() // protected - not visible!
// - hand_tracker->update() // protected - not visible!
// - hand_tracker->cleanup() // protected - not visible!
// Step 2: External user queries required extensions (public API)
std::cout << "[Step 2] Querying required extensions..." << std::endl;
std::vector<std::shared_ptr<core::ITracker>> trackers = { hand_tracker, head_tracker };
auto required_extensions = core::DeviceIOSession::get_required_extensions(trackers);
std::cout << " Required extensions:" << std::endl;
for (const auto& ext : required_extensions)
{
std::cout << " - " << ext << std::endl;
}
std::cout << std::endl;
// Step 3: External user creates OpenXR session
std::cout << "[Step 3] Creating OpenXR session..." << std::endl;
// Create OpenXR session with required extensions
auto oxr_session = std::make_shared<core::OpenXRSession>("SimpleAPIDemo", required_extensions);
std::cout << " ✓ OpenXR session created" << std::endl;
std::cout << std::endl;
// Step 4: Run deviceio session with trackers (handles internal lifecycle, throws on failure)
std::cout << "[Step 4] Running deviceio session with trackers..." << std::endl;
auto handles = oxr_session->get_handles();
auto session = core::DeviceIOSession::run(trackers, handles);
std::cout << " ✓ Session created (internal initialization handled automatically)" << std::endl;
// Step 5: External user updates and queries data (public API only!)
std::cout << "[Step 5] Querying tracker data..." << std::endl;
std::cout << std::endl;
for (int i = 0; i < 5; ++i)
{
// Session handles internal update() calls to trackers
session->update(core::os_monotonic_now_ns());
// External user only uses public query methods
const auto& left_tracked = hand_tracker->get_left_hand(*session);
const auto& right_tracked = hand_tracker->get_right_hand(*session);
const auto& head_tracked = head_tracker->get_head(*session);
std::cout << "Frame " << i << ":" << std::endl;
std::cout << " Left hand: " << (left_tracked.data ? "ACTIVE" : "INACTIVE") << std::endl;
std::cout << " Right hand: " << (right_tracked.data ? "ACTIVE" : "INACTIVE") << std::endl;
std::cout << " Head pose: " << ((head_tracked.data && head_tracked.data->is_valid) ? "VALID" : "INVALID")
<< std::endl;
if (head_tracked.data && head_tracked.data->is_valid)
{
const auto& pos = head_tracked.data->pose->position();
std::cout << " Position: [" << pos.x() << ", " << pos.y() << ", " << pos.z() << "]" << std::endl;
}
std::cout << std::endl;
}
std::cout << "✓ Clean public API demo complete!" << std::endl;
std::cout << std::endl;
std::cout << "Summary:" << std::endl;
std::cout << " ✓ External users only see public API methods" << std::endl;
std::cout << " ✓ Lifecycle methods (initialize/update/cleanup) are hidden" << std::endl;
std::cout << " ✓ Session manages internal lifecycle automatically" << std::endl;
std::cout << std::endl;
return 0;
}
catch (const std::exception& e)
{
std::cerr << argv[0] << ": " << e.what() << std::endl;
return 1;
}
catch (...)
{
std::cerr << argv[0] << ": Unknown error occurred" << std::endl;
return 1;
}