Camera tracking: code cleanup
authorSergey Sharybin <sergey.vfx@gmail.com>
Mon, 14 Nov 2011 06:41:32 +0000 (06:41 +0000)
committerSergey Sharybin <sergey.vfx@gmail.com>
Mon, 14 Nov 2011 06:41:32 +0000 (06:41 +0000)
extern/libmv/libmv-capi.cpp
extern/libmv/libmv-capi.h
source/blender/blenkernel/intern/tracking.c

index 4d17aa6f5386044cfb68bd676dff58d0aba6fcb6..9d6cfd5d17a7c9931d167c5a1dd8f3b95f5e4679 100644 (file)
 #  define snprintf _snprintf
 #endif
 
-#define DEFAULT_WINDOW_HALFSIZE        5
-
-typedef struct libmv_RegionTracker {
-       libmv::EsmRegionTracker *klt_region_tracker;
-       libmv::RegionTracker *region_tracker;
-} libmv_RegionTracker;
-
 typedef struct libmv_Reconstruction {
        libmv::EuclideanReconstruction reconstruction;
 
@@ -113,22 +106,18 @@ void libmv_setLoggingVerbosity(int verbosity)
 
 /* ************ RegionTracker ************ */
 
-libmv_RegionTracker *libmv_regionTrackerNew(int max_iterations, int pyramid_level)
+libmv_RegionTracker *libmv_regionTrackerNew(int max_iterations, int pyramid_level, int half_window_size)
 {
        libmv::EsmRegionTracker *klt_region_tracker = new libmv::EsmRegionTracker;
 
-       klt_region_tracker->half_window_size = DEFAULT_WINDOW_HALFSIZE;
+       klt_region_tracker->half_window_size = half_window_size;
        klt_region_tracker->max_iterations = max_iterations;
        klt_region_tracker->min_determinant = 1e-4;
 
        libmv::PyramidRegionTracker *region_tracker =
                new libmv::PyramidRegionTracker(klt_region_tracker, pyramid_level);
 
-       libmv_RegionTracker *configured_region_tracker = new libmv_RegionTracker;
-       configured_region_tracker->klt_region_tracker = klt_region_tracker;
-       configured_region_tracker->region_tracker = region_tracker;
-
-       return configured_region_tracker;
+       return (libmv_RegionTracker *)region_tracker;
 }
 
 static void floatBufToImage(const float *buf, int width, int height, libmv::FloatImage *image)
@@ -270,18 +259,11 @@ static void saveBytesImage(char *prefix, unsigned char *data, int width, int hei
 #endif
 
 int libmv_regionTrackerTrack(libmv_RegionTracker *libmv_tracker, const float *ima1, const float *ima2,
-                        int width, int height, int half_window_size,
-                        double x1, double y1, double *x2, double *y2)
+                        int width, int height, double x1, double y1, double *x2, double *y2)
 {
-       libmv::RegionTracker *region_tracker;
-       libmv::EsmRegionTracker *klt_region_tracker;
+       libmv::RegionTracker *region_tracker = (libmv::RegionTracker *)libmv_tracker;
        libmv::FloatImage old_patch, new_patch;
 
-       klt_region_tracker = libmv_tracker->klt_region_tracker;
-       region_tracker = libmv_tracker->region_tracker;
-
-       klt_region_tracker->half_window_size = half_window_size;
-
        floatBufToImage(ima1, width, height, &old_patch);
        floatBufToImage(ima2, width, height, &new_patch);
 
@@ -304,8 +286,9 @@ int libmv_regionTrackerTrack(libmv_RegionTracker *libmv_tracker, const float *im
 
 void libmv_regionTrackerDestroy(libmv_RegionTracker *libmv_tracker)
 {
-       delete libmv_tracker->region_tracker;
-       delete libmv_tracker;
+       libmv::RegionTracker *region_tracker= (libmv::RegionTracker *)libmv_tracker;
+
+       delete region_tracker;
 }
 
 /* ************ Tracks ************ */
index d378afc5ea8ebb96da1df7ae386dcd614390b9c1..8252a11739b2306e012cc1b5cda31460971cd2da 100644 (file)
@@ -43,10 +43,9 @@ void libmv_startDebugLogging(void);
 void libmv_setLoggingVerbosity(int verbosity);
 
 /* RegionTracker */
-struct libmv_RegionTracker *libmv_regionTrackerNew(int max_iterations, int pyramid_level);
+struct libmv_RegionTracker *libmv_regionTrackerNew(int max_iterations, int pyramid_level, int half_window_size);
 int libmv_regionTrackerTrack(struct libmv_RegionTracker *libmv_tracker, const float *ima1, const float *ima2,
-                       int width, int height, int half_window_size,
-                       double  x1, double  y1, double *x2, double *y2);
+                       int width, int height, double  x1, double  y1, double *x2, double *y2);
 void libmv_regionTrackerDestroy(struct libmv_RegionTracker *libmv_tracker);
 
 /* SAD Tracker */
index 4989e8f1b550711d322ad35d21de706cb7caf9a4..d236c053058765b76aa299761a182f710c26de78 100644 (file)
@@ -120,10 +120,6 @@ void BKE_tracking_clamp_track(MovieTrackingTrack *track, int event)
                }
        }
        else if(event==CLAMP_SEARCH_DIM) {
-               float max_pyramid_level_factor = 1.0;
-               if (track->tracker == TRACKER_KLT) {
-                       max_pyramid_level_factor = 1 << (track->pyramid_levels - 1);
-               }
                for(a= 0; a<2; a++) {
                        /* search shouldn't be resized smaller than pattern */
                        track->search_min[a]= MIN2(pat_min[a], track->search_min[a]);
@@ -146,7 +142,6 @@ void BKE_tracking_clamp_track(MovieTrackingTrack *track, int event)
                        }
                }
        }
-
        else if(event==CLAMP_PYRAMID_LEVELS || (event==CLAMP_SEARCH_DIM && track->tracker == TRACKER_KLT))  {
                float dim[2];
                sub_v2_v2v2(dim, track->pat_max, track->pat_min);
@@ -619,18 +614,23 @@ MovieTrackingContext *BKE_tracking_context_new(MovieClip *clip, MovieClipUser *u
                                                        float search_size_y= (track->search_max[1]-track->search_min[1])*height;
                                                        float pattern_size_x= (track->pat_max[0]-track->pat_min[0])*width;
                                                        float pattern_size_y= (track->pat_max[1]-track->pat_min[1])*height;
+                                                       int wndx, wndy;
 
                                                        /* compute the maximum pyramid size */
-                                                       double search_to_pattern_ratio= MIN2(search_size_x,  search_size_y)
+                                                       float search_to_pattern_ratio= MIN2(search_size_x,  search_size_y)
                                                                / MAX2(pattern_size_x, pattern_size_y);
-                                                       double log2_search_to_pattern_ratio = log(floor(search_to_pattern_ratio)) / M_LN2;
+                                                       float log2_search_to_pattern_ratio = log(floor(search_to_pattern_ratio)) / M_LN2;
                                                        int max_pyramid_levels= floor(log2_search_to_pattern_ratio + 1);
 
                                                        /* try to accomodate the user's choice of pyramid level in a way
                                                         * that doesn't cause the coarsest pyramid pattern to be larger
                                                         * than the search size */
                                                        int level= MIN2(track_context->track->pyramid_levels, max_pyramid_levels);
-                                                       track_context->region_tracker= libmv_regionTrackerNew(100, level);
+
+                                                       wndx= (int)((track->pat_max[0]-track->pat_min[0])*width)/2;
+                                                       wndy= (int)((track->pat_max[1]-track->pat_min[1])*height)/2;
+
+                                                       track_context->region_tracker= libmv_regionTrackerNew(100, level, MAX2(wndx, wndy));
                                                }
                                                else if(track_context->track->tracker==TRACKER_SAD) {
                                                        /* nothing to initialize */
@@ -880,7 +880,7 @@ static ImBuf *get_keyframed_ibuf(MovieTrackingContext *context, MovieTrackingTra
        while(a>=0 && a<track->markersnr) {
                int next= (context->backwards) ? a+1 : a-1;
                int is_keyframed= 0;
-               MovieTrackingMarker *marker= &track->markers[a];
+               MovieTrackingMarker *cur_marker= &track->markers[a];
                MovieTrackingMarker *next_marker= NULL;
 
                if(next>=0 && next<track->markersnr)
@@ -890,11 +890,11 @@ static ImBuf *get_keyframed_ibuf(MovieTrackingContext *context, MovieTrackingTra
                if(next_marker && next_marker->flag&MARKER_DISABLED)
                        is_keyframed= 1;
 
-               is_keyframed|= (marker->flag&MARKER_TRACKED)==0;
+               is_keyframed|= (cur_marker->flag&MARKER_TRACKED)==0;
 
                if(is_keyframed) {
-                       framenr= marker->framenr;
-                       *marker_keyed= marker;
+                       framenr= cur_marker->framenr;
+                       *marker_keyed= cur_marker;
                        break;
                }
 
@@ -1080,7 +1080,6 @@ int BKE_tracking_next(MovieTrackingContext *context)
                                onbound= 1;
                        }
                        else if(track_context->track->tracker==TRACKER_KLT) {
-                               int wndx, wndy;
                                float *patch_new;
 
                                if(need_readjust) {
@@ -1103,11 +1102,8 @@ int BKE_tracking_next(MovieTrackingContext *context)
                                x2= pos[0];
                                y2= pos[1];
 
-                               wndx= (int)((track->pat_max[0]-track->pat_min[0])*ibuf_new->x)/2;
-                               wndy= (int)((track->pat_max[1]-track->pat_min[1])*ibuf_new->y)/2;
-
                                tracked= libmv_regionTrackerTrack(track_context->region_tracker, track_context->patch, patch_new,
-                                                       width, height, MAX2(wndx, wndy), x1, y1, &x2, &y2);
+                                                       width, height, x1, y1, &x2, &y2);
 
                                MEM_freeN(patch_new);
                        }