stl_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 
31 int8_t StlParser::read(std::vector<TriangleSupport>& tri_vec)
32 {
33  char header_info[80] = "";
34  char nTri[4];
35  uint32_t nTriLong;
36 
37  std::string file_path = this->file_path_;
38  if (!boost::filesystem::exists(this->file_path_))
39  {
40  file_path = resolveURI(this->file_path_);
41  }
42 
43  std::ifstream myFile(
44  file_path.c_str(),
45  std::ios::in | std::ios::binary);
46 
47  if (!myFile)
48  {
49  ROS_ERROR_STREAM("Could not read file: " << file_path);
50  return -1;
51  }
52 
53  // read 80 byte header
54  myFile.read(header_info, 80);
55  ROS_DEBUG_STREAM("header: " << header_info);
56 
57  // read 4-byte ulong
58  myFile.read(nTri, 4);
59  nTriLong = *((uint32_t*) nTri);
60  ROS_DEBUG_STREAM("Number of Triangles: " << nTriLong);
61 
62  // now read in all the triangles
63  for (int i = 0; i < nTriLong; i++)
64  {
65  char facet[50];
66  if (myFile)
67  {
68  // read one 50-byte triangle
69  myFile.read(facet, 50);
70 
71  // populate each point of the triangle
72  // facet + 12 skips the triangle's unit normal
73 
75  t.a = this->toVec3f(facet + 12);
76  t.b = this->toVec3f(facet + 24);
77  t.c = this->toVec3f(facet + 36);
78 
79  tri_vec.push_back(t);
80  }
81  else
82  {
83  ROS_ERROR_STREAM("File handle is not valid anymore: " << file_path);
84  return -2;
85  }
86  }
87 
88  return 0;
89 }
90 
96 fcl::Vec3f StlParser::toVec3f(char* facet)
97 {
98  double x = this->toDouble(facet, 0);
99  double y = this->toDouble(facet, 4);
100  double z = this->toDouble(facet, 8);
101 
102  fcl::Vec3f v3(x, y, z);
103  return v3;
104 }
105 
112 double StlParser::toDouble(char* facet, uint8_t start_idx)
113 {
114  char f1[4] = { facet[start_idx],
115  facet[start_idx + 1],
116  facet[start_idx + 2],
117  facet[start_idx + 3]};
118  float f_val = *((float*) f1);
119  double d_val = static_cast<double>(f_val);
120  return d_val;
121 }
122 
fcl::Vec3f toVec3f(char *facet)
Definition: stl_parser.cpp:96
double toDouble(char *facet, uint8_t start_idx)
Definition: stl_parser.cpp:112
std::string file_path_
Definition: parser_base.hpp:30
geometry_msgs::TransformStamped t
int8_t read(std::vector< TriangleSupport > &tri_vec)
Definition: stl_parser.cpp:31
#define ROS_DEBUG_STREAM(args)
const std::string resolveURI(const std::string &path)
#define ROS_ERROR_STREAM(args)


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