Weighted tracks
[blender.git] / source / blender / editors / space_clip / clip_utils.c
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  *
22  * Contributor(s): Blender Foundation,
23  *                 Sergey Sharybin
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_clip/clip_utils.c
29  *  \ingroup spclip
30  */
31
32 #include "DNA_scene_types.h"
33 #include "DNA_object_types.h"   /* SELECT */
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_utildefines.h"
38 #include "BLI_math.h"
39 #include "BLI_listbase.h"
40 #include "BLI_string.h"
41
42 #include "BKE_animsys.h"
43 #include "BKE_context.h"
44 #include "BKE_movieclip.h"
45 #include "BKE_tracking.h"
46 #include "BKE_depsgraph.h"
47
48 #include "BIF_gl.h"
49 #include "BIF_glutil.h"
50
51 #include "WM_api.h"
52 #include "WM_types.h"
53
54 #include "ED_screen.h"
55 #include "ED_clip.h"
56
57 #include "UI_interface.h"
58
59 #include "RNA_access.h"
60 #include "RNA_define.h"
61
62 #include "UI_interface.h"
63 #include "UI_resources.h"
64 #include "UI_view2d.h"
65
66 #include "clip_intern.h"    // own include
67
68 void clip_graph_tracking_values_iterate_track(
69         SpaceClip *sc, MovieTrackingTrack *track, void *userdata,
70         void (*func)(void *userdata, MovieTrackingTrack *track, MovieTrackingMarker *marker, int coord,
71                      int scene_framenr, float val),
72         void (*segment_start)(void *userdata, MovieTrackingTrack *track, int coord),
73         void (*segment_end)(void *userdata))
74 {
75         MovieClip *clip = ED_space_clip_get_clip(sc);
76         int width, height, coord;
77
78         BKE_movieclip_get_size(clip, &sc->user, &width, &height);
79
80         for (coord = 0; coord < 2; coord++) {
81                 int i, prevfra = 0;
82                 bool open = false;
83                 float prevval = 0.0f;
84
85                 for (i = 0; i < track->markersnr; i++) {
86                         MovieTrackingMarker *marker = &track->markers[i];
87                         float val;
88
89                         if (marker->flag & MARKER_DISABLED) {
90                                 if (open) {
91                                         if (segment_end)
92                                                 segment_end(userdata);
93
94                                         open = false;
95                                 }
96
97                                 continue;
98                         }
99
100                         if (!open) {
101                                 if (segment_start)
102                                         segment_start(userdata, track, coord);
103
104                                 open = true;
105                                 prevval = marker->pos[coord];
106                         }
107
108                         /* value is a pixels per frame speed */
109                         val = (marker->pos[coord] - prevval) * ((coord == 0) ? (width) : (height));
110                         val /= marker->framenr - prevfra;
111
112                         if (func) {
113                                 int scene_framenr = BKE_movieclip_remap_clip_to_scene_frame(clip, marker->framenr);
114
115                                 func(userdata, track, marker, coord, scene_framenr, val);
116                         }
117
118                         prevval = marker->pos[coord];
119                         prevfra = marker->framenr;
120                 }
121
122                 if (open) {
123                         if (segment_end)
124                                 segment_end(userdata);
125                 }
126         }
127 }
128
129 void clip_graph_tracking_values_iterate(
130         SpaceClip *sc, bool selected_only, bool include_hidden, void *userdata,
131         void (*func)(void *userdata, MovieTrackingTrack *track, MovieTrackingMarker *marker,
132                      int coord, int scene_framenr, float val),
133         void (*segment_start)(void *userdata, MovieTrackingTrack *track, int coord),
134         void (*segment_end)(void *userdata))
135 {
136         MovieClip *clip = ED_space_clip_get_clip(sc);
137         MovieTracking *tracking = &clip->tracking;
138         ListBase *tracksbase = BKE_tracking_get_active_tracks(tracking);
139         MovieTrackingTrack *track;
140
141         for (track = tracksbase->first; track; track = track->next) {
142                 if (!include_hidden && (track->flag & TRACK_HIDDEN) != 0)
143                         continue;
144
145                 if (selected_only && !TRACK_SELECTED(track))
146                         continue;
147
148                 clip_graph_tracking_values_iterate_track(sc, track, userdata, func, segment_start, segment_end);
149         }
150 }
151
152 void clip_graph_tracking_iterate(SpaceClip *sc, bool selected_only, bool include_hidden, void *userdata,
153                                  void (*func)(void *userdata, MovieTrackingMarker *marker))
154 {
155         MovieClip *clip = ED_space_clip_get_clip(sc);
156         MovieTracking *tracking = &clip->tracking;
157         ListBase *tracksbase = BKE_tracking_get_active_tracks(tracking);
158         MovieTrackingTrack *track;
159
160         for (track = tracksbase->first; track; track = track->next) {
161                 int i;
162
163                 if (!include_hidden && (track->flag & TRACK_HIDDEN) != 0)
164                         continue;
165
166                 if (selected_only && !TRACK_SELECTED(track))
167                         continue;
168
169                 for (i = 0; i < track->markersnr; i++) {
170                         MovieTrackingMarker *marker = &track->markers[i];
171
172                         if (marker->flag & MARKER_DISABLED)
173                                 continue;
174
175                         if (func)
176                                 func(userdata, marker);
177                 }
178         }
179 }
180
181 void clip_delete_track(bContext *C, MovieClip *clip, MovieTrackingTrack *track)
182 {
183         MovieTracking *tracking = &clip->tracking;
184         MovieTrackingStabilization *stab = &tracking->stabilization;
185         MovieTrackingTrack *act_track = BKE_tracking_track_get_active(tracking);
186         MovieTrackingPlaneTrack *plane_track, *next_plane_track;
187         ListBase *tracksbase = BKE_tracking_get_active_tracks(tracking);
188         ListBase *plane_tracks_base = BKE_tracking_get_active_plane_tracks(tracking);
189         bool has_bundle = false, update_stab = false;
190         char track_name_escaped[MAX_NAME], prefix[MAX_NAME * 2];
191
192         if (track == act_track)
193                 tracking->act_track = NULL;
194
195         if (track == stab->rot_track) {
196                 stab->rot_track = NULL;
197
198                 update_stab = true;
199         }
200
201         /* handle reconstruction display in 3d viewport */
202         if (track->flag & TRACK_HAS_BUNDLE)
203                 has_bundle = true;
204
205         /* Make sure no plane will use freed track */
206         for (plane_track = plane_tracks_base->first;
207              plane_track;
208              plane_track = next_plane_track)
209         {
210                 bool found = false;
211                 int i;
212
213                 next_plane_track = plane_track->next;
214
215                 for (i = 0; i < plane_track->point_tracksnr; i++) {
216                         if (plane_track->point_tracks[i] == track) {
217                                 found = true;
218                                 break;
219                         }
220                 }
221
222                 if (!found) {
223                         continue;
224                 }
225
226                 if (plane_track->point_tracksnr > 4) {
227                         int track_index;
228                         MovieTrackingTrack **new_point_tracks;
229
230                         new_point_tracks = MEM_mallocN(sizeof(*new_point_tracks) * plane_track->point_tracksnr,
231                                                        "new point tracks array");
232
233                         for (i = 0, track_index = 0; i < plane_track->point_tracksnr; i++) {
234                                 if (plane_track->point_tracks[i] != track) {
235                                         new_point_tracks[track_index++] = plane_track->point_tracks[i];
236                                 }
237                         }
238
239                         MEM_freeN(plane_track->point_tracks);
240                         plane_track->point_tracks = new_point_tracks;
241                         plane_track->point_tracksnr--;
242                 }
243                 else {
244                         /* Delete planes with less than 3 point tracks in it. */
245                         BKE_tracking_plane_track_free(plane_track);
246                         BLI_freelinkN(plane_tracks_base, plane_track);
247                 }
248         }
249
250         /* Delete f-curves associated with the track (such as weight, i.e.) */
251         BLI_strescape(track_name_escaped, track->name, sizeof(track_name_escaped));
252         BLI_snprintf(prefix, sizeof(prefix), "tracks[\"%s\"]", track_name_escaped);
253         BKE_animdata_fix_paths_remove(&clip->id, prefix);
254
255         BKE_tracking_track_free(track);
256         BLI_freelinkN(tracksbase, track);
257
258         WM_event_add_notifier(C, NC_MOVIECLIP | NA_EDITED, clip);
259
260         if (update_stab) {
261                 tracking->stabilization.ok = FALSE;
262                 WM_event_add_notifier(C, NC_MOVIECLIP | ND_DISPLAY, clip);
263         }
264
265         DAG_id_tag_update(&clip->id, 0);
266
267         if (has_bundle)
268                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, NULL);
269 }
270
271 void clip_delete_marker(bContext *C, MovieClip *clip, MovieTrackingTrack *track,
272                         MovieTrackingMarker *marker)
273 {
274         if (track->markersnr == 1) {
275                 clip_delete_track(C, clip, track);
276         }
277         else {
278                 BKE_tracking_marker_delete(track, marker->framenr);
279
280                 WM_event_add_notifier(C, NC_MOVIECLIP | NA_EDITED, clip);
281         }
282 }
283
284 void clip_view_center_to_point(SpaceClip *sc, float x, float y)
285 {
286         int width, height;
287         float aspx, aspy;
288
289         ED_space_clip_get_size(sc, &width, &height);
290         ED_space_clip_get_aspect(sc, &aspx, &aspy);
291
292         sc->xof = (x - 0.5f) * width * aspx;
293         sc->yof = (y - 0.5f) * height * aspy;
294 }
295
296 void clip_draw_cfra(SpaceClip *sc, ARegion *ar, Scene *scene)
297 {
298         View2D *v2d = &ar->v2d;
299         float xscale, yscale;
300         float vec[2];
301
302         /* Draw a light green line to indicate current frame */
303         vec[0] = (float)(sc->user.framenr * scene->r.framelen);
304
305         UI_ThemeColor(TH_CFRAME);
306         glLineWidth(2.0);
307
308         glBegin(GL_LINE_STRIP);
309         vec[1] = v2d->cur.ymin;
310         glVertex2fv(vec);
311
312         vec[1] = v2d->cur.ymax;
313         glVertex2fv(vec);
314         glEnd();
315
316         glLineWidth(1.0);
317
318         UI_view2d_view_orthoSpecial(ar, v2d, 1);
319
320         /* because the frame number text is subject to the same scaling as the contents of the view */
321         UI_view2d_getscale(v2d, &xscale, &yscale);
322         glScalef(1.0f / xscale, 1.0f, 1.0f);
323
324         clip_draw_curfra_label(sc->user.framenr, (float)sc->user.framenr * xscale, 18);
325
326         /* restore view transform */
327         glScalef(xscale, 1.0, 1.0);
328 }
329
330 void clip_draw_sfra_efra(View2D *v2d, Scene *scene)
331 {
332         UI_view2d_view_ortho(v2d);
333
334         /* currently clip editor supposes that editing clip length is equal to scene frame range */
335         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
336         glEnable(GL_BLEND);
337         glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
338
339         glRectf(v2d->cur.xmin, v2d->cur.ymin, (float)SFRA, v2d->cur.ymax);
340         glRectf((float)EFRA, v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
341         glDisable(GL_BLEND);
342
343         UI_ThemeColorShade(TH_BACK, -60);
344
345         /* thin lines where the actual frames are */
346         fdrawline((float)SFRA, v2d->cur.ymin, (float)SFRA, v2d->cur.ymax);
347         fdrawline((float)EFRA, v2d->cur.ymin, (float)EFRA, v2d->cur.ymax);
348 }