59 template <
typename Scalar>
60 using Vector2 = std::array<Scalar, 2>;
62 template <
unsigned int N,
typename T>
63 using RegularGrid = MishaK::RegularGrid<N, T>;
66 unsigned int left = 0;
67 unsigned int right = 0;
68 unsigned int bottom = 0;
72 unsigned int width()
const {
return left + right; }
73 unsigned int height()
const {
return bottom + top; }
75 template <
typename Scalar>
77 init(
unsigned int width,
unsigned int height,
span<
const std::array<Scalar, 2>> texcoords)
80 Vector2<Scalar> pix_min_corner{
Scalar(0.5) / width,
Scalar(0.5) / height};
81 Vector2<Scalar> pix_max_corner{
Scalar(width - 0.5) / width,
Scalar(height - 0.5) / height};
83 Vector2<Scalar> tex_min_corner{texcoords[0][0], texcoords[0][1]};
84 Vector2<Scalar> tex_max_corner{texcoords[0][0], texcoords[0][1]};
85 for (
size_t i = 0; i < texcoords.size(); i++) {
86 for (
int c = 0; c < 2; c++) {
87 tex_min_corner[c] = std::min<Scalar>(tex_min_corner[c], texcoords[i][c]);
89 for (
int c = 0; c < 2; c++) {
90 tex_max_corner[c] = std::max<Scalar>(tex_max_corner[c], texcoords[i][c]);
94 "Texture coordinate bounding box : Min ({}, {}). Max ({}, {}). SafeMin ({}, {}). "
105 padding.left = tex_min_corner[0] < pix_min_corner[0]
106 ? (int)ceil((pix_min_corner[0] - tex_min_corner[0]) * width)
108 padding.bottom = tex_min_corner[1] < pix_min_corner[1]
109 ? (int)ceil((pix_min_corner[1] - tex_min_corner[1]) * height)
112 padding.right = tex_max_corner[0] > pix_max_corner[0]
113 ? (int)ceil((tex_max_corner[0] - pix_max_corner[0]) * width)
115 padding.top = tex_max_corner[1] > pix_max_corner[1]
116 ? (int)ceil((tex_max_corner[1] - pix_max_corner[1]) * height)
121 int new_width = width + padding.left + padding.right;
122 int new_height = height + padding.bottom + padding.top;
124 int padded_width = 8 * (((new_width - 1) / 8) + 1);
125 int padded_height = 8 * (((new_height - 1) / 8) + 1);
126 padding.left += (padded_width - new_width);
127 padding.bottom += (padded_height - new_height);
130 if (padding.width() || padding.height()) {
132 "Padding applied : Left {}. Right {}. Bottom {}. Top {}.",
138 logger().debug(
"No padding required!");
146 template <
typename DataType>
147 void pad(RegularGrid<2, DataType>& im)
const
149 if (!(left || right || bottom || top))
return;
151 unsigned int new_width = im.res(0) + left + right;
152 unsigned int new_height = im.res(1) + bottom + top;
154 RegularGrid<2, DataType> new_im;
155 new_im.resize(new_width, new_height);
156 for (
unsigned int i = 0; i < new_width; i++)
157 for (
unsigned int j = 0; j < new_height; j++) {
159 std::min<int>(std::max<int>(0, (
int)i - (
int)left), im.res(0) - 1);
161 std::min<int>(std::max<int>(0, (
int)j - (
int)bottom), im.res(1) - 1);
162 new_im(i, j) = im(ni, nj);
168 template <
class DataType>
169 void unpad(RegularGrid<2, DataType>& im)
const
171 if (!(left || right || bottom || top))
return;
173 unsigned int output_width = im.res(0) - left - right;
174 unsigned int output_height = im.res(1) - bottom - top;
175 RegularGrid<2, DataType> new_im;
176 new_im.resize(output_width, output_height);
177 for (
unsigned int i = 0; i < output_width; i++) {
178 for (
unsigned int j = 0; j < output_height; j++) {
179 new_im(i, j) = im(left + i, bottom + j);
185 template <
typename Scalar>
186 void pad(
int width,
int height,
span<std::array<Scalar, 2>> texcoords)
const
188 if (!(left || right || bottom || top))
return;
190 int new_width = width + left + right;
191 int new_height = height + bottom + top;
193 for (
size_t i = 0; i < texcoords.size(); i++) {
194 texcoords[i][0] = (texcoords[i][0] * width + (
Scalar)(left)) / new_width;
195 texcoords[i][1] = (texcoords[i][1] * height + (
Scalar)(bottom)) / new_height;
199 template <
typename Scalar>
200 void unpad(
int width,
int height,
span<std::array<Scalar, 2>> texcoords)
const
202 if (!(left || right || bottom || top))
return;
204 int new_width = width + left + right;
205 int new_height = height + bottom + top;
207 for (
size_t i = 0; i < texcoords.size(); i++) {
208 texcoords[i][0] = (texcoords[i][0] * new_width - (
Scalar)(left)) / width;
209 texcoords[i][1] = (texcoords[i][1] * new_height - (
Scalar)(bottom)) / height;