51946da3857ca49910d60e782613767a81399e82
[blender-staging.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 MovieTrackingContext;
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
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 void BKE_tracking_track_unique_name(struct ListBase *tracksbase, struct MovieTrackingTrack *track);
78 void BKE_tracking_track_free(struct MovieTrackingTrack *track);
79
80 void BKE_tracking_track_flag_set(struct MovieTrackingTrack *track, int area, int flag);
81 void BKE_tracking_track_flag_clear(struct MovieTrackingTrack *track, int area, int flag);
82
83 bool BKE_tracking_track_has_marker_at_frame(struct MovieTrackingTrack *track, int framenr);
84 bool BKE_tracking_track_has_enabled_marker_at_frame(struct MovieTrackingTrack *track, int framenr);
85
86 void BKE_tracking_track_path_clear(struct MovieTrackingTrack *track, int ref_frame, int action);
87 void BKE_tracking_tracks_join(struct MovieTracking *tracking, struct MovieTrackingTrack *dst_track, struct MovieTrackingTrack *src_track);
88
89 struct MovieTrackingTrack *BKE_tracking_track_get_named(struct MovieTracking *tracking,
90                                                         struct MovieTrackingObject *object,
91                                                         const char *name);
92 struct MovieTrackingTrack *BKE_tracking_track_get_indexed(struct MovieTracking *tracking, int tracknr,
93                                                           struct ListBase **tracksbase_r);
94
95 struct MovieTrackingTrack *BKE_tracking_track_get_active(struct MovieTracking *tracking);
96
97 float *BKE_tracking_track_get_mask(int frame_width, int frame_height, struct MovieTrackingTrack *track,
98                                    struct MovieTrackingMarker *marker);
99
100 /* selection */
101 void BKE_tracking_track_select(struct ListBase *tracksbase, struct MovieTrackingTrack *track, int area, bool extend);
102 void BKE_tracking_track_deselect(struct MovieTrackingTrack *track, int area);
103 void BKE_tracking_tracks_deselect_all(struct ListBase *tracksbase);
104
105 /* **** Marker **** */
106 struct MovieTrackingMarker *BKE_tracking_marker_insert(struct MovieTrackingTrack *track,
107                                                        struct MovieTrackingMarker *marker);
108 void BKE_tracking_marker_delete(struct MovieTrackingTrack *track, int framenr);
109
110 void BKE_tracking_marker_clamp(struct MovieTrackingMarker *marker, int event);
111
112 struct MovieTrackingMarker *BKE_tracking_marker_get(struct MovieTrackingTrack *track, int framenr);
113 struct MovieTrackingMarker *BKE_tracking_marker_get_exact(struct MovieTrackingTrack *track, int framenr);
114 struct MovieTrackingMarker *BKE_tracking_marker_ensure(struct MovieTrackingTrack *track, int framenr);
115
116 void BKE_tracking_marker_pattern_minmax(const struct MovieTrackingMarker *marker, float min[2], float max[2]);
117
118 void BKE_tracking_marker_get_subframe_position(struct MovieTrackingTrack *track, float framenr, float pos[2]);
119
120 /* **** Plane Track **** */
121 struct MovieTrackingPlaneTrack *BKE_tracking_plane_track_add(struct MovieTracking *tracking, struct ListBase *plane_tracks_base,
122                                                              struct ListBase *tracks, int framenr);
123 void BKE_tracking_plane_track_unique_name(struct ListBase *plane_tracks_base, struct MovieTrackingPlaneTrack *plane_track);
124 void BKE_tracking_plane_track_free(struct MovieTrackingPlaneTrack *plane_track);
125
126 bool BKE_tracking_plane_track_has_marker_at_frame(struct MovieTrackingPlaneTrack *plane_track, int framenr);
127 bool BKE_tracking_plane_track_has_enabled_marker_at_frame(struct MovieTrackingPlaneTrack *plane_track, int framenr);
128
129 struct MovieTrackingPlaneTrack *BKE_tracking_plane_track_get_named(struct MovieTracking *tracking,
130                                                                    struct MovieTrackingObject *object,
131                                                                    const char *name);
132
133 struct MovieTrackingPlaneTrack *BKE_tracking_plane_track_get_active(struct MovieTracking *tracking);
134
135 void BKE_tracking_plane_tracks_deselect_all(struct ListBase *plane_tracks_base);
136
137 /* **** Plane Marker **** */
138 struct MovieTrackingPlaneMarker *BKE_tracking_plane_marker_insert(struct MovieTrackingPlaneTrack *plane_track,
139                                                                   struct MovieTrackingPlaneMarker *plane_marker);
140 void BKE_tracking_plane_marker_delete(struct MovieTrackingPlaneTrack *plane_track, int framenr);
141
142 struct MovieTrackingPlaneMarker *BKE_tracking_plane_marker_get(struct MovieTrackingPlaneTrack *plane_track, int framenr);
143 struct MovieTrackingPlaneMarker *BKE_tracking_plane_marker_get_exact(struct MovieTrackingPlaneTrack *plane_track, int framenr);
144 struct MovieTrackingPlaneMarker *BKE_tracking_plane_marker_ensure(struct MovieTrackingPlaneTrack *plane_track, int framenr);
145
146 /* **** Object **** */
147 struct MovieTrackingObject *BKE_tracking_object_add(struct MovieTracking *tracking, const char *name);
148 bool BKE_tracking_object_delete(struct MovieTracking *tracking, struct MovieTrackingObject *object);
149
150 void BKE_tracking_object_unique_name(struct MovieTracking *tracking, struct MovieTrackingObject *object);
151
152 struct MovieTrackingObject *BKE_tracking_object_get_named(struct MovieTracking *tracking, const char *name);
153
154 struct MovieTrackingObject *BKE_tracking_object_get_active(struct MovieTracking *tracking);
155 struct MovieTrackingObject *BKE_tracking_object_get_camera(struct MovieTracking *tracking);
156
157 struct ListBase *BKE_tracking_object_get_tracks(struct MovieTracking *tracking, struct MovieTrackingObject *object);
158 struct ListBase *BKE_tracking_object_get_plane_tracks(struct MovieTracking *tracking, struct MovieTrackingObject *object);
159 struct MovieTrackingReconstruction *BKE_tracking_object_get_reconstruction(struct MovieTracking *tracking,
160                                                                            struct MovieTrackingObject *object);
161
162 /* **** Camera **** */
163 void BKE_tracking_camera_shift_get(struct MovieTracking *tracking, int winx, int winy, float *shiftx, float *shifty);
164 void BKE_tracking_camera_to_blender(struct MovieTracking *tracking, struct Scene *scene,
165                                     struct Camera *camera, int width, int height);
166
167 struct MovieReconstructedCamera *BKE_tracking_camera_get_reconstructed(struct MovieTracking *tracking,
168                                                                        struct MovieTrackingObject *object,
169                                                                        int framenr);
170 void BKE_tracking_camera_get_reconstructed_interpolate(struct MovieTracking *tracking,
171                                                        struct MovieTrackingObject *object,
172                                                        int framenr, float mat[4][4]);
173
174 /* **** Distortion/Undistortion **** */
175 struct MovieDistortion *BKE_tracking_distortion_new(void);
176 void BKE_tracking_distortion_update(struct MovieDistortion *distortion, struct MovieTracking *tracking,
177                                     int calibration_width, int calibration_height);
178 void BKE_tracking_distortion_set_threads(struct MovieDistortion *distortion, int threads);
179 struct MovieDistortion *BKE_tracking_distortion_copy(struct MovieDistortion *distortion);
180 struct ImBuf *BKE_tracking_distortion_exec(struct MovieDistortion *distortion, struct MovieTracking *tracking,
181                                            struct ImBuf *ibuf, int width, int height, float overscan, bool undistort);
182 void BKE_tracking_distortion_free(struct MovieDistortion *distortion);
183
184 void BKE_tracking_distort_v2(struct MovieTracking *tracking, const float co[2], float r_co[2]);
185 void BKE_tracking_undistort_v2(struct MovieTracking *tracking, const float co[2], float r_co[2]);
186
187 struct ImBuf *BKE_tracking_undistort_frame(struct MovieTracking *tracking, struct ImBuf *ibuf,
188                                            int calibration_width, int calibration_height, float overscan);
189 struct ImBuf *BKE_tracking_distort_frame(struct MovieTracking *tracking, struct ImBuf *ibuf,
190                                          int calibration_width, int calibration_height, float overscan);
191
192 void BKE_tracking_max_undistortion_delta_across_bound(struct MovieTracking *tracking, struct rcti *rect, float delta[2]);
193
194 /* **** Image sampling **** */
195 struct ImBuf *BKE_tracking_sample_pattern(int frame_width, int frame_height,
196                                           struct ImBuf *struct_ibuf, struct MovieTrackingTrack *track,
197                                           struct MovieTrackingMarker *marker, bool from_anchor, bool use_mask,
198                                           int num_samples_x, int num_samples_y, float pos[2]);
199 struct ImBuf *BKE_tracking_get_pattern_imbuf(struct ImBuf *ibuf, struct MovieTrackingTrack *track,
200                                              struct MovieTrackingMarker *marker, bool anchored, bool disable_channels);
201 struct ImBuf *BKE_tracking_get_search_imbuf(struct ImBuf *ibuf, struct MovieTrackingTrack *track,
202                                             struct MovieTrackingMarker *marker, bool anchored, bool disable_channels);
203
204 void BKE_tracking_disable_channels(struct ImBuf *ibuf, bool disable_red, bool disable_green,
205                                    bool disable_blue, bool grayscale);
206
207 /* **** 2D tracking **** */
208 struct MovieTrackingContext *BKE_tracking_context_new(struct MovieClip *clip, struct MovieClipUser *user,
209                                                       short backwards, short sequence);
210 void BKE_tracking_context_free(struct MovieTrackingContext *context);
211 void BKE_tracking_context_sync(struct MovieTrackingContext *context);
212 void BKE_tracking_context_sync_user(const struct MovieTrackingContext *context, struct MovieClipUser *user);
213 bool BKE_tracking_context_step(struct MovieTrackingContext *context);
214 void BKE_tracking_refine_marker(struct MovieClip *clip, struct MovieTrackingTrack *track, struct MovieTrackingMarker *marker, bool backwards);
215
216 /* **** Plane tracking **** */
217
218 void BKE_tracking_track_plane_from_existing_motion(struct MovieTrackingPlaneTrack *plane_track, int start_frame);
219 void BKE_tracking_homography_between_two_quads(/*const*/ float reference_corners[4][2], /*const*/ float corners[4][2], float H[3][3]);
220
221 /* **** Camera solving **** */
222 bool BKE_tracking_reconstruction_check(struct MovieTracking *tracking, struct MovieTrackingObject *object,
223                                        char *error_msg, int error_size);
224
225 struct MovieReconstructContext *BKE_tracking_reconstruction_context_new(struct MovieTracking *tracking,
226                                                                         struct MovieTrackingObject *object,
227                                                                         int keyframe1, int keyframe2,
228                                                                         int width, int height);
229 void BKE_tracking_reconstruction_context_free(struct MovieReconstructContext *context);
230 void BKE_tracking_reconstruction_solve(struct MovieReconstructContext *context, short *stop, short *do_update,
231                                        float *progress, char *stats_message, int message_size);
232 bool BKE_tracking_reconstruction_finish(struct MovieReconstructContext *context, struct MovieTracking *tracking);
233
234 void BKE_tracking_reconstruction_scale(struct MovieTracking *tracking, float scale[3]);
235
236 /* **** Feature detection **** */
237 void BKE_tracking_detect_fast(struct MovieTracking *tracking, struct ListBase *tracksbase, struct ImBuf *imbuf,
238                               int framenr, int margin, int min_trackness, int min_distance, struct bGPDlayer *layer,
239                               bool place_outside_layer);
240
241 /* **** 2D stabilization **** */
242 void BKE_tracking_stabilization_data_get(struct MovieTracking *tracking, int framenr, int width, int height,
243                                          float translation[2], float *scale, float *angle);
244 struct ImBuf *BKE_tracking_stabilize_frame(struct MovieTracking *tracking, int framenr, struct ImBuf *ibuf,
245                                            float translation[2], float *scale, float *angle);
246 void BKE_tracking_stabilization_data_to_mat4(int width, int height, float aspect, float translation[2],
247                                              float scale, float angle, float mat[4][4]);
248
249 /* Dopesheet */
250 void BKE_tracking_dopesheet_tag_update(struct MovieTracking *tracking);
251 void BKE_tracking_dopesheet_update(struct MovieTracking *tracking);
252
253 #define TRACK_SELECTED(track)               ((track)->flag & SELECT || (track)->pat_flag & SELECT || (track)->search_flag & SELECT)
254
255 #define TRACK_AREA_SELECTED(track, area)    ((area) == TRACK_AREA_POINT ? (track)->flag & SELECT : \
256                                              ((area) == TRACK_AREA_PAT ? (track)->pat_flag & SELECT : \
257                                               (track)->search_flag & SELECT))
258
259 #define TRACK_VIEW_SELECTED(sc, track)      ((((track)->flag & TRACK_HIDDEN) == 0) && \
260                                              (TRACK_AREA_SELECTED(track, TRACK_AREA_POINT) || \
261                                               (((sc)->flag & SC_SHOW_MARKER_PATTERN) && TRACK_AREA_SELECTED(track, TRACK_AREA_PAT)) || \
262                                               (((sc)->flag & SC_SHOW_MARKER_SEARCH) && TRACK_AREA_SELECTED(track, TRACK_AREA_SEARCH))))
263
264 #define MARKER_VISIBLE(sc, track, marker)       (((marker)->flag & MARKER_DISABLED) == 0 || ((sc)->flag & SC_HIDE_DISABLED) == 0 || (sc->clip->tracking.act_track == track))
265
266 #define TRACK_CLEAR_UPTO        0
267 #define TRACK_CLEAR_REMAINED    1
268 #define TRACK_CLEAR_ALL         2
269
270 #define CLAMP_PAT_DIM       1
271 #define CLAMP_PAT_POS       2
272 #define CLAMP_SEARCH_DIM    3
273 #define CLAMP_SEARCH_POS    4
274
275 #define TRACK_AREA_NONE     -1
276 #define TRACK_AREA_POINT    1
277 #define TRACK_AREA_PAT      2
278 #define TRACK_AREA_SEARCH   4
279
280 #define TRACK_AREA_ALL      (TRACK_AREA_POINT | TRACK_AREA_PAT | TRACK_AREA_SEARCH)
281
282 #endif