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