Ocean
Loading...
Searching...
No Matches
InitializerCoarserMappingAdaptionF1.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_INITIALIZER_COARSER_MAPPING_ADAPTION_F_1_H
9#define META_OCEAN_CV_SYNTHESIS_INITIALIZER_COARSER_MAPPING_ADAPTION_F_1_H
10
16
17namespace Ocean
18{
19
20namespace CV
21{
22
23namespace Synthesis
24{
25
26/**
27 * This initializer creates an initial mapping by the adaption of an already existing mapping of a coarser synthesis layer.<br>
28 * The initializer supports mapping with float accuracy.<br>
29 * The coarser mapping is upsampled and adjusted to the synthesis mask.<br>
30 * @tparam tFactor Defines the dimension increase factor between the synthesis layer and the given coarser layer. A factor of 2 means that the width and height of the synthesis layer is two times larger than the width and height of the given coarser layer, with range [0, infinity)
31 * @see MappingF, LayerF1, InitializerCoarserMappingAdaptionI1.
32 * @ingroup cvsynthesis
33 */
34template <unsigned int tFactor>
36 virtual public InitializerF,
37 virtual public InitializerRandomized,
38 virtual public InitializerSubset,
39 virtual public Initializer1
40{
41 public:
42
43 /**
44 * Creates a new initializer object.
45 * @param layer The layer for that the initial mapping has to be provided
46 * @param randomGenerator Random number generator
47 * @param coarserLayer The coarser synthesis layer from the mapping will be adopted
48 */
49 inline InitializerCoarserMappingAdaptionF1(LayerF1& layer, RandomGenerator& randomGenerator, const LayerF1& coarserLayer);
50
51 private:
52
53 /**
54 * Initializes a subset of the entire mapping area.
55 * @see InitializerSubset::initializeSubset().
56 * @see initializeSubsetChannels().
57 */
58 void initializeSubset(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const override;
59
60 private:
61
62 /// Coarser layer that has to be adopted.
64};
65
66template <unsigned int tFactor>
68 Initializer(layer),
69 InitializerF(layer),
70 InitializerRandomized(layer, randomGenerator),
71 InitializerSubset(layer),
72 Initializer1(layer),
73 coarserLayerF_(coarserLayer)
74{
75 // nothing to do here
76}
77
78template <unsigned int tFactor>
79inline void InitializerCoarserMappingAdaptionF1<tFactor>::initializeSubset(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const
80{
81 static_assert(tFactor >= 2u, "Invalid factor!");
82
83 const unsigned int width = layerF_.width();
84 const unsigned int height = layerF_.height();
85
86 const unsigned int coarserWidth = coarserLayerF_.width();
87 const unsigned int coarserHeight = coarserLayerF_.height();
88
89 ocean_assert(width / tFactor == coarserWidth);
90 ocean_assert(height / tFactor == coarserHeight);
91
92 MappingF& mapping = layerF_.mapping();
93 const MappingF& coarserMapping = coarserLayerF_.mapping();
94
95 RandomGenerator randomGenerator(randomGenerator_);
96
97 const uint8_t* const mask = layerF_.mask().template constdata<uint8_t>();
98 const uint8_t* const coarserMask = coarserLayerF_.mask().template constdata<uint8_t>();
99
100 const unsigned int maskStrideElements = layerF_.mask().strideElements();
101 const unsigned int coarserMaskStrideElements = coarserLayerF_.mask().strideElements();
102
103 for (unsigned int y = firstRow; y < firstRow + numberRows; ++y)
104 {
105 const uint8_t* maskRow = mask + y * maskStrideElements;
106 Vector2* positionRow = mapping.row(y);
107
108 const unsigned int yCoarser = min(y / tFactor, coarserHeight - 1u);
109
110 const uint8_t* coarserMaskRow = coarserMask + yCoarser * coarserMaskStrideElements;
111 const Vector2* coarserPositionRow = coarserMapping.row(yCoarser);
112
113 for (unsigned int x = firstColumn; x < firstColumn + numberColumns; ++x)
114 {
115 if (maskRow[x] != 0xFFu)
116 {
117 const unsigned int xCoarser = min(x / tFactor, coarserWidth - 1u);
118
119 // if the corresponding coarser layer pixel is a mask pixel
120 if (coarserMaskRow[xCoarser] != 0xFFu)
121 {
122 const Vector2& coarserPosition = coarserPositionRow[xCoarser];
123 ocean_assert(coarserPosition.x() < Scalar(coarserLayerF_.width()));
124 ocean_assert(coarserPosition.y() < Scalar(coarserLayerF_.height()));
125
126 const Scalar candidateX = Scalar(x) + (coarserPosition.x() - Scalar(xCoarser)) * Scalar(tFactor);
127 const Scalar candidateY = Scalar(y) + (coarserPosition.y() - Scalar(yCoarser)) * Scalar(tFactor);
128
129 const unsigned int intCandidateX = (unsigned int)(Numeric::round32(candidateX));
130 const unsigned int intCandidateY = (unsigned int)(Numeric::round32(candidateY));
131
132 if (mask[intCandidateY * maskStrideElements + intCandidateX] == 0xFFu)
133 {
134 positionRow[x] = Vector2(candidateX, candidateY);
135 continue;
136 }
137 }
138
139 Vector2 candidate;
140
141 while (true)
142 {
143 candidate = Random::vector2(randomGenerator, Scalar(2u), Scalar(width - 3u), Scalar(2u), Scalar(height - 3u));
144
145 const unsigned int intCandidateX = (unsigned int)(Numeric::round32(candidate.x()));
146 const unsigned int intCandidateY = (unsigned int)(Numeric::round32(candidate.y()));
147
148 if (mask[intCandidateY * maskStrideElements + intCandidateX] == 0xFFu)
149 {
150 positionRow[x] = candidate;
151 break;
152 }
153 }
154 }
155 }
156 }
157}
158
159}
160
161}
162
163}
164
165#endif // META_OCEAN_CV_SYNTHESIS_INITIALIZER_COARSER_MAPPING_ADAPTION_F_1_H
This class is the base class for all initializers that are provided for a single frame only.
Definition Initializer1.h:29
This initializer creates an initial mapping by the adaption of an already existing mapping of a coars...
Definition InitializerCoarserMappingAdaptionF1.h:40
const LayerF1 & coarserLayerF_
Coarser layer that has to be adopted.
Definition InitializerCoarserMappingAdaptionF1.h:63
void initializeSubset(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const override
Initializes a subset of the entire mapping area.
Definition InitializerCoarserMappingAdaptionF1.h:79
InitializerCoarserMappingAdaptionF1(LayerF1 &layer, RandomGenerator &randomGenerator, const LayerF1 &coarserLayer)
Creates a new initializer object.
Definition InitializerCoarserMappingAdaptionF1.h:67
This class implements the base class for all initializer objects that are applied for mappings with f...
Definition InitializerF.h:30
This class implements the base class for all synthesis initializers.
Definition Initializer.h:34
This class is the base class for all initializers that mainly initialize the synthesis mapping by a h...
Definition InitializerRandomized.h:30
This class is the base class for all initializer objects that can separate the initialization process...
Definition InitializerSubset.h:29
This class implements a single layer for pixel synthesis within one frame and sub-pixel accuracy.
Definition LayerF1.h:44
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
static constexpr int32_t round32(const T value)
Returns the rounded 32 bit integer value of a given value.
Definition Numeric.h:2064
This class implements a generator for random numbers.
Definition RandomGenerator.h:42
static VectorT2< T > vector2()
Returns a random 2D vector with length 1 which is equal distributed within a circle.
Definition Random.h:472
const T & x() const noexcept
Returns the x value.
Definition Vector2.h:710
const T & y() const noexcept
Returns the y value.
Definition Vector2.h:722
float Scalar
Definition of a scalar type.
Definition Math.h:129
VectorT2< Scalar > Vector2
Definition of a 2D vector.
Definition Vector2.h:28
The namespace covering the entire Ocean framework.
Definition Accessor.h:15