Camera tracking integration
authorSergey Sharybin <sergey.vfx@gmail.com>
Wed, 20 Jul 2011 20:31:25 +0000 (20:31 +0000)
committerSergey Sharybin <sergey.vfx@gmail.com>
Wed, 20 Jul 2011 20:31:25 +0000 (20:31 +0000)
===========================

- Fixed crash when holding down S and R keys.
- Fixed deleting of first marker.
- Tracking should now work fine with float buffers.
- Added option to show distortion grid. It's most probably
  temporary solution until [auto]calibration tools aren't
  merged.
  This grid defines straight lines on the image.

extern/libmv/libmv-capi.cpp
extern/libmv/libmv-capi.h
release/scripts/startup/bl_ui/space_clip.py
source/blender/blenkernel/BKE_tracking.h
source/blender/blenkernel/intern/tracking.c
source/blender/editors/include/ED_transform.h
source/blender/editors/space_clip/clip_draw.c
source/blender/editors/transform/transform.c
source/blender/makesdna/DNA_space_types.h
source/blender/makesrna/intern/rna_space.c

index 0595ac304944fa1b0ac2d30ddb4109d6742c2505..2041c9460fc0b9850077d0f15d04409769426ad5 100644 (file)
@@ -450,3 +450,19 @@ void libmv_applyCameraIntrinsics(double focal_length, double principal_x, double
                intrinsics.ApplyIntrinsics(x, y, x1, y1);
        }
 }
+
+void libmv_InvertIntrinsics(double focal_length, double principal_x, double principal_y, double k1, double k2, double k3,
+                       double x, double y, double *x1, double *y1)
+{
+       libmv::CameraIntrinsics intrinsics;
+
+       intrinsics.SetFocalLength(focal_length, focal_length);
+       intrinsics.SetPrincipalPoint(principal_x, principal_y);
+       intrinsics.SetRadialDistortion(k1, k2, k3);
+
+       if(focal_length) {
+               /* do a lens distortion if focal length is non-zero only */
+
+               intrinsics.InvertIntrinsics(x, y, x1, y1);
+       }
+}
index 094f787e616a25bc2ba551eedb873dfd77dd55f5..cad93b3c538662c6b38012797bdb682f307fa1de 100644 (file)
@@ -72,6 +72,8 @@ void libmv_destroyCorners(struct libmv_Corners *corners);
 /* utils */
 void libmv_applyCameraIntrinsics(double focal_length, double principal_x, double principal_y, double k1, double k2, double k3,
                        double x, double y, double *x1, double *y1);
+void libmv_InvertIntrinsics(double focal_length, double principal_x, double principal_y, double k1, double k2, double k3,
+                       double x, double y, double *x1, double *y1);
 
 #ifdef __cplusplus
 }
index 2d3693cdad58501763e61f6191c5570c151ae3eb..642ecdd0e06abfc0eecf3508955ea818ea57620e 100644 (file)
@@ -332,7 +332,10 @@ class CLIP_PT_display(bpy.types.Panel):
         row.prop(sc, "show_disabled", text="Disabled")
         row.prop(sc, "show_bundles", text="Bundles")
 
-        layout.prop(sc, "show_names", text="Names")
+        row = layout.row()
+        row.prop(sc, "show_names", text="Names")
+        row.prop(sc, "show_grid", text="Grid")
+
         layout.prop(sc, "show_tiny_markers", text="Tiny Markers")
 
         layout.prop(sc, "lock_selection")
index 79c0e6c62a5349291bd642e133060ab865054112..75cb457734ccb1a046a482e1e2fc534357751b7e 100644 (file)
@@ -79,6 +79,7 @@ struct MovieReconstructedCamera *BKE_tracking_get_reconstructed_camera(struct Mo
 void BKE_get_tracking_mat(struct Scene *scene, float mat[4][4]);
 void BKE_tracking_projection_matrix(struct MovieTracking *tracking, int framenr, int winx, int winy, float mat[4][4]);
 void BKE_tracking_apply_intrinsics(struct MovieTracking *tracking, float co[2], float width, float height, float nco[2]);
+void BKE_tracking_invert_intrinsics(struct MovieTracking *tracking, float co[2], float width, float height, float nco[2]);
 
 void BKE_tracking_detect(struct MovieTracking *tracking, struct ImBuf *imbuf, int framenr);
 
index 571aeac0e5f2af12ce113bbaa3f1a69a877fa4e4..d009a42e8311b78eef41a092dab41915ab13ab7d 100644 (file)
@@ -216,7 +216,7 @@ void BKE_tracking_insert_marker(MovieTrackingTrack *track, MovieTrackingMarker *
 
 void BKE_tracking_delete_marker(MovieTrackingTrack *track, int framenr)
 {
-       int a= 1;
+       int a= 0;
 
        while(a<track->markersnr) {
                if(track->markers[a].framenr==framenr) {
@@ -234,7 +234,8 @@ void BKE_tracking_delete_marker(MovieTrackingTrack *track, int framenr)
                }
 
                a++;
-       }}
+       }
+}
 
 MovieTrackingMarker *BKE_tracking_get_marker(MovieTrackingTrack *track, int framenr)
 {
@@ -472,11 +473,20 @@ static void disable_imbuf_channels(ImBuf *ibuf, MovieTrackingTrack *track)
        for(y= 0; y<ibuf->y; y++) {
                for (x= 0; x<ibuf->x; x++) {
                        int pixel= ibuf->x*y + x;
-                       char *rrgb= (char*)ibuf->rect + pixel*4;
 
-                       if(track->flag&TRACK_DISABLE_RED)       rrgb[0]= 0;
-                       if(track->flag&TRACK_DISABLE_GREEN)     rrgb[1]= 0;
-                       if(track->flag&TRACK_DISABLE_BLUE)      rrgb[2]= 0;
+                       if(ibuf->rect_float) {
+                               float *rrgbf= ibuf->rect_float + pixel*4;
+
+                               if(track->flag&TRACK_DISABLE_RED)       rrgbf[0]= 0;
+                               if(track->flag&TRACK_DISABLE_GREEN)     rrgbf[1]= 0;
+                               if(track->flag&TRACK_DISABLE_BLUE)      rrgbf[2]= 0;
+                       } else {
+                               char *rrgb= (char*)ibuf->rect + pixel*4;
+
+                               if(track->flag&TRACK_DISABLE_RED)       rrgb[0]= 0;
+                               if(track->flag&TRACK_DISABLE_GREEN)     rrgb[1]= 0;
+                               if(track->flag&TRACK_DISABLE_BLUE)      rrgb[2]= 0;
+                       }
                }
        }
 }
@@ -554,9 +564,17 @@ static float *acquire_search_floatbuf(ImBuf *ibuf, MovieTrackingTrack *track, Mo
        for(y= 0; y<(int)height; y++) {
                for (x= 0; x<(int)width; x++) {
                        int pixel= tmpibuf->x*y + x;
-                       char *rrgb= (char*)tmpibuf->rect + pixel*4;
 
-                       *fp= (0.2126*rrgb[0] + 0.7152*rrgb[1] + 0.0722*rrgb[2])/255;
+                       if(tmpibuf->rect_float) {
+                               float *rrgbf= ibuf->rect_float + pixel*4;
+
+                               *fp= (0.2126*rrgbf[0] + 0.7152*rrgbf[1] + 0.0722*rrgbf[2])/255;
+                       } else {
+                               char *rrgb= (char*)tmpibuf->rect + pixel*4;
+
+                               *fp= (0.2126*rrgb[0] + 0.7152*rrgb[1] + 0.0722*rrgb[2])/255;
+                       }
+
                        fp++;
                }
        }
@@ -964,7 +982,7 @@ void BKE_tracking_apply_intrinsics(MovieTracking *tracking, float co[2], float w
        MovieTrackingCamera *camera= &tracking->camera;
 
 #ifdef WITH_LIBMV
-       double x= nco[0], y= nco[1];
+       double x, y;
 
        /* normalize coords */
        x= (co[0]-camera->principal[0]) / camera->focal;
@@ -979,6 +997,21 @@ void BKE_tracking_apply_intrinsics(MovieTracking *tracking, float co[2], float w
 #endif
 }
 
+void BKE_tracking_invert_intrinsics(MovieTracking *tracking, float co[2], float width, float height, float nco[2])
+{
+       MovieTrackingCamera *camera= &tracking->camera;
+
+#ifdef WITH_LIBMV
+       double x= co[0], y= co[1];
+
+       libmv_InvertIntrinsics(camera->focal, camera->principal[0], camera->principal[1],
+                               camera->k1, camera->k2, camera->k3, x, y, &x, &y);
+
+       nco[0]= x * camera->focal + camera->principal[0];
+       nco[1]= y * camera->focal + camera->principal[1];
+#endif
+}
+
 #ifdef WITH_LIBMV
 /* flips upside-down */
 static unsigned char *acquire_ucharbuf(ImBuf *ibuf)
@@ -990,10 +1023,18 @@ static unsigned char *acquire_ucharbuf(ImBuf *ibuf)
        for(y= 0; y<ibuf->y; y++) {
                for (x= 0; x<ibuf->x; x++) {
                        int pixel= ibuf->x*(ibuf->y-y-1) + x;
-                       char *rrgb= (char*)ibuf->rect + pixel*4;
 
-                       //*fp= 0.2126f*rrgb[0] + 0.7152f*rrgb[1] + 0.0722f*rrgb[2];
-                       *fp= (11*rrgb[0]+16*rrgb[1]+5*rrgb[2])/32;
+                       if(ibuf->rect_float) {
+                               float *rrgbf= ibuf->rect_float + pixel*4;
+
+                               //*fp= 0.2126f*rrgbf[0] + 0.7152f*rrgbf[1] + 0.0722f*rrgbf[2];
+                               *fp= (11*rrgbf[0]+16*rrgbf[1]+5*rrgbf[2])/32;
+                       } else {
+                               char *rrgb= (char*)ibuf->rect + pixel*4;
+
+                               //*fp= 0.2126f*rrgb[0] + 0.7152f*rrgb[1] + 0.0722f*rrgb[2];
+                               *fp= (11*rrgb[0]+16*rrgb[1]+5*rrgb[2])/32;
+                       }
 
                        fp++;
                }
index 0e955f93e76e341636ede970a21c5cb52d57c07c..3fd3a0ba0e1878708c083a17de4f2663dcf0de06 100644 (file)
@@ -96,7 +96,7 @@ enum {
 #define CTX_AUTOCONFIRM                32
 #define CTX_BMESH                      64
 #define CTX_NDOF                       128
-#define CTX_VIDEOCLIP          256
+#define CTX_MOVIECLIP          256
 
 /* Standalone call to get the transformation center corresponding to the current situation
  * returns 1 if successful, 0 otherwise (usually means there's no selection)
index 2b4d382c901754d77e77a29c72275f4a9cae7a8b..7a1d9dda4d70ad28d463dfb392328a0da086032c 100644 (file)
@@ -579,6 +579,51 @@ static void view2d_to_region_float(View2D *v2d, float x, float y, float *regionx
        *regiony= v2d->mask.ymin + y*(v2d->mask.ymax-v2d->mask.ymin);
 }
 
+static void draw_distorion_grid(MovieTracking *tracking, int width, int height)
+{
+       const int n= 9;
+       int x, y;
+       float pos[2], grid[10][10][2];
+       float dx= (float)width/n, dy= (float)height/n;
+
+       if(!tracking->camera.focal)
+               return;
+
+       zero_v2(pos);
+
+       for(y= 0; y<=n; y++) {
+               for(x= 0; x<=n; x++) {
+                       BKE_tracking_invert_intrinsics(tracking, pos, width, height, grid[y][x]);
+
+                       grid[y][x][0]/= width;
+                       grid[y][x][1]/= height;
+
+                       pos[0]+= dx;
+               }
+
+               pos[0]= 0.f;
+               pos[1]+= dy;
+       }
+
+       glColor3f(1.f, 0.f, 0.f);
+
+       for(y= 0; y<=n; y++) {
+               glBegin(GL_LINE_STRIP);
+                       for(x= 0; x<=n; x++) {
+                               glVertex2fv(grid[y][x]);
+                       }
+               glEnd();
+       }
+
+       for(x= 0; x<=n; x++) {
+               glBegin(GL_LINE_STRIP);
+                       for(y= 0; y<=n; y++) {
+                               glVertex2fv(grid[y][x]);
+                       }
+               glEnd();
+       }
+}
+
 static void draw_tracking_tracks(SpaceClip *sc, ARegion *ar, MovieClip *clip,
                        int width, int height, float zoomx, float zoomy)
 {
@@ -709,6 +754,9 @@ static void draw_tracking_tracks(SpaceClip *sc, ARegion *ar, MovieClip *clip,
                glDisable(GL_POINT_SMOOTH);
        }
 
+       if(sc->flag&SC_SHOW_GRID)
+               draw_distorion_grid(tracking, width, height);
+
        glPopMatrix();
 
        if(sc->flag&SC_SHOW_NAMES) {
index 3c45038b1593329097b7f25a5e7484cf8ef09cd3..d7110ac81ba6972ace78c9df88d7e5e72ebeb2c6 100644 (file)
@@ -628,7 +628,7 @@ int transformEvent(TransInfo *t, wmEvent *event)
                                break;
                        case TFM_MODAL_ROTATE:
                                /* only switch when... */
-                               if(!(t->options & CTX_TEXTURE) && !(t->options & CTX_VIDEOCLIP)) {
+                               if(!(t->options & CTX_TEXTURE) && !(t->options & CTX_MOVIECLIP)) {
                                        if( ELEM4(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_TRANSLATION) ) {
                                                
                                                resetTransRestrictions(t);
@@ -883,7 +883,7 @@ int transformEvent(TransInfo *t, wmEvent *event)
                        break;
                case RKEY:
                        /* only switch when... */
-                       if(!(t->options & CTX_TEXTURE)) {
+                       if(!(t->options & CTX_TEXTURE) && !(t->options & CTX_MOVIECLIP)) {
                                if( ELEM4(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_TRANSLATION) ) {
 
                                        resetTransRestrictions(t);
@@ -1587,7 +1587,7 @@ int initTransform(bContext *C, TransInfo *t, wmOperator *op, wmEvent *event, int
        else if(t->spacetype == SPACE_CLIP) {
                unit_m3(t->spacemtx);
                t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
-               t->options |= CTX_VIDEOCLIP;
+               t->options |= CTX_MOVIECLIP;
        }
        else
                unit_m3(t->spacemtx);
index 31a605e1030660a62a314bcefeade884b852f7ae..4d861327011b5d9ca055aa7f4ad6d2a3d6e728e1 100644 (file)
@@ -971,6 +971,7 @@ enum {
 #define SC_MUTE_FOOTAGE                        (1<<6)
 #define SC_HIDE_DISABLED               (1<<7)
 #define SC_SHOW_NAMES                  (1<<8)
+#define SC_SHOW_GRID                   (1<<9)
 
 
 /* space types, moved from DNA_screen_types.h */
index e63a3fb0c96edbfe3a44eeac22e56f740c12c175..54f7bc38e7aa6b4d149fa8b7315f903dcc5879f0 100644 (file)
@@ -2727,6 +2727,12 @@ static void rna_def_space_clip(BlenderRNA *brna)
        RNA_def_property_boolean_sdna(prop, NULL, "flag", SC_SHOW_NAMES);
        RNA_def_property_ui_text(prop, "Show Names", "Show track names and status");
        RNA_def_property_update(prop, NC_SPACE|ND_SPACE_CLIP, NULL);
+
+       /* show grid */
+       prop= RNA_def_property(srna, "show_grid", PROP_BOOLEAN, PROP_NONE);
+       RNA_def_property_boolean_sdna(prop, NULL, "flag", SC_SHOW_GRID);
+       RNA_def_property_ui_text(prop, "Show Grid", "Show grid showing lens distortion");
+       RNA_def_property_update(prop, NC_SPACE|ND_SPACE_CLIP, NULL);
 }