Added nonlinear Values

release/4.3a0
Frank Dellaert 2023-01-08 16:09:20 -08:00
parent d41d4c224d
commit e0a40b306d
1 changed files with 48 additions and 22 deletions

View File

@ -37,12 +37,15 @@ namespace gtsam {
*/ */
class GTSAM_EXPORT HybridValues { class GTSAM_EXPORT HybridValues {
private: private:
// VectorValue stored the continuous components of the HybridValues. /// Continuous multi-dimensional vectors for \class GaussianFactor.
VectorValues continuous_; VectorValues continuous_;
// DiscreteValue stored the discrete components of the HybridValues. /// Discrete values for \class DiscreteFactor.
DiscreteValues discrete_; DiscreteValues discrete_;
/// Continuous, differentiable manifold values for \class NonlinearFactor.
Values nonlinear_;
public: public:
/// @name Standard Constructors /// @name Standard Constructors
/// @{ /// @{
@ -54,6 +57,11 @@ class GTSAM_EXPORT HybridValues {
HybridValues(const VectorValues& cv, const DiscreteValues& dv) HybridValues(const VectorValues& cv, const DiscreteValues& dv)
: continuous_(cv), discrete_(dv){}; : continuous_(cv), discrete_(dv){};
/// Construct from all values types.
HybridValues(const VectorValues& cv, const DiscreteValues& dv,
const Values& v)
: continuous_(cv), discrete_(dv), nonlinear_(v){};
/// @} /// @}
/// @name Testable /// @name Testable
/// @{ /// @{
@ -77,26 +85,30 @@ class GTSAM_EXPORT HybridValues {
/// @name Interface /// @name Interface
/// @{ /// @{
/// Return the discrete MPE assignment /// Return the multi-dimensional vector values.
const DiscreteValues& discrete() const { return discrete_; }
/// Return the delta update for the continuous vectors
const VectorValues& continuous() const { return continuous_; } const VectorValues& continuous() const { return continuous_; }
/// Check whether a variable with key \c j exists in DiscreteValue. /// Return the discrete values.
bool existsDiscrete(Key j) { return (discrete_.find(j) != discrete_.end()); }; const DiscreteValues& discrete() const { return discrete_; }
/// Check whether a variable with key \c j exists in VectorValue. /// Return the nonlinear values.
const Values& nonlinear() const { return nonlinear_; }
/// Check whether a variable with key \c j exists in VectorValues.
bool existsVector(Key j) { return continuous_.exists(j); }; bool existsVector(Key j) { return continuous_.exists(j); };
/// Check whether a variable with key \c j exists. /// Check whether a variable with key \c j exists in DiscreteValues.
bool exists(Key j) { return existsDiscrete(j) || existsVector(j); }; bool existsDiscrete(Key j) { return (discrete_.find(j) != discrete_.end()); };
/** Insert a discrete \c value with key \c j. Replaces the existing value if /// Check whether a variable with key \c j exists in values.
* the key \c j is already used. bool existsNonlinear(Key j) {
* @param value The vector to be inserted. return (nonlinear_.find(j) != nonlinear_.end());
* @param j The index with which the value will be associated. */ };
void insert(Key j, size_t value) { discrete_[j] = value; };
/// Check whether a variable with key \c j exists.
bool exists(Key j) {
return existsVector(j) || existsDiscrete(j) || existsNonlinear(j);
};
/** Insert a vector \c value with key \c j. Throws an invalid_argument /** Insert a vector \c value with key \c j. Throws an invalid_argument
* exception if the key \c j is already used. * exception if the key \c j is already used.
@ -104,6 +116,12 @@ class GTSAM_EXPORT HybridValues {
* @param j The index with which the value will be associated. */ * @param j The index with which the value will be associated. */
void insert(Key j, const Vector& value) { continuous_.insert(j, value); } void insert(Key j, const Vector& value) { continuous_.insert(j, value); }
/** Insert a discrete \c value with key \c j. Replaces the existing value if
* the key \c j is already used.
* @param value The vector to be inserted.
* @param j The index with which the value will be associated. */
void insert(Key j, size_t value) { discrete_[j] = value; };
/** Insert all continuous values from \c values. Throws an invalid_argument /** Insert all continuous values from \c values. Throws an invalid_argument
* exception if any keys to be inserted are already used. */ * exception if any keys to be inserted are already used. */
HybridValues& insert(const VectorValues& values) { HybridValues& insert(const VectorValues& values) {
@ -118,28 +136,36 @@ class GTSAM_EXPORT HybridValues {
return *this; return *this;
} }
/** Insert all values from \c values. Throws an invalid_argument
* exception if any keys to be inserted are already used. */
HybridValues& insert(const Values& values) {
nonlinear_.insert(values);
return *this;
}
/** Insert all values from \c values. Throws an invalid_argument exception if /** Insert all values from \c values. Throws an invalid_argument exception if
* any keys to be inserted are already used. */ * any keys to be inserted are already used. */
HybridValues& insert(const HybridValues& values) { HybridValues& insert(const HybridValues& values) {
continuous_.insert(values.continuous()); continuous_.insert(values.continuous());
discrete_.insert(values.discrete()); discrete_.insert(values.discrete());
nonlinear_.insert(values.nonlinear());
return *this; return *this;
} }
// TODO(Shangjie)- insert_or_assign() , similar to Values.h // TODO(Shangjie)- insert_or_assign() , similar to Values.h
/**
* Read/write access to the discrete value with key \c j, throws
* std::out_of_range if \c j does not exist.
*/
size_t& atDiscrete(Key j) { return discrete_.at(j); };
/** /**
* Read/write access to the vector value with key \c j, throws * Read/write access to the vector value with key \c j, throws
* std::out_of_range if \c j does not exist. * std::out_of_range if \c j does not exist.
*/ */
Vector& at(Key j) { return continuous_.at(j); }; Vector& at(Key j) { return continuous_.at(j); };
/**
* Read/write access to the discrete value with key \c j, throws
* std::out_of_range if \c j does not exist.
*/
size_t& atDiscrete(Key j) { return discrete_.at(j); };
/** For all key/value pairs in \c values, replace continuous values with /** For all key/value pairs in \c values, replace continuous values with
* corresponding keys in this object with those in \c values. Throws * corresponding keys in this object with those in \c values. Throws
* std::out_of_range if any keys in \c values are not present in this object. * std::out_of_range if any keys in \c values are not present in this object.