BoundingConstraint.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
18 #pragma once
19 
20 #include <gtsam/base/Lie.h>
23 
24 namespace gtsam {
25 
33 template<class VALUE>
35  typedef VALUE X;
37  typedef std::shared_ptr<BoundingConstraint1<VALUE> > shared_ptr;
38 
39  double threshold_;
41 
43  bool isGreaterThan, double mu = 1000.0) :
44  Base(noiseModel::Constrained::All(1, mu), KeyVector{key}),
46  }
47 
48  ~BoundingConstraint1() override {}
49 
50  inline double threshold() const { return threshold_; }
51  inline bool isGreaterThan() const { return isGreaterThan_; }
52  inline Key key() const { return keys().front(); }
53 
59  virtual double value(const X& x, OptionalMatrixType H =
60  OptionalNone) const = 0;
61 
62  Vector unwhitenedExpr(const Values& x, OptionalMatrixVecType H = {}) const override {
63  if (H) {
64  double d = value(x.at<X>(this->key()), &(H->front()));
65  if (isGreaterThan_) {
66  H->front() *= -1;
67  return Vector1(threshold_ - d);
68  } else {
69  return Vector1(d - threshold_);
70  }
71  } else {
72  double d = value(x.at<X>(this->key()));
73  return Vector1((isGreaterThan_) ? threshold_ - d : d - threshold_);
74  }
75  }
76 
78  Vector evaluateError(const X& x, OptionalMatrixType H = {}) const {
79  Matrix D;
80  double error = value(x, &D) - threshold_;
81  if (H) {
82  if (isGreaterThan_) *H = D;
83  else *H = -1.0 * D;
84  }
85 
86  if (isGreaterThan_)
87  return (Vector(1) << error).finished();
88  else
89  return -1.0 * (Vector(1) << error).finished();
90  }
91 
92 private:
93 
94 #if GTSAM_ENABLE_BOOST_SERIALIZATION
95 
96  friend class boost::serialization::access;
97  template<class ARCHIVE>
98  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
99  // NoiseModelFactor1 instead of NoiseModelFactorN for backward compatibility
100  ar & boost::serialization::make_nvp("NoiseModelFactor1",
101  boost::serialization::base_object<Base>(*this));
102  ar & BOOST_SERIALIZATION_NVP(threshold_);
103  ar & BOOST_SERIALIZATION_NVP(isGreaterThan_);
104  }
105 #endif
106 };
107 
112 template<class VALUE1, class VALUE2>
114  typedef VALUE1 X1;
115  typedef VALUE2 X2;
116 
118  typedef std::shared_ptr<BoundingConstraint2<VALUE1, VALUE2> > shared_ptr;
119 
120  double threshold_;
122 
124  bool isGreaterThan, double mu = 1000.0)
125  : Base(noiseModel::Constrained::All(1, mu), KeyVector{key1, key2}),
127 
128  ~BoundingConstraint2() override {}
129 
130  inline double threshold() const { return threshold_; }
131  inline bool isGreaterThan() const { return isGreaterThan_; }
132 
137  virtual double value(const X1& x1, const X2& x2,
139  OptionalMatrixType H2 = OptionalNone) const = 0;
140 
141  Vector unwhitenedExpr(const Values& x, OptionalMatrixVecType H = {}) const override {
142  X1 x1 = x.at<X1>(keys().front());
143  X2 x2 = x.at<X2>(keys().back());
144  if (H) {
145  double d = value(x1, x2, &(H->front()), &(H->back()));
146  if (isGreaterThan_) {
147  H->front() *= -1;
148  H->back() *= -1;
149  return Vector1(threshold_ - d);
150  } else {
151  return Vector1(d - threshold_);
152  }
153  } else {
154  double d = value(x1, x2);
155  return Vector1((isGreaterThan_) ? threshold_ - d : d - threshold_);
156  }
157  }
158 
160  Vector evaluateError(const X1& x1, const X2& x2,
161  OptionalMatrixType H1 = {}, OptionalMatrixType H2 = {}) const {
162  Matrix D1, D2;
163  double error = value(x1, x2, &D1, &D2) - threshold_;
164  if (H1) {
165  if (isGreaterThan_) *H1 = D1;
166  else *H1 = -1.0 * D1;
167  }
168  if (H2) {
169  if (isGreaterThan_) *H2 = D2;
170  else *H2 = -1.0 * D2;
171  }
172 
173  if (isGreaterThan_)
174  return (Vector(1) << error).finished();
175  else
176  return -1.0 * (Vector(1) << error).finished();
177  }
178 
179 private:
180 
181 #if GTSAM_ENABLE_BOOST_SERIALIZATION
182 
183  friend class boost::serialization::access;
184  template<class ARCHIVE>
185  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
186  // NoiseModelFactor2 instead of NoiseModelFactorN for backward compatibility
187  ar & boost::serialization::make_nvp("NoiseModelFactor2",
188  boost::serialization::base_object<Base>(*this));
189  ar & BOOST_SERIALIZATION_NVP(threshold_);
190  ar & BOOST_SERIALIZATION_NVP(isGreaterThan_);
191  }
192 #endif
193 };
194 
195 } // \namespace gtsam
key1
const Symbol key1('v', 1)
gtsam::BoundingConstraint2::threshold
double threshold() const
Definition: BoundingConstraint.h:130
H
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate set rrange[*:*] noreverse nowriteback set trange[*:*] noreverse nowriteback set urange[*:*] noreverse nowriteback set vrange[*:*] noreverse nowriteback set xlabel matrix size set x2label set timefmt d m y n H
Definition: gnuplot_common_settings.hh:74
gtsam::BoundingConstraint1
Definition: BoundingConstraint.h:34
gtsam::BoundingConstraint2::X2
VALUE2 X2
Definition: BoundingConstraint.h:115
gtsam::BoundingConstraint1::shared_ptr
std::shared_ptr< BoundingConstraint1< VALUE > > shared_ptr
Definition: BoundingConstraint.h:37
gtsam::BoundingConstraint2::~BoundingConstraint2
~BoundingConstraint2() override
Definition: BoundingConstraint.h:128
gtsam::Vector1
Eigen::Matrix< double, 1, 1 > Vector1
Definition: Vector.h:42
gtsam::BoundingConstraint2::Base
NonlinearInequalityConstraint Base
Definition: BoundingConstraint.h:117
gtsam::BoundingConstraint2::isGreaterThan_
bool isGreaterThan_
Definition: BoundingConstraint.h:121
D
MatrixXcd D
Definition: EigenSolver_EigenSolver_MatrixType.cpp:14
d
static const double d[K][N]
Definition: igam.h:11
gtsam::NoiseModelFactor::error
double error(const Values &c) const override
Definition: NonlinearFactor.cpp:138
gtsam::BoundingConstraint1::BoundingConstraint1
BoundingConstraint1(Key key, double threshold, bool isGreaterThan, double mu=1000.0)
flag for greater/less than
Definition: BoundingConstraint.h:42
mu
double mu
Definition: testBoundingConstraint.cpp:37
pybind_wrapper_test_script.this
this
Definition: pybind_wrapper_test_script.py:38
gtsam::BoundingConstraint1::isGreaterThan
bool isGreaterThan() const
Definition: BoundingConstraint.h:51
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
gtsam::BoundingConstraint1::evaluateError
Vector evaluateError(const X &x, OptionalMatrixType H={}) const
TODO: This should be deprecated.
Definition: BoundingConstraint.h:78
gtsam::Matrix
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
gtsam::BoundingConstraint2
Definition: BoundingConstraint.h:113
gtsam::BoundingConstraint1::threshold_
double threshold_
Definition: BoundingConstraint.h:39
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:39
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:92
gtsam::BoundingConstraint1::threshold
double threshold() const
Definition: BoundingConstraint.h:50
Eigen::internal::VALUE
@ VALUE
Definition: SpecialFunctionsImpl.h:729
gtsam::BoundingConstraint2::isGreaterThan
bool isGreaterThan() const
Definition: BoundingConstraint.h:131
gtsam::BoundingConstraint1::~BoundingConstraint1
~BoundingConstraint1() override
Definition: BoundingConstraint.h:48
key2
const Symbol key2('v', 2)
OptionalNone
#define OptionalNone
Definition: NonlinearFactor.h:50
x1
Pose3 x1
Definition: testPose3.cpp:692
gtsam::NoiseModelFactor::noiseModel
const SharedNoiseModel & noiseModel() const
access to the noise model
Definition: NonlinearFactor.h:246
gtsam::BoundingConstraint2::unwhitenedExpr
Vector unwhitenedExpr(const Values &x, OptionalMatrixVecType H={}) const override
Definition: BoundingConstraint.h:141
gtsam::BoundingConstraint2::shared_ptr
std::shared_ptr< BoundingConstraint2< VALUE1, VALUE2 > > shared_ptr
Definition: BoundingConstraint.h:118
gtsam::BoundingConstraint1::value
virtual double value(const X &x, OptionalMatrixType H=OptionalNone) const =0
gtsam::BoundingConstraint1::Base
NonlinearInequalityConstraint Base
Definition: BoundingConstraint.h:36
NonlinearFactor.h
Non-linear factor base classes.
gtsam::OptionalMatrixVecType
std::vector< Matrix > * OptionalMatrixVecType
Definition: NonlinearFactor.h:62
gtsam::NonlinearInequalityConstraint
Definition: NonlinearInequalityConstraint.h:30
gtsam::BoundingConstraint2::BoundingConstraint2
BoundingConstraint2(Key key1, Key key2, double threshold, bool isGreaterThan, double mu=1000.0)
flag for greater/less than
Definition: BoundingConstraint.h:123
Lie.h
Base class and basic functions for Lie types.
gtsam
traits
Definition: SFMdata.h:40
gtsam::BoundingConstraint1::key
Key key() const
Definition: BoundingConstraint.h:52
gtsam::BoundingConstraint2::value
virtual double value(const X1 &x1, const X2 &x2, OptionalMatrixType H1=OptionalNone, OptionalMatrixType H2=OptionalNone) const =0
gtsam::NoiseModelFactor
Definition: NonlinearFactor.h:198
gtsam::Values
Definition: Values.h:65
gtsam::Factor::keys
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition: Factor.h:143
gtsam::BoundingConstraint1::unwhitenedExpr
Vector unwhitenedExpr(const Values &x, OptionalMatrixVecType H={}) const override
Definition: BoundingConstraint.h:62
gtsam::BoundingConstraint1::X
VALUE X
Definition: BoundingConstraint.h:35
gtsam::OptionalMatrixType
Matrix * OptionalMatrixType
Definition: NonlinearFactor.h:56
NonlinearInequalityConstraint.h
Nonlinear inequality constraints in constrained optimization.
gtsam::BoundingConstraint2::evaluateError
Vector evaluateError(const X1 &x1, const X2 &x2, OptionalMatrixType H1={}, OptionalMatrixType H2={}) const
TODO: This should be deprecated.
Definition: BoundingConstraint.h:160
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::BoundingConstraint2::threshold_
double threshold_
Definition: BoundingConstraint.h:120
gtsam::BoundingConstraint2::X1
VALUE1 X1
Definition: BoundingConstraint.h:114
x2
Pose3 x2(Rot3::Ypr(0.0, 0.0, 0.0), l2)
gtsam::BoundingConstraint1::isGreaterThan_
bool isGreaterThan_
Definition: BoundingConstraint.h:40


gtsam
Author(s):
autogenerated on Wed Mar 19 2025 03:01:20