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