spa2d.h
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2009, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 //
36 // Sparse pose adjustment, 2d version
37 //
38 
39 #ifndef _SPA2D_H_
40 #define _SPA2D_H_
41 
42 #ifndef EIGEN_USE_NEW_STDVECTOR
43 #define EIGEN_USE_NEW_STDVECTOR
44 #endif // EIGEN_USE_NEW_STDVECTOR
45 
46 #include <stdio.h>
47 #include <iostream>
48 #include <Eigen/Core>
49 #include <Eigen/Geometry>
50 #include <Eigen/LU>
51 #ifndef EIGEN_USE_NEW_STDVECTOR
52 #define EIGEN_USE_NEW_STDVECTOR
53 #endif
54 #include <Eigen/StdVector>
55 #include <vector>
56 
57 // sparse Cholesky
59 // block jacobian pcg
61 
62 // Defines for methods to use with doSBA().
63 #define SBA_DENSE_CHOLESKY 0
64 #define SBA_SPARSE_CHOLESKY 1
65 #define SBA_GRADIENT 2
66 #define SBA_BLOCK_JACOBIAN_PCG 3
67 
68 
69 // put things into a namespace
70 namespace sba
71 {
72 
75 
93 
94  class Node2d
95  {
96  public:
97  EIGEN_MAKE_ALIGNED_OPERATOR_NEW // needed for 16B alignment
98 
100  int nodeId;
101 
103  Eigen::Matrix<double,3,1> trans; // homogeneous coordinates, last element is 1.0
104  double arot; // angle in radians, normalized to [-pi,+pi]
106  inline void normArot()
107  {
108  if (arot > M_PI) arot -= 2.0*M_PI;
109  if (arot < -M_PI) arot += 2.0*M_PI;
110  }
111 
113  Eigen::Matrix<double,2,3> w2n;
114  void setTransform();
115 
119  // Eigen::Matrix<double,3,3> covar;
120 
123  Eigen::Matrix2d dRdx;
124 
125  void setDr(); // set local angle derivatives
126 
128  bool isFixed;
129 
132  Eigen::Matrix<double,3,1> oldtrans; // homogeneous coordinates, last element is 1.0
133  double oldarot; // angle
134  };
135 
136 
140 
141  class Con2dP2
142  {
143  public:
144  EIGEN_MAKE_ALIGNED_OPERATOR_NEW // needed for 16B alignment
145 
147  int ndr;
148 
150  int nd1;
151 
153  Eigen::Vector2d tmean;
154  double amean;
155  Eigen::Matrix<double,3,3> prec;
156 
158  Eigen::Matrix<double,3,1> err;
160  inline double calcErr(const Node2d &nd0, const Node2d &nd1);
161 
163  double calcErrDist(const Node2d &nd0, const Node2d &nd1);
164 
165 
167  Eigen::Matrix<double,3,3> J0,J0t,J1,J1t;
168 
171  static constexpr double qScale = 1.0;
172 
175 
178 
182  void setJacobians(std::vector<Node2d,Eigen::aligned_allocator<Node2d> > &nodes);
183 
185  bool isValid;
186  };
187 
188 
189 
191 
192  class SysSPA2d
193  {
194  public:
195  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
196 
198  SysSPA2d() { nFixed = 1; verbose = false; lambda = 1.0e-4, print_iros_stats=false; }
199 
202  int addNode(const Vector3d &pos, int id);
203  void removeNode(int id);
204 
205  // add a constraint
206  // <nd0>, <nd1> are node id's
207  // <mean> is x,y,th, with th in radians
208  // <prec> is a 3x3 precision matrix (inverse covariance
209  bool addConstraint(int nd0, int nd1, const Vector3d &mean, const Matrix3d &prec);
210  bool removeConstraint(int ndi0, int ndi1);
211 
212 
214  std::vector<Node2d,Eigen::aligned_allocator<Node2d> > nodes;
215  std::vector<Node2d,Eigen::aligned_allocator<Node2d> > getNodes()
216  { return nodes; }
217 
219  std::vector< std::vector< Eigen::Vector2d, Eigen::aligned_allocator<Eigen::Vector2d> > > scans;
220 
222  int nFixed;
223 
225  std::vector<Con2dP2,Eigen::aligned_allocator<Con2dP2> > p2cons;
226 
229  double calcCost(bool tcost = false);
231  double calcCost(double dist);
232  double errcost;
233 
234 
236  void printStats();
237  void writeSparseA(char *fname, bool useCSparse = false); // save precision matrix in CSPARSE format
238 
241  void setupSys(double sLambda);
242  void setupSparseSys(double sLambda, int iter, int sparseType);
243 
248  double lambda;
249  int doSPA(int niter, double sLambda = 1.0e-4, int useCSparse = SBA_SPARSE_CHOLESKY, double initTol = 1.0e-8, int CGiters = 50);
250  int doSPAwindowed(int window, int niter, double sLambda, int useCSparse);
251 
252 
253 #ifdef SBA_DSIF
254  void doDSIF(int newnode);
256  void setupSparseDSIF(int newnode);
257 #endif
258 
260  double sqMinDelta;
261 
263  Eigen::MatrixXd A;
264  Eigen::VectorXd B;
265 
268 
270  void useCholmod(bool yes)
271  { csp.useCholmod = yes; }
272 
274  bool verbose;
276 
279  void getGraph(std::vector<float> &graph);
280  };
281 
282 
283 
284 
286 
288  bool read2dP2File(char *fname, SysSPA2d spa);
289 
290 
291 } // ends namespace sba
292 
293 #endif // _SPA2d_H_
sba::SysSPA2d::doSPA
int doSPA(int niter, double sLambda=1.0e-4, int useCSparse=SBA_SPARSE_CHOLESKY, double initTol=1.0e-8, int CGiters=50)
Definition: spa2d.cpp:425
sba::Con2dP2
Definition: spa2d.h:141
sba::CSparse2d
Definition: csparse.h:146
sba::SysSPA2d::lambda
double lambda
Definition: spa2d.h:248
sba::Con2dP2::tmean
Eigen::Vector2d tmean
Mean vector, quaternion (inverse) and precision matrix for this constraint.
Definition: spa2d.h:153
sba::SysSPA2d::csp
CSparse2d csp
sparse matrix object
Definition: spa2d.h:267
sba::Node2d::arot
double arot
Definition: spa2d.h:104
sba::SysSPA2d::verbose
bool verbose
if we want statistics
Definition: spa2d.h:274
sba::SysSPA2d::B
Eigen::VectorXd B
Definition: spa2d.h:264
sba::Node2d::w2n
Eigen::Matrix< double, 2, 3 > w2n
Resultant transform from world to node coordinates;.
Definition: spa2d.h:113
sba::SysSPA2d::useCholmod
void useCholmod(bool yes)
use CHOLMOD or CSparse
Definition: spa2d.h:270
bpcg.h
sba::SysSPA2d::addNode
int addNode(const Vector3d &pos, int id)
Definition: spa2d.cpp:207
sba::Con2dP2::J1
Eigen::Matrix< double, 3, 3 > J1
Definition: spa2d.h:167
sba::SysSPA2d::calcCost
double calcCost(bool tcost=false)
Definition: spa2d.cpp:173
sba::SysSPA2d::scans
std::vector< std::vector< Eigen::Vector2d, Eigen::aligned_allocator< Eigen::Vector2d > > > scans
set of point scans, corresponding to nodes
Definition: spa2d.h:219
sba::Con2dP2::amean
double amean
Definition: spa2d.h:154
sba::Node2d::dRdx
Eigen::Matrix2d dRdx
Definition: spa2d.h:123
sba::SysSPA2d::A
Eigen::MatrixXd A
linear system matrix and vector
Definition: spa2d.h:263
sba::Node2d::setTransform
void setTransform()
Definition: spa2d.cpp:63
sba::SysSPA2d::getNodes
std::vector< Node2d, Eigen::aligned_allocator< Node2d > > getNodes()
Definition: spa2d.h:215
sba::Node2d::isFixed
bool isFixed
For SPA, is this camera fixed or free?
Definition: spa2d.h:128
sba::SysSPA2d::writeSparseA
void writeSparseA(char *fname, bool useCSparse=false)
csparse.h
sba::SysSPA2d::addConstraint
bool addConstraint(int nd0, int nd1, const Vector3d &mean, const Matrix3d &prec)
Definition: spa2d.cpp:230
sba::Con2dP2::setJacobians
void setJacobians(std::vector< Node2d, Eigen::aligned_allocator< Node2d > > &nodes)
Definition: spa2d.cpp:86
sba::Con2dP2::J0t
Eigen::Matrix< double, 3, 3 > J0t
Definition: spa2d.h:167
sba::CSparse2d::useCholmod
bool useCholmod
Definition: csparse.h:189
sba::SysSPA2d::nFixed
int nFixed
Number of fixed nodes.
Definition: spa2d.h:222
sba::Con2dP2::err
Eigen::Matrix< double, 3, 1 > err
error
Definition: spa2d.h:158
sba::SysSPA2d::p2cons
std::vector< Con2dP2, Eigen::aligned_allocator< Con2dP2 > > p2cons
Set of P2 constraints.
Definition: spa2d.h:225
sba::Con2dP2::prec
Eigen::Matrix< double, 3, 3 > prec
Definition: spa2d.h:155
sba::read2dP2File
bool read2dP2File(char *fname, SysSPA2d spa)
constraint files
sba
Definition: bpcg.h:60
sba::Node2d::setDr
void setDr()
Definition: spa2d.cpp:76
sba::SysSPA2d::setupSparseSys
void setupSparseSys(double sLambda, int iter, int sparseType)
Definition: spa2d.cpp:328
sba::Con2dP2::J1t
Eigen::Matrix< double, 3, 3 > J1t
Definition: spa2d.h:167
sba::SysSPA2d::errcost
double errcost
Definition: spa2d.h:232
sba::SysSPA2d::SysSPA2d
EIGEN_MAKE_ALIGNED_OPERATOR_NEW SysSPA2d()
constructor
Definition: spa2d.h:198
sba::Con2dP2::qScale
static constexpr double qScale
Definition: spa2d.h:171
sba::Node2d
Definition: spa2d.h:94
SBA_SPARSE_CHOLESKY
#define SBA_SPARSE_CHOLESKY
Definition: spa2d.h:64
sba::SysSPA2d::print_iros_stats
bool print_iros_stats
Definition: spa2d.h:275
sba::Con2dP2::nd1
int nd1
Node2d index for the second node.
Definition: spa2d.h:150
sba::Con2dP2::calcErr
double calcErr(const Node2d &nd0, const Node2d &nd1)
calculates projection error and stores it in <err>
Definition: spa2d.cpp:148
sba::SysSPA2d::removeConstraint
bool removeConstraint(int ndi0, int ndi1)
sba::SysSPA2d
SysSPA2d holds a set of nodes and constraints for sparse pose adjustment.
Definition: spa2d.h:192
sba::SysSPA2d::sqMinDelta
double sqMinDelta
Convergence bound (square of minimum acceptable delta change)
Definition: spa2d.h:260
sba::SysSPA2d::setupSys
void setupSys(double sLambda)
Definition: spa2d.cpp:258
sba::Node2d::trans
Eigen::Matrix< double, 3, 1 > trans
6DOF pose as a unit quaternion and translation vector
Definition: spa2d.h:103
sba::SysSPA2d::doSPAwindowed
int doSPAwindowed(int window, int niter, double sLambda, int useCSparse)
Definition: spa2d.cpp:629
sba::Con2dP2::J0
Eigen::Matrix< double, 3, 3 > J0
jacobian with respect to frames; uses dR'/dq from Node2d calculation
Definition: spa2d.h:167
sba::Node2d::oldarot
double oldarot
Definition: spa2d.h:133
sba::SysSPA2d::removeNode
void removeNode(int id)
sba::SysSPA2d::printStats
void printStats()
print some system stats
sba::SysSPA2d::nodes
std::vector< Node2d, Eigen::aligned_allocator< Node2d > > nodes
set of nodes (camera frames) for SPA system, indexed by position;
Definition: spa2d.h:214
sba::Node2d::normArot
void normArot()
Normalize to [-pi,+pi].
Definition: spa2d.h:106
sba::Node2d::oldtrans
Eigen::Matrix< double, 3, 1 > oldtrans
Definition: spa2d.h:132
sba::Con2dP2::calcErrDist
double calcErrDist(const Node2d &nd0, const Node2d &nd1)
calculate error in distance only, no weighting
Definition: spa2d.cpp:163
sba::Node2d::nodeId
EIGEN_MAKE_ALIGNED_OPERATOR_NEW int nodeId
node id - somewhat redundant, but can be useful, e.g., in KARTO links
Definition: spa2d.h:100
sba::Con2dP2::isValid
bool isValid
valid or not (could be out of bounds)
Definition: spa2d.h:185
sba::Con2dP2::ndr
EIGEN_MAKE_ALIGNED_OPERATOR_NEW int ndr
Reference pose index.
Definition: spa2d.h:147
sba::SysSPA2d::getGraph
void getGraph(std::vector< float > &graph)
Definition: spa2d.cpp:972


sparse_bundle_adjustment
Author(s):
autogenerated on Wed Mar 2 2022 01:03:04