Internal refactoring of tracking module, should be no functional changes
[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(SpaceClip *sc, MovieTrackingTrack *track, void *userdata,
67                                               void (*func)(void *userdata, MovieTrackingTrack *track, MovieTrackingMarker *marker, int coord, int scene_framenr, float val),
68                                               void (*segment_start)(void *userdata, MovieTrackingTrack *track, int coord),
69                                               void (*segment_end)(void *userdata))
70 {
71         MovieClip *clip = ED_space_clip(sc);
72         int width, height, coord;
73
74         BKE_movieclip_get_size(clip, &sc->user, &width, &height);
75
76         for (coord = 0; coord < 2; coord++) {
77                 int i, open = FALSE, prevfra = 0;
78                 float prevval = 0.0f;
79
80                 for (i = 0; i < track->markersnr; i++) {
81                         MovieTrackingMarker *marker = &track->markers[i];
82                         float val;
83
84                         if (marker->flag & MARKER_DISABLED) {
85                                 if (open) {
86                                         if (segment_end)
87                                                 segment_end(userdata);
88
89                                         open = FALSE;
90                                 }
91
92                                 continue;
93                         }
94
95                         if (!open) {
96                                 if (segment_start)
97                                         segment_start(userdata, track, coord);
98
99                                 open = TRUE;
100                                 prevval = marker->pos[coord];
101                         }
102
103                         /* value is a pixels per frame speed */
104                         val = (marker->pos[coord] - prevval) * ((coord == 0) ? (width) : (height));
105                         val /= marker->framenr - prevfra;
106
107                         if (func) {
108                                 int scene_framenr = BKE_movieclip_remap_clip_to_scene_frame(clip, marker->framenr);
109
110                                 func(userdata, track, marker, coord, scene_framenr, val);
111                         }
112
113                         prevval = marker->pos[coord];
114                         prevfra = marker->framenr;
115                 }
116
117                 if (open) {
118                         if (segment_end)
119                                 segment_end(userdata);
120                 }
121         }
122 }
123
124 void clip_graph_tracking_values_iterate(SpaceClip *sc, int selected_only, int include_hidden, void *userdata,
125                                         void (*func)(void *userdata, MovieTrackingTrack *track, MovieTrackingMarker *marker, int coord, int scene_framenr, float val),
126                                         void (*segment_start)(void *userdata, MovieTrackingTrack *track, int coord),
127                                         void (*segment_end)(void *userdata))
128 {
129         MovieClip *clip = ED_space_clip(sc);
130         MovieTracking *tracking = &clip->tracking;
131         ListBase *tracksbase = BKE_tracking_get_active_tracks(tracking);
132         MovieTrackingTrack *track;
133
134         for (track = tracksbase->first; track; track = track->next) {
135                 if (!include_hidden && (track->flag & TRACK_HIDDEN) != 0)
136                         continue;
137
138                 if (selected_only && !TRACK_SELECTED(track))
139                         continue;
140
141                 clip_graph_tracking_values_iterate_track(sc, track, userdata, func, segment_start, segment_end);
142         }
143 }
144
145 void clip_graph_tracking_iterate(SpaceClip *sc, int selected_only, int include_hidden, void *userdata,
146                                  void (*func)(void *userdata, MovieTrackingMarker *marker))
147 {
148         MovieClip *clip = ED_space_clip(sc);
149         MovieTracking *tracking = &clip->tracking;
150         ListBase *tracksbase = BKE_tracking_get_active_tracks(tracking);
151         MovieTrackingTrack *track;
152
153         for (track = tracksbase->first; track; track = track->next) {
154                 int i;
155
156                 if (!include_hidden && (track->flag & TRACK_HIDDEN) != 0)
157                         continue;
158
159                 if (selected_only && !TRACK_SELECTED(track))
160                         continue;
161
162                 for (i = 0; i < track->markersnr; i++) {
163                         MovieTrackingMarker *marker = &track->markers[i];
164
165                         if (marker->flag & MARKER_DISABLED)
166                                 continue;
167
168                         if (func)
169                                 func(userdata, marker);
170                 }
171         }
172 }
173
174 void clip_delete_track(bContext *C, MovieClip *clip, ListBase *tracksbase, MovieTrackingTrack *track)
175 {
176         MovieTracking *tracking = &clip->tracking;
177         MovieTrackingStabilization *stab = &tracking->stabilization;
178         MovieTrackingTrack *act_track = BKE_tracking_track_get_active(tracking);
179
180         int has_bundle = FALSE, update_stab = FALSE;
181
182         if (track == act_track)
183                 tracking->act_track = NULL;
184
185         if (track == stab->rot_track) {
186                 stab->rot_track = NULL;
187
188                 update_stab = TRUE;
189         }
190
191         /* handle reconstruction display in 3d viewport */
192         if (track->flag & TRACK_HAS_BUNDLE)
193                 has_bundle = TRUE;
194
195         BKE_tracking_track_free(track);
196         BLI_freelinkN(tracksbase, track);
197
198         WM_event_add_notifier(C, NC_MOVIECLIP | NA_EDITED, clip);
199
200         if (update_stab) {
201                 tracking->stabilization.ok = FALSE;
202                 WM_event_add_notifier(C, NC_MOVIECLIP | ND_DISPLAY, clip);
203         }
204
205         DAG_id_tag_update(&clip->id, 0);
206
207         if (has_bundle)
208                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, NULL);
209 }
210
211 void clip_delete_marker(bContext *C, MovieClip *clip, ListBase *tracksbase,
212                         MovieTrackingTrack *track, MovieTrackingMarker *marker)
213 {
214         if (track->markersnr == 1) {
215                 clip_delete_track(C, clip, tracksbase, track);
216         }
217         else {
218                 BKE_tracking_marker_delete(track, marker->framenr);
219
220                 WM_event_add_notifier(C, NC_MOVIECLIP | NA_EDITED, clip);
221         }
222 }
223
224 void clip_view_center_to_point(SpaceClip *sc, float x, float y)
225 {
226         int width, height;
227         float aspx, aspy;
228
229         ED_space_clip_size(sc, &width, &height);
230         ED_space_clip_aspect(sc, &aspx, &aspy);
231
232         sc->xof = (x - 0.5f) * width * aspx;
233         sc->yof = (y - 0.5f) * height * aspy;
234 }
235
236 void clip_draw_cfra(SpaceClip *sc, ARegion *ar, Scene *scene)
237 {
238         View2D *v2d = &ar->v2d;
239         float xscale, yscale;
240         float vec[2];
241
242         /* Draw a light green line to indicate current frame */
243         vec[0] = (float)(sc->user.framenr * scene->r.framelen);
244
245         UI_ThemeColor(TH_CFRAME);
246         glLineWidth(2.0);
247
248         glBegin(GL_LINE_STRIP);
249         vec[1] = v2d->cur.ymin;
250         glVertex2fv(vec);
251
252         vec[1] = v2d->cur.ymax;
253         glVertex2fv(vec);
254         glEnd();
255
256         glLineWidth(1.0);
257
258         UI_view2d_view_orthoSpecial(ar, v2d, 1);
259
260         /* because the frame number text is subject to the same scaling as the contents of the view */
261         UI_view2d_getscale(v2d, &xscale, &yscale);
262         glScalef(1.0f / xscale, 1.0f, 1.0f);
263
264         clip_draw_curfra_label(sc, (float)sc->user.framenr * xscale, 18);
265
266         /* restore view transform */
267         glScalef(xscale, 1.0, 1.0);
268 }
269
270 void clip_draw_sfra_efra(View2D *v2d, Scene *scene)
271 {
272         UI_view2d_view_ortho(v2d);
273
274         /* currently clip editor supposes that editing clip length is equal to scene frame range */
275         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
276         glEnable(GL_BLEND);
277         glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
278
279         glRectf(v2d->cur.xmin, v2d->cur.ymin, (float)SFRA, v2d->cur.ymax);
280         glRectf((float)EFRA, v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
281         glDisable(GL_BLEND);
282
283         UI_ThemeColorShade(TH_BACK, -60);
284
285         /* thin lines where the actual frames are */
286         fdrawline((float)SFRA, v2d->cur.ymin, (float)SFRA, v2d->cur.ymax);
287         fdrawline((float)EFRA, v2d->cur.ymin, (float)EFRA, v2d->cur.ymax);
288 }