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