gtsam
Loading...
Searching...
No Matches
gtsam::BatchFactor< FactorType, ErrorDim > Class Template Reference

Detailed Description

template<typename FactorType, int ErrorDim>
class gtsam::BatchFactor< FactorType, ErrorDim >

BatchFactor is a NonlinearFactor that wraps a collection of identical factors.

It linearizes them all at once into a single JacobianFactor.

This is useful for optimizing Structure-from-Motion (SfM) and SLAM graphs where we have many factors of the same type (e.g., projection factors) that can be grouped together to reduce overhead.

Usage Example:

// Assume we have GenericProjectionFactor<Pose3, Point3> using ProjectionFactor = GenericProjectionFactor<Pose3, Point3>;

// Create a batch factor std::vector<Key> poses = {Symbol('x', 1)}; std::vector<Key> points; std::vector<Point2> measurements; for (int i = 0; i < 100; ++i) { points.push_back(Symbol('l', i)); measurements.push_back(Point2(10, 10)); // Dummy measurement }

auto noise = noiseModel::Isotropic::Sigma(2, 1.0);

// Construct using the helper (1 camera, 100 points) auto batch = std::make_shared<BatchFactor<ProjectionFactor, 2>>( poses, points, measurements, noise);

// Add to graph NonlinearFactorGraph graph; graph.add(batch);

// Optimize as usual LevenbergMarquardtOptimizer optimizer(graph, initial_values); Values result = optimizer.optimize();

Template Parameters
FactorTypeThe type of the individual factors
ErrorDimThe dimension of the error vector for a single factor
Inheritance diagram for gtsam::BatchFactor< FactorType, ErrorDim >:

Public Member Functions

Constructors
 BatchFactor ()=default
 Default constructor.
const FactorVector & factors () const
 Return the child factors represented by this batch.
size_t numFactors () const
 Return the number of child factors represented by this batch.
 BatchFactor (std::vector< FactorType, Allocator > &&factors)
 Constructor from a vector of factors (moves the vector).
 BatchFactor (const std::vector< FactorType, Allocator > &factors)
 Constructor from a vector of factors (copies the vector).
 BatchFactor (const std::vector< FactorType > &factors)
 Constructor from a standard vector of factors (copies the vector).
 BatchFactor (std::vector< FactorType > &&factors)
 Constructor from a standard vector of factors (moves elements).
template<typename Measurement, typename... Args>
 BatchFactor (Key key1, const std::map< Key, Measurement > &measurements, const SharedNoiseModel &model, Args &&... args)
 Map-based Constructor (Varying Key2).
template<typename Measurement, typename... Args>
 BatchFactor (const std::map< Key, Measurement > &measurements, Key key2, const SharedNoiseModel &model, Args &&... args)
 Map-based Constructor (Varying Key1).
Testable
void print (const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
 Print the BatchFactor.
bool equals (const NonlinearFactor &f, double tol=1e-9) const override
 Check equality with another factor.
Standard Interface
double error (const Values &c) const override
 Calculate the error of the factor.
size_t dim () const override
 Get the dimension of the factor (number of rows on linearization).
void setUseHessianFactor (bool flag)
std::shared_ptr< GaussianFactor > linearize (const Values &values) const override
 Linearize to a single JacobianFactor.
template<size_t... Is>
void collectKeys (const FactorType &f, std::index_sequence< Is... >)
 Helper to collect keys and dimensions using fold expression.
void updateKeys ()
 Update keys_ by collecting unique keys from all factors.
 NonlinearFactor ()
 Default constructor for I/O only.
template<typename CONTAINER>
 NonlinearFactor (const CONTAINER &keys)
 Constructor from a collection of the keys involved in this factor.
void print (const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
 print
double error (const HybridValues &c) const override
 All factor types need to implement an error function.
virtual bool active (const Values &c) const
 Checks whether a factor should be used based on a set of values.
virtual void qcqpFactors (NonlinearFactorGraph *costs, NonlinearEqualityConstraints *constraints, size_t columnDimension=1) const
 Add this factor's QCQP cost and constraints over matrix-valued QCQP variables with the given column dimension.
virtual shared_ptr clone () const
 Creates a shared_ptr clone of the factor - needs to be specialized to allow for subclasses.
virtual shared_ptr rekey (const std::map< Key, Key > &rekey_mapping) const
 Creates a shared_ptr clone of the factor with different keys using a map from old->new keys.
virtual shared_ptr rekey (const KeyVector &new_keys) const
 Clones a factor and fully replaces its keys.
virtual bool sendable () const
 Should the factor be evaluated in the same thread as the caller This is to enable factors that has shared states (like the Python GIL lock).
Public Member Functions inherited from gtsam::Factor
virtual ~Factor ()=default
 Default destructor.
bool empty () const
 Whether the factor is empty (involves zero variables).
Key front () const
 First key.
Key back () const
 Last key.
const_iterator find (Key key) const
 find
const KeyVector & keys () const
 Access the factor's involved variable keys.
const_iterator begin () const
 Iterator at beginning of involved variable keys.
const_iterator end () const
 Iterator at end of involved variable keys.
size_t size () const
virtual void printKeys (const std::string &s="Factor", const KeyFormatter &formatter=DefaultKeyFormatter) const
 print only keys
bool equals (const This &other, double tol=1e-9) const
 check equality
KeyVector & keys ()
iterator begin ()
 Iterator at beginning of involved variable keys.
iterator end ()
 Iterator at end of involved variable keys.

Public Types

using Base = NonlinearFactor
using This = BatchFactor<FactorType, ErrorDim>
using shared_ptr = std::shared_ptr<This>
using FactorVector
Public Types inherited from gtsam::NonlinearFactor
typedef std::shared_ptr< This > shared_ptr
Public Types inherited from gtsam::Factor
typedef KeyVector::iterator iterator
 Iterator over keys.
typedef KeyVector::const_iterator const_iterator
 Const iterator over keys.

Additional Inherited Members

Protected Types inherited from gtsam::NonlinearFactor
typedef Factor Base
typedef NonlinearFactor This
 Factor ()
 Default constructor for I/O.
template<typename CONTAINER>
 Factor (const CONTAINER &keys)
 Construct factor from container of keys.
template<typename ITERATOR>
 Factor (ITERATOR first, ITERATOR last)
 Construct factor from iterator keys.
template<typename CONTAINER>
static Factor FromKeys (const CONTAINER &keys)
 Construct factor from container of keys.
template<typename ITERATOR>
static Factor FromIterators (ITERATOR first, ITERATOR last)
 Construct factor from iterator keys.
Protected Attributes inherited from gtsam::Factor
KeyVector keys_
 The keys involved in this factor.

Member Typedef Documentation

◆ FactorVector

template<typename FactorType, int ErrorDim>
using gtsam::BatchFactor< FactorType, ErrorDim >::FactorVector
Initial value:
std::vector<FactorType, Eigen::aligned_allocator<FactorType>>

Constructor & Destructor Documentation

◆ BatchFactor() [1/2]

template<typename FactorType, int ErrorDim>
template<typename Measurement, typename... Args>
gtsam::BatchFactor< FactorType, ErrorDim >::BatchFactor ( Key key1,
const std::map< Key, Measurement > & measurements,
const SharedNoiseModel & model,
Args &&... args )
inline

Map-based Constructor (Varying Key2).

Constructs factors from a map of measurements, where the map key is the second factor key.

Parameters
key1The fixed first key (e.g., camera pose).
measurementsMap from Key (2nd key) to Measurement.
modelNoise model.
argsExtra arguments passed to the factor constructor.

◆ BatchFactor() [2/2]

template<typename FactorType, int ErrorDim>
template<typename Measurement, typename... Args>
gtsam::BatchFactor< FactorType, ErrorDim >::BatchFactor ( const std::map< Key, Measurement > & measurements,
Key key2,
const SharedNoiseModel & model,
Args &&... args )
inline

Map-based Constructor (Varying Key1).

Constructs factors from a map of measurements, where the map key is the first factor key.

Parameters
measurementsMap from Key (1st key) to Measurement.
key2The fixed second key (e.g., landmark).
modelNoise model.
argsExtra arguments passed to the factor constructor.

Member Function Documentation

◆ dim()

template<typename FactorType, int ErrorDim>
size_t gtsam::BatchFactor< FactorType, ErrorDim >::dim ( ) const
inlineoverridevirtual

Get the dimension of the factor (number of rows on linearization).

Implements gtsam::NonlinearFactor.

◆ equals()

template<typename FactorType, int ErrorDim>
bool gtsam::BatchFactor< FactorType, ErrorDim >::equals ( const NonlinearFactor & f,
double tol = 1e-9 ) const
inlineoverridevirtual

Check equality with another factor.

Reimplemented from gtsam::NonlinearFactor.

◆ error()

template<typename FactorType, int ErrorDim>
double gtsam::BatchFactor< FactorType, ErrorDim >::error ( const Values & c) const
inlineoverridevirtual

Calculate the error of the factor.

This is the sum of the errors of all internal factors.

Reimplemented from gtsam::NonlinearFactor.

◆ linearize()

template<typename FactorType, int ErrorDim>
std::shared_ptr< GaussianFactor > gtsam::BatchFactor< FactorType, ErrorDim >::linearize ( const Values & values) const
inlineoverridevirtual

Linearize to a single JacobianFactor.

Optimization:

  • Pre-calculates the total size required for the JacobianFactor.
  • Collects all unique Keys involved across all sub-factors.
  • Iterates linearly over factors_ (cache-friendly) to compute Jacobians.
  • Fills the pre-allocated JacobianFactor directly.

Implements gtsam::NonlinearFactor.

◆ print()

template<typename FactorType, int ErrorDim>
void gtsam::BatchFactor< FactorType, ErrorDim >::print ( const std::string & s = "",
const KeyFormatter & keyFormatter = DefaultKeyFormatter ) const
inlineoverridevirtual

Print the BatchFactor.

Reimplemented from gtsam::Factor.


The documentation for this class was generated from the following file:
  • /tmp/gtsam-4.3.0-doxygen.rsXPUS/source/gtsam/nonlinear/BatchFactor.h