Blender  V3.3
tracking_plane_tracker.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2011 Blender Foundation. All rights reserved. */
3 
10 #include "MEM_guardedalloc.h"
11 
12 #include "DNA_movieclip_types.h"
13 
14 #include "BLI_math.h"
15 #include "BLI_utildefines.h"
16 
17 #include "BKE_tracking.h"
18 
19 #include "libmv-capi.h"
20 
21 typedef double Vec2[2];
22 
24  MovieTrackingPlaneTrack *plane_track, int frame1, int frame2, Vec2 **r_x1, Vec2 **r_x2)
25 {
26  Vec2 *x1, *x2;
27 
28  *r_x1 = x1 = MEM_mallocN(sizeof(*x1) * plane_track->point_tracksnr, "point correspondences x1");
29  *r_x2 = x2 = MEM_mallocN(sizeof(*x1) * plane_track->point_tracksnr, "point correspondences x2");
30 
31  int correspondence_index = 0;
32  for (int i = 0; i < plane_track->point_tracksnr; i++) {
33  MovieTrackingTrack *point_track = plane_track->point_tracks[i];
34  MovieTrackingMarker *point_marker1, *point_marker2;
35 
36  point_marker1 = BKE_tracking_marker_get_exact(point_track, frame1);
37  point_marker2 = BKE_tracking_marker_get_exact(point_track, frame2);
38 
39  if (point_marker1 != NULL && point_marker2 != NULL) {
40  /* Here conversion from float to double happens. */
41  x1[correspondence_index][0] = point_marker1->pos[0];
42  x1[correspondence_index][1] = point_marker1->pos[1];
43 
44  x2[correspondence_index][0] = point_marker2->pos[0];
45  x2[correspondence_index][1] = point_marker2->pos[1];
46 
47  correspondence_index++;
48  }
49  }
50 
51  return correspondence_index;
52 }
53 
54 /* NOTE: frame number should be in clip space, not scene space */
56  int start_frame,
57  int direction,
58  bool retrack)
59 {
60  MovieTrackingPlaneMarker *start_plane_marker = BKE_tracking_plane_marker_get(plane_track,
61  start_frame);
62  MovieTrackingPlaneMarker *keyframe_plane_marker = NULL;
63  MovieTrackingPlaneMarker new_plane_marker;
64  int frame_delta = direction > 0 ? 1 : -1;
65 
66  if (plane_track->flag & PLANE_TRACK_AUTOKEY) {
67  /* Find a keyframe in given direction. */
68  for (int current_frame = start_frame;; current_frame += frame_delta) {
70  plane_track, current_frame + frame_delta);
71 
72  if (next_plane_marker == NULL) {
73  break;
74  }
75 
76  if ((next_plane_marker->flag & PLANE_MARKER_TRACKED) == 0) {
77  keyframe_plane_marker = next_plane_marker;
78  break;
79  }
80  }
81  }
82  else {
83  start_plane_marker->flag |= PLANE_MARKER_TRACKED;
84  }
85 
86  new_plane_marker = *start_plane_marker;
87  new_plane_marker.flag |= PLANE_MARKER_TRACKED;
88 
89  for (int current_frame = start_frame;; current_frame += frame_delta) {
91  plane_track, current_frame + frame_delta);
92  Vec2 *x1, *x2;
93  double H_double[3][3];
94  float H[3][3];
95 
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 
104  const int num_correspondences = point_markers_correspondences_on_both_image(
105  plane_track, current_frame, current_frame + frame_delta, &x1, &x2);
106  if (num_correspondences < 4) {
107  MEM_freeN(x1);
108  MEM_freeN(x2);
109  break;
110  }
111 
112  libmv_homography2DFromCorrespondencesEuc(x1, x2, num_correspondences, H_double);
113 
114  copy_m3_m3d(H, H_double);
115 
116  for (int i = 0; i < 4; i++) {
117  float vec[3] = {0.0f, 0.0f, 1.0f}, vec2[3];
118  copy_v2_v2(vec, new_plane_marker.corners[i]);
119 
120  /* Apply homography */
121  mul_v3_m3v3(vec2, H, vec);
122 
123  /* Normalize. */
124  vec2[0] /= vec2[2];
125  vec2[1] /= vec2[2];
126 
127  copy_v2_v2(new_plane_marker.corners[i], vec2);
128  }
129 
130  new_plane_marker.framenr = current_frame + frame_delta;
131 
132  if (!retrack && keyframe_plane_marker && next_plane_marker &&
133  (plane_track->flag & PLANE_TRACK_AUTOKEY)) {
134  float fac = ((float)next_plane_marker->framenr - start_plane_marker->framenr) /
135  ((float)keyframe_plane_marker->framenr - start_plane_marker->framenr);
136 
137  fac = 3 * fac * fac - 2 * fac * fac * fac;
138 
139  for (int i = 0; i < 4; i++) {
140  interp_v2_v2v2(new_plane_marker.corners[i],
141  new_plane_marker.corners[i],
142  next_plane_marker->corners[i],
143  fac);
144  }
145  }
146 
147  BKE_tracking_plane_marker_insert(plane_track, &new_plane_marker);
148 
149  MEM_freeN(x1);
150  MEM_freeN(x2);
151  }
152 }
153 
155  int start_frame)
156 {
157  track_plane_from_existing_motion(plane_track, start_frame, 1, false);
158  track_plane_from_existing_motion(plane_track, start_frame, -1, false);
159 }
160 
162  int start_frame,
163  int direction)
164 {
165  MovieTrackingPlaneMarker *plane_marker = BKE_tracking_plane_marker_get(plane_track, start_frame);
166  int index = plane_marker - plane_track->markers;
167  int frame_delta = direction > 0 ? 1 : -1;
168 
169  while (index >= 0 && index < plane_track->markersnr) {
170  if ((plane_marker->flag & PLANE_MARKER_TRACKED) == 0) {
171  return plane_marker;
172  }
173  plane_marker += frame_delta;
174  }
175 
176  return NULL;
177 }
178 
180  MovieTrackingPlaneTrack *plane_track, int start_frame)
181 {
182  MovieTrackingPlaneMarker *prev_plane_keyframe, *next_plane_keyframe;
183 
184  prev_plane_keyframe = find_plane_keyframe(plane_track, start_frame, -1);
185  next_plane_keyframe = find_plane_keyframe(plane_track, start_frame, 1);
186 
187  if (prev_plane_keyframe != NULL && next_plane_keyframe != NULL) {
188  /* First we track from left keyframe to the right one without any blending. */
189  track_plane_from_existing_motion(plane_track, prev_plane_keyframe->framenr, 1, true);
190 
191  /* And then we track from the right keyframe to the left one, so shape blends in nicely */
192  track_plane_from_existing_motion(plane_track, next_plane_keyframe->framenr, -1, false);
193  }
194  else if (prev_plane_keyframe != NULL) {
195  track_plane_from_existing_motion(plane_track, prev_plane_keyframe->framenr, 1, true);
196  }
197  else if (next_plane_keyframe != NULL) {
198  track_plane_from_existing_motion(plane_track, next_plane_keyframe->framenr, -1, true);
199  }
200 }
201 
202 BLI_INLINE void float_corners_to_double(/*const*/ float corners[4][2], double double_corners[4][2])
203 {
204  copy_v2db_v2fl(double_corners[0], corners[0]);
205  copy_v2db_v2fl(double_corners[1], corners[1]);
206  copy_v2db_v2fl(double_corners[2], corners[2]);
207  copy_v2db_v2fl(double_corners[3], corners[3]);
208 }
209 
210 void BKE_tracking_homography_between_two_quads(/*const*/ float reference_corners[4][2],
211  /*const*/ float corners[4][2],
212  float H[3][3])
213 {
214  Vec2 x1[4], x2[4];
215  double H_double[3][3];
216 
217  float_corners_to_double(reference_corners, x1);
218  float_corners_to_double(corners, x2);
219 
221 
222  copy_m3_m3d(H, H_double);
223 }
typedef float(TangentPoint)[2]
struct MovieTrackingPlaneMarker * BKE_tracking_plane_marker_insert(struct MovieTrackingPlaneTrack *plane_track, struct MovieTrackingPlaneMarker *plane_marker)
Definition: tracking.c:1818
struct MovieTrackingPlaneMarker * BKE_tracking_plane_marker_get(struct MovieTrackingPlaneTrack *plane_track, int framenr)
Definition: tracking.c:1890
struct MovieTrackingMarker * BKE_tracking_marker_get_exact(struct MovieTrackingTrack *track, int framenr)
Definition: tracking.c:1457
struct MovieTrackingPlaneMarker * BKE_tracking_plane_marker_get_exact(struct MovieTrackingPlaneTrack *plane_track, int framenr)
Definition: tracking.c:1938
#define BLI_INLINE
void copy_m3_m3d(float m1[3][3], const double m2[3][3])
Definition: math_matrix.c:203
void mul_v3_m3v3(float r[3], const float M[3][3], const float a[3])
Definition: math_matrix.c:897
MINLINE void copy_v2db_v2fl(double r[2], const float a[2])
void interp_v2_v2v2(float r[2], const float a[2], const float b[2], float t)
Definition: math_vector.c:14
MINLINE void copy_v2_v2(float r[2], const float a[2])
@ PLANE_MARKER_TRACKED
@ PLANE_TRACK_AUTOKEY
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble x2
Read Guarded memory(de)allocation.
void libmv_homography2DFromCorrespondencesEuc(double(*x1)[2], double(*x2)[2], int num_points, double H[3][3])
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
#define H(x, y, z)
MovieTrackingTrack ** point_tracks
MovieTrackingPlaneMarker * markers
static MovieTrackingPlaneMarker * find_plane_keyframe(MovieTrackingPlaneTrack *plane_track, int start_frame, int direction)
double Vec2[2]
static void track_plane_from_existing_motion(MovieTrackingPlaneTrack *plane_track, int start_frame, int direction, bool retrack)
static int point_markers_correspondences_on_both_image(MovieTrackingPlaneTrack *plane_track, int frame1, int frame2, Vec2 **r_x1, Vec2 **r_x2)
BLI_INLINE void float_corners_to_double(float corners[4][2], double double_corners[4][2])
void BKE_tracking_retrack_plane_from_existing_motion_at_segment(MovieTrackingPlaneTrack *plane_track, int start_frame)
void BKE_tracking_track_plane_from_existing_motion(MovieTrackingPlaneTrack *plane_track, int start_frame)
void BKE_tracking_homography_between_two_quads(float reference_corners[4][2], float corners[4][2], float H[3][3])