Skip to content

Commit

Permalink
fix loading from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
RainerKuemmerle committed Apr 6, 2017
1 parent 2f0c167 commit 2e35669
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 19 deletions.
14 changes: 10 additions & 4 deletions g2o/apps/g2o_viewer/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,17 @@ void MainWindow::updateDisplayedSolvers()

bool MainWindow::load(const QString& filename)
{
ifstream ifs(filename.toStdString().c_str());
if (! ifs)
return false;
viewer->graph->clear();
bool loadStatus = viewer->graph->load(ifs);
bool loadStatus = false;
if (filename == "-") {
cerr << "reading stdin" << endl;
loadStatus = viewer->graph->load(cin);
} else {
ifstream ifs(filename.toStdString().c_str());
if (! ifs)
return false;
loadStatus = viewer->graph->load(ifs);
}
if (! loadStatus)
return false;
_lastSolver = -1;
Expand Down
6 changes: 6 additions & 0 deletions g2o/apps/g2o_viewer/run_g2o_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class SleepThread : public QThread
int RunG2OViewer::run(int argc, char** argv, CommandArgs& arg)
{
std::string inputFilename;
std::string loadLookup;
arg.param("renameTypes", loadLookup, "", "create a lookup for loading types into other types,\n\t TAG_IN_FILE=INTERNAL_TAG_FOR_TYPE,TAG2=INTERNAL2\n\t e.g., VERTEX_CAM=VERTEX_SE3:EXPMAP");
arg.paramLeftOver("graph-input", inputFilename, "", "graph file which will be processed", true);
arg.parseArgs(argc, argv);

Expand All @@ -65,6 +67,10 @@ int RunG2OViewer::run(int argc, char** argv, CommandArgs& arg)

// setting up the optimizer
SparseOptimizer* optimizer = new SparseOptimizer();
// Loading the input data
if (loadLookup.size() > 0) {
optimizer->setRenamedTypesFromString(loadLookup);
}
mw.viewer->graph = optimizer;

// set up the GUI action
Expand Down
5 changes: 4 additions & 1 deletion g2o/core/factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ namespace g2o {
#ifdef G2O_DEBUG_FACTORY
std::cout << __FUNCTION__ << ": Registering " << _name << " of type " << typeid(T).name() << std::endl;
#endif
Factory::instance()->registerType(_name, new HyperGraphElementCreator<T>());
_creator = new HyperGraphElementCreator<T>();
Factory::instance()->registerType(_name, _creator);
}

~RegisterTypeProxy()
Expand All @@ -145,10 +146,12 @@ namespace g2o {
std::cout << __FUNCTION__ << ": Unregistering " << _name << " of type " << typeid(T).name() << std::endl;
#endif
Factory::instance()->unregisterType(_name);
delete _creator;
}

private:
std::string _name;
HyperGraphElementCreator<T>* _creator;
};

#if defined _MSC_VER && defined G2O_SHARED_LIBS
Expand Down
7 changes: 4 additions & 3 deletions g2o/core/hyper_graph_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,10 @@ namespace g2o {

HyperGraphActionLibrary::~HyperGraphActionLibrary()
{
for (HyperGraphElementAction::ActionMap::iterator it = _actionMap.begin(); it != _actionMap.end(); ++it) {
delete it->second;
}
// memory is freed by Proxy
//for (HyperGraphElementAction::ActionMap::iterator it = _actionMap.begin(); it != _actionMap.end(); ++it) {
//delete it->second;
//}
}

HyperGraphElementAction* HyperGraphActionLibrary::actionByName(const std::string& name)
Expand Down
41 changes: 30 additions & 11 deletions g2o/core/optimizable_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,25 +370,16 @@ void OptimizableGraph::discardTop(HyperGraph::VertexSet& vset)
}
}

void OptimizableGraph::setFixed(HyperGraph::VertexSet& vset, bool fixed)
void OptimizableGraph::setFixed(HyperGraph::VertexSet& vset, bool fixed)
{
for (HyperGraph::VertexSet::iterator it=vset.begin(); it!=vset.end(); ++it) {
OptimizableGraph::Vertex* v = static_cast<OptimizableGraph::Vertex*>(*it);
v->setFixed(fixed);
}
}


bool OptimizableGraph::load(istream& is, bool createEdges)
{
// scna for the paramers in the whole file
if (!_parameters.read(is,&_renamedTypesLookup))
return false;
#ifndef NDEBUG
cerr << "Loaded " << _parameters.size() << " parameters" << endl;
#endif
is.clear();
is.seekg(ios_base::beg);
set<string> warnedUnknownTypes;
stringstream currentLine;
string token;
Expand All @@ -398,7 +389,10 @@ bool OptimizableGraph::load(istream& is, bool createEdges)
elemBitset[HyperGraph::HGET_PARAMETER] = 1;
elemBitset.flip();

HyperGraph::DataContainer* previousDataContainer = 0;
HyperGraph::GraphElemBitset elemParamBitset;
elemParamBitset[HyperGraph::HGET_PARAMETER] = 1;

HyperGraph::DataContainer* previousDataContainer = 0;
Data* previousData = 0;

int lineNumber = 0;
Expand Down Expand Up @@ -450,6 +444,26 @@ bool OptimizableGraph::load(istream& is, bool createEdges)
continue;
}

// first handle the parameters
HyperGraph::HyperGraphElement* pelement = factory->construct(token, elemParamBitset);
if (pelement) { // not a parameter or otherwise unknown tag
assert(pelement->elementType() == HyperGraph::HGET_PARAMETER && "Should be a param");
Parameter* p = static_cast<Parameter*>(pelement);
int pid;
currentLine >> pid;
p->setId(pid);
bool r = p->read(currentLine);
if (! r) {
cerr << __PRETTY_FUNCTION__ << ": Error reading data " << token << " for parameter " << pid << endl;
delete p;
} else {
if (! _parameters.addParameter(p) ){
cerr << __PRETTY_FUNCTION__ << ": Parameter of type:" << token << " id:" << pid << " already defined" << endl;
}
}
continue;
}

HyperGraph::HyperGraphElement* element = factory->construct(token, elemBitset);
if (dynamic_cast<Vertex*>(element)) { // it's a vertex type
//cerr << "it is a vertex" << endl;
Expand Down Expand Up @@ -623,6 +637,10 @@ bool OptimizableGraph::load(istream& is, bool createEdges)
}
} // while read line

#ifndef NDEBUG
cerr << "Loaded " << _parameters.size() << " parameters" << endl;
#endif

return true;
}

Expand All @@ -646,6 +664,7 @@ bool OptimizableGraph::save(const char* filename, int level) const

bool OptimizableGraph::save(ostream& os, int level) const
{
// write the parameters to the top of the file
if (! _parameters.write(os))
return false;
set<Vertex*, VertexIDCompare> verticesToSave;
Expand Down

0 comments on commit 2e35669

Please # to comment.