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