From 2243a617371edf503c709695ce99eabaad796864 Mon Sep 17 00:00:00 2001 From: Nate Koenig Date: Mon, 10 May 2021 16:22:00 -0700 Subject: [PATCH] Backport collada fixes (#211) * Backport #204 to ign-common3 Signed-off-by: Nate Koenig * Remove the regex so that path separators in the middle of a string are not altered Signed-off-by: Nate Koenig * Added debug output to test Signed-off-by: Nate Koenig * windows debugging Signed-off-by: Nate Koenig * Fixing windows Signed-off-by: Nate Koenig * One more windows fix Signed-off-by: Nate Koenig * Debug output and added missing backport Signed-off-by: Nate Koenig * Copy file, and more windows debugging Signed-off-by: Nate Koenig * Use windows path separator Signed-off-by: Nate Koenig * Remove debug Signed-off-by: Nate Koenig Co-authored-by: Nate Koenig --- graphics/src/ColladaExporter.cc | 26 +++++++++++++++----------- graphics/src/ColladaExporter_TEST.cc | 12 ++++++++++++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/graphics/src/ColladaExporter.cc b/graphics/src/ColladaExporter.cc index cc13497e3..b4d064941 100644 --- a/graphics/src/ColladaExporter.cc +++ b/graphics/src/ColladaExporter.cc @@ -32,7 +32,10 @@ #endif #ifdef _WIN32 + static const char pathSeparator = '\\'; #define snprintf _snprintf +#else + static const char pathSeparator = '/'; #endif using namespace ignition; @@ -513,7 +516,7 @@ int ColladaExporterPrivate::ExportImages( this->mesh->MaterialByIndex(i); std::string imageString = material->TextureImage(); - if (imageString.find("meshes/") != std::string::npos) + if (imageString.find(pathSeparator) != std::string::npos) { char id[100]; snprintf(id, sizeof(id), "image_%u", i); @@ -525,7 +528,8 @@ int ColladaExporterPrivate::ExportImages( tinyxml2::XMLElement *initFromXml = _libraryImagesXml->GetDocument()->NewElement("init_from"); - const auto imageName = imageString.substr(imageString.rfind("/")); + const auto imageName = imageString.substr( + imageString.rfind(pathSeparator)); const auto imagePath = ignition::common::joinPaths("..", "materials", "textures", imageName); initFromXml->LinkEndChild( @@ -534,12 +538,12 @@ int ColladaExporterPrivate::ExportImages( if (this->exportTextures) { - createDirectories(this->path + this->filename + "/materials/textures"); - std::ifstream src(imageString.c_str(), std::ios::binary); - std::ofstream dst((this->path + this->filename + - "/materials/textures" + imageString.substr( - imageString.rfind("/"))).c_str(), std::ios::binary); - dst << src.rdbuf(); + std::string textureDir = joinPaths(this->path, this->filename, + "materials", "textures"); + std::string destFilename = joinPaths(textureDir, imageString.substr( + imageString.rfind(pathSeparator))); + createDirectories(textureDir); + copyFile(imageString, destFilename); } imageCount++; @@ -594,7 +598,7 @@ void ColladaExporterPrivate::ExportEffects( this->mesh->MaterialByIndex(i); std::string imageString = material->TextureImage(); - if (imageString.find("meshes/") != std::string::npos) + if (imageString.find(pathSeparator) != std::string::npos) { tinyxml2::XMLElement *newParamXml = _libraryEffectsXml->GetDocument()->NewElement("newparam"); @@ -689,7 +693,7 @@ void ColladaExporterPrivate::ExportEffects( _libraryEffectsXml->GetDocument()->NewElement("diffuse"); phongXml->LinkEndChild(diffuseXml); - if (imageString.find("meshes/") != std::string::npos) + if (imageString.find(pathSeparator) != std::string::npos) { tinyxml2::XMLElement *textureXml = _libraryEffectsXml->GetDocument()->NewElement("texture"); @@ -832,7 +836,7 @@ void ColladaExporterPrivate::ExportVisualScenes( std::string imageString = material->TextureImage(); - if (imageString.find("meshes/") != std::string::npos) + if (imageString.find(pathSeparator) != std::string::npos) { tinyxml2::XMLElement *bindVertexInputXml = _libraryVisualScenesXml->GetDocument()->NewElement( diff --git a/graphics/src/ColladaExporter_TEST.cc b/graphics/src/ColladaExporter_TEST.cc index b2725f5c4..f005b163e 100644 --- a/graphics/src/ColladaExporter_TEST.cc +++ b/graphics/src/ColladaExporter_TEST.cc @@ -121,6 +121,8 @@ TEST_F(ColladaExporter, ExportCordlessDrill) "cordless_drill_exported"); const auto filenameOutExt = common::joinPaths(filenameOut, "meshes", "cordless_drill_exported.dae"); + const auto filenameOutTexture = common::joinPaths(filenameOut, + "materials", "textures", "cordless_drill.png"); // Load original mesh common::ColladaLoader loader; @@ -130,6 +132,16 @@ TEST_F(ColladaExporter, ExportCordlessDrill) common::ColladaExporter exporter; exporter.Export(meshOriginal, filenameOut, true); + // The export directory and texture should now exist. + EXPECT_TRUE(common::exists(this->pathOut)) << this->pathOut; + ASSERT_TRUE(common::exists(filenameOut)) << filenameOut; + EXPECT_TRUE(common::exists(common::joinPaths(filenameOut, "materials"))) + << common::joinPaths(filenameOut, "materials"); + EXPECT_TRUE(common::exists(common::joinPaths(filenameOut, "materials", + "textures"))) << common::joinPaths(filenameOut, "materials", + "textures"); + EXPECT_TRUE(common::exists(filenameOutTexture)) << filenameOutTexture; + // Check .dae file tinyxml2::XMLDocument xmlDoc; ASSERT_EQ(xmlDoc.LoadFile(filenameOutExt.c_str()), tinyxml2::XML_SUCCESS);