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 /** \file
21  * \ingroup spclip
22  */
23 
24 #include "DNA_scene_types.h"
25 
26 #include "MEM_guardedalloc.h"
27 
28 #include "BLI_listbase.h"
29 #include "BLI_math.h"
30 #include "BLI_utildefines.h"
31 
32 #include "BKE_animsys.h"
33 #include "BKE_context.h"
34 #include "BKE_movieclip.h"
35 #include "BKE_tracking.h"
36 
37 #include "DEG_depsgraph.h"
38 #include "DEG_depsgraph_build.h"
39 
40 #include "GPU_immediate.h"
41 #include "GPU_state.h"
42 
43 #include "WM_api.h"
44 #include "WM_types.h"
45 
46 #include "ED_clip.h"
47 #include "ED_screen.h"
48 
49 #include "UI_interface.h"
50 #include "UI_resources.h"
51 #include "UI_view2d.h"
52 
53 #include "clip_intern.h" /* own include */
54 
clip_graph_value_visible(SpaceClip * sc,eClipCurveValueSource value_source)55 bool clip_graph_value_visible(SpaceClip *sc, eClipCurveValueSource value_source)
56 {
57   if (ELEM(value_source, CLIP_VALUE_SOURCE_SPEED_X, CLIP_VALUE_SOURCE_SPEED_Y)) {
58     if ((sc->flag & SC_SHOW_GRAPH_TRACKS_MOTION) == 0) {
59       return false;
60     }
61   }
62   else if (value_source == CLIP_VALUE_SOURCE_REPROJECTION_ERROR) {
63     if ((sc->flag & SC_SHOW_GRAPH_TRACKS_ERROR) == 0) {
64       return false;
65     }
66   }
67   return true;
68 }
69 
clip_graph_tracking_values_iterate_track_speed_values(SpaceClip * sc,MovieTrackingTrack * track,void * userdata,ClipTrackValueCallback func,ClipTrackValueSegmentStartCallback segment_start,ClipTrackValueSegmentEndCallback segment_end)70 static void clip_graph_tracking_values_iterate_track_speed_values(
71     SpaceClip *sc,
72     MovieTrackingTrack *track,
73     void *userdata,
74     ClipTrackValueCallback func,
75     ClipTrackValueSegmentStartCallback segment_start,
76     ClipTrackValueSegmentEndCallback segment_end)
77 {
78   MovieClip *clip = ED_space_clip_get_clip(sc);
79   int width, height, coord;
80 
81   BKE_movieclip_get_size(clip, &sc->user, &width, &height);
82 
83   for (coord = 0; coord < 2; coord++) {
84     eClipCurveValueSource value_source = (coord == 0) ? CLIP_VALUE_SOURCE_SPEED_X :
85                                                         CLIP_VALUE_SOURCE_SPEED_Y;
86     int i, prevfra = track->markers[0].framenr;
87     bool open = false;
88     float prevval = 0.0f;
89 
90     for (i = 0; i < track->markersnr; i++) {
91       MovieTrackingMarker *marker = &track->markers[i];
92       float val;
93 
94       if (marker->flag & MARKER_DISABLED) {
95         if (open) {
96           if (segment_end) {
97             segment_end(userdata, value_source);
98           }
99 
100           open = false;
101         }
102 
103         continue;
104       }
105 
106       if (!open) {
107         if (segment_start) {
108           if ((i + 1) == track->markersnr) {
109             segment_start(userdata, track, value_source, true);
110           }
111           else {
112             segment_start(
113                 userdata, track, value_source, (track->markers[i + 1].flag & MARKER_DISABLED));
114           }
115         }
116 
117         open = true;
118         prevval = marker->pos[coord];
119       }
120 
121       /* value is a pixels per frame speed */
122       val = (marker->pos[coord] - prevval) * ((coord == 0) ? (width) : (height));
123       val /= marker->framenr - prevfra;
124 
125       if (func) {
126         int scene_framenr = BKE_movieclip_remap_clip_to_scene_frame(clip, marker->framenr);
127 
128         func(userdata, track, marker, value_source, scene_framenr, val);
129       }
130 
131       prevval = marker->pos[coord];
132       prevfra = marker->framenr;
133     }
134 
135     if (open) {
136       if (segment_end) {
137         segment_end(userdata, value_source);
138       }
139     }
140   }
141 }
142 
calculate_reprojection_error_at_marker(MovieClip * clip,MovieTracking * tracking,MovieTrackingObject * tracking_object,MovieTrackingTrack * track,MovieTrackingMarker * marker,const int clip_width,const int clip_height,const int scene_framenr)143 static float calculate_reprojection_error_at_marker(MovieClip *clip,
144                                                     MovieTracking *tracking,
145                                                     MovieTrackingObject *tracking_object,
146                                                     MovieTrackingTrack *track,
147                                                     MovieTrackingMarker *marker,
148                                                     const int clip_width,
149                                                     const int clip_height,
150                                                     const int scene_framenr)
151 {
152   float reprojected_position[4], bundle_position[4], marker_position[2], delta[2];
153   float weight = BKE_tracking_track_get_weight_for_marker(clip, track, marker);
154   const float aspy = 1.0f / tracking->camera.pixel_aspect;
155 
156   float projection_matrix[4][4];
157   BKE_tracking_get_projection_matrix(
158       tracking, tracking_object, scene_framenr, clip_width, clip_height, projection_matrix);
159 
160   copy_v3_v3(bundle_position, track->bundle_pos);
161   bundle_position[3] = 1;
162 
163   mul_v4_m4v4(reprojected_position, projection_matrix, bundle_position);
164   reprojected_position[0] = (reprojected_position[0] / (reprojected_position[3] * 2.0f) + 0.5f) *
165                             clip_width;
166   reprojected_position[1] = (reprojected_position[1] / (reprojected_position[3] * 2.0f) + 0.5f) *
167                             clip_height * aspy;
168 
169   BKE_tracking_distort_v2(
170       tracking, clip_width, clip_height, reprojected_position, reprojected_position);
171 
172   marker_position[0] = (marker->pos[0] + track->offset[0]) * clip_width;
173   marker_position[1] = (marker->pos[1] + track->offset[1]) * clip_height * aspy;
174 
175   sub_v2_v2v2(delta, reprojected_position, marker_position);
176   return len_v2(delta) * weight;
177 }
178 
clip_graph_tracking_values_iterate_track_reprojection_error_values(SpaceClip * sc,MovieTrackingTrack * track,void * userdata,ClipTrackValueCallback func,ClipTrackValueSegmentStartCallback segment_start,ClipTrackValueSegmentEndCallback segment_end)179 static void clip_graph_tracking_values_iterate_track_reprojection_error_values(
180     SpaceClip *sc,
181     MovieTrackingTrack *track,
182     void *userdata,
183     ClipTrackValueCallback func,
184     ClipTrackValueSegmentStartCallback segment_start,
185     ClipTrackValueSegmentEndCallback segment_end)
186 {
187   /* Tracks without bundle can not have any reprojection error curve. */
188   if ((track->flag & TRACK_HAS_BUNDLE) == 0) {
189     return;
190   }
191 
192   MovieClip *clip = ED_space_clip_get_clip(sc);
193   MovieTracking *tracking = &clip->tracking;
194   MovieTrackingObject *tracking_object = BKE_tracking_object_get_active(tracking);
195 
196   int clip_width, clip_height;
197   BKE_movieclip_get_size(clip, &sc->user, &clip_width, &clip_height);
198 
199   /* Iterate over segments. */
200   bool is_segment_open = false;
201   for (int marker_index = 0; marker_index < track->markersnr; marker_index++) {
202     MovieTrackingMarker *marker = &track->markers[marker_index];
203 
204     /* End of tracked segment, no reprojection error can be calculated here since the ground truth
205      * 2D position is not known. */
206     if (marker->flag & MARKER_DISABLED) {
207       if (is_segment_open) {
208         if (segment_end != NULL) {
209           segment_end(userdata, CLIP_VALUE_SOURCE_REPROJECTION_ERROR);
210         }
211         is_segment_open = false;
212       }
213       continue;
214     }
215 
216     /* Begin new segment if it is not open yet. */
217     if (!is_segment_open) {
218       if (segment_start != NULL) {
219         if ((marker_index + 1) == track->markersnr) {
220           segment_start(userdata, track, CLIP_VALUE_SOURCE_REPROJECTION_ERROR, true);
221         }
222         else {
223           segment_start(userdata,
224                         track,
225                         CLIP_VALUE_SOURCE_REPROJECTION_ERROR,
226                         (track->markers[marker_index + 1].flag & MARKER_DISABLED));
227         }
228       }
229       is_segment_open = true;
230     }
231 
232     if (func != NULL) {
233       const int scene_framenr = BKE_movieclip_remap_clip_to_scene_frame(clip, marker->framenr);
234       const float reprojection_error = calculate_reprojection_error_at_marker(
235           clip, tracking, tracking_object, track, marker, clip_width, clip_height, scene_framenr);
236       func(userdata,
237            track,
238            marker,
239            CLIP_VALUE_SOURCE_REPROJECTION_ERROR,
240            scene_framenr,
241            reprojection_error);
242     }
243   }
244 
245   if (is_segment_open && segment_end != NULL) {
246     segment_end(userdata, CLIP_VALUE_SOURCE_REPROJECTION_ERROR);
247   }
248 }
249 
clip_graph_tracking_values_iterate_track(SpaceClip * sc,MovieTrackingTrack * track,void * userdata,ClipTrackValueCallback func,ClipTrackValueSegmentStartCallback segment_start,ClipTrackValueSegmentEndCallback segment_end)250 void clip_graph_tracking_values_iterate_track(SpaceClip *sc,
251                                               MovieTrackingTrack *track,
252                                               void *userdata,
253                                               ClipTrackValueCallback func,
254                                               ClipTrackValueSegmentStartCallback segment_start,
255                                               ClipTrackValueSegmentEndCallback segment_end)
256 {
257   clip_graph_tracking_values_iterate_track_speed_values(
258       sc, track, userdata, func, segment_start, segment_end);
259 
260   clip_graph_tracking_values_iterate_track_reprojection_error_values(
261       sc, track, userdata, func, segment_start, segment_end);
262 }
263 
clip_graph_tracking_values_iterate(SpaceClip * sc,bool selected_only,bool include_hidden,void * userdata,ClipTrackValueCallback func,ClipTrackValueSegmentStartCallback segment_start,ClipTrackValueSegmentEndCallback segment_end)264 void clip_graph_tracking_values_iterate(SpaceClip *sc,
265                                         bool selected_only,
266                                         bool include_hidden,
267                                         void *userdata,
268                                         ClipTrackValueCallback func,
269                                         ClipTrackValueSegmentStartCallback segment_start,
270                                         ClipTrackValueSegmentEndCallback segment_end)
271 {
272   MovieClip *clip = ED_space_clip_get_clip(sc);
273   MovieTracking *tracking = &clip->tracking;
274   ListBase *tracksbase = BKE_tracking_get_active_tracks(tracking);
275   MovieTrackingTrack *track;
276 
277   for (track = tracksbase->first; track; track = track->next) {
278     if (!include_hidden && (track->flag & TRACK_HIDDEN) != 0) {
279       continue;
280     }
281 
282     if (selected_only && !TRACK_SELECTED(track)) {
283       continue;
284     }
285 
286     clip_graph_tracking_values_iterate_track(
287         sc, track, userdata, func, segment_start, segment_end);
288   }
289 }
290 
clip_graph_tracking_iterate(SpaceClip * sc,bool selected_only,bool include_hidden,void * userdata,void (* func)(void * userdata,MovieTrackingMarker * marker))291 void clip_graph_tracking_iterate(SpaceClip *sc,
292                                  bool selected_only,
293                                  bool include_hidden,
294                                  void *userdata,
295                                  void (*func)(void *userdata, MovieTrackingMarker *marker))
296 {
297   MovieClip *clip = ED_space_clip_get_clip(sc);
298   MovieTracking *tracking = &clip->tracking;
299   ListBase *tracksbase = BKE_tracking_get_active_tracks(tracking);
300 
301   LISTBASE_FOREACH (MovieTrackingTrack *, track, tracksbase) {
302     if (!include_hidden && (track->flag & TRACK_HIDDEN) != 0) {
303       continue;
304     }
305 
306     if (selected_only && !TRACK_SELECTED(track)) {
307       continue;
308     }
309 
310     for (int i = 0; i < track->markersnr; i++) {
311       MovieTrackingMarker *marker = &track->markers[i];
312 
313       if (marker->flag & MARKER_DISABLED) {
314         continue;
315       }
316 
317       if (func) {
318         func(userdata, marker);
319       }
320     }
321   }
322 }
323 
clip_delete_track(bContext * C,MovieClip * clip,MovieTrackingTrack * track)324 void clip_delete_track(bContext *C, MovieClip *clip, MovieTrackingTrack *track)
325 {
326   MovieTracking *tracking = &clip->tracking;
327   MovieTrackingTrack *act_track = BKE_tracking_track_get_active(tracking);
328   ListBase *tracksbase = BKE_tracking_get_active_tracks(tracking);
329   bool has_bundle = false;
330   const bool used_for_stabilization = (track->flag &
331                                        (TRACK_USE_2D_STAB | TRACK_USE_2D_STAB_ROT)) != 0;
332   if (track == act_track) {
333     tracking->act_track = NULL;
334   }
335   /* Handle reconstruction display in 3d viewport. */
336   if (track->flag & TRACK_HAS_BUNDLE) {
337     has_bundle = true;
338   }
339   /* Make sure no plane will use freed track */
340   BKE_tracking_plane_tracks_remove_point_track(tracking, track);
341   /* Delete f-curves associated with the track (such as weight, i.e.) */
342   /* Escaped object name, escaped track name, rest of the path. */
343   char rna_path[MAX_NAME * 4 + 64];
344   BKE_tracking_get_rna_path_for_track(tracking, track, rna_path, sizeof(rna_path));
345   if (BKE_animdata_fix_paths_remove(&clip->id, rna_path)) {
346     DEG_relations_tag_update(CTX_data_main(C));
347   }
348   /* Delete track itself. */
349   BKE_tracking_track_free(track);
350   BLI_freelinkN(tracksbase, track);
351   /* Send notifiers. */
352   WM_event_add_notifier(C, NC_MOVIECLIP | NA_EDITED, clip);
353   if (used_for_stabilization) {
354     WM_event_add_notifier(C, NC_MOVIECLIP | ND_DISPLAY, clip);
355   }
356   /* Inform dependency graph. */
357   DEG_id_tag_update(&clip->id, 0);
358   if (has_bundle) {
359     WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, NULL);
360   }
361 }
362 
clip_delete_marker(bContext * C,MovieClip * clip,MovieTrackingTrack * track,MovieTrackingMarker * marker)363 void clip_delete_marker(bContext *C,
364                         MovieClip *clip,
365                         MovieTrackingTrack *track,
366                         MovieTrackingMarker *marker)
367 {
368   if (track->markersnr == 1) {
369     clip_delete_track(C, clip, track);
370   }
371   else {
372     BKE_tracking_marker_delete(track, marker->framenr);
373 
374     WM_event_add_notifier(C, NC_MOVIECLIP | NA_EDITED, clip);
375   }
376 }
377 
clip_delete_plane_track(bContext * C,MovieClip * clip,MovieTrackingPlaneTrack * plane_track)378 void clip_delete_plane_track(bContext *C, MovieClip *clip, MovieTrackingPlaneTrack *plane_track)
379 {
380   MovieTracking *tracking = &clip->tracking;
381   ListBase *plane_tracks_base = BKE_tracking_get_active_plane_tracks(tracking);
382   /* Delete f-curves associated with the track (such as weight, i.e.) */
383   /* Escaped object name, escaped track name, rest of the path. */
384   char rna_path[MAX_NAME * 4 + 64];
385   BKE_tracking_get_rna_path_for_plane_track(tracking, plane_track, rna_path, sizeof(rna_path));
386   if (BKE_animdata_fix_paths_remove(&clip->id, rna_path)) {
387     DEG_relations_tag_update(CTX_data_main(C));
388   }
389   /* Delete the plane track itself. */
390   BKE_tracking_plane_track_free(plane_track);
391   BLI_freelinkN(plane_tracks_base, plane_track);
392   /* TODO(sergey): Any notifiers to be sent here? */
393   (void)C;
394   /* Inform dependency graph. */
395   DEG_id_tag_update(&clip->id, 0);
396 }
397 
clip_view_center_to_point(SpaceClip * sc,float x,float y)398 void clip_view_center_to_point(SpaceClip *sc, float x, float y)
399 {
400   int width, height;
401   float aspx, aspy;
402 
403   ED_space_clip_get_size(sc, &width, &height);
404   ED_space_clip_get_aspect(sc, &aspx, &aspy);
405 
406   sc->xof = (x - 0.5f) * width * aspx;
407   sc->yof = (y - 0.5f) * height * aspy;
408 }
409 
clip_draw_sfra_efra(View2D * v2d,Scene * scene)410 void clip_draw_sfra_efra(View2D *v2d, Scene *scene)
411 {
412   UI_view2d_view_ortho(v2d);
413 
414   /* currently clip editor supposes that editing clip length is equal to scene frame range */
415   GPU_blend(GPU_BLEND_ALPHA);
416 
417   uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
418   immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
419 
420   immUniformColor4f(0.0f, 0.0f, 0.0f, 0.4f);
421   immRectf(pos, v2d->cur.xmin, v2d->cur.ymin, (float)SFRA, v2d->cur.ymax);
422   immRectf(pos, (float)EFRA, v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
423 
424   GPU_blend(GPU_BLEND_NONE);
425 
426   immUniformThemeColorShade(TH_BACK, -60);
427 
428   /* thin lines where the actual frames are */
429   GPU_line_width(1.0f);
430 
431   immBegin(GPU_PRIM_LINES, 4);
432   immVertex2f(pos, (float)SFRA, v2d->cur.ymin);
433   immVertex2f(pos, (float)SFRA, v2d->cur.ymax);
434   immVertex2f(pos, (float)EFRA, v2d->cur.ymin);
435   immVertex2f(pos, (float)EFRA, v2d->cur.ymax);
436   immEnd();
437 
438   immUnbindProgram();
439 }
440