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  std::size_t line_no{1};
86  while (std::getline(ss, line))
87  {
88  // Vertex
89  if (line.compare(0, 2, "v ") == 0)
90  {
91  vert.conservativeResize((vn + 1) * 3);
92  std::stringstream sss(line.substr(2));
93  sss >> v[0] >> v[1] >> v[2];
94  vert(vn * 3) = v[0];
95  vert(vn * 3 + 1) = v[1];
96  vert(vn * 3 + 2) = v[2];
97  ++vn;
98  }
99  // Face
100  else if (line.compare(0, 2, "f ") == 0)
101  {
102  std::stringstream sss(line.substr(2));
103  int i;
104  for (i = 0; i < 9 && sss >> vv[i]; ++i)
105  {
106  while (sss.peek() == '/' || sss.peek() == ' ')
107  sss.ignore();
108  }
109  if (i < 8)
110  {
111  auto vv_eigen = Eigen::Map<Eigen::Matrix<int, 1, 9>>(&vv[0]);
112  ThrowPretty("Invalid OBJ format when reading line " << line_no << ": '" << line << "', parsed vv: " << vv_eigen);
113  }
114  tri.conservativeResize((tn + 1) * 3);
115  tri(tn * 3) = vv[0] - 1;
116  tri(tn * 3 + 1) = vv[3] - 1;
117  tri(tn * 3 + 2) = vv[6] - 1;
118  ++tn;
119  }
120  line_no++;
121  }
122 }
123 
124 std::shared_ptr<octomap::OcTree> LoadOctree(const std::string& file_path)
125 {
126  std::shared_ptr<octomap::OcTree> octree(new octomap::OcTree(file_path));
127  if (!octree) ThrowPretty("Could not load OcTree!");
128  return octree;
129 }
130 
131 std::shared_ptr<shapes::Shape> LoadOctreeAsShape(const std::string& file_path)
132 {
133  std::shared_ptr<octomap::OcTree> octree = LoadOctree(file_path);
134  std::shared_ptr<shapes::Shape> shape(new shapes::OcTree(octree));
135  return shape;
136 }
137 
138 std::string GetTypeName(const std::type_info& type)
139 {
140  int status;
141  std::string name;
142  char* temp;
143 
144  temp = abi::__cxa_demangle(type.name(), 0, 0, &status);
145  name = std::string(temp);
146  free(temp);
147  return name;
148 }
149 
150 std::string ParsePath(const std::string& path)
151 {
152  std::string ret = path;
153  std::smatch matches;
154  std::regex_search(ret, matches, std::regex("\\{([^\\}]+){1,}\\}"));
155  for (auto& match : matches)
156  {
157  std::string package = match.str();
158  if (package[0] == '{' || package == "") continue;
159  std::string package_path = ros::package::getPath(package);
160  if (package_path == "") ThrowPretty("Unknown package '" << package << "'");
161  try
162  {
163  ret = std::regex_replace(ret, std::regex("\\{" + package + "\\}"), package_path, std::regex_constants::match_any);
164  }
165  catch (const std::regex_error& e)
166  {
167  ThrowPretty("Package name resolution failed (regex error " << e.code() << ")");
168  }
169  }
170  std::regex_search(ret, matches, std::regex("package://([^\\/]+){1,}"));
171  for (auto& match : matches)
172  {
173  std::string package = match.str();
174  if (package.substr(0, 10) == "package://" || package == "") continue;
175  std::string package_path = ros::package::getPath(package);
176  if (package_path == "") ThrowPretty("Unknown package '" << package << "'");
177  try
178  {
179  ret = std::regex_replace(ret, std::regex("package://" + package + "/"), package_path + "/", std::regex_constants::match_any);
180  }
181  catch (const std::regex_error& e)
182  {
183  ThrowPretty("Package name resolution failed (regex error " << e.code() << ")");
184  }
185  }
186  return ret;
187 }
188 
189 std::string LoadFile(const std::string& path)
190 {
191  std::string file_name = ParsePath(path);
192  std::ifstream fstream(file_name);
193  if (!fstream) ThrowPretty("File does not exist '" << file_name << "'");
194  try
195  {
196  return std::string((std::istreambuf_iterator<char>(fstream)), std::istreambuf_iterator<char>());
197  }
198  catch (const std::ifstream::failure& e)
199  {
200  ThrowPretty("Can't read file '" << file_name << "'");
201  }
202 }
203 
204 bool PathExists(const std::string& path)
205 {
206  std::ifstream file(ParsePath(path));
207  return (bool)file;
208 }
209 } // namespace exotica
exotica::LoadOBJ
void LoadOBJ(const std::string &data, Eigen::VectorXi &tri, Eigen::VectorXd &vert)
LoadOBJ Loads mesh data from an OBJ file.
Definition: tools.cpp:75
ros::package::getPath
ROSLIB_DECL std::string getPath(const std::string &package_name)
exotica::RandomColor
std_msgs::ColorRGBA RandomColor()
RandomColor Generates random opaque color.
Definition: tools.cpp:45
exotica
Definition: collision_scene.h:46
OcTree.h
package
string package
octomap::OcTree
name
std::string name
exotica::LoadFile
std::string LoadFile(const std::string &path)
Definition: tools.cpp:189
exotica::SaveMatrix
void SaveMatrix(std::string file_name, const Eigen::Ref< const Eigen::MatrixXd > mat)
Definition: tools.cpp:58
shapes.h
exotica::ParsePath
std::string ParsePath(const std::string &path)
Definition: tools.cpp:150
shapes::OcTree
package.h
ThrowPretty
#define ThrowPretty(m)
Definition: exception.h:36
exotica::LoadOctreeAsShape
std::shared_ptr< shapes::Shape > LoadOctreeAsShape(const std::string &file_path)
Definition: tools.cpp:131
exotica::LoadOctree
std::shared_ptr< octomap::OcTree > LoadOctree(const std::string &file_path)
Definition: tools.cpp:124
exotica::PathExists
bool PathExists(const std::string &path)
Definition: tools.cpp:204
tools.h
exotica::GetTypeName
std::string GetTypeName(const std::type_info &type)
Definition: tools.cpp:138


exotica_core
Author(s): Yiming Yang, Michael Camilleri
autogenerated on Sun Jun 2 2024 02:58:18