@@ -516,15 +516,15 @@ TwoViewGeometry Database::ReadTwoViewGeometry(const image_t image_id1,
516516
517517 SQLITE3_CALL (sqlite3_reset (sql_stmt_read_two_view_geometry_));
518518
519- if (SwapImagePair (image_id1, image_id2)) {
520- SwapFeatureMatchesBlob (&blob);
521- }
522-
523519 two_view_geometry.inlier_matches = FeatureMatchesFromBlob (blob);
524520 two_view_geometry.F .transposeInPlace ();
525521 two_view_geometry.E .transposeInPlace ();
526522 two_view_geometry.H .transposeInPlace ();
527523
524+ if (SwapImagePair (image_id1, image_id2)) {
525+ two_view_geometry.Invert ();
526+ }
527+
528528 return two_view_geometry;
529529}
530530
@@ -695,26 +695,32 @@ void Database::WriteTwoViewGeometry(
695695 SQLITE3_CALL (
696696 sqlite3_bind_int64 (sql_stmt_write_two_view_geometry_, 1 , pair_id));
697697
698- // Important: the swapped data must live until the query is executed.
699- FeatureMatchesBlob inlier_matches =
700- FeatureMatchesToBlob (two_view_geometry.inlier_matches );
698+ const TwoViewGeometry* two_view_geometry_ptr = &two_view_geometry;
699+
700+ // Invert the two-view geometry if the image pair has to be swapped.
701+ std::unique_ptr<TwoViewGeometry> swapped_two_view_geometry;
701702 if (SwapImagePair (image_id1, image_id2)) {
702- SwapFeatureMatchesBlob (&inlier_matches);
703+ swapped_two_view_geometry.reset (new TwoViewGeometry ());
704+ *swapped_two_view_geometry = two_view_geometry;
705+ swapped_two_view_geometry->Invert ();
706+ two_view_geometry_ptr = swapped_two_view_geometry.get ();
703707 }
704708
709+ const FeatureMatchesBlob inlier_matches =
710+ FeatureMatchesToBlob (two_view_geometry_ptr->inlier_matches );
705711 WriteDynamicMatrixBlob (sql_stmt_write_two_view_geometry_, inlier_matches, 2 );
706712
707713 SQLITE3_CALL (sqlite3_bind_int64 (sql_stmt_write_two_view_geometry_, 5 ,
708- two_view_geometry. config ));
714+ two_view_geometry_ptr-> config ));
709715
710716 // Transpose the matrices to obtain row-major data layout.
711717 // Important: Do not move these objects inside the if-statement, because
712718 // the objects must live until `sqlite3_step` is called on the statement.
713- const Eigen::Matrix3d Ft = two_view_geometry. F .transpose ();
714- const Eigen::Matrix3d Et = two_view_geometry. E .transpose ();
715- const Eigen::Matrix3d Ht = two_view_geometry. H .transpose ();
719+ const Eigen::Matrix3d Ft = two_view_geometry_ptr-> F .transpose ();
720+ const Eigen::Matrix3d Et = two_view_geometry_ptr-> E .transpose ();
721+ const Eigen::Matrix3d Ht = two_view_geometry_ptr-> H .transpose ();
716722
717- if (two_view_geometry. inlier_matches .size () > 0 ) {
723+ if (two_view_geometry_ptr-> inlier_matches .size () > 0 ) {
718724 WriteStaticMatrixBlob (sql_stmt_write_two_view_geometry_, Ft, 6 );
719725 WriteStaticMatrixBlob (sql_stmt_write_two_view_geometry_, Et, 7 );
720726 WriteStaticMatrixBlob (sql_stmt_write_two_view_geometry_, Ht, 8 );
0 commit comments