32 #ifdef GTSAM_USE_BOOST_FEATURES 33 #include <boost/assign/list_inserter.hpp> 36 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION 37 #include <boost/serialization/nvp.hpp> 38 #include <boost/serialization/vector.hpp> 42 #include <type_traits> 51 template <
class CLIQUE>
64 void operator()(
const A& a) {
77 void operator()(A& a) {
90 void operator()(
const A& a) {
100 template <
class FACTOR>
104 typedef std::shared_ptr<FACTOR>
112 typedef std::shared_ptr<This>
116 template <
typename DERIVEDFACTOR>
117 using IsDerived =
typename std::enable_if<
118 std::is_base_of<FactorType, DERIVEDFACTOR>::value>::type;
121 template <
typename T>
122 using HasDerivedValueType =
typename std::enable_if<
123 std::is_base_of<FactorType, typename T::value_type>::value>::type;
126 template <
typename T>
127 using HasDerivedElementType =
typename std::enable_if<std::is_base_of<
128 FactorType,
typename T::value_type::element_type>::value>::type;
132 GTSAM_CONCEPT_TESTABLE_TYPE(FACTOR)
139 return factors_ == other.factors_;
149 template <
typename ITERATOR>
151 push_back(firstFactor, lastFactor);
155 template <
class CONTAINER>
174 template <
class DERIVEDFACTOR,
typename = IsDerived<DERIVEDFACTOR>>
175 FactorGraph(std::initializer_list<std::shared_ptr<DERIVEDFACTOR>> sharedFactors)
176 : factors_(sharedFactors) {}
186 void reserve(
size_t size) { factors_.reserve(size); }
189 template <
class DERIVEDFACTOR>
190 IsDerived<DERIVEDFACTOR>
push_back(std::shared_ptr<DERIVEDFACTOR> factor) {
191 factors_.push_back(std::shared_ptr<FACTOR>(factor));
195 template <
class DERIVEDFACTOR,
class... Args>
197 factors_.push_back(std::allocate_shared<DERIVEDFACTOR>(
198 Eigen::aligned_allocator<DERIVEDFACTOR>(),
199 std::forward<Args>(args)...));
206 template <
class DERIVEDFACTOR>
207 IsDerived<DERIVEDFACTOR>
push_back(
const DERIVEDFACTOR& factor) {
208 factors_.push_back(std::allocate_shared<DERIVEDFACTOR>(
209 Eigen::aligned_allocator<DERIVEDFACTOR>(), factor));
213 template <
class DERIVEDFACTOR>
214 IsDerived<DERIVEDFACTOR>
add(std::shared_ptr<DERIVEDFACTOR> factor) {
218 #ifdef GTSAM_USE_BOOST_FEATURES 219 template <
class DERIVEDFACTOR>
221 typename std::enable_if<
222 std::is_base_of<FactorType, DERIVEDFACTOR>::value,
223 boost::assign::list_inserter<RefCallPushBack<This>>>::type
224 operator+=(std::shared_ptr<DERIVEDFACTOR> factor) {
238 template <
typename ITERATOR>
239 HasDerivedElementType<ITERATOR>
push_back(ITERATOR firstFactor,
240 ITERATOR lastFactor) {
241 factors_.insert(end(), firstFactor, lastFactor);
245 template <
typename ITERATOR>
246 HasDerivedValueType<ITERATOR>
push_back(ITERATOR firstFactor,
247 ITERATOR lastFactor) {
248 for (ITERATOR f = firstFactor; f != lastFactor; ++f) push_back(*f);
259 template <
typename CONTAINER>
260 HasDerivedElementType<CONTAINER>
push_back(
const CONTAINER& container) {
261 push_back(container.begin(), container.end());
265 template <
typename CONTAINER>
266 HasDerivedValueType<CONTAINER>
push_back(
const CONTAINER& container) {
267 push_back(container.begin(), container.end());
274 template <
class FACTOR_OR_CONTAINER>
275 void add(
const FACTOR_OR_CONTAINER& factorOrContainer) {
276 push_back(factorOrContainer);
279 #ifdef GTSAM_USE_BOOST_FEATURES 284 template <
class FACTOR_OR_CONTAINER>
285 boost::assign::list_inserter<CRefCallPushBack<This>> operator+=(
286 const FACTOR_OR_CONTAINER& factorOrContainer) {
301 template <
class CLIQUE>
302 typename std::enable_if<
303 std::is_base_of<This, typename CLIQUE::FactorGraphType>::value>::type
312 template <
typename CONTAINER,
typename = HasDerivedElementType<CONTAINER>>
313 FactorIndices add_factors(
const CONTAINER& factors,
314 bool useEmptySlots =
false);
321 virtual void print(
const std::string& s =
"FactorGraph",
322 const KeyFormatter& formatter = DefaultKeyFormatter)
const;
325 bool equals(
const This& fg,
double tol = 1e-9)
const;
334 size_t size()
const {
return factors_.size(); }
338 bool empty()
const {
return factors_.empty(); }
361 const_iterator
begin()
const {
return factors_.begin(); }
364 const_iterator
end()
const {
return factors_.end(); }
380 iterator
begin() {
return factors_.begin(); }
383 iterator
end() {
return factors_.end(); }
389 virtual void resize(
size_t size) { factors_.resize(size); }
393 void remove(
size_t i) { factors_.at(i).reset(); }
399 iterator
erase(iterator item) {
return factors_.erase(item); }
402 iterator
erase(iterator first, iterator last) {
403 return factors_.erase(first, last);
411 void dot(std::ostream& os,
416 std::string
dot(
const KeyFormatter& keyFormatter = DefaultKeyFormatter,
420 void saveGraph(
const std::string& filename,
429 size_t nrFactors()
const;
442 inline bool exists(
size_t idx)
const {
return idx < size() && at(idx); }
445 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION 447 friend class boost::serialization::access;
448 template <
class ARCHIVE>
449 void serialize(ARCHIVE& ar,
const unsigned int ) {
450 ar& BOOST_SERIALIZATION_NVP(factors_);
Definition: HybridValues.h:38
sharedFactor back() const
Definition: FactorGraph.h:370
FactorGraph()
Definition: FactorGraph.h:146
void add(const FACTOR_OR_CONTAINER &factorOrContainer)
Definition: FactorGraph.h:275
void replace(size_t index, sharedFactor factor)
Definition: FactorGraph.h:396
Concept check for values that can be used in unit tests.
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
Definition: FactorGraph.h:196
double dot(const V1 &a, const V2 &b)
Definition: Vector.h:195
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:113
HasDerivedElementType< ITERATOR > push_back(ITERATOR firstFactor, ITERATOR lastFactor)
Definition: FactorGraph.h:239
FactorGraph(ITERATOR firstFactor, ITERATOR lastFactor)
Definition: FactorGraph.h:150
sharedFactor front() const
Definition: FactorGraph.h:367
const sharedFactor operator[](size_t i) const
Definition: FactorGraph.h:353
IsDerived< DERIVEDFACTOR > push_back(std::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:190
HasDerivedValueType< ITERATOR > push_back(ITERATOR firstFactor, ITERATOR lastFactor)
Push back many factors with an iterator (factors are copied)
Definition: FactorGraph.h:246
FastVector< FactorIndex > FactorIndices
Define collection types:
Definition: Factor.h:36
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
HasDerivedValueType< CONTAINER > push_back(const CONTAINER &container)
Push back non-pointer objects in a container (factors are copied).
Definition: FactorGraph.h:266
IsDerived< DERIVEDFACTOR > push_back(const DERIVEDFACTOR &factor)
Definition: FactorGraph.h:207
Definition: BayesTree.h:34
bool exists(size_t idx) const
Definition: FactorGraph.h:442
FACTOR FactorType
factor type
Definition: FactorGraph.h:103
Definition: FactorGraph.h:84
IsDerived< DERIVEDFACTOR > add(std::shared_ptr< DERIVEDFACTOR > factor)
add is a synonym for push_back.
Definition: FactorGraph.h:214
Definition: FactorGraph.h:71
size_t size() const
Definition: FactorGraph.h:334
Definition: BayesTree.h:66
const_iterator begin() const
Definition: FactorGraph.h:361
Definition: Testable.h:112
sharedFactor & at(size_t i)
Definition: FactorGraph.h:348
FactorGraph(const CONTAINER &factors)
Definition: FactorGraph.h:156
GTSAM_EXPORT void print(const Matrix &A, const std::string &s, std::ostream &stream)
Definition: FactorGraph.h:58
bool empty() const
Definition: FactorGraph.h:338
iterator begin()
Definition: FactorGraph.h:380
FactorGraph(std::initializer_list< std::shared_ptr< DERIVEDFACTOR >> sharedFactors)
Definition: FactorGraph.h:175
iterator end()
Definition: FactorGraph.h:383
void addFactorsToGraph(FactorGraph< FactorType > *graph) const
Definition: BayesTree-inst.h:168
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:35
sharedFactor & operator[](size_t i)
Definition: FactorGraph.h:358
const_iterator end() const
Definition: FactorGraph.h:364
DotWriter is a helper class for writing graphviz .dot files.
Definition: DotWriter.h:36
A thin wrapper around std::vector that uses a custom allocator.
Definition: chartTesting.h:28
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
virtual void resize(size_t size)
Definition: FactorGraph.h:389
iterator erase(iterator first, iterator last)
Definition: FactorGraph.h:402
HasDerivedElementType< CONTAINER > push_back(const CONTAINER &container)
Definition: FactorGraph.h:260
std::enable_if< std::is_base_of< This, typename CLIQUE::FactorGraphType >::value >::type push_back(const BayesTree< CLIQUE > &bayesTree)
Definition: FactorGraph.h:304
const sharedFactor at(size_t i) const
Definition: FactorGraph.h:343
void reserve(size_t size)
Definition: FactorGraph.h:186
std::shared_ptr< FACTOR > sharedFactor
Shared pointer to a factor.
Definition: FactorGraph.h:105
iterator erase(iterator item)
Definition: FactorGraph.h:399