g2o
Loading...
Searching...
No Matches
optimization_algorithm_factory.cpp
Go to the documentation of this file.
1// g2o - General Graph Optimization
2// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright notice,
10// this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the distribution.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
28
29#include <cassert>
30#include <iostream>
31#include <typeinfo>
32
33#include "g2o/stuff/logger.h"
34
35using namespace std;
36
37namespace g2o {
38
42
43std::unique_ptr<OptimizationAlgorithmFactory>
45
52
54 const std::shared_ptr<AbstractOptimizationAlgorithmCreator>& c) {
55 const string& name = c->property().name;
56 CreatorList::iterator foundIt = findSolver(name);
57 if (foundIt != _creator.end()) {
58 _creator.erase(foundIt);
59 G2O_WARN("SOLVER FACTORY: Overwriting Solver creator {}", name);
60 assert(0);
61 }
62 _creator.push_back(c);
63}
64
66 const std::shared_ptr<AbstractOptimizationAlgorithmCreator>& c) {
67 const string& name = c->property().name;
68 CreatorList::iterator foundIt = findSolver(name);
69 if (foundIt != _creator.end()) {
70 _creator.erase(foundIt);
71 }
72}
73
75 const std::string& name,
76 OptimizationAlgorithmProperty& solverProperty) const {
77 CreatorList::const_iterator foundIt = findSolver(name);
78 if (foundIt != _creator.end()) {
79 solverProperty = (*foundIt)->property();
80 return (*foundIt)->construct();
81 }
82 G2O_WARN("SOLVER FACTORY: Unable to create solver {}", name);
83 return nullptr;
84}
85
87 std::unique_ptr<OptimizationAlgorithmFactory> aux;
88 factoryInstance.swap(aux);
89}
90
91void OptimizationAlgorithmFactory::listSolvers(std::ostream& os) const {
92 size_t solverNameColumnLength = 0;
93 for (CreatorList::const_iterator it = _creator.begin(); it != _creator.end();
94 ++it)
95 solverNameColumnLength =
96 std::max(solverNameColumnLength, (*it)->property().name.size());
97 solverNameColumnLength += 4;
98
99 for (CreatorList::const_iterator it = _creator.begin(); it != _creator.end();
100 ++it) {
101 const OptimizationAlgorithmProperty& sp = (*it)->property();
102 os << sp.name;
103 for (size_t i = sp.name.size(); i < solverNameColumnLength; ++i) os << ' ';
104 os << sp.type << " \t" << sp.desc << endl;
105 }
106}
107
108OptimizationAlgorithmFactory::CreatorList::const_iterator
109OptimizationAlgorithmFactory::findSolver(const std::string& name) const {
110 for (CreatorList::const_iterator it = _creator.begin(); it != _creator.end();
111 ++it) {
112 const OptimizationAlgorithmProperty& sp = (*it)->property();
113 if (sp.name == name) return it;
114 }
115 return _creator.end();
116}
117
118OptimizationAlgorithmFactory::CreatorList::iterator
120 for (CreatorList::iterator it = _creator.begin(); it != _creator.end();
121 ++it) {
122 const OptimizationAlgorithmProperty& sp = (*it)->property();
123 if (sp.name == name) return it;
124 }
125 return _creator.end();
126}
127
128} // namespace g2o
AbstractOptimizationAlgorithmCreator(const OptimizationAlgorithmProperty &p)
create solvers based on their short name
void registerSolver(const std::shared_ptr< AbstractOptimizationAlgorithmCreator > &c)
CreatorList::const_iterator findSolver(const std::string &name) const
OptimizationAlgorithm * construct(const std::string &tag, OptimizationAlgorithmProperty &solverProperty) const
static OptimizationAlgorithmFactory * instance()
return the instance
static std::unique_ptr< OptimizationAlgorithmFactory > factoryInstance
void listSolvers(std::ostream &os) const
list the known solvers into a stream
void unregisterSolver(const std::shared_ptr< AbstractOptimizationAlgorithmCreator > &c)
Generic interface for a non-linear solver operating on a graph.
#define G2O_WARN(...)
Definition logger.h:88
Definition jet.h:876
std::string type
type of solver, e.g., "CSparse Cholesky", "PCG"
std::string desc
short description of the solver
std::string name
name of the solver, e.g., var