SuperDex Physics C++ API
Loading...
Searching...
No Matches
coordinate_space.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
23
24#include <cstdint>
25
26namespace superdex {
27
28/**
29 * @brief Combination of axis directions for a coordinate space convention.
30 *
31 * @details Each three-letter name gives the semantic direction of positive X,
32 * positive Y, and positive Z, respectively. For example, @ref CoordinateSpaceAxes::FLU
33 * means X-forward, Y-left, Z-up.
34 */
35enum class CoordinateSpaceAxes : uint16_t {
36 // Coordinate space conventions are composed of three of these directions for X, Y, and Z.
37 // These enumerators are not valid on their own.
44
45 // X=right/left, Y=up/down, Z=forward/backward.
46 RUF = Right << 0 | Up << 3 | Forward << 6,
47 RUB = Right << 0 | Up << 3 | Backward << 6,
48 RDF = Right << 0 | Down << 3 | Forward << 6,
49 RDB = Right << 0 | Down << 3 | Backward << 6,
50 LUF = Left << 0 | Up << 3 | Forward << 6,
51 LUB = Left << 0 | Up << 3 | Backward << 6,
52 LDF = Left << 0 | Down << 3 | Forward << 6,
53 LDB = Left << 0 | Down << 3 | Backward << 6,
54
55 // X=right/left, Y=forward/backward, Z=up/down.
56 RFU = Right << 0 | Forward << 3 | Up << 6,
57 RFD = Right << 0 | Forward << 3 | Down << 6,
58 RBU = Right << 0 | Backward << 3 | Up << 6,
59 RBD = Right << 0 | Backward << 3 | Down << 6,
60 LFU = Left << 0 | Forward << 3 | Up << 6,
61 LFD = Left << 0 | Forward << 3 | Down << 6,
62 LBU = Left << 0 | Backward << 3 | Up << 6,
63 LBD = Left << 0 | Backward << 3 | Down << 6,
64
65 // X=up/down, Y=right/left, Z=forward/backward.
66 URF = Up << 0 | Right << 3 | Forward << 6,
67 URB = Up << 0 | Right << 3 | Backward << 6,
68 ULF = Up << 0 | Left << 3 | Forward << 6,
69 ULB = Up << 0 | Left << 3 | Backward << 6,
70 DRF = Down << 0 | Right << 3 | Forward << 6,
71 DRB = Down << 0 | Right << 3 | Backward << 6,
72 DLF = Down << 0 | Left << 3 | Forward << 6,
73 DLB = Down << 0 | Left << 3 | Backward << 6,
74
75 // X=up/down, Y=forward/backward, Z=right/left.
76 UFR = Up << 0 | Forward << 3 | Right << 6,
77 UFL = Up << 0 | Forward << 3 | Left << 6,
78 UBR = Up << 0 | Backward << 3 | Right << 6,
79 UBL = Up << 0 | Backward << 3 | Left << 6,
80 DFR = Down << 0 | Forward << 3 | Right << 6,
81 DFL = Down << 0 | Forward << 3 | Left << 6,
82 DBR = Down << 0 | Backward << 3 | Right << 6,
83 DBL = Down << 0 | Backward << 3 | Left << 6,
84
85 // X=forward/backward, Y=right/left, Z=up/down.
86 FRU = Forward << 0 | Right << 3 | Up << 6,
87 FRD = Forward << 0 | Right << 3 | Down << 6,
88 FLU = Forward << 0 | Left << 3 | Up << 6,
89 FLD = Forward << 0 | Left << 3 | Down << 6,
90 BRU = Backward << 0 | Right << 3 | Up << 6,
91 BRD = Backward << 0 | Right << 3 | Down << 6,
92 BLU = Backward << 0 | Left << 3 | Up << 6,
93 BLD = Backward << 0 | Left << 3 | Down << 6,
94
95 // X=forward/backward, Y=up/down, Z=right/left.
96 FUR = Forward << 0 | Up << 3 | Right << 6,
97 FUL = Forward << 0 | Up << 3 | Left << 6,
98 FDR = Forward << 0 | Down << 3 | Right << 6,
99 FDL = Forward << 0 | Down << 3 | Left << 6,
100 BUR = Backward << 0 | Up << 3 | Right << 6,
101 BUL = Backward << 0 | Up << 3 | Left << 6,
102 BDR = Backward << 0 | Down << 3 | Right << 6,
103 BDL = Backward << 0 | Down << 3 | Left << 6,
104
105 // Default convention for Mochi
107};
108
109} // namespace superdex
110
112MOCHI_ENUM_ITEM(RUF) MOCHI_ATTRIBUTE(DisplayName("X-right, Y-up, Z-forward (Unity)"));
113MOCHI_ENUM_ITEM(RUB) MOCHI_ATTRIBUTE(DisplayName("X-right, Y-up, Z-backward (OpenGL, Filament)"));
114MOCHI_ENUM_ITEM(RDF) MOCHI_ATTRIBUTE(DisplayName("X-right, Y-down, Z-forward"));
115MOCHI_ENUM_ITEM(RDB) MOCHI_ATTRIBUTE(DisplayName("X-right, Y-down, Z-backward"));
116MOCHI_ENUM_ITEM(LUF) MOCHI_ATTRIBUTE(DisplayName("X-left, Y-up, Z-forward"));
117MOCHI_ENUM_ITEM(LUB) MOCHI_ATTRIBUTE(DisplayName("X-left, Y-up, Z-backward"));
118MOCHI_ENUM_ITEM(LDF) MOCHI_ATTRIBUTE(DisplayName("X-left, Y-down, Z-forward"));
119MOCHI_ENUM_ITEM(LDB) MOCHI_ATTRIBUTE(DisplayName("X-left, Y-down, Z-backward"));
120MOCHI_ENUM_ITEM(RFU) MOCHI_ATTRIBUTE(DisplayName("X-right, Y-forward, Z-up"));
121MOCHI_ENUM_ITEM(RFD) MOCHI_ATTRIBUTE(DisplayName("X-right, Y-forward, Z-down"));
122MOCHI_ENUM_ITEM(RBU) MOCHI_ATTRIBUTE(DisplayName("X-right, Y-backward, Z-up"));
123MOCHI_ENUM_ITEM(RBD) MOCHI_ATTRIBUTE(DisplayName("X-right, Y-backward, Z-down"));
124MOCHI_ENUM_ITEM(LFU) MOCHI_ATTRIBUTE(DisplayName("X-left, Y-forward, Z-up"));
125MOCHI_ENUM_ITEM(LFD) MOCHI_ATTRIBUTE(DisplayName("X-left, Y-forward, Z-down"));
126MOCHI_ENUM_ITEM(LBU) MOCHI_ATTRIBUTE(DisplayName("X-left, Y-backward, Z-up"));
127MOCHI_ENUM_ITEM(LBD) MOCHI_ATTRIBUTE(DisplayName("X-left, Y-backward, Z-down"));
128MOCHI_ENUM_ITEM(URF) MOCHI_ATTRIBUTE(DisplayName("X-up, Y-right, Z-forward"));
129MOCHI_ENUM_ITEM(URB) MOCHI_ATTRIBUTE(DisplayName("X-up, Y-right, Z-backward"));
130MOCHI_ENUM_ITEM(ULF) MOCHI_ATTRIBUTE(DisplayName("X-up, Y-left, Z-forward"));
131MOCHI_ENUM_ITEM(ULB) MOCHI_ATTRIBUTE(DisplayName("X-up, Y-left, Z-backward"));
132MOCHI_ENUM_ITEM(DRF) MOCHI_ATTRIBUTE(DisplayName("X-down, Y-right, Z-forward"));
133MOCHI_ENUM_ITEM(DRB) MOCHI_ATTRIBUTE(DisplayName("X-down, Y-right, Z-backward"));
134MOCHI_ENUM_ITEM(DLF) MOCHI_ATTRIBUTE(DisplayName("X-down, Y-left, Z-forward"));
135MOCHI_ENUM_ITEM(DLB) MOCHI_ATTRIBUTE(DisplayName("X-down, Y-left, Z-backward"));
136MOCHI_ENUM_ITEM(UFR) MOCHI_ATTRIBUTE(DisplayName("X-up, Y-forward, Z-right"));
137MOCHI_ENUM_ITEM(UFL) MOCHI_ATTRIBUTE(DisplayName("X-up, Y-forward, Z-left"));
138MOCHI_ENUM_ITEM(UBR) MOCHI_ATTRIBUTE(DisplayName("X-up, Y-backward, Z-right"));
139MOCHI_ENUM_ITEM(UBL) MOCHI_ATTRIBUTE(DisplayName("X-up, Y-backward, Z-left"));
140MOCHI_ENUM_ITEM(DFR) MOCHI_ATTRIBUTE(DisplayName("X-down, Y-forward, Z-right"));
141MOCHI_ENUM_ITEM(DFL) MOCHI_ATTRIBUTE(DisplayName("X-down, Y-forward, Z-left"));
142MOCHI_ENUM_ITEM(DBR) MOCHI_ATTRIBUTE(DisplayName("X-down, Y-backward, Z-right"));
143MOCHI_ENUM_ITEM(DBL) MOCHI_ATTRIBUTE(DisplayName("X-down, Y-backward, Z-left"));
144MOCHI_ENUM_ITEM(FRU) MOCHI_ATTRIBUTE(DisplayName("X-forward, Y-right, Z-up (Unreal)"));
145MOCHI_ENUM_ITEM(FRD) MOCHI_ATTRIBUTE(DisplayName("X-forward, Y-right, Z-down"));
146MOCHI_ENUM_ITEM(FLU) MOCHI_ATTRIBUTE(DisplayName("X-forward, Y-left, Z-up (Default)"));
147MOCHI_ENUM_ITEM(FLD) MOCHI_ATTRIBUTE(DisplayName("X-forward, Y-left, Z-down"));
148MOCHI_ENUM_ITEM(BRU) MOCHI_ATTRIBUTE(DisplayName("X-backward, Y-right, Z-up"));
149MOCHI_ENUM_ITEM(BRD) MOCHI_ATTRIBUTE(DisplayName("X-backward, Y-right, Z-down"));
150MOCHI_ENUM_ITEM(BLU) MOCHI_ATTRIBUTE(DisplayName("X-backward, Y-left, Z-up"));
151MOCHI_ENUM_ITEM(BLD) MOCHI_ATTRIBUTE(DisplayName("X-backward, Y-left, Z-down"));
152MOCHI_ENUM_ITEM(FUR) MOCHI_ATTRIBUTE(DisplayName("X-forward, Y-up, Z-right"));
153MOCHI_ENUM_ITEM(FUL) MOCHI_ATTRIBUTE(DisplayName("X-forward, Y-up, Z-left"));
154MOCHI_ENUM_ITEM(FDR) MOCHI_ATTRIBUTE(DisplayName("X-forward, Y-down, Z-right"));
155MOCHI_ENUM_ITEM(FDL) MOCHI_ATTRIBUTE(DisplayName("X-forward, Y-down, Z-left"));
156MOCHI_ENUM_ITEM(BUR) MOCHI_ATTRIBUTE(DisplayName("X-backward, Y-up, Z-right"));
157MOCHI_ENUM_ITEM(BUL) MOCHI_ATTRIBUTE(DisplayName("X-backward, Y-up, Z-left"));
158MOCHI_ENUM_ITEM(BDR) MOCHI_ATTRIBUTE(DisplayName("X-backward, Y-down, Z-right"));
159MOCHI_ENUM_ITEM(BDL) MOCHI_ATTRIBUTE(DisplayName("X-backward, Y-down, Z-left"));
161
162namespace superdex {
163
164namespace details {
165
166/**
167 * @brief Extract the semantic direction for one numeric coordinate axis.
168 *
169 * @param[in] axes @ref CoordinateSpaceAxes value.
170 * @param[in] axisIndex 0 for X, 1 for Y, 2 for Z.
171 *
172 * @return The @ref CoordinateSpaceAxes direction (e.g. Right) of that axis, as an integer.
173 */
174[[nodiscard]] constexpr int GetAxisDirection(CoordinateSpaceAxes axes, int axisIndex) {
175 return static_cast<int>((static_cast<uint16_t>(axes) >> (3 * axisIndex)) & 0x7);
176}
177
178// The unit vector, in the coordinates of @p axes, that points in direction @p semantic.
179[[nodiscard]] inline Real3 GetSemanticAxis(CoordinateSpaceAxes axes, CoordinateSpaceAxes semantic) {
180 int const wanted = static_cast<int>(semantic);
181 Real3 axis{};
182 [[maybe_unused]] int matches = 0;
183 for (int i = 0; i < 3; ++i) {
184 int const direction = GetAxisDirection(axes, i);
185 // Opposite directions are adjacent, so >> 1 identifies their shared axis pair.
186 if ((direction >> 1) == (wanted >> 1)) {
187 axis[i] = direction == wanted ? 1_r : -1_r;
188 ++matches;
189 }
190 }
191 MOCHI_ASSERT_VERBOSE(matches == 1, "CoordinateSpaceAxes must name three orthogonal directions.");
192 return axis;
193}
194
195} // namespace details
196
197/** @brief Coordinate-axis convention and linear unit scale. */
200
201 // Always double precision for precision-independent debugger protocol.
202 double unitsPerMeter = 1.0;
203
204 constexpr CoordinateSpace() = default;
205 constexpr CoordinateSpace(CoordinateSpaceAxes inAxes, double inUnitsPerMeter)
206 : axes(inAxes), unitsPerMeter(inUnitsPerMeter) {}
207
208 /**
209 * @brief Validate that @ref axes is a complete convention and
210 * @ref unitsPerMeter is finite and positive.
211 */
212 void Validate(Error& error) const;
213
214 /** @brief Return the unit vector pointing right, in this space's own coordinates. */
215 [[nodiscard]] Real3 GetRight() const {
216 return details::GetSemanticAxis(axes, CoordinateSpaceAxes::Right);
217 }
218
219 /** @brief Return the unit vector pointing up, in this space's own coordinates. */
220 [[nodiscard]] Real3 GetUp() const {
221 return details::GetSemanticAxis(axes, CoordinateSpaceAxes::Up);
222 }
223
224 /** @brief Return the unit vector pointing forward, in this space's own coordinates. */
225 [[nodiscard]] Real3 GetForward() const {
226 return details::GetSemanticAxis(axes, CoordinateSpaceAxes::Forward);
227 }
228
229 /** @brief Return the default coordinate space convention for Mochi. */
230 [[nodiscard]] static CoordinateSpace Default() {
231 return {CoordinateSpaceAxes::Default, 1.0};
232 }
233
234 /** @brief Return the coordinate space convention for Filament. */
235 [[nodiscard]] static CoordinateSpace Filament() {
236 return {CoordinateSpaceAxes::RUB, 1.0};
237 }
238
239 /** @brief Return the coordinate space convention for OpenGL. */
240 [[nodiscard]] static CoordinateSpace OpenGL() {
241 return {CoordinateSpaceAxes::RUB, 1.0};
242 }
243
244 /** @brief Return the coordinate space convention for ROS */
245 [[nodiscard]] static CoordinateSpace ROS() {
246 return {CoordinateSpaceAxes::FLU, 1.0}; // Same as Default
247 }
248
249 /** @brief Return the coordinate space convention for Unity. */
250 [[nodiscard]] static CoordinateSpace Unity() {
251 return {CoordinateSpaceAxes::RUF, 1.0};
252 }
253
254 /** @brief Return the coordinate space convention for Unreal Engine. */
255 [[nodiscard]] static CoordinateSpace Unreal() {
256 return {CoordinateSpaceAxes::FRU, 100.0};
257 }
258
259#if MOCHI_LANGUAGE_CPP20
260 constexpr bool operator==(CoordinateSpace const& rhs) const = default;
261 constexpr bool operator!=(CoordinateSpace const& rhs) const = default;
262#endif
263
268};
269
270} // namespace superdex
#define MOCHI_ASSERT_VERBOSE(condition_without_side_effects,...)
Definition debug.h:102
CoordinateSpaceAxes
Combination of axis directions for a coordinate space convention.
NdArray< real, 3 > Real3
Definition nd_array.h:115
#define MOCHI_ENUM_END()
Definition reflection.h:330
#define MOCHI_STRUCT_END()
Definition reflection.h:333
#define MOCHI_ENUM_BEGIN(name)
Definition reflection.h:327
#define MOCHI_FIELD(name)
Definition reflection.h:351
#define MOCHI_STRUCT_BEGIN(name)
Definition reflection.h:332
#define MOCHI_ATTRIBUTE(...)
Definition reflection.h:357
#define MOCHI_ENUM_ITEM(name)
Definition reflection.h:328
Coordinate-axis convention and linear unit scale.
static CoordinateSpace Unreal()
Return the coordinate space convention for Unreal Engine.
static CoordinateSpace Filament()
Return the coordinate space convention for Filament.
Real3 GetForward() const
Return the unit vector pointing forward, in this space's own coordinates.
constexpr bool operator!=(CoordinateSpace const &rhs) const =default
constexpr CoordinateSpace(CoordinateSpaceAxes inAxes, double inUnitsPerMeter)
static CoordinateSpace OpenGL()
Return the coordinate space convention for OpenGL.
static CoordinateSpace ROS()
Return the coordinate space convention for ROS.
static CoordinateSpace Default()
Return the default coordinate space convention for Mochi.
static CoordinateSpace Unity()
Return the coordinate space convention for Unity.
Real3 GetRight() const
Return the unit vector pointing right, in this space's own coordinates.
void Validate(Error &error) const
Validate that axes is a complete convention and unitsPerMeter is finite and positive.
constexpr CoordinateSpace()=default
constexpr bool operator==(CoordinateSpace const &rhs) const =default
Real3 GetUp() const
Return the unit vector pointing up, in this space's own coordinates.