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