Skip to content

Commit d827aee

Browse files
committed
Change NoiseModelFactorN to be an alias to NoiseModelFactorT
1 parent 182fd0d commit d827aee

File tree

5 files changed

+110
-109
lines changed

5 files changed

+110
-109
lines changed

gtsam/inference/BayesTreeCliqueBase-inst.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace gtsam {
4848
KeySet indicesB(B->conditional()->begin(), B->conditional()->end());
4949
KeyVector S_setminus_B;
5050
std::set_difference(p_F_S_parents.begin(), p_F_S_parents.end(),
51-
indicesB.begin(), indicesB.end(), back_inserter(S_setminus_B));
51+
indicesB.begin(), indicesB.end(), std::back_inserter(S_setminus_B));
5252
return S_setminus_B;
5353
}
5454

@@ -64,10 +64,10 @@ namespace gtsam {
6464
KeyVector keep;
6565
// keep = S\B intersect allKeys (S_setminus_B is already sorted)
6666
std::set_intersection(S_setminus_B.begin(), S_setminus_B.end(), //
67-
allKeys.begin(), allKeys.end(), back_inserter(keep));
67+
allKeys.begin(), allKeys.end(), std::back_inserter(keep));
6868
// keep += B intersect allKeys
6969
std::set_intersection(indicesB.begin(), indicesB.end(), //
70-
allKeys.begin(), allKeys.end(), back_inserter(keep));
70+
allKeys.begin(), allKeys.end(), std::back_inserter(keep));
7171
return keep;
7272
}
7373

gtsam/navigation/MagFactor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#pragma once
2020

2121
#include <gtsam/nonlinear/NonlinearFactor.h>
22+
#include <gtsam/nonlinear/NoiseModelFactorN.h>
2223
#include <gtsam/geometry/Rot2.h>
2324
#include <gtsam/geometry/Rot3.h>
2425

gtsam/nonlinear/NoiseModelFactorN.h

Lines changed: 92 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace detail {
3131
/** Convenience base class to add aliases `X1`, `X2`, ..., `X6` -> ValueType<N>.
3232
* Usage example:
3333
* ```
34-
* class MyFactor : public NoiseModelFactorN<Pose3, Point3>,
34+
* class MyFactor : public NoiseModelFactorT<Pose3, Point3>,
3535
* public NoiseModelFactorAliases<Pose3, Point3> {
3636
* // class implementation ...
3737
* };
@@ -94,9 +94,9 @@ struct NoiseModelFactorAliases<T1, T2, T3, T4, T5, T6, TExtra...> {
9494
* between a Pose3 and Point3 could be implemented like so:
9595
*
9696
* ~~~~~~~~~~~~~~~~~~~~{.cpp}
97-
* class MyFactor : public NoiseModelFactorN<Pose3, Point3> {
97+
* class MyFactor : public NoiseModelFactorT<Vector, Pose3, Point3> {
9898
* public:
99-
* using Base = NoiseModelFactorN<Pose3, Point3>;
99+
* using Base = NoiseModelFactorT<Pose3, Point3>;
100100
*
101101
* MyFactor(Key pose_key, Key point_key, const SharedNoiseModel& noiseModel)
102102
* : Base(noiseModel, pose_key, point_key) {}
@@ -121,7 +121,7 @@ struct NoiseModelFactorAliases<T1, T2, T3, T4, T5, T6, TExtra...> {
121121
* return Vector1(error);
122122
* }
123123
* };
124-
*
124+
*
125125
* // Unit Test
126126
* TEST(NonlinearFactor, MyFactor) {
127127
* MyFactor f(X(1), X(2), noiseModel::Unit::Create(1));
@@ -138,8 +138,8 @@ struct NoiseModelFactorAliases<T1, T2, T3, T4, T5, T6, TExtra...> {
138138
* are typically more general than just vectors, e.g., Rot3 or Pose3, which are
139139
* objects in non-linear manifolds (Lie groups).
140140
*/
141-
template <class... ValueTypes>
142-
class NoiseModelFactorN
141+
template <class OutputVec, class... ValueTypes>
142+
class NoiseModelFactorT
143143
: public NoiseModelFactor,
144144
public detail::NoiseModelFactorAliases<ValueTypes...> {
145145
public:
@@ -150,7 +150,7 @@ class NoiseModelFactorN
150150

151151
protected:
152152
using Base = NoiseModelFactor;
153-
using This = NoiseModelFactorN<ValueTypes...>;
153+
using This = NoiseModelFactorT<OutputVec, ValueTypes...>;
154154

155155
/// @name SFINAE aliases
156156
/// @{
@@ -212,88 +212,88 @@ class NoiseModelFactorN
212212
using MatrixTypeT = Matrix;
213213

214214
public:
215-
/**
216-
* The type of the I'th template param can be obtained as ValueType<I>.
217-
* I is 1-indexed for backwards compatibility/consistency! So for example,
218-
* ```
219-
* using Factor = NoiseModelFactorN<Pose3, Point3>;
220-
* Factor::ValueType<1> // Pose3
221-
* Factor::ValueType<2> // Point3
222-
* // Factor::ValueType<0> // ERROR! Will not compile.
223-
* // Factor::ValueType<3> // ERROR! Will not compile.
224-
* ```
225-
*
226-
* You can also use the shortcuts `X1`, ..., `X6` which are the same as
227-
* `ValueType<1>`, ..., `ValueType<6>` respectively (see
228-
* detail::NoiseModelFactorAliases).
229-
*
230-
* Note that, if your class is templated AND you want to use `ValueType<1>`
231-
* inside your class, due to dependent types you need the `template` keyword:
232-
* `typename MyFactor<T>::template ValueType<1>`.
233-
*/
234-
template <int I, typename = IndexIsValid<I>>
235-
using ValueType =
236-
typename std::tuple_element<I - 1, std::tuple<ValueTypes...>>::type;
215+
/**
216+
* The type of the I'th template param can be obtained as ValueType<I>.
217+
* I is 1-indexed for backwards compatibility/consistency! So for example,
218+
* ```
219+
* using Factor = NoiseModelFactorT<Vector, Pose3, Point3>;
220+
* Factor::ValueType<1> // Pose3
221+
* Factor::ValueType<2> // Point3
222+
* // Factor::ValueType<0> // ERROR! Will not compile.
223+
* // Factor::ValueType<3> // ERROR! Will not compile.
224+
* ```
225+
*
226+
* You can also use the shortcuts `X1`, ..., `X6` which are the same as
227+
* `ValueType<1>`, ..., `ValueType<6>` respectively (see
228+
* detail::NoiseModelFactorAliases).
229+
*
230+
* Note that, if your class is templated AND you want to use `ValueType<1>`
231+
* inside your class, due to dependent types you need the `template` keyword:
232+
* `typename MyFactor<T>::template ValueType<1>`.
233+
*/
234+
template <int I, typename = IndexIsValid<I>>
235+
using ValueType =
236+
typename std::tuple_element<I - 1, std::tuple<ValueTypes...>>::type;
237237

238-
public:
239-
240-
/// @name Constructors
241-
/// @{
242-
243-
/// Default Constructor for I/O
244-
NoiseModelFactorN() {}
245-
246-
/**
247-
* Constructor.
248-
* Example usage: NoiseModelFactorN(noise, key1, key2, ..., keyN)
249-
* @param noiseModel Shared pointer to noise model.
250-
* @param keys Keys for the variables in this factor, passed in as separate
251-
* arguments.
252-
*/
253-
NoiseModelFactorN(const SharedNoiseModel& noiseModel,
254-
KeyType<ValueTypes>... keys)
255-
: Base(noiseModel, std::array<Key, N>{keys...}) {}
256-
257-
/**
258-
* Constructor.
259-
* Example usage: `NoiseModelFactorN(noise, {key1, key2, ..., keyN})`
260-
* Example usage: `NoiseModelFactorN(noise, keys)` where keys is a vector<Key>
261-
* @param noiseModel Shared pointer to noise model.
262-
* @param keys A container of keys for the variables in this factor.
263-
*/
264-
template <typename CONTAINER = std::initializer_list<Key>,
265-
typename = IsContainerOfKeys<CONTAINER>>
266-
NoiseModelFactorN(const SharedNoiseModel& noiseModel, CONTAINER keys)
267-
: Base(noiseModel, keys) {
268-
if (keys.size() != N) {
269-
throw std::invalid_argument(
270-
"NoiseModelFactorN: wrong number of keys given");
271-
}
272-
}
238+
public:
239+
/// @name Constructors
240+
/// @{
241+
242+
/// Default Constructor for I/O
243+
NoiseModelFactorT() {}
244+
245+
/**
246+
* Constructor.
247+
* Example usage: NoiseModelFactorT(noise, key1, key2, ..., keyN)
248+
* @param noiseModel Shared pointer to noise model.
249+
* @param keys Keys for the variables in this factor, passed in as separate
250+
* arguments.
251+
*/
252+
NoiseModelFactorT(const SharedNoiseModel& noiseModel,
253+
KeyType<ValueTypes>... keys)
254+
: Base(noiseModel, std::array<Key, N>{keys...}) {}
255+
256+
/**
257+
* Constructor.
258+
* Example usage: `NoiseModelFactorT(noise, {key1, key2, ..., keyN})`
259+
* Example usage: `NoiseModelFactorT(noise, keys)` where keys is a
260+
* vector<Key>
261+
* @param noiseModel Shared pointer to noise model.
262+
* @param keys A container of keys for the variables in this factor.
263+
*/
264+
template <typename CONTAINER = std::initializer_list<Key>,
265+
typename = IsContainerOfKeys<CONTAINER>>
266+
NoiseModelFactorT(const SharedNoiseModel& noiseModel, CONTAINER keys)
267+
: Base(noiseModel, keys) {
268+
if (keys.size() != N) {
269+
throw std::invalid_argument(
270+
"NoiseModelFactorT: wrong number of keys given");
271+
}
272+
}
273273

274274
/// @}
275275

276-
~NoiseModelFactorN() override {}
277-
278-
/** Returns a key. Usage: `key<I>()` returns the I'th key.
279-
* I is 1-indexed for backwards compatibility/consistency! So for example,
280-
* ```
281-
* NoiseModelFactorN<Pose3, Point3> factor(noise, key1, key2);
282-
* key<1>() // = key1
283-
* key<2>() // = key2
284-
* // key<0>() // ERROR! Will not compile
285-
* // key<3>() // ERROR! Will not compile
286-
* ```
287-
*
288-
* Note that, if your class is templated AND you are trying to call `key<1>`
289-
* inside your class, due to dependent types you need the `template` keyword:
290-
* `this->key1()`.
291-
*/
292-
template <int I = 1>
293-
inline Key key() const {
294-
static_assert(I <= N, "Index out of bounds");
295-
return keys_[I - 1];
296-
}
276+
~NoiseModelFactorT() override {}
277+
278+
/** Returns a key. Usage: `key<I>()` returns the I'th key.
279+
* I is 1-indexed for backwards compatibility/consistency! So for example,
280+
* ```
281+
* NoiseModelFactorT<Vector, Pose3, Point3> factor(noise, key1, key2);
282+
* key<1>() // = key1
283+
* key<2>() // = key2
284+
* // key<0>() // ERROR! Will not compile
285+
* // key<3>() // ERROR! Will not compile
286+
* ```
287+
*
288+
* Note that, if your class is templated AND you are trying to call `key<1>`
289+
* inside your class, due to dependent types you need the `template` keyword:
290+
* `this->key1()`.
291+
*/
292+
template <int I = 1>
293+
inline Key key() const {
294+
static_assert(I <= N, "Index out of bounds");
295+
return keys_[I - 1];
296+
}
297297

298298
/// @name NoiseModelFactor methods
299299
/// @{
@@ -347,15 +347,15 @@ class NoiseModelFactorN
347347
* as separate arguments.
348348
* @param[out] H The Jacobian with respect to each variable (optional).
349349
*/
350-
virtual Vector evaluateError(const ValueTypes&... x,
350+
virtual OutputVec evaluateError(const ValueTypes&... x,
351351
OptionalMatrixTypeT<ValueTypes>... H) const = 0;
352352

353353
/** If all the optional arguments are matrices then redirect the call to
354354
* the one which takes pointers.
355355
* To get access to this version of the function from derived classes
356356
* one will need to use the "using" keyword and specify that like this:
357357
* public:
358-
* using NoiseModelFactorN<list the value types here>::evaluateError;
358+
* using NoiseModelFactorT<list the value types here>::evaluateError;
359359
*/
360360
Vector evaluateError(const ValueTypes&... x,
361361
MatrixTypeT<ValueTypes>&... H) const {
@@ -406,7 +406,7 @@ class NoiseModelFactorN
406406
/** Pack expansion with index_sequence template pattern, used to index into
407407
* `keys_` and `H`.
408408
*
409-
* Example: For `NoiseModelFactorN<Pose3, Point3>`, the call would look like:
409+
* Example: For `NoiseModelFactorT<Pose3, Point3>`, the call would look like:
410410
* `const Vector error = unwhitenedError(0, 1, values, H);`
411411
*/
412412
template <std::size_t... Indices>
@@ -471,7 +471,10 @@ class NoiseModelFactorN
471471

472472
/// @}
473473

474-
}; // \class NoiseModelFactorN
474+
}; // \class NoiseModelFactorT
475+
476+
template <class... ValueTypes>
477+
using NoiseModelFactorN = NoiseModelFactorT<Vector, ValueTypes...>;
475478

476479
#define NoiseModelFactor1 NoiseModelFactorN
477480
#define NoiseModelFactor2 NoiseModelFactorN

gtsam/slam/PlanarProjectionFactor.h

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@
1414
*/
1515
#pragma once
1616

17-
#include <gtsam/base/Testable.h>
1817
#include <gtsam/base/Lie.h>
19-
18+
#include <gtsam/base/Testable.h>
19+
#include <gtsam/base/numericalDerivative.h>
2020
#include <gtsam/geometry/Cal3DS2.h>
2121
#include <gtsam/geometry/PinholeCamera.h>
2222
#include <gtsam/geometry/Point3.h>
2323
#include <gtsam/geometry/Pose2.h>
2424
#include <gtsam/geometry/Pose3.h>
2525
#include <gtsam/geometry/Rot3.h>
26+
#include <gtsam/nonlinear/NoiseModelFactorN.h>
2627
#include <gtsam/nonlinear/NonlinearFactor.h>
27-
#include <gtsam/base/numericalDerivative.h>
28-
2928

3029
namespace gtsam {
3130

@@ -139,7 +138,7 @@ namespace gtsam {
139138
const Cal3DS2& calib,
140139
const SharedNoiseModel& model = {})
141140
: PlanarProjectionFactorBase(measured),
142-
NoiseModelFactorN(model, poseKey),
141+
NoiseModelFactorN<Pose2>(model, poseKey),
143142
landmark_(landmark),
144143
bTc_(bTc),
145144
calib_(calib) {}
@@ -201,7 +200,7 @@ namespace gtsam {
201200
const Cal3DS2& calib,
202201
const SharedNoiseModel& model = {})
203202
: PlanarProjectionFactorBase(measured),
204-
NoiseModelFactorN(model, poseKey, landmarkKey),
203+
NoiseModelFactorN<Pose2, Point3>(model, poseKey, landmarkKey),
205204
bTc_(bTc),
206205
calib_(calib) {}
207206

@@ -258,16 +257,13 @@ namespace gtsam {
258257
* @param measured corresponding point2 in the camera frame
259258
* @param model stddev of the measurements, ~one pixel?
260259
*/
261-
PlanarProjectionFactor3(
262-
Key poseKey,
263-
Key offsetKey,
264-
Key calibKey,
265-
const Point3& landmark,
266-
const Point2& measured,
267-
const SharedNoiseModel& model = {})
260+
PlanarProjectionFactor3(Key poseKey, Key offsetKey, Key calibKey,
261+
const Point3& landmark, const Point2& measured,
262+
const SharedNoiseModel& model = {})
268263
: PlanarProjectionFactorBase(measured),
269-
NoiseModelFactorN(model, poseKey, offsetKey, calibKey),
270-
landmark_(landmark) {}
264+
NoiseModelFactorN<Pose2, Pose3, Cal3DS2>(model, poseKey,
265+
offsetKey, calibKey),
266+
landmark_(landmark) {}
271267

272268
/**
273269
* @param wTb "world to body": estimated pose2

tests/testNonlinearFactor.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <tests/simulated2D.h>
3232
#include <gtsam/linear/GaussianFactor.h>
3333
#include <gtsam/nonlinear/NonlinearFactorGraph.h>
34+
#include <gtsam/nonlinear/NoiseModelFactorN.h>
3435
#include <gtsam/inference/Symbol.h>
3536

3637
using namespace std;
@@ -344,7 +345,7 @@ class TestFactor1 : public NoiseModelFactor1<double> {
344345
TestFactor1() : Base(noiseModel::Diagonal::Sigmas(Vector1(2.0)), L(1)) {}
345346

346347
// Provide access to the Matrix& version of evaluateError:
347-
using Base::NoiseModelFactor1; // inherit constructors
348+
using Base::NoiseModelFactorT; // inherit constructors
348349

349350
Vector evaluateError(const double& x1, OptionalMatrixType H1) const override {
350351
if (H1) *H1 = (Matrix(1, 1) << 1.0).finished();
@@ -401,7 +402,7 @@ class TestFactor4 : public NoiseModelFactor4<double, double, double, double> {
401402
TestFactor4() : Base(noiseModel::Diagonal::Sigmas((Vector(1) << 2.0).finished()), X(1), X(2), X(3), X(4)) {}
402403

403404
// Provide access to the Matrix& version of evaluateError:
404-
using Base::NoiseModelFactor4; // inherit constructors
405+
using Base::NoiseModelFactorT; // inherit constructors
405406

406407
Vector
407408
evaluateError(const double& x1, const double& x2, const double& x3, const double& x4,

0 commit comments

Comments
 (0)