URDF loader: reuse the same resources finder routine for TinyRendererVisualShapeConverter
This commit is contained in:
@@ -457,9 +457,8 @@ static btCollisionShape* createConvexHullFromShapes(std::vector<tinyobj::shape_t
|
||||
return compound;
|
||||
}
|
||||
|
||||
static
|
||||
bool findExistingMeshFile(
|
||||
const std::string& path_or_shorter, std::string fn,
|
||||
const std::string& urdf_path, std::string fn,
|
||||
const std::string& error_message_prefix,
|
||||
std::string* out_found_filename, int* out_type)
|
||||
{
|
||||
@@ -472,9 +471,9 @@ bool findExistingMeshFile(
|
||||
std::string ext_ = fn.substr(fn.size()-4);
|
||||
for (std::string::iterator i=ext_.begin(); i!=ext_.end(); ++i)
|
||||
ext += char(tolower(*i));
|
||||
if (ext==".dae") *out_type = FILE_COLLADA;
|
||||
else if (ext==".stl") *out_type = FILE_STL;
|
||||
else if (ext==".obj") *out_type = FILE_OBJ;
|
||||
if (ext==".dae") *out_type = UrdfGeometry::FILE_COLLADA;
|
||||
else if (ext==".stl") *out_type = UrdfGeometry::FILE_STL;
|
||||
else if (ext==".obj") *out_type = UrdfGeometry::FILE_OBJ;
|
||||
else
|
||||
{
|
||||
b3Warning("%s: invalid mesh filename extension '%s'\n", error_message_prefix.c_str(), ext.c_str());
|
||||
@@ -486,12 +485,13 @@ bool findExistingMeshFile(
|
||||
fn = fn.substr(drop_it.length());
|
||||
|
||||
std::list<std::string> shorter;
|
||||
int cnt = path_or_shorter.size();
|
||||
shorter.push_back("../..");
|
||||
shorter.push_back("..");
|
||||
shorter.push_back(".");
|
||||
int cnt = urdf_path.size();
|
||||
for (int i=0; i<cnt; ++i) {
|
||||
if (path_or_shorter[i]=='/' || path_or_shorter[i]=='\\')
|
||||
shorter.push_back(path_or_shorter.substr(0, i));
|
||||
else if (i==cnt-1)
|
||||
shorter.push_back(path_or_shorter.substr(0, cnt));
|
||||
if (urdf_path[i]=='/' || urdf_path[i]=='\\')
|
||||
shorter.push_back(urdf_path.substr(0, i));
|
||||
}
|
||||
shorter.reverse();
|
||||
|
||||
@@ -589,22 +589,17 @@ btCollisionShape* convertURDFToCollisionShape(const UrdfCollision* collision, co
|
||||
|
||||
case URDF_GEOM_MESH:
|
||||
{
|
||||
std::string existing_file;
|
||||
int fileType;
|
||||
bool success = findExistingMeshFile(urdfPathPrefix, collision->m_geometry.m_meshFileName, collision->m_sourceFileLocation, &existing_file, &fileType);
|
||||
if (!success) break; // error message already printed
|
||||
|
||||
GLInstanceGraphicsShape* glmesh = 0;
|
||||
switch (fileType) {
|
||||
switch (collision->m_geometry.m_meshFileType) {
|
||||
case FILE_OBJ:
|
||||
if (collision->m_flags & URDF_FORCE_CONCAVE_TRIMESH)
|
||||
{
|
||||
glmesh = LoadMeshFromObj(existing_file.c_str(), 0);
|
||||
glmesh = LoadMeshFromObj(collision->m_geometry.m_meshFileName.c_str(), 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::string err = tinyobj::LoadObj(shapes, existing_file.c_str());
|
||||
std::string err = tinyobj::LoadObj(shapes, collision->m_geometry.m_meshFileName.c_str());
|
||||
//create a convex hull for each shape, and store it in a btCompoundShape
|
||||
|
||||
shape = createConvexHullFromShapes(shapes, collision->m_geometry.m_meshScale);
|
||||
@@ -613,7 +608,7 @@ btCollisionShape* convertURDFToCollisionShape(const UrdfCollision* collision, co
|
||||
break;
|
||||
|
||||
case FILE_STL:
|
||||
glmesh = LoadMeshFromSTL(existing_file.c_str());
|
||||
glmesh = LoadMeshFromSTL(collision->m_geometry.m_meshFileName.c_str());
|
||||
break;
|
||||
|
||||
case FILE_COLLADA:
|
||||
@@ -622,7 +617,7 @@ btCollisionShape* convertURDFToCollisionShape(const UrdfCollision* collision, co
|
||||
btAlignedObjectArray<ColladaGraphicsInstance> visualShapeInstances;
|
||||
btTransform upAxisTrans;upAxisTrans.setIdentity();
|
||||
float unitMeterScaling = 1;
|
||||
LoadMeshFromCollada(existing_file.c_str(), visualShapes, visualShapeInstances, upAxisTrans, unitMeterScaling, 2);
|
||||
LoadMeshFromCollada(collision->m_geometry.m_meshFileName.c_str(), visualShapes, visualShapeInstances, upAxisTrans, unitMeterScaling, 2);
|
||||
|
||||
glmesh = new GLInstanceGraphicsShape;
|
||||
glmesh->m_indices = new b3AlignedObjectArray<int>();
|
||||
@@ -691,7 +686,7 @@ upAxisMat.setIdentity();
|
||||
|
||||
if (!glmesh || glmesh->m_numvertices<=0)
|
||||
{
|
||||
b3Warning("%s: cannot extract mesh from '%s'\n", urdfPathPrefix, existing_file.c_str());
|
||||
b3Warning("%s: cannot extract mesh from '%s'\n", urdfPathPrefix, collision->m_geometry.m_meshFileName.c_str());
|
||||
delete glmesh;
|
||||
break;
|
||||
}
|
||||
@@ -802,17 +797,12 @@ static void convertURDFToVisualShapeInternal(const UrdfVisual* visual, const cha
|
||||
|
||||
case URDF_GEOM_MESH:
|
||||
{
|
||||
std::string existing_file;
|
||||
int fileType;
|
||||
bool success = findExistingMeshFile(urdfPathPrefix, visual->m_geometry.m_meshFileName, visual->m_sourceFileLocation, &existing_file, &fileType);
|
||||
if (!success) break; // error message already printed
|
||||
|
||||
switch (fileType)
|
||||
switch (visual->m_geometry.m_meshFileType)
|
||||
{
|
||||
case FILE_OBJ:
|
||||
{
|
||||
b3ImportMeshData meshData;
|
||||
if (b3ImportMeshUtility::loadAndRegisterMeshFromFileInternal(existing_file, meshData))
|
||||
if (b3ImportMeshUtility::loadAndRegisterMeshFromFileInternal(visual->m_geometry.m_meshFileName, meshData))
|
||||
{
|
||||
|
||||
if (meshData.m_textureImage)
|
||||
@@ -830,7 +820,7 @@ static void convertURDFToVisualShapeInternal(const UrdfVisual* visual, const cha
|
||||
|
||||
case FILE_STL:
|
||||
{
|
||||
glmesh = LoadMeshFromSTL(existing_file.c_str());
|
||||
glmesh = LoadMeshFromSTL(visual->m_geometry.m_meshFileName.c_str());
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -842,7 +832,7 @@ static void convertURDFToVisualShapeInternal(const UrdfVisual* visual, const cha
|
||||
float unitMeterScaling = 1;
|
||||
int upAxis = 2;
|
||||
|
||||
LoadMeshFromCollada(existing_file.c_str(),
|
||||
LoadMeshFromCollada(visual->m_geometry.m_meshFileName.c_str(),
|
||||
visualShapes,
|
||||
visualShapeInstances,
|
||||
upAxisTrans,
|
||||
@@ -910,7 +900,7 @@ static void convertURDFToVisualShapeInternal(const UrdfVisual* visual, const cha
|
||||
}
|
||||
glmesh->m_numIndices = glmesh->m_indices->size();
|
||||
glmesh->m_numvertices = glmesh->m_vertices->size();
|
||||
//glmesh = LoadMeshFromCollada(existing_file);
|
||||
//glmesh = LoadMeshFromCollada(visual->m_geometry.m_meshFileName);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -918,7 +908,7 @@ static void convertURDFToVisualShapeInternal(const UrdfVisual* visual, const cha
|
||||
|
||||
if (!glmesh || !glmesh->m_vertices || glmesh->m_numvertices<=0)
|
||||
{
|
||||
b3Warning("%s: cannot extract anything useful from mesh '%s'\n", urdfPathPrefix, existing_file.c_str());
|
||||
b3Warning("%s: cannot extract anything useful from mesh '%s'\n", urdfPathPrefix, visual->m_geometry.m_meshFileName.c_str());
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -444,14 +444,17 @@ bool UrdfParser::parseGeometry(UrdfGeometry& geom, TiXmlElement* g, ErrorLogger*
|
||||
return false;
|
||||
}
|
||||
|
||||
geom.m_meshFileName = shape->Attribute("filename");
|
||||
bool success = findExistingMeshFile(
|
||||
m_urdf2Model.m_sourceFile, shape->Attribute("filename"), sourceFileLocation(shape),
|
||||
&geom.m_meshFileName, &geom.m_meshFileType);
|
||||
if (!success) return false; // warning printed
|
||||
geom.m_meshScale.setValue(1,1,1);
|
||||
|
||||
if (shape->Attribute("scale"))
|
||||
{
|
||||
if (!parseVector3(geom.m_meshScale,shape->Attribute("scale"),logger))
|
||||
{
|
||||
logger->reportWarning("scale should be a vector3, not single scalar. Workaround activated.\n");
|
||||
logger->reportWarning("%s: scale should be a vector3, not single scalar. Workaround activated.\n");
|
||||
std::string scalar_str = shape->Attribute("scale");
|
||||
double scaleFactor = urdfLexicalCast<double>(scalar_str.c_str());
|
||||
if (scaleFactor)
|
||||
|
||||
@@ -71,10 +71,20 @@ struct UrdfGeometry
|
||||
|
||||
btVector3 m_planeNormal;
|
||||
|
||||
enum {
|
||||
FILE_STL =1,
|
||||
FILE_COLLADA =2,
|
||||
FILE_OBJ =3,
|
||||
};
|
||||
int m_meshFileType;
|
||||
std::string m_meshFileName;
|
||||
btVector3 m_meshScale;
|
||||
};
|
||||
|
||||
bool findExistingMeshFile(const std::string& urdf_path, std::string fn,
|
||||
const std::string& error_message_prefix,
|
||||
std::string* out_found_filename, int* out_type); // intended to fill UrdfGeometry::m_meshFileName and Type, but can be used elsewhere
|
||||
|
||||
struct UrdfVisual
|
||||
{
|
||||
std::string m_sourceFileLocation;
|
||||
|
||||
@@ -36,13 +36,6 @@ subject to the following restrictions:
|
||||
#include "../TinyRenderer/model.h"
|
||||
#include "../ThirdPartyLibs/stb_image/stb_image.h"
|
||||
|
||||
enum MyFileType
|
||||
{
|
||||
MY_FILE_STL=1,
|
||||
MY_FILE_COLLADA=2,
|
||||
MY_FILE_OBJ=3,
|
||||
};
|
||||
|
||||
struct MyTexture2
|
||||
{
|
||||
unsigned char* textureData;
|
||||
@@ -241,54 +234,16 @@ void convertURDFToVisualShape(const UrdfVisual* visual, const char* urdfPathPref
|
||||
convexColShape = sphereShape;
|
||||
convexColShape->setMargin(0.001);
|
||||
break;
|
||||
|
||||
break;
|
||||
}
|
||||
case URDF_GEOM_MESH:
|
||||
{
|
||||
if (visual->m_name.length())
|
||||
{
|
||||
//b3Printf("visual->name=%s\n", visual->m_name.c_str());
|
||||
}
|
||||
if (1)//visual->m_geometry)
|
||||
{
|
||||
if (visual->m_geometry.m_meshFileName.length())
|
||||
{
|
||||
const char* filename = visual->m_geometry.m_meshFileName.c_str();
|
||||
//b3Printf("mesh->filename=%s\n", filename);
|
||||
char fullPath[1024];
|
||||
int fileType = 0;
|
||||
|
||||
char tmpPathPrefix[1024];
|
||||
std::string xml_string;
|
||||
int maxPathLen = 1024;
|
||||
b3FileUtils::extractPath(filename,tmpPathPrefix,maxPathLen);
|
||||
|
||||
char visualPathPrefix[1024];
|
||||
sprintf(visualPathPrefix,"%s%s",urdfPathPrefix,tmpPathPrefix);
|
||||
|
||||
|
||||
sprintf(fullPath, "%s%s", urdfPathPrefix, filename);
|
||||
b3FileUtils::toLower(fullPath);
|
||||
if (strstr(fullPath, ".dae"))
|
||||
{
|
||||
fileType = MY_FILE_COLLADA;
|
||||
}
|
||||
if (strstr(fullPath, ".stl"))
|
||||
{
|
||||
fileType = MY_FILE_STL;
|
||||
}
|
||||
if (strstr(fullPath,".obj"))
|
||||
{
|
||||
fileType = MY_FILE_OBJ;
|
||||
}
|
||||
|
||||
|
||||
sprintf(fullPath, "%s%s", urdfPathPrefix, filename);
|
||||
strncpy(visualShapeOut.m_meshAssetFileName, visual->m_geometry.m_meshFileName.c_str(), VISUAL_SHAPE_MAX_PATH_LEN);
|
||||
visualShapeOut.m_meshAssetFileName[VISUAL_SHAPE_MAX_PATH_LEN-1] = 0;
|
||||
|
||||
visualShapeOut.m_dimensions[0] = visual->m_geometry.m_meshScale[0];
|
||||
visualShapeOut.m_dimensions[1] = visual->m_geometry.m_meshScale[1];
|
||||
visualShapeOut.m_dimensions[2] = visual->m_geometry.m_meshScale[2];
|
||||
|
||||
visualShapeOut.m_localVisualFrame[0] = visual->m_linkLocalFrame.getOrigin()[0];
|
||||
visualShapeOut.m_localVisualFrame[1] = visual->m_linkLocalFrame.getOrigin()[1];
|
||||
visualShapeOut.m_localVisualFrame[2] = visual->m_linkLocalFrame.getOrigin()[2];
|
||||
@@ -297,27 +252,13 @@ void convertURDFToVisualShape(const UrdfVisual* visual, const char* urdfPathPref
|
||||
visualShapeOut.m_localVisualFrame[5] = visual->m_linkLocalFrame.getRotation()[2];
|
||||
visualShapeOut.m_localVisualFrame[6] = visual->m_linkLocalFrame.getRotation()[3];
|
||||
|
||||
int sl = strlen(fullPath);
|
||||
if (sl < (VISUAL_SHAPE_MAX_PATH_LEN-1))
|
||||
switch (visual->m_geometry.m_meshFileType)
|
||||
{
|
||||
memcpy(visualShapeOut.m_meshAssetFileName, fullPath, sl);
|
||||
visualShapeOut.m_meshAssetFileName[sl] = 0;
|
||||
}
|
||||
|
||||
FILE* f = fopen(fullPath, "rb");
|
||||
if (f)
|
||||
{
|
||||
fclose(f);
|
||||
|
||||
|
||||
|
||||
switch (fileType)
|
||||
{
|
||||
case MY_FILE_OBJ:
|
||||
case UrdfGeometry::FILE_OBJ:
|
||||
{
|
||||
//glmesh = LoadMeshFromObj(fullPath,visualPathPrefix);
|
||||
b3ImportMeshData meshData;
|
||||
if (b3ImportMeshUtility::loadAndRegisterMeshFromFileInternal(fullPath, meshData))
|
||||
if (b3ImportMeshUtility::loadAndRegisterMeshFromFileInternal(visual->m_geometry.m_meshFileName, meshData))
|
||||
{
|
||||
|
||||
if (meshData.m_textureImage)
|
||||
@@ -330,26 +271,20 @@ void convertURDFToVisualShape(const UrdfVisual* visual, const char* urdfPathPref
|
||||
}
|
||||
glmesh = meshData.m_gfxShape;
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case MY_FILE_STL:
|
||||
{
|
||||
glmesh = LoadMeshFromSTL(fullPath);
|
||||
case UrdfGeometry::FILE_STL:
|
||||
glmesh = LoadMeshFromSTL(visual->m_geometry.m_meshFileName.c_str());
|
||||
break;
|
||||
}
|
||||
case MY_FILE_COLLADA:
|
||||
case UrdfGeometry::FILE_COLLADA:
|
||||
{
|
||||
|
||||
btAlignedObjectArray<GLInstanceGraphicsShape> visualShapes;
|
||||
btAlignedObjectArray<ColladaGraphicsInstance> visualShapeInstances;
|
||||
btTransform upAxisTrans; upAxisTrans.setIdentity();
|
||||
float unitMeterScaling = 1;
|
||||
int upAxis = 2;
|
||||
|
||||
LoadMeshFromCollada(fullPath,
|
||||
LoadMeshFromCollada(visual->m_geometry.m_meshFileName.c_str(),
|
||||
visualShapes,
|
||||
visualShapeInstances,
|
||||
upAxisTrans,
|
||||
@@ -417,17 +352,14 @@ void convertURDFToVisualShape(const UrdfVisual* visual, const char* urdfPathPref
|
||||
}
|
||||
glmesh->m_numIndices = glmesh->m_indices->size();
|
||||
glmesh->m_numvertices = glmesh->m_vertices->size();
|
||||
//glmesh = LoadMeshFromCollada(fullPath);
|
||||
|
||||
//glmesh = LoadMeshFromCollada(visual->m_geometry.m_meshFileName.c_str());
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
b3Warning("Error: unsupported file type for Visual mesh: %s\n", fullPath);
|
||||
// should never get here (findExistingMeshFile returns false if it doesn't recognize extension)
|
||||
btAssert(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (glmesh && glmesh->m_vertices && (glmesh->m_numvertices>0))
|
||||
{
|
||||
@@ -438,26 +370,14 @@ void convertURDFToVisualShape(const UrdfVisual* visual, const char* urdfPathPref
|
||||
glmesh->m_vertices->at(i).xyzw[1] *= visual->m_geometry.m_meshScale[1];
|
||||
glmesh->m_vertices->at(i).xyzw[2] *= visual->m_geometry.m_meshScale[2];
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
b3Warning("issue extracting mesh from COLLADA/STL file %s\n", fullPath);
|
||||
b3Warning("issue extracting mesh from COLLADA/STL file %s\n", visual->m_geometry.m_meshFileName.c_str());
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
b3Warning("mesh geometry not found %s\n", fullPath);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
} // case mesh
|
||||
|
||||
default:
|
||||
{
|
||||
b3Warning("Error: unknown visual geometry type\n");
|
||||
|
||||
Reference in New Issue
Block a user