Ocean
Loading...
Searching...
No Matches
platform/openxr/Utilities.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_PLATFORM_OPENXR_UTILITIES_H
9#define META_OCEAN_PLATFORM_OPENXR_UTILITIES_H
10
12
15
16namespace Ocean
17{
18
19namespace Platform
20{
21
22namespace OpenXR
23{
24
25/**
26 * This class implements utility functions for OpenXR.
27 * @ingroup platformopenxr
28 */
29class OCEAN_PLATFORM_OPENXR_EXPORT Utilities final
30{
31 public:
32
33 /**
34 * Translates an OpenXR result associated with an instance into a readable string.
35 * @param xrInstance The OpenXR instance associated with the given result, must be valid
36 * @param xrResult The OpenXR result to translate
37 * @return The translated result, 'Unknown' if unknown
38 */
39 static std::string translateResult(const XrInstance& xrInstance, const XrResult xrResult);
40
41 /**
42 * Translates an OpenXR session state into a readable string.
43 * @param xrSessionState The OpenXR session state to translate
44 * @return The translated state, 'Unknown' if unknown
45 */
46 static std::string translateSessionState(const XrSessionState xrSessionState);
47
48 /**
49 * Converts an OpenXR view configuration type into a readable string.
50 * @param xrViewConfigurationType The view configuration type to translate
51 * @return The translated type, 'Unknown' if unknown
52 */
53 static std::string translateViewConfigurationType(const XrViewConfigurationType xrViewConfigurationType);
54
55 /**
56 * Converts an OpenXR color space into a readable string.
57 * @param xrColorSpaceFB The color space to translate
58 * @return The translated color space, 'Unknown' if unknown
59 */
60 static std::string translateColorSpace(const XrColorSpaceFB xrColorSpaceFB);
61
62 /**
63 * Converts an OpenXR path to a string.
64 * @param xrInstance The OpenXR instance associated with the given path, must be valid
65 * @param xrPath The OpenXR path to convert, XR_NULL_PATH to return an empty string
66 * @return The converted path as string
67 */
68 static std::string translatePath(const XrInstance& xrInstance, const XrPath& xrPath);
69
70 /**
71 * Converts a string to an OpenXR path.
72 * @param xrInstance The OpenXR instance associated with the given path, must be valid
73 * @param path The path to convert, empty to return XR_NULL_PATH
74 * @return The converted path as string
75 */
76 static XrPath translatePath(const XrInstance& xrInstance, const std::string& path);
77
78 /**
79 * Converts an OpenXR pose to a HomogenousMatrix4.
80 * @param xrPose The pose to convert
81 * @return The resulting matrix
82 * @tparam T The data type of the scalar to be used
83 */
84 template <typename T = Scalar>
85 static inline HomogenousMatrixT4<T> toHomogenousMatrix4(const XrPosef& xrPose);
86
87 /**
88 * Converts an OpenXR XrQuaternionf to a Quaternion
89 * @param xrQuaternionf The rotation to convert
90 * @return The resulting quaternion
91 * @tparam T The data type of the scalar to be used
92 */
93 template <typename T = Scalar>
94 static inline QuaternionT<T> toQuaternion(const XrQuaternionf& xrQuaternionf);
95
96 /**
97 * Converts an OpenXR XrVector3f to a Vector3
98 * @param xrVector3f The vector to convert
99 * @return The resulting quaternion
100 * @tparam T The data type of the scalar to be used
101 */
102 template <typename T = Scalar>
103 static inline VectorT3<T> toVector3(const XrVector3f& xrVector3f);
104
105 /**
106 * Converts a 4x4 homogenous matrix containing a pose to an OpenXR XrPosef object.
107 * @param pose The pose matrix to convert, must be valid
108 * @return The resulting OpenXR object
109 * @tparam T The data type of the scalars of the matrix
110 */
111 template <typename T = Scalar>
112 static inline XrPosef toXrPose(const HomogenousMatrixT4<T>& pose);
113
114 /**
115 * Converts an OpenXR field of view into a 4x4 projection matrix.
116 * @param xrFovf The OpenXR field of view for which the projection matrix will be determined
117 * @param nearDistance Positive distance to the near clipping plane, with range (0, infinity)
118 * @param farDistance Positive distance to the far clipping plane, with range (nearDistance, infinity)
119 */
120 static SquareMatrixF4 toProjectionMatrix4(const XrFovf& xrFovf, const float nearDistance, const float farDistance);
121
122 /**
123 * Determines the pose of an OpenXR space.
124 * @param xrSpace The space for which the pose will be returned, must be valid
125 * @param xrBaseSpace The base space in relation to which the pose will be returned, must be valid
126 * @param xrTime The time for which the pose will be determined, must be valid
127 * @param xrSpaceLocationFlags Optional resulting location flags, nullptr if not of interest
128 * @return The resulting pose, invalid in case of an error
129 */
130 template <typename T = Scalar>
131 static inline HomogenousMatrixT4<T> determinePose(const XrSpace& xrSpace, const XrSpace& xrBaseSpace, const XrTime& xrTime, XrSpaceLocationFlags* xrSpaceLocationFlags = nullptr);
132};
133
134template <>
135inline HomogenousMatrixT4<float> Utilities::toHomogenousMatrix4<float>(const XrPosef& xrPose)
136{
137 ocean_assert(QuaternionF(xrPose.orientation.w, xrPose.orientation.x, xrPose.orientation.y, xrPose.orientation.z).isValid());
138
139 return HomogenousMatrixF4(VectorF3(xrPose.position.x, xrPose.position.y, xrPose.position.z), QuaternionF(xrPose.orientation.w, xrPose.orientation.x, xrPose.orientation.y, xrPose.orientation.z));
140}
141
142template <typename T>
144{
145 ocean_assert(QuaternionF(xrPose.orientation.w, xrPose.orientation.x, xrPose.orientation.y, xrPose.orientation.z).isValid());
146
147 const VectorT3<T> position(T(xrPose.position.x), T(xrPose.position.y), T(xrPose.position.z));
148 const QuaternionT<T> orientation(T(xrPose.orientation.w), T(xrPose.orientation.x), T(xrPose.orientation.y), T(xrPose.orientation.z));
149
150 return HomogenousMatrixT4<T>(position, orientation.normalized()); // we normalize the quaternion to prevent precision issues
151}
152
153template <>
154inline QuaternionT<float> Utilities::toQuaternion(const XrQuaternionf& xrQuaternionf)
155{
156 ocean_assert(QuaternionF(xrQuaternionf.w, xrQuaternionf.x, xrQuaternionf.y, xrQuaternionf.z).isValid());
157
158 return QuaternionF(xrQuaternionf.w, xrQuaternionf.x, xrQuaternionf.y, xrQuaternionf.z);
159}
160
161template <typename T>
162inline QuaternionT<T> Utilities::toQuaternion(const XrQuaternionf& xrQuaternionf)
163{
164 ocean_assert(QuaternionF(xrQuaternionf.w, xrQuaternionf.x, xrQuaternionf.y, xrQuaternionf.z).isValid());
165
166 const QuaternionT<T> orientation(T(xrQuaternionf.w), T(xrQuaternionf.x), T(xrQuaternionf.y), T(xrQuaternionf.z));
167
168 return orientation.normalized(); // we normalize the quaternion to prevent precision issues
169}
170
171template <typename T>
172inline VectorT3<T> Utilities::toVector3(const XrVector3f& xrVector3f)
173{
174 return VectorT3<T>(T(xrVector3f.x), T(xrVector3f.y), T(xrVector3f.z));
175}
176
177template <>
179{
180 ocean_assert(pose.isValid());
181 ocean_assert(pose.rotationMatrix().isOrthonormal(NumericF::weakEps()));
182
183 const VectorF3 position = pose.translation();
184 const QuaternionF orientation = pose.rotation();
185
186 XrPosef result;
187
188 result.orientation.x = orientation.x();
189 result.orientation.y = orientation.y();
190 result.orientation.z = orientation.z();
191 result.orientation.w = orientation.w();
192
193 result.position.x = position.x();
194 result.position.y = position.y();
195 result.position.z = position.z();
196
197 return result;
198}
199
200template <typename T>
202{
203 ocean_assert(pose.isValid());
204 ocean_assert(pose.rotationMatrix().isOrthonormal(NumericT<T>::weakEps()));
205
206 const VectorT3<T> position = pose.translation();
207
208 QuaternionF orientation(pose.rotation()); // we normalize the quaternion to prevent precision issues
209 orientation.normalize();
210
211 XrPosef result;
212
213 result.orientation.x = orientation.x();
214 result.orientation.y = orientation.y();
215 result.orientation.z = orientation.z();
216 result.orientation.w = orientation.w();
217
218 result.position.x = float(position.x());
219 result.position.y = float(position.y());
220 result.position.z = float(position.z());
221
222 return result;
223}
224
225template <typename T>
226inline HomogenousMatrixT4<T> Utilities::determinePose(const XrSpace& xrSpace, const XrSpace& xrBaseSpace, const XrTime& xrTime, XrSpaceLocationFlags* xrSpaceLocationFlags)
227{
228 ocean_assert(xrSpace != XR_NULL_HANDLE);
229 ocean_assert(xrBaseSpace != XR_NULL_HANDLE);
230
231 XrSpaceLocation xrSpaceLocation = {XR_TYPE_SPACE_LOCATION};
232 const XrResult xrResult = xrLocateSpace(xrSpace, xrBaseSpace, xrTime, &xrSpaceLocation);
233
234 if (xrResult != XR_SUCCESS)
235 {
236 return HomogenousMatrixT4<T>(false);
237 }
238
239 if (xrSpaceLocationFlags != nullptr)
240 {
241 *xrSpaceLocationFlags = xrSpaceLocation.locationFlags;
242 }
243
244 return toHomogenousMatrix4<T>(xrSpaceLocation.pose);
245}
246
247}
248
249}
250
251}
252
253#endif // META_OCEAN_PLATFORM_OPENXR_UTILITIES_H
This class implements a 4x4 homogeneous transformation matrix using floating point values with the pr...
Definition HomogenousMatrix4.h:110
SquareMatrixT3< T > rotationMatrix() const
Returns the rotation matrix of the transformation.
Definition HomogenousMatrix4.h:1493
VectorT3< T > translation() const
Returns the translation of the transformation.
Definition HomogenousMatrix4.h:1381
bool isValid() const
Returns whether this matrix is a valid homogeneous transformation.
Definition HomogenousMatrix4.h:1806
QuaternionT< T > rotation() const
Returns the rotation of the transformation as quaternion.
Definition HomogenousMatrix4.h:1388
This class provides basic numeric functionalities.
Definition Numeric.h:57
static constexpr T weakEps()
Returns a weak epsilon.
This class implements utility functions for OpenXR.
Definition platform/openxr/Utilities.h:30
static std::string translateSessionState(const XrSessionState xrSessionState)
Translates an OpenXR session state into a readable string.
static HomogenousMatrixT4< T > determinePose(const XrSpace &xrSpace, const XrSpace &xrBaseSpace, const XrTime &xrTime, XrSpaceLocationFlags *xrSpaceLocationFlags=nullptr)
Determines the pose of an OpenXR space.
Definition platform/openxr/Utilities.h:226
static std::string translateResult(const XrInstance &xrInstance, const XrResult xrResult)
Translates an OpenXR result associated with an instance into a readable string.
static XrPath translatePath(const XrInstance &xrInstance, const std::string &path)
Converts a string to an OpenXR path.
static XrPosef toXrPose(const HomogenousMatrixT4< T > &pose)
Converts a 4x4 homogenous matrix containing a pose to an OpenXR XrPosef object.
Definition platform/openxr/Utilities.h:201
static VectorT3< T > toVector3(const XrVector3f &xrVector3f)
Converts an OpenXR XrVector3f to a Vector3.
Definition platform/openxr/Utilities.h:172
static std::string translateViewConfigurationType(const XrViewConfigurationType xrViewConfigurationType)
Converts an OpenXR view configuration type into a readable string.
static HomogenousMatrixT4< T > toHomogenousMatrix4(const XrPosef &xrPose)
Converts an OpenXR pose to a HomogenousMatrix4.
Definition platform/openxr/Utilities.h:143
static SquareMatrixF4 toProjectionMatrix4(const XrFovf &xrFovf, const float nearDistance, const float farDistance)
Converts an OpenXR field of view into a 4x4 projection matrix.
static std::string translatePath(const XrInstance &xrInstance, const XrPath &xrPath)
Converts an OpenXR path to a string.
static QuaternionT< T > toQuaternion(const XrQuaternionf &xrQuaternionf)
Converts an OpenXR XrQuaternionf to a Quaternion.
Definition platform/openxr/Utilities.h:162
static std::string translateColorSpace(const XrColorSpaceFB xrColorSpaceFB)
Converts an OpenXR color space into a readable string.
This class implements a unit quaternion rotation.
Definition Quaternion.h:100
bool normalize()
Normalizes the quaternion in place.
Definition Quaternion.h:714
const T & x() const
Returns the x value of the quaternion.
Definition Quaternion.h:917
const T & w() const
Returns the w value of the quaternion.
Definition Quaternion.h:905
QuaternionT< T > normalized() const
Returns the normalized quaternion.
Definition Quaternion.h:699
const T & y() const
Returns the y value of the quaternion.
Definition Quaternion.h:929
const T & z() const
Returns the z value of the quaternion.
Definition Quaternion.h:941
This class implements a 4x4 square matrix.
Definition SquareMatrix4.h:85
This class implements a vector with three elements.
Definition Vector3.h:97
const T & y() const noexcept
Returns the y value.
Definition Vector3.h:824
const T & x() const noexcept
Returns the x value.
Definition Vector3.h:812
const T & z() const noexcept
Returns the z value.
Definition Vector3.h:836
QuaternionT< float > QuaternionF
Instantiation of the QuaternionT template class using a single precision float data type.
Definition Quaternion.h:54
VectorT3< float > VectorF3
Definition of a 3D vector with float values.
Definition Vector3.h:43
HomogenousMatrixT4< float > HomogenousMatrixF4
Instantiation of the HomogenousMatrixT4 template class using a float precision float data type.
Definition HomogenousMatrix4.h:58
The namespace covering the entire Ocean framework.
Definition Accessor.h:15