43 #if defined(ASSIMP_UNIFIED_HEADER_NAMES) 44 #include <assimp/Importer.hpp> 45 #include <assimp/scene.h> 46 #include <assimp/postprocess.h> 47 #include <assimp/IOStream.hpp> 48 #include <assimp/IOSystem.hpp> 50 #include <assimp/assimp.hpp> 51 #include <assimp/aiScene.h> 52 #include <assimp/aiPostProcess.h> 53 #include <assimp/IOStream.h> 54 #include <assimp/IOSystem.h> 76 size_t Read(
void* buffer,
size_t size,
size_t count)
78 size_t to_read = size * count;
84 memcpy(buffer,
pos_, to_read);
90 size_t Write(
const void* buffer,
size_t size,
size_t count) {
ROS_BREAK();
return 0; }
92 aiReturn
Seek(
size_t offset, aiOrigin origin)
101 new_pos =
pos_ + offset;
112 return aiReturn_FAILURE;
116 return aiReturn_SUCCESS;
157 res = retriever_.get(file);
174 Assimp::IOStream*
Open(
const char* file,
const char* mode =
"rb")
181 res = retriever_.get(file);
191 void Close(Assimp::IOStream* stream);
204 static std::map<std::string, float> rescale_cache;
207 TiXmlDocument xmlDoc;
208 float unit_scale(1.0);
213 res = retriever.
get(resource_path);
228 const char * data =
reinterpret_cast<const char *
> (res.
data.get());
234 TiXmlElement * colladaXml = xmlDoc.FirstChildElement(
"COLLADA");
237 TiXmlElement *assetXml = colladaXml->FirstChildElement(
"asset");
240 TiXmlElement *unitXml = assetXml->FirstChildElement(
"unit");
241 if (unitXml && unitXml->Attribute(
"meter"))
244 if(unitXml->QueryFloatAttribute(
"meter", &unit_scale) != 0)
245 ROS_WARN_STREAM(
"getMeshUnitRescale::Failed to convert unit element meter attribute to determine scaling. unit element: " 256 std::vector<tf::Vector3> vertices;
262 aiMatrix4x4 transform = node->mTransformation;
263 aiNode *pnode = node->mParent;
268 if (pnode->mParent != NULL)
269 transform = pnode->mTransformation * transform;
270 pnode = pnode->mParent;
273 aiMatrix3x3 rotation(transform);
274 aiMatrix3x3 inverse_transpose_rotation(rotation);
275 inverse_transpose_rotation.Inverse();
276 inverse_transpose_rotation.Transpose();
278 for (uint32_t i = 0; i < node->mNumMeshes; i++)
280 aiMesh* input_mesh = scene->mMeshes[node->mMeshes[i]];
282 for (uint32_t j = 0; j < input_mesh->mNumVertices; j++)
284 aiVector3D p = input_mesh->mVertices[j];
288 vertices.push_back(v);
292 for (uint32_t i=0; i < node->mNumChildren; ++i)
296 for (
size_t j = 0; j < sub_vertices.size(); j++) {
297 vertices.push_back(sub_vertices[j]);
305 if (!scene->HasMeshes())
307 ROS_ERROR(
"No meshes found in file [%s]", name.c_str());
357 unsigned int nt = triangles.size() / 3;
359 for (
unsigned int i = 0 ; i < vertices.size() ; ++i)
361 mesh->
vertices[3 * i ] = vertices[i].getX();
362 mesh->
vertices[3 * i + 1] = vertices[i].getY();
363 mesh->
vertices[3 * i + 2] = vertices[i].getZ();
366 std::copy(triangles.begin(), triangles.end(), mesh->
triangles);
369 for (
unsigned int i = 0 ; i < nt ; ++i)
371 tf::Vector3 s1 = vertices[triangles[i * 3 ]] - vertices[triangles[i * 3 + 1]];
372 tf::Vector3 s2 = vertices[triangles[i * 3 + 1]] - vertices[triangles[i * 3 + 2]];
384 if (source.size() < 3)
387 std::set<detail::myVertex, detail::ltVertexValue> vertices;
388 std::vector<unsigned int> triangles;
390 for (
unsigned int i = 0 ; i < source.size() / 3 ; ++i)
395 vt.
point = source[3 * i];
396 std::set<detail::myVertex, detail::ltVertexValue>::iterator p1 = vertices.find(vt);
397 if (p1 == vertices.end())
399 vt.
index = vertices.size();
403 vt.
index = p1->index;
404 triangles.push_back(vt.
index);
406 vt.
point = source[3 * i + 1];
407 std::set<detail::myVertex, detail::ltVertexValue>::iterator p2 = vertices.find(vt);
408 if (p2 == vertices.end())
410 vt.
index = vertices.size();
414 vt.
index = p2->index;
415 triangles.push_back(vt.
index);
417 vt.
point = source[3 * i + 2];
418 std::set<detail::myVertex, detail::ltVertexValue>::iterator p3 = vertices.find(vt);
419 if (p3 == vertices.end())
421 vt.
index = vertices.size();
425 vt.
index = p3->index;
427 triangles.push_back(vt.
index);
431 std::vector<detail::myVertex> vt;
432 vt.insert(vt.begin(), vertices.begin(), vertices.end());
436 unsigned int nt = triangles.size() / 3;
439 for (
unsigned int i = 0 ; i < vt.size() ; ++i)
441 mesh->
vertices[3 * i ] = vt[i].point.getX();
442 mesh->
vertices[3 * i + 1] = vt[i].point.getY();
443 mesh->
vertices[3 * i + 2] = vt[i].point.getZ();
446 std::copy(triangles.begin(), triangles.end(), mesh->
triangles);
449 for (
unsigned int i = 0 ; i < nt ; ++i)
451 tf::Vector3 s1 = vt[triangles[i * 3 ]].point - vt[triangles[i * 3 + 1]].point;
452 tf::Vector3 s2 = vt[triangles[i * 3 + 1]].point - vt[triangles[i * 3 + 2]].point;
465 const char* pos = data;
468 unsigned int numTriangles = *(
unsigned int*)pos;
472 if ((
long)(50 * numTriangles + 84) <= size)
474 std::vector<tf::Vector3> vertices;
476 for (
unsigned int currentTriangle = 0 ; currentTriangle < numTriangles ; ++currentTriangle)
486 v1.
setX(*(
float*)pos);
488 v1.
setY(*(
float*)pos);
490 v1.
setZ(*(
float*)pos);
493 v2.
setX(*(
float*)pos);
495 v2.
setY(*(
float*)pos);
497 v2.
setZ(*(
float*)pos);
500 v3.
setX(*(
float*)pos);
502 v3.
setY(*(
float*)pos);
504 v3.
setZ(*(
float*)pos);
510 vertices.push_back(v1);
511 vertices.push_back(v2);
512 vertices.push_back(v3);
523 std::string resource_path(filename);
524 Assimp::Importer importer;
526 const aiScene* scene = importer.ReadFile(resource_path, aiProcess_SortByPType|aiProcess_GenNormals|aiProcess_Triangulate|aiProcess_GenUVCoords|aiProcess_FlipUVs);
529 ROS_ERROR(
"Could not load resource [%s]: %s", resource_path.c_str(), importer.GetErrorString());
537 FILE* input = fopen(filename,
"r");
541 fseek(input, 0, SEEK_END);
542 long fileSize = ftell(input);
543 fseek(input, 0, SEEK_SET);
545 char* buffer =
new char[fileSize];
546 size_t rd = fread(buffer, fileSize, 1, input);
std::vector< tf::Vector3 > getVerticesFromAssimpNode(const aiScene *scene, const aiNode *node, const float scale)
bool Exists(const char *file) const
TFSIMD_FORCE_INLINE void setX(tfScalar x)
TFSIMD_FORCE_INLINE void setY(tfScalar y)
float getMeshUnitRescale(const std::string &resource_path)
double * normals
the normal to each triangle unit vector represented as (x,y,z)
ResourceIOStream(const resource_retriever::MemoryResource &res)
TFSIMD_FORCE_INLINE void setZ(tfScalar z)
TFSIMD_FORCE_INLINE const tfScalar & getY() const
TFSIMD_FORCE_INLINE const tfScalar & getZ() const
bool operator()(const myVertex &p1, const myVertex &p2) const
TFSIMD_FORCE_INLINE Vector3 cross(const Vector3 &v) const
double * vertices
the position for each vertex vertex k has values at index (3k, 3k+1, 3k+2) = (x,y,z)
resource_retriever::Retriever retriever_
boost::shared_array< uint8_t > data
shapes::Mesh * meshFromAssimpScene(const std::string &name, const aiScene *scene)
char getOsSeparator() const
aiReturn Seek(size_t offset, aiOrigin origin)
size_t Read(void *buffer, size_t size, size_t count)
Mesh * createMeshFromBinaryStl(const char *filename)
Load a mesh from a binary STL file. Normals are recomputed and repeating vertices are identified...
resource_retriever::MemoryResource res_
#define ROS_WARN_STREAM(args)
TFSIMD_FORCE_INLINE Vector3 & normalize()
MemoryResource get(const std::string &url)
bool operator()(const myVertex &p1, const myVertex &p2) const
Mesh * createMeshFromBinaryDAE(const char *filename)
Load a mesh from a binary DAE file. Normals are recomputed and repeating vertices are identified...
TFSIMD_FORCE_INLINE const tfScalar & getX() const
Mesh * createMeshFromBinaryStlData(const char *data, unsigned int size)
Load a mesh from a binary STL stream. Normals are recomputed and repeating vertices are identified...
Assimp::IOStream * Open(const char *file, const char *mode="rb")
unsigned int * triangles
the vertex indices for each triangle triangle k has vertices at index (3k, 3k+1, 3k+2) = (v1...
Mesh * createMeshFromVertices(const std::vector< tf::Vector3 > &vertices, const std::vector< unsigned int > &triangles)
Load a mesh from a set of vertices. Triangles are constructed using index values from the triangles v...
size_t Write(const void *buffer, size_t size, size_t count)
void Close(Assimp::IOStream *stream)