Blender V5.3
distortion_grid.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <cstdint>
6#include <memory>
7
8#include "BLI_hash.hh"
10
11#include "DNA_movieclip_types.h"
12#include "DNA_tracking_types.h"
13
14#include "BKE_movieclip.hh"
15#include "BKE_tracking.hh"
16
17#include "COM_context.hh"
19#include "COM_result.hh"
20#include "COM_utilities.hh"
21
22namespace blender::compositor {
23
24/* --------------------------------------------------------------------
25 * Distortion Grid Key.
26 */
27
35
45
47{
48 return BKE_tracking_camera_distortion_equal(&a.camera, &b.camera) &&
49 a.domain.data_size == b.domain.data_size &&
50 a.domain.display_size == b.domain.display_size &&
51 a.domain.data_offset == b.domain.data_offset && a.type == b.type &&
52 a.calibration_size == b.calibration_size;
53}
54
55/* --------------------------------------------------------------------
56 * Distortion Grid.
57 */
58
59/* Reduces the given function in parallel over the given range, the reduction function should have
60 * the given identity value. The given function gets as arguments the index of the element of the
61 * range as well as a reference to the value where the result should be accumulated, while the
62 * reduction function gets a reference to two values and returns their reduction. */
63template<typename Value, typename Function, typename Reduction>
64static Value parallel_reduce(const int range,
65 const Value &identity,
66 const Function &function,
67 const Reduction &reduction)
68{
70 IndexRange(range),
71 32,
72 identity,
73 [&](const IndexRange sub_range, const Value &initial_value) {
74 Value result = initial_value;
75 for (const int64_t i : sub_range) {
76 function(i, result);
77 }
78 return result;
79 },
80 reduction);
81}
82
83/* Given the domain of an image, compute its domain after distortion by the given distortion
84 * parameters. The data window of the domain will likely grow or shrink depending on the
85 * distortion, while the display window will stay the same. */
87 const int2 &calibration_size,
88 const DistortionType &type,
89 const Domain &domain)
90{
91 auto distortion_function = [&](const float2 &coordinates) {
92 /* We are looping over the data space, so transfer to the display space by adding the data
93 * offset. Finally, transform to the calibration space since this is what the distortion
94 * functions expect. */
95 const float2 display_coordinates = coordinates + float2(domain.data_offset);
96 const float2 normalized_coordinates = display_coordinates / float2(domain.display_size);
97 const float2 calibrated_coordinates = normalized_coordinates * float2(calibration_size);
98
99 float2 distorted_coordinates;
100 if (type == DistortionType::Undistort) {
102 distortion, calibrated_coordinates, distorted_coordinates);
103 }
104 else {
106 distortion, calibrated_coordinates, distorted_coordinates);
107 }
108
109 /* Undo the space transformations into the data space and finally into the normalized sampling
110 * coordinates. */
111 const float2 distorted_normalized_coordinates = distorted_coordinates /
112 float2(calibration_size);
113 const float2 distorted_display_coordinates = distorted_normalized_coordinates *
114 float2(domain.display_size);
115 return distorted_display_coordinates;
116 };
117
118 /* Maximum distorted x location along the right edge of the image. */
119 const float maximum_x = parallel_reduce(
120 domain.data_size.y + 1,
121 std::numeric_limits<float>::lowest(),
122 [&](const int i, float &accumulated_value) {
123 const float2 position = float2(domain.data_size.x, i);
124 accumulated_value = math::max(accumulated_value, distortion_function(position).x);
125 },
127
128 /* Minimum distorted x location along the left edge of the image. */
129 const float minimum_x = parallel_reduce(
130 domain.data_size.y + 1,
131 std::numeric_limits<float>::max(),
132 [&](const int i, float &accumulated_value) {
133 const float2 position = float2(0.0f, i);
134 accumulated_value = math::min(accumulated_value, distortion_function(position).x);
135 },
137
138 /* Minimum distorted y location along the bottom edge of the image. */
139 const float minimum_y = parallel_reduce(
140 domain.data_size.x + 1,
141 std::numeric_limits<float>::max(),
142 [&](const int i, float &accumulated_value) {
143 const float2 position = float2(i, 0.0f);
144 accumulated_value = math::min(accumulated_value, distortion_function(position).y);
145 },
147
148 /* Maximum distorted y location along the top edge of the image. */
149 const float maximum_y = parallel_reduce(
150 domain.data_size.x + 1,
151 std::numeric_limits<float>::lowest(),
152 [&](const int i, float &accumulated_value) {
153 const float2 position = float2(i, domain.data_size.y);
154 accumulated_value = math::max(accumulated_value, distortion_function(position).y);
155 },
157
158 /* Compute the deltas from the image edges to the maximum/minimum distorted location along the
159 * direction of that edge. */
160 const float2 lower_left_delta = float2(0.0f) - float2(minimum_x, minimum_y);
161 const float2 upper_right_delta = float2(maximum_x, maximum_y) - float2(domain.data_size);
162
163 /* Rounds the deltas away from zero and clamp to the size to avoid excessive sizes in case of
164 * extreme distortion. */
165 const int2 lower_left_offset = math::min(domain.data_size, int2(math::ceil(lower_left_delta)));
166 const int2 upper_right_offset = math::min(domain.data_size, int2(math::ceil(upper_right_delta)));
167
168 /* Grow/Shrink the domain of the image according to the offsets and set the data offset
169 * accordingly. */
170 Domain output_domain = domain;
171 output_domain.data_size = domain.data_size + lower_left_offset + upper_right_offset;
172 output_domain.data_offset = -lower_left_offset;
173 return output_domain;
174}
175
177 MovieClip *movie_clip,
178 Domain domain,
179 DistortionType type,
180 int2 calibration_size)
181 : result(context.create_result(ResultType::Float2, ResultPrecision::Full))
182{
184 &movie_clip->tracking, calibration_size.x, calibration_size.y);
185
186 const Domain output_domain = compute_output_domain(distortion, calibration_size, type, domain);
187 Result distortion_grid_cpu = context.create_result(ResultType::Float2, ResultPrecision::Full);
188 distortion_grid_cpu.allocate_texture(output_domain, false, ResultStorageType::CPUImage);
189
190 parallel_for(distortion_grid_cpu.domain().data_size, [&](const int2 texel) {
191 /* We are looping over the data space, so transfer to the display space by adding the data
192 * offset. Add 0.5 to distort at the pixel centers. Finally, transform to the calibration space
193 * since this is what the distortion functions expect. */
194 const float2 display_coordinates = float2(texel + output_domain.data_offset) + 0.5f;
195 const float2 normalized_coordinates = display_coordinates / float2(domain.display_size);
196 const float2 calibrated_coordinates = normalized_coordinates * float2(calibration_size);
197
198 /* Notice that if we are undistorting the image, we need to distort the coordinates space and
199 * vice versa, hence the inverted condition. */
200 float2 distorted_coordinates;
201 if (type == DistortionType::Undistort) {
202 BKE_tracking_distortion_distort_v2(
203 distortion, calibrated_coordinates, distorted_coordinates);
204 }
205 else {
206 BKE_tracking_distortion_undistort_v2(
207 distortion, calibrated_coordinates, distorted_coordinates);
208 }
209
210 /* Undo the space transformations into the data space and finally into the normalized sampling
211 * coordinates. */
212 const float2 distorted_normalized_coordinates = distorted_coordinates /
213 float2(calibration_size);
214 const float2 distorted_display_coordinates = distorted_normalized_coordinates *
215 float2(domain.display_size);
216 const float2 distorted_data_coordinates = distorted_display_coordinates -
217 float2(domain.data_offset);
218 const float2 sampling_coordinates = distorted_data_coordinates / float2(domain.data_size);
219 distortion_grid_cpu.store_pixel(texel, sampling_coordinates);
220 });
221
223
224 if (context.use_gpu()) {
225 Result distortion_grid_gpu = distortion_grid_cpu.upload_to_gpu(false);
226 this->result.share_data(distortion_grid_gpu);
227 distortion_grid_gpu.release();
228 }
229 else {
230 this->result.share_data(distortion_grid_cpu);
231 }
232
233 distortion_grid_cpu.release();
234}
235
237{
238 this->result.release();
239}
240
241/* --------------------------------------------------------------------
242 * Distortion Grid Container.
243 */
244
246{
247 /* First, delete all resources that are no longer needed. */
248 map_.remove_if([](auto item) { return !item.value->needed; });
249
250 /* Second, reset the needed status of the remaining resources to false to ready them to track
251 * their needed status for the next evaluation. */
252 for (auto &value : map_.values()) {
253 value->needed = false;
254 }
255}
256
257static int2 get_movie_clip_size(MovieClip *movie_clip, int frame_number)
258{
259 MovieClipUser user = {};
260 BKE_movieclip_user_set_frame(&user, frame_number);
261
262 int2 size;
263 BKE_movieclip_get_size(movie_clip, &user, &size.x, &size.y);
264
265 return size;
266}
267
269 Context &context, MovieClip *movie_clip, Domain domain, DistortionType type, int frame_number)
270{
271 const int2 calibration_size = get_movie_clip_size(movie_clip, frame_number);
272
273 const DistortionGridKey key(movie_clip->tracking.camera, domain, type, calibration_size);
274
275 auto &distortion_grid = *map_.lookup_or_add_cb(key, [&]() {
276 return std::make_unique<DistortionGrid>(context, movie_clip, domain, type, calibration_size);
277 });
278
279 distortion_grid.needed = true;
280 return distortion_grid.result;
281}
282
283} // namespace blender::compositor
constexpr IndexRange()=default
Result & get(Context &context, MovieClip *movie_clip, Domain domain, DistortionType type, int frame_number)
DistortionGridKey(const MovieTrackingCamera &camera, Domain domain, DistortionType type, int2 calibration_size)
DistortionGrid(Context &context, MovieClip *movie_clip, Domain domain, DistortionType type, int2 calibration_size)
Result upload_to_gpu(const bool from_pool) const
Definition result.cc:671
void store_pixel(const int2 &texel, const T &pixel_value)
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
Definition result.cc:554
static Value parallel_reduce(const int2 range, const Value &identity, const Function &function, const Reduction &reduction)
bool operator==(const BokehKernelKey &a, const BokehKernelKey &b)
static Domain compute_output_domain(MovieDistortion *distortion, const int2 &calibration_size, const DistortionType &type, const Domain &domain)
static int2 get_movie_clip_size(MovieClip *movie_clip, int frame_number)
void parallel_for(const int2 range, const Function &function)
T min(const T &a, const T &b)
T ceil(const T &a)
T max(const T &a, const T &b)
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
bool BKE_tracking_camera_distortion_equal(const MovieTrackingCamera *a, const MovieTrackingCamera *b)
Definition tracking.cc:2203
void BKE_movieclip_get_size(MovieClip *clip, const MovieClipUser *user, int *r_width, int *r_height)
void BKE_tracking_distortion_distort_v2(MovieDistortion *distortion, const float co[2], float r_co[2])
Definition tracking.cc:2384
void BKE_tracking_distortion_undistort_v2(MovieDistortion *distortion, const float co[2], float r_co[2])
Definition tracking.cc:2402
void BKE_movieclip_user_set_frame(MovieClipUser *user, int framenr)
MovieDistortion * BKE_tracking_distortion_new(MovieTracking *tracking, int calibration_width, int calibration_height)
Definition tracking.cc:2268
void BKE_tracking_distortion_free(MovieDistortion *distortion)
Definition tracking.cc:2414
uint64_t BKE_tracking_camera_distortion_hash(const MovieTrackingCamera *camera)
Definition tracking.cc:2234
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
struct MovieTracking tracking
MovieTrackingCamera camera