utils.cpp
Go to the documentation of this file.
3 // #include <assimp/Exporter.hpp>
5 
8 #include <tesseract_urdf/utils.h>
9 
10 namespace tesseract_urdf
11 {
12 std::string toString(const double& float_value, const int precision)
13 {
14  std::stringstream sstring;
15  sstring.precision(precision);
16  sstring << float_value;
17  return sstring.str();
18 }
19 
20 std::string trailingSlash(const std::string& path)
21 {
22  std::string ret;
23  if (path.empty())
24  ret = "/";
25  else
26  {
27  if (path.back() == '/')
28  ret = path;
29  else
30  ret = path + "/";
31  }
32  return ret;
33 }
34 
35 std::string noTrailingSlash(const std::string& path)
36 {
37  std::string ret = path;
38  while (!ret.empty() && (ret.back() == '/' || ret.back() == '\\'))
39  {
40  ret = ret.substr(0, ret.size() - 1);
41  }
42  return ret;
43 }
44 
45 std::string noLeadingSlash(const std::string& filename)
46 {
47  std::string ret = filename;
48  while (!ret.empty() && (ret.front() == '/' || ret.front() == '\\'))
49  {
50  ret = ret.substr(1); // from second char to end
51  }
52  return ret;
53 }
54 
55 std::string makeURDFFilePath(const std::string& package_path, const std::string& filename)
56 {
57  std::string ret;
58  if (!package_path.empty())
59  {
60  // Use a package-relative path if a package was specified
61 
62  // Extract package name
63  std::string package_name = noTrailingSlash(package_path);
64  package_name = package_name.substr(package_name.find_last_of("/\\") + 1);
65 
66  // Set the path to the file
67  ret = "package://" + trailingSlash(package_name) + noLeadingSlash(filename);
68  }
69  else
70  {
71  // Use an absolute path if no package was specified
72  ret = filename;
73  }
74  return ret;
75 }
76 
77 /*
78 aiScene createAssetFromMesh(const std::shared_ptr<const tesseract_geometry::PolygonMesh>& mesh)
79 {
80  // Create an assimp scene
81  aiScene scene;
82  scene.mRootNode = new aiNode();
83 
84  // Make space for and create a default material
85  scene.mMaterials = new aiMaterial*[1];
86  scene.mMaterials[0] = new aiMaterial();
87  scene.mNumMaterials = 1;
88 
89  // Make space for and create a mesh
90  scene.mMeshes = new aiMesh*[1];
91  scene.mMeshes[0] = new aiMesh();
92  scene.mMeshes[0]->mMaterialIndex = 0;
93  scene.mNumMeshes = 1;
94 
95  // Register the mesh as part of the scene root node
96  scene.mRootNode->mMeshes = new unsigned int[1];
97  scene.mRootNode->mMeshes[0] = 0;
98  scene.mRootNode->mNumMeshes = 1;
99 
100  // Transcribe in the mesh vertices
101  scene.mMeshes[0]->mVertices = new aiVector3D[static_cast<unsigned long>(mesh->getVertexCount())];
102  scene.mMeshes[0]->mNumVertices = static_cast<unsigned int>(mesh->getVertexCount());
103 
104  for (std::size_t i = 0; i < static_cast<std::size_t>(mesh->getVertexCount()); ++i)
105  {
106  scene.mMeshes[0]->mVertices[i] = aiVector3D(static_cast<float>(mesh->getVertices()->at(i).x()),
107  static_cast<float>(mesh->getVertices()->at(i).y()),
108  static_cast<float>(mesh->getVertices()->at(i).z()));
109  }
110 
111  // Transcribe in the mesh triangles
112  scene.mMeshes[0]->mFaces = new aiFace[static_cast<unsigned long>(mesh->getFaceCount())];
113  scene.mMeshes[0]->mNumFaces = static_cast<unsigned int>(mesh->getFaceCount());
114 
115  int indices = 0;
116  for (std::size_t i = 0; i < static_cast<std::size_t>(mesh->getFaceCount()) && indices < mesh->getFaces()->size(); ++i)
117  {
118  // Find and set the number of vertices for this face
119  int num_vertices = (*mesh->getFaces())(indices);
120  scene.mMeshes[0]->mFaces[i].mIndices = new unsigned int[static_cast<unsigned long>(num_vertices)];
121  scene.mMeshes[0]->mFaces[i].mNumIndices = static_cast<unsigned int>(num_vertices);
122 
123  // Copy over the index pointing to each vertex
124  for (int j = 1; j <= num_vertices; ++j)
125  scene.mMeshes[0]->mFaces[i].mIndices[j - 1] = static_cast<unsigned int>((*mesh->getFaces())(indices + j));
126 
127  // Move along all the vertex indices just applied
128  indices += num_vertices;
129 
130  // And move one more to get to the next face-size-indicator
131  ++indices;
132  }
133 
134  // mTextureCoords? mNumUVComponents?
135 
136  return scene;
137 }
138 */
139 
140 void writeMeshToFile(const std::shared_ptr<const tesseract_geometry::PolygonMesh>& mesh, const std::string& filepath)
141 {
143  filepath, *(mesh->getVertices()), *(mesh->getFaces()), mesh->getFaceCount()))
144  std::throw_with_nested(std::runtime_error("Could not export file"));
145 
146  /* Option to use the Assimp code if errors are resolved.
147  aiScene scene = createAssetFromMesh(mesh);
148  Assimp::Exporter exporter;
149 
150  aiReturn ret = exporter.Export(&scene, "pFormatId", filepath.c_str());
151 
152  if (ret != AI_SUCCESS)
153  std::throw_with_nested(std::runtime_error("Could not export file"));
154  */
155 }
156 
157 } // namespace tesseract_urdf
tesseract_urdf::makeURDFFilePath
std::string makeURDFFilePath(const std::string &package_path, const std::string &filename)
Definition: utils.cpp:55
tesseract_collision::writeSimplePlyFile
bool writeSimplePlyFile(const std::string &path, const tesseract_common::VectorVector3d &vertices, const Eigen::VectorXi &faces, int num_faces)
tesseract_urdf::trailingSlash
std::string trailingSlash(const std::string &path)
Definition: utils.cpp:20
common.h
tesseract_urdf::noTrailingSlash
std::string noTrailingSlash(const std::string &path)
Definition: utils.cpp:35
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
polygon_mesh.h
utils.h
TESSERACT_COMMON_IGNORE_WARNINGS_POP
tesseract_urdf::toString
std::string toString(const double &float_value, int precision=3)
Definition: utils.cpp:12
tesseract_urdf::writeMeshToFile
void writeMeshToFile(const std::shared_ptr< const tesseract_geometry::PolygonMesh > &mesh, const std::string &filepath)
Definition: utils.cpp:140
macros.h
tesseract_urdf
Definition: box.h:43
tesseract_urdf::noLeadingSlash
std::string noLeadingSlash(const std::string &filename)
Definition: utils.cpp:45


tesseract_urdf
Author(s): Levi Armstrong
autogenerated on Mon Apr 21 2025 03:01:38