Ocean
VRVisualizer.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_META_QUEST_APPLICATION_VR_VISUALIZER_H
9 #define META_OCEAN_PLATFORM_META_QUEST_APPLICATION_VR_VISUALIZER_H
10 
12 
13 #include "ocean/base/Timestamp.h"
14 
15 #include "ocean/math/Numeric.h"
16 #include "ocean/math/Vector2.h"
17 
18 #include "ocean/rendering/Engine.h"
20 
21 namespace Ocean
22 {
23 
24 namespace Platform
25 {
26 
27 namespace Meta
28 {
29 
30 namespace Quest
31 {
32 
33 namespace Application
34 {
35 
36 /**
37  * This class implements the base class for all VR visualizers allowing to visualize e.g., images or text in an Ocean-based VR application (e.g., VRNativeApplication).
38  * The visualize allows to place the visuals at arbitrary locations in the 3D environment, to update the visuals, or to remove them again.
39  * @see VRNativeApplication.
40  * @ingroup platformmetaquestapplication
41  */
43 {
44  public:
45 
46  /**
47  * Definition of a size object allowing to specify either width and height, or only width, or only height.
48  */
49  class ObjectSize
50  {
51  public:
52 
53  /**
54  * Creates a new size object with invalid value.
55  */
56  inline ObjectSize();
57 
58  /**
59  * Creates a new size object
60  * The size object can be specified with either width and height, or only width, or only height.<br>
61  * In case width and height is specified, the resulting visualization will have the desired width and height in object space (regardless of the actual aspect ratio of the object to visualize).<br>
62  * In case either width or height is specified, the resulting visualization will have either the desired width or height in object space (and will preserve the aspect ratio of the object to visualize).
63  * @param width The width of the object, with range [0, infinity)
64  * @param height The height of the object, with range [0, infinity)
65  */
66  inline ObjectSize(const Scalar width, const Scalar height = 0);
67 
68  /**
69  * Returns the width value.
70  * @return The object width
71  */
72  inline Scalar width() const;
73 
74  /**
75  * Returns the width value.
76  * @return The object width
77  */
78  inline Scalar height() const;
79 
80  /**
81  * Returns the desired extent in object space for a given reference width and height (mainly for the reference aspect ratio).
82  * The resulting extent will preserve the aspect ratio of the reference values as long as either width or height is specified in this size object.
83  * @param referenceWidth The width of the reference object for which the desired extent will be determined, with range (0, infinity)
84  * @param referenceHeight The height of the reference object for which the desired extent will be determined, with range (0, infinity)
85  */
86  inline Vector2 size(const Scalar referenceWidth, const Scalar referenceHeight) const;
87 
88  /**
89  * Returns whether this object has at least one value size value.
90  * @return True, if so
91  */
92  inline bool isValid() const;
93 
94  protected:
95 
96  /// The width, if any.
98 
99  /// the object height, if any.
101  };
102 
103  public:
104 
105  /**
106  * Returns whether the visualizer has been initialized correctly and thus can be used.
107  * @return True, if so
108  */
109  inline bool isValid() const;
110 
111  /**
112  * Returns whether the visualizer has been initialized correctly and thus can be used.
113  * @return True, if so
114  */
115  explicit inline operator bool() const;
116 
117  protected:
118 
119  /**
120  * Default constructor, creates a new invalid visualizer.
121  */
122  inline VRVisualizer() = default;
123 
124  /**
125  * Default destructor.
126  */
127  virtual ~VRVisualizer() = default;
128 
129  /**
130  * Creates a new text visualizer and initializes the object with a given rendering engine and associated framebuffer.
131  * Rendering engine and framebuffer are necessary so that the rendering objects (like Scene, Transform, Texture2D) can be created and attached to the existing rendering objects.
132  * @param engine The rendering engine to be used, must be valid
133  * @param framebuffer The framebuffer to be used, must be valid
134  */
135  inline VRVisualizer(const Rendering::EngineRef& engine, const Rendering::FramebufferRef framebuffer);
136 
137  /**
138  * Disable the copy constructor
139  * @param vrVisualizer The other instance that would have been copied to this instance
140  */
141  VRVisualizer(const VRVisualizer& vrVisualizer) = delete;
142 
143  /**
144  * Move constructor
145  * @param vrVisualizer The other instance that will be moved to this instance
146  */
147  inline VRVisualizer(VRVisualizer&& vrVisualizer);
148 
149  /**
150  * Disable the copy assignment operator
151  * @param vrVisualizer The other instance that would have been copied to this instance
152  */
153  VRVisualizer& operator=(const VRVisualizer& vrVisualizer) = delete;
154 
155  /**
156  * The move assignment operator
157  * @param vrVisualizer The other instance that will be moved to this instance
158  */
159  inline VRVisualizer& operator=(VRVisualizer&& vrVisualizer);
160 
161  protected:
162 
163  /// The rendering engine to be used for visualization
165 
166  /// The rendering framebuffer to be used for visualization.
168 
169  /// The visualizer's lock.
170  mutable Lock lock_;
171 };
172 
174  width_(0),
175  height_(0)
176 {
177  ocean_assert(!isValid());
178 }
179 
180 inline VRVisualizer::ObjectSize::ObjectSize(const Scalar width, const Scalar height) :
181  width_(width),
182  height_(height)
183 {
184  ocean_assert(width_ >= 0);
185  ocean_assert(height_ >= 0);
186 }
187 
189 {
190  return width_;
191 }
192 
194 {
195  return height_;
196 }
197 
198 inline Vector2 VRVisualizer::ObjectSize::size(const Scalar referenceWidth, const Scalar referenceHeight) const
199 {
200  ocean_assert(isValid());
201  ocean_assert(referenceWidth > Numeric::eps() && referenceHeight > Numeric::eps());
202 
203  if (Numeric::isNotEqualEps(width_) && Numeric::isNotEqualEps(height_))
204  {
205  return Vector2(width_, height_);
206  }
207 
208  const Scalar referenceAspectRatio = Numeric::ratio(referenceWidth, referenceHeight);
209 
210  if (Numeric::isNotEqualEps(width_))
211  {
212  ocean_assert(Numeric::isNotEqualEps(referenceAspectRatio));
213  return Vector2(width_, width_ / referenceAspectRatio);
214  }
215  else
216  {
217  return Vector2(height_ * referenceAspectRatio, height_);
218  }
219 }
220 
222 {
223  return Numeric::isNotEqualEps(width_) || Numeric::isNotEqualEps(height_);
224 }
225 
227  engine_(engine),
228  framebuffer_(framebuffer)
229 {
230  ocean_assert(engine_ && framebuffer_);
231 }
232 
233 inline VRVisualizer::VRVisualizer(VRVisualizer&& vrVisualizer) :
234  VRVisualizer()
235 {
236  *this = std::move(vrVisualizer);
237 }
238 
240 {
241  if (this != &vrVisualizer)
242  {
243  engine_ = std::move(vrVisualizer.engine_);
244  framebuffer_ = std::move(vrVisualizer.framebuffer_);
245  }
246 
247  return *this;
248 };
249 
250 inline bool VRVisualizer::isValid() const
251 {
252  return engine_ && framebuffer_;
253 }
254 
255 inline VRVisualizer::operator bool() const
256 {
257  return isValid();
258 }
259 
260 }
261 
262 }
263 
264 }
265 
266 }
267 
268 }
269 
270 #endif // META_OCEAN_PLATFORM_META_QUEST_APPLICATION_VR_VISUALIZER_H
This class implements a recursive lock object.
Definition: Lock.h:31
static constexpr T eps()
Returns a small epsilon.
static constexpr T ratio(const T nominator, const T denominator, const T fallback=T(1))
Returns the ratio between two values if the denominator is not equal a small epsilon.
Definition: Numeric.h:2076
static constexpr bool isNotEqualEps(const T value)
Returns whether a value is not smaller than or equal to a small epsilon.
Definition: Numeric.h:2237
Definition of a size object allowing to specify either width and height, or only width,...
Definition: VRVisualizer.h:50
Vector2 size(const Scalar referenceWidth, const Scalar referenceHeight) const
Returns the desired extent in object space for a given reference width and height (mainly for the ref...
Definition: VRVisualizer.h:198
bool isValid() const
Returns whether this object has at least one value size value.
Definition: VRVisualizer.h:221
ObjectSize()
Creates a new size object with invalid value.
Definition: VRVisualizer.h:173
Scalar height_
the object height, if any.
Definition: VRVisualizer.h:100
Scalar width() const
Returns the width value.
Definition: VRVisualizer.h:188
Scalar height() const
Returns the width value.
Definition: VRVisualizer.h:193
Scalar width_
The width, if any.
Definition: VRVisualizer.h:97
This class implements the base class for all VR visualizers allowing to visualize e....
Definition: VRVisualizer.h:43
VRVisualizer(const VRVisualizer &vrVisualizer)=delete
Disable the copy constructor.
Rendering::EngineRef engine_
The rendering engine to be used for visualization.
Definition: VRVisualizer.h:164
virtual ~VRVisualizer()=default
Default destructor.
VRVisualizer()=default
Default constructor, creates a new invalid visualizer.
Lock lock_
The visualizer's lock.
Definition: VRVisualizer.h:170
VRVisualizer & operator=(const VRVisualizer &vrVisualizer)=delete
Disable the copy assignment operator.
bool isValid() const
Returns whether the visualizer has been initialized correctly and thus can be used.
Definition: VRVisualizer.h:250
Rendering::WindowFramebufferRef framebuffer_
The rendering framebuffer to be used for visualization.
Definition: VRVisualizer.h:167
float Scalar
Definition of a scalar type.
Definition: Math.h:128
VectorT2< Scalar > Vector2
Definition of a 2D vector.
Definition: Vector2.h:21
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15