optimizable_graph.h
Go to the documentation of this file.
00001 // g2o - General Graph Optimization
00002 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, H. Strasdat, W. Burgard
00003 // 
00004 // g2o is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU Lesser General Public License as published
00006 // by the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 // 
00009 // g2o is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 // 
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #ifndef _AIS_OPTIMIZABLE_GRAPH_HH_
00018 #define _AIS_OPTIMIZABLE_GRAPH_HH_
00019 
00020 #include <deque>
00021 #include <set>
00022 #include <iostream>
00023 #include <list>
00024 #include <limits>
00025 #include <cmath>
00026 
00027 #include "openmp_mutex.h"
00028 #include "hyper_graph.h"
00029 
00030 namespace g2o {
00031 
00032   class HyperGraphAction;
00033   struct SolverProperty;
00034 
00046   struct OptimizableGraph : public HyperGraph {
00047 
00048     enum ActionType {
00049       AT_PREITERATION, AT_POSTITERATION,
00050       AT_NUM_ELEMENTS, // keep as last element
00051     };
00052 
00053     typedef std::set<HyperGraphAction*>    HyperGraphActionSet;
00054 
00055     // forward declarations
00056     class Vertex;
00057     class Edge;
00058 
00063     struct Data : public HyperGraph::HyperGraphElement
00064     {
00065       virtual ~Data() {};
00067       virtual bool read(std::istream& is) = 0;
00069       virtual bool write(std::ostream& os) const = 0;
00070     };
00071 
00075     struct VertexIDCompare {
00076       bool operator() (const Vertex* v1, const Vertex* v2) const
00077       {
00078         return v1->id() < v2->id();
00079       }
00080     };
00081 
00085     struct EdgeIDCompare {
00086       bool operator() (const Edge* e1, const Edge* e2) const
00087       {
00088         return e1->internalId() < e2->internalId();
00089       }
00090     };
00091 
00093     typedef std::vector<OptimizableGraph::Vertex*>      VertexContainer;
00095     typedef std::vector<OptimizableGraph::Edge*>        EdgeContainer;
00096 
00100     class Vertex : public HyperGraph::Vertex {
00101       private:
00102         friend struct OptimizableGraph;
00103       public:
00104         Vertex();
00105 
00107         virtual Vertex* clone() const ;
00108 
00110         const Data* userData() const { return _userData; }
00111         Data* userData() { return _userData; }
00112 
00113         void setUserData(Data* obs) { _userData = obs;}
00114 
00115         virtual ~Vertex();
00116 
00118         virtual void setToOrigin() = 0;
00119 
00121         virtual const double& hessian(int i, int j) const = 0;
00122         virtual double& hessian(int i, int j) = 0;
00123         virtual double hessianDeterminant() const = 0;
00124         virtual double* hessianData() = 0;
00125 
00127         virtual void mapHessianMemory(double* d) = 0;
00128 
00133         virtual int copyB(double* b_) const = 0;
00134 
00136         virtual const double& b(int i) const = 0;
00137         virtual double& b(int i) = 0;
00139         virtual double* bData() = 0;
00140 
00144         virtual void clearQuadraticForm() = 0;
00145 
00150         virtual double solveDirect(double lambda=0) = 0;
00151 
00156         virtual bool setEstimateData(const double* estimate);
00157 
00162         virtual bool getEstimateData(double* estimate) const ;
00163 
00168         virtual int estimateDimension() const;
00169 
00170         
00175         virtual bool setMinimalEstimateData(const double* estimate);
00176 
00181         virtual bool getMinimalEstimateData(double* estimate) const ;
00182 
00187         virtual int minimalEstimateDimension() const;
00188 
00190         virtual void push() = 0;
00191 
00193         virtual void pop() = 0;
00194 
00196         virtual void discardTop() = 0;
00197 
00199         virtual int stackSize() const = 0;
00200 
00202         virtual void oplus(double* v) = 0;
00203 
00205         int tempIndex() const { return _tempIndex;}
00207         void setTempIndex(int ti) { _tempIndex = ti;}
00208 
00210         bool fixed() const {return _fixed;}
00212         void setFixed(bool fixed) { _fixed = fixed;}
00213 
00215         bool marginalized() const {return _marginalized;}
00217         void setMarginalized(bool marginalized) { _marginalized = marginalized;}
00218 
00220         int dimension() const { return _dimension;}
00221 
00223         void setId(int id) {_id = id;}
00224 
00226         void setColInHessian(int c) { _colInHessian = c;}
00228         int colInHessian() const {return _colInHessian;}
00229 
00230         const OptimizableGraph* graph() const {return _graph;}
00231 
00233         virtual void setUncertainty(double* c) = 0;
00235         virtual double* uncertaintyData() = 0;
00236 
00241         void lockQuadraticForm() { _quadraticFormMutex.lock();}
00245         void unlockQuadraticForm() { _quadraticFormMutex.unlock();}
00246 
00248         virtual bool read(std::istream& is) = 0;
00250         virtual bool write(std::ostream& os) const = 0;
00251 
00252       protected:
00253         OptimizableGraph* _graph;
00254         Data* _userData;
00255         int _tempIndex;
00256         bool _fixed;
00257         bool _marginalized;
00258         int _dimension;
00259         int _colInHessian;
00260         OpenMPMutex _quadraticFormMutex;
00261 
00262     };
00263     
00264     class Edge: public HyperGraph::Edge {
00265       private:
00266         friend struct OptimizableGraph;
00267       public:
00268         Edge();
00269         virtual Edge* clone() const;
00270 
00271         // computes the error of the edge and stores it in an internal structure
00272         virtual void computeError() = 0;
00273 
00276         virtual bool setMeasurementData(const double* m);
00277 
00280         virtual bool getMeasurementData(double* m) const;
00281 
00284         virtual int measurementDimension() const;
00285 
00290         virtual bool setMeasurementFromState();
00291 
00296         virtual void robustifyError() = 0;
00297 
00299         bool robustKernel() const { return _robustKernel;}
00300         void setRobustKernel(bool rk) { _robustKernel = rk;}
00301 
00303         double huberWidth() const { return _huberWidth;}
00304         void setHuberWidth(double hw) { _huberWidth = hw;}
00305 
00307         virtual const double* errorData() const = 0;
00308         virtual double* errorData() = 0;
00309 
00311         virtual const double* informationData() const = 0;
00312         virtual double* informationData() = 0;
00313 
00315         virtual double chi2() const = 0;
00316 
00323         virtual void constructQuadraticForm() = 0;
00324 
00333         virtual void mapHessianMemory(double* d, int i, int j, bool rowMajor) = 0;
00334 
00339         virtual void linearizeOplus() = 0;
00340 
00342         virtual void initialEstimate(const OptimizableGraph::VertexSet& from, OptimizableGraph::Vertex* to) = 0;
00343 
00349         virtual double initialEstimatePossible(const OptimizableGraph::VertexSet& from, OptimizableGraph::Vertex* to) { (void) from; (void) to; return -1.;}
00350 
00352         int level() const { return _level;}
00354         void setLevel(int l) { _level=l;}
00355 
00357         int dimension() const { return _dimension;}
00358 
00359         virtual Vertex* createFrom() {return 0;}
00360         virtual Vertex* createTo()   {return 0;}
00361 
00363         virtual bool read(std::istream& is) = 0;
00365         virtual bool write(std::ostream& os) const = 0;
00366 
00368         long long internalId() const { return _internalId;}
00369 
00370       protected:
00371         int _dimension;
00372         int _level;
00373         bool _robustKernel;
00374         double _huberWidth;
00375         long long _internalId;
00376 
00393         inline double sqrtOfHuberByNrm(double delta, double b) const
00394         {
00395           if (delta<b)
00396             return 1;
00397           return sqrt(2*b*fabs(delta) - b*b)/delta;
00398         }
00399 
00400     };
00401 
00403     inline Vertex* vertex(int id) { return reinterpret_cast<Vertex*>(HyperGraph::vertex(id));}
00404 
00406     inline const Vertex* vertex (int id) const{ return reinterpret_cast<const Vertex*>(HyperGraph::vertex(id));}
00407 
00409     OptimizableGraph();
00410     virtual ~OptimizableGraph();
00411 
00413     void addGraph(OptimizableGraph* g);
00414  
00419     virtual bool addVertex(OptimizableGraph::Vertex* v, Data* userData=0);
00420 
00426     virtual bool addEdge(OptimizableGraph::Edge* e);
00427 
00429     double chi2() const;
00430 
00432     int maxDimension() const;
00433 
00437     std::set<int> dimensions() const;
00438 
00443     virtual int optimize(int iterations, bool online=false);
00444 
00446     virtual void preIteration(int);
00448     virtual void postIteration(int);
00449 
00451     bool addPreIterationAction(HyperGraphAction* action);
00453     bool addPostIterationAction(HyperGraphAction* action);
00454 
00456     bool removePreIterationAction(HyperGraphAction* action);
00458     bool removePostIterationAction(HyperGraphAction* action);
00459 
00461     virtual void push();
00463     virtual void pop();
00465     virtual void discardTop();
00466 
00468     virtual bool load(std::istream& is, bool createEdges=true);
00469     bool load(const char* filename, bool createEdges=true);
00471     virtual bool save(std::ostream& os, int level = 0) const;
00473     bool save(const char* filename, int level = 0) const;
00474 
00476     bool saveSubset(std::ostream& os, HyperGraph::VertexSet& vset, int level = 0);
00477 
00479     bool saveSubset(std::ostream& os, HyperGraph::EdgeSet& eset);
00480 
00482     virtual void push(HyperGraph::VertexSet& vset);
00484     virtual void pop(HyperGraph::VertexSet& vset);
00486     virtual void discardTop(HyperGraph::VertexSet& vset);
00487 
00489     virtual void setFixed(HyperGraph::VertexSet& vset, bool fixed);
00490 
00496     void setRenamedTypesFromString(const std::string& types);
00497 
00503     bool isSolverSuitable(const SolverProperty& solverProperty, const std::set<int>& vertDims = std::set<int>()) const;
00504 
00505   protected:
00506       std::list<Vertex*> 
00507         _taintedList, 
00508         _partialList; 
00509       OptimizableGraph* _upperGraph, *_lowerGraph;
00510       std::map<std::string, std::string> _renamedTypesLookup;
00511       long long _nextEdgeId;
00512       std::vector<HyperGraphActionSet> _graphActions;
00513 
00514       // do not watch this. To be removed soon, or integrated in a nice way
00515       bool _edge_has_id;
00516   };
00517   
00522 } // end namespace
00523 
00524 #endif


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:32:02