Skip to content

Commit 6457937

Browse files
committed
Fix small issues and store scaled s_
1 parent 0836852 commit 6457937

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

gtsam/geometry/FundamentalMatrix.cpp

+12-18
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
* @file FundamentalMatrix.cpp
33
* @brief FundamentalMatrix classes
44
* @author Frank Dellaert
5-
* @date Oct 23, 2024
5+
* @date October 2024
66
*/
77

8+
#include <gtsam/base/Matrix.h>
89
#include <gtsam/geometry/FundamentalMatrix.h>
10+
#include <gtsam/geometry/Point2.h>
911

1012
namespace gtsam {
1113

@@ -26,8 +28,8 @@ Point2 EpipolarTransfer(const Matrix3& Fca, const Point2& pa, //
2628
}
2729

2830
//*************************************************************************
29-
FundamentalMatrix::FundamentalMatrix(const Matrix& U, double s,
30-
const Matrix& V) {
31+
FundamentalMatrix::FundamentalMatrix(const Matrix3& U, double s,
32+
const Matrix3& V) {
3133
initialize(U, s, V);
3234
}
3335

@@ -57,38 +59,30 @@ FundamentalMatrix::FundamentalMatrix(const Matrix3& F) {
5759

5860
void FundamentalMatrix::initialize(const Matrix3& U, double s,
5961
const Matrix3& V) {
60-
s_ = s;
62+
s_ = s / kScale;
6163
sign_ = 1.0;
6264

63-
// Check if U is a reflection and its determinant is close to -1 or 1
64-
double detU = U.determinant();
65-
if (std::abs(std::abs(detU) - 1.0) > 1e-9) {
66-
throw std::invalid_argument(
67-
"Matrix U does not have a determinant close to -1 or 1.");
68-
}
65+
// Check if U is a reflection and flip U and sign_ if so
66+
double detU = U.determinant(); // detU will be -1 or 1
6967
if (detU < 0) {
7068
U_ = Rot3(-U);
71-
sign_ = -sign_; // Flip sign if U is a reflection
69+
sign_ = -sign_;
7270
} else {
7371
U_ = Rot3(U);
7472
}
7573

76-
// Check if V is a reflection and its determinant is close to -1 or 1
74+
// Same check for V
7775
double detV = V.determinant();
78-
if (std::abs(std::abs(detV) - 1.0) > 1e-9) {
79-
throw std::invalid_argument(
80-
"Matrix V does not have a determinant close to -1 or 1.");
81-
}
8276
if (detV < 0) {
8377
V_ = Rot3(-V);
84-
sign_ = -sign_; // Flip sign if V is a reflection
78+
sign_ = -sign_;
8579
} else {
8680
V_ = Rot3(V);
8781
}
8882
}
8983

9084
Matrix3 FundamentalMatrix::matrix() const {
91-
return sign_ * U_.matrix() * Vector3(1.0, s_, 0).asDiagonal() *
85+
return sign_ * U_.matrix() * Vector3(1.0, s_ * kScale, 0).asDiagonal() *
9286
V_.transpose().matrix();
9387
}
9488

gtsam/geometry/FundamentalMatrix.h

+10-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file FundamentalMatrix.h
33
* @brief FundamentalMatrix classes
44
* @author Frank Dellaert
5-
* @date Oct 23, 2024
5+
* @date October 2024
66
*/
77

88
#pragma once
@@ -34,17 +34,19 @@ class GTSAM_EXPORT FundamentalMatrix {
3434
double s_; ///< Scalar parameter for S
3535
Rot3 V_; ///< Right rotation
3636

37+
static constexpr double kScale = 1000; // s is stored in s_ as s/kScale
38+
3739
public:
3840
/// Default constructor
39-
FundamentalMatrix() : U_(Rot3()), sign_(1.0), s_(1.0), V_(Rot3()) {}
41+
FundamentalMatrix() : U_(Rot3()), sign_(1.0), s_(1.0 / kScale), V_(Rot3()) {}
4042

4143
/**
4244
* @brief Construct from U, V, and scalar s
4345
*
4446
* Initializes the FundamentalMatrix From the SVD representation
4547
* U*diag(1,s,0)*V^T. It will internally convert to using SO(3).
4648
*/
47-
FundamentalMatrix(const Matrix& U, double s, const Matrix& V);
49+
FundamentalMatrix(const Matrix3& U, double s, const Matrix3& V);
4850

4951
/**
5052
* @brief Construct from a 3x3 matrix using SVD
@@ -60,7 +62,9 @@ class GTSAM_EXPORT FundamentalMatrix {
6062
* @brief Construct from essential matrix and calibration matrices
6163
*
6264
* Initializes the FundamentalMatrix from the given essential matrix E
63-
* and calibration matrices Ka and Kb.
65+
* and calibration matrices Ka and Kb, using
66+
* F = Ka^(-T) * E * Kb^(-1)
67+
* and then calls constructor that decomposes F via SVD.
6468
*
6569
* @param E Essential matrix
6670
* @param Ka Calibration matrix for the left camera
@@ -111,8 +115,8 @@ class GTSAM_EXPORT FundamentalMatrix {
111115
/// @}
112116
private:
113117
/// Private constructor for internal use
114-
FundamentalMatrix(const Rot3& U, double sign, double s, const Rot3& V)
115-
: U_(U), sign_(sign), s_(s), V_(V) {}
118+
FundamentalMatrix(const Rot3& U, double sign, double scaled_s, const Rot3& V)
119+
: U_(U), sign_(sign), s_(scaled_s), V_(V) {}
116120

117121
/// Initialize SO(3) matrices from general O(3) matrices
118122
void initialize(const Matrix3& U, double s, const Matrix3& V);

gtsam/geometry/tests/testFundamentalMatrix.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
*/
77

88
#include <CppUnitLite/TestHarness.h>
9+
#include <gtsam/base/Matrix.h>
910
#include <gtsam/base/Testable.h>
1011
#include <gtsam/geometry/FundamentalMatrix.h>
1112
#include <gtsam/geometry/Rot3.h>
1213
#include <gtsam/geometry/SimpleCamera.h>
1314
#include <gtsam/geometry/Unit3.h>
1415

16+
#include <cmath>
17+
1518
using namespace std::placeholders;
1619
using namespace std;
1720
using namespace gtsam;

0 commit comments

Comments
 (0)