Debian Codegraph @

modules/openxr/extensions/spatial_entities/openxr_spatial_plane_tracking.h

raw symbols SCIP
1 /**************************************************************************/
2 /* openxr_spatial_plane_tracking.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "openxr_spatial_entities.h"
34 #include "openxr_spatial_entity_extension.h"
35 #include "scene/resources/3d/shape_3d.h"
36
37 // Plane tracking capability configuration
38 class OpenXRSpatialCapabilityConfigurationPlaneTracking : public OpenXRSpatialCapabilityConfigurationBaseHeader {
39 GDCLASS(OpenXRSpatialCapabilityConfigurationPlaneTracking, OpenXRSpatialCapabilityConfigurationBaseHeader);
40
41 public:
42 virtual bool has_valid_configuration() const override;
43 virtual XrSpatialCapabilityConfigurationBaseHeaderEXT *get_configuration() override;
44
45 bool get_supports_mesh_2d();
46 bool get_supports_polygons();
47 bool get_supports_labels();
48
49 Vector<XrSpatialComponentTypeEXT> get_enabled_components() const { return plane_enabled_components; }
50
51 protected:
52 static void _bind_methods();
53
54 private:
55 int supports_mesh_2d = -1;
56 int supports_polygons = -1;
57 int supports_labels = -1;
58
59 Vector<XrSpatialComponentTypeEXT> plane_enabled_components;
60 XrSpatialCapabilityConfigurationPlaneTrackingEXT plane_config = { XR_TYPE_SPATIAL_CAPABILITY_CONFIGURATION_PLANE_TRACKING_EXT, nullptr, XR_SPATIAL_CAPABILITY_PLANE_TRACKING_EXT, 0, nullptr };
61
62 PackedInt64Array _get_enabled_components() const;
63 };
64
65 // Plane alignment component data
66 class OpenXRSpatialComponentPlaneAlignmentList : public OpenXRSpatialComponentData {
67 GDCLASS(OpenXRSpatialComponentPlaneAlignmentList, OpenXRSpatialComponentData);
68
69 public:
70 enum PlaneAlignment {
71 PLANE_ALIGNMENT_HORIZONTAL_UPWARD = XR_SPATIAL_PLANE_ALIGNMENT_HORIZONTAL_UPWARD_EXT,
72 PLANE_ALIGNMENT_HORIZONTAL_DOWNWARD = XR_SPATIAL_PLANE_ALIGNMENT_HORIZONTAL_DOWNWARD_EXT,
73 PLANE_ALIGNMENT_VERTICAL = XR_SPATIAL_PLANE_ALIGNMENT_VERTICAL_EXT,
74 PLANE_ALIGNMENT_ARBITRARY = XR_SPATIAL_PLANE_ALIGNMENT_ARBITRARY_EXT,
75 };
76
77 virtual void set_capacity(uint32_t p_capacity) override;
78 virtual XrSpatialComponentTypeEXT get_component_type() const override;
79 virtual void *get_structure_data(void *p_next) override;
80
81 XrSpatialPlaneAlignmentEXT get_plane_alignment(int64_t p_index) const;
82
83 protected:
84 static void _bind_methods();
85
86 private:
87 Vector<XrSpatialPlaneAlignmentEXT> plane_alignment_data;
88
89 XrSpatialComponentPlaneAlignmentListEXT plane_alignment_list = { XR_TYPE_SPATIAL_COMPONENT_PLANE_ALIGNMENT_LIST_EXT, nullptr, 0, nullptr };
90
91 PlaneAlignment _get_plane_alignment(int64_t p_index) const;
92 };
93
94 VARIANT_ENUM_CAST(OpenXRSpatialComponentPlaneAlignmentList::PlaneAlignment);
95
96 class OpenXRSpatialComponentPolygon2DList : public OpenXRSpatialComponentData {
97 GDCLASS(OpenXRSpatialComponentPolygon2DList, OpenXRSpatialComponentData);
98
99 protected:
100 static void _bind_methods();
101
102 public:
103 virtual void set_capacity(uint32_t p_capacity) override;
104 virtual XrSpatialComponentTypeEXT get_component_type() const override;
105 virtual void *get_structure_data(void *p_next) override;
106
107 Transform3D get_transform(int64_t p_index) const;
108 PackedVector2Array get_vertices(RID p_snapshot, int64_t p_index) const;
109
110 private:
111 Vector<XrSpatialPolygon2DDataEXT> polygon2d_data;
112
113 XrSpatialComponentPolygon2DListEXT polygon2d_list = { XR_TYPE_SPATIAL_COMPONENT_POLYGON_2D_LIST_EXT, nullptr, 0, nullptr };
114 };
115
116 // Plane semantic label component data.
117 class OpenXRSpatialComponentPlaneSemanticLabelList : public OpenXRSpatialComponentData {
118 GDCLASS(OpenXRSpatialComponentPlaneSemanticLabelList, OpenXRSpatialComponentData);
119
120 public:
121 enum PlaneSemanticLabel {
122 PLANE_SEMANTIC_LABEL_UNCATEGORIZED = XR_SPATIAL_PLANE_SEMANTIC_LABEL_UNCATEGORIZED_EXT,
123 PLANE_SEMANTIC_LABEL_FLOOR = XR_SPATIAL_PLANE_SEMANTIC_LABEL_FLOOR_EXT,
124 PLANE_SEMANTIC_LABEL_WALL = XR_SPATIAL_PLANE_SEMANTIC_LABEL_WALL_EXT,
125 PLANE_SEMANTIC_LABEL_CEILING = XR_SPATIAL_PLANE_SEMANTIC_LABEL_CEILING_EXT,
126 PLANE_SEMANTIC_LABEL_TABLE = XR_SPATIAL_PLANE_SEMANTIC_LABEL_TABLE_EXT,
127 };
128
129 virtual void set_capacity(uint32_t p_capacity) override;
130 virtual XrSpatialComponentTypeEXT get_component_type() const override;
131 virtual void *get_structure_data(void *p_next) override;
132
133 XrSpatialPlaneSemanticLabelEXT get_plane_semantic_label(int64_t p_index) const;
134
135 protected:
136 static void _bind_methods();
137
138 private:
139 Vector<XrSpatialPlaneSemanticLabelEXT> plane_semantic_label_data;
140
141 XrSpatialComponentPlaneSemanticLabelListEXT plane_semantic_label_list = { XR_TYPE_SPATIAL_COMPONENT_PLANE_SEMANTIC_LABEL_LIST_EXT, nullptr, 0, nullptr };
142
143 PlaneSemanticLabel _get_plane_semantic_label(int64_t p_index) const;
144 };
145
146 VARIANT_ENUM_CAST(OpenXRSpatialComponentPlaneSemanticLabelList::PlaneSemanticLabel);
147
148 // Plane tracker
149 class OpenXRPlaneTracker : public OpenXRSpatialEntityTracker {
150 GDCLASS(OpenXRPlaneTracker, OpenXRSpatialEntityTracker);
151
152 public:
153 void set_bounds_size(const Vector2 &p_bounds_size);
154 Vector2 get_bounds_size() const;
155
156 void set_plane_alignment(OpenXRSpatialComponentPlaneAlignmentList::PlaneAlignment p_plane_alignment);
157 OpenXRSpatialComponentPlaneAlignmentList::PlaneAlignment get_plane_alignment() const;
158
159 void set_plane_label(const String &p_plane_label);
160 String get_plane_label() const;
161
162 void set_mesh_data(const Transform3D &p_origin, const PackedVector2Array &p_vertices, const PackedInt32Array &p_indices = PackedInt32Array());
163 void clear_mesh_data();
164
165 Transform3D get_mesh_offset() const;
166 Ref<Mesh> get_mesh();
167 Ref<Shape3D> get_shape(real_t p_thickness = 0.01);
168
169 protected:
170 static void _bind_methods();
171
172 private:
173 Vector2 bounds_size;
174 OpenXRSpatialComponentPlaneAlignmentList::PlaneAlignment plane_alignment = OpenXRSpatialComponentPlaneAlignmentList::PLANE_ALIGNMENT_HORIZONTAL_UPWARD;
175 String plane_label;
176
177 // Mesh data (if we have this)
178 struct MeshData {
179 bool has_mesh_data = false;
180 Transform3D origin;
181 PackedVector2Array vertices;
182 PackedInt32Array indices;
183
184 Ref<Mesh> mesh;
185 Ref<Shape3D> shape3d;
186 } mesh;
187
188 struct Edge {
189 int32_t a;
190 int32_t b;
191 static _FORCE_INLINE_ uint32_t hash(const Edge &p_edge) {
192 uint32_t h = hash_murmur3_one_32(p_edge.a);
193 return hash_murmur3_one_32(p_edge.b, h);
194 }
195 bool operator==(const Edge &p_edge) const {
196 return (a == p_edge.a && b == p_edge.b);
197 }
198
199 Edge(int32_t p_a = 0, int32_t p_b = 0) {
200 a = p_a;
201 b = p_b;
202 if (a < b) {
203 SWAP(a, b);
204 }
205 }
206 };
207 };
208
209 // Plane tracking logic
210 class OpenXRSpatialPlaneTrackingCapability : public OpenXRExtensionWrapper {
211 GDCLASS(OpenXRSpatialPlaneTrackingCapability, OpenXRExtensionWrapper);
212
213 protected:
214 static void _bind_methods();
215
216 public:
217 static OpenXRSpatialPlaneTrackingCapability *get_singleton();
218
219 OpenXRSpatialPlaneTrackingCapability();
220 virtual ~OpenXRSpatialPlaneTrackingCapability() override;
221
222 virtual HashMap<String, bool *> get_requested_extensions(XrVersion p_version) override;
223
224 virtual void on_session_created(const XrSession p_session) override;
225 virtual void on_session_destroyed() override;
226
227 virtual void on_process() override;
228
229 bool is_supported();
230
231 private:
232 static OpenXRSpatialPlaneTrackingCapability *singleton;
233 bool spatial_plane_tracking_ext = false;
234 bool spatial_plane_tracking_supported = false;
235
236 RID spatial_context;
237 bool need_discovery = false;
238 int discovery_cooldown = 0;
239 Ref<OpenXRFutureResult> discovery_query_result;
240
241 Ref<OpenXRSpatialCapabilityConfigurationPlaneTracking> plane_configuration;
242
243 // Discovery logic
244 Ref<OpenXRFutureResult> _create_spatial_context();
245 void _on_spatial_context_created(RID p_spatial_context);
246
247 void _on_spatial_discovery_recommended(RID p_spatial_context);
248
249 Ref<OpenXRFutureResult> _start_entity_discovery();
250 void _process_snapshot(RID p_snapshot);
251
252 // Trackers
253 HashMap<XrSpatialEntityIdEXT, Ref<OpenXRPlaneTracker>> plane_trackers;
254 };