Merge branch 'blender2.7'
[blender.git] / source / blender / blenkernel / intern / tracking_plane_tracker.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  * Contributor(s): Blender Foundation,
22  *                 Sergey Sharybin
23  *                 Keir Mierle
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/tracking_plane_tracker.c
29  *  \ingroup bke
30  *
31  * This file contains implementation of plane tracker.
32  */
33
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_movieclip_types.h"
37
38 #include "BLI_utildefines.h"
39 #include "BLI_math.h"
40
41 #include "BKE_tracking.h"
42
43 #include "libmv-capi.h"
44
45 typedef double Vec2[2];
46
47 static int point_markers_correspondences_on_both_image(MovieTrackingPlaneTrack *plane_track, int frame1, int frame2,
48                                                        Vec2 **x1_r, Vec2 **x2_r)
49 {
50         int i, correspondence_index;
51         Vec2 *x1, *x2;
52
53         *x1_r = x1 = MEM_mallocN(sizeof(*x1) * plane_track->point_tracksnr, "point correspondences x1");
54         *x2_r = x2 = MEM_mallocN(sizeof(*x1) * plane_track->point_tracksnr, "point correspondences x2");
55
56         for (i = 0, correspondence_index = 0; i < plane_track->point_tracksnr; i++) {
57                 MovieTrackingTrack *point_track = plane_track->point_tracks[i];
58                 MovieTrackingMarker *point_marker1, *point_marker2;
59
60                 point_marker1 = BKE_tracking_marker_get_exact(point_track, frame1);
61                 point_marker2 = BKE_tracking_marker_get_exact(point_track, frame2);
62
63                 if (point_marker1 != NULL && point_marker2 != NULL) {
64                         /* Here conversion from float to double happens. */
65                         x1[correspondence_index][0] = point_marker1->pos[0];
66                         x1[correspondence_index][1] = point_marker1->pos[1];
67
68                         x2[correspondence_index][0] = point_marker2->pos[0];
69                         x2[correspondence_index][1] = point_marker2->pos[1];
70
71                         correspondence_index++;
72                 }
73         }
74
75         return correspondence_index;
76 }
77
78 /* NOTE: frame number should be in clip space, not scene space */
79 static void track_plane_from_existing_motion(MovieTrackingPlaneTrack *plane_track, int start_frame,
80                                              int direction, bool retrack)
81 {
82         MovieTrackingPlaneMarker *start_plane_marker = BKE_tracking_plane_marker_get(plane_track, start_frame);
83         MovieTrackingPlaneMarker *keyframe_plane_marker = NULL;
84         MovieTrackingPlaneMarker new_plane_marker;
85         int current_frame, frame_delta = direction > 0 ? 1 : -1;
86
87         if (plane_track->flag & PLANE_TRACK_AUTOKEY) {
88                 /* Find a keyframe in given direction. */
89                 for (current_frame = start_frame; ; current_frame += frame_delta) {
90                         MovieTrackingPlaneMarker *next_plane_marker =
91                                 BKE_tracking_plane_marker_get_exact(plane_track, current_frame + frame_delta);
92
93                         if (next_plane_marker == NULL) {
94                                 break;
95                         }
96
97                         if ((next_plane_marker->flag & PLANE_MARKER_TRACKED) == 0) {
98                                 keyframe_plane_marker = next_plane_marker;
99                                 break;
100                         }
101                 }
102         }
103         else {
104                 start_plane_marker->flag |= PLANE_MARKER_TRACKED;
105         }
106
107         new_plane_marker = *start_plane_marker;
108         new_plane_marker.flag |= PLANE_MARKER_TRACKED;
109
110         for (current_frame = start_frame; ; current_frame += frame_delta) {
111                 MovieTrackingPlaneMarker *next_plane_marker =
112                         BKE_tracking_plane_marker_get_exact(plane_track, current_frame + frame_delta);
113                 Vec2 *x1, *x2;
114                 int i, num_correspondences;
115                 double H_double[3][3];
116                 float H[3][3];
117
118                 /* As soon as we meet keyframed plane, we stop updating the sequence. */
119                 if (next_plane_marker && (next_plane_marker->flag & PLANE_MARKER_TRACKED) == 0) {
120                         /* Don't override keyframes if track is in auto-keyframe mode */
121                         if (plane_track->flag & PLANE_TRACK_AUTOKEY) {
122                                 break;
123                         }
124                 }
125
126                 num_correspondences =
127                         point_markers_correspondences_on_both_image(plane_track, current_frame, current_frame + frame_delta,
128                                                                     &x1, &x2);
129
130                 if (num_correspondences < 4) {
131                         MEM_freeN(x1);
132                         MEM_freeN(x2);
133
134                         break;
135                 }
136
137                 libmv_homography2DFromCorrespondencesEuc(x1, x2, num_correspondences, H_double);
138
139                 copy_m3_m3d(H, H_double);
140
141                 for (i = 0; i < 4; i++) {
142                         float vec[3] = {0.0f, 0.0f, 1.0f}, vec2[3];
143                         copy_v2_v2(vec, new_plane_marker.corners[i]);
144
145                         /* Apply homography */
146                         mul_v3_m3v3(vec2, H, vec);
147
148                         /* Normalize. */
149                         vec2[0] /= vec2[2];
150                         vec2[1] /= vec2[2];
151
152                         copy_v2_v2(new_plane_marker.corners[i], vec2);
153                 }
154
155                 new_plane_marker.framenr = current_frame + frame_delta;
156
157                 if (!retrack && keyframe_plane_marker &&
158                     next_plane_marker &&
159                     (plane_track->flag & PLANE_TRACK_AUTOKEY))
160                 {
161                         float fac = ((float) next_plane_marker->framenr - start_plane_marker->framenr) /
162                                     ((float) keyframe_plane_marker->framenr - start_plane_marker->framenr);
163
164                         fac = 3 * fac * fac - 2 * fac * fac * fac;
165
166                         for (i = 0; i < 4; i++) {
167                                 interp_v2_v2v2(new_plane_marker.corners[i], new_plane_marker.corners[i],
168                                                next_plane_marker->corners[i], fac);
169                         }
170                 }
171
172                 BKE_tracking_plane_marker_insert(plane_track, &new_plane_marker);
173
174                 MEM_freeN(x1);
175                 MEM_freeN(x2);
176         }
177 }
178
179 /* NOTE: frame number should be in clip space, not scene space */
180 void BKE_tracking_track_plane_from_existing_motion(MovieTrackingPlaneTrack *plane_track, int start_frame)
181 {
182         track_plane_from_existing_motion(plane_track, start_frame, 1, false);
183         track_plane_from_existing_motion(plane_track, start_frame, -1, false);
184 }
185
186 static MovieTrackingPlaneMarker *find_plane_keyframe(MovieTrackingPlaneTrack *plane_track,
187                                                      int start_frame, int direction)
188 {
189         MovieTrackingPlaneMarker *plane_marker = BKE_tracking_plane_marker_get(plane_track, start_frame);
190         int index = plane_marker - plane_track->markers;
191         int frame_delta = direction > 0 ? 1 : -1;
192
193         while (index >= 0 && index < plane_track->markersnr) {
194                 if ((plane_marker->flag & PLANE_MARKER_TRACKED) == 0) {
195                         return plane_marker;
196                 }
197                 plane_marker += frame_delta;
198         }
199
200         return NULL;
201 }
202
203 void BKE_tracking_retrack_plane_from_existing_motion_at_segment(MovieTrackingPlaneTrack *plane_track, int start_frame)
204 {
205         MovieTrackingPlaneMarker *prev_plane_keyframe, *next_plane_keyframe;
206
207         prev_plane_keyframe = find_plane_keyframe(plane_track, start_frame, -1);
208         next_plane_keyframe = find_plane_keyframe(plane_track, start_frame, 1);
209
210         if (prev_plane_keyframe != NULL && next_plane_keyframe != NULL) {
211                 /* First we track from left keyframe to the right one without any blending. */
212                 track_plane_from_existing_motion(plane_track, prev_plane_keyframe->framenr, 1, true);
213
214                 /* And then we track from the right keyframe to the left one, so shape blends in nicely */
215                 track_plane_from_existing_motion(plane_track, next_plane_keyframe->framenr, -1, false);
216         }
217         else if (prev_plane_keyframe != NULL) {
218                 track_plane_from_existing_motion(plane_track, prev_plane_keyframe->framenr, 1, true);
219         }
220         else if (next_plane_keyframe != NULL) {
221                 track_plane_from_existing_motion(plane_track, next_plane_keyframe->framenr, -1, true);
222         }
223 }
224
225 BLI_INLINE void float_corners_to_double(/*const*/ float corners[4][2], double double_corners[4][2])
226 {
227         copy_v2db_v2fl(double_corners[0], corners[0]);
228         copy_v2db_v2fl(double_corners[1], corners[1]);
229         copy_v2db_v2fl(double_corners[2], corners[2]);
230         copy_v2db_v2fl(double_corners[3], corners[3]);
231 }
232
233 void BKE_tracking_homography_between_two_quads(/*const*/ float reference_corners[4][2],
234                                                /*const*/ float corners[4][2],
235                                                float H[3][3])
236 {
237         Vec2 x1[4], x2[4];
238         double H_double[3][3];
239
240         float_corners_to_double(reference_corners, x1);
241         float_corners_to_double(corners, x2);
242
243         libmv_homography2DFromCorrespondencesEuc(x1, x2, 4, H_double);
244
245         copy_m3_m3d(H, H_double);
246 }