Skip to content

Commit

Permalink
Backport collada fixes (#211)
Browse files Browse the repository at this point in the history
* Backport #204 to ign-common3

Signed-off-by: Nate Koenig <nate@openrobotics.org>

* Remove the regex so that path separators in the middle of a string are not altered

Signed-off-by: Nate Koenig <nate@openrobotics.org>

* Added debug output to test

Signed-off-by: Nate Koenig <nate@openrobotics.org>

* windows debugging

Signed-off-by: Nate Koenig <nate@openrobotics.org>

* Fixing windows

Signed-off-by: Nate Koenig <nate@openrobotics.org>

* One more windows fix

Signed-off-by: Nate Koenig <nate@openrobotics.org>

* Debug output and added missing backport

Signed-off-by: Nate Koenig <nate@openrobotics.org>

* Copy file, and more windows debugging

Signed-off-by: Nate Koenig <nate@openrobotics.org>

* Use windows path separator

Signed-off-by: Nate Koenig <nate@openrobotics.org>

* Remove debug

Signed-off-by: Nate Koenig <nate@openrobotics.org>

Co-authored-by: Nate Koenig <nate@openrobotics.org>
  • Loading branch information
nkoenig and Nate Koenig authored May 10, 2021
1 parent d96242a commit 2243a61
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
26 changes: 15 additions & 11 deletions graphics/src/ColladaExporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
#endif

#ifdef _WIN32
static const char pathSeparator = '\\';
#define snprintf _snprintf
#else
static const char pathSeparator = '/';
#endif

using namespace ignition;
Expand Down Expand Up @@ -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);
Expand All @@ -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(
Expand All @@ -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++;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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(
Expand Down
12 changes: 12 additions & 0 deletions graphics/src/ColladaExporter_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 2243a61

Please sign in to comment.