mesh_parser.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include <string>
19 #include <vector>
20 
21 #include <boost/filesystem.hpp>
22 #include <ros/ros.h>
23 #include <fstream>
24 
27 
28 #define MAX_NUM_MESHES 1
29 
36 int8_t MeshParser::read(std::vector<TriangleSupport>& tri_vec)
37 {
38  std::string file_path = this->file_path_;
39  if (!boost::filesystem::exists(this->file_path_))
40  {
41  file_path = resolveURI(this->file_path_);
42  }
43 
44  // Create an instance of the Importer class
45  Assimp::Importer importer;
46  const aiScene* scene = importer.ReadFile(file_path,
47  0);
48  if (!scene)
49  {
50  ROS_ERROR_STREAM("Assimp::Importer Error: " << importer.GetErrorString());
51  return -1;
52  }
53 
54  if (0 >= scene->mNumMeshes)
55  {
56  ROS_ERROR("Found no meshes. Check mesh file. Aborting ...");
57  return -2;
58  }
59 
60  if (MAX_NUM_MESHES < scene->mNumMeshes)
61  {
62  ROS_WARN("Found more than one mesh in mesh file. The current implementation can only process %d mesh!!!", static_cast<uint32_t>(MAX_NUM_MESHES));
63  }
64 
65  aiMesh** mesh = scene->mMeshes;
66  aiVector3D* vertex = mesh[0]->mVertices; // point to the first vertex
67 
68  ROS_DEBUG_STREAM("mesh[0]->mNumVertices: " << mesh[0]->mNumVertices);
69  ROS_DEBUG_STREAM("mesh[0]->mNumFaces: " << mesh[0]->mNumFaces); // num of faces == num of triangles in STL
70 
71  // now read in all the triangles
72  for (uint32_t i = 0; i < mesh[0]->mNumFaces; ++i)
73  {
74  // populate each point of the triangle
76  if (0 != this->toVec3f(i, vertex++, t.a)) // after processing the pointer will be increased to point to the next vertex!
77  {
78  break;
79  }
80 
81  if (0 != this->toVec3f(i, vertex++, t.b))
82  {
83  break;
84  }
85 
86  if (0 != this->toVec3f(i, vertex++, t.c))
87  {
88  break;
89  }
90 
91  tri_vec.push_back(t);
92  }
93 
94  return 0;
95 }
96 
104 int8_t MeshParser::toVec3f(uint32_t num_current_face, aiVector3D* vertex, fcl::Vec3f& out)
105 {
106  if (!vertex)
107  {
108  ROS_ERROR("No valid vertex found at face %d", num_current_face);
109  return -3;
110  }
111 
112  out = fcl::Vec3f((*vertex)[0], (*vertex)[1], (*vertex)[2]);
113  return 0;
114 }
115 
int8_t read(std::vector< TriangleSupport > &tri_vec)
Definition: mesh_parser.cpp:36
std::string file_path_
Definition: parser_base.hpp:30
#define ROS_WARN(...)
geometry_msgs::TransformStamped t
#define MAX_NUM_MESHES
Definition: mesh_parser.cpp:28
int8_t toVec3f(uint32_t num_current_face, aiVector3D *vertex, fcl::Vec3f &out)
#define ROS_DEBUG_STREAM(args)
const std::string resolveURI(const std::string &path)
#define ROS_ERROR_STREAM(args)
#define ROS_ERROR(...)


cob_obstacle_distance
Author(s): Marco Bezzon
autogenerated on Thu Apr 8 2021 02:39:47