OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
PathTreeExport.cpp
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 // Internal Includes
26 #include <osvr/ClientKit/Context.h>
29 #include <osvr/Common/PathNode.h>
35 #include <osvr/Common/ParseAlias.h>
36 #include <osvr/Util/Verbosity.h>
37 #include "GraphOutputInterface.h"
38 
39 // Library/third-party includes
40 #include <boost/program_options.hpp>
41 #include <boost/variant.hpp>
42 
43 // Standard includes
44 #include <iostream>
45 #include <fstream>
46 
47 struct Options {
48  std::string graphOutputType = "dot";
49  bool fullPaths = false;
50  bool showAliases = false;
51  bool showTree = true;
52 };
53 int osvrToStream(std::ostream &os, Options const &opts) {
54  osvr::clientkit::ClientContext context("com.osvr.tools.pathtreexport");
55 
57  osvr::common::PathTree pathTree;
58  osvr::common::clonePathTree(context.get()->getPathTree(), pathTree);
59 
62  {
64  os, opts.graphOutputType);
65  using namespace osvr::common;
66 
68  bool fullPaths = opts.fullPaths;
70  pathTree.getRoot(),
71  [&graph, &pathTree, fullPaths](osvr::common::PathNode const &node) {
72  auto parent = node.getParent();
73  if (nullptr == parent) {
74  return;
75  }
76  auto fullPath = getFullPath(node);
77  auto &graphNode = graph->addNode(
78  fullPaths ? fullPath : ("/" + node.getName()), fullPath,
79  getTypeName(node));
80  });
81 
82  if (opts.showTree) {
84  graph->enableTreeOrganization();
86  pathTree.getRoot(), [&graph, &pathTree, fullPaths](
87  osvr::common::PathNode const &node) {
88  auto parent = node.getParent();
89  if (nullptr == parent) {
90  return;
91  }
92  auto fullPath = getFullPath(node);
93  auto &graphNode = graph->getNode(getFullPath(node));
94  graph->addEdge(graph->getNode(getFullPath(*parent)),
95  graphNode, "child");
96  });
97  }
98  if (opts.showAliases) {
101  pathTree.getRoot(),
102  [&graph, &pathTree](osvr::common::PathNode const &node) {
103  auto alias =
104  boost::get<elements::AliasElement>(&node.value());
105  if (nullptr == alias) {
106  return;
107  }
108  auto fullPath = getFullPath(node);
109  ParsedAlias parsed(alias->getSource());
110  auto &graphNode = graph->getNode(fullPath);
111 
112  PathNode &source = pathTree.getNodeByPath(parsed.getLeaf());
113  graph->addEdge(
114  graphNode, graph->getNode(getFullPath(source)), "alias",
115  parsed.isSimple() ? std::string() : parsed.getAlias());
116  });
117  }
118  }
119  return 0;
120 }
121 
122 int main(int argc, char *argv[]) {
123  std::ostream *stream = &std::cout;
124  Options opt;
125  std::string type;
126  {
127  namespace po = boost::program_options;
128  // clang-format off
129  po::options_description desc("Options");
130  desc.add_options()
131  ("help,h", "produce help message")
132  ("output,O", po::value<std::string>(), "output file (defaults to standard out)")
133  //("type,T", po::value<std::string>(&opt.graphOutputType)->default_value("dot"), "output data format (defaults to graphviz/dot)")
134  ("show-tree,t", po::value<bool>(&opt.showTree)->default_value(true), "Whether or not to show the path tree structure")
135  ("show-aliases,a", po::value<bool>(&opt.showAliases)->default_value(false), "Whether or not to show the alias links")
136  ("full-paths,p", po::value<bool>(&opt.fullPaths)->default_value(false), "Whether or not to use a node's full path as its label")
137  ;
138  // clang-format on
139  po::variables_map vm;
140  bool usage = false;
141  try {
142  po::store(po::command_line_parser(argc, argv).options(desc).run(),
143  vm);
144  po::notify(vm);
145  } catch (std::exception &e) {
146  std::cerr << "\nError parsing command line: " << e.what()
147  << std::endl;
148  usage = true;
149  }
150  if (usage || vm.count("help")) {
151  std::cerr
152  << "\nTraverses the path tree and outputs it as input data "
153  "for a graph software package\n";
154  std::cerr << "Usage: " << argv[0] << " [options]\n";
155  std::cerr << desc << "\n";
156  return 1;
157  }
158  std::ofstream file;
159  if (vm.count("output")) {
160  file.open(vm["output"].as<std::string>().c_str());
161  if (!file) {
162  std::cerr << "Error opening output file." << std::endl;
163  return 1;
164  }
165  stream = &file;
166  }
167  return osvrToStream(*stream, opt);
168  }
169 }
Header.
Handles spatial transformations.
Client context object: Create and keep one in your application. Handles lifetime management and provi...
Definition: Context_decl.h:57
A tree representation, with path/url syntax, of the known OSVR system.
Definition: PathTree.h:43
::osvr::util::TreeNode< PathElement > PathNode
The specific tree node type that contains a path element.
Definition: PathNode_fwd.h:42
Header including PathTree.h and all additional headers needed to define related types.
Header.
std::string getFullPath(PathNode const &node)
Gets the absolute path for the given node.
Definition: PathNode.cpp:42
void traverseWith(T &node, F &&functor)
A method to handle visiting every node in a tree with a lambda or other by-value function object...
static osvr::unique_ptr< GraphOutputInterface > createGraphOutputInterface(std::ostream &os, std::string const &type)
Factory method.
osvr::common::PathTree const & getPathTree() const
Accessor for the path tree.
OSVR_ClientContext get()
Gets the bare OSVR_ClientContext.
Definition: Context.h:109
int osvrToStream(std::ostream &os, Options const &opts)
const char * getTypeName(PathNode const &node)
Gets an identifying string for the node value type.
Definition: PathNode.cpp:39
std::vector< std::string > resolveFullTree(PathTree &tree)
Traverse the given path tree, resolving all aliases found to fully populate any generated sensor targ...
Internal, configured header file for verbosity macros.
Header.