Merge branch 'blender-v2.91-release'
[blender.git] / source / blender / blenkernel / BKE_tracking.h
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2011 Blender Foundation.
17  * All rights reserved.
18  */
19
20 #pragma once
21
22 /** \file
23  * \ingroup bke
24  */
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 struct Camera;
31 struct ImBuf;
32 struct ListBase;
33 struct MovieClipUser;
34 struct MovieDistortion;
35 struct MovieReconstructContext;
36 struct MovieTracking;
37 struct MovieTrackingMarker;
38 struct MovieTrackingObject;
39 struct MovieTrackingPlaneMarker;
40 struct MovieTrackingPlaneTrack;
41 struct MovieTrackingTrack;
42 struct Object;
43 struct Scene;
44 struct bGPDlayer;
45 struct rcti;
46
47 /* **** Common functions **** */
48
49 void BKE_tracking_free(struct MovieTracking *tracking);
50 void BKE_tracking_copy(struct MovieTracking *tracking_dst,
51                        const struct MovieTracking *tracking_src,
52                        const int flag);
53
54 void BKE_tracking_settings_init(struct MovieTracking *tracking);
55
56 struct ListBase *BKE_tracking_get_active_tracks(struct MovieTracking *tracking);
57 struct ListBase *BKE_tracking_get_active_plane_tracks(struct MovieTracking *tracking);
58 struct MovieTrackingReconstruction *BKE_tracking_get_active_reconstruction(
59     struct MovieTracking *tracking);
60
61 /* matrices for constraints and drawing */
62 void BKE_tracking_get_camera_object_matrix(struct Object *camera_object, float mat[4][4]);
63 void BKE_tracking_get_projection_matrix(struct MovieTracking *tracking,
64                                         struct MovieTrackingObject *object,
65                                         int framenr,
66                                         int winx,
67                                         int winy,
68                                         float mat[4][4]);
69
70 /* **** Clipboard **** */
71 void BKE_tracking_clipboard_free(void);
72 void BKE_tracking_clipboard_copy_tracks(struct MovieTracking *tracking,
73                                         struct MovieTrackingObject *object);
74 bool BKE_tracking_clipboard_has_tracks(void);
75 void BKE_tracking_clipboard_paste_tracks(struct MovieTracking *tracking,
76                                          struct MovieTrackingObject *object);
77
78 /* **** Track **** */
79 struct MovieTrackingTrack *BKE_tracking_track_add(struct MovieTracking *tracking,
80                                                   struct ListBase *tracksbase,
81                                                   float x,
82                                                   float y,
83                                                   int framenr,
84                                                   int width,
85                                                   int height);
86 struct MovieTrackingTrack *BKE_tracking_track_duplicate(struct MovieTrackingTrack *track);
87 void BKE_tracking_track_unique_name(struct ListBase *tracksbase, struct MovieTrackingTrack *track);
88 void BKE_tracking_track_free(struct MovieTrackingTrack *track);
89
90 void BKE_tracking_track_flag_set(struct MovieTrackingTrack *track, int area, int flag);
91 void BKE_tracking_track_flag_clear(struct MovieTrackingTrack *track, int area, int flag);
92
93 bool BKE_tracking_track_has_marker_at_frame(struct MovieTrackingTrack *track, int framenr);
94 bool BKE_tracking_track_has_enabled_marker_at_frame(struct MovieTrackingTrack *track, int framenr);
95
96 void BKE_tracking_track_path_clear(struct MovieTrackingTrack *track, int ref_frame, int action);
97 void BKE_tracking_tracks_join(struct MovieTracking *tracking,
98                               struct MovieTrackingTrack *dst_track,
99                               struct MovieTrackingTrack *src_track);
100
101 struct MovieTrackingTrack *BKE_tracking_track_get_named(struct MovieTracking *tracking,
102                                                         struct MovieTrackingObject *object,
103                                                         const char *name);
104 struct MovieTrackingTrack *BKE_tracking_track_get_indexed(struct MovieTracking *tracking,
105                                                           int tracknr,
106                                                           struct ListBase **r_tracksbase);
107
108 struct MovieTrackingTrack *BKE_tracking_track_get_active(struct MovieTracking *tracking);
109
110 float *BKE_tracking_track_get_mask(int frame_width,
111                                    int frame_height,
112                                    struct MovieTrackingTrack *track,
113                                    struct MovieTrackingMarker *marker);
114
115 float BKE_tracking_track_get_weight_for_marker(struct MovieClip *clip,
116                                                struct MovieTrackingTrack *track,
117                                                struct MovieTrackingMarker *marker);
118
119 /* selection */
120 void BKE_tracking_track_select(struct ListBase *tracksbase,
121                                struct MovieTrackingTrack *track,
122                                int area,
123                                bool extend);
124 void BKE_tracking_track_deselect(struct MovieTrackingTrack *track, int area);
125 void BKE_tracking_tracks_deselect_all(struct ListBase *tracksbase);
126
127 /* **** Marker **** */
128 struct MovieTrackingMarker *BKE_tracking_marker_insert(struct MovieTrackingTrack *track,
129                                                        struct MovieTrackingMarker *marker);
130 void BKE_tracking_marker_delete(struct MovieTrackingTrack *track, int framenr);
131
132 void BKE_tracking_marker_clamp(struct MovieTrackingMarker *marker, int event);
133
134 struct MovieTrackingMarker *BKE_tracking_marker_get(struct MovieTrackingTrack *track, int framenr);
135 struct MovieTrackingMarker *BKE_tracking_marker_get_exact(struct MovieTrackingTrack *track,
136                                                           int framenr);
137 struct MovieTrackingMarker *BKE_tracking_marker_ensure(struct MovieTrackingTrack *track,
138                                                        int framenr);
139
140 void BKE_tracking_marker_pattern_minmax(const struct MovieTrackingMarker *marker,
141                                         float min[2],
142                                         float max[2]);
143
144 void BKE_tracking_marker_get_subframe_position(struct MovieTrackingTrack *track,
145                                                float framenr,
146                                                float pos[2]);
147
148 /* **** Plane Track **** */
149 struct MovieTrackingPlaneTrack *BKE_tracking_plane_track_add(struct MovieTracking *tracking,
150                                                              struct ListBase *plane_tracks_base,
151                                                              struct ListBase *tracks,
152                                                              int framenr);
153 void BKE_tracking_plane_track_unique_name(struct ListBase *plane_tracks_base,
154                                           struct MovieTrackingPlaneTrack *plane_track);
155 void BKE_tracking_plane_track_free(struct MovieTrackingPlaneTrack *plane_track);
156
157 bool BKE_tracking_plane_track_has_marker_at_frame(struct MovieTrackingPlaneTrack *plane_track,
158                                                   int framenr);
159 bool BKE_tracking_plane_track_has_enabled_marker_at_frame(
160     struct MovieTrackingPlaneTrack *plane_track, int framenr);
161
162 struct MovieTrackingPlaneTrack *BKE_tracking_plane_track_get_named(
163     struct MovieTracking *tracking, struct MovieTrackingObject *object, const char *name);
164
165 struct MovieTrackingPlaneTrack *BKE_tracking_plane_track_get_active(
166     struct MovieTracking *tracking);
167
168 void BKE_tracking_plane_tracks_deselect_all(struct ListBase *plane_tracks_base);
169
170 bool BKE_tracking_plane_track_has_point_track(struct MovieTrackingPlaneTrack *plane_track,
171                                               struct MovieTrackingTrack *track);
172 bool BKE_tracking_plane_track_remove_point_track(struct MovieTrackingPlaneTrack *plane_track,
173                                                  struct MovieTrackingTrack *track);
174
175 void BKE_tracking_plane_tracks_remove_point_track(struct MovieTracking *tracking,
176                                                   struct MovieTrackingTrack *track);
177
178 void BKE_tracking_plane_track_replace_point_track(struct MovieTrackingPlaneTrack *plane_track,
179                                                   struct MovieTrackingTrack *old_track,
180                                                   struct MovieTrackingTrack *new_track);
181 void BKE_tracking_plane_tracks_replace_point_track(struct MovieTracking *tracking,
182                                                    struct MovieTrackingTrack *old_track,
183                                                    struct MovieTrackingTrack *new_track);
184
185 /* **** Plane Marker **** */
186 struct MovieTrackingPlaneMarker *BKE_tracking_plane_marker_insert(
187     struct MovieTrackingPlaneTrack *plane_track, struct MovieTrackingPlaneMarker *plane_marker);
188 void BKE_tracking_plane_marker_delete(struct MovieTrackingPlaneTrack *plane_track, int framenr);
189
190 struct MovieTrackingPlaneMarker *BKE_tracking_plane_marker_get(
191     struct MovieTrackingPlaneTrack *plane_track, int framenr);
192 struct MovieTrackingPlaneMarker *BKE_tracking_plane_marker_get_exact(
193     struct MovieTrackingPlaneTrack *plane_track, int framenr);
194 struct MovieTrackingPlaneMarker *BKE_tracking_plane_marker_ensure(
195     struct MovieTrackingPlaneTrack *plane_track, int framenr);
196 void BKE_tracking_plane_marker_get_subframe_corners(struct MovieTrackingPlaneTrack *plane_track,
197                                                     float framenr,
198                                                     float corners[4][2]);
199
200 /* **** Object **** */
201 struct MovieTrackingObject *BKE_tracking_object_add(struct MovieTracking *tracking,
202                                                     const char *name);
203 bool BKE_tracking_object_delete(struct MovieTracking *tracking,
204                                 struct MovieTrackingObject *object);
205
206 void BKE_tracking_object_unique_name(struct MovieTracking *tracking,
207                                      struct MovieTrackingObject *object);
208
209 struct MovieTrackingObject *BKE_tracking_object_get_named(struct MovieTracking *tracking,
210                                                           const char *name);
211
212 struct MovieTrackingObject *BKE_tracking_object_get_active(struct MovieTracking *tracking);
213 struct MovieTrackingObject *BKE_tracking_object_get_camera(struct MovieTracking *tracking);
214
215 struct ListBase *BKE_tracking_object_get_tracks(struct MovieTracking *tracking,
216                                                 struct MovieTrackingObject *object);
217 struct ListBase *BKE_tracking_object_get_plane_tracks(struct MovieTracking *tracking,
218                                                       struct MovieTrackingObject *object);
219 struct MovieTrackingReconstruction *BKE_tracking_object_get_reconstruction(
220     struct MovieTracking *tracking, struct MovieTrackingObject *object);
221
222 /* **** Camera **** */
223 void BKE_tracking_camera_shift_get(
224     struct MovieTracking *tracking, int winx, int winy, float *shiftx, float *shifty);
225 void BKE_tracking_camera_to_blender(struct MovieTracking *tracking,
226                                     struct Scene *scene,
227                                     struct Camera *camera,
228                                     int width,
229                                     int height);
230
231 struct MovieReconstructedCamera *BKE_tracking_camera_get_reconstructed(
232     struct MovieTracking *tracking, struct MovieTrackingObject *object, int framenr);
233 void BKE_tracking_camera_get_reconstructed_interpolate(struct MovieTracking *tracking,
234                                                        struct MovieTrackingObject *object,
235                                                        float framenr,
236                                                        float mat[4][4]);
237
238 /* **** Distortion/Undistortion **** */
239 struct MovieDistortion *BKE_tracking_distortion_new(struct MovieTracking *tracking,
240                                                     int calibration_width,
241                                                     int calibration_height);
242 void BKE_tracking_distortion_update(struct MovieDistortion *distortion,
243                                     struct MovieTracking *tracking,
244                                     int calibration_width,
245                                     int calibration_height);
246 void BKE_tracking_distortion_set_threads(struct MovieDistortion *distortion, int threads);
247 struct MovieDistortion *BKE_tracking_distortion_copy(struct MovieDistortion *distortion);
248 struct ImBuf *BKE_tracking_distortion_exec(struct MovieDistortion *distortion,
249                                            struct MovieTracking *tracking,
250                                            struct ImBuf *ibuf,
251                                            int width,
252                                            int height,
253                                            float overscan,
254                                            bool undistort);
255 void BKE_tracking_distortion_distort_v2(struct MovieDistortion *distortion,
256                                         const float co[2],
257                                         float r_co[2]);
258 void BKE_tracking_distortion_undistort_v2(struct MovieDistortion *distortion,
259                                           const float co[2],
260                                           float r_co[2]);
261 void BKE_tracking_distortion_free(struct MovieDistortion *distortion);
262
263 void BKE_tracking_distort_v2(struct MovieTracking *tracking,
264                              int image_width,
265                              int image_height,
266                              const float co[2],
267                              float r_co[2]);
268 void BKE_tracking_undistort_v2(struct MovieTracking *tracking,
269                                int image_width,
270                                int image_height,
271                                const float co[2],
272                                float r_co[2]);
273
274 struct ImBuf *BKE_tracking_undistort_frame(struct MovieTracking *tracking,
275                                            struct ImBuf *ibuf,
276                                            int calibration_width,
277                                            int calibration_height,
278                                            float overscan);
279 struct ImBuf *BKE_tracking_distort_frame(struct MovieTracking *tracking,
280                                          struct ImBuf *ibuf,
281                                          int calibration_width,
282                                          int calibration_height,
283                                          float overscan);
284
285 void BKE_tracking_max_distortion_delta_across_bound(struct MovieTracking *tracking,
286                                                     int image_width,
287                                                     int image_height,
288                                                     struct rcti *rect,
289                                                     bool undistort,
290                                                     float delta[2]);
291
292 /* **** Image sampling **** */
293 struct ImBuf *BKE_tracking_sample_pattern(int frame_width,
294                                           int frame_height,
295                                           struct ImBuf *search_ib,
296                                           struct MovieTrackingTrack *track,
297                                           struct MovieTrackingMarker *marker,
298                                           bool from_anchor,
299                                           bool use_mask,
300                                           int num_samples_x,
301                                           int num_samples_y,
302                                           float pos[2]);
303 struct ImBuf *BKE_tracking_get_pattern_imbuf(struct ImBuf *ibuf,
304                                              struct MovieTrackingTrack *track,
305                                              struct MovieTrackingMarker *marker,
306                                              bool anchored,
307                                              bool disable_channels);
308 struct ImBuf *BKE_tracking_get_search_imbuf(struct ImBuf *ibuf,
309                                             struct MovieTrackingTrack *track,
310                                             struct MovieTrackingMarker *marker,
311                                             bool anchored,
312                                             bool disable_channels);
313
314 void BKE_tracking_disable_channels(
315     struct ImBuf *ibuf, bool disable_red, bool disable_green, bool disable_blue, bool grayscale);
316
317 /* **** 2D tracking **** */
318 void BKE_tracking_refine_marker(struct MovieClip *clip,
319                                 struct MovieTrackingTrack *track,
320                                 struct MovieTrackingMarker *marker,
321                                 bool backwards);
322
323 /* *** 2D auto track  *** */
324
325 struct AutoTrackContext *BKE_autotrack_context_new(struct MovieClip *clip,
326                                                    struct MovieClipUser *user,
327                                                    const bool backwards,
328                                                    const bool sequence);
329 bool BKE_autotrack_context_step(struct AutoTrackContext *context);
330 void BKE_autotrack_context_sync(struct AutoTrackContext *context);
331 void BKE_autotrack_context_sync_user(struct AutoTrackContext *context, struct MovieClipUser *user);
332 void BKE_autotrack_context_finish(struct AutoTrackContext *context);
333 void BKE_autotrack_context_free(struct AutoTrackContext *context);
334
335 /* **** Plane tracking **** */
336
337 void BKE_tracking_track_plane_from_existing_motion(struct MovieTrackingPlaneTrack *plane_track,
338                                                    int start_frame);
339 void BKE_tracking_retrack_plane_from_existing_motion_at_segment(
340     struct MovieTrackingPlaneTrack *plane_track, int start_frame);
341 void BKE_tracking_homography_between_two_quads(/*const*/ float reference_corners[4][2],
342                                                /*const*/ float corners[4][2],
343                                                float H[3][3]);
344
345 /* **** Camera solving **** */
346 bool BKE_tracking_reconstruction_check(struct MovieTracking *tracking,
347                                        struct MovieTrackingObject *object,
348                                        char *error_msg,
349                                        int error_size);
350
351 struct MovieReconstructContext *BKE_tracking_reconstruction_context_new(
352     struct MovieClip *clip,
353     struct MovieTrackingObject *object,
354     int keyframe1,
355     int keyframe2,
356     int width,
357     int height);
358 void BKE_tracking_reconstruction_context_free(struct MovieReconstructContext *context);
359 void BKE_tracking_reconstruction_solve(struct MovieReconstructContext *context,
360                                        short *stop,
361                                        short *do_update,
362                                        float *progress,
363                                        char *stats_message,
364                                        int message_size);
365 bool BKE_tracking_reconstruction_finish(struct MovieReconstructContext *context,
366                                         struct MovieTracking *tracking);
367
368 void BKE_tracking_reconstruction_report_error_message(struct MovieReconstructContext *context,
369                                                       const char *error_message);
370
371 const char *BKE_tracking_reconstruction_error_message_get(
372     const struct MovieReconstructContext *context);
373
374 void BKE_tracking_reconstruction_scale(struct MovieTracking *tracking, float scale[3]);
375
376 /* **** Feature detection **** */
377 void BKE_tracking_detect_fast(struct MovieTracking *tracking,
378                               struct ListBase *tracksbase,
379                               struct ImBuf *ibuf,
380                               int framenr,
381                               int margin,
382                               int min_trackness,
383                               int min_distance,
384                               struct bGPDlayer *layer,
385                               bool place_outside_layer);
386
387 void BKE_tracking_detect_harris(struct MovieTracking *tracking,
388                                 struct ListBase *tracksbase,
389                                 struct ImBuf *ibuf,
390                                 int framenr,
391                                 int margin,
392                                 float threshold,
393                                 int min_distance,
394                                 struct bGPDlayer *layer,
395                                 bool place_outside_layer);
396
397 /* **** 2D stabilization **** */
398 void BKE_tracking_stabilization_data_get(struct MovieClip *clip,
399                                          int framenr,
400                                          int width,
401                                          int height,
402                                          float translation[2],
403                                          float *scale,
404                                          float *angle);
405 struct ImBuf *BKE_tracking_stabilize_frame(struct MovieClip *clip,
406                                            int framenr,
407                                            struct ImBuf *ibuf,
408                                            float translation[2],
409                                            float *scale,
410                                            float *angle);
411 void BKE_tracking_stabilization_data_to_mat4(int width,
412                                              int height,
413                                              float aspect,
414                                              float translation[2],
415                                              float scale,
416                                              float angle,
417                                              float mat[4][4]);
418
419 /* Dopesheet */
420 void BKE_tracking_dopesheet_tag_update(struct MovieTracking *tracking);
421 void BKE_tracking_dopesheet_update(struct MovieTracking *tracking);
422
423 /* **** Query/search **** */
424
425 struct MovieTrackingObject *BKE_tracking_find_object_for_track(
426     const struct MovieTracking *tracking, const struct MovieTrackingTrack *track);
427 struct ListBase *BKE_tracking_find_tracks_list_for_track(struct MovieTracking *tracking,
428                                                          const struct MovieTrackingTrack *track);
429
430 struct MovieTrackingObject *BKE_tracking_find_object_for_plane_track(
431     const struct MovieTracking *tracking, const struct MovieTrackingPlaneTrack *plane_track);
432 struct ListBase *BKE_tracking_find_tracks_list_for_plane_track(
433     struct MovieTracking *tracking, const struct MovieTrackingPlaneTrack *plane_track);
434
435 void BKE_tracking_get_rna_path_for_track(const struct MovieTracking *tracking,
436                                          const struct MovieTrackingTrack *track,
437                                          char *rna_path,
438                                          size_t rna_path_len);
439 void BKE_tracking_get_rna_path_prefix_for_track(const struct MovieTracking *tracking,
440                                                 const struct MovieTrackingTrack *track,
441                                                 char *rna_path,
442                                                 size_t rna_path_len);
443 void BKE_tracking_get_rna_path_for_plane_track(const struct MovieTracking *tracking,
444                                                const struct MovieTrackingPlaneTrack *plane_track,
445                                                char *rna_path,
446                                                size_t rna_path_len);
447 void BKE_tracking_get_rna_path_prefix_for_plane_track(
448     const struct MovieTracking *tracking,
449     const struct MovieTrackingPlaneTrack *plane_track,
450     char *rna_path,
451     size_t rna_path_len);
452
453 /* **** Utility macros **** */
454
455 #define TRACK_SELECTED(track) \
456   ((track)->flag & SELECT || (track)->pat_flag & SELECT || (track)->search_flag & SELECT)
457
458 #define TRACK_AREA_SELECTED(track, area) \
459   ((area) == TRACK_AREA_POINT ? \
460        (track)->flag & SELECT : \
461        ((area) == TRACK_AREA_PAT ? (track)->pat_flag & SELECT : (track)->search_flag & SELECT))
462
463 #define TRACK_VIEW_SELECTED(sc, track) \
464   ((((track)->flag & TRACK_HIDDEN) == 0) && \
465    (TRACK_AREA_SELECTED(track, TRACK_AREA_POINT) || \
466     (((sc)->flag & SC_SHOW_MARKER_PATTERN) && TRACK_AREA_SELECTED(track, TRACK_AREA_PAT)) || \
467     (((sc)->flag & SC_SHOW_MARKER_SEARCH) && TRACK_AREA_SELECTED(track, TRACK_AREA_SEARCH))))
468
469 #define PLANE_TRACK_VIEW_SELECTED(plane_track) \
470   ((((plane_track)->flag & PLANE_TRACK_HIDDEN) == 0) && ((plane_track)->flag & SELECT))
471
472 #define MARKER_VISIBLE(sc, track, marker) \
473   (((marker)->flag & MARKER_DISABLED) == 0 || ((sc)->flag & SC_HIDE_DISABLED) == 0 || \
474    ((sc)->clip->tracking.act_track == track))
475
476 #define TRACK_CLEAR_UPTO 0
477 #define TRACK_CLEAR_REMAINED 1
478 #define TRACK_CLEAR_ALL 2
479
480 #define CLAMP_PAT_DIM 1
481 #define CLAMP_PAT_POS 2
482 #define CLAMP_SEARCH_DIM 3
483 #define CLAMP_SEARCH_POS 4
484
485 #define TRACK_AREA_NONE -1
486 #define TRACK_AREA_POINT 1
487 #define TRACK_AREA_PAT 2
488 #define TRACK_AREA_SEARCH 4
489
490 #define TRACK_AREA_ALL (TRACK_AREA_POINT | TRACK_AREA_PAT | TRACK_AREA_SEARCH)
491
492 #ifdef __cplusplus
493 }
494 #endif