Ocean
MappingF.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_CV_SYNTHESIS_MAPPING_F_H
9 #define META_OCEAN_CV_SYNTHESIS_MAPPING_F_H
10 
13 
14 #include "ocean/math/Vector2.h"
15 
16 namespace Ocean
17 {
18 
19 namespace CV
20 {
21 
22 namespace Synthesis
23 {
24 
25 /**
26  * This class implements a mapping with float accuracy.
27  * @ingroup cvsynthesis
28  */
29 class MappingF : public Mapping
30 {
31  public:
32 
33  /**
34  * Destructs a mapping object.
35  */
36  inline ~MappingF() override;
37 
38  /**
39  * Returns the mapping for a given position.
40  * @param x Horizontal position to return the mapping for, with range [0, width)
41  * @param y Vertical position to return the mapping for, with range [0, height)
42  * @return Current mapping for the specified position
43  */
44  inline const Vector2& position(const unsigned int x, const unsigned int y) const;
45 
46  /**
47  * Returns the mapping for a given position.
48  * @param x Horizontal position to return the mapping for, with range [0, width)
49  * @param y Vertical position to return the mapping for, with range [0, height)
50  * @return Current mapping for the specified position
51  */
52  inline Vector2& position(const unsigned int x, const unsigned int y);
53 
54  /**
55  * Sets a new mapping for a specified position.
56  * @param x Horizontal position to set the mapping for, with range [0, width)
57  * @param y Vertical position to set the mapping for, with range [0, height)
58  * @param pixelPosition New mapping to be set
59  */
60  inline void setPosition(const unsigned int x, const unsigned int y, const Vector2& pixelPosition);
61 
62  /**
63  * Returns the pointer to a mapping row.
64  * @param y The index of the row to return, with range [0, height - 1]
65  * @return The mapping row
66  */
67  inline const Vector2* row(const unsigned int y) const;
68 
69  /**
70  * Returns the pointer to a mapping row.
71  * @param y The index of the row to return, with range [0, height - 1]
72  * @return The mapping row
73  */
74  inline Vector2* row(const unsigned int y);
75 
76  /**
77  * Resets the stored mapping.
78  */
79  inline void reset();
80 
81  /**
82  * Returns the mappings of this object.
83  * @return All mapping
84  */
85  inline const Vector2* operator()() const;
86 
87  /**
88  * Returns the mappings of this object.
89  * @return All mapping
90  */
91  inline Vector2* operator()();
92 
93  protected:
94 
95  /**
96  * Creates an empty mapping object.
97  */
98  inline MappingF();
99 
100  /**
101  * Copies a mapping from a given mapping object.
102  * @param mappingObject Pixel mapping to be copied
103  */
104  inline MappingF(const MappingF& mappingObject);
105 
106  /**
107  * Move constructor.
108  * @param mappingObject Pixel mapping to be moved
109  */
110  inline MappingF(MappingF&& mappingObject) noexcept;
111 
112  /**
113  * Creates a new mapping object width defined dimension.
114  * Beware: An initial mapping is not provided.<br>
115  * @param width The width of the mapping object in pixel, with range [1, infinity)
116  * @param height The height of the mapping object in pixel, with range [1, infinity)
117  */
118  inline MappingF(const unsigned int width, const unsigned int height);
119 
120  /**
121  * Assign operator.
122  * @param mappingObject Mapping object to be copied
123  * @return Reference to this object
124  */
125  inline MappingF& operator=(const MappingF& mappingObject);
126 
127  /**
128  * Move operator.
129  * @param mappingObject Mapping object to be moved
130  * @return Reference to this object
131  */
132  inline MappingF& operator=(MappingF&& mappingObject) noexcept;
133 
134  protected:
135 
136  /// Sub-pixel mappings for each pixel.
137  Vector2* mappingF_ = nullptr;
138 };
139 
141  Mapping(),
142  mappingF_(nullptr)
143 {
144  // nothing to do here
145 }
146 
147 inline MappingF::MappingF(const MappingF& mappingObject) :
148  Mapping(mappingObject),
149  mappingF_(nullptr)
150 {
151  const unsigned int size = width_ * height_;
152 
153  if (size != 0u)
154  {
155  mappingF_ = (Vector2*)malloc(size * sizeof(Vector2));
156  ocean_assert(mappingF_ != nullptr);
157 
158  memcpy(mappingF_, mappingObject.mappingF_, size * sizeof(Vector2));
159  }
160 }
161 
162 inline MappingF::MappingF(MappingF&& mappingObject) noexcept
163 {
164  *this = std::move(mappingObject);
165 }
166 
167 inline MappingF::MappingF(const unsigned int width, const unsigned int height) :
168  Mapping(width, height),
169  mappingF_(nullptr)
170 {
171  const unsigned int size = width * height;
172 
173  if (size != 0)
174  {
175  mappingF_ = (Vector2*)malloc(size * sizeof(Vector2));
176  ocean_assert(mappingF_ != nullptr);
177 
178 //#ifdef OCEAN_DEBUG // **TODO** currently, we set the entire mapping information to zero to ensure that we really overwrite the information layer, however that needs to be checked before removing the memset execution
179  memset(mappingF_, 0, size * sizeof(Vector2));
180 //#endif
181  }
182 }
183 
185 {
186  free(mappingF_);
187 }
188 
189 const Vector2& MappingF::position(const unsigned int x, const unsigned int y) const
190 {
191  ocean_assert(x < width_ && y < height_);
192 
193  return mappingF_[y * width_ + x];
194 }
195 
196 Vector2& MappingF::position(const unsigned int x, const unsigned int y)
197 {
198  ocean_assert(x < width_ && y < height_);
199 
200  return mappingF_[y * width_ + x];
201 }
202 
203 inline void MappingF::setPosition(const unsigned int x, const unsigned int y, const Vector2& pixelPosition)
204 {
205  ocean_assert(x < width_ && y < height_);
206 
207  mappingF_[y * width_ + x] = pixelPosition;
208 }
209 
210 inline const Vector2* MappingF::row(const unsigned int y) const
211 {
212  ocean_assert(y < height_);
213 
214  return mappingF_ + y * width_;
215 }
216 
217 inline Vector2* MappingF::row(const unsigned int y)
218 {
219  ocean_assert(y < height_);
220 
221  return mappingF_ + y * width_;
222 }
223 
224 inline void MappingF::reset()
225 {
226  ocean_assert(mappingF_);
227  memset(mappingF_, 0, sizeof(Vector2) * width_ * height_);
228 }
229 
230 inline const Vector2* MappingF::operator()() const
231 {
232  return mappingF_;
233 }
234 
236 {
237  return mappingF_;
238 }
239 
240 inline MappingF& MappingF::operator=(const MappingF& mappingObject)
241 {
242  if (this != &mappingObject)
243  {
244  Mapping::operator=(mappingObject);
245 
246  if (mappingF_)
247  {
248  free(mappingF_);
249  mappingF_ = nullptr;
250  }
251 
252  const unsigned int size = width_ * height_;
253 
254  if (size != 0u)
255  {
256  mappingF_ = (Vector2*)malloc(size * sizeof(Vector2));
257  ocean_assert(mappingF_ != nullptr);
258 
259  memcpy(mappingF_, mappingObject.mappingF_, size * sizeof(Vector2));
260  }
261  }
262 
263  return *this;
264 }
265 
266 inline MappingF& MappingF::operator=(MappingF&& mappingObject) noexcept
267 {
268  if (this != &mappingObject)
269  {
270  mappingF_ = mappingObject.mappingF_;
271  mappingObject.mappingF_ = nullptr;
272 
273  Mapping::operator=(std::move(mappingObject));
274  }
275 
276  return *this;
277 }
278 
279 }
280 
281 }
282 
283 }
284 
285 #endif // META_OCEAN_CV_SYNTHESIS_MAPPING_F_H
This class implements a mapping with float accuracy.
Definition: MappingF.h:30
const Vector2 * row(const unsigned int y) const
Returns the pointer to a mapping row.
Definition: MappingF.h:210
~MappingF() override
Destructs a mapping object.
Definition: MappingF.h:184
Vector2 * mappingF_
Sub-pixel mappings for each pixel.
Definition: MappingF.h:137
MappingF & operator=(const MappingF &mappingObject)
Assign operator.
Definition: MappingF.h:240
const Vector2 & position(const unsigned int x, const unsigned int y) const
Returns the mapping for a given position.
Definition: MappingF.h:189
const Vector2 * operator()() const
Returns the mappings of this object.
Definition: MappingF.h:230
void reset()
Resets the stored mapping.
Definition: MappingF.h:224
MappingF()
Creates an empty mapping object.
Definition: MappingF.h:140
void setPosition(const unsigned int x, const unsigned int y, const Vector2 &pixelPosition)
Sets a new mapping for a specified position.
Definition: MappingF.h:203
This class is the base class for all mappings.
Definition: Mapping.h:35
unsigned int width_
Width of this pixel mapping object in pixel.
Definition: Mapping.h:147
unsigned int width() const
Returns the width of this mapping object.
Definition: Mapping.h:282
Mapping & operator=(const Mapping &mapping)
Assign operator.
Definition: Mapping.h:407
unsigned int height_
Height of this pixel mapping object in pixel.
Definition: Mapping.h:150
unsigned int height() const
Returns the height of this mapping object.
Definition: Mapping.h:287
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15