@@ -286,11 +286,42 @@ TEST(FeatureMatcherController, MatchMultiplePairs) {
286286 EXPECT_EQ (data.database ->ReadTwoViewGeometries ().size (), 6 );
287287}
288288
289+ TEST (FeatureMatcherController, MatchReplacesExistingGeometryAfterSuccess) {
290+ auto data = CreateTestData (2 );
291+ data.database ->ClearMatches ();
292+ data.database ->ClearTwoViewGeometries ();
293+ const image_t id1 = data.image_ids [0 ];
294+ const image_t id2 = data.image_ids [1 ];
295+ TwoViewGeometry old_geometry;
296+ old_geometry.config = TwoViewGeometry::CALIBRATED ;
297+ old_geometry.E = Eigen::Matrix3d::Identity ();
298+ old_geometry.inlier_matches = {{0 , 0 }, {1 , 1 }};
299+ data.database ->WriteTwoViewGeometry (id1, id2, old_geometry);
300+
301+ FeatureMatchingOptions options = DefaultMatchingOptions ();
302+ options.skip_geometric_verification = true ;
303+ FeatureMatcherController controller (
304+ options, TwoViewGeometryOptions (), data.cache );
305+ ASSERT_TRUE (controller.Setup ());
306+ controller.Match ({{id1, id2}});
307+ EXPECT_TRUE (data.database ->ExistsMatches (id1, id2));
308+ EXPECT_FALSE (data.database ->ExistsTwoViewGeometry (id1, id2));
309+ controller.Match ({{id1, id2}});
310+ EXPECT_TRUE (data.database ->ExistsMatches (id1, id2));
311+ }
312+
289313TEST (FeatureMatcherController, MatchPropagatesMatcherErrors) {
290314 auto data = CreateTestData (4 );
291315 data.database ->ClearMatches ();
292316 data.database ->ClearTwoViewGeometries ();
293317
318+ TwoViewGeometry original_geometry;
319+ original_geometry.config = TwoViewGeometry::CALIBRATED ;
320+ original_geometry.E = Eigen::Matrix3d::Identity ();
321+ original_geometry.inlier_matches = {{0 , 0 }, {1 , 1 }};
322+ data.database ->WriteTwoViewGeometry (
323+ data.image_ids [0 ], data.image_ids [1 ], original_geometry);
324+
294325 // Descriptors of an unexpected dimension make the SIFT matcher throw for all
295326 // pairs with the first image.
296327 std::vector<FeatureDescriptors> descriptors;
@@ -319,12 +350,126 @@ TEST(FeatureMatcherController, MatchPropagatesMatcherErrors) {
319350 EXPECT_FALSE (
320351 data.database ->ExistsMatches (data.image_ids [0 ], data.image_ids [1 ]));
321352
353+ const auto retained =
354+ data.database ->ReadTwoViewGeometry (data.image_ids [0 ], data.image_ids [1 ]);
355+ EXPECT_EQ (retained.config , original_geometry.config );
356+ ASSERT_TRUE (retained.E .has_value ());
357+ EXPECT_EQ (*retained.E , *original_geometry.E );
358+ EXPECT_FALSE (retained.F .has_value ());
359+ EXPECT_FALSE (retained.H .has_value ());
360+ EXPECT_FALSE (retained.cam2_from_cam1 .has_value ());
361+ EXPECT_FALSE (retained.camera1 .has_value ());
362+ EXPECT_FALSE (retained.camera2 .has_value ());
363+ ASSERT_EQ (retained.inlier_matches .size (),
364+ original_geometry.inlier_matches .size ());
365+ for (size_t i = 0 ; i < original_geometry.inlier_matches .size (); ++i) {
366+ EXPECT_EQ (retained.inlier_matches [i].point2D_idx1 ,
367+ original_geometry.inlier_matches [i].point2D_idx1 );
368+ EXPECT_EQ (retained.inlier_matches [i].point2D_idx2 ,
369+ original_geometry.inlier_matches [i].point2D_idx2 );
370+ }
371+
322372 // The controller remains usable for pairs without the broken image.
323373 controller.Match ({{data.image_ids [1 ], data.image_ids [2 ]}});
324374 EXPECT_TRUE (
325375 data.database ->ExistsMatches (data.image_ids [1 ], data.image_ids [2 ]));
326376}
327377
378+ TEST (FeatureMatcherController, MatchRetainsRawMatchesOnVerifierError) {
379+ auto data = CreateTestData (3 );
380+ data.database ->ClearMatches ();
381+ data.database ->ClearTwoViewGeometries ();
382+ const image_t id1 = data.image_ids [0 ];
383+ const image_t id2 = data.image_ids [1 ];
384+ const image_t id3 = data.image_ids [2 ];
385+ FeatureMatches original;
386+ for (point2D_t i = 0 ; i < 20 ; ++i) {
387+ original.emplace_back (i, i);
388+ }
389+ data.database ->WriteMatches (id1, id2, original);
390+ data.database ->WriteMatches (id2, id3, original);
391+
392+ auto invalid_keypoints = data.database ->ReadKeypointsBlob (id1);
393+ invalid_keypoints = FeatureKeypointsBlob::Zero (invalid_keypoints.rows (), 3 );
394+ const auto keypoints2 = data.database ->ReadKeypointsBlob (id2);
395+ const auto keypoints3 = data.database ->ReadKeypointsBlob (id3);
396+ data.database ->ClearKeypoints ();
397+ data.database ->WriteKeypoints (id1, invalid_keypoints);
398+ data.database ->WriteKeypoints (id2, keypoints2);
399+ data.database ->WriteKeypoints (id3, keypoints3);
400+ data.cache = std::make_shared<FeatureMatcherCache>(100 , data.database );
401+
402+ FeatureMatcherController controller (
403+ DefaultMatchingOptions (), TwoViewGeometryOptions (), data.cache );
404+ ASSERT_TRUE (controller.Setup ());
405+ EXPECT_THAT (
406+ ThrownMessage ([&]() { controller.Match ({{id1, id2}, {id2, id3}}); }),
407+ testing::HasSubstr (" Keypoint format not supported" ));
408+ const auto retained = data.database ->ReadMatches (id1, id2);
409+ ASSERT_EQ (retained.size (), original.size ());
410+ for (size_t i = 0 ; i < original.size (); ++i) {
411+ EXPECT_EQ (retained[i].point2D_idx1 , original[i].point2D_idx1 );
412+ EXPECT_EQ (retained[i].point2D_idx2 , original[i].point2D_idx2 );
413+ }
414+ EXPECT_FALSE (data.database ->ExistsTwoViewGeometry (id1, id2));
415+ EXPECT_TRUE (data.database ->ExistsTwoViewGeometry (id2, id3));
416+ // Force a new verification job after draining both successful and failed
417+ // jobs.
418+ data.database ->DeleteTwoViewGeometry (id2, id3);
419+ ASSERT_FALSE (data.database ->ExistsTwoViewGeometry (id2, id3));
420+ controller.Match ({{id2, id3}});
421+ EXPECT_TRUE (data.database ->ExistsTwoViewGeometry (id2, id3));
422+ }
423+
424+ TEST (FeatureMatcherController, GuidedFailureRetainsRawMatches) {
425+ auto data = CreateTestData (3 );
426+ data.database ->ClearMatches ();
427+ data.database ->ClearTwoViewGeometries ();
428+ const image_t id1 = data.image_ids [0 ];
429+ const image_t id2 = data.image_ids [1 ];
430+ const image_t id3 = data.image_ids [2 ];
431+ FeatureMatches original;
432+ for (point2D_t i = 0 ; i < 20 ; ++i) {
433+ original.emplace_back (i, i);
434+ }
435+ data.database ->WriteMatches (id1, id2, original);
436+
437+ auto descriptors1 = data.database ->ReadDescriptors (id1);
438+ const auto descriptors2 = data.database ->ReadDescriptors (id2);
439+ const auto descriptors3 = data.database ->ReadDescriptors (id3);
440+ descriptors1.data =
441+ FeatureDescriptorsData::Zero (descriptors1.data .rows (), 64 );
442+ data.database ->ClearDescriptors ();
443+ data.database ->WriteDescriptors (id1, descriptors1);
444+ data.database ->WriteDescriptors (id2, descriptors2);
445+ data.database ->WriteDescriptors (id3, descriptors3);
446+ data.cache = std::make_shared<FeatureMatcherCache>(100 , data.database );
447+
448+ FeatureMatchingOptions matching_options = DefaultMatchingOptions ();
449+ matching_options.guided_matching = true ;
450+ TwoViewGeometryOptions geometry_options;
451+ // The verifier completes without touching descriptors; guided SIFT checks
452+ // their dimensions even when the geometry was marked DEGENERATE.
453+ geometry_options.min_num_inliers = 200 ;
454+ FeatureMatcherController controller (
455+ matching_options, geometry_options, data.cache );
456+ ASSERT_TRUE (controller.Setup ());
457+ EXPECT_THAT (ThrownMessage ([&]() { controller.Match ({{id1, id2}}); }),
458+ testing::HasSubstr (" kSiftDescriptorDim" ));
459+ const auto retained = data.database ->ReadMatches (id1, id2);
460+ ASSERT_EQ (retained.size (), original.size ());
461+ for (size_t i = 0 ; i < original.size (); ++i) {
462+ EXPECT_EQ (retained[i].point2D_idx1 , original[i].point2D_idx1 );
463+ EXPECT_EQ (retained[i].point2D_idx2 , original[i].point2D_idx2 );
464+ }
465+ EXPECT_FALSE (data.database ->ExistsTwoViewGeometry (id1, id2));
466+ // A fresh healthy pair must traverse the workers after the guided error.
467+ ASSERT_FALSE (data.database ->ExistsMatches (id2, id3));
468+ controller.Match ({{id2, id3}});
469+ EXPECT_TRUE (data.database ->ExistsMatches (id2, id3));
470+ EXPECT_TRUE (data.database ->ExistsTwoViewGeometry (id2, id3));
471+ }
472+
328473TEST (FeatureMatcherController, SetupPropagatesMatcherCreationErrors) {
329474 auto data = CreateTestData (2 );
330475 FeatureMatchingOptions matching_options (FeatureMatcherType::ALIKED_LIGHTGLUE );
@@ -629,6 +774,33 @@ TEST(GeometricVerifierController, VerifyClearsDegenerateGeometry) {
629774 100 );
630775}
631776
777+ TEST (FeatureMatcherController, MatchReplacesRawMatchesAfterSuccess) {
778+ auto pair_data = CreateLowRatioPairData ();
779+ auto & data = pair_data.data ;
780+ const image_t id1 = pair_data.image_id1 ;
781+ const image_t id2 = pair_data.image_id2 ;
782+ ASSERT_EQ (data.database ->ReadMatches (id1, id2).size (), 100 );
783+
784+ TwoViewGeometryOptions geometry_options;
785+ geometry_options.min_num_inliers = 200 ;
786+ FeatureMatcherController controller (
787+ DefaultMatchingOptions (), geometry_options, data.cache );
788+ ASSERT_TRUE (controller.Setup ());
789+ controller.Match ({{id1, id2}});
790+
791+ ASSERT_TRUE (data.database ->ExistsMatches (id1, id2));
792+ EXPECT_TRUE (data.database ->ReadMatches (id1, id2).empty ());
793+ ASSERT_TRUE (data.database ->ExistsTwoViewGeometry (id1, id2));
794+ const auto geometry = data.database ->ReadTwoViewGeometry (id1, id2);
795+ EXPECT_EQ (geometry.config , TwoViewGeometry::DEGENERATE );
796+ EXPECT_TRUE (geometry.inlier_matches .empty ());
797+ EXPECT_FALSE (geometry.F .has_value ());
798+ EXPECT_FALSE (geometry.E .has_value ());
799+ EXPECT_FALSE (geometry.H .has_value ());
800+ controller.Match ({{id1, id2}});
801+ EXPECT_TRUE (data.database ->ReadMatches (id1, id2).empty ());
802+ }
803+
632804TEST (FeatureMatcherController, MatchClearsDegenerateGeometry) {
633805 auto pair_data = CreateLowRatioPairData ();
634806 auto & data = pair_data.data ;
0 commit comments