stl_parser.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
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     // read 80 byte header
00054     myFile.read(header_info, 80);
00055     ROS_DEBUG_STREAM("header: " << header_info);
00056 
00057     // read 4-byte ulong
00058     myFile.read(nTri, 4);
00059     nTriLong = *((uint32_t*) nTri);
00060     ROS_DEBUG_STREAM("Number of Triangles: " << nTriLong);
00061 
00062     // now read in all the triangles
00063     for (int i = 0; i < nTriLong; i++)
00064     {
00065         char facet[50];
00066         if (myFile)
00067         {
00068             // read one 50-byte triangle
00069             myFile.read(facet, 50);
00070 
00071             // populate each point of the triangle
00072             // facet + 12 skips the triangle's unit normal
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 


cob_obstacle_distance
Author(s): Marco Bezzon
autogenerated on Thu Jun 6 2019 21:19:14