Changed std::string to std::fs::path for MeshWriter & MeshLoader

This commit is contained in:
Vladyslav Baranovskyi
2025-02-11 20:29:04 +02:00
parent 08229c3be6
commit f5b3ac5190
4 changed files with 36 additions and 34 deletions

View File

@@ -102,27 +102,27 @@ namespace
namespace OpenVulkano::Scene namespace OpenVulkano::Scene
{ {
void MeshWriter::WriteAsOBJ(Geometry* geometry, const std::string& filePath) void MeshWriter::WriteAsOBJ(Geometry* geometry, const std::filesystem::path& filePath)
{ {
std::ofstream file(filePath); std::ofstream file(filePath);
if (!file.is_open()) [[unlikely]] if (!file.is_open()) [[unlikely]]
throw std::runtime_error("Failed to open file '" + filePath + "' for writing!"); throw std::runtime_error("Failed to open file '" + filePath.string() + "' for writing!");
WriteObjContents(geometry, "", file); WriteObjContents(geometry, "", file);
file.close(); file.close();
} }
void MeshWriter::WriteAsUSD(Geometry* geometry, const std::string& filePath) void MeshWriter::WriteAsUSD(Geometry* geometry, const std::filesystem::path& filePath)
{ {
std::ofstream file(filePath); std::ofstream file(filePath);
if (!file.is_open()) [[unlikely]] if (!file.is_open()) [[unlikely]]
throw std::runtime_error("Failed to open file '" + filePath + "' for writing!"); throw std::runtime_error("Failed to open file '" + filePath.string() + "' for writing!");
WriteUsdContents(file, geometry); WriteUsdContents(file, geometry);
file.close(); file.close();
} }
void MeshWriter::WriteObjAsZip(Geometry* geometry, const std::string& texturePath, const std::string& zipPath) void MeshWriter::WriteObjAsZip(Geometry* geometry, const std::filesystem::path& texturePath, const std::filesystem::path& zipPath)
{ {
OpenVulkano::ArchiveWriter zipWriter(zipPath.c_str()); OpenVulkano::ArchiveWriter zipWriter(zipPath.c_str());
@@ -146,7 +146,7 @@ namespace OpenVulkano::Scene
} }
} }
void MeshWriter::WriteAsUSDZ(Geometry* geometry, const std::string& texturePath, const std::string& usdzPath) void MeshWriter::WriteAsUSDZ(Geometry* geometry, const std::filesystem::path& texturePath, const std::filesystem::path& usdzPath)
{ {
OpenVulkano::ZipWriter zipWriter(usdzPath, true); OpenVulkano::ZipWriter zipWriter(usdzPath, true);
@@ -166,7 +166,7 @@ namespace OpenVulkano::Scene
} }
} }
void MeshWriter::WriteAsSTL(Geometry* geometry, const std::string& filePath, bool binary) void MeshWriter::WriteAsSTL(Geometry* geometry, const std::filesystem::path& filePath, bool binary)
{ {
#if __has_include("assimp/Exporter.hpp") #if __has_include("assimp/Exporter.hpp")
std::unique_ptr<aiScene> scene(new aiScene()); std::unique_ptr<aiScene> scene(new aiScene());
@@ -178,7 +178,7 @@ namespace OpenVulkano::Scene
Assimp::Exporter exporter; Assimp::Exporter exporter;
const char* formatId = binary ? "stlb" : "stl"; const char* formatId = binary ? "stlb" : "stl";
aiReturn result = exporter.Export(scene.get(), formatId, filePath); aiReturn result = exporter.Export(scene.get(), formatId, filePath.string().c_str());
scene->mRootNode = nullptr; scene->mRootNode = nullptr;
scene->mMeshes = nullptr; scene->mMeshes = nullptr;
@@ -190,14 +190,14 @@ namespace OpenVulkano::Scene
if (result != aiReturn_SUCCESS) if (result != aiReturn_SUCCESS)
{ {
throw std::runtime_error("Unable to write STL file to " + filePath + ": " + exporter.GetErrorString()); throw std::runtime_error("Unable to write STL file to " + filePath.string() + ": " + exporter.GetErrorString());
} }
#else #else
throw std::runtime_error("Unable to export to STL: Assimp is not available!"); throw std::runtime_error("Unable to export to STL: Assimp is not available!");
#endif #endif
} }
void MeshWriter::WriteAsFBX(Geometry* geometry, const std::string& texturePath, const std::string& fbxPath) void MeshWriter::WriteAsFBX(Geometry* geometry, const std::filesystem::path& texturePath, const std::filesystem::path& fbxPath)
{ {
#if __has_include("assimp/Exporter.hpp") #if __has_include("assimp/Exporter.hpp")
std::unique_ptr<aiScene> scene(new aiScene()); std::unique_ptr<aiScene> scene(new aiScene());
@@ -209,12 +209,12 @@ namespace OpenVulkano::Scene
if (!texturePath.empty()) if (!texturePath.empty())
{ {
aiString externalPath(texturePath); aiString externalPath(texturePath.string().c_str());
scene->mMaterials[0]->AddProperty(&externalPath, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0)); scene->mMaterials[0]->AddProperty(&externalPath, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0));
} }
Assimp::Exporter exporter; Assimp::Exporter exporter;
aiReturn result = exporter.Export(scene.get(), "fbx", fbxPath); aiReturn result = exporter.Export(scene.get(), "fbx", fbxPath.string().c_str());
scene->mRootNode = nullptr; scene->mRootNode = nullptr;
scene->mMeshes = nullptr; scene->mMeshes = nullptr;
@@ -226,7 +226,7 @@ namespace OpenVulkano::Scene
if (result != aiReturn_SUCCESS) if (result != aiReturn_SUCCESS)
{ {
throw std::runtime_error("Unable to write a fbx file to " + fbxPath + ": " + exporter.GetErrorString()); throw std::runtime_error("Unable to write a fbx file to " + fbxPath.string() + ": " + exporter.GetErrorString());
} }
#else #else
throw std::runtime_error("Unable to convert the scene to FBX: Assimp is not available!"); throw std::runtime_error("Unable to convert the scene to FBX: Assimp is not available!");

View File

@@ -6,7 +6,7 @@
#pragma once #pragma once
#include <string> #include <filesystem>
namespace OpenVulkano::Scene namespace OpenVulkano::Scene
{ {
@@ -14,11 +14,11 @@ namespace OpenVulkano::Scene
class MeshWriter class MeshWriter
{ {
public: public:
[[deprecated]] static void WriteAsOBJ(Geometry* geometry, const std::string& filePath); static void WriteAsOBJ(Geometry* geometry, const std::filesystem::path& filePath);
[[deprecated]] static void WriteAsUSD(Geometry* geometry, const std::string& filePath); static void WriteAsUSD(Geometry* geometry, const std::filesystem::path& filePath);
[[deprecated]] static void WriteObjAsZip(Geometry* geometry, const std::string& texturePath, const std::string& zipPath); static void WriteObjAsZip(Geometry* geometry, const std::filesystem::path& texturePath, const std::filesystem::path& zipPath);
[[deprecated]] static void WriteAsUSDZ(Geometry* geometry, const std::string& texturePath, const std::string& usdzPath); static void WriteAsUSDZ(Geometry* geometry, const std::filesystem::path& texturePath, const std::filesystem::path& usdzPath);
[[deprecated]] static void WriteAsFBX(Geometry* geometry, const std::string& texturePath, const std::string& fbxPath); static void WriteAsFBX(Geometry* geometry, const std::filesystem::path& texturePath, const std::filesystem::path& fbxPath);
[[deprecated]] static void WriteAsSTL(Geometry* geometry, const std::string& filePath, bool binary); static void WriteAsSTL(Geometry* geometry, const std::filesystem::path& filePath, bool binary);
}; };
} }

View File

@@ -36,7 +36,7 @@ namespace
namespace OpenVulkano::Scene namespace OpenVulkano::Scene
{ {
void MeshLoader::ParseAssimpFile(Geometry *geometry, const std::string& file) void MeshLoader::ParseAssimpFile(Geometry *geometry, const std::filesystem::path& file)
{ {
#ifdef ASSIMP_AVAILABLE #ifdef ASSIMP_AVAILABLE
Assimp::Importer importer; Assimp::Importer importer;
@@ -45,10 +45,10 @@ namespace OpenVulkano::Scene
aiProcess_ImproveCacheLocality | aiProcess_RemoveRedundantMaterials | aiProcess_GenUVCoords | aiProcess_TransformUVCoords | aiProcess_ImproveCacheLocality | aiProcess_RemoveRedundantMaterials | aiProcess_GenUVCoords | aiProcess_TransformUVCoords |
aiProcess_ConvertToLeftHanded | aiProcess_PreTransformVertices | aiProcess_OptimizeGraph; aiProcess_ConvertToLeftHanded | aiProcess_PreTransformVertices | aiProcess_OptimizeGraph;
const aiScene* scene = importer.ReadFile(file, flags); const aiScene* scene = importer.ReadFile(file.string().c_str(), flags);
if (!scene) throw std::runtime_error("Failed to load file \"" + file + "\" Error: " + importer.GetErrorString()); if (!scene) throw std::runtime_error("Failed to load file \"" + file.string() + "\" Error: " + importer.GetErrorString());
if (!scene->HasMeshes()) throw std::runtime_error("File \"" + file + "\" does not have any meshes"); if (!scene->HasMeshes()) throw std::runtime_error("File \"" + file.string() + "\" does not have any meshes");
if (scene->mNumMeshes > 1) Logger::DATA->warn("File {0} contains more than one mesh. Only first one will be loaded", file); if (scene->mNumMeshes > 1) Logger::DATA->warn("File {0} contains more than one mesh. Only first one will be loaded", file.string());
aiMesh *mesh = scene->mMeshes[0]; aiMesh *mesh = scene->mMeshes[0];
geometry->aabb.Reset(); geometry->aabb.Reset();
@@ -91,15 +91,15 @@ namespace OpenVulkano::Scene
#endif #endif
} }
void MeshLoader::ParseUSDFile(Geometry *geometry, const std::string& file) void MeshLoader::ParseUSDFile(Geometry *geometry, const std::filesystem::path& file)
{ {
tinyusdz::Stage stage; tinyusdz::Stage stage;
std::string warning, err; std::string warning, err;
auto result = tinyusdz::LoadUSDFromFile(file, &stage, &warning, &err); auto result = tinyusdz::LoadUSDFromFile(file.string().c_str(), &stage, &warning, &err);
if (!result) if (!result)
{ {
throw std::runtime_error("Failed to load USD file: " + file); throw std::runtime_error("Failed to load USD file: " + file.string());
} }
for (auto &prim : stage.root_prims()) for (auto &prim : stage.root_prims())
@@ -148,14 +148,15 @@ namespace OpenVulkano::Scene
} }
} }
} }
throw std::runtime_error("No mesh found inside a xform in USD file: " + file); throw std::runtime_error("No mesh found inside a xform in USD file: " + file.string());
} }
Geometry* MeshLoader::LoadFromFile(const std::string& file) Geometry* MeshLoader::LoadFromFile(const std::filesystem::path& file)
{ {
Geometry* geometry = new Geometry(); Geometry* geometry = new Geometry();
if (ends_with(file, ".usd") || ends_with(file, ".usda") || ends_with(file, ".usdc") || ends_with(file, ".usdz")) std::string fileStr = file.string();
if (ends_with(fileStr, ".usd") || ends_with(fileStr, ".usda") || ends_with(fileStr, ".usdc") || ends_with(fileStr, ".usdz"))
{ {
ParseUSDFile(geometry, file); ParseUSDFile(geometry, file);
} }

View File

@@ -6,6 +6,7 @@
#pragma once #pragma once
#include <filesystem>
#include <string> #include <string>
namespace OpenVulkano::Scene namespace OpenVulkano::Scene
@@ -13,9 +14,9 @@ namespace OpenVulkano::Scene
class Geometry; class Geometry;
class MeshLoader class MeshLoader
{ {
[[deprecated]] static void ParseAssimpFile(Geometry *geometry, const std::string& file); static void ParseAssimpFile(Geometry *geometry, const std::filesystem::path& file);
[[deprecated]] static void ParseUSDFile(Geometry *geometry, const std::string& file); static void ParseUSDFile(Geometry *geometry, const std::filesystem::path& file);
public: public:
[[deprecated]] static Geometry* LoadFromFile(const std::string& file); static Geometry* LoadFromFile(const std::filesystem::path& file);
}; };
} }