[Bf-blender-cvs] [7dea18b3aa1] master: Tracking: Store lens principal point in normalized space

Sergey Sharybin noreply at git.blender.org
Tue Nov 22 11:54:24 CET 2022


Commit: 7dea18b3aa17b73fca470c9402a75f68a0a6c75b
Author: Sergey Sharybin
Date:   Fri Nov 18 17:15:13 2022 +0100
Branches: master
https://developer.blender.org/rB7dea18b3aa17b73fca470c9402a75f68a0a6c75b

Tracking: Store lens principal point in normalized space

This avoids need to do special trickery detecting whether the principal
point is to be changed when reloading movie clip. This also allows to
transfer the optical center from high-res footage to possibly its lower
resolution proxy without manual adjustment.

On a user level the difference is that the principal point is exposed in
the normalized coordinates: frame center has coordinate of (0, 0), left
bottom corner of a frame has coordinate of (-1, -1) and the right top
corner has coordinate of (1, 1).

Another user-visible change is that there is no more operator for setting
the principal point to center: use backspace on the center sliders will
reset values to 0 which corresponds to the center.

The code implements versioning in both directions, so it should be
possible to open file in older Blender versions without loosing
configuration.

For the Python API there are two ways to access the property:
- `tracking.camera.principal_point` which is measured in the normalized
  space.
- `tracking.camera.principal_point_pixels` to access the pixel-space
  principal point.

Both properties are not animatable, so there will by no conflict coming.

Differential Revision: https://developer.blender.org/D16573

===================================================================

M	release/scripts/startup/bl_ui/space_clip.py
M	source/blender/blenkernel/BKE_blender_version.h
M	source/blender/blenkernel/BKE_tracking.h
M	source/blender/blenkernel/intern/movieclip.c
M	source/blender/blenkernel/intern/tracking.c
M	source/blender/blenkernel/intern/tracking_util.c
M	source/blender/blenkernel/tracking_private.h
M	source/blender/blenloader/intern/versioning_300.cc
M	source/blender/editors/space_clip/clip_intern.h
M	source/blender/editors/space_clip/space_clip.c
M	source/blender/editors/space_clip/tracking_ops.c
M	source/blender/makesdna/DNA_tracking_types.h
M	source/blender/makesdna/intern/dna_rename_defs.h
M	source/blender/makesrna/intern/rna_tracking.c

===================================================================

diff --git a/release/scripts/startup/bl_ui/space_clip.py b/release/scripts/startup/bl_ui/space_clip.py
index 2cb523442dc..fd46fe3099e 100644
--- a/release/scripts/startup/bl_ui/space_clip.py
+++ b/release/scripts/startup/bl_ui/space_clip.py
@@ -920,8 +920,7 @@ class CLIP_PT_tracking_lens(Panel):
         col.prop(camera, "units", text="Units")
 
         col = layout.column()
-        col.prop(clip.tracking.camera, "principal", text="Optical Center")
-        col.operator("clip.set_center_principal", text="Set Center")
+        col.prop(clip.tracking.camera, "principal_point", text="Optical Center")
 
         col = layout.column()
         col.prop(camera, "distortion_model", text="Lens Distortion")
@@ -1353,7 +1352,6 @@ class CLIP_MT_clip(Menu):
 
         if clip:
             layout.operator("clip.set_scene_frames")
-            layout.operator("clip.set_center_principal")
             layout.operator("clip.prefetch")
             layout.operator("clip.reload")
             layout.menu("CLIP_MT_proxy")
diff --git a/source/blender/blenkernel/BKE_blender_version.h b/source/blender/blenkernel/BKE_blender_version.h
index 9c5ed01bed1..5709891893b 100644
--- a/source/blender/blenkernel/BKE_blender_version.h
+++ b/source/blender/blenkernel/BKE_blender_version.h
@@ -25,7 +25,7 @@ extern "C" {
 
 /* Blender file format version. */
 #define BLENDER_FILE_VERSION BLENDER_VERSION
-#define BLENDER_FILE_SUBVERSION 1
+#define BLENDER_FILE_SUBVERSION 2
 
 /* Minimum Blender version that supports reading file written with the current
  * version. Older Blender versions will test this and show a warning if the file
diff --git a/source/blender/blenkernel/BKE_tracking.h b/source/blender/blenkernel/BKE_tracking.h
index 38cb2b17f72..63aad6b4dff 100644
--- a/source/blender/blenkernel/BKE_tracking.h
+++ b/source/blender/blenkernel/BKE_tracking.h
@@ -457,6 +457,12 @@ void BKE_tracking_camera_get_reconstructed_interpolate(struct MovieTracking *tra
                                                        float framenr,
                                                        float mat[4][4]);
 
+/* Access the principal point in pixels space. */
+void BKE_tracking_camera_principal_point_pixel_get(struct MovieClip *clip,
+                                                   float r_principal_point_pixel[2]);
+void BKE_tracking_camera_principal_point_pixel_set(struct MovieClip *clip,
+                                                   const float principal_point_pixel[2]);
+
 /* --------------------------------------------------------------------
  * (Un)distortion.
  */
diff --git a/source/blender/blenkernel/intern/movieclip.c b/source/blender/blenkernel/intern/movieclip.c
index f77a03c4eac..6ea537f9038 100644
--- a/source/blender/blenkernel/intern/movieclip.c
+++ b/source/blender/blenkernel/intern/movieclip.c
@@ -69,6 +69,8 @@
 
 #include "BLO_read_write.h"
 
+#include "tracking_private.h"
+
 /* Convert camera object to legacy format where the camera tracks are stored in the MovieTracking
  * structure when saving .blend file. */
 #define USE_LEGACY_CAMERA_OBJECT_FORMAT_ON_SAVE 1
@@ -221,6 +223,16 @@ static void movieclip_blend_write(BlendWriter *writer, ID *id, const void *id_ad
   }
 #endif
 
+  /* Assign the pixel-space principal point for forward compatibility. */
+  /* TODO(sergey): Remove with the next major version update when forward compatibility is allowed
+   * to be broken. */
+  if (!is_undo && clip->lastsize[0] != 0 && clip->lastsize[1] != 0) {
+    tracking_principal_point_normalized_to_pixel(tracking->camera.principal_point,
+                                                 clip->lastsize[0],
+                                                 clip->lastsize[1],
+                                                 tracking->camera.principal_legacy);
+  }
+
   BLO_write_id_struct(writer, MovieClip, id_address, &clip->id);
   BKE_id_blend_write(writer, &clip->id);
 
@@ -725,7 +737,7 @@ typedef struct MovieClipCache {
 
     /* cache for undistorted shot */
     float focal_length;
-    float principal[2];
+    float principal_point[2];
     float polynomial_k[3];
     float division_k[2];
     float nuke_k[2];
@@ -981,17 +993,6 @@ static void movieclip_load_get_size(MovieClip *clip)
   }
 }
 
-static void movieclip_principal_to_center(MovieClip *clip)
-{
-  MovieClipUser user = *DNA_struct_default_get(MovieClipUser);
-
-  int width, height;
-  BKE_movieclip_get_size(clip, &user, &width, &height);
-
-  clip->tracking.camera.principal[0] = ((float)width) / 2.0f;
-  clip->tracking.camera.principal[1] = ((float)height) / 2.0f;
-}
-
 static void detect_clip_source(Main *bmain, MovieClip *clip)
 {
   ImBuf *ibuf;
@@ -1041,7 +1042,6 @@ MovieClip *BKE_movieclip_file_add(Main *bmain, const char *name)
     clip->tracking.camera.focal = 24.0f * width / clip->tracking.camera.sensor_width;
   }
 
-  movieclip_principal_to_center(clip);
   movieclip_calc_length(clip);
 
   return clip;
@@ -1154,7 +1154,7 @@ static bool check_undistortion_cache_flags(const MovieClip *clip)
   }
 
   /* check for distortion model changes */
-  if (!equals_v2v2(camera->principal, cache->postprocessed.principal)) {
+  if (!equals_v2v2(camera->principal_point, cache->postprocessed.principal_point)) {
     return false;
   }
 
@@ -1279,7 +1279,7 @@ static void put_postprocessed_frame_to_cache(
   if (need_undistortion_postprocess(user, flag)) {
     cache->postprocessed.distortion_model = camera->distortion_model;
     cache->postprocessed.focal_length = camera->focal;
-    copy_v2_v2(cache->postprocessed.principal, camera->principal);
+    copy_v2_v2(cache->postprocessed.principal_point, camera->principal_point);
     copy_v3_v3(cache->postprocessed.polynomial_k, &camera->k1);
     copy_v2_v2(cache->postprocessed.division_k, &camera->division_k1);
     copy_v2_v2(cache->postprocessed.nuke_k, &camera->nuke_k1);
@@ -1719,26 +1719,12 @@ void BKE_movieclip_reload(Main *bmain, MovieClip *clip)
   /* update clip source */
   detect_clip_source(bmain, clip);
 
-  const int old_width = clip->lastsize[0];
-  const int old_height = clip->lastsize[1];
-
   /* Tag for re-calculation of the actual size. */
   clip->lastsize[0] = clip->lastsize[1] = 0;
 
   movieclip_load_get_size(clip);
   movieclip_calc_length(clip);
 
-  int width, height;
-  MovieClipUser user = *DNA_struct_default_get(MovieClipUser);
-  BKE_movieclip_get_size(clip, &user, &width, &height);
-
-  /* If the resolution changes then re-initialize the principal point.
-   * Ideally the principal point will be in some sort of relative space, but this is not how it is
-   * designed currently. The code should cover the most of the common cases. */
-  if (width != old_width || height != old_height) {
-    movieclip_principal_to_center(clip);
-  }
-
   BKE_ntree_update_tag_id_changed(bmain, &clip->id);
 }
 
diff --git a/source/blender/blenkernel/intern/tracking.c b/source/blender/blenkernel/intern/tracking.c
index a49464a11eb..dfb39c7bc37 100644
--- a/source/blender/blenkernel/intern/tracking.c
+++ b/source/blender/blenkernel/intern/tracking.c
@@ -14,6 +14,7 @@
 
 #include "DNA_anim_types.h"
 #include "DNA_camera_types.h"
+#include "DNA_defaults.h"
 #include "DNA_gpencil_types.h"
 #include "DNA_movieclip_types.h"
 #include "DNA_object_types.h" /* SELECT */
@@ -51,7 +52,7 @@
 typedef struct MovieDistortion {
   struct libmv_CameraIntrinsics *intrinsics;
   /* Parameters needed for coordinates normalization. */
-  float principal[2];
+  float principal_px[2];
   float pixel_aspect;
   float focal;
 } MovieDistortion;
@@ -2103,10 +2104,14 @@ static void reconstructed_camera_scale_set(const MovieTrackingObject *tracking_o
 void BKE_tracking_camera_shift_get(
     MovieTracking *tracking, int winx, int winy, float *shiftx, float *shifty)
 {
+  float principal_px[2];
+  tracking_principal_point_normalized_to_pixel(
+      tracking->camera.principal_point, winx, winy, principal_px);
+
   /* Indeed in both of cases it should be winx -
    * it's just how camera shift works for blender's camera. */
-  *shiftx = (0.5f * winx - tracking->camera.principal[0]) / winx;
-  *shifty = (0.5f * winy - tracking->camera.principal[1]) / winx;
+  *shiftx = (0.5f * winx - principal_px[0]) / winx;
+  *shifty = (0.5f * winy - principal_px[1]) / winx;
 }
 
 void BKE_tracking_camera_to_blender(
@@ -2167,6 +2172,32 @@ void BKE_tracking_camera_get_reconstructed_interpolate(MovieTracking *UNUSED(tra
   reconstructed_camera_scale_set(tracking_object, mat);
 }
 
+void BKE_tracking_camera_principal_point_pixel_get(struct MovieClip *clip,
+                                                   float r_principal_point_pixel[2])
+{
+  const MovieTrackingCamera *camera = &clip->tracking.camera;
+
+  int frame_width, frame_height;
+  MovieClipUser user = *DNA_struct_default_get(MovieClipUser);
+  BKE_movieclip_get_size(clip, &user, &frame_width, &frame_height);
+
+  tracking_principal_point_normalized_to_pixel(
+      camera->principal_point, frame_width, frame_height, r_principal_point_pixel);
+}
+
+void BKE_tracking_camera_principal_point_pixel_set(struct MovieClip *clip,
+                                                   const float principal_point_pixel[2])
+{
+  MovieTrackingCamera *camera = &clip->tracking.camera;
+
+  int frame_width, frame_height;
+  MovieClipUser user = *DNA_struct_default_get(MovieClipUser);
+  BKE_movieclip_get_size(clip, &user, &frame_width, &frame_height);
+
+  tracking_principal_point_pixel_to_normalized(
+      principal_point_pixel, frame_width, frame_height, camera->principal_point);
+}
+
 /* --------------------------------------------------------------------
  * (Un)distortion.
  */
@@ -2185,7 +2216,10 @@ MovieDistortion *BKE_tracking_distortion_new(MovieTracking *tracking,
   distortion->intrinsics = libmv_cameraIntrinsicsNew(&camera_intrinsics_options);
 
   const MovieTrackingCamera *camera = &tracking->camera;
-  copy_v2_v2(distortion->principal, camera->principal);
+  tracking_principal_point_normalized_to_pixel(tracking->camera.principal_point,
+                                               calibration_width,
+                                               calibration_height,
+                                               distortion->principal_px);

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list