Skip to content

Commit ece53ee

Browse files
committed
d
1 parent eeac505 commit ece53ee

File tree

7 files changed

+41
-54
lines changed

7 files changed

+41
-54
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/build
22
/data
33
/.vscode
4+
/compile_commands.json

glomap/controllers/rotation_averager.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ bool SolveRotationAveraging(ViewGraph& view_graph,
4444

4545
// If there is no image pairs with gravity or most image pairs are with
4646
// gravity, then just run the 3dof version
47-
bool status = (grav_pairs == 0) || (grav_pairs > total_pairs * 0.95);
47+
const bool status = (grav_pairs == 0) || (grav_pairs > total_pairs * 0.95);
4848
solve_1dof_system = solve_1dof_system && (!status);
4949

5050
if (solve_1dof_system) {

glomap/controllers/rotation_averager_test.cc

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "glomap/types.h"
88

99
#include <colmap/estimators/alignment.h>
10+
#include <colmap/math/random.h>
1011
#include <colmap/scene/synthetic.h>
1112
#include <colmap/util/testing.h>
1213

@@ -20,8 +21,8 @@ void CreateRandomRotation(const double stddev, Eigen::Quaterniond& q) {
2021
std::mt19937 gen{rd()};
2122

2223
// Construct a random axis
23-
double theta = double(rand()) / RAND_MAX * 2 * M_PI;
24-
double phi = double(rand()) / RAND_MAX * M_PI;
24+
double theta = colmap::RandomUniformReal<double>(0, 2 * M_PI);
25+
double phi = colmap::RandomUniformReal<double>(0, M_PI);
2526
Eigen::Vector3d axis(std::cos(theta) * std::sin(phi),
2627
std::sin(theta) * std::sin(phi),
2728
std::cos(phi));
@@ -34,25 +35,28 @@ void CreateRandomRotation(const double stddev, Eigen::Quaterniond& q) {
3435

3536
void PrepareGravity(const colmap::Reconstruction& gt,
3637
std::unordered_map<frame_t, Frame>& frames,
37-
double stddev_gravity = 0.0,
38+
double gravity_noise_stddev = 0.0,
3839
double outlier_ratio = 0.0) {
40+
const Eigen::Vector3d kGravityInWorld = Eigen::Vector3d(0, 1, 0);
3941
for (auto& frame_id : gt.RegFrameIds()) {
40-
Eigen::Vector3d gravity =
41-
gt.Frame(frame_id).RigFromWorld().rotation * Eigen::Vector3d(0, 1, 0);
42+
Eigen::Vector3d gravityInRig =
43+
gt.Frame(frame_id).RigFromWorld().rotation * kGravityInWorld;
4244

43-
if (stddev_gravity > 0.0) {
44-
Eigen::Quaterniond q;
45-
CreateRandomRotation(DegToRad(stddev_gravity), q);
46-
gravity = q * gravity;
45+
if (gravity_noise_stddev > 0.0) {
46+
Eigen::Quaterniond noise;
47+
CreateRandomRotation(DegToRad(gravity_noise_stddev), noise);
48+
gravityInRig = noise * gravityInRig;
4749
}
4850

49-
if (outlier_ratio > 0.0 && double(rand()) / RAND_MAX < outlier_ratio) {
51+
if (outlier_ratio > 0.0 &&
52+
colmap::RandomUniformReal<double>(0, 1) < outlier_ratio) {
5053
Eigen::Quaterniond q;
5154
CreateRandomRotation(1., q);
52-
gravity =
55+
gravityInRig =
5356
Rigid3dToAngleAxis(Rigid3d(q, Eigen::Vector3d::Zero())).normalized();
5457
}
55-
frames[frame_id].gravity_info.SetGravity(gravity);
58+
59+
frames[frame_id].gravity_info.SetGravity(gravityInRig);
5660
Rigid3d& cam_from_world = frames[frame_id].RigFromWorld();
5761
cam_from_world.rotation = frames[frame_id].gravity_info.GetRAlign();
5862
}
@@ -67,7 +71,6 @@ GlobalMapperOptions CreateMapperTestOptions() {
6771
options.skip_global_positioning = true;
6872
options.skip_bundle_adjustment = true;
6973
options.skip_retriangulation = true;
70-
7174
return options;
7275
}
7376

@@ -82,23 +85,21 @@ RotationAveragerOptions CreateRATestOptions(bool use_gravity = false) {
8285
void ExpectEqualRotations(const colmap::Reconstruction& gt,
8386
const colmap::Reconstruction& computed,
8487
const double max_rotation_error_deg) {
85-
// const std::set<image_t> reg_image_ids_set = gt.RegImageIds();
86-
std::vector<image_t> reg_image_ids = gt.RegImageIds();
88+
const std::vector<image_t> reg_image_ids = gt.RegImageIds();
8789
for (size_t i = 0; i < reg_image_ids.size(); i++) {
8890
const image_t image_id1 = reg_image_ids[i];
89-
for (size_t j = 0; j < reg_image_ids.size(); j++) {
90-
if (i == j) continue;
91+
for (size_t j = 0; j < i; j++) {
9192
const image_t image_id2 = reg_image_ids[j];
9293

9394
const Rigid3d cam2_from_cam1 =
9495
computed.Image(image_id2).CamFromWorld() *
95-
colmap::Inverse(computed.Image(image_id1).CamFromWorld());
96-
96+
Inverse(computed.Image(image_id1).CamFromWorld());
9797
const Rigid3d cam2_from_cam1_gt =
9898
gt.Image(image_id2).CamFromWorld() *
99-
colmap::Inverse(gt.Image(image_id1).CamFromWorld());
99+
Inverse(gt.Image(image_id1).CamFromWorld());
100100

101-
double rotation_error_deg = CalcAngle(cam2_from_cam1_gt, cam2_from_cam1);
101+
const double rotation_error_deg =
102+
CalcAngle(cam2_from_cam1_gt, cam2_from_cam1);
102103
EXPECT_LT(rotation_error_deg, max_rotation_error_deg);
103104
}
104105
}
@@ -123,6 +124,8 @@ void ExpectEqualGravity(
123124
}
124125

125126
TEST(RotationEstimator, WithoutNoise) {
127+
colmap::SetPRNGSeed(2);
128+
126129
const std::string database_path = colmap::CreateTestDir() + "/database.db";
127130

128131
auto database = colmap::Database::Open(database_path);
@@ -152,8 +155,7 @@ TEST(RotationEstimator, WithoutNoise) {
152155
global_mapper.Solve(
153156
*database, view_graph, rigs, cameras, frames, images, tracks);
154157

155-
// Version with Gravity
156-
for (bool use_gravity : {true}) {
158+
for (const bool use_gravity : {true, false}) {
157159
SolveRotationAveraging(
158160
view_graph, rigs, frames, images, CreateRATestOptions(use_gravity));
159161

@@ -195,8 +197,7 @@ TEST(RotationEstimator, WithoutNoiseWithNoneTrivialKnownRig) {
195197
global_mapper.Solve(
196198
*database, view_graph, rigs, cameras, frames, images, tracks);
197199

198-
// Version with Gravity
199-
for (bool use_gravity : {true, false}) {
200+
for (const bool use_gravity : {true, false}) {
200201
SolveRotationAveraging(
201202
view_graph, rigs, frames, images, CreateRATestOptions(use_gravity));
202203

@@ -282,7 +283,7 @@ TEST(RotationEstimator, WithNoiseAndOutliers) {
282283

283284
ConvertDatabaseToGlomap(*database, view_graph, rigs, cameras, frames, images);
284285

285-
PrepareGravity(gt_reconstruction, frames, /*stddev_gravity=*/3e-1);
286+
PrepareGravity(gt_reconstruction, frames, /*gravity_noise_stddev=*/3e-1);
286287

287288
GlobalMapper global_mapper(CreateMapperTestOptions());
288289
global_mapper.Solve(
@@ -327,7 +328,7 @@ TEST(RotationEstimator, WithNoiseAndOutliersWithNonTrivialKnownRigs) {
327328
std::unordered_map<track_t, Track> tracks;
328329

329330
ConvertDatabaseToGlomap(*database, view_graph, rigs, cameras, frames, images);
330-
PrepareGravity(gt_reconstruction, frames, /*stddev_gravity=*/3e-1);
331+
PrepareGravity(gt_reconstruction, frames, /*gravity_noise_stddev=*/3e-1);
331332

332333
GlobalMapper global_mapper(CreateMapperTestOptions());
333334
global_mapper.Solve(
@@ -374,7 +375,7 @@ TEST(RotationEstimator, RefineGravity) {
374375

375376
PrepareGravity(gt_reconstruction,
376377
frames,
377-
/*stddev_gravity=*/0.,
378+
/*gravity_noise_stddev=*/0.,
378379
/*outlier_ratio=*/0.3);
379380

380381
GlobalMapper global_mapper(CreateMapperTestOptions());
@@ -416,7 +417,7 @@ TEST(RotationEstimator, RefineGravityWithNontrivialRigs) {
416417

417418
PrepareGravity(gt_reconstruction,
418419
frames,
419-
/*stddev_gravity=*/0.,
420+
/*gravity_noise_stddev=*/0.,
420421
/*outlier_ratio=*/0.3);
421422

422423
GlobalMapper global_mapper(CreateMapperTestOptions());

glomap/estimators/global_rotation_averaging.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace glomap {
1414
namespace {
15+
1516
double RelAngleError(double angle_12, double angle_1, double angle_2) {
1617
double est = (angle_2 - angle_1) - angle_12;
1718

@@ -30,6 +31,7 @@ double RelAngleError(double angle_12, double angle_1, double angle_2) {
3031

3132
return est;
3233
}
34+
3335
} // namespace
3436

3537
bool RotationEstimator::EstimateRotations(

glomap/math/gravity.cc

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,8 @@ namespace glomap {
99

1010
// The second col of R_align is gravity direction
1111
Eigen::Matrix3d GetAlignRot(const Eigen::Vector3d& gravity) {
12-
Eigen::Matrix3d R;
13-
Eigen::Vector3d v = gravity.normalized();
14-
R.col(1) = v;
15-
16-
Eigen::Matrix3d Q = v.householderQr().householderQ();
17-
Eigen::Matrix<double, 3, 2> N = Q.rightCols(2);
18-
R.col(0) = N.col(0);
19-
R.col(2) = N.col(1);
20-
if (R.determinant() < 0) {
21-
R.col(2) = -R.col(2);
22-
}
23-
return R;
12+
return Eigen::Quaterniond::FromTwoVectors(Eigen::Vector3d(0, 1, 0), gravity)
13+
.toRotationMatrix();
2414
}
2515

2616
double RotUpToAngle(const Eigen::Matrix3d& R_up) {
@@ -97,4 +87,4 @@ double CalcAngle(const Eigen::Vector3d& gravity1,
9787

9888
return std::acos(cos_r) * 180 / EIGEN_PI;
9989
}
100-
} // namespace glomap
90+
} // namespace glomap

glomap/math/rigid3d.cc

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@
55
namespace glomap {
66

77
double CalcAngle(const Rigid3d& pose1, const Rigid3d& pose2) {
8-
double cos_r =
9-
((pose1.rotation.inverse() * pose2.rotation).toRotationMatrix().trace() -
10-
1) /
11-
2;
12-
cos_r = std::min(std::max(cos_r, -1.), 1.);
13-
14-
return std::acos(cos_r) * 180 / EIGEN_PI;
8+
return pose1.rotation.angularDistance(pose2.rotation) * 180 / EIGEN_PI;
159
}
1610

1711
double CalcTrans(const Rigid3d& pose1, const Rigid3d& pose2) {
@@ -22,15 +16,13 @@ double CalcTransAngle(const Rigid3d& pose1, const Rigid3d& pose2) {
2216
double cos_r = (pose1.translation).dot(pose2.translation) /
2317
(pose1.translation.norm() * pose2.translation.norm());
2418
cos_r = std::min(std::max(cos_r, -1.), 1.);
25-
2619
return std::acos(cos_r) * 180 / EIGEN_PI;
2720
}
2821

2922
double CalcAngle(const Eigen::Matrix3d& rotation1,
3023
const Eigen::Matrix3d& rotation2) {
3124
double cos_r = ((rotation1.transpose() * rotation2).trace() - 1) / 2;
3225
cos_r = std::min(std::max(cos_r, -1.), 1.);
33-
3426
return std::acos(cos_r) * 180 / EIGEN_PI;
3527
}
3628

@@ -74,4 +66,4 @@ Eigen::Vector3d CenterFromPose(const Rigid3d& pose) {
7466
return pose.rotation.inverse() * -pose.translation;
7567
}
7668

77-
} // namespace glomap
69+
} // namespace glomap

glomap/scene/frame.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ void GravityInfo::SetGravity(const Eigen::Vector3d& g) {
4848
R_align_ = GetAlignRot(g);
4949
has_gravity = true;
5050
}
51-
} // namespace glomap
51+
52+
} // namespace glomap

0 commit comments

Comments
 (0)