Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <string>
00019 #include <vector>
00020
00021 #include <boost/filesystem.hpp>
00022 #include <ros/ros.h>
00023 #include <fstream>
00024
00025 #include "cob_obstacle_distance/parsers/stl_parser.hpp"
00026 #include "cob_obstacle_distance/helpers/helper_functions.hpp"
00027
00031 int8_t StlParser::read(std::vector<TriangleSupport>& tri_vec)
00032 {
00033 char header_info[80] = "";
00034 char nTri[4];
00035 uint32_t nTriLong;
00036
00037 std::string file_path = this->file_path_;
00038 if (!boost::filesystem::exists(this->file_path_))
00039 {
00040 file_path = resolveURI(this->file_path_);
00041 }
00042
00043 std::ifstream myFile(
00044 file_path.c_str(),
00045 std::ios::in | std::ios::binary);
00046
00047 if (!myFile)
00048 {
00049 ROS_ERROR_STREAM("Could not read file: " << file_path);
00050 return -1;
00051 }
00052
00053
00054 myFile.read(header_info, 80);
00055 ROS_DEBUG_STREAM("header: " << header_info);
00056
00057
00058 myFile.read(nTri, 4);
00059 nTriLong = *((uint32_t*) nTri);
00060 ROS_DEBUG_STREAM("Number of Triangles: " << nTriLong);
00061
00062
00063 for (int i = 0; i < nTriLong; i++)
00064 {
00065 char facet[50];
00066 if (myFile)
00067 {
00068
00069 myFile.read(facet, 50);
00070
00071
00072
00073
00074 TriangleSupport t;
00075 t.a = this->toVec3f(facet + 12);
00076 t.b = this->toVec3f(facet + 24);
00077 t.c = this->toVec3f(facet + 36);
00078
00079 tri_vec.push_back(t);
00080 }
00081 else
00082 {
00083 ROS_ERROR_STREAM("File handle is not valid anymore: " << file_path);
00084 return -2;
00085 }
00086 }
00087
00088 return 0;
00089 }
00090
00096 fcl::Vec3f StlParser::toVec3f(char* facet)
00097 {
00098 double x = this->toDouble(facet, 0);
00099 double y = this->toDouble(facet, 4);
00100 double z = this->toDouble(facet, 8);
00101
00102 fcl::Vec3f v3(x, y, z);
00103 return v3;
00104 }
00105
00112 double StlParser::toDouble(char* facet, uint8_t start_idx)
00113 {
00114 char f1[4] = { facet[start_idx],
00115 facet[start_idx + 1],
00116 facet[start_idx + 2],
00117 facet[start_idx + 3]};
00118 float f_val = *((float*) f1);
00119 double d_val = static_cast<double>(f_val);
00120 return d_val;
00121 }
00122