33 #include <OgreManualObject.h> 47 FILE* input = fopen(path.c_str(),
"r");
50 ROS_ERROR(
"Could not open '%s' for read", path.c_str());
77 fseek(input, 0, SEEK_END);
78 long fileSize = ftell(input);
81 std::vector<uint8_t> buffer_vec(fileSize);
82 uint8_t* buffer = &buffer_vec[0];
84 long num_bytes_read = fread(buffer, 1, fileSize, input);
85 if (num_bytes_read != fileSize)
87 ROS_ERROR(
"STLLoader::load( \"%s\" ) only read %ld bytes out of total %ld.", path.c_str(),
88 num_bytes_read, fileSize);
94 return this->
load(buffer, num_bytes_read, path);
97 bool STLLoader::load(uint8_t* buffer,
const size_t num_bytes,
const std::string& origin)
100 std::string buffer_str = std::string(reinterpret_cast<char*>(buffer), num_bytes);
102 if (buffer_str.substr(0, 5) == std::string(
"solid"))
107 if (buffer_str.find(
"endsolid", 5) != std::string::npos)
110 <<
"' is malformed. It " 111 "starts with the word 'solid' and also contains the " 112 "word 'endsolid', indicating that it's an ASCII STL " 113 "file, but rviz can only load binary STL files so it " 114 "will not be loaded. Please convert it to a " 121 <<
"' is malformed. It starts " 122 "with the word 'solid', indicating that it's an ASCII " 123 "STL file, but it does not contain the word 'endsolid' so " 124 "it is either a malformed ASCII STL file or it is actually " 125 "a binary STL file. Trying to interpret it as a binary " 126 "STL file instead.");
130 static const size_t binary_stl_header_len = 84;
131 if (num_bytes <= binary_stl_header_len)
134 <<
"' is malformed. It " 135 "appears to be a binary STL file but does not contain " 136 "enough data for the 80 byte header and 32-bit integer " 142 unsigned int num_triangles = *(
reinterpret_cast<uint32_t*
>(buffer + 80));
143 static const size_t number_of_bytes_per_triangle = 50;
144 size_t expected_size = binary_stl_header_len + num_triangles * number_of_bytes_per_triangle;
145 if (num_bytes < expected_size)
148 << origin <<
"' is malformed. According to the binary STL header it should have '" 150 <<
"' triangles, but it has too little data for that to be the case.");
153 else if (num_bytes > expected_size)
156 << origin <<
"' is malformed. According to the binary STL header it should have '" 158 <<
"' triangles, but it has too much data for that to be the case. " 159 "The extra data will be ignored.");
168 uint8_t* pos = buffer;
172 unsigned int numTriangles = *(
unsigned int*)pos;
175 for (
unsigned int currentTriangle = 0; currentTriangle < numTriangles; ++currentTriangle)
214 if (tri.
normal_.squaredLength() < 0.001)
218 tri.
normal_ = side1.crossProduct(side2);
230 Ogre::Vector3 pos(vec);
232 u =
acos(pos.y / pos.length());
234 float val = pos.x / (
sin(u));
244 Ogre::ManualObject*
object =
new Ogre::ManualObject(
"the one and only");
245 object->begin(
"BaseWhiteNoLighting", Ogre::RenderOperation::OT_TRIANGLE_LIST);
247 unsigned int vertexCount = 0;
248 V_Triangle::const_iterator it =
triangles_.begin();
249 V_Triangle::const_iterator end =
triangles_.end();
250 for (; it != end; ++it)
252 if (vertexCount >= 2004)
257 object->begin(
"BaseWhiteNoLighting", Ogre::RenderOperation::OT_TRIANGLE_LIST);
268 object->textureCoord(u, v);
273 object->textureCoord(u, v);
278 object->textureCoord(u, v);
280 object->triangle(vertexCount + 0, vertexCount + 1, vertexCount + 2);
288 object->convertToMesh(name, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
289 mesh->buildEdgeList();
#define ROS_WARN_STREAM(args)
INLINE Rall1d< T, V, S > acos(const Rall1d< T, V, S > &x)
#define ROS_ERROR_STREAM(args)
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)