Merging r42178 through r42192 form trunk into soc-2011-tomato
[blender-staging.git] / source / blender / editors / space_clip / tracking_ops.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/tracking_ops.c
29  *  \ingroup spclip
30  */
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_camera_types.h"
35 #include "DNA_gpencil_types.h"
36 #include "DNA_movieclip_types.h"
37 #include "DNA_object_types.h"   /* SELECT */
38 #include "DNA_scene_types.h"
39
40 #include "BLI_utildefines.h"
41 #include "BLI_math.h"
42 #include "BLI_listbase.h"
43 #include "BLI_rect.h"
44 #include "BLI_blenlib.h"
45
46 #include "BKE_main.h"
47 #include "BKE_context.h"
48 #include "BKE_movieclip.h"
49 #include "BKE_tracking.h"
50 #include "BKE_global.h"
51 #include "BKE_depsgraph.h"
52 #include "BKE_object.h"
53 #include "BKE_report.h"
54 #include "BKE_scene.h"
55 #include "BKE_library.h"
56 #include "BKE_sound.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "ED_screen.h"
62 #include "ED_clip.h"
63 #include "ED_keyframing.h"
64
65 #include "IMB_imbuf_types.h"
66 #include "IMB_imbuf.h"
67
68 #include "UI_interface.h"
69
70 #include "RNA_access.h"
71 #include "RNA_define.h"
72
73 #include "PIL_time.h"
74
75 #include "UI_view2d.h"
76
77 #include "clip_intern.h"        // own include
78
79 static int space_clip_frame_poll(bContext *C)
80 {
81         SpaceClip *sc= CTX_wm_space_clip(C);
82
83         if(sc) {
84                 MovieClip *clip= ED_space_clip(sc);
85
86                 if(clip)
87                         return BKE_movieclip_has_frame(clip, &sc->user);
88         }
89
90         return 0;
91 }
92
93 static int space_clip_frame_camera_poll(bContext *C)
94 {
95         Scene *scene= CTX_data_scene(C);
96
97         if(space_clip_frame_poll(C)) {
98                 return scene->camera != NULL;
99         }
100
101         return 0;
102 }
103
104 static int space_clip_camera_poll(bContext *C)
105 {
106         SpaceClip *sc= CTX_wm_space_clip(C);
107         Scene *scene= CTX_data_scene(C);
108
109         if(sc && sc->clip && scene->camera)
110                 return 1;
111
112         return 0;
113 }
114
115 /********************** add marker operator *********************/
116
117 static void add_marker(SpaceClip *sc, float x, float y)
118 {
119         MovieClip *clip= ED_space_clip(sc);
120         MovieTrackingTrack *track;
121         int width, height;
122         
123         ED_space_clip_size(sc, &width, &height);
124
125         track= BKE_tracking_add_track(&clip->tracking, x, y, sc->user.framenr, width, height);
126
127         BKE_tracking_select_track(&clip->tracking, track, TRACK_AREA_ALL, 0);
128
129         clip->tracking.act_track= track;
130 }
131
132 static int add_marker_exec(bContext *C, wmOperator *op)
133 {
134         SpaceClip *sc= CTX_wm_space_clip(C);
135         MovieClip *clip= ED_space_clip(sc);
136         float pos[2];
137         int width, height;
138
139         ED_space_clip_size(sc, &width, &height);
140         if(!width || !height)
141                 return OPERATOR_CANCELLED;
142
143         RNA_float_get_array(op->ptr, "location", pos);
144
145         add_marker(sc, pos[0], pos[1]);
146
147         /* reset offset from locked position, so frame jumping wouldn't be so confusing */
148         sc->xlockof= 0;
149         sc->ylockof= 0;
150
151         WM_event_add_notifier(C, NC_MOVIECLIP|NA_EDITED, clip);
152
153         return OPERATOR_FINISHED;
154 }
155
156 static int add_marker_invoke(bContext *C, wmOperator *op, wmEvent *event)
157 {
158         float co[2];
159
160         ED_clip_mouse_pos(C, event, co);
161
162         RNA_float_set_array(op->ptr, "location", co);
163
164         return add_marker_exec(C, op);
165 }
166
167 void CLIP_OT_add_marker(wmOperatorType *ot)
168 {
169         /* identifiers */
170         ot->name= "Add Marker";
171         ot->idname= "CLIP_OT_add_marker";
172         ot->description= "Place new marker at specified location";
173
174         /* api callbacks */
175         ot->invoke= add_marker_invoke;
176         ot->exec= add_marker_exec;
177         ot->poll= space_clip_frame_poll;
178
179         /* flags */
180         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
181
182         /* properties */
183         RNA_def_float_vector(ot->srna, "location", 2, NULL, -FLT_MIN, FLT_MAX,
184                 "Location", "Location of marker on frame", -1.0f, 1.0f);
185 }
186
187 /********************** delete track operator *********************/
188
189 static int delete_track_exec(bContext *C, wmOperator *UNUSED(op))
190 {
191         SpaceClip *sc= CTX_wm_space_clip(C);
192         MovieClip *clip= ED_space_clip(sc);
193         MovieTracking *tracking= &clip->tracking;
194         MovieTrackingTrack *track= tracking->tracks.first, *next;
195
196         while(track) {
197                 next= track->next;
198
199                 if(TRACK_VIEW_SELECTED(sc, track))
200                         clip_delete_track(C, clip, track);
201
202                 track= next;
203         }
204
205         /* nothing selected now, unlock view so it can be scrolled nice again */
206         sc->flag&= ~SC_LOCK_SELECTION;
207
208         return OPERATOR_FINISHED;
209 }
210
211 void CLIP_OT_delete_track(wmOperatorType *ot)
212 {
213         /* identifiers */
214         ot->name= "Delete Track";
215         ot->idname= "CLIP_OT_delete_track";
216         ot->description= "Delete selected tracks";
217
218         /* api callbacks */
219         ot->invoke= WM_operator_confirm;
220         ot->exec= delete_track_exec;
221         ot->poll= ED_space_clip_poll;
222
223         /* flags */
224         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
225 }
226
227 /********************** delete marker operator *********************/
228
229 static int delete_marker_exec(bContext *C, wmOperator *UNUSED(op))
230 {
231         SpaceClip *sc= CTX_wm_space_clip(C);
232         MovieClip *clip= ED_space_clip(sc);
233         MovieTrackingTrack *track= clip->tracking.tracks.first, *next;
234         int framenr= sc->user.framenr;
235         int has_selection= 0;
236
237         while(track) {
238                 next= track->next;
239
240                 if(TRACK_VIEW_SELECTED(sc, track)) {
241                         MovieTrackingMarker *marker= BKE_tracking_exact_marker(track, framenr);
242
243                         if(marker) {
244                                 has_selection|= track->markersnr>1;
245
246                                 clip_delete_marker(C, clip, track, marker);
247                         }
248                 }
249
250                 track= next;
251         }
252
253         if(!has_selection) {
254                 /* nothing selected now, unlock view so it can be scrolled nice again */
255                 sc->flag&= ~SC_LOCK_SELECTION;
256         }
257
258         return OPERATOR_FINISHED;
259 }
260
261 void CLIP_OT_delete_marker(wmOperatorType *ot)
262 {
263         /* identifiers */
264         ot->name= "Delete Marker";
265         ot->idname= "CLIP_OT_delete_marker";
266         ot->description= "Delete marker for current frame from selected tracks";
267
268         /* api callbacks */
269         ot->invoke= WM_operator_confirm;
270         ot->exec= delete_marker_exec;
271         ot->poll= ED_space_clip_poll;
272
273         /* flags */
274         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
275 }
276
277 /********************** slide marker operator *********************/
278
279 #define SLIDE_ACTION_POS        0
280 #define SLIDE_ACTION_SIZE       1
281 #define SLIDE_ACTION_OFFSET     2
282
283 typedef struct {
284         int area, action;
285         MovieTrackingTrack *track;
286         MovieTrackingMarker *marker;
287
288         int mval[2];
289         int width, height;
290         float *min, *max, *pos, *offset;
291         float smin[2], smax[2], spos[2], soff[2];
292         float (*smarkers)[2];
293
294         int lock, accurate;
295 } SlideMarkerData;
296
297 static SlideMarkerData *create_slide_marker_data(SpaceClip *sc, MovieTrackingTrack *track,
298                         MovieTrackingMarker *marker, wmEvent *event, int area, int action, int width, int height)
299 {
300         SlideMarkerData *data= MEM_callocN(sizeof(SlideMarkerData), "slide marker data");
301
302         marker= BKE_tracking_ensure_marker(track, sc->user.framenr);
303
304         data->area= area;
305         data->action= action;
306         data->track= track;
307         data->marker= marker;
308
309         if(area==TRACK_AREA_POINT) {
310                 data->pos= marker->pos;
311                 data->offset= track->offset;
312                 copy_v2_v2(data->spos, marker->pos);
313                 copy_v2_v2(data->soff, track->offset);
314         } else if(area==TRACK_AREA_PAT) {
315                 if(action==SLIDE_ACTION_SIZE) {
316                         data->min= track->pat_min;
317                         data->max= track->pat_max;
318                 } else {
319                         int a;
320
321                         data->pos= marker->pos;
322                         data->offset= track->offset;
323
324                         copy_v2_v2(data->soff, track->offset);
325
326                         data->smarkers= MEM_callocN(sizeof(*data->smarkers)*track->markersnr, "slide marekrs");
327                         for(a= 0; a<track->markersnr; a++)
328                                 copy_v2_v2(data->smarkers[a], track->markers[a].pos);
329                 }
330         } else if(area==TRACK_AREA_SEARCH) {
331                 data->min= track->search_min;
332                 data->max= track->search_max;
333         }
334
335         if(area==TRACK_AREA_SEARCH || (area==TRACK_AREA_PAT && action!=SLIDE_ACTION_OFFSET)) {
336                 copy_v2_v2(data->smin, data->min);
337                 copy_v2_v2(data->smax, data->max);
338         }
339
340         data->mval[0]= event->mval[0];
341         data->mval[1]= event->mval[1];
342
343         data->width= width;
344         data->height= height;
345
346         if(action==SLIDE_ACTION_SIZE)
347                 data->lock= 1;
348
349         return data;
350 }
351
352 /* corner = 0: right-bottom corner,
353    corner = 1: left-top corner */
354 static int mouse_on_corner(SpaceClip *sc, MovieTrackingTrack *track, MovieTrackingMarker *marker,
355                         int area, float co[2], int corner, int width, int height)
356 {
357         int inside= 0;
358         float size= 12.0f;
359         float min[2], max[2];
360         float crn[2], dx, dy, tdx, tdy;
361
362         if(area==TRACK_AREA_SEARCH) {
363                 copy_v2_v2(min, track->search_min);
364                 copy_v2_v2(max, track->search_max);
365         } else {
366                 copy_v2_v2(min, track->pat_min);
367                 copy_v2_v2(max, track->pat_max);
368         }
369
370         dx= size/width/sc->zoom;
371         dy= size/height/sc->zoom;
372
373         tdx= 5.0f/width/sc->zoom;
374         tdy= 5.0f/height/sc->zoom;
375
376         dx= MIN2(dx, (max[0]-min[0])/6.0f) + tdx;
377         dy= MIN2(dy, (max[1]-min[1])/6.0f) + tdy;
378
379         if(corner==0) {
380                 crn[0]= marker->pos[0]+max[0];
381                 crn[1]= marker->pos[1]+min[1];
382
383                 inside= co[0]>=crn[0]-dx && co[0]<=crn[0]+tdx && co[1]>=crn[1]-tdy && co[1]<=crn[1]+dy;
384         } else {
385                 crn[0]= marker->pos[0]+min[0];
386                 crn[1]= marker->pos[1]+max[1];
387
388                 inside= co[0]>=crn[0]-dx && co[0]<=crn[0]+dx && co[1]>=crn[1]-dy && co[1]<=crn[1]+dy;
389         }
390
391         return inside;
392 }
393
394 static int mouse_on_offset(SpaceClip *sc, MovieTrackingTrack *track, MovieTrackingMarker *marker,
395                         float co[2], int width, int height)
396 {
397         float pos[2], dx, dy;
398
399         add_v2_v2v2(pos, marker->pos, track->offset);
400
401         dx= 12.0f/width/sc->zoom;
402         dy= 12.0f/height/sc->zoom;
403
404         dx=MIN2(dx, (track->pat_max[0]-track->pat_min[0])/2.0f);
405         dy=MIN2(dy, (track->pat_max[1]-track->pat_min[1])/2.0f);
406
407         return co[0]>=pos[0]-dx && co[0]<=pos[0]+dx && co[1]>=pos[1]-dy && co[1]<=pos[1]+dy;
408 }
409
410 static void hide_cursor(bContext *C)
411 {
412         wmWindow *win= CTX_wm_window(C);
413
414         WM_cursor_set(win, CURSOR_NONE);
415 }
416
417 static void show_cursor(bContext *C)
418 {
419         wmWindow *win= CTX_wm_window(C);
420
421         WM_cursor_set(win, CURSOR_STD);
422 }
423
424 static void *slide_marker_customdata(bContext *C, wmEvent *event)
425 {
426         SpaceClip *sc= CTX_wm_space_clip(C);
427         MovieClip *clip= ED_space_clip(sc);
428         MovieTrackingTrack *track;
429         int width, height;
430         float co[2];
431         void *customdata= NULL;
432
433         ED_space_clip_size(sc, &width, &height);
434
435         if(width==0 || height==0)
436                 return NULL;
437
438         ED_clip_mouse_pos(C, event, co);
439
440         track= clip->tracking.tracks.first;
441         while(track) {
442                 if(TRACK_VIEW_SELECTED(sc, track) && (track->flag&TRACK_LOCKED)==0) {
443                         MovieTrackingMarker *marker= BKE_tracking_get_marker(track, sc->user.framenr);
444
445                         if((marker->flag&MARKER_DISABLED)==0) {
446                                 if(!customdata)
447                                         if(mouse_on_offset(sc, track, marker, co, width, height))
448                                                 customdata= create_slide_marker_data(sc, track, marker, event, TRACK_AREA_POINT, SLIDE_ACTION_POS, width, height);
449
450                                 if(sc->flag&SC_SHOW_MARKER_SEARCH) {
451                                         if(mouse_on_corner(sc, track, marker, TRACK_AREA_SEARCH, co, 1, width, height))
452                                                 customdata= create_slide_marker_data(sc, track, marker, event, TRACK_AREA_SEARCH, SLIDE_ACTION_OFFSET, width, height);
453                                         else if(mouse_on_corner(sc, track, marker, TRACK_AREA_SEARCH, co, 0, width, height))
454                                                 customdata= create_slide_marker_data(sc, track, marker, event, TRACK_AREA_SEARCH, SLIDE_ACTION_SIZE, width, height);
455                                 }
456
457                                 if(!customdata && sc->flag&SC_SHOW_MARKER_PATTERN) {
458                                         if(mouse_on_corner(sc, track, marker, TRACK_AREA_PAT, co, 1,  width, height))
459                                                 customdata= create_slide_marker_data(sc, track, marker, event, TRACK_AREA_PAT, SLIDE_ACTION_OFFSET, width, height);
460
461                                         if(!customdata && mouse_on_corner(sc, track, marker, TRACK_AREA_PAT, co, 0, width, height))
462                                                 customdata= create_slide_marker_data(sc, track, marker, event, TRACK_AREA_PAT, SLIDE_ACTION_SIZE, width, height);
463                                 }
464
465                                 if(customdata)
466                                         break;
467                         }
468                 }
469
470                 track= track->next;
471         }
472
473         return customdata;
474 }
475
476 static int slide_marker_invoke(bContext *C, wmOperator *op, wmEvent *event)
477 {
478         SlideMarkerData *slidedata= slide_marker_customdata(C, event);
479
480         if(slidedata) {
481                 SpaceClip *sc= CTX_wm_space_clip(C);
482                 MovieClip *clip= ED_space_clip(sc);
483                 MovieTracking *tracking= &clip->tracking;
484
485                 tracking->act_track= slidedata->track;
486
487                 op->customdata= slidedata;
488
489                 hide_cursor(C);
490                 WM_event_add_modal_handler(C, op);
491
492                 WM_event_add_notifier(C, NC_GEOM|ND_SELECT, NULL);
493
494                 return OPERATOR_RUNNING_MODAL;
495         }
496
497         return OPERATOR_PASS_THROUGH;
498 }
499
500 static void cancel_mouse_slide(SlideMarkerData *data)
501 {
502         /* cancel sliding */
503         if(data->area == TRACK_AREA_POINT) {
504                 if(data->action==SLIDE_ACTION_OFFSET)
505                         copy_v2_v2(data->offset, data->soff);
506                 else
507                         copy_v2_v2(data->pos, data->spos);
508         } else {
509                 if(data->action==SLIDE_ACTION_SIZE) {
510                         copy_v2_v2(data->min, data->smin);
511                         copy_v2_v2(data->max, data->smax);
512                 } else {
513                         int a;
514
515                         for(a= 0; a<data->track->markersnr; a++)
516                                 copy_v2_v2(data->track->markers[a].pos, data->smarkers[a]);
517
518                         copy_v2_v2(data->offset, data->soff);
519                 }
520         }
521 }
522
523 static void free_slide_data(SlideMarkerData *data)
524 {
525         if(data->smarkers) MEM_freeN(data->smarkers);
526         MEM_freeN(data);
527 }
528
529 static int slide_marker_modal(bContext *C, wmOperator *op, wmEvent *event)
530 {
531         SpaceClip *sc= CTX_wm_space_clip(C);
532         SlideMarkerData *data= (SlideMarkerData *)op->customdata;
533         float dx, dy, mdelta[2];
534
535         switch(event->type) {
536                 case LEFTCTRLKEY:
537                 case RIGHTCTRLKEY:
538                 case LEFTSHIFTKEY:
539                 case RIGHTSHIFTKEY:
540                         if(data->action==SLIDE_ACTION_SIZE)
541                                 if(ELEM(event->type, LEFTCTRLKEY, RIGHTCTRLKEY))
542                                         data->lock= event->val==KM_RELEASE;
543
544                         if(ELEM(event->type, LEFTSHIFTKEY, RIGHTSHIFTKEY))
545                                 data->accurate= event->val==KM_PRESS;
546
547                         /* no break! update area size */
548
549                 case MOUSEMOVE:
550                         mdelta[0]= event->mval[0]-data->mval[0];
551                         mdelta[1]= event->mval[1]-data->mval[1];
552
553                         dx= mdelta[0]/data->width/sc->zoom;
554
555                         if(data->lock) dy= -dx/data->height*data->width;
556                         else dy= mdelta[1]/data->height/sc->zoom;
557
558                         if(data->accurate) {
559                                 dx/= 5;
560                                 dy/= 5;
561                         }
562
563                         if(data->area==TRACK_AREA_POINT) {
564                                 if(data->action==SLIDE_ACTION_OFFSET) {
565                                         data->offset[0]= data->soff[0]+dx;
566                                         data->offset[1]= data->soff[1]+dy;
567                                 } else {
568                                         data->pos[0]= data->spos[0]+dx;
569                                         data->pos[1]= data->spos[1]+dy;
570
571                                         data->marker->flag&= ~MARKER_TRACKED;
572                                 }
573
574                                 WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
575                                 DAG_id_tag_update(&sc->clip->id, 0);
576                         } else {
577                                 if(data->action==SLIDE_ACTION_SIZE) {
578                                         data->min[0]= data->smin[0]-dx;
579                                         data->max[0]= data->smax[0]+dx;
580
581                                         data->min[1]= data->smin[1]+dy;
582                                         data->max[1]= data->smax[1]-dy;
583
584                                         if(data->area==TRACK_AREA_SEARCH) BKE_tracking_clamp_track(data->track, CLAMP_SEARCH_DIM);
585                                         else BKE_tracking_clamp_track(data->track, CLAMP_PAT_DIM);
586                                 } else {
587                                         float d[2]={dx, dy};
588
589                                         if(data->area==TRACK_AREA_SEARCH) {
590                                                 add_v2_v2v2(data->min, data->smin, d);
591                                                 add_v2_v2v2(data->max, data->smax, d);
592                                         } else {
593                                                 int a;
594
595                                                 for(a= 0; a<data->track->markersnr; a++)
596                                                         add_v2_v2v2(data->track->markers[a].pos, data->smarkers[a], d);
597
598                                                 sub_v2_v2v2(data->offset, data->soff, d);
599                                         }
600
601                                         if(data->area==TRACK_AREA_SEARCH)
602                                                 BKE_tracking_clamp_track(data->track, CLAMP_SEARCH_POS);
603                                 }
604                         }
605
606                         WM_event_add_notifier(C, NC_MOVIECLIP|NA_EDITED, NULL);
607
608                         break;
609
610                 case LEFTMOUSE:
611                         if(event->val==KM_RELEASE) {
612                                 free_slide_data(op->customdata);
613
614                                 show_cursor(C);
615
616                                 return OPERATOR_FINISHED;
617                         }
618
619                         break;
620
621                 case ESCKEY:
622                         cancel_mouse_slide(op->customdata);
623
624                         free_slide_data(op->customdata);
625
626                         show_cursor(C);
627
628                         WM_event_add_notifier(C, NC_MOVIECLIP|NA_EDITED, NULL);
629
630                         return OPERATOR_CANCELLED;
631         }
632
633         return OPERATOR_RUNNING_MODAL;
634 }
635
636 void CLIP_OT_slide_marker(wmOperatorType *ot)
637 {
638         /* identifiers */
639         ot->name= "Slide Marker";
640         ot->description= "Slide marker areas";
641         ot->idname= "CLIP_OT_slide_marker";
642
643         /* api callbacks */
644         ot->poll= space_clip_frame_poll;
645         ot->invoke= slide_marker_invoke;
646         ot->modal= slide_marker_modal;
647
648         /* flags */
649         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_GRAB_POINTER|OPTYPE_BLOCKING;
650
651         /* properties */
652         RNA_def_float_vector(ot->srna, "offset", 2, NULL, -FLT_MAX, FLT_MAX,
653                 "Offset", "Offset in floating point units, 1.0 is the width and height of the image", -FLT_MAX, FLT_MAX);
654 }
655
656 /********************** mouse select operator *********************/
657
658 static int mouse_on_side(float co[2], float x1, float y1, float x2, float y2, float epsx, float epsy)
659 {
660         if(x1>x2) SWAP(float, x1, x2);
661         if(y1>y2) SWAP(float, y1, y2);
662
663         return (co[0]>=x1-epsx && co[0]<=x2+epsx) && (co[1]>=y1-epsy && co[1]<=y2+epsy);
664 }
665
666 static int mouse_on_rect(float co[2], float pos[2], float min[2], float max[2], float epsx, float epsy)
667 {
668         return mouse_on_side(co, pos[0]+min[0], pos[1]+min[1], pos[0]+max[0], pos[1]+min[1], epsx, epsy) ||
669                mouse_on_side(co, pos[0]+min[0], pos[1]+min[1], pos[0]+min[0], pos[1]+max[1], epsx, epsy) ||
670                mouse_on_side(co, pos[0]+min[0], pos[1]+max[1], pos[0]+max[0], pos[1]+max[1], epsx, epsy) ||
671                mouse_on_side(co, pos[0]+max[0], pos[1]+min[1], pos[0]+max[0], pos[1]+max[1], epsx, epsy);
672 }
673
674 static int track_mouse_area(SpaceClip *sc, float co[2], MovieTrackingTrack *track)
675 {
676         MovieTrackingMarker *marker= BKE_tracking_get_marker(track, sc->user.framenr);
677         float epsx, epsy;
678         int width, height;
679
680         ED_space_clip_size(sc, &width, &height);
681
682         epsx= MIN4(track->pat_min[0]-track->search_min[0], track->search_max[0]-track->pat_max[0],
683                    fabsf(track->pat_min[0]), fabsf(track->pat_max[0])) / 2;
684         epsy= MIN4(track->pat_min[1]-track->search_min[1], track->search_max[1]-track->pat_max[1],
685                    fabsf(track->pat_min[1]), fabsf(track->pat_max[1])) / 2;
686
687         epsx= MAX2(epsy, 2.0f / width);
688         epsy= MAX2(epsy, 2.0f / height);
689
690         if(sc->flag&SC_SHOW_MARKER_SEARCH)
691                 if(mouse_on_rect(co, marker->pos, track->search_min, track->search_max, epsx, epsy))
692                         return TRACK_AREA_SEARCH;
693
694         if((marker->flag&MARKER_DISABLED)==0) {
695                 if(sc->flag&SC_SHOW_MARKER_PATTERN)
696                         if(mouse_on_rect(co, marker->pos, track->pat_min, track->pat_max, epsx, epsy))
697                                 return TRACK_AREA_PAT;
698
699                 epsx= 12.0f/width;
700                 epsy= 12.0f/height;
701
702                 if(fabsf(co[0]-marker->pos[0]-track->offset[0])< epsx && fabsf(co[1]-marker->pos[1]-track->offset[1])<=epsy)
703                         return TRACK_AREA_POINT;
704         }
705
706         return TRACK_AREA_NONE;
707 }
708
709 static float dist_to_rect(float co[2], float pos[2], float min[2], float max[2])
710 {
711         float d1, d2, d3, d4;
712         float p[2]= {co[0]-pos[0], co[1]-pos[1]};
713         float v1[2]= {min[0], min[1]}, v2[2]= {max[0], min[1]},
714               v3[2]= {max[0], max[1]}, v4[2]= {min[0], max[1]};
715
716         d1= dist_to_line_segment_v2(p, v1, v2);
717         d2= dist_to_line_segment_v2(p, v2, v3);
718         d3= dist_to_line_segment_v2(p, v3, v4);
719         d4= dist_to_line_segment_v2(p, v4, v1);
720
721         return MIN4(d1, d2, d3, d4);
722 }
723
724 static MovieTrackingTrack *find_nearest_track(SpaceClip *sc, MovieClip *clip, float co[2])
725 {
726         MovieTrackingTrack *track= NULL, *cur;
727         float mindist= 0.0f;
728
729         cur= clip->tracking.tracks.first;
730         while(cur) {
731                 MovieTrackingMarker *marker= BKE_tracking_get_marker(cur, sc->user.framenr);
732
733                 if(((cur->flag&TRACK_HIDDEN)==0) && MARKER_VISIBLE(sc, marker)) {
734                         float dist, d1, d2=FLT_MAX, d3=FLT_MAX;
735
736                         d1= sqrtf((co[0]-marker->pos[0]-cur->offset[0])*(co[0]-marker->pos[0]-cur->offset[0])+
737                                           (co[1]-marker->pos[1]-cur->offset[1])*(co[1]-marker->pos[1]-cur->offset[1])); /* distance to marker point */
738
739                         /* distance to pattern boundbox */
740                         if(sc->flag&SC_SHOW_MARKER_PATTERN)
741                                 d2= dist_to_rect(co, marker->pos, cur->pat_min, cur->pat_max);
742
743                         /* distance to search boundbox */
744                         if(sc->flag&SC_SHOW_MARKER_SEARCH)
745                                 d3= dist_to_rect(co, marker->pos, cur->search_min, cur->search_max);
746
747                         /* choose minimal distance. useful for cases of overlapped markers. */
748                         dist= MIN3(d1, d2, d3);
749
750                         if(track==NULL || dist<mindist) {
751                                 track= cur;
752                                 mindist= dist;
753                         }
754                 }
755
756                 cur= cur->next;
757         }
758
759         return track;
760 }
761
762 static int mouse_select(bContext *C, float co[2], int extend)
763 {
764         SpaceClip *sc= CTX_wm_space_clip(C);
765         MovieClip *clip= ED_space_clip(sc);
766         MovieTracking *tracking= &clip->tracking;
767         MovieTrackingTrack *act_track= tracking->act_track;
768         MovieTrackingTrack *track= NULL;        /* selected marker */
769
770         track= find_nearest_track(sc, clip, co);
771
772         if(track) {
773                 int area= track_mouse_area(sc, co, track);
774
775                 if(!extend || !TRACK_VIEW_SELECTED(sc, track))
776                         area= TRACK_AREA_ALL;
777
778                 if(extend && TRACK_AREA_SELECTED(track, area)) {
779                         if(track==act_track)
780                                 BKE_tracking_deselect_track(track, area);
781                         else
782                                 clip->tracking.act_track= track;
783                 } else {
784                         if(area==TRACK_AREA_POINT)
785                                 area= TRACK_AREA_ALL;
786
787                         BKE_tracking_select_track(tracking, track, area, extend);
788                         clip->tracking.act_track= track;
789                 }
790         }
791
792         if(!extend) {
793                 sc->xlockof= 0.0f;
794                 sc->ylockof= 0.0f;
795         }
796
797         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, NULL);
798
799         return OPERATOR_FINISHED;
800 }
801
802 static int select_exec(bContext *C, wmOperator *op)
803 {
804         float co[2];
805         int extend;
806
807         RNA_float_get_array(op->ptr, "location", co);
808         extend= RNA_boolean_get(op->ptr, "extend");
809
810         return mouse_select(C, co, extend);
811 }
812
813 static int select_invoke(bContext *C, wmOperator *op, wmEvent *event)
814 {
815         float co[2];
816         int extend= RNA_boolean_get(op->ptr, "extend");
817
818         if(!extend) {
819                 SlideMarkerData *slidedata= slide_marker_customdata(C, event);
820
821                 if(slidedata) {
822                         SpaceClip *sc= CTX_wm_space_clip(C);
823                         MovieClip *clip= ED_space_clip(sc);
824
825                         clip->tracking.act_track= slidedata->track;
826
827                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, NULL);
828
829                         MEM_freeN(slidedata);
830
831                         return OPERATOR_PASS_THROUGH;
832                 }
833         }
834
835         ED_clip_mouse_pos(C, event, co);
836         RNA_float_set_array(op->ptr, "location", co);
837
838         return select_exec(C, op);
839 }
840
841 void CLIP_OT_select(wmOperatorType *ot)
842 {
843         /* identifiers */
844         ot->name= "Select";
845         ot->description= "Select tracking markers";
846         ot->idname= "CLIP_OT_select";
847
848         /* api callbacks */
849         ot->exec= select_exec;
850         ot->invoke= select_invoke;
851         ot->poll= ED_space_clip_poll;
852
853         /* flags */
854         ot->flag= OPTYPE_UNDO;
855
856         /* properties */
857         RNA_def_boolean(ot->srna, "extend", 0,
858                 "Extend", "Extend selection rather than clearing the existing selection");
859         RNA_def_float_vector(ot->srna, "location", 2, NULL, -FLT_MAX, FLT_MAX,
860                 "Location", "Mouse location in normalized coordinates, 0.0 to 1.0 is within the image bounds", -100.0f, 100.0f);
861 }
862
863 /********************** border select operator *********************/
864
865 static int border_select_exec(bContext *C, wmOperator *op)
866 {
867         SpaceClip *sc= CTX_wm_space_clip(C);
868         MovieClip *clip= ED_space_clip(sc);
869         MovieTrackingTrack *track;
870         rcti rect;
871         rctf rectf;
872         int change= 0, mode, extend;
873
874         /* get rectangle from operator */
875         rect.xmin= RNA_int_get(op->ptr, "xmin");
876         rect.ymin= RNA_int_get(op->ptr, "ymin");
877         rect.xmax= RNA_int_get(op->ptr, "xmax");
878         rect.ymax= RNA_int_get(op->ptr, "ymax");
879
880         ED_clip_point_stable_pos(C, rect.xmin, rect.ymin, &rectf.xmin, &rectf.ymin);
881         ED_clip_point_stable_pos(C, rect.xmax, rect.ymax, &rectf.xmax, &rectf.ymax);
882
883         mode= RNA_int_get(op->ptr, "gesture_mode");
884         extend= RNA_boolean_get(op->ptr, "extend");
885
886         /* do actual selection */
887         track= clip->tracking.tracks.first;
888         while(track) {
889                 if((track->flag&TRACK_HIDDEN)==0) {
890                         MovieTrackingMarker *marker= BKE_tracking_get_marker(track, sc->user.framenr);
891
892                         if(MARKER_VISIBLE(sc, marker)) {
893                                 if(BLI_in_rctf(&rectf, marker->pos[0], marker->pos[1])) {
894                                         BKE_tracking_track_flag(track, TRACK_AREA_ALL, SELECT, mode!=GESTURE_MODAL_SELECT);
895                                 }
896                                 else if(!extend) {
897                                         BKE_tracking_track_flag(track, TRACK_AREA_ALL, SELECT, 1);
898                                 }
899
900                                 change= 1;
901                         }
902                 }
903
904                 track= track->next;
905         }
906
907         if(change) {
908                 WM_event_add_notifier(C, NC_GEOM|ND_SELECT, NULL);
909
910                 return OPERATOR_FINISHED;
911         }
912
913         return OPERATOR_CANCELLED;
914 }
915
916 void CLIP_OT_select_border(wmOperatorType *ot)
917 {
918         /* identifiers */
919         ot->name= "Border Select";
920         ot->description= "Select markers using border selection";
921         ot->idname= "CLIP_OT_select_border";
922
923         /* api callbacks */
924         ot->invoke= WM_border_select_invoke;
925         ot->exec= border_select_exec;
926         ot->modal= WM_border_select_modal;
927         ot->poll= ED_space_clip_poll;
928
929         /* flags */
930         ot->flag= OPTYPE_UNDO;
931
932         /* properties */
933         WM_operator_properties_gesture_border(ot, TRUE);
934 }
935
936 /********************** circle select operator *********************/
937
938 static int marker_inside_ellipse(MovieTrackingMarker *marker, float offset[2], float ellipse[2])
939 {
940         /* normalized ellipse: ell[0] = scaleX, ell[1] = scaleY */
941         float x, y;
942
943         x= (marker->pos[0] - offset[0])*ellipse[0];
944         y= (marker->pos[1] - offset[1])*ellipse[1];
945
946         return x*x + y*y < 1.0f;
947 }
948
949 static int circle_select_exec(bContext *C, wmOperator *op)
950 {
951         SpaceClip *sc= CTX_wm_space_clip(C);
952         MovieClip *clip= ED_space_clip(sc);
953         ARegion *ar= CTX_wm_region(C);
954         MovieTrackingTrack *track;
955         int x, y, radius, width, height, mode, change= 0;
956         float zoomx, zoomy, offset[2], ellipse[2];
957
958         /* get operator properties */
959         x= RNA_int_get(op->ptr, "x");
960         y= RNA_int_get(op->ptr, "y");
961         radius= RNA_int_get(op->ptr, "radius");
962
963         mode= RNA_int_get(op->ptr, "gesture_mode");
964
965         /* compute ellipse and position in unified coordinates */
966         ED_space_clip_size(sc, &width, &height);
967         ED_space_clip_zoom(sc, ar, &zoomx, &zoomy);
968
969         ellipse[0]= width*zoomx/radius;
970         ellipse[1]= height*zoomy/radius;
971
972         ED_clip_point_stable_pos(C, x, y, &offset[0], &offset[1]);
973
974         /* do selection */
975         track= clip->tracking.tracks.first;
976         while(track) {
977                 if((track->flag&TRACK_HIDDEN)==0) {
978                         MovieTrackingMarker *marker= BKE_tracking_get_marker(track, sc->user.framenr);
979
980                         if(MARKER_VISIBLE(sc, marker) && marker_inside_ellipse(marker, offset, ellipse)) {
981                                 BKE_tracking_track_flag(track, TRACK_AREA_ALL, SELECT, mode!=GESTURE_MODAL_SELECT);
982
983                                 change= 1;
984                         }
985                 }
986
987                 track= track->next;
988         }
989
990         if(change) {
991                 WM_event_add_notifier(C, NC_GEOM|ND_SELECT, NULL);
992
993                 return OPERATOR_FINISHED;
994         }
995
996         return OPERATOR_CANCELLED;
997 }
998
999 void CLIP_OT_select_circle(wmOperatorType *ot)
1000 {
1001         /* identifiers */
1002         ot->name= "Circle Select";
1003         ot->description= "Select markers using circle selection";
1004         ot->idname= "CLIP_OT_select_circle";
1005
1006         /* api callbacks */
1007         ot->invoke= WM_gesture_circle_invoke;
1008         ot->modal= WM_gesture_circle_modal;
1009         ot->exec= circle_select_exec;
1010         ot->poll= ED_space_clip_poll;
1011
1012         /* flags */
1013         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1014
1015         /* properties */
1016         RNA_def_int(ot->srna, "x", 0, INT_MIN, INT_MAX, "X", "", INT_MIN, INT_MAX);
1017         RNA_def_int(ot->srna, "y", 0, INT_MIN, INT_MAX, "Y", "", INT_MIN, INT_MAX);
1018         RNA_def_int(ot->srna, "radius", 0, INT_MIN, INT_MAX, "Radius", "", INT_MIN, INT_MAX);
1019         RNA_def_int(ot->srna, "gesture_mode", 0, INT_MIN, INT_MAX, "Gesture Mode", "", INT_MIN, INT_MAX);
1020 }
1021
1022 /********************** select all operator *********************/
1023
1024 static int select_all_exec(bContext *C, wmOperator *op)
1025 {
1026         SpaceClip *sc= CTX_wm_space_clip(C);
1027         MovieClip *clip= ED_space_clip(sc);
1028         MovieTrackingTrack *track= NULL;        /* selected track */
1029         int action= RNA_enum_get(op->ptr, "action");
1030         int framenr= sc->user.framenr;
1031         int has_selection= 0;
1032
1033         if(action == SEL_TOGGLE){
1034                 action= SEL_SELECT;
1035                 track= clip->tracking.tracks.first;
1036                 while(track) {
1037                         if(TRACK_VIEW_SELECTED(sc, track)) {
1038                                 action= SEL_DESELECT;
1039                                 break;
1040                         }
1041
1042                         track= track->next;
1043                 }
1044         }
1045
1046         track= clip->tracking.tracks.first;
1047         while(track) {
1048                 if((track->flag&TRACK_HIDDEN)==0) {
1049                         MovieTrackingMarker *marker= BKE_tracking_get_marker(track, framenr);
1050
1051                         if(marker && MARKER_VISIBLE(sc, marker)) {
1052                                 switch (action) {
1053                                         case SEL_SELECT:
1054                                                 track->flag|= SELECT;
1055                                                 track->pat_flag|= SELECT;
1056                                                 track->search_flag|= SELECT;
1057                                                 break;
1058                                         case SEL_DESELECT:
1059                                                 track->flag&= ~SELECT;
1060                                                 track->pat_flag&= ~SELECT;
1061                                                 track->search_flag&= ~SELECT;
1062                                                 break;
1063                                         case SEL_INVERT:
1064                                                 track->flag^= SELECT;
1065                                                 track->pat_flag^= SELECT;
1066                                                 track->search_flag^= SELECT;
1067                                                 break;
1068                                 }
1069                         }
1070                 }
1071
1072                 if(TRACK_VIEW_SELECTED(sc, track))
1073                         has_selection= 1;
1074
1075                 track= track->next;
1076         }
1077
1078         if(!has_selection)
1079                 sc->flag&= ~SC_LOCK_SELECTION;
1080
1081         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, NULL);
1082
1083         return OPERATOR_FINISHED;
1084 }
1085
1086 void CLIP_OT_select_all(wmOperatorType *ot)
1087 {
1088         /* identifiers */
1089         ot->name= "Select or Deselect All";
1090         ot->description= "Change selection of all tracking markers";
1091         ot->idname= "CLIP_OT_select_all";
1092
1093         /* api callbacks */
1094         ot->exec= select_all_exec;
1095         ot->poll= ED_space_clip_poll;
1096
1097         /* flags */
1098         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1099
1100         WM_operator_properties_select_all(ot);
1101 }
1102
1103 /********************** select grouped operator *********************/
1104
1105 static int select_groped_exec(bContext *C, wmOperator *op)
1106 {
1107         SpaceClip *sc= CTX_wm_space_clip(C);
1108         MovieClip *clip= ED_space_clip(sc);
1109         MovieTrackingTrack *track;
1110         MovieTrackingMarker *marker;
1111         int group= RNA_enum_get(op->ptr, "group");
1112
1113         track= clip->tracking.tracks.first;
1114         while(track) {
1115                 int ok= 0;
1116
1117                 marker= BKE_tracking_get_marker(track, sc->user.framenr);
1118
1119                 if(group==0) { /* Keyframed */
1120                         ok= marker->framenr==sc->user.framenr && (marker->flag&MARKER_TRACKED)==0;
1121                 }
1122                 else if(group==1) { /* Estimated */
1123                         ok= marker->framenr!=sc->user.framenr;
1124                 }
1125                 else if(group==2) { /* tracked */
1126                         ok= marker->framenr==sc->user.framenr && (marker->flag&MARKER_TRACKED);
1127                 }
1128                 else if(group==3) { /* locked */
1129                         ok= track->flag&TRACK_LOCKED;
1130                 }
1131                 else if(group==4) { /* disabled */
1132                         ok= marker->flag&MARKER_DISABLED;
1133                 }
1134                 else if(group==5) { /* color */
1135                         if(clip->tracking.act_track) {
1136                                 ok= (track->flag&TRACK_CUSTOMCOLOR) == (clip->tracking.act_track->flag&TRACK_CUSTOMCOLOR);
1137
1138                                 if(ok && track->flag&TRACK_CUSTOMCOLOR)
1139                                         ok= equals_v3v3(track->color, clip->tracking.act_track->color);
1140                         }
1141                 }
1142                 else if(group==6) { /* failed */
1143                         ok= (track->flag&TRACK_HAS_BUNDLE) == 0;
1144                 }
1145
1146                 if(ok) {
1147                         track->flag|= SELECT;
1148                         if(sc->flag&SC_SHOW_MARKER_PATTERN) track->pat_flag|= SELECT;;
1149                         if(sc->flag&SC_SHOW_MARKER_SEARCH) track->search_flag|= SELECT;;
1150                 }
1151
1152                 track= track->next;
1153         }
1154
1155         WM_event_add_notifier(C, NC_MOVIECLIP|ND_DISPLAY, clip);
1156
1157         return OPERATOR_FINISHED;
1158 }
1159
1160 void CLIP_OT_select_grouped(wmOperatorType *ot)
1161 {
1162         static EnumPropertyItem select_group_items[] = {
1163                         {0, "KEYFRAMED", 0, "Keyframed tracks", "Select all keyframed tracks"},
1164                         {1, "ESTIMATED", 0, "Estimated tracks", "Select all estimated tracks"},
1165                         {2, "TRACKED", 0, "Tracked tracks", "Select all tracked tracks"},
1166                         {3, "LOCKED", 0, "Locked tracks", "Select all locked tracks"},
1167                         {4, "DISABLED", 0, "Disabled tracks", "Select all disabled tracks"},
1168                         {5, "COLOR", 0, "Tracks with same color", "Select all tracks with same color as active track"},
1169                         {6, "FAILED", 0, "Failed Tracks", "Select all tracks which failed to be reconstructed"},
1170                         {0, NULL, 0, NULL, NULL}
1171         };
1172
1173         /* identifiers */
1174         ot->name= "Select Grouped";
1175         ot->description= "Joint Selected Tracks";
1176         ot->idname= "CLIP_OT_select_grouped";
1177
1178         /* api callbacks */
1179         ot->exec= select_groped_exec;
1180         ot->poll= space_clip_frame_poll;
1181
1182         /* flags */
1183         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1184
1185         /* proeprties */
1186         RNA_def_enum(ot->srna, "group", select_group_items, TRACK_CLEAR_REMAINED, "Action", "Clear action to execute");
1187 }
1188
1189 /********************** track operator *********************/
1190
1191 typedef struct TrackMarkersJob {
1192         struct MovieTrackingContext *context;   /* tracking context */
1193         int sfra, efra, lastfra;        /* Start, end and recently tracked frames */
1194         int backwards;                          /* Backwards tracking flag */
1195         MovieClip *clip;                        /* Clip which is tracking */
1196         float delay;                            /* Delay in milliseconds to allow tracking at fixed FPS */
1197
1198         struct Main *main;
1199         struct Scene *scene;
1200         struct bScreen *screen;
1201 } TrackMarkersJob;
1202
1203 static int track_markers_testbreak(void)
1204 {
1205         return G.afbreek;
1206 }
1207
1208 static int track_count_markers(SpaceClip *sc, MovieClip *clip)
1209 {
1210         int tot= 0;
1211         MovieTrackingTrack *track;
1212
1213         track= clip->tracking.tracks.first;
1214         while(track) {
1215                 if(TRACK_VIEW_SELECTED(sc, track) && (track->flag&TRACK_LOCKED)==0)
1216                         tot++;
1217
1218                 track= track->next;
1219         }
1220
1221         return tot;
1222 }
1223
1224 static void track_init_markers(SpaceClip *sc, MovieClip *clip)
1225 {
1226         MovieTrackingTrack *track;
1227         int framenr= sc->user.framenr, hidden= 0;
1228
1229         if((sc->flag&SC_SHOW_MARKER_PATTERN)==0) hidden|= TRACK_AREA_PAT;
1230         if((sc->flag&SC_SHOW_MARKER_SEARCH)==0) hidden|= TRACK_AREA_SEARCH;
1231
1232         track= clip->tracking.tracks.first;
1233         while(track) {
1234                 if(hidden)
1235                         BKE_tracking_track_flag(track, hidden, SELECT, 1);
1236
1237                 if(TRACK_SELECTED(track)) {
1238                         if((track->flag&TRACK_HIDDEN)==0 && (track->flag&TRACK_LOCKED)==0)
1239                                 BKE_tracking_ensure_marker(track, framenr);
1240                 }
1241
1242                 track= track->next;
1243         }
1244 }
1245
1246 static int track_markers_check_direction(int backwards, int curfra, int efra)
1247 {
1248         if(backwards) {
1249                 if(curfra<efra) return 0;
1250         }
1251         else {
1252                 if(curfra>efra) return 0;
1253         }
1254
1255         return 1;
1256 }
1257
1258 static int track_markers_initjob(bContext *C, TrackMarkersJob *tmj, int backwards)
1259 {
1260         SpaceClip *sc= CTX_wm_space_clip(C);
1261         MovieClip *clip= ED_space_clip(sc);
1262         Scene *scene= CTX_data_scene(C);
1263         MovieTrackingSettings *settings= &clip->tracking.settings;
1264
1265         tmj->sfra= sc->user.framenr;
1266         tmj->clip= clip;
1267         tmj->backwards= backwards;
1268
1269         if(backwards) tmj->efra= SFRA;
1270         else tmj->efra= EFRA;
1271
1272         /* limit frames to be tracked by user setting */
1273         if(settings->frames_limit) {
1274                 if(backwards) tmj->efra= MAX2(tmj->efra, tmj->sfra-settings->frames_limit);
1275                 else tmj->efra= MIN2(tmj->efra, tmj->sfra+settings->frames_limit);
1276         }
1277
1278         if(settings->speed!=TRACKING_SPEED_FASTEST) {
1279                 tmj->delay= 1.0f/scene->r.frs_sec*1000.0f;
1280
1281                 if(settings->speed==TRACKING_SPEED_HALF) tmj->delay*= 2;
1282                 else if(settings->speed==TRACKING_SPEED_QUARTER) tmj->delay*= 4;
1283                 else if(settings->speed==TRACKING_SPEED_DOUBLE) tmj->delay/= 2;
1284         }
1285
1286         track_init_markers(sc, clip);
1287
1288         tmj->context= BKE_tracking_context_new(clip, &sc->user, backwards, 1);
1289
1290         clip->tracking_context= tmj->context;
1291
1292         tmj->lastfra= tmj->sfra;
1293
1294         /* XXX: silly to store this, but this data is needed to update scene and movieclip
1295                 frame numbers when tracking is finished. This introduces better feedback for artists.
1296                 Maybe there's another way to solve this problem, but can't think better way atm.
1297                 Anyway, this way isn't more unstable as animation rendering animation
1298                 which uses the same approach (except storing screen). */
1299         tmj->scene= scene;
1300         tmj->main= CTX_data_main(C);
1301         tmj->screen= CTX_wm_screen(C);
1302
1303         return track_markers_check_direction(backwards, tmj->sfra, tmj->efra);
1304 }
1305
1306 static void track_markers_startjob(void *tmv, short *stop, short *do_update, float *progress)
1307 {
1308         TrackMarkersJob *tmj= (TrackMarkersJob *)tmv;
1309         int framenr= tmj->sfra;
1310         //double t= PIL_check_seconds_timer();
1311
1312         while(framenr != tmj->efra) {
1313                 if(tmj->delay>0) {
1314                         /* tracking should happen with fixed fps. Calculate time
1315                            using current timer value before tracking frame and after.
1316
1317                            Small (and maybe unneeded optimization): do not calculate exec_time
1318                            for "Fastest" tracking */
1319
1320                         double start_time= PIL_check_seconds_timer(), exec_time;
1321
1322                         if(!BKE_tracking_next(tmj->context))
1323                                 break;
1324
1325                         exec_time= PIL_check_seconds_timer()-start_time;
1326                         if(tmj->delay > (float)exec_time)
1327                                 PIL_sleep_ms(tmj->delay-(float)exec_time);
1328                 } else if(!BKE_tracking_next(tmj->context))
1329                                 break;
1330
1331                 *do_update= 1;
1332                 *progress=(float)(framenr-tmj->sfra) / (tmj->efra-tmj->sfra);
1333
1334                 if(tmj->backwards) framenr--;
1335                 else framenr++;
1336
1337                 tmj->lastfra= framenr;
1338
1339                 if(*stop || track_markers_testbreak())
1340                         break;
1341         }
1342
1343         //printf("Tracking time: %lf\n", PIL_check_seconds_timer()-t);
1344 }
1345
1346 static void track_markers_updatejob(void *tmv)
1347 {
1348         TrackMarkersJob *tmj= (TrackMarkersJob *)tmv;
1349
1350         BKE_tracking_sync(tmj->context);
1351 }
1352
1353 static void track_markers_freejob(void *tmv)
1354 {
1355         TrackMarkersJob *tmj= (TrackMarkersJob *)tmv;
1356
1357         tmj->clip->tracking_context= NULL;
1358         tmj->scene->r.cfra= tmj->lastfra;
1359         ED_update_for_newframe(tmj->main, tmj->scene, tmj->screen, 0);
1360
1361         BKE_tracking_sync(tmj->context);
1362         BKE_tracking_context_free(tmj->context);
1363
1364         MEM_freeN(tmj);
1365
1366         WM_main_add_notifier(NC_SCENE|ND_FRAME, tmj->scene);
1367 }
1368
1369 static int track_markers_exec(bContext *C, wmOperator *op)
1370 {
1371         SpaceClip *sc= CTX_wm_space_clip(C);
1372         MovieClip *clip= ED_space_clip(sc);
1373         Scene *scene= CTX_data_scene(C);
1374         struct MovieTrackingContext *context;
1375         int framenr= sc->user.framenr;
1376         int sfra= framenr, efra;
1377         int backwards= RNA_boolean_get(op->ptr, "backwards");
1378         int sequence= RNA_boolean_get(op->ptr, "sequence");
1379         MovieTrackingSettings *settings= &clip->tracking.settings;
1380
1381         if(track_count_markers(sc, clip)==0)
1382                 return OPERATOR_CANCELLED;
1383
1384         if(backwards) efra= SFRA;
1385         else efra= EFRA;
1386
1387         /* limit frames to be tracked by user setting */
1388         if(settings->frames_limit) {
1389                 if(backwards) efra= MAX2(efra, sfra-settings->frames_limit);
1390                 else efra= MIN2(efra, sfra+settings->frames_limit);
1391         }
1392
1393         if(!track_markers_check_direction(backwards, framenr, efra))
1394                 return OPERATOR_CANCELLED;
1395
1396         track_init_markers(sc, clip);
1397
1398         /* do not disable tracks due to threshold when tracking frame-by-frame */
1399         context= BKE_tracking_context_new(clip, &sc->user, backwards, sequence);
1400
1401         while(framenr != efra) {
1402                 if(!BKE_tracking_next(context))
1403                         break;
1404
1405                 if(backwards) framenr--;
1406                 else framenr++;
1407
1408                 if(!sequence)
1409                         break;
1410         }
1411
1412         BKE_tracking_sync(context);
1413         BKE_tracking_context_free(context);
1414
1415         /* update scene current frame to the lastes tracked frame */
1416         scene->r.cfra= framenr;
1417
1418         WM_event_add_notifier(C, NC_MOVIECLIP|NA_EVALUATED, clip);
1419         WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene);
1420
1421         return OPERATOR_FINISHED;
1422 }
1423
1424 static int track_markers_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1425 {
1426         TrackMarkersJob *tmj;
1427         ScrArea *sa= CTX_wm_area(C);
1428         SpaceClip *sc= CTX_wm_space_clip(C);
1429         MovieClip *clip= ED_space_clip(sc);
1430         wmJob *steve;
1431         int backwards= RNA_boolean_get(op->ptr, "backwards");
1432         int sequence= RNA_boolean_get(op->ptr, "sequence");
1433
1434         if(clip->tracking_context)
1435                 return OPERATOR_CANCELLED;
1436
1437         if(track_count_markers(sc, clip)==0)
1438                 return OPERATOR_CANCELLED;
1439
1440         if(!sequence)
1441                 return track_markers_exec(C, op);
1442
1443         tmj= MEM_callocN(sizeof(TrackMarkersJob), "TrackMarkersJob data");
1444         if(!track_markers_initjob(C, tmj, backwards)) {
1445                 track_markers_freejob(tmj);
1446
1447                 return OPERATOR_CANCELLED;
1448         }
1449
1450         /* setup job */
1451         steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), sa, "Track Markers", WM_JOB_PROGRESS);
1452         WM_jobs_customdata(steve, tmj, track_markers_freejob);
1453
1454         /* if there's delay set in tracking job, tracking should happen
1455            with fixed FPS. To deal with editor refresh we have to syncronize
1456            tracks from job and tracks in clip. Do this in timer callback
1457            to prevent threading conflicts. */
1458         if(tmj->delay>0) WM_jobs_timer(steve, tmj->delay/1000.0f, NC_MOVIECLIP|NA_EVALUATED, 0);
1459         else WM_jobs_timer(steve, 0.2, NC_MOVIECLIP|NA_EVALUATED, 0);
1460
1461         WM_jobs_callbacks(steve, track_markers_startjob, NULL, track_markers_updatejob, NULL);
1462
1463         G.afbreek= 0;
1464
1465         WM_jobs_start(CTX_wm_manager(C), steve);
1466         WM_cursor_wait(0);
1467
1468         /* add modal handler for ESC */
1469         WM_event_add_modal_handler(C, op);
1470
1471         return OPERATOR_RUNNING_MODAL;
1472 }
1473
1474 static int track_markers_modal(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
1475 {
1476         /* no running tracking, remove handler and pass through */
1477         if(0==WM_jobs_test(CTX_wm_manager(C), CTX_wm_area(C)))
1478                 return OPERATOR_FINISHED|OPERATOR_PASS_THROUGH;
1479
1480         /* running tracking */
1481         switch (event->type) {
1482                 case ESCKEY:
1483                         return OPERATOR_RUNNING_MODAL;
1484                         break;
1485         }
1486
1487         return OPERATOR_PASS_THROUGH;
1488 }
1489
1490 void CLIP_OT_track_markers(wmOperatorType *ot)
1491 {
1492         /* identifiers */
1493         ot->name= "Track Markers";
1494         ot->description= "Track selected markers";
1495         ot->idname= "CLIP_OT_track_markers";
1496
1497         /* api callbacks */
1498         ot->exec= track_markers_exec;
1499         ot->invoke= track_markers_invoke;
1500         ot->poll= space_clip_frame_poll;
1501         ot->modal= track_markers_modal;
1502
1503         /* flags */
1504         ot->flag= OPTYPE_UNDO;
1505
1506         /* properties */
1507         RNA_def_boolean(ot->srna, "backwards", 0, "Backwards", "Do backwards tracking");
1508         RNA_def_boolean(ot->srna, "sequence", 0, "Track Sequence", "Track marker during image sequence rather than single image");
1509 }
1510
1511 /********************** solve camera operator *********************/
1512
1513 typedef struct {
1514         Scene *scene;
1515         MovieClip *clip;
1516         MovieClipUser user;
1517
1518         ReportList *reports;
1519
1520         char stats_message[256];
1521
1522         struct MovieReconstructContext *context;
1523 } SolveCameraJob;
1524
1525 static int solve_camera_initjob(bContext *C, SolveCameraJob *scj, wmOperator *op, char *error_msg, int max_error)
1526 {
1527         SpaceClip *sc= CTX_wm_space_clip(C);
1528         MovieClip *clip= ED_space_clip(sc);
1529         Scene *scene= CTX_data_scene(C);
1530         MovieTracking *tracking= &clip->tracking;
1531         MovieTrackingSettings *settings= &clip->tracking.settings;
1532         int width, height;
1533
1534         if(!BKE_tracking_can_reconstruct(tracking, error_msg, max_error))
1535                 return 0;
1536
1537         /* could fail if footage uses images with different sizes */
1538         BKE_movieclip_get_size(clip, &sc->user, &width, &height);
1539
1540         scj->clip= clip;
1541         scj->scene= scene;
1542         scj->reports= op->reports;
1543         scj->user= sc->user;
1544
1545         scj->context= BKE_tracking_reconstruction_context_new(tracking,
1546                         settings->keyframe1, settings->keyframe2, width, height);
1547
1548         tracking->stats= MEM_callocN(sizeof(MovieTrackingStats), "solve camera stats");
1549
1550         return 1;
1551 }
1552
1553 static void solve_camera_updatejob(void *scv)
1554 {
1555         SolveCameraJob *scj= (SolveCameraJob *)scv;
1556         MovieTracking *tracking= &scj->clip->tracking;
1557
1558         BLI_strncpy(tracking->stats->message, scj->stats_message, sizeof(tracking->stats->message));
1559 }
1560
1561 static void solve_camera_startjob(void *scv, short *stop, short *do_update, float *progress)
1562 {
1563         SolveCameraJob *scj= (SolveCameraJob *)scv;
1564
1565         BKE_tracking_solve_reconstruction(scj->context, stop, do_update, progress,
1566                         scj->stats_message, sizeof(scj->stats_message));
1567 }
1568
1569 static void solve_camera_freejob(void *scv)
1570 {
1571         SolveCameraJob *scj= (SolveCameraJob *)scv;
1572         MovieTracking *tracking= &scj->clip->tracking;
1573         Scene *scene= scj->scene;
1574         MovieClip *clip= scj->clip;
1575         int solved;
1576
1577         if(!scj->context) {
1578                 /* job weren't fully initialized due to some error */
1579                 MEM_freeN(scj);
1580                 return;
1581         }
1582
1583         solved= BKE_tracking_finish_reconstruction(scj->context, tracking);
1584
1585         if(!solved)
1586                 BKE_report(scj->reports, RPT_WARNING, "Some data failed to reconstruct, see console for details");
1587         else
1588                 BKE_reportf(scj->reports, RPT_INFO, "Average reprojection error %.3f", tracking->reconstruction.error);
1589
1590         /* set currently solved clip as active for scene */
1591         if(scene->clip)
1592                 id_us_min(&clip->id);
1593
1594         scene->clip= clip;
1595         id_us_plus(&clip->id);
1596
1597         /* set blender camera focal length so result would look fine there */
1598         if(!scene->camera)
1599                 scene->camera= scene_find_camera(scene);
1600
1601         if(scene->camera) {
1602                 Camera *camera= (Camera*)scene->camera->data;
1603                 int width, height;
1604
1605                 BKE_movieclip_get_size(clip, &scj->user, &width, &height);
1606
1607                 BKE_tracking_camera_to_blender(tracking, scene, camera, width, height);
1608
1609                 WM_main_add_notifier(NC_OBJECT, camera);
1610         }
1611
1612         MEM_freeN(tracking->stats);
1613         tracking->stats= NULL;
1614
1615         DAG_id_tag_update(&clip->id, 0);
1616
1617         WM_main_add_notifier(NC_MOVIECLIP|NA_EVALUATED, clip);
1618         WM_main_add_notifier(NC_OBJECT|ND_TRANSFORM, NULL);
1619
1620         /* update active clip displayed in scene buttons */
1621         WM_main_add_notifier(NC_SCENE, scene);
1622
1623         BKE_tracking_reconstruction_context_free(scj->context);
1624         MEM_freeN(scj);
1625 }
1626
1627 static int solve_camera_exec(bContext *C, wmOperator *op)
1628 {
1629         SolveCameraJob *scj;
1630         char error_msg[256]= "\0";
1631
1632         scj= MEM_callocN(sizeof(SolveCameraJob), "SolveCameraJob data");
1633         if(!solve_camera_initjob(C, scj, op, error_msg, sizeof(error_msg))) {
1634                 if(error_msg[0])
1635                         BKE_report(op->reports, RPT_ERROR, error_msg);
1636
1637                 solve_camera_freejob(scj);
1638
1639                 return OPERATOR_CANCELLED;
1640         }
1641
1642         solve_camera_startjob(scj, NULL, NULL, NULL);
1643
1644         solve_camera_freejob(scj);
1645
1646         return OPERATOR_FINISHED;
1647 }
1648
1649 static int solve_camera_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1650 {
1651         SolveCameraJob *scj;
1652         ScrArea *sa= CTX_wm_area(C);
1653         SpaceClip *sc= CTX_wm_space_clip(C);
1654         MovieClip *clip= ED_space_clip(sc);
1655         MovieTracking *tracking= &clip->tracking;
1656         wmJob *steve;
1657         char error_msg[256]= "\0";
1658
1659         scj= MEM_callocN(sizeof(SolveCameraJob), "SolveCameraJob data");
1660         if(!solve_camera_initjob(C, scj, op, error_msg, sizeof(error_msg))) {
1661                 if(error_msg[0])
1662                         BKE_report(op->reports, RPT_ERROR, error_msg);
1663
1664                 solve_camera_freejob(scj);
1665
1666                 return OPERATOR_CANCELLED;
1667         }
1668
1669         BLI_strncpy(tracking->stats->message, "Solving camera | Preparing solve", sizeof(tracking->stats->message));
1670
1671         /* hide reconstruction statistics from previous solve */
1672         clip->tracking.reconstruction.flag&= ~TRACKING_RECONSTRUCTED;
1673         WM_event_add_notifier(C, NC_MOVIECLIP|NA_EVALUATED, clip);
1674
1675         /* setup job */
1676         steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), sa, "Solve Camera", WM_JOB_PROGRESS);
1677         WM_jobs_customdata(steve, scj, solve_camera_freejob);
1678         WM_jobs_timer(steve, 0.1, NC_MOVIECLIP|NA_EVALUATED, 0);
1679         WM_jobs_callbacks(steve, solve_camera_startjob, NULL, solve_camera_updatejob, NULL);
1680
1681         G.afbreek= 0;
1682
1683         WM_jobs_start(CTX_wm_manager(C), steve);
1684         WM_cursor_wait(0);
1685
1686         /* add modal handler for ESC */
1687         WM_event_add_modal_handler(C, op);
1688
1689         return OPERATOR_RUNNING_MODAL;
1690 }
1691
1692 static int solve_camera_modal(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
1693 {
1694         /* no running solver, remove handler and pass through */
1695         if(0==WM_jobs_test(CTX_wm_manager(C), CTX_wm_area(C)))
1696                 return OPERATOR_FINISHED|OPERATOR_PASS_THROUGH;
1697
1698         /* running tracking */
1699         switch (event->type) {
1700                 case ESCKEY:
1701                         return OPERATOR_RUNNING_MODAL;
1702                         break;
1703         }
1704
1705         return OPERATOR_PASS_THROUGH;
1706 }
1707
1708 void CLIP_OT_solve_camera(wmOperatorType *ot)
1709 {
1710         /* identifiers */
1711         ot->name= "Solve Camera";
1712         ot->description= "Solve camera motion from tracks";
1713         ot->idname= "CLIP_OT_solve_camera";
1714
1715         /* api callbacks */
1716         ot->exec= solve_camera_exec;
1717         ot->invoke= solve_camera_invoke;
1718         ot->modal= solve_camera_modal;
1719         ot->poll= ED_space_clip_poll;
1720
1721         /* flags */
1722         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1723 }
1724
1725 /********************** clear solution operator *********************/
1726
1727 static int clear_solution_exec(bContext *C, wmOperator *UNUSED(op))
1728 {
1729         SpaceClip *sc= CTX_wm_space_clip(C);
1730         MovieClip *clip= ED_space_clip(sc);
1731         MovieTracking *tracking= &clip->tracking;
1732         MovieTrackingTrack *track= tracking->tracks.first;
1733
1734         while(track) {
1735                 track->flag&= ~TRACK_HAS_BUNDLE;
1736
1737                 track= track->next;
1738         }
1739
1740         if(tracking->reconstruction.cameras)
1741                 MEM_freeN(tracking->reconstruction.cameras);
1742
1743         tracking->reconstruction.cameras= NULL;
1744         tracking->reconstruction.camnr= 0;
1745
1746         tracking->reconstruction.flag&= ~TRACKING_RECONSTRUCTED;
1747
1748         DAG_id_tag_update(&clip->id, 0);
1749
1750         WM_event_add_notifier(C, NC_MOVIECLIP|NA_EVALUATED, clip);
1751         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, NULL);
1752
1753         return OPERATOR_FINISHED;
1754 }
1755
1756 void CLIP_OT_clear_solution(wmOperatorType *ot)
1757 {
1758         /* identifiers */
1759         ot->name= "Clear Solution";
1760         ot->description= "Clear all calculated data";
1761         ot->idname= "CLIP_OT_clear_solution";
1762
1763         /* api callbacks */
1764         ot->exec= clear_solution_exec;
1765         ot->poll= ED_space_clip_poll;
1766
1767         /* flags */
1768         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1769 }
1770
1771 /********************** clear track operator *********************/
1772
1773 static int clear_track_path_exec(bContext *C, wmOperator *op)
1774 {
1775         SpaceClip *sc= CTX_wm_space_clip(C);
1776         MovieClip *clip= ED_space_clip(sc);
1777         MovieTrackingTrack *track;
1778         int action= RNA_enum_get(op->ptr, "action");
1779
1780         track= clip->tracking.tracks.first;
1781         while(track) {
1782                 if(TRACK_VIEW_SELECTED(sc, track))
1783                         BKE_tracking_clear_path(track, sc->user.framenr, action);
1784
1785                 track= track->next;
1786         }
1787
1788         WM_event_add_notifier(C, NC_MOVIECLIP|NA_EVALUATED, clip);
1789
1790         return OPERATOR_FINISHED;
1791 }
1792
1793 void CLIP_OT_clear_track_path(wmOperatorType *ot)
1794 {
1795         static EnumPropertyItem clear_path_actions[] = {
1796                         {TRACK_CLEAR_UPTO, "UPTO", 0, "Clear up-to", "Clear path up to current frame"},
1797                         {TRACK_CLEAR_REMAINED, "REMAINED", 0, "Clear remained", "Clear path at remaining frames (after current)"},
1798                         {TRACK_CLEAR_ALL, "ALL", 0, "Clear all", "Clear the whole path"},
1799                         {0, NULL, 0, NULL, NULL}
1800         };
1801
1802         /* identifiers */
1803         ot->name= "Clear Track Path";
1804         ot->description= "Clear tracks after/before current position or clear the whole track";
1805         ot->idname= "CLIP_OT_clear_track_path";
1806
1807         /* api callbacks */
1808         ot->exec= clear_track_path_exec;
1809         ot->poll= ED_space_clip_poll;
1810
1811         /* flags */
1812         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1813
1814         /* proeprties */
1815         RNA_def_enum(ot->srna, "action", clear_path_actions, TRACK_CLEAR_REMAINED, "Action", "Clear action to execute");
1816 }
1817
1818 /********************** disable markers operator *********************/
1819
1820 static int disable_markers_exec(bContext *C, wmOperator *op)
1821 {
1822         SpaceClip *sc= CTX_wm_space_clip(C);
1823         MovieClip *clip= ED_space_clip(sc);
1824         MovieTracking *tracking= &clip->tracking;
1825         MovieTrackingTrack *track= tracking->tracks.first;
1826         int action= RNA_enum_get(op->ptr, "action");
1827
1828         while(track) {
1829                 if(TRACK_VIEW_SELECTED(sc, track) && (track->flag&TRACK_LOCKED)==0) {
1830                         MovieTrackingMarker *marker= BKE_tracking_ensure_marker(track, sc->user.framenr);
1831
1832                         if(action==0) marker->flag|= MARKER_DISABLED;
1833                         else if(action==1) marker->flag&= ~MARKER_DISABLED;
1834                         else marker->flag^= MARKER_DISABLED;
1835                 }
1836
1837                 track= track->next;
1838         }
1839
1840         DAG_id_tag_update(&clip->id, 0);
1841
1842         WM_event_add_notifier(C, NC_MOVIECLIP|NA_EVALUATED, clip);
1843
1844         return OPERATOR_FINISHED;
1845 }
1846
1847 void CLIP_OT_disable_markers(wmOperatorType *ot)
1848 {
1849         static EnumPropertyItem actions_items[] = {
1850                         {0, "DISABLE", 0, "Disable", "Disable selected markers"},
1851                         {1, "ENABLE", 0, "Enable", "Enable selected markers"},
1852                         {2, "TOGGLE", 0, "Toggle", "Toggle disabled flag for selected markers"},
1853                         {0, NULL, 0, NULL, NULL}
1854         };
1855
1856         /* identifiers */
1857         ot->name= "Disable Markers";
1858         ot->description= "Disable/enable selected markers";
1859         ot->idname= "CLIP_OT_disable_markers";
1860
1861         /* api callbacks */
1862         ot->exec= disable_markers_exec;
1863         ot->poll= ED_space_clip_poll;
1864
1865         /* flags */
1866         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1867
1868         /* properties */
1869         RNA_def_enum(ot->srna, "action", actions_items, 0, "Action", "Disable action to execute");
1870 }
1871
1872 /********************** set origin operator *********************/
1873
1874 static int count_selected_bundles(bContext *C)
1875 {
1876         SpaceClip *sc= CTX_wm_space_clip(C);
1877         MovieClip *clip= ED_space_clip(sc);
1878         MovieTrackingTrack *track;
1879         int tot= 0;
1880
1881         track= clip->tracking.tracks.first;
1882         while(track) {
1883                 if(TRACK_VIEW_SELECTED(sc, track) && (track->flag&TRACK_HAS_BUNDLE))
1884                         tot++;
1885
1886                 track= track->next;
1887         }
1888
1889         return tot;
1890 }
1891
1892 static int set_origin_exec(bContext *C, wmOperator *op)
1893 {
1894         SpaceClip *sc= CTX_wm_space_clip(C);
1895         MovieClip *clip= ED_space_clip(sc);
1896         MovieTrackingTrack *track;
1897         Scene *scene= CTX_data_scene(C);
1898         Object *parent= scene->camera;
1899         float mat[4][4], vec[3];
1900
1901         if(count_selected_bundles(C)!=1) {
1902                 BKE_report(op->reports, RPT_ERROR, "Track with bundle should be selected to define origin position");
1903                 return OPERATOR_CANCELLED;
1904         }
1905
1906         if(scene->camera->parent)
1907                 parent= scene->camera->parent;
1908
1909         track= clip->tracking.tracks.first;
1910         while(track) {
1911                 if(TRACK_VIEW_SELECTED(sc, track))
1912                         break;
1913
1914                 track= track->next;
1915         }
1916
1917         BKE_get_tracking_mat(scene, NULL, mat);
1918         mul_v3_m4v3(vec, mat, track->bundle_pos);
1919
1920         sub_v3_v3(parent->loc, vec);
1921
1922         DAG_id_tag_update(&clip->id, 0);
1923         DAG_id_tag_update(&parent->id, OB_RECALC_OB);
1924
1925         WM_event_add_notifier(C, NC_MOVIECLIP|NA_EVALUATED, clip);
1926         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
1927
1928         return OPERATOR_FINISHED;
1929 }
1930
1931 void CLIP_OT_set_origin(wmOperatorType *ot)
1932 {
1933         /* identifiers */
1934         ot->name= "Set Origin";
1935         ot->description= "Set active marker as origin by moving camera (or it's parent if present) in 3D space";
1936         ot->idname= "CLIP_OT_set_origin";
1937
1938         /* api callbacks */
1939         ot->exec= set_origin_exec;
1940         ot->poll= space_clip_frame_camera_poll;
1941
1942         /* flags */
1943         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1944 }
1945
1946 /********************** set floor operator *********************/
1947
1948 static void set_axis(Scene *scene,  Object *ob, MovieTrackingTrack *track, char axis)
1949 {
1950         float mat[4][4], vec[3], obmat[4][4];
1951
1952         BKE_get_tracking_mat(scene, NULL, mat);
1953         mul_v3_m4v3(vec, mat, track->bundle_pos);
1954
1955         if(len_v2(vec) < 1e-3f)
1956                 return;
1957
1958         unit_m4(mat);
1959
1960         if(axis=='X') {
1961                 if(fabsf(vec[1])<1e-3f) {
1962                         mat[0][0]= -1.0f; mat[0][1]= 0.0f; mat[0][2]= 0.0f;
1963                         mat[1][0]= 0.0f; mat[1][1]= -1.0f; mat[1][2]= 0.0f;
1964                         mat[2][0]= 0.0f; mat[2][1]= 0.0f; mat[2][2]= 1.0f;
1965                 } else {
1966                         copy_v3_v3(mat[0], vec);
1967                         mat[0][2]= 0.0f;
1968                         mat[2][0]= 0.0f; mat[2][1]= 0.0f; mat[2][2]= 1.0f;
1969                         cross_v3_v3v3(mat[1], mat[2], mat[0]);
1970                 }
1971         } else {
1972                 if(fabsf(vec[0])<1e-3f) {
1973                         mat[0][0]= -1.0f; mat[0][1]= 0.0f; mat[0][2]= 0.0f;
1974                         mat[1][0]= 0.0f; mat[1][1]= -1.0f; mat[1][2]= 0.0f;
1975                         mat[2][0]= 0.0f; mat[2][1]= 0.0f; mat[2][2]= 1.0f;
1976                 } else {
1977                         copy_v3_v3(mat[1], vec);
1978                         mat[1][2]= 0.0f;
1979                         mat[2][0]= 0.0f; mat[2][1]= 0.0f; mat[2][2]= 1.0f;
1980                         cross_v3_v3v3(mat[0], mat[1], mat[2]);
1981                 }
1982         }
1983
1984         normalize_v3(mat[0]);
1985         normalize_v3(mat[1]);
1986         normalize_v3(mat[2]);
1987
1988         invert_m4(mat);
1989
1990         object_to_mat4(ob, obmat);
1991         mul_m4_m4m4(mat, obmat, mat);
1992         object_apply_mat4(ob, mat, 0, 0);
1993 }
1994
1995 static int set_floor_exec(bContext *C, wmOperator *op)
1996 {
1997         SpaceClip *sc= CTX_wm_space_clip(C);
1998         MovieClip *clip= ED_space_clip(sc);
1999         Scene *scene= CTX_data_scene(C);
2000         MovieTrackingTrack *track, *axis_track= NULL;
2001         Object *camera= scene->camera;
2002         Object *parent= camera;
2003         int tot= 0;
2004         float vec[3][3], mat[4][4], obmat[4][4], newmat[4][4], orig[3]= {0.0f, 0.0f, 0.0f};
2005         float rot[4][4]={{0.0f, 0.0f, -1.0f, 0.0f},
2006                          {0.0f, 1.0f, 0.0f, 0.0f},
2007                          {1.0f, 0.0f, 0.0f, 0.0f},
2008                          {0.0f, 0.0f, 0.0f, 1.0f}};     /* 90 degrees Y-axis rotation matrix */
2009
2010         if(count_selected_bundles(C)!=3) {
2011                 BKE_report(op->reports, RPT_ERROR, "Three tracks with bundles are needed to orient the floor");
2012                 return OPERATOR_CANCELLED;
2013         }
2014
2015         if(scene->camera->parent)
2016                 parent= scene->camera->parent;
2017
2018         BKE_get_tracking_mat(scene, NULL, mat);
2019
2020         /* get 3 bundles to use as reference */
2021         track= clip->tracking.tracks.first;
2022         while(track && tot<3) {
2023                 if(track->flag&TRACK_HAS_BUNDLE && TRACK_VIEW_SELECTED(sc, track)) {
2024                         mul_v3_m4v3(vec[tot], mat, track->bundle_pos);
2025
2026                         if(tot==0 || track==clip->tracking.act_track)
2027                                 copy_v3_v3(orig, vec[tot]);
2028                         else
2029                                 axis_track= track;
2030
2031                         tot++;
2032                 }
2033
2034                 track= track->next;
2035         }
2036
2037         sub_v3_v3(vec[1], vec[0]);
2038         sub_v3_v3(vec[2], vec[0]);
2039
2040         /* construct ortho-normal basis */
2041         unit_m4(mat);
2042
2043         cross_v3_v3v3(mat[0], vec[1], vec[2]);
2044         copy_v3_v3(mat[1], vec[1]);
2045         cross_v3_v3v3(mat[2], mat[0], mat[1]);
2046
2047         normalize_v3(mat[0]);
2048         normalize_v3(mat[1]);
2049         normalize_v3(mat[2]);
2050
2051         /* move to origin point */
2052         mat[3][0]= orig[0];
2053         mat[3][1]= orig[1];
2054         mat[3][2]= orig[2];
2055
2056         invert_m4(mat);
2057
2058         object_to_mat4(parent, obmat);
2059         mul_m4_m4m4(mat, obmat, mat);
2060         mul_m4_m4m4(newmat, mat, rot);
2061         object_apply_mat4(parent, newmat, 0, 0);
2062
2063         /* make camera have positive z-coordinate */
2064         if(parent->loc[2]<0) {
2065                 invert_m4(rot);
2066                 mul_m4_m4m4(newmat, mat, rot);
2067                 object_apply_mat4(parent, newmat, 0, 0);
2068         }
2069
2070         where_is_object(scene, parent);
2071         set_axis(scene, parent, axis_track, 'X');
2072
2073         DAG_id_tag_update(&clip->id, 0);
2074         DAG_id_tag_update(&parent->id, OB_RECALC_OB);
2075
2076         WM_event_add_notifier(C, NC_MOVIECLIP|NA_EVALUATED, clip);
2077         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
2078
2079         return OPERATOR_FINISHED;
2080 }
2081
2082 void CLIP_OT_set_floor(wmOperatorType *ot)
2083 {
2084         /* identifiers */
2085         ot->name= "Set Floor";
2086         ot->description= "Set floor based on 3 selected bundles by moving camera (or it's parent if present) in 3D space";
2087         ot->idname= "CLIP_OT_set_floor";
2088
2089         /* api callbacks */
2090         ot->exec= set_floor_exec;
2091         ot->poll= space_clip_camera_poll;
2092
2093         /* flags */
2094         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2095 }
2096
2097 /********************** set axis operator *********************/
2098
2099 static int set_axis_exec(bContext *C, wmOperator *op)
2100 {
2101         SpaceClip *sc= CTX_wm_space_clip(C);
2102         MovieClip *clip= ED_space_clip(sc);
2103         MovieTrackingTrack *track;
2104         Scene *scene= CTX_data_scene(C);
2105         Object *parent= scene->camera;
2106         int axis= RNA_enum_get(op->ptr, "axis");
2107
2108         if(count_selected_bundles(C)!=1) {
2109                 BKE_report(op->reports, RPT_ERROR, "Single track with bundle should be selected to define axis");
2110
2111                 return OPERATOR_CANCELLED;
2112         }
2113
2114         if(scene->camera->parent)
2115                 parent= scene->camera->parent;
2116
2117         track= clip->tracking.tracks.first;
2118         while(track) {
2119                 if(TRACK_VIEW_SELECTED(sc, track))
2120                         break;
2121
2122                 track= track->next;
2123         }
2124
2125         set_axis(scene, parent, track, axis==0?'X':'Y');
2126
2127         DAG_id_tag_update(&clip->id, 0);
2128         DAG_id_tag_update(&parent->id, OB_RECALC_OB);
2129
2130         WM_event_add_notifier(C, NC_MOVIECLIP|NA_EVALUATED, clip);
2131         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
2132
2133         return OPERATOR_FINISHED;
2134 }
2135
2136 void CLIP_OT_set_axis(wmOperatorType *ot)
2137 {
2138         static EnumPropertyItem axis_actions[] = {
2139                         {0, "X", 0, "X", "Align bundle align X axis"},
2140                         {1, "Y", 0, "Y", "Align bundle align Y axis"},
2141                         {0, NULL, 0, NULL, NULL}
2142         };
2143
2144         /* identifiers */
2145         ot->name= "Set Axis";
2146         ot->description= "Set direction of scene axis rotating camera (or it's parent if present) and assuming selected track lies on real axis joining it with the origin";
2147         ot->idname= "CLIP_OT_set_axis";
2148
2149         /* api callbacks */
2150         ot->exec= set_axis_exec;
2151         ot->poll= space_clip_frame_camera_poll;
2152
2153         /* flags */
2154         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2155
2156         /* properties */
2157         RNA_def_enum(ot->srna, "axis", axis_actions, 0, "Axis", "Axis to use to align bundle along");
2158 }
2159
2160 /********************** set scale operator *********************/
2161
2162 static int set_scale_exec(bContext *C, wmOperator *op)
2163 {
2164         SpaceClip *sc= CTX_wm_space_clip(C);
2165         MovieClip *clip= ED_space_clip(sc);
2166         MovieTrackingTrack *track;
2167         Scene *scene= CTX_data_scene(C);
2168         Object *parent= scene->camera;
2169         int tot= 0;
2170         float vec[2][3], mat[4][4], scale;
2171         float dist= RNA_float_get(op->ptr, "distance");
2172
2173         if(count_selected_bundles(C)!=2) {
2174                 BKE_report(op->reports, RPT_ERROR, "Two tracks with bundles should be selected to scale scene");
2175
2176                 return OPERATOR_CANCELLED;
2177         }
2178
2179         if(scene->camera->parent)
2180                 parent= scene->camera->parent;
2181
2182         BKE_get_tracking_mat(scene, NULL, mat);
2183
2184         track= clip->tracking.tracks.first;
2185         while(track) {
2186                 if(TRACK_VIEW_SELECTED(sc, track)) {
2187                         mul_v3_m4v3(vec[tot], mat, track->bundle_pos);
2188                         tot++;
2189                 }
2190
2191                 track= track->next;
2192         }
2193
2194         sub_v3_v3(vec[0], vec[1]);
2195
2196         if(len_v3(vec[0])>1e-5f) {
2197                 scale= dist / len_v3(vec[0]);
2198
2199                 mul_v3_fl(parent->size, scale);
2200                 mul_v3_fl(parent->loc, scale);
2201
2202                 DAG_id_tag_update(&clip->id, 0);
2203                 DAG_id_tag_update(&parent->id, OB_RECALC_OB);
2204
2205                 WM_event_add_notifier(C, NC_MOVIECLIP|NA_EVALUATED, clip);
2206                 WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
2207         }
2208
2209         return OPERATOR_FINISHED;
2210 }
2211
2212 static int set_scale_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
2213 {
2214         SpaceClip *sc= CTX_wm_space_clip(C);
2215         MovieClip *clip= ED_space_clip(sc);
2216         float dist= RNA_float_get(op->ptr, "distance");
2217
2218         if(dist==0.0f)
2219                 RNA_float_set(op->ptr, "distance", clip->tracking.settings.dist);
2220
2221         return set_scale_exec(C, op);
2222 }
2223
2224 void CLIP_OT_set_scale(wmOperatorType *ot)
2225 {
2226         /* identifiers */
2227         ot->name= "Set Scale";
2228         ot->description= "Set scale of scene by scaling camera (or it's parent if present)";
2229         ot->idname= "CLIP_OT_set_scale";
2230
2231         /* api callbacks */
2232         ot->exec= set_scale_exec;
2233         ot->invoke= set_scale_invoke;
2234         ot->poll= space_clip_frame_camera_poll;
2235
2236         /* flags */
2237         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2238
2239         /* properties */
2240         RNA_def_float(ot->srna, "distance", 0.0f, -FLT_MAX, FLT_MAX,
2241                 "Distance", "Distance between selected tracks", -100.0f, 100.0f);
2242 }
2243
2244 /********************** set principal center operator *********************/
2245
2246 static int set_center_principal_exec(bContext *C, wmOperator *UNUSED(op))
2247 {
2248         SpaceClip *sc= CTX_wm_space_clip(C);
2249         MovieClip *clip= ED_space_clip(sc);
2250         int width, height;
2251
2252         BKE_movieclip_get_size(clip, &sc->user, &width, &height);
2253
2254         if(width==0 || height==0)
2255                 return OPERATOR_CANCELLED;
2256
2257         clip->tracking.camera.principal[0]= ((float)width)/2.0f;
2258         clip->tracking.camera.principal[1]= ((float)height)/2.0f;
2259
2260         WM_event_add_notifier(C, NC_MOVIECLIP|NA_EDITED, clip);
2261
2262         return OPERATOR_FINISHED;
2263 }
2264
2265 void CLIP_OT_set_center_principal(wmOperatorType *ot)
2266 {
2267         /* identifiers */
2268         ot->name= "Set Principal to Center";
2269         ot->description= "Set optical center to center of footage";
2270         ot->idname= "CLIP_OT_set_center_principal";
2271
2272         /* api callbacks */
2273         ot->exec= set_center_principal_exec;
2274         ot->poll= ED_space_clip_poll;
2275
2276         /* flags */
2277         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2278 }
2279
2280 /********************** hide tracks operator *********************/
2281
2282 static int hide_tracks_exec(bContext *C, wmOperator *op)
2283 {
2284         SpaceClip *sc= CTX_wm_space_clip(C);
2285         MovieClip *clip= ED_space_clip(sc);
2286         MovieTrackingTrack *track;
2287         int unselected;
2288
2289         unselected= RNA_boolean_get(op->ptr, "unselected");
2290
2291         track= clip->tracking.tracks.first;
2292         while(track) {
2293                 if(unselected==0 && TRACK_VIEW_SELECTED(sc, track)) {
2294                         track->flag|= TRACK_HIDDEN;
2295                 } else if(unselected==1 && !TRACK_VIEW_SELECTED(sc, track)) {
2296                         track->flag|= TRACK_HIDDEN;
2297                 }
2298
2299                 track= track->next;
2300         }
2301
2302         if(clip->tracking.act_track && clip->tracking.act_track->flag&TRACK_HIDDEN)
2303                 clip->tracking.act_track= NULL;
2304
2305         if(unselected==0) {
2306                 /* no selection on screen now, unlock view so it can be scrolled nice again */
2307                 sc->flag&= ~SC_LOCK_SELECTION;
2308         }
2309
2310         WM_event_add_notifier(C, NC_MOVIECLIP|ND_DISPLAY, NULL);
2311
2312         return OPERATOR_FINISHED;
2313 }
2314
2315 void CLIP_OT_hide_tracks(wmOperatorType *ot)
2316 {
2317         /* identifiers */
2318         ot->name= "Hide Tracks";
2319         ot->description= "Hide selected tracks";
2320         ot->idname= "CLIP_OT_hide_tracks";
2321
2322         /* api callbacks */
2323         ot->exec= hide_tracks_exec;
2324         ot->poll= ED_space_clip_poll;
2325
2326         /* flags */
2327         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2328
2329         /* properties */
2330         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected tracks");
2331 }
2332
2333 /********************** hide tracks clear operator *********************/
2334
2335 static int hide_tracks_clear_exec(bContext *C, wmOperator *UNUSED(op))
2336 {
2337         SpaceClip *sc= CTX_wm_space_clip(C);
2338         MovieClip *clip= ED_space_clip(sc);
2339         MovieTrackingTrack *track;
2340
2341         track= clip->tracking.tracks.first;
2342         while(track) {
2343                 track->flag&= ~TRACK_HIDDEN;
2344
2345                 track= track->next;
2346         }
2347
2348         WM_event_add_notifier(C, NC_MOVIECLIP|ND_DISPLAY, NULL);
2349
2350         return OPERATOR_FINISHED;
2351 }
2352
2353 void CLIP_OT_hide_tracks_clear(wmOperatorType *ot)
2354 {
2355         /* identifiers */
2356         ot->name= "Hide Tracks Clear";
2357         ot->description= "Clear hide selected tracks";
2358         ot->idname= "CLIP_OT_hide_tracks_clear";
2359
2360         /* api callbacks */
2361         ot->exec= hide_tracks_clear_exec;
2362         ot->poll= ED_space_clip_poll;
2363
2364         /* flags */
2365         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2366 }
2367
2368 /********************** detect features operator *********************/
2369
2370 static bGPDlayer *detect_get_layer(MovieClip *clip)
2371 {
2372         bGPDlayer *layer;
2373
2374         if(!clip->gpd)
2375                 return NULL;
2376
2377         layer= clip->gpd->layers.first;
2378         while(layer) {
2379                 if(layer->flag&GP_LAYER_ACTIVE)
2380                         return layer;
2381
2382                 layer= layer->next;
2383         }
2384
2385         return NULL;
2386 }
2387
2388 static int detect_features_exec(bContext *C, wmOperator *op)
2389 {
2390         SpaceClip *sc= CTX_wm_space_clip(C);
2391         MovieClip *clip= ED_space_clip(sc);
2392         ImBuf *ibuf= BKE_movieclip_get_ibuf_flag(clip, &sc->user, 0);
2393         MovieTrackingTrack *track= clip->tracking.tracks.first;
2394         int placement= RNA_enum_get(op->ptr, "placement");
2395         int margin= RNA_int_get(op->ptr, "margin");
2396         int min_trackability= RNA_int_get(op->ptr, "min_trackability");
2397         int min_distance= RNA_int_get(op->ptr, "min_distance");
2398         int place_outside_layer= 0;
2399         bGPDlayer *layer= NULL;
2400
2401         if(placement!=0) {
2402                 layer= detect_get_layer(clip);
2403                 place_outside_layer= placement==2;
2404         }
2405
2406         /* deselect existing tracks */
2407         while(track) {
2408                 track->flag&= ~SELECT;
2409                 track->pat_flag&= ~SELECT;
2410                 track->search_flag&= ~SELECT;
2411
2412                 track= track->next;
2413         }
2414
2415         BKE_tracking_detect_fast(&clip->tracking, ibuf, sc->user.framenr, margin, min_trackability, min_distance, layer, place_outside_layer);
2416
2417         IMB_freeImBuf(ibuf);
2418
2419         WM_event_add_notifier(C, NC_MOVIECLIP|NA_EDITED, NULL);
2420
2421         return OPERATOR_FINISHED;
2422 }
2423
2424 void CLIP_OT_detect_features(wmOperatorType *ot)
2425 {
2426         static EnumPropertyItem placement_items[] = {
2427                         {0, "FRAME",                    0, "Whole Frame",                       "Place markers across the whole frame"},
2428                         {1, "INSIDE_GPENCIL",   0, "Inside grease pencil",      "Place markers only inside areas outlined with grease pencil"},
2429                         {2, "OUTSIDE_GPENCIL",  0, "Outside grease pencil",     "Place markers only outside areas outlined with grease pencil"},
2430                         {0, NULL, 0, NULL, NULL}
2431         };
2432
2433         /* identifiers */
2434         ot->name= "Detect Features";
2435         ot->description= "Automatically detect features and place markers to track";
2436         ot->idname= "CLIP_OT_detect_features";
2437
2438         /* api callbacks */
2439         ot->exec= detect_features_exec;
2440         ot->poll= space_clip_frame_poll;
2441
2442         /* flags */
2443         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2444
2445         /* properties */
2446         RNA_def_enum(ot->srna, "placement", placement_items, 0, "Placement", "Placement for detected features");
2447         RNA_def_int(ot->srna, "margin", 16, 0, INT_MAX, "Margin", "Only corners further than margin pixels from the image edges are considered", 0, 300);
2448         RNA_def_int(ot->srna, "min_trackability", 16, 0, INT_MAX, "Trackability", "Minimum trackability score to add a corner", 0, 300);
2449         RNA_def_int(ot->srna, "min_distance", 120, 0, INT_MAX, "Distance", "Minimal distance accepted between two corners", 0, 300);
2450 }
2451
2452 /********************** frame jump operator *********************/
2453
2454 static int frame_jump_exec(bContext *C, wmOperator *op)
2455 {
2456         Scene *scene= CTX_data_scene(C);
2457         SpaceClip *sc= CTX_wm_space_clip(C);
2458         MovieClip *clip= ED_space_clip(sc);
2459         MovieTrackingTrack *track;
2460         int pos= RNA_enum_get(op->ptr, "position");
2461         int delta;
2462
2463         if(pos<=1) {    /* jump to path */
2464                 track= clip->tracking.act_track;
2465
2466                 if(!track)
2467                         return OPERATOR_CANCELLED;
2468
2469                 delta= pos == 1 ? 1 : -1;
2470
2471                 while(sc->user.framenr+delta >= SFRA && sc->user.framenr+delta <= EFRA) {
2472                         MovieTrackingMarker *marker= BKE_tracking_exact_marker(track, sc->user.framenr+delta);
2473
2474                         if(!marker || marker->flag&MARKER_DISABLED)
2475                                 break;
2476
2477                         sc->user.framenr+= delta;
2478                 }
2479         }
2480         else {  /* to to failed frame */
2481                 if(clip->tracking.reconstruction.flag&TRACKING_RECONSTRUCTED) {
2482                         int a= sc->user.framenr;
2483                         MovieTracking *tracking= &clip->tracking;
2484
2485                         delta= pos == 3 ? 1 : -1;
2486
2487                         a+= delta;
2488
2489                         while(a+delta >= SFRA && a+delta <= EFRA) {
2490                                 MovieReconstructedCamera *cam= BKE_tracking_get_reconstructed_camera(tracking, a);
2491
2492                                 if(!cam) {
2493                                         sc->user.framenr= a;
2494
2495                                         break;
2496                                 }
2497
2498                                 a+= delta;
2499                         }
2500                 }
2501         }
2502
2503         if(CFRA!=sc->user.framenr) {
2504                 CFRA= sc->user.framenr;
2505                 sound_seek_scene(CTX_data_main(C), CTX_data_scene(C));
2506
2507                 WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene);
2508         }
2509
2510         WM_event_add_notifier(C, NC_MOVIECLIP|ND_DISPLAY, NULL);
2511
2512         return OPERATOR_FINISHED;
2513 }
2514
2515 void CLIP_OT_frame_jump(wmOperatorType *ot)
2516 {
2517         static EnumPropertyItem position_items[] = {
2518                         {0, "PATHSTART",        0, "Path Start",                "Jump to start of current path"},
2519                         {1, "PATHEND",          0, "Path End",                  "Jump to end of current path"},
2520                         {2, "FAILEDPREV",       0, "Previous Failed",   "Jump to previous failed frame"},
2521                         {2, "FAILNEXT",         0, "Next Failed",               "Jump to next failed frame"},
2522                         {0, NULL, 0, NULL, NULL}
2523         };
2524
2525         /* identifiers */
2526         ot->name= "Jump to Frame";
2527         ot->description= "Jump to special frame";
2528         ot->idname= "CLIP_OT_frame_jump";
2529
2530         /* api callbacks */
2531         ot->exec= frame_jump_exec;
2532         ot->poll= space_clip_frame_poll;
2533
2534         /* flags */
2535         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2536
2537         /* properties */
2538         RNA_def_enum(ot->srna, "position", position_items, 0, "Position", "Position to jumo to");
2539 }
2540
2541 /********************** join tracks operator *********************/
2542
2543 static int join_tracks_exec(bContext *C, wmOperator *op)
2544 {
2545         SpaceClip *sc= CTX_wm_space_clip(C);
2546         MovieClip *clip= ED_space_clip(sc);
2547         MovieTrackingTrack *act_track, *track, *next;
2548
2549         act_track= clip->tracking.act_track;
2550
2551         if(!act_track) {
2552                 BKE_report(op->reports, RPT_ERROR, "No active track to join to");
2553                 return OPERATOR_CANCELLED;
2554         }
2555
2556         track= clip->tracking.tracks.first;
2557         while(track) {
2558                 if(TRACK_VIEW_SELECTED(sc, track) && track!=act_track) {
2559                         if(!BKE_tracking_test_join_tracks(act_track, track)) {
2560                                 BKE_report(op->reports, RPT_ERROR, "Some selected tracks have got keyframed markers to the same frame");
2561                                 return OPERATOR_CANCELLED;
2562                         }
2563                 }
2564
2565                 track= track->next;
2566         }
2567
2568         track= clip->tracking.tracks.first;
2569         while(track) {
2570                 next= track->next;
2571
2572                 if(TRACK_VIEW_SELECTED(sc, track) && track!=act_track) {
2573                         BKE_tracking_join_tracks(act_track, track);
2574
2575                         BKE_tracking_free_track(track);
2576                         BLI_freelinkN(&clip->tracking.tracks, track);
2577                 }
2578
2579                 track= next;
2580         }
2581
2582         WM_event_add_notifier(C, NC_MOVIECLIP|NA_EDITED, clip);
2583
2584         return OPERATOR_FINISHED;
2585 }
2586
2587 void CLIP_OT_join_tracks(wmOperatorType *ot)
2588 {
2589         /* identifiers */
2590         ot->name= "Join Tracks";
2591         ot->description= "Join selected tracks";
2592         ot->idname= "CLIP_OT_join_tracks";
2593
2594         /* api callbacks */
2595         ot->exec= join_tracks_exec;
2596         ot->poll= space_clip_frame_poll;
2597
2598         /* flags */
2599         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2600 }
2601
2602 /********************** lock tracks operator *********************/
2603
2604 static int lock_tracks_exec(bContext *C, wmOperator *op)
2605 {
2606         SpaceClip *sc= CTX_wm_space_clip(C);
2607         MovieClip *clip= ED_space_clip(sc);
2608         MovieTracking *tracking= &clip->tracking;
2609         MovieTrackingTrack *track= tracking->tracks.first;
2610         int action= RNA_enum_get(op->ptr, "action");
2611
2612         while(track) {
2613                 if(TRACK_VIEW_SELECTED(sc, track)) {
2614                         if(action==0) track->flag|= TRACK_LOCKED;
2615                         else if(action==1) track->flag&= ~TRACK_LOCKED;
2616                         else track->flag^= TRACK_LOCKED;
2617                 }
2618
2619                 track= track->next;
2620         }
2621
2622         WM_event_add_notifier(C, NC_MOVIECLIP|NA_EVALUATED, clip);
2623
2624         return OPERATOR_FINISHED;
2625 }
2626
2627 void CLIP_OT_lock_tracks(wmOperatorType *ot)
2628 {
2629         static EnumPropertyItem actions_items[] = {
2630                         {0, "LOCK", 0, "Lock", "Lock selected tracks"},
2631                         {1, "UNLOCK", 0, "Unlock", "Unlock selected tracks"},
2632                         {2, "TOGGLE", 0, "Toggle", "Toggle locked flag for selected tracks"},
2633                         {0, NULL, 0, NULL, NULL}
2634         };
2635
2636         /* identifiers */
2637         ot->name= "Lock Tracks";
2638         ot->description= "Lock/unlock selected tracks";
2639         ot->idname= "CLIP_OT_lock_tracks";
2640
2641         /* api callbacks */
2642         ot->exec= lock_tracks_exec;
2643         ot->poll= ED_space_clip_poll;
2644
2645         /* flags */
2646         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2647
2648         /* properties */
2649         RNA_def_enum(ot->srna, "action", actions_items, 0, "Action", "Lock action to execute");
2650 }
2651
2652 /********************** track copy color operator *********************/
2653
2654 static int track_copy_color_exec(bContext *C, wmOperator *UNUSED(op))
2655 {
2656         SpaceClip *sc= CTX_wm_space_clip(C);
2657         MovieClip *clip= ED_space_clip(sc);
2658         MovieTrackingTrack *track, *act_track= clip->tracking.act_track;
2659
2660         if(!act_track)
2661                 return OPERATOR_CANCELLED;
2662
2663         track= clip->tracking.tracks.first;
2664         while(track) {
2665                 if(TRACK_VIEW_SELECTED(sc, track) && track!=act_track) {
2666                         track->flag&= ~TRACK_CUSTOMCOLOR;
2667
2668                         if(act_track->flag&TRACK_CUSTOMCOLOR) {
2669                                 copy_v3_v3(track->color, act_track->color);
2670                                 track->flag|= TRACK_CUSTOMCOLOR;
2671                         }
2672                 }
2673
2674                 track= track->next;
2675         }
2676
2677         WM_event_add_notifier(C, NC_MOVIECLIP|ND_DISPLAY, clip);
2678
2679         return OPERATOR_FINISHED;
2680 }
2681
2682 void CLIP_OT_track_copy_color(wmOperatorType *ot)
2683 {
2684         /* identifiers */
2685         ot->name= "Copy Color";
2686         ot->description= "Copy color to all selected tracks";
2687         ot->idname= "CLIP_OT_track_copy_color";
2688
2689         /* api callbacks */
2690         ot->exec= track_copy_color_exec;
2691         ot->poll= ED_space_clip_poll;
2692
2693         /* flags */
2694         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2695 }
2696
2697 /********************** add 2d stabilization tracks operator *********************/
2698
2699 static int stabilize_2d_add_exec(bContext *C, wmOperator *UNUSED(op))
2700 {
2701         SpaceClip *sc= CTX_wm_space_clip(C);
2702         MovieClip *clip= ED_space_clip(sc);
2703         MovieTracking *tracking= &clip->tracking;
2704         MovieTrackingTrack *track;
2705         MovieTrackingStabilization *stab= &tracking->stabilization;
2706         int update= 0;
2707
2708         track= tracking->tracks.first;
2709         while(track) {
2710                 if(TRACK_VIEW_SELECTED(sc, track) && (track->flag&TRACK_USE_2D_STAB)==0) {
2711                         track->flag|= TRACK_USE_2D_STAB;
2712                         stab->tot_track++;
2713
2714                         update= 1;
2715                 }
2716
2717                 track= track->next;
2718         }
2719
2720         if(update) {
2721                 stab->ok= 0;
2722
2723                 DAG_id_tag_update(&clip->id, 0);
2724                 WM_event_add_notifier(C, NC_MOVIECLIP|ND_DISPLAY, clip);
2725         }
2726
2727         return OPERATOR_FINISHED;
2728 }
2729
2730 void CLIP_OT_stabilize_2d_add(wmOperatorType *ot)
2731 {
2732         /* identifiers */
2733         ot->name= "Add Stabilization Tracks";
2734         ot->description= "Add selected tracks to 2D stabilization tool";
2735         ot->idname= "CLIP_OT_stabilize_2d_add";
2736
2737         /* api callbacks */
2738         ot->exec= stabilize_2d_add_exec;
2739         ot->poll= ED_space_clip_poll;
2740
2741         /* flags */
2742         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2743 }
2744
2745 /********************** remove 2d stabilization tracks operator *********************/
2746
2747 static int stabilize_2d_remove_exec(bContext *C, wmOperator *UNUSED(op))
2748 {
2749         SpaceClip *sc= CTX_wm_space_clip(C);
2750         MovieClip *clip= ED_space_clip(sc);
2751         MovieTracking *tracking= &clip->tracking;
2752         MovieTrackingStabilization *stab= &tracking->stabilization;
2753         MovieTrackingTrack *track;
2754         int a= 0, update= 0;
2755
2756         track= tracking->tracks.first;
2757         while(track) {
2758                 if(track->flag&TRACK_USE_2D_STAB) {
2759                         if(a==stab->act_track) {
2760                                 track->flag&= ~TRACK_USE_2D_STAB;
2761
2762                                 stab->act_track--;
2763                                 stab->tot_track--;
2764
2765                                 if(stab->act_track<0)
2766                                         stab->act_track= 0;
2767
2768                                 update= 1;
2769
2770                                 break;
2771                         }
2772
2773                         a++;
2774                 }
2775
2776                 track= track->next;
2777         }
2778
2779         if(update) {
2780                 stab->ok= 0;
2781
2782                 DAG_id_tag_update(&clip->id, 0);
2783                 WM_event_add_notifier(C, NC_MOVIECLIP|ND_DISPLAY, clip);
2784         }
2785
2786         return OPERATOR_FINISHED;
2787 }
2788
2789 void CLIP_OT_stabilize_2d_remove(wmOperatorType *ot)
2790 {
2791         /* identifiers */
2792         ot->name= "Remove Stabilization Track";
2793         ot->description= "Remove selected track from stabilization";
2794         ot->idname= "CLIP_OT_stabilize_2d_remove";
2795
2796         /* api callbacks */
2797         ot->exec= stabilize_2d_remove_exec;
2798         ot->poll= ED_space_clip_poll;
2799
2800         /* flags */
2801         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2802 }
2803
2804 /********************** select 2d stabilization tracks operator *********************/
2805
2806 static int stabilize_2d_select_exec(bContext *C, wmOperator *UNUSED(op))
2807 {
2808         SpaceClip *sc= CTX_wm_space_clip(C);
2809         MovieClip *clip= ED_space_clip(sc);
2810         MovieTracking *tracking= &clip->tracking;
2811         MovieTrackingTrack *track;
2812         int update= 0;
2813
2814         track= tracking->tracks.first;
2815         while(track) {
2816                 if(track->flag&TRACK_USE_2D_STAB) {
2817                         BKE_tracking_track_flag(track, TRACK_AREA_ALL, SELECT, 0);
2818
2819                         update= 1;
2820                 }
2821
2822                 track= track->next;
2823         }
2824
2825         if(update)
2826                 WM_event_add_notifier(C, NC_MOVIECLIP|ND_SELECT, clip);
2827
2828         return OPERATOR_FINISHED;
2829 }
2830
2831 void CLIP_OT_stabilize_2d_select(wmOperatorType *ot)
2832 {
2833         /* identifiers */
2834         ot->name= "Select Stabilization Tracks";
2835         ot->description= "Select track which are used for stabilization";
2836         ot->idname= "CLIP_OT_stabilize_2d_select";
2837
2838         /* api callbacks */
2839         ot->exec= stabilize_2d_select_exec;
2840         ot->poll= ED_space_clip_poll;
2841
2842         /* flags */
2843         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2844 }
2845
2846 /********************** set 2d stabilization rotation track operator *********************/
2847
2848 static int stabilize_2d_set_rotation_exec(bContext *C, wmOperator *UNUSED(op))
2849 {
2850         SpaceClip *sc= CTX_wm_space_clip(C);
2851         MovieClip *clip= ED_space_clip(sc);
2852         MovieTracking *tracking= &clip->tracking;
2853
2854         if(tracking->act_track) {
2855                 MovieTrackingStabilization *stab= &tracking->stabilization;
2856
2857                 stab->rot_track= tracking->act_track;
2858                 stab->ok= 0;
2859
2860                 DAG_id_tag_update(&clip->id, 0);
2861                 WM_event_add_notifier(C, NC_MOVIECLIP|ND_DISPLAY, clip);
2862         }
2863
2864         return OPERATOR_FINISHED;
2865 }
2866
2867 void CLIP_OT_stabilize_2d_set_rotation(wmOperatorType *ot)
2868 {
2869         /* identifiers */
2870         ot->name= "Set Rotation Track";
2871         ot->description= "Use active track to compensate rotation when doing 2D stabilization";
2872         ot->idname= "CLIP_OT_stabilize_2d_set_rotation";
2873
2874         /* api callbacks */
2875         ot->exec= stabilize_2d_set_rotation_exec;
2876         ot->poll= ED_space_clip_poll;
2877
2878         /* flags */
2879         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2880 }
2881
2882 /********************** clean tracks operator *********************/
2883
2884 static int is_track_clean(MovieTrackingTrack *track, int frames, int del)
2885 {
2886         int ok= 1, a, prev= -1, count= 0;
2887         MovieTrackingMarker *markers= track->markers, *new_markers= NULL;
2888         int start_disabled= 0;
2889         int markersnr= track->markersnr;
2890
2891         if(del)
2892                 new_markers= MEM_callocN(markersnr*sizeof(MovieTrackingMarker), "track cleaned markers");
2893
2894         for(a= 0; a<markersnr; a++) {
2895                 int end= 0;
2896
2897                 if(prev==-1) {
2898                         if((markers[a].flag&MARKER_DISABLED)==0)
2899                                 prev= a;
2900                         else
2901                                 start_disabled= 1;
2902                 }
2903
2904                 if(prev >= 0) {
2905                         end=  a == markersnr-1;
2906                         end|= (a < markersnr-1) && (markers[a].framenr != markers[a+1].framenr-1 ||
2907                                                     markers[a].flag&MARKER_DISABLED);
2908                 }
2909
2910                 if(end) {
2911                         int segok= 1, len= 0;
2912
2913                         if(a != prev && markers[a].framenr != markers[a-1].framenr+1)
2914                                 len= a-prev;
2915                         else if(markers[a].flag&MARKER_DISABLED)
2916                                 len= a-prev;
2917                         else len= a-prev+1;
2918
2919                         if(frames) {
2920                                 if(len < frames) {
2921                                         segok= 0;
2922                                         ok= 0;
2923
2924                                         if(!del)
2925                                                 break;
2926                                 }
2927                         }
2928
2929                         if(del) {
2930                                 if(segok) {
2931                                         int t= len;
2932
2933                                         if(markers[a].flag&MARKER_DISABLED)
2934                                                 t++;
2935
2936                                         /* place disabled marker in front of current segment */
2937                                         if(start_disabled) {
2938                                                 memcpy(new_markers+count, markers+prev, sizeof(MovieTrackingMarker));
2939                                                 new_markers[count].framenr--;
2940                                                 new_markers[count].flag|= MARKER_DISABLED;
2941
2942                                                 count++;
2943                                                 start_disabled= 0;
2944                                         }
2945
2946                                         memcpy(new_markers+count, markers+prev, t*sizeof(MovieTrackingMarker));
2947                                         count+= t;
2948                                 }
2949                                 else if(markers[a].flag&MARKER_DISABLED) {
2950                                         /* current segment which would be deleted was finished by disabled marker,
2951                                            so next segment should be started from disabled marker */
2952                                         start_disabled= 1;
2953                                 }
2954                         }
2955
2956                         prev= -1;
2957                 }
2958         }
2959
2960         if(del) {
2961                 MEM_freeN(track->markers);
2962
2963                 if(count) {
2964                         track->markers= new_markers;
2965                 }
2966                 else {
2967                         track->markers= NULL;
2968                         MEM_freeN(new_markers);
2969                 }
2970
2971                 track->markersnr= count;
2972         }
2973
2974         return ok;
2975 }
2976
2977 static int clean_tracks_exec(bContext *C, wmOperator *op)
2978 {
2979         SpaceClip *sc= CTX_wm_space_clip(C);
2980         MovieClip *clip= ED_space_clip(sc);
2981         MovieTracking *tracking= &clip->tracking;
2982         MovieTrackingTrack *track, *next, *act_track= clip->tracking.act_track;
2983         int frames= RNA_int_get(op->ptr, "frames");
2984         int action= RNA_enum_get(op->ptr, "action");
2985         float error= RNA_float_get(op->ptr, "error");
2986
2987         if(error && action==TRACKING_CLEAN_DELETE_SEGMENT)
2988                 action= TRACKING_CLEAN_DELETE_TRACK;
2989
2990         track= tracking->tracks.first;
2991         while(track) {
2992                 next= track->next;
2993
2994                 if((track->flag&TRACK_HIDDEN)==0 && (track->flag&TRACK_LOCKED)==0) {
2995                         int ok= 1;
2996
2997                         ok= (is_track_clean(track, frames, action==TRACKING_CLEAN_DELETE_SEGMENT)) &&
2998                             (error == 0.0f || (track->flag&TRACK_HAS_BUNDLE)==0  || track->error < error);
2999
3000                         if(!ok) {
3001                                 if(action==TRACKING_CLEAN_SELECT) {
3002                                         BKE_tracking_track_flag(track, TRACK_AREA_ALL, SELECT, 0);
3003                                 }
3004                                 else if(action==TRACKING_CLEAN_DELETE_TRACK) {
3005                                         if(track==act_track)
3006                                                 clip->tracking.act_track= NULL;
3007
3008                                         BKE_tracking_free_track(track);
3009                                         BLI_freelinkN(&clip->tracking.tracks, track);
3010                                         track= NULL;
3011                                 }
3012
3013                                 /* happens when all tracking segments are not long enough */
3014                                 if(track && track->markersnr==0) {
3015                                         if(track==act_track)
3016                                                 clip->tracking.act_track= NULL;
3017
3018                                         BKE_tracking_free_track(track);
3019                                         BLI_freelinkN(&clip->tracking.tracks, track);
3020                                 }
3021                         }
3022                 }
3023
3024                 track= next;
3025         }
3026
3027         WM_event_add_notifier(C, NC_MOVIECLIP|ND_SELECT, clip);
3028
3029         return OPERATOR_FINISHED;
3030 }
3031
3032 static int clean_tracks_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
3033 {
3034         SpaceClip *sc= CTX_wm_space_clip(C);
3035         MovieClip *clip= ED_space_clip(sc);
3036         int frames= RNA_int_get(op->ptr, "frames");
3037         float error= RNA_float_get(op->ptr, "error");
3038         int action= RNA_enum_get(op->ptr, "action");
3039
3040         if(frames==0 && error==0 && action==0) {
3041                 RNA_int_set(op->ptr, "frames", clip->tracking.settings.clean_frames);
3042                 RNA_float_set(op->ptr, "error", clip->tracking.settings.clean_error);
3043                 RNA_enum_set(op->ptr, "action", clip->tracking.settings.clean_action);
3044         }
3045
3046         return clean_tracks_exec(C, op);
3047 }
3048
3049 void CLIP_OT_clean_tracks(wmOperatorType *ot)
3050 {
3051         static EnumPropertyItem actions_items[] = {
3052                         {TRACKING_CLEAN_SELECT, "SELECT", 0, "Select", "Select unclean tracks"},
3053                         {TRACKING_CLEAN_DELETE_TRACK, "DELETE_TRACK", 0, "Delete Track", "Delete unclean tracks"},
3054                         {TRACKING_CLEAN_DELETE_SEGMENT, "DELETE_SEGMENTS", 0, "Delete Segments", "Delete unclean segments of tracks"},
3055                         {0, NULL, 0, NULL, NULL}
3056         };
3057
3058         /* identifiers */
3059         ot->name= "Clean Tracks";
3060         ot->description= "Clean tracks with high error values or few frames";
3061         ot->idname= "CLIP_OT_clean_tracks";
3062
3063         /* api callbacks */
3064         ot->exec= clean_tracks_exec;
3065         ot->invoke= clean_tracks_invoke;
3066         ot->poll= ED_space_clip_poll;
3067
3068         /* flags */
3069         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
3070
3071         /* properties */
3072         RNA_def_int(ot->srna, "frames", 0, 0, INT_MAX, "Tracked Frames", "Effect on tracks which are tracked less than specified amount of frames", 0, INT_MAX);
3073         RNA_def_float(ot->srna, "error", 0.0f, 0.0f, FLT_MAX, "Reprojection Error", "Effect on tracks with have got larger reprojection error", 0.0f, 100.0f);
3074         RNA_def_enum(ot->srna, "action", actions_items, 0, "Action", "Cleanup action to execute");
3075 }