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