Skip to content

Commit f8575f7

Browse files
authored
Merge pull request #2254 from jaffarrell/BugFix_ImuFactorsExample
Bug fix imu factors example
2 parents 0fe6e0f + 4256803 commit f8575f7

File tree

7 files changed

+62
-49
lines changed

7 files changed

+62
-49
lines changed

examples/ImuFactorsExample.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,20 @@ int main(int argc, char* argv[]) {
212212
// exactly the same, so keeping this for simplicity.
213213

214214
// All priors have been set up, now iterate through the data file.
215-
while (file.good()) {
216-
// Parse out first value
217-
getline(file, value, ',');
218-
int type = stoi(value.c_str());
215+
std::string line;
216+
int type{1000};
217+
while (std::getline(file, line)) {
218+
std::stringstream ss(line);
219+
std::string value;
220+
221+
// Read value until comma. Skip to next line on failure to read.
222+
if (!std::getline(ss, value, ',')) continue;
223+
try {
224+
type = std::stoi(value);
225+
} catch (const std::invalid_argument& e) {
226+
std::cerr << "Invalid integer in input: \"" << value << "\"\n";
227+
continue; // Or break, depending on desired behavior
228+
}
219229

220230
if (type == 0) { // IMU measurement
221231
Vector6 imu;

gtsam/basis/tests/testFourier.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222
#include <gtsam/basis/Fourier.h>
2323
#include <gtsam/nonlinear/factorTesting.h>
2424

25-
#if defined(__GNUC__) && !defined(__clang__)
25+
#if defined(__GNUC__) && !defined(__clang__) && (__GNUC__ >= 11)
2626
#pragma GCC diagnostic warning "-Wstringop-overread"
27+
#endif
28+
29+
#if defined(__GNUC__) && !defined(__clang__)
2730
#pragma GCC diagnostic warning "-Warray-bounds"
2831
#endif
32+
2933
using namespace std;
3034
using namespace gtsam;
3135

gtsam/discrete/tests/testDecisionTree.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929

3030
#include <iomanip>
3131

32-
using std::vector;
33-
using std::string;
3432
using std::map;
33+
using std::string;
34+
using std::vector;
3535
using namespace gtsam;
3636

3737
template <typename T>
@@ -54,7 +54,8 @@ struct CrazyDecisionTree : public DecisionTree<string, Crazy> {
5454
auto keyFormatter = [](const std::string& s) { return s; };
5555
auto valueFormatter = [](const Crazy& v) {
5656
std::stringstream ss;
57-
ss << "{" << v.a << "," << std::setw(4) << std::setprecision(2) << v.b << "}";
57+
ss << "{" << v.a << "," << std::setw(4) << std::setprecision(2) << v.b
58+
<< "}";
5859
return ss.str();
5960
};
6061
DecisionTree<string, Crazy>::print("", keyFormatter, valueFormatter);
@@ -103,9 +104,7 @@ struct DT : public DecisionTree<string, int> {
103104
/// print to stdout
104105
void print(const std::string& s = "") const {
105106
auto keyFormatter = [](const std::string& s) { return s; };
106-
auto valueFormatter = [](const int& v) {
107-
return std::to_string(v);
108-
};
107+
auto valueFormatter = [](const int& v) { return std::to_string(v); };
109108
std::cout << s;
110109
Base::print("", keyFormatter, valueFormatter);
111110
}
@@ -132,10 +131,10 @@ TEST(DecisionTree, ConstructorOrder) {
132131
string A("A"), B("B");
133132

134133
const std::vector<int> ys1 = {1, 2, 3, 4};
135-
DT tree1({{B, 2}, {A, 2}}, ys1); // faster version, as B is "higher" than A!
134+
DT tree1({{B, 2}, {A, 2}}, ys1); // faster version, as B is "higher" than A!
136135

137136
const std::vector<int> ys2 = {1, 3, 2, 4};
138-
DT tree2({{A, 2}, {B, 2}}, ys2); // slower version !
137+
DT tree2({{A, 2}, {B, 2}}, ys2); // slower version !
139138

140139
// Both trees will be the same, tree is order from high to low labels.
141140
// Choice(B)
@@ -266,7 +265,8 @@ TEST(DecisionTree, Example) {
266265
}
267266

268267
/* ************************************************************************** */
269-
// Test that we can create two trees out of one, using a function that returns a pair.
268+
// Test that we can create two trees out of one, using a function that returns a
269+
// pair.
270270
TEST(DecisionTree, Split) {
271271
// Create labels
272272
string A("A"), B("B");
@@ -276,11 +276,11 @@ TEST(DecisionTree, Split) {
276276

277277
// Define a function that returns an int/bool pair
278278
auto split_function = [](const int& value) -> std::pair<int, bool> {
279-
return {value*3, value*3 % 2 == 0};
279+
return {value * 3, value * 3 % 2 == 0};
280280
};
281281

282282
// Split the original tree into two new trees
283-
auto [la,lb] = original.split<int,bool>(split_function);
283+
auto [la, lb] = original.split<int, bool>(split_function);
284284

285285
// Check the first resulting tree
286286
EXPECT_LONGS_EQUAL(3, la(Assignment<string>{{A, 0}, {B, 0}}));
@@ -295,7 +295,6 @@ TEST(DecisionTree, Split) {
295295
EXPECT(lb(Assignment<string>{{A, 1}, {B, 1}}));
296296
}
297297

298-
299298
/* ************************************************************************** */
300299
// Test that we can create a tree by modifying an rvalue.
301300
TEST(DecisionTree, Consume) {
@@ -305,7 +304,7 @@ TEST(DecisionTree, Consume) {
305304
// Create a decision tree
306305
DT original(A, DT(B, 1, 2), DT(B, 3, 4));
307306

308-
DT modified([](int i){return i*2;}, std::move(original));
307+
DT modified([](int i) { return i * 2; }, std::move(original));
309308

310309
// Check the first resulting tree
311310
EXPECT_LONGS_EQUAL(2, modified(Assignment<string>{{A, 0}, {B, 0}}));
@@ -319,7 +318,7 @@ TEST(DecisionTree, Consume) {
319318

320319
/* ************************************************************************** */
321320
// test Conversion of values
322-
bool bool_of_int(const int& y) { return y != 0; };
321+
bool bool_of_int(const int& y) { return y != 0; }
323322
typedef DecisionTree<string, bool> StringBoolTree;
324323

325324
TEST(DecisionTree, ConvertValuesOnly) {
@@ -333,7 +332,7 @@ TEST(DecisionTree, ConvertValuesOnly) {
333332
StringBoolTree f2(f1, bool_of_int);
334333

335334
// Check a value
336-
Assignment<string> x00 {{A, 0}, {B, 0}};
335+
Assignment<string> x00{{A, 0}, {B, 0}};
337336
EXPECT(!f2(x00));
338337
}
339338

gtsam/discrete/tests/testSerializationDiscrete.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ BOOST_CLASS_EXPORT_GUID(Tree, "gtsam_DecisionTreeStringInt")
3232
BOOST_CLASS_EXPORT_GUID(Tree::Leaf, "gtsam_DecisionTreeStringInt_Leaf")
3333
BOOST_CLASS_EXPORT_GUID(Tree::Choice, "gtsam_DecisionTreeStringInt_Choice")
3434

35-
BOOST_CLASS_EXPORT_GUID(DecisionTreeFactor, "gtsam_DecisionTreeFactor");
36-
BOOST_CLASS_EXPORT_GUID(TableFactor, "gtsam_TableFactor");
35+
BOOST_CLASS_EXPORT_GUID(DecisionTreeFactor, "gtsam_DecisionTreeFactor")
36+
BOOST_CLASS_EXPORT_GUID(TableFactor, "gtsam_TableFactor")
3737

3838
using ADT = AlgebraicDecisionTree<Key>;
39-
BOOST_CLASS_EXPORT_GUID(ADT, "gtsam_AlgebraicDecisionTree");
39+
BOOST_CLASS_EXPORT_GUID(ADT, "gtsam_AlgebraicDecisionTree")
4040
BOOST_CLASS_EXPORT_GUID(ADT::Leaf, "gtsam_AlgebraicDecisionTree_Leaf")
4141
BOOST_CLASS_EXPORT_GUID(ADT::Choice, "gtsam_AlgebraicDecisionTree_Choice")
4242

gtsam/hybrid/tests/testGaussianMixture.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ DiscreteConditional::shared_ptr mixing =
4747
/// Gaussian density function
4848
double Gaussian(double mu, double sigma, double z) {
4949
return exp(-0.5 * pow((z - mu) / sigma, 2)) / sqrt(2 * M_PI * sigma * sigma);
50-
};
50+
}
5151

5252
/**
5353
* Closed form computation of P(m=1|z).
@@ -59,7 +59,7 @@ double prob_m_z(double mu0, double mu1, double sigma0, double sigma1,
5959
const double p0 = 0.6 * Gaussian(mu0, sigma0, z);
6060
const double p1 = 0.4 * Gaussian(mu1, sigma1, z);
6161
return p1 / (p0 + p1);
62-
};
62+
}
6363

6464
/*
6565
* Test a Gaussian Mixture Model P(m)p(z|m) with same sigma.

gtsam/hybrid/tests/testSerializationHybrid.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,44 +40,44 @@ using symbol_shorthand::Z;
4040

4141
using namespace serializationTestHelpers;
4242

43-
BOOST_CLASS_EXPORT_GUID(Factor, "gtsam_Factor");
44-
BOOST_CLASS_EXPORT_GUID(HybridFactor, "gtsam_HybridFactor");
45-
BOOST_CLASS_EXPORT_GUID(JacobianFactor, "gtsam_JacobianFactor");
46-
BOOST_CLASS_EXPORT_GUID(GaussianConditional, "gtsam_GaussianConditional");
47-
BOOST_CLASS_EXPORT_GUID(DiscreteConditional, "gtsam_DiscreteConditional");
48-
BOOST_CLASS_EXPORT_GUID(TableDistribution, "gtsam_TableDistribution");
49-
50-
BOOST_CLASS_EXPORT_GUID(DecisionTreeFactor, "gtsam_DecisionTreeFactor");
43+
BOOST_CLASS_EXPORT_GUID(Factor, "gtsam_Factor")
44+
BOOST_CLASS_EXPORT_GUID(HybridFactor, "gtsam_HybridFactor")
45+
BOOST_CLASS_EXPORT_GUID(JacobianFactor, "gtsam_JacobianFactor")
46+
BOOST_CLASS_EXPORT_GUID(GaussianConditional, "gtsam_GaussianConditional")
47+
BOOST_CLASS_EXPORT_GUID(DiscreteConditional, "gtsam_DiscreteConditional")
48+
BOOST_CLASS_EXPORT_GUID(TableDistribution, "gtsam_TableDistribution")
49+
50+
BOOST_CLASS_EXPORT_GUID(DecisionTreeFactor, "gtsam_DecisionTreeFactor")
5151
using ADT = AlgebraicDecisionTree<Key>;
52-
BOOST_CLASS_EXPORT_GUID(ADT, "gtsam_AlgebraicDecisionTree");
53-
BOOST_CLASS_EXPORT_GUID(ADT::Leaf, "gtsam_AlgebraicDecisionTree_Leaf");
52+
BOOST_CLASS_EXPORT_GUID(ADT, "gtsam_AlgebraicDecisionTree")
53+
BOOST_CLASS_EXPORT_GUID(ADT::Leaf, "gtsam_AlgebraicDecisionTree_Leaf")
5454
BOOST_CLASS_EXPORT_GUID(ADT::Choice, "gtsam_AlgebraicDecisionTree_Choice")
5555

56-
BOOST_CLASS_EXPORT_GUID(HybridGaussianFactor, "gtsam_HybridGaussianFactor");
56+
BOOST_CLASS_EXPORT_GUID(HybridGaussianFactor, "gtsam_HybridGaussianFactor")
5757
BOOST_CLASS_EXPORT_GUID(HybridGaussianFactor::FactorValuePairs,
58-
"gtsam_HybridGaussianFactor_Factors");
58+
"gtsam_HybridGaussianFactor_Factors")
5959
BOOST_CLASS_EXPORT_GUID(HybridGaussianFactor::FactorValuePairs::Leaf,
60-
"gtsam_HybridGaussianFactor_Factors_Leaf");
60+
"gtsam_HybridGaussianFactor_Factors_Leaf")
6161
BOOST_CLASS_EXPORT_GUID(HybridGaussianFactor::FactorValuePairs::Choice,
62-
"gtsam_HybridGaussianFactor_Factors_Choice");
62+
"gtsam_HybridGaussianFactor_Factors_Choice")
6363

6464
BOOST_CLASS_EXPORT_GUID(GaussianFactorGraphValuePair,
65-
"gtsam_GaussianFactorGraphValuePair");
65+
"gtsam_GaussianFactorGraphValuePair")
6666
BOOST_CLASS_EXPORT_GUID(HybridGaussianProductFactor,
67-
"gtsam_HybridGaussianProductFactor");
67+
"gtsam_HybridGaussianProductFactor")
6868

6969
BOOST_CLASS_EXPORT_GUID(HybridGaussianConditional,
70-
"gtsam_HybridGaussianConditional");
70+
"gtsam_HybridGaussianConditional")
7171
BOOST_CLASS_EXPORT_GUID(HybridGaussianConditional::Conditionals,
72-
"gtsam_HybridGaussianConditional_Conditionals");
72+
"gtsam_HybridGaussianConditional_Conditionals")
7373
BOOST_CLASS_EXPORT_GUID(HybridGaussianConditional::Conditionals::Leaf,
74-
"gtsam_HybridGaussianConditional_Conditionals_Leaf");
74+
"gtsam_HybridGaussianConditional_Conditionals_Leaf")
7575
BOOST_CLASS_EXPORT_GUID(HybridGaussianConditional::Conditionals::Choice,
76-
"gtsam_HybridGaussianConditional_Conditionals_Choice");
76+
"gtsam_HybridGaussianConditional_Conditionals_Choice")
7777
// Needed since GaussianConditional::FromMeanAndStddev uses it
78-
BOOST_CLASS_EXPORT_GUID(noiseModel::Isotropic, "gtsam_noiseModel_Isotropic");
78+
BOOST_CLASS_EXPORT_GUID(noiseModel::Isotropic, "gtsam_noiseModel_Isotropic")
7979

80-
BOOST_CLASS_EXPORT_GUID(HybridBayesNet, "gtsam_HybridBayesNet");
80+
BOOST_CLASS_EXPORT_GUID(HybridBayesNet, "gtsam_HybridBayesNet")
8181

8282
/* ****************************************************************************/
8383
// Test HybridGaussianFactor serialization.

gtsam/sam/tests/testSerializationSam.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ constexpr double rangeMmeasurement(10.0);
3939
/* ************************************************************************* */
4040
// Export Noisemodels
4141
// See http://www.boost.org/doc/libs/1_32_0/libs/serialization/doc/special.html
42-
BOOST_CLASS_EXPORT(gtsam::noiseModel::Isotropic);
43-
BOOST_CLASS_EXPORT(gtsam::noiseModel::Unit);
42+
BOOST_CLASS_EXPORT(gtsam::noiseModel::Isotropic)
43+
BOOST_CLASS_EXPORT(gtsam::noiseModel::Unit)
4444

4545
/* ************************************************************************* */
4646
TEST(SerializationSam, BearingFactor2D) {

0 commit comments

Comments
 (0)