33 #include <OgreManualObject.h> 50 FILE* input = fopen( path.c_str(),
"r" );
53 ROS_ERROR(
"Could not open '%s' for read", path.c_str() );
72 fseek( input, 0, SEEK_END );
73 long fileSize = ftell( input );
76 std::vector<uint8_t> buffer_vec(fileSize);
77 uint8_t* buffer = &buffer_vec[0];
79 long num_bytes_read = fread( buffer, 1, fileSize, input );
80 if ( num_bytes_read != fileSize )
82 ROS_ERROR(
"STLLoader::load( \"%s\" ) only read %ld bytes out of total %ld.",
83 path.c_str(), num_bytes_read, fileSize);
89 return this->
load(buffer, num_bytes_read, path);
92 bool STLLoader::load(uint8_t* buffer,
const size_t num_bytes,
const std::string& origin)
95 std::string buffer_str = std::string(reinterpret_cast<char *>(buffer), num_bytes);
97 if (buffer_str.substr(0, 5) == std::string(
"solid"))
102 if (buffer_str.find(
"endsolid", 5) != std::string::npos)
105 "starts with the word 'solid' and also contains the " 106 "word 'endsolid', indicating that it's an ASCII STL " 107 "file, but rviz can only load binary STL files so it " 108 "will not be loaded. Please convert it to a " 114 ROS_WARN_STREAM(
"The STL file '" << origin <<
"' is malformed. It starts " 115 "with the word 'solid', indicating that it's an ASCII " 116 "STL file, but it does not contain the word 'endsolid' so " 117 "it is either a malformed ASCII STL file or it is actually " 118 "a binary STL file. Trying to interpret it as a binary " 119 "STL file instead.");
123 static const size_t binary_stl_header_len = 84;
124 if (num_bytes <= binary_stl_header_len)
127 "appears to be a binary STL file but does not contain " 128 "enough data for the 80 byte header and 32-bit integer " 134 unsigned int num_triangles = *(
reinterpret_cast<uint32_t *
>(buffer + 80));
135 static const size_t number_of_bytes_per_triangle = 50;
136 size_t expected_size = binary_stl_header_len + num_triangles * number_of_bytes_per_triangle;
137 if (num_bytes < expected_size)
140 "to the binary STL header it should have '" <<
141 num_triangles <<
"' triangles, but it has too little" <<
142 " data for that to be the case.");
145 else if (num_bytes > expected_size)
147 ROS_WARN_STREAM(
"The STL file '" << origin <<
"' is malformed. According " 148 "to the binary STL header it should have '" <<
149 num_triangles <<
"' triangles, but it has too much" <<
150 " data for that to be the case. The extra data will be" <<
160 uint8_t* pos = buffer;
164 unsigned int numTriangles = *(
unsigned int*)pos;
167 for (
unsigned int currentTriangle = 0; currentTriangle < numTriangles; ++currentTriangle )
205 if (tri.
normal_.squaredLength() < 0.001)
209 tri.
normal_ = side1.crossProduct(side2);
221 Ogre::Vector3 pos(vec);
223 u = acos( pos.y / pos.length() );
225 float val = pos.x / ( sin( u ) );
235 Ogre::ManualObject*
object =
new Ogre::ManualObject(
"the one and only" );
236 object->begin(
"BaseWhiteNoLighting", Ogre::RenderOperation::OT_TRIANGLE_LIST );
238 unsigned int vertexCount = 0;
239 V_Triangle::const_iterator it =
triangles_.begin();
240 V_Triangle::const_iterator end =
triangles_.end();
241 for (; it != end; ++it )
243 if( vertexCount >= 2004 )
248 object->begin(
"BaseWhiteNoLighting", Ogre::RenderOperation::OT_TRIANGLE_LIST );
259 object->textureCoord( u, v );
264 object->textureCoord( u, v );
269 object->textureCoord( u, v );
271 object->triangle( vertexCount + 0, vertexCount + 1, vertexCount + 2 );
278 Ogre::MeshPtr mesh =
object->convertToMesh( name, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME );
279 mesh->buildEdgeList();
#define ROS_WARN_STREAM(args)
#define ROS_ERROR_STREAM(args)