tracking_plane_tracker.c 8.1 KB

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