| 1 |
import numpy as np |
| 2 |
import pytest |
| 3 |
|
| 4 |
import pycolmap |
| 5 |
|
| 6 |
|
| 7 |
def test_absolute_pose_estimation_options_default_init(): |
| 8 |
options = pycolmap.AbsolutePoseEstimationOptions() |
| 9 |
assert options is not None |
| 10 |
|
| 11 |
|
| 12 |
def test_absolute_pose_estimation_options_estimate_focal_length_readwrite(): |
| 13 |
options = pycolmap.AbsolutePoseEstimationOptions() |
| 14 |
original = options.estimate_focal_length |
| 15 |
assert isinstance(original, bool) |
| 16 |
options.estimate_focal_length = not original |
| 17 |
assert options.estimate_focal_length == (not original) |
| 18 |
|
| 19 |
|
| 20 |
def test_absolute_pose_estimation_options_ransac_property(): |
| 21 |
options = pycolmap.AbsolutePoseEstimationOptions() |
| 22 |
ransac = options.ransac |
| 23 |
assert isinstance(ransac, pycolmap.RANSACOptions) |
| 24 |
|
| 25 |
|
| 26 |
def test_absolute_pose_refinement_options_default_init(): |
| 27 |
options = pycolmap.AbsolutePoseRefinementOptions() |
| 28 |
assert options is not None |
| 29 |
|
| 30 |
|
| 31 |
def test_absolute_pose_refinement_options_gradient_tolerance_readwrite(): |
| 32 |
options = pycolmap.AbsolutePoseRefinementOptions() |
| 33 |
assert isinstance(options.gradient_tolerance, float) |
| 34 |
options.gradient_tolerance = 0.5 |
| 35 |
assert options.gradient_tolerance == 0.5 |
| 36 |
|
| 37 |
|
| 38 |
def test_absolute_pose_refinement_options_max_num_iterations_readwrite(): |
| 39 |
options = pycolmap.AbsolutePoseRefinementOptions() |
| 40 |
assert isinstance(options.max_num_iterations, int) |
| 41 |
options.max_num_iterations = 200 |
| 42 |
assert options.max_num_iterations == 200 |
| 43 |
|
| 44 |
|
| 45 |
def test_absolute_pose_refinement_options_loss_function_scale_readwrite(): |
| 46 |
options = pycolmap.AbsolutePoseRefinementOptions() |
| 47 |
assert isinstance(options.loss_function_scale, float) |
| 48 |
options.loss_function_scale = 2.0 |
| 49 |
assert options.loss_function_scale == 2.0 |
| 50 |
|
| 51 |
|
| 52 |
def test_absolute_pose_refinement_options_refine_focal_length_readwrite(): |
| 53 |
options = pycolmap.AbsolutePoseRefinementOptions() |
| 54 |
original = options.refine_focal_length |
| 55 |
assert isinstance(original, bool) |
| 56 |
options.refine_focal_length = not original |
| 57 |
assert options.refine_focal_length == (not original) |
| 58 |
|
| 59 |
|
| 60 |
def test_absolute_pose_refinement_options_refine_extra_params_readwrite(): |
| 61 |
options = pycolmap.AbsolutePoseRefinementOptions() |
| 62 |
original = options.refine_extra_params |
| 63 |
assert isinstance(original, bool) |
| 64 |
options.refine_extra_params = not original |
| 65 |
assert options.refine_extra_params == (not original) |
| 66 |
|
| 67 |
|
| 68 |
def test_absolute_pose_refinement_options_print_summary_readwrite(): |
| 69 |
options = pycolmap.AbsolutePoseRefinementOptions() |
| 70 |
original = options.print_summary |
| 71 |
assert isinstance(original, bool) |
| 72 |
options.print_summary = not original |
| 73 |
assert options.print_summary == (not original) |
| 74 |
|
| 75 |
|
| 76 |
def test_absolute_pose_refinement_options_use_position_prior_readwrite(): |
| 77 |
options = pycolmap.AbsolutePoseRefinementOptions() |
| 78 |
original = options.use_position_prior |
| 79 |
assert isinstance(original, bool) |
| 80 |
options.use_position_prior = not original |
| 81 |
assert options.use_position_prior == (not original) |
| 82 |
|
| 83 |
|
| 84 |
def test_absolute_pose_refinement_options_position_prior_covariance_readwrite(): |
| 85 |
options = pycolmap.AbsolutePoseRefinementOptions() |
| 86 |
covariance = options.position_prior_covariance |
| 87 |
assert covariance is not None |
| 88 |
new_covariance = np.eye(3) * 2.0 |
| 89 |
options.position_prior_covariance = new_covariance |
| 90 |
result = options.position_prior_covariance |
| 91 |
np.testing.assert_array_almost_equal(result, new_covariance) |
| 92 |
|
| 93 |
|
| 94 |
@pytest.mark.parametrize( |
| 95 |
"name", |
| 96 |
[ |
| 97 |
"estimate_absolute_pose", |
| 98 |
"refine_absolute_pose", |
| 99 |
"estimate_and_refine_absolute_pose", |
| 100 |
"estimate_relative_pose", |
| 101 |
"refine_relative_pose", |
| 102 |
], |
| 103 |
) |
| 104 |
def test_public_api_callable(name): |
| 105 |
assert callable(getattr(pycolmap, name)) |
| 106 |
|
| 107 |
|
| 108 |
def test_relative_pose_ray_overloads(): |
| 109 |
rng = np.random.default_rng(0) |
| 110 |
points3D = rng.uniform(-1.0, 1.0, size=(20, 3)) |
| 111 |
points3D[:, 2] += 4.0 |
| 112 |
translation = np.array([0.5, 0.1, 0.0]) |
| 113 |
cam_rays1 = points3D / np.linalg.norm(points3D, axis=1, keepdims=True) |
| 114 |
points3D_in_cam2 = points3D + translation |
| 115 |
cam_rays2 = points3D_in_cam2 / np.linalg.norm( |
| 116 |
points3D_in_cam2, axis=1, keepdims=True |
| 117 |
) |
| 118 |
|
| 119 |
options = pycolmap.RANSACOptions() |
| 120 |
options.max_error = 1e-3 |
| 121 |
options.random_seed = 0 |
| 122 |
estimation = pycolmap.estimate_relative_pose( |
| 123 |
cam_rays1=cam_rays1, cam_rays2=cam_rays2, options=options |
| 124 |
) |
| 125 |
assert estimation is not None |
| 126 |
assert estimation["num_inliers"] == len(points3D) |
| 127 |
|
| 128 |
refinement = pycolmap.refine_relative_pose( |
| 129 |
cam2_from_cam1=estimation["cam2_from_cam1"], |
| 130 |
cam_rays1=cam_rays1, |
| 131 |
cam_rays2=cam_rays2, |
| 132 |
inlier_mask=estimation["inlier_mask"], |
| 133 |
) |
| 134 |
assert refinement is not None |