Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding serialization support to be used for GT-SFM #650

Merged
merged 10 commits into from
Jan 4, 2021
6 changes: 6 additions & 0 deletions gtsam/gtsam.i
Original file line number Diff line number Diff line change
Expand Up @@ -2768,6 +2768,9 @@ class SfmTrack {
pair<size_t, gtsam::Point2> measurement(size_t idx) const;
pair<size_t, size_t> siftIndex(size_t idx) const;
void add_measurement(size_t idx, const gtsam::Point2& m);

// enabling serialization functionality
void serialize() const;
};

class SfmData {
Expand All @@ -2778,6 +2781,9 @@ class SfmData {
gtsam::SfmTrack track(size_t idx) const;
void add_track(const gtsam::SfmTrack& t) ;
void add_camera(const gtsam::SfmCamera& cam);

// enabling serialization functionality
void serialize() const;
};

gtsam::SfmData readBal(string filename);
Expand Down
124 changes: 121 additions & 3 deletions gtsam/slam/dataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
#include <gtsam/nonlinear/NonlinearFactorGraph.h>
#include <gtsam/nonlinear/Values.h>
#include <gtsam/linear/NoiseModel.h>
#include <gtsam/base/Testable.h>
#include <gtsam/base/types.h>


#include <boost/serialization/vector.hpp>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is used to serialize vectors

#include <boost/smart_ptr/shared_ptr.hpp>
#include <string>
#include <utility> // for pair
Expand Down Expand Up @@ -222,7 +225,7 @@ struct SfmTrack {
float r, g, b; ///< RGB color of the 3D point
std::vector<SfmMeasurement> measurements; ///< The 2D image projections (id,(u,v))
std::vector<SiftIndex> siftIndices;

/// Total number of measurements in this track
size_t number_measurements() const {
return measurements.size();
Expand All @@ -243,6 +246,72 @@ struct SfmTrack {
void add_measurement(size_t idx, const gtsam::Point2& m) {
measurements.emplace_back(idx, m);
}

/** Serialization function */
friend class boost::serialization::access;
template<class ARCHIVE>
void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
ar & p;
ar & r;
ar & g;
ar & b;
ar & measurements;
ar & siftIndices;
}

/// assert equality up to a tolerance
bool equals(const SfmTrack &sfmTrack, double tol = 1e-9) const {
// check the 3D point
if (!p.isApprox(sfmTrack.p)) {
return false;
}

// check the RGB values
if (r!=sfmTrack.r || g!=sfmTrack.g || b!=sfmTrack.b) {
return false;
}

// compare size of vectors for measurements and siftIndices
if (number_measurements() != sfmTrack.number_measurements() ||
siftIndices.size() != sfmTrack.siftIndices.size()) {
return false;
}

// compare measurements (order sensitive)
for (size_t idx = 0; idx < number_measurements(); ++idx) {
SfmMeasurement measurement = measurements[idx];
SfmMeasurement otherMeasurement = sfmTrack.measurements[idx];

if (measurement.first != otherMeasurement.first ||
!measurement.second.isApprox(otherMeasurement.second)) {
return false;
}
}

// compare sift indices (order sensitive)
for (size_t idx = 0; idx < siftIndices.size(); ++idx) {
SiftIndex index = siftIndices[idx];
SiftIndex otherIndex = sfmTrack.siftIndices[idx];

if (index.first != otherIndex.first ||
index.second != otherIndex.second) {
return false;
}
}

return true;
}

/// print
void print(const std::string& s = "") const {
cout << "Track with " << measurements.size() << "measurements\n";
ayushbaid marked this conversation as resolved.
Show resolved Hide resolved
}
};

/* ************************************************************************* */
/// traits
johnwlambert marked this conversation as resolved.
Show resolved Hide resolved
template<>
struct traits<SfmTrack> : public Testable<SfmTrack> {
};


Expand All @@ -269,13 +338,62 @@ struct SfmData {
return tracks[idx];
}
/// Add a track to SfmData
void add_track(const SfmTrack& t) {
void add_track(const SfmTrack& t) {
tracks.push_back(t);
}
/// Add a camera to SfmData
void add_camera(const SfmCamera& cam){
void add_camera(const SfmCamera& cam) {
cameras.push_back(cam);
}

/** Serialization function */
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int /*version*/) {
ar & cameras;
ar & tracks;
}

/// @}
/// @name Testable
/// @{

/// assert equality up to a tolerance
bool equals(const SfmData &sfmData, double tol = 1e-9) const {
// check number of cameras and tracks
if (number_cameras() != sfmData.number_cameras() ||
number_tracks() != sfmData.number_tracks()) {
return false;
}

// check each camera
for (size_t i = 0; i < number_cameras(); ++i) {
if (!camera(i).equals(sfmData.camera(i), tol)) {
return false;
}
}

// check each track
for (size_t j = 0; j < number_tracks(); ++j) {
if (!track(j).equals(sfmData.track(j), tol)) {
return false;
}
}

return true;
}

/// print
void print(const std::string& s = "") const {
cout << "Number of cameras = " << number_cameras() << "\n";
cout << "Number of tracks = " << number_tracks() << "\n";
}
};

/* ************************************************************************* */
/// traits
template<>
struct traits<SfmData> : public Testable<SfmData> {
};

/**
Expand Down
56 changes: 56 additions & 0 deletions gtsam/slam/tests/testSerializationDataset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* ----------------------------------------------------------------------------

* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)

* See LICENSE for the license information

* -------------------------------------------------------------------------- */

/**
* @file testSerializationDataset.cpp
* @brief serialization tests for dataset.cpp
* @author Ayush Baid
* @date Jan 1, 2021
*/

#include <gtsam/slam/dataset.h>

#include <gtsam/base/serializationTestHelpers.h>
#include <CppUnitLite/TestHarness.h>

using namespace std;
using namespace gtsam;
using namespace gtsam::serializationTestHelpers;

/* ************************************************************************* */
TEST(dataSet, sfmDataSerialization){
// Test the serialization of SfmData
const string filename = findExampleDataFile("dubrovnik-3-7-pre");
SfmData mydata;
CHECK(readBAL(filename, mydata));

EXPECT(equalsObj(mydata));
johnwlambert marked this conversation as resolved.
Show resolved Hide resolved
// EXPECT(equalsXML(mydata));
ayushbaid marked this conversation as resolved.
Show resolved Hide resolved
// EXPECT(equalsBinary(mydata));
}

/* ************************************************************************* */
TEST(dataSet, sfmTrackSerialization){
// Test the serialization of SfmTrack
const string filename = findExampleDataFile("dubrovnik-3-7-pre");
SfmData mydata;
CHECK(readBAL(filename, mydata));

SfmTrack track = mydata.track(0);

EXPECT(equalsObj(track));
ayushbaid marked this conversation as resolved.
Show resolved Hide resolved
// EXPECT(equalsXML(track));
ayushbaid marked this conversation as resolved.
Show resolved Hide resolved
// EXPECT(equalsBinary(track));
}

/* ************************************************************************* */
int main() { TestResult tr; return TestRegistry::runAllTests(tr); }
/* ************************************************************************* */