Ocean
AKSceneTracker6DOF.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_DEVICES_ARKIT_AK_SCENE_TRACKER_6_DOF_H
9 #define META_OCEAN_DEVICES_ARKIT_AK_SCENE_TRACKER_6_DOF_H
10 
13 
14 #include "ocean/base/StringApple.h"
15 
18 
19 namespace Ocean
20 {
21 
22 namespace Devices
23 {
24 
25 namespace ARKit
26 {
27 
28 /**
29  * This class implements the 6DOF scene tracker.
30  * @ingroup devicesarkit
31  */
32 class OCEAN_DEVICES_ARKIT_EXPORT AKSceneTracker6DOF :
33  virtual public AKDevice,
34  virtual public SceneTracker6DOF,
35  virtual public VisualTracker
36 {
37  friend class AKFactory;
38 
39  protected:
40 
41  /**
42  * Definition of an unordered map mapping anchor identifier strings to unique ids.
43  */
44  using IdentifierMap = std::unordered_map<std::string, Index32>;
45 
46  /**
47  * Helper class implementing a hash function for ARMeshAnchor.
48  */
50  {
51  /**
52  * Hash function returning a hash value for an ARMeshAnchor object
53  * @param anchor The anchor for which the hash will be returned
54  * @return The resulting hash value
55  */
56  inline size_t operator()(const ARMeshAnchor* anchor) const;
57  };
58 
59  /**
60  * Definition of an unordered set holding ARMeshAnchor objects.
61  */
62  using ARMeshAnchorSet = std::unordered_set<ARMeshAnchor*, ARMeshAnchorHash>;
63 
64  public:
65 
66  /**
67  * Starts the device.
68  * @see Device::start().
69  */
70  bool start() override;
71 
72  /**
73  * Pauses the device.
74  * @see Device::pause().
75  */
76  bool pause() override;
77 
78  /**
79  * Stops the device.
80  * @see Device::stop().
81  */
82  bool stop() override;
83 
84  /**
85  * Sets the multi-view visual input of this tracker.
86  * @see VisualTracker::setInput().
87  */
88  void setInput(Media::FrameMediumRefs&& frameMediums) override;
89 
90  /**
91  * Returns whether a specific object is currently actively tracked by this tracker.
92  * @see Tracker::isObjectTracked().
93  */
94  bool isObjectTracked(const ObjectId& objectId) const override;
95 
96  /**
97  * Event function for a new 6DOF pose.
98  * @param world_T_camera The transformation between camera and world, invalid if unknown/lost
99  * @param world_T_rotatedWorld The optional transformation between ARKit's rotated world and the standard world, should only be identity or the flip matrix around y-axis
100  * @param timestamp The timestamp of the new transformation
101  * @param arFrame The current ARFRame object containing additional data for the sample, must be valid
102  */
103  API_AVAILABLE(ios(11.3))
104  void onNewSample(const HomogenousMatrix4& world_T_camera, const HomogenousMatrix4& world_T_rotatedWorld, const Timestamp& timestamp, ARFrame* arFrame);
105 
106  /**
107  * Event function for a new 6DOF pose.
108  * @param world_T_camera The transformation between camera and world, invalid if unknown/lost
109  * @param sceneElements The scene elements which are part of the sample, at least one
110  * @param timestamp The timestamp of the new transformation
111  * @param metadata The metadata of the sample
112  */
113  void onNewSample(const HomogenousMatrix4& world_T_camera, SharedSceneElements&& sceneElements, const Timestamp& timestamp, Metadata&& metadata);
114 
115  /**
116  * Event function for added anchors.
117  * @see AKDevice::onAddedAnchors().
118  */
119  void onAddedAnchors(const ARAnchors& anchors) override;
120 
121  /**
122  * Event function for updated anchors.
123  * @see AKDevice::onUpdateAnchors().
124  */
125  void onUpdateAnchors(const ARAnchors& anchors) override;
126 
127  /**
128  * Returns the name of this tracker.
129  * @return The trackers's name
130  */
131  static inline std::string deviceNameAKSceneTracker6DOF();
132 
133  /**
134  * Returns the device type of this tracker.
135  * @return The tracker's device type
136  */
137  static inline DeviceType deviceTypeAKSceneTracker6DOF();
138 
139  protected:
140 
141  /**
142  * Creates a new 6DOF scene tracker.
143  */
144  explicit AKSceneTracker6DOF();
145 
146  /**
147  * Destructs this 6DOF tracker.
148  */
149  ~AKSceneTracker6DOF() override;
150 
151  /**
152  * Exracts the 3D vectors from a geometry source.
153  * @param geometrySource The geometry source from wich all vectors will be extracted
154  * @param vectors The resulting 3D vectors
155  * @return True, if succeeded
156  */
157  API_AVAILABLE(ios(13.4))
158  static bool extractVectors3(ARGeometrySource* geometrySource, Vectors3& vectors);
159 
160  /**
161  * Extracts the indices from a geometry element.
162  * @param geometryElement The geometry elemnt from which the indices will be extracted
163  * @param indices The resulting indices
164  * @return True, if succeeded
165  */
166  API_AVAILABLE(ios(13.4))
167  static bool extractIndices(ARGeometryElement* geometryElement, Indices32& indices);
168 
169  protected:
170 
171  /// The unique id for the world object.
172  ObjectId worldObjectId_ = invalidObjectId();
173 
174  /// True, if the tracker has been started.
175  bool isStarted_ = false;
176 
177  /// True, if the world object is currently tracked.
178  bool worldIsTracked_ = false;
179 
180  /// The map mapping unique plane identifier strings to mesh ids.
181  IdentifierMap identifierMap_;
182 
183  /// The counter for unique mesh ids.
184  unsigned int meshIdCounter_ = 0u;
185 
186  /// The set holding all updated ARMeshAnchor objects.
187  ARMeshAnchorSet updatedMeshAnchors_;
188 };
189 
190 inline size_t AKSceneTracker6DOF::ARMeshAnchorHash::operator()(const ARMeshAnchor* anchor) const
191 {
192  return std::hash<std::string>()(StringApple::toUTF8(anchor.identifier.UUIDString));
193 }
194 
196 {
197  return std::string("ARKit 6DOF Scene Tracker");
198 }
199 
201 {
202  return DeviceType(deviceTypeTracker6DOF(), TRACKER_VISUAL | SCENE_TRACKER_6DOF);
203 }
204 
205 }
206 
207 }
208 
209 }
210 
211 #endif // META_OCEAN_DEVICES_ARKIT_AK_SCENE_TRACKER_6_DOF_H
This class implements a device for the ARKit library.
Definition: AKDevice.h:48
std::vector< ARAnchor * > ARAnchors
Definition of a vector holding ARAnchors.
Definition: AKDevice.h:75
This class implements a device factory for the ARKit tracking library.
Definition: AKFactory.h:32
This class implements the 6DOF scene tracker.
Definition: AKSceneTracker6DOF.h:36
std::unordered_set< ARMeshAnchor *, ARMeshAnchorHash > ARMeshAnchorSet
Definition of an unordered set holding ARMeshAnchor objects.
Definition: AKSceneTracker6DOF.h:62
bool start() override
Starts the device.
std::unordered_map< std::string, Index32 > IdentifierMap
Definition of an unordered map mapping anchor identifier strings to unique ids.
Definition: AKSceneTracker6DOF.h:44
bool pause() override
Pauses the device.
bool stop() override
Stops the device.
static DeviceType deviceTypeAKSceneTracker6DOF()
Returns the device type of this tracker.
Definition: AKSceneTracker6DOF.h:200
API_AVAILABLE(ios(11.3)) void onNewSample(const HomogenousMatrix4 &world_T_camera
Event function for a new 6DOF pose.
void setInput(Media::FrameMediumRefs &&frameMediums) override
Sets the multi-view visual input of this tracker.
static std::string deviceNameAKSceneTracker6DOF()
Returns the name of this tracker.
Definition: AKSceneTracker6DOF.h:195
bool isObjectTracked(const ObjectId &objectId) const override
Returns whether a specific object is currently actively tracked by this tracker.
Definition of a class holding the major and minor device type.
Definition: devices/Device.h:62
unsigned int ObjectId
Definition of an object id.
Definition: Measurement.h:46
std::unordered_map< std::string, Value > Metadata
Definition of an unordered map mapping keys to values.
Definition: Measurement.h:61
This class implements the base for all 6-DOF scene trackers.
Definition: SceneTracker6DOF.h:42
std::vector< SharedSceneElement > SharedSceneElements
Definition of a vector holding scene elements.
Definition: SceneTracker6DOF.h:901
This class is the base class for all tracker using visual input to create the tracking results.
Definition: devices/VisualTracker.h:41
static std::string toUTF8(NSString *object)
Converts a given NSString object to a string with UTF8 encoding.
This class implements a timestamp.
Definition: Timestamp.h:36
std::vector< Index32 > Indices32
Definition of a vector holding 32 bit index values.
Definition: Base.h:96
std::vector< Vector3 > Vectors3
Definition of a vector holding Vector3 objects.
Definition: Vector3.h:65
std::vector< FrameMediumRef > FrameMediumRefs
Definition of a vector holding frame medium reference objects.
Definition: FrameMedium.h:46
const ObjectId invalidObjectId
Definition of an invalid object id.
Definition: rendering/Rendering.h:65
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15
Helper class implementing a hash function for ARMeshAnchor.
Definition: AKSceneTracker6DOF.h:50