tools.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018, University of Edinburgh
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 met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without specific
15 // prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #include <cxxabi.h> // The demangler for gcc... this makes this system dependent!
31 #include <fstream>
32 #include <iostream>
33 #include <random>
34 #include <regex>
35 #include <typeinfo> // The run-time type information (RTTI) Functionality of C++
36 
38 #include <octomap/OcTree.h>
39 #include "exotica_core/tools.h"
40 
41 #include <ros/package.h>
42 
43 namespace exotica
44 {
45 std_msgs::ColorRGBA RandomColor()
46 {
47  std_msgs::ColorRGBA ret;
48  ret.a = 1.0;
49  std::random_device rd_;
50  std::mt19937 gen(rd_());
51  std::uniform_real_distribution<> dis(0.0, 1.0);
52  ret.r = static_cast<float>(dis(gen));
53  ret.g = static_cast<float>(dis(gen));
54  ret.b = static_cast<float>(dis(gen));
55  return ret;
56 }
57 
58 void SaveMatrix(std::string file_name,
59  const Eigen::Ref<const Eigen::MatrixXd> mat)
60 {
61  std::ofstream myfile;
62  myfile.open(file_name);
63  if (myfile.good())
64  {
65  myfile << mat;
66  myfile.close();
67  }
68  else
69  {
70  myfile.close();
71  ThrowPretty("Can't open file!");
72  }
73 }
74 
75 void LoadOBJ(const std::string& data, Eigen::VectorXi& tri,
76  Eigen::VectorXd& vert)
77 {
78  std::stringstream ss(data);
79  std::string line;
80  tri.resize(0, 1);
81  vert.resize(0, 1);
82  int vn = 0, tn = 0;
83  double v[3];
84  int vv[9];
85  while (std::getline(ss, line))
86  {
87  if (line.compare(0, 2, "v ") == 0)
88  {
89  vert.conservativeResize((vn + 1) * 3);
90  std::stringstream sss(line.substr(2));
91  sss >> v[0] >> v[1] >> v[2];
92  vert(vn * 3) = v[0];
93  vert(vn * 3 + 1) = v[1];
94  vert(vn * 3 + 2) = v[2];
95  ++vn;
96  }
97  else if (line.compare(0, 2, "f ") == 0)
98  {
99  std::stringstream sss(line.substr(2));
100  int i;
101  for (i = 0; i < 9 && sss >> vv[i]; ++i)
102  {
103  while (sss.peek() == '/' || sss.peek() == ' ')
104  sss.ignore();
105  }
106  if (i < 8)
107  {
108  ThrowPretty("Invalid format!");
109  }
110  tri.conservativeResize((tn + 1) * 3);
111  tri(tn * 3) = vv[0] - 1;
112  tri(tn * 3 + 1) = vv[3] - 1;
113  tri(tn * 3 + 2) = vv[6] - 1;
114  ++tn;
115  }
116  }
117 }
118 
119 std::shared_ptr<octomap::OcTree> LoadOctree(const std::string& file_path)
120 {
121  std::shared_ptr<octomap::OcTree> octree(new octomap::OcTree(file_path));
122  if (!octree) ThrowPretty("Could not load OcTree!");
123  return octree;
124 }
125 
126 std::shared_ptr<shapes::Shape> LoadOctreeAsShape(const std::string& file_path)
127 {
128  std::shared_ptr<octomap::OcTree> octree = LoadOctree(file_path);
129  std::shared_ptr<shapes::Shape> shape(new shapes::OcTree(octree));
130  return shape;
131 }
132 
133 std::string GetTypeName(const std::type_info& type)
134 {
135  int status;
136  std::string name;
137  char* temp;
138 
139  temp = abi::__cxa_demangle(type.name(), 0, 0, &status);
140  name = std::string(temp);
141  free(temp);
142  return name;
143 }
144 
145 std::string ParsePath(const std::string& path)
146 {
147  std::string ret = path;
148  std::smatch matches;
149  std::regex_search(ret, matches, std::regex("\\{([^\\}]+){1,}\\}"));
150  for (auto& match : matches)
151  {
152  std::string package = match.str();
153  if (package[0] == '{' || package == "") continue;
154  std::string package_path = ros::package::getPath(package);
155  if (package_path == "") ThrowPretty("Unknown package '" << package << "'");
156  try
157  {
158  ret = std::regex_replace(ret, std::regex("\\{" + package + "\\}"), package_path, std::regex_constants::match_any);
159  }
160  catch (const std::regex_error& e)
161  {
162  ThrowPretty("Package name resolution failed (regex error " << e.code() << ")");
163  }
164  }
165  std::regex_search(ret, matches, std::regex("package://([^\\/]+){1,}"));
166  for (auto& match : matches)
167  {
168  std::string package = match.str();
169  if (package.substr(0, 10) == "package://" || package == "") continue;
170  std::string package_path = ros::package::getPath(package);
171  if (package_path == "") ThrowPretty("Unknown package '" << package << "'");
172  try
173  {
174  ret = std::regex_replace(ret, std::regex("package://" + package + "/"), package_path + "/", std::regex_constants::match_any);
175  }
176  catch (const std::regex_error& e)
177  {
178  ThrowPretty("Package name resolution failed (regex error " << e.code() << ")");
179  }
180  }
181  return ret;
182 }
183 
184 std::string LoadFile(const std::string& path)
185 {
186  std::string file_name = ParsePath(path);
187  std::ifstream fstream(file_name);
188  if (!fstream) ThrowPretty("File does not exist '" << file_name << "'");
189  try
190  {
191  return std::string((std::istreambuf_iterator<char>(fstream)), std::istreambuf_iterator<char>());
192  }
193  catch (const std::ifstream::failure& e)
194  {
195  ThrowPretty("Can't read file '" << file_name << "'");
196  }
197 }
198 
199 bool PathExists(const std::string& path)
200 {
201  std::ifstream file(ParsePath(path));
202  return (bool)file;
203 }
204 } // namespace exotica
bool PathExists(const std::string &path)
Definition: tools.cpp:199
std::shared_ptr< octomap::OcTree > LoadOctree(const std::string &file_path)
Definition: tools.cpp:119
void LoadOBJ(const std::string &data, Eigen::VectorXi &tri, Eigen::VectorXd &vert)
LoadOBJ Loads mesh data from an OBJ file.
Definition: tools.cpp:75
string package
std_msgs::ColorRGBA RandomColor()
RandomColor Generates random opaque color.
Definition: tools.cpp:45
#define ThrowPretty(m)
Definition: exception.h:36
std::string LoadFile(const std::string &path)
Definition: tools.cpp:184
void SaveMatrix(std::string file_name, const Eigen::Ref< const Eigen::MatrixXd > mat)
Definition: tools.cpp:58
ROSLIB_DECL std::string getPath(const std::string &package_name)
std::string ParsePath(const std::string &path)
Definition: tools.cpp:145
std::shared_ptr< shapes::Shape > LoadOctreeAsShape(const std::string &file_path)
Definition: tools.cpp:126
std::string GetTypeName(const std::type_info &type)
Definition: tools.cpp:133


exotica_core
Author(s): Yiming Yang, Michael Camilleri
autogenerated on Sat Apr 10 2021 02:34:49