ClangFormat: apply to source, most of intern
[blender.git] / source / blender / blenkernel / intern / tracking_plane_tracker.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2011 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup bke
22  *
23  * This file contains implementation of plane tracker.
24  */
25
26 #include "MEM_guardedalloc.h"
27
28 #include "DNA_movieclip_types.h"
29
30 #include "BLI_utildefines.h"
31 #include "BLI_math.h"
32
33 #include "BKE_tracking.h"
34
35 #include "libmv-capi.h"
36
37 typedef double Vec2[2];
38
39 static int point_markers_correspondences_on_both_image(
40     MovieTrackingPlaneTrack *plane_track, int frame1, int frame2, Vec2 **x1_r, Vec2 **x2_r)
41 {
42   int i, correspondence_index;
43   Vec2 *x1, *x2;
44
45   *x1_r = x1 = MEM_mallocN(sizeof(*x1) * plane_track->point_tracksnr, "point correspondences x1");
46   *x2_r = x2 = MEM_mallocN(sizeof(*x1) * plane_track->point_tracksnr, "point correspondences x2");
47
48   for (i = 0, correspondence_index = 0; i < plane_track->point_tracksnr; i++) {
49     MovieTrackingTrack *point_track = plane_track->point_tracks[i];
50     MovieTrackingMarker *point_marker1, *point_marker2;
51
52     point_marker1 = BKE_tracking_marker_get_exact(point_track, frame1);
53     point_marker2 = BKE_tracking_marker_get_exact(point_track, frame2);
54
55     if (point_marker1 != NULL && point_marker2 != NULL) {
56       /* Here conversion from float to double happens. */
57       x1[correspondence_index][0] = point_marker1->pos[0];
58       x1[correspondence_index][1] = point_marker1->pos[1];
59
60       x2[correspondence_index][0] = point_marker2->pos[0];
61       x2[correspondence_index][1] = point_marker2->pos[1];
62
63       correspondence_index++;
64     }
65   }
66
67   return correspondence_index;
68 }
69
70 /* NOTE: frame number should be in clip space, not scene space */
71 static void track_plane_from_existing_motion(MovieTrackingPlaneTrack *plane_track,
72                                              int start_frame,
73                                              int direction,
74                                              bool retrack)
75 {
76   MovieTrackingPlaneMarker *start_plane_marker = BKE_tracking_plane_marker_get(plane_track,
77                                                                                start_frame);
78   MovieTrackingPlaneMarker *keyframe_plane_marker = NULL;
79   MovieTrackingPlaneMarker new_plane_marker;
80   int current_frame, frame_delta = direction > 0 ? 1 : -1;
81
82   if (plane_track->flag & PLANE_TRACK_AUTOKEY) {
83     /* Find a keyframe in given direction. */
84     for (current_frame = start_frame;; current_frame += frame_delta) {
85       MovieTrackingPlaneMarker *next_plane_marker = BKE_tracking_plane_marker_get_exact(
86           plane_track, current_frame + frame_delta);
87
88       if (next_plane_marker == NULL) {
89         break;
90       }
91
92       if ((next_plane_marker->flag & PLANE_MARKER_TRACKED) == 0) {
93         keyframe_plane_marker = next_plane_marker;
94         break;
95       }
96     }
97   }
98   else {
99     start_plane_marker->flag |= PLANE_MARKER_TRACKED;
100   }
101
102   new_plane_marker = *start_plane_marker;
103   new_plane_marker.flag |= PLANE_MARKER_TRACKED;
104
105   for (current_frame = start_frame;; current_frame += frame_delta) {
106     MovieTrackingPlaneMarker *next_plane_marker = BKE_tracking_plane_marker_get_exact(
107         plane_track, current_frame + frame_delta);
108     Vec2 *x1, *x2;
109     int i, num_correspondences;
110     double H_double[3][3];
111     float H[3][3];
112
113     /* As soon as we meet keyframed plane, we stop updating the sequence. */
114     if (next_plane_marker && (next_plane_marker->flag & PLANE_MARKER_TRACKED) == 0) {
115       /* Don't override keyframes if track is in auto-keyframe mode */
116       if (plane_track->flag & PLANE_TRACK_AUTOKEY) {
117         break;
118       }
119     }
120
121     num_correspondences = point_markers_correspondences_on_both_image(
122         plane_track, current_frame, current_frame + frame_delta, &x1, &x2);
123
124     if (num_correspondences < 4) {
125       MEM_freeN(x1);
126       MEM_freeN(x2);
127
128       break;
129     }
130
131     libmv_homography2DFromCorrespondencesEuc(x1, x2, num_correspondences, H_double);
132
133     copy_m3_m3d(H, H_double);
134
135     for (i = 0; i < 4; i++) {
136       float vec[3] = {0.0f, 0.0f, 1.0f}, vec2[3];
137       copy_v2_v2(vec, new_plane_marker.corners[i]);
138
139       /* Apply homography */
140       mul_v3_m3v3(vec2, H, vec);
141
142       /* Normalize. */
143       vec2[0] /= vec2[2];
144       vec2[1] /= vec2[2];
145
146       copy_v2_v2(new_plane_marker.corners[i], vec2);
147     }
148
149     new_plane_marker.framenr = current_frame + frame_delta;
150
151     if (!retrack && keyframe_plane_marker && next_plane_marker &&
152         (plane_track->flag & PLANE_TRACK_AUTOKEY)) {
153       float fac = ((float)next_plane_marker->framenr - start_plane_marker->framenr) /
154                   ((float)keyframe_plane_marker->framenr - start_plane_marker->framenr);
155
156       fac = 3 * fac * fac - 2 * fac * fac * fac;
157
158       for (i = 0; i < 4; i++) {
159         interp_v2_v2v2(new_plane_marker.corners[i],
160                        new_plane_marker.corners[i],
161                        next_plane_marker->corners[i],
162                        fac);
163       }
164     }
165
166     BKE_tracking_plane_marker_insert(plane_track, &new_plane_marker);
167
168     MEM_freeN(x1);
169     MEM_freeN(x2);
170   }
171 }
172
173 /* NOTE: frame number should be in clip space, not scene space */
174 void BKE_tracking_track_plane_from_existing_motion(MovieTrackingPlaneTrack *plane_track,
175                                                    int start_frame)
176 {
177   track_plane_from_existing_motion(plane_track, start_frame, 1, false);
178   track_plane_from_existing_motion(plane_track, start_frame, -1, false);
179 }
180
181 static MovieTrackingPlaneMarker *find_plane_keyframe(MovieTrackingPlaneTrack *plane_track,
182                                                      int start_frame,
183                                                      int direction)
184 {
185   MovieTrackingPlaneMarker *plane_marker = BKE_tracking_plane_marker_get(plane_track, start_frame);
186   int index = plane_marker - plane_track->markers;
187   int frame_delta = direction > 0 ? 1 : -1;
188
189   while (index >= 0 && index < plane_track->markersnr) {
190     if ((plane_marker->flag & PLANE_MARKER_TRACKED) == 0) {
191       return plane_marker;
192     }
193     plane_marker += frame_delta;
194   }
195
196   return NULL;
197 }
198
199 void BKE_tracking_retrack_plane_from_existing_motion_at_segment(
200     MovieTrackingPlaneTrack *plane_track, int start_frame)
201 {
202   MovieTrackingPlaneMarker *prev_plane_keyframe, *next_plane_keyframe;
203
204   prev_plane_keyframe = find_plane_keyframe(plane_track, start_frame, -1);
205   next_plane_keyframe = find_plane_keyframe(plane_track, start_frame, 1);
206
207   if (prev_plane_keyframe != NULL && next_plane_keyframe != NULL) {
208     /* First we track from left keyframe to the right one without any blending. */
209     track_plane_from_existing_motion(plane_track, prev_plane_keyframe->framenr, 1, true);
210
211     /* And then we track from the right keyframe to the left one, so shape blends in nicely */
212     track_plane_from_existing_motion(plane_track, next_plane_keyframe->framenr, -1, false);
213   }
214   else if (prev_plane_keyframe != NULL) {
215     track_plane_from_existing_motion(plane_track, prev_plane_keyframe->framenr, 1, true);
216   }
217   else if (next_plane_keyframe != NULL) {
218     track_plane_from_existing_motion(plane_track, next_plane_keyframe->framenr, -1, true);
219   }
220 }
221
222 BLI_INLINE void float_corners_to_double(/*const*/ float corners[4][2], double double_corners[4][2])
223 {
224   copy_v2db_v2fl(double_corners[0], corners[0]);
225   copy_v2db_v2fl(double_corners[1], corners[1]);
226   copy_v2db_v2fl(double_corners[2], corners[2]);
227   copy_v2db_v2fl(double_corners[3], corners[3]);
228 }
229
230 void BKE_tracking_homography_between_two_quads(/*const*/ float reference_corners[4][2],
231                                                /*const*/ float corners[4][2],
232                                                float H[3][3])
233 {
234   Vec2 x1[4], x2[4];
235   double H_double[3][3];
236
237   float_corners_to_double(reference_corners, x1);
238   float_corners_to_double(corners, x2);
239
240   libmv_homography2DFromCorrespondencesEuc(x1, x2, 4, H_double);
241
242   copy_m3_m3d(H, H_double);
243 }