diff options
Diffstat (limited to 'intern/mantaflow/intern/MANTA_main.cpp')
-rw-r--r-- | intern/mantaflow/intern/MANTA_main.cpp | 1174 |
1 files changed, 565 insertions, 609 deletions
diff --git a/intern/mantaflow/intern/MANTA_main.cpp b/intern/mantaflow/intern/MANTA_main.cpp index bf58e22938a..54b728b734e 100644 --- a/intern/mantaflow/intern/MANTA_main.cpp +++ b/intern/mantaflow/intern/MANTA_main.cpp @@ -48,7 +48,6 @@ #include "MEM_guardedalloc.h" -std::atomic<bool> MANTA::mantaInitialized(false); std::atomic<int> MANTA::solverID(0); int MANTA::with_debug(0); @@ -523,6 +522,7 @@ MANTA::~MANTA() // Destruction string for Python std::string tmpString = ""; std::vector<std::string> pythonCommands; + bool result = false; tmpString += manta_import; tmpString += fluid_delete_all; @@ -530,11 +530,15 @@ MANTA::~MANTA() // Leave out mmd argument in parseScript since only looking up IDs std::string finalString = parseScript(tmpString); pythonCommands.push_back(finalString); - runPythonString(pythonCommands); + result = runPythonString(pythonCommands); + + assert(result); + (void)result; // not needed in release } -void MANTA::runPythonString(std::vector<std::string> commands) +bool MANTA::runPythonString(std::vector<std::string> commands) { + int success = -1; PyGILState_STATE gilstate = PyGILState_Ensure(); for (std::vector<std::string>::iterator it = commands.begin(); it != commands.end(); ++it) { std::string command = *it; @@ -546,19 +550,23 @@ void MANTA::runPythonString(std::vector<std::string> commands) memcpy(buffer, command.data(), cmdLength); buffer[cmdLength] = '\0'; - PyRun_SimpleString(buffer); + success = PyRun_SimpleString(buffer); delete[] buffer; #else - PyRun_SimpleString(command.c_str()); + success = PyRun_SimpleString(command.c_str()); #endif } PyGILState_Release(gilstate); + + /* PyRun_SimpleString returns 0 on success, -1 when an error occurred. */ + assert(success == 0); + return (success != -1); } void MANTA::initializeMantaflow() { if (with_debug) - std::cout << "Initializing Mantaflow" << std::endl; + std::cout << "Fluid: Initializing Mantaflow framework." << std::endl; std::string filename = "manta_scene_" + std::to_string(mCurrentID) + ".py"; std::vector<std::string> fill = std::vector<std::string>(); @@ -568,18 +576,16 @@ void MANTA::initializeMantaflow() PyGILState_STATE gilstate = PyGILState_Ensure(); Pb::setup(filename, fill); // Namespace from Mantaflow (registry) PyGILState_Release(gilstate); - mantaInitialized = true; } void MANTA::terminateMantaflow() { if (with_debug) - std::cout << "Terminating Mantaflow" << std::endl; + std::cout << "Fluid: Releasing Mantaflow framework." << std::endl; PyGILState_STATE gilstate = PyGILState_Ensure(); Pb::finalize(); // Namespace from Mantaflow (registry) PyGILState_Release(gilstate); - mantaInitialized = false; } static std::string getCacheFileEnding(char cache_format) @@ -589,19 +595,19 @@ static std::string getCacheFileEnding(char cache_format) switch (cache_format) { case FLUID_DOMAIN_FILE_UNI: - return ".uni"; + return FLUID_DOMAIN_EXTENSION_UNI; case FLUID_DOMAIN_FILE_OPENVDB: - return ".vdb"; + return FLUID_DOMAIN_EXTENSION_OPENVDB; case FLUID_DOMAIN_FILE_RAW: - return ".raw"; + return FLUID_DOMAIN_EXTENSION_RAW; case FLUID_DOMAIN_FILE_BIN_OBJECT: - return ".bobj.gz"; + return FLUID_DOMAIN_EXTENSION_BINOBJ; case FLUID_DOMAIN_FILE_OBJECT: - return ".obj"; + return FLUID_DOMAIN_EXTENSION_OBJ; default: - if (MANTA::with_debug) - std::cout << "Error: Could not find file extension" << std::endl; - return ".uni"; + std::cerr << "Fluid Error -- Could not find file extension. Using default file extension." + << std::endl; + return FLUID_DOMAIN_EXTENSION_UNI; } } @@ -618,8 +624,7 @@ std::string MANTA::getRealValue(const std::string &varName, FluidModifierData *m } if (!mmd) { - if (with_debug) - std::cout << "Invalid modifier data in getRealValue()" << std::endl; + std::cerr << "Fluid Error -- Invalid modifier data." << std::endl; ss << "ERROR - INVALID MODIFIER DATA"; return ss.str(); } @@ -994,7 +999,7 @@ std::string MANTA::getRealValue(const std::string &varName, FluidModifierData *m else if (varName == "USING_DIFFUSION") ss << (mmd->domain->flags & FLUID_DOMAIN_USE_DIFFUSION ? "True" : "False"); else - std::cout << "ERROR: Unknown option: " << varName << std::endl; + std::cerr << "Fluid Error -- Unknown option: " << varName << std::endl; return ss.str(); } @@ -1034,54 +1039,50 @@ std::string MANTA::parseScript(const std::string &setup_string, FluidModifierDat return res.str(); } -int MANTA::updateFlipStructures(FluidModifierData *mmd, int framenr) +bool MANTA::updateFlipStructures(FluidModifierData *mmd, int framenr) { if (MANTA::with_debug) std::cout << "MANTA::updateFlipStructures()" << std::endl; mFlipFromFile = false; - // Ensure empty data structures at start - if (mFlipParticleData) - mFlipParticleData->clear(); - if (mFlipParticleVelocity) - mFlipParticleVelocity->clear(); - if (!mUsingLiquid) - return 0; + return false; if (BLI_path_is_rel(mmd->domain->cache_directory)) - return 0; + return false; - std::ostringstream ss; - char cacheDir[FILE_MAX], targetFile[FILE_MAX]; - cacheDir[0] = '\0'; - targetFile[0] = '\0'; + int result = 0; + int expected = 0; /* Expected number of read successes for this frame. */ - std::string pformat = getCacheFileEnding(mmd->domain->cache_particle_format); - BLI_path_join( - cacheDir, sizeof(cacheDir), mmd->domain->cache_directory, FLUID_DOMAIN_DIR_DATA, nullptr); + /* Ensure empty data structures at start. */ + if (!mFlipParticleData || !mFlipParticleVelocity) + return false; - ss << "pp_####" << pformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); + mFlipParticleData->clear(); + mFlipParticleVelocity->clear(); - if (BLI_exists(targetFile)) { - updateParticlesFromFile(targetFile, false, false); - } + std::string pformat = getCacheFileEnding(mmd->domain->cache_particle_format); + std::string file = getFile( + mmd, FLUID_DOMAIN_DIR_DATA, FLUID_DOMAIN_FILE_PP, pformat.c_str(), framenr); - ss.str(""); - ss << "pVel_####" << pformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); + expected += 1; + if (BLI_exists(file.c_str())) { + result += updateParticlesFromFile(file, false, false); + assert(result == expected); + } - if (BLI_exists(targetFile)) { - updateParticlesFromFile(targetFile, false, true); + file = getFile(mmd, FLUID_DOMAIN_DIR_DATA, FLUID_DOMAIN_FILE_PVEL, pformat.c_str(), framenr); + expected += 1; + if (BLI_exists(file.c_str())) { + result += updateParticlesFromFile(file, false, true); + assert(result == expected); } + mFlipFromFile = true; - return 1; + return (result == expected); } -int MANTA::updateMeshStructures(FluidModifierData *mmd, int framenr) +bool MANTA::updateMeshStructures(FluidModifierData *mmd, int framenr) { if (MANTA::with_debug) std::cout << "MANTA::updateMeshStructures()" << std::endl; @@ -1089,51 +1090,45 @@ int MANTA::updateMeshStructures(FluidModifierData *mmd, int framenr) mMeshFromFile = false; if (!mUsingMesh) - return 0; + return false; if (BLI_path_is_rel(mmd->domain->cache_directory)) - return 0; + return false; - // Ensure empty data structures at start - if (mMeshNodes) - mMeshNodes->clear(); - if (mMeshTriangles) - mMeshTriangles->clear(); - if (mMeshVelocities) - mMeshVelocities->clear(); + int result = 0; + int expected = 0; /* Expected number of read successes for this frame. */ - std::ostringstream ss; - char cacheDir[FILE_MAX], targetFile[FILE_MAX]; - cacheDir[0] = '\0'; - targetFile[0] = '\0'; + /* Ensure empty data structures at start. */ + if (!mMeshNodes || !mMeshTriangles || !mMeshVelocities) + return false; + + mMeshNodes->clear(); + mMeshTriangles->clear(); + mMeshVelocities->clear(); std::string mformat = getCacheFileEnding(mmd->domain->cache_mesh_format); std::string dformat = getCacheFileEnding(mmd->domain->cache_data_format); - BLI_path_join( - cacheDir, sizeof(cacheDir), mmd->domain->cache_directory, FLUID_DOMAIN_DIR_MESH, nullptr); + std::string file = getFile(mmd, FLUID_DOMAIN_DIR_MESH, FLUID_DOMAIN_FILE_MESH, mformat, framenr); - ss << "lMesh_####" << mformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - - if (BLI_exists(targetFile)) { - updateMeshFromFile(targetFile); + expected += 1; + if (BLI_exists(file.c_str())) { + result += updateMeshFromFile(file); + assert(result == expected); } if (mUsingMVel) { - ss.str(""); - ss << "lVelMesh_####" << dformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - - if (BLI_exists(targetFile)) { - updateMeshFromFile(targetFile); + file = getFile(mmd, FLUID_DOMAIN_DIR_MESH, FLUID_DOMAIN_FILE_MESHVEL, dformat, framenr); + expected += 1; + if (BLI_exists(file.c_str())) { + result += updateMeshFromFile(file); + assert(result == expected); } } + mMeshFromFile = true; - return 1; + return (result == expected); } -int MANTA::updateParticleStructures(FluidModifierData *mmd, int framenr) +bool MANTA::updateParticleStructures(FluidModifierData *mmd, int framenr) { if (MANTA::with_debug) std::cout << "MANTA::updateParticleStructures()" << std::endl; @@ -1141,60 +1136,50 @@ int MANTA::updateParticleStructures(FluidModifierData *mmd, int framenr) mParticlesFromFile = false; if (!mUsingDrops && !mUsingBubbles && !mUsingFloats && !mUsingTracers) - return 0; + return false; if (BLI_path_is_rel(mmd->domain->cache_directory)) - return 0; + return false; - // Ensure empty data structures at start - if (mSndParticleData) - mSndParticleData->clear(); - if (mSndParticleVelocity) - mSndParticleVelocity->clear(); - if (mSndParticleLife) - mSndParticleLife->clear(); + int result = 0; + int expected = 0; /* Expected number of read successes for this frame. */ - std::ostringstream ss; - char cacheDir[FILE_MAX], targetFile[FILE_MAX]; - cacheDir[0] = '\0'; - targetFile[0] = '\0'; + /* Ensure empty data structures at start. */ + if (!mSndParticleData || !mSndParticleVelocity || !mSndParticleLife) + return false; - std::string pformat = getCacheFileEnding(mmd->domain->cache_particle_format); - BLI_path_join(cacheDir, - sizeof(cacheDir), - mmd->domain->cache_directory, - FLUID_DOMAIN_DIR_PARTICLES, - nullptr); + mSndParticleData->clear(); + mSndParticleVelocity->clear(); + mSndParticleLife->clear(); - ss << "ppSnd_####" << pformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); + std::string pformat = getCacheFileEnding(mmd->domain->cache_particle_format); + std::string file = getFile( + mmd, FLUID_DOMAIN_DIR_PARTICLES, FLUID_DOMAIN_FILE_PPSND, pformat, framenr); - if (BLI_exists(targetFile)) { - updateParticlesFromFile(targetFile, true, false); + expected += 1; + if (BLI_exists(file.c_str())) { + result += updateParticlesFromFile(file, true, false); + assert(result == expected); } - ss.str(""); - ss << "pVelSnd_####" << pformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - - if (BLI_exists(targetFile)) { - updateParticlesFromFile(targetFile, true, true); + file = getFile(mmd, FLUID_DOMAIN_DIR_PARTICLES, FLUID_DOMAIN_FILE_PVELSND, pformat, framenr); + expected += 1; + if (BLI_exists(file.c_str())) { + result += updateParticlesFromFile(file, true, true); + assert(result == expected); } - ss.str(""); - ss << "pLifeSnd_####" << pformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - - if (BLI_exists(targetFile)) { - updateParticlesFromFile(targetFile, true, false); + file = getFile(mmd, FLUID_DOMAIN_DIR_PARTICLES, FLUID_DOMAIN_FILE_PLIFESND, pformat, framenr); + expected += 1; + if (BLI_exists(file.c_str())) { + result += updateParticlesFromFile(file, true, false); + assert(result == expected); } + mParticlesFromFile = true; - return 1; + return (result == expected); } -int MANTA::updateSmokeStructures(FluidModifierData *mmd, int framenr) +bool MANTA::updateSmokeStructures(FluidModifierData *mmd, int framenr) { if (MANTA::with_debug) std::cout << "MANTA::updateGridStructures()" << std::endl; @@ -1202,119 +1187,86 @@ int MANTA::updateSmokeStructures(FluidModifierData *mmd, int framenr) mSmokeFromFile = false; if (!mUsingSmoke) - return 0; + return false; if (BLI_path_is_rel(mmd->domain->cache_directory)) - return 0; + return false; int result = 0; int expected = 0; /* Expected number of read successes for this frame. */ - std::ostringstream ss; - char cacheDir[FILE_MAX], targetFile[FILE_MAX]; - cacheDir[0] = '\0'; - targetFile[0] = '\0'; - std::string dformat = getCacheFileEnding(mmd->domain->cache_data_format); - BLI_path_join( - cacheDir, sizeof(cacheDir), mmd->domain->cache_directory, FLUID_DOMAIN_DIR_DATA, nullptr); + std::string file = getFile( + mmd, FLUID_DOMAIN_DIR_DATA, FLUID_DOMAIN_FILE_DENSITY, dformat, framenr); expected += 1; - ss.str(""); - ss << "density_####" << dformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) { - return 0; + if (BLI_exists(file.c_str())) { + result += updateGridFromFile(file, mDensity, false); + assert(result == expected); } - result += updateGridFromFile(targetFile, mDensity, false); + file = getFile(mmd, FLUID_DOMAIN_DIR_DATA, FLUID_DOMAIN_FILE_SHADOW, dformat, framenr); expected += 1; - ss.str(""); - ss << "shadow_####" << dformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) { - return 0; + if (BLI_exists(file.c_str())) { + result += updateGridFromFile(file, mShadow, false); + assert(result == expected); } - result += updateGridFromFile(targetFile, mShadow, false); if (mUsingHeat) { + file = getFile(mmd, FLUID_DOMAIN_DIR_DATA, FLUID_DOMAIN_FILE_HEAT, dformat, framenr); expected += 1; - ss.str(""); - ss << "heat_####" << dformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) { - return 0; + if (BLI_exists(file.c_str())) { + result += updateGridFromFile(file, mHeat, false); + assert(result == expected); } - result += updateGridFromFile(targetFile, mHeat, false); } if (mUsingColors) { + file = getFile(mmd, FLUID_DOMAIN_DIR_DATA, FLUID_DOMAIN_FILE_COLORR, dformat, framenr); expected += 3; - ss.str(""); - ss << "color_r_####" << dformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) { - return 0; + if (BLI_exists(file.c_str())) { + result += updateGridFromFile(file, mColorR, false); + assert(result == expected); } - result += updateGridFromFile(targetFile, mColorR, false); - ss.str(""); - ss << "color_g_####" << dformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) { - return 0; + file = getFile(mmd, FLUID_DOMAIN_DIR_DATA, FLUID_DOMAIN_FILE_COLORG, dformat, framenr); + if (BLI_exists(file.c_str())) { + result += updateGridFromFile(file, mColorG, false); + assert(result == expected); } - result += updateGridFromFile(targetFile, mColorG, false); - ss.str(""); - ss << "color_b_####" << dformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) { - return 0; + file = getFile(mmd, FLUID_DOMAIN_DIR_DATA, FLUID_DOMAIN_FILE_COLORB, dformat, framenr); + if (BLI_exists(file.c_str())) { + result += updateGridFromFile(file, mColorB, false); + assert(result == expected); } - result += updateGridFromFile(targetFile, mColorB, false); } if (mUsingFire) { + file = getFile(mmd, FLUID_DOMAIN_DIR_DATA, FLUID_DOMAIN_FILE_FLAME, dformat, framenr); expected += 3; - ss.str(""); - ss << "flame_####" << dformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) { - return 0; + if (BLI_exists(file.c_str())) { + result += updateGridFromFile(file, mFlame, false); + assert(result == expected); } - result += updateGridFromFile(targetFile, mFlame, false); - ss.str(""); - ss << "fuel_####" << dformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) { - return 0; + file = getFile(mmd, FLUID_DOMAIN_DIR_DATA, FLUID_DOMAIN_FILE_FUEL, dformat, framenr); + if (BLI_exists(file.c_str())) { + result += updateGridFromFile(file, mFuel, false); + assert(result == expected); } - result += updateGridFromFile(targetFile, mFuel, false); - ss.str(""); - ss << "react_####" << dformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) { - return 0; + file = getFile(mmd, FLUID_DOMAIN_DIR_DATA, FLUID_DOMAIN_FILE_REACT, dformat, framenr); + if (BLI_exists(file.c_str())) { + result += updateGridFromFile(file, mReact, false); + assert(result == expected); } - result += updateGridFromFile(targetFile, mReact, false); } mSmokeFromFile = true; - return (result == expected) ? 1 : 0; + return (result == expected); } -int MANTA::updateNoiseStructures(FluidModifierData *mmd, int framenr) +bool MANTA::updateNoiseStructures(FluidModifierData *mmd, int framenr) { if (MANTA::with_debug) std::cout << "MANTA::updateNoiseStructures()" << std::endl; @@ -1322,114 +1274,75 @@ int MANTA::updateNoiseStructures(FluidModifierData *mmd, int framenr) mNoiseFromFile = false; if (!mUsingSmoke || !mUsingNoise) - return 0; + return false; if (BLI_path_is_rel(mmd->domain->cache_directory)) - return 0; + return false; int result = 0; int expected = 0; /* Expected number of read successes for this frame. */ - std::ostringstream ss; - char cacheDirData[FILE_MAX], cacheDirNoise[FILE_MAX], targetFile[FILE_MAX]; - cacheDirData[0] = '\0'; - cacheDirNoise[0] = '\0'; - targetFile[0] = '\0'; - std::string dformat = getCacheFileEnding(mmd->domain->cache_data_format); std::string nformat = getCacheFileEnding(mmd->domain->cache_noise_format); - BLI_path_join(cacheDirData, - sizeof(cacheDirData), - mmd->domain->cache_directory, - FLUID_DOMAIN_DIR_DATA, - nullptr); - BLI_path_join(cacheDirNoise, - sizeof(cacheDirNoise), - mmd->domain->cache_directory, - FLUID_DOMAIN_DIR_NOISE, - nullptr); + std::string file = getFile( + mmd, FLUID_DOMAIN_DIR_NOISE, FLUID_DOMAIN_FILE_DENSITYNOISE, nformat, framenr); expected += 1; - ss.str(""); - ss << "density_noise_####" << nformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDirNoise, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) { - return 0; + if (BLI_exists(file.c_str())) { + result += updateGridFromFile(file, mDensityHigh, true); + assert(result == expected); } - result += updateGridFromFile(targetFile, mDensityHigh, true); + file = getFile(mmd, FLUID_DOMAIN_DIR_DATA, FLUID_DOMAIN_FILE_SHADOW, dformat, framenr); expected += 1; - ss.str(""); - ss << "shadow_####" << dformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDirData, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) { - return 0; + if (BLI_exists(file.c_str())) { + result += updateGridFromFile(file, mShadow, false); + assert(result == expected); } - result += updateGridFromFile(targetFile, mShadow, false); if (mUsingColors) { + file = getFile(mmd, FLUID_DOMAIN_DIR_NOISE, FLUID_DOMAIN_FILE_COLORRNOISE, nformat, framenr); expected += 3; - ss.str(""); - ss << "color_r_noise_####" << nformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDirNoise, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) { - return 0; + if (BLI_exists(file.c_str())) { + result += updateGridFromFile(file, mColorRHigh, true); + assert(result == expected); } - result += updateGridFromFile(targetFile, mColorRHigh, true); - ss.str(""); - ss << "color_g_noise_####" << nformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDirNoise, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) { - return 0; + file = getFile(mmd, FLUID_DOMAIN_DIR_NOISE, FLUID_DOMAIN_FILE_COLORGNOISE, nformat, framenr); + if (BLI_exists(file.c_str())) { + result += updateGridFromFile(file, mColorGHigh, true); + assert(result == expected); } - result += updateGridFromFile(targetFile, mColorGHigh, true); - ss.str(""); - ss << "color_b_noise_####" << nformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDirNoise, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) { - return 0; + file = getFile(mmd, FLUID_DOMAIN_DIR_NOISE, FLUID_DOMAIN_FILE_COLORBNOISE, nformat, framenr); + if (BLI_exists(file.c_str())) { + result += updateGridFromFile(file, mColorBHigh, true); + assert(result == expected); } - result += updateGridFromFile(targetFile, mColorBHigh, true); } if (mUsingFire) { + file = getFile(mmd, FLUID_DOMAIN_DIR_NOISE, FLUID_DOMAIN_FILE_FLAMENOISE, nformat, framenr); expected += 3; - ss.str(""); - ss << "flame_noise_####" << nformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDirNoise, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) { - return 0; + if (BLI_exists(file.c_str())) { + result += updateGridFromFile(file, mFlameHigh, true); + assert(result == expected); } - result += updateGridFromFile(targetFile, mFlameHigh, true); - ss.str(""); - ss << "fuel_noise_####" << nformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDirNoise, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) { - return 0; + file = getFile(mmd, FLUID_DOMAIN_DIR_NOISE, FLUID_DOMAIN_FILE_FUELNOISE, nformat, framenr); + if (BLI_exists(file.c_str())) { + result += updateGridFromFile(file, mFuelHigh, true); + assert(result == expected); } - result += updateGridFromFile(targetFile, mFuelHigh, true); - ss.str(""); - ss << "react_noise_####" << nformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDirNoise, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) { - return 0; + file = getFile(mmd, FLUID_DOMAIN_DIR_NOISE, FLUID_DOMAIN_FILE_REACTNOISE, nformat, framenr); + if (BLI_exists(file.c_str())) { + result += updateGridFromFile(file, mReactHigh, true); + assert(result == expected); } - result += updateGridFromFile(targetFile, mReactHigh, true); } mNoiseFromFile = true; - return (result == expected) ? 1 : 0; + return (result == expected); } /* Dirty hack: Needed to format paths from python code that is run via PyRun_SimpleString */ @@ -1446,32 +1359,26 @@ static std::string escapeSlashes(std::string const &s) return result; } -int MANTA::writeConfiguration(FluidModifierData *mmd, int framenr) +bool MANTA::writeConfiguration(FluidModifierData *mmd, int framenr) { if (with_debug) std::cout << "MANTA::writeConfiguration()" << std::endl; FluidDomainSettings *mds = mmd->domain; - std::ostringstream ss; - char cacheDir[FILE_MAX], targetFile[FILE_MAX]; - cacheDir[0] = '\0'; - targetFile[0] = '\0'; - std::string dformat = ".uni"; + std::string directory = getDirectory(mmd, FLUID_DOMAIN_DIR_CONFIG); + std::string format = FLUID_DOMAIN_EXTENSION_UNI; + std::string file = getFile( + mmd, FLUID_DOMAIN_DIR_CONFIG, FLUID_DOMAIN_FILE_CONFIG, format, framenr); - BLI_path_join( - cacheDir, sizeof(cacheDir), mmd->domain->cache_directory, FLUID_DOMAIN_DIR_CONFIG, nullptr); - BLI_path_make_safe(cacheDir); - BLI_dir_create_recursive(cacheDir); /* Create 'config' subdir if it does not exist already */ + /* Create 'config' subdir if it does not exist already. */ + BLI_dir_create_recursive(directory.c_str()); - ss.str(""); - ss << "config_####" << dformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - - gzFile gzf = (gzFile)BLI_gzopen(targetFile, "wb1"); // do some compression - if (!gzf) - std::cerr << "writeConfiguration: can't open file: " << targetFile << std::endl; + gzFile gzf = (gzFile)BLI_gzopen(file.c_str(), "wb1"); // do some compression + if (!gzf) { + std::cerr << "Fluid Error -- Cannot open file " << file << std::endl; + return false; + } gzwrite(gzf, &mds->active_fields, sizeof(int)); gzwrite(gzf, &mds->res, 3 * sizeof(int)); @@ -1488,12 +1395,10 @@ int MANTA::writeConfiguration(FluidModifierData *mmd, int framenr) gzwrite(gzf, &mds->res_max, 3 * sizeof(int)); gzwrite(gzf, &mds->active_color, 3 * sizeof(float)); - gzclose(gzf); - - return 1; + return (gzclose(gzf) == Z_OK); } -int MANTA::writeData(FluidModifierData *mmd, int framenr) +bool MANTA::writeData(FluidModifierData *mmd, int framenr) { if (with_debug) std::cout << "MANTA::writeData()" << std::endl; @@ -1501,72 +1406,54 @@ int MANTA::writeData(FluidModifierData *mmd, int framenr) std::ostringstream ss; std::vector<std::string> pythonCommands; - char cacheDirData[FILE_MAX]; - cacheDirData[0] = '\0'; - + std::string directory = getDirectory(mmd, FLUID_DOMAIN_DIR_DATA); std::string dformat = getCacheFileEnding(mmd->domain->cache_data_format); std::string pformat = getCacheFileEnding(mmd->domain->cache_particle_format); bool final_cache = (mmd->domain->cache_type == FLUID_DOMAIN_CACHE_FINAL); std::string resumable_cache = (final_cache) ? "False" : "True"; - BLI_path_join(cacheDirData, - sizeof(cacheDirData), - mmd->domain->cache_directory, - FLUID_DOMAIN_DIR_DATA, - nullptr); - BLI_path_make_safe(cacheDirData); - ss.str(""); - ss << "fluid_save_data_" << mCurrentID << "('" << escapeSlashes(cacheDirData) << "', " << framenr + ss << "fluid_save_data_" << mCurrentID << "('" << escapeSlashes(directory) << "', " << framenr << ", '" << dformat << "', " << resumable_cache << ")"; pythonCommands.push_back(ss.str()); if (mUsingSmoke) { ss.str(""); - ss << "smoke_save_data_" << mCurrentID << "('" << escapeSlashes(cacheDirData) << "', " - << framenr << ", '" << dformat << "', " << resumable_cache << ")"; + ss << "smoke_save_data_" << mCurrentID << "('" << escapeSlashes(directory) << "', " << framenr + << ", '" << dformat << "', " << resumable_cache << ")"; pythonCommands.push_back(ss.str()); } if (mUsingLiquid) { ss.str(""); - ss << "liquid_save_data_" << mCurrentID << "('" << escapeSlashes(cacheDirData) << "', " - << framenr << ", '" << dformat << "', " << resumable_cache << ")"; + ss << "liquid_save_data_" << mCurrentID << "('" << escapeSlashes(directory) << "', " << framenr + << ", '" << dformat << "', " << resumable_cache << ")"; pythonCommands.push_back(ss.str()); } - runPythonString(pythonCommands); - return 1; + return runPythonString(pythonCommands); } -int MANTA::readConfiguration(FluidModifierData *mmd, int framenr) +bool MANTA::readConfiguration(FluidModifierData *mmd, int framenr) { if (with_debug) std::cout << "MANTA::readConfiguration()" << std::endl; FluidDomainSettings *mds = mmd->domain; - std::ostringstream ss; - char cacheDir[FILE_MAX], targetFile[FILE_MAX]; - cacheDir[0] = '\0'; - targetFile[0] = '\0'; float dummy; - std::string dformat = ".uni"; - - BLI_path_join( - cacheDir, sizeof(cacheDir), mmd->domain->cache_directory, FLUID_DOMAIN_DIR_CONFIG, nullptr); - BLI_path_make_safe(cacheDir); + std::string directory = getDirectory(mmd, FLUID_DOMAIN_DIR_CONFIG); + std::string format = FLUID_DOMAIN_EXTENSION_UNI; + std::string file = getFile( + mmd, FLUID_DOMAIN_DIR_CONFIG, FLUID_DOMAIN_FILE_CONFIG, format, framenr); - ss.str(""); - ss << "config_####" << dformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - - if (!BLI_exists(targetFile)) - return 0; + if (!hasConfig(mmd, framenr)) + return false; - gzFile gzf = (gzFile)BLI_gzopen(targetFile, "rb"); // do some compression - if (!gzf) - std::cerr << "readConfiguration: can't open file: " << targetFile << std::endl; + gzFile gzf = (gzFile)BLI_gzopen(file.c_str(), "rb"); // do some compression + if (!gzf) { + std::cerr << "Fluid Error -- Cannot open file " << file << std::endl; + return false; + } gzread(gzf, &mds->active_fields, sizeof(int)); gzread(gzf, &mds->res, 3 * sizeof(int)); @@ -1584,271 +1471,191 @@ int MANTA::readConfiguration(FluidModifierData *mmd, int framenr) gzread(gzf, &mds->active_color, 3 * sizeof(float)); mds->total_cells = mds->res[0] * mds->res[1] * mds->res[2]; - gzclose(gzf); - return 1; + return (gzclose(gzf) == Z_OK); } -int MANTA::readData(FluidModifierData *mmd, int framenr) +bool MANTA::readData(FluidModifierData *mmd, int framenr) { if (with_debug) std::cout << "MANTA::readData()" << std::endl; if (!mUsingSmoke && !mUsingLiquid) - return 0; + return false; std::ostringstream ss; std::vector<std::string> pythonCommands; + bool result = true; - char cacheDirData[FILE_MAX], targetFile[FILE_MAX]; - cacheDirData[0] = '\0'; - targetFile[0] = '\0'; - + std::string directory = getDirectory(mmd, FLUID_DOMAIN_DIR_DATA); std::string dformat = getCacheFileEnding(mmd->domain->cache_data_format); std::string pformat = getCacheFileEnding(mmd->domain->cache_particle_format); bool final_cache = (mmd->domain->cache_type == FLUID_DOMAIN_CACHE_FINAL); std::string resumable_cache = (final_cache) ? "False" : "True"; - BLI_path_join(cacheDirData, - sizeof(cacheDirData), - mmd->domain->cache_directory, - FLUID_DOMAIN_DIR_DATA, - nullptr); - BLI_path_make_safe(cacheDirData); - - /* Exit early if there is nothing present in the cache for this frame */ - ss.str(""); - if (mUsingSmoke) { - ss << "density_####" << dformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDirData, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) - return 0; - } - if (mUsingLiquid) { - ss << "phi_####" << dformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDirData, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) - return 0; - } + /* Sanity check: Are cache files present? */ + if (!hasData(mmd, framenr)) + return false; ss.str(""); - ss << "fluid_load_data_" << mCurrentID << "('" << escapeSlashes(cacheDirData) << "', " << framenr + ss << "fluid_load_data_" << mCurrentID << "('" << escapeSlashes(directory) << "', " << framenr << ", '" << dformat << "', " << resumable_cache << ")"; pythonCommands.push_back(ss.str()); if (mUsingSmoke) { ss.str(""); - ss << "smoke_load_data_" << mCurrentID << "('" << escapeSlashes(cacheDirData) << "', " - << framenr << ", '" << dformat << "', " << resumable_cache << ")"; + ss << "smoke_load_data_" << mCurrentID << "('" << escapeSlashes(directory) << "', " << framenr + << ", '" << dformat << "', " << resumable_cache << ")"; pythonCommands.push_back(ss.str()); + result &= runPythonString(pythonCommands); } if (mUsingLiquid) { - /* Exit early if there is nothing present in the cache for this frame */ ss.str(""); - ss << "liquid_load_data_" << mCurrentID << "('" << escapeSlashes(cacheDirData) << "', " - << framenr << ", '" << dformat << "', " << resumable_cache << ")"; + ss << "liquid_load_data_" << mCurrentID << "('" << escapeSlashes(directory) << "', " << framenr + << ", '" << dformat << "', " << resumable_cache << ")"; pythonCommands.push_back(ss.str()); + result &= runPythonString(pythonCommands); } - runPythonString(pythonCommands); - return 1; + return result; } -int MANTA::readNoise(FluidModifierData *mmd, int framenr) +bool MANTA::readNoise(FluidModifierData *mmd, int framenr) { if (with_debug) std::cout << "MANTA::readNoise()" << std::endl; if (!mUsingSmoke || !mUsingNoise) - return 0; + return false; std::ostringstream ss; std::vector<std::string> pythonCommands; - char cacheDirNoise[FILE_MAX], targetFile[FILE_MAX]; - cacheDirNoise[0] = '\0'; - targetFile[0] = '\0'; - + std::string directory = getDirectory(mmd, FLUID_DOMAIN_DIR_NOISE); std::string nformat = getCacheFileEnding(mmd->domain->cache_noise_format); bool final_cache = (mmd->domain->cache_type == FLUID_DOMAIN_CACHE_FINAL); std::string resumable_cache = (final_cache) ? "False" : "True"; - BLI_path_join(cacheDirNoise, - sizeof(cacheDirNoise), - mmd->domain->cache_directory, - FLUID_DOMAIN_DIR_NOISE, - nullptr); - BLI_path_make_safe(cacheDirNoise); + /* Sanity check: Are cache files present? */ + if (!hasNoise(mmd, framenr)) + return false; - /* Exit early if there is nothing present in the cache for this frame */ ss.str(""); - ss << "density_noise_####" << nformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDirNoise, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) - return 0; - - ss.str(""); - ss << "smoke_load_noise_" << mCurrentID << "('" << escapeSlashes(cacheDirNoise) << "', " - << framenr << ", '" << nformat << "', " << resumable_cache << ")"; + ss << "smoke_load_noise_" << mCurrentID << "('" << escapeSlashes(directory) << "', " << framenr + << ", '" << nformat << "', " << resumable_cache << ")"; pythonCommands.push_back(ss.str()); - runPythonString(pythonCommands); - return 1; + return runPythonString(pythonCommands); } -/* Deprecated! This function read mesh data via the Manta Python API. +/* Deprecated! This function reads mesh data via the Manta Python API. * MANTA:updateMeshStructures() reads cache files directly from disk * and is preferred due to its better performance. */ -int MANTA::readMesh(FluidModifierData *mmd, int framenr) +bool MANTA::readMesh(FluidModifierData *mmd, int framenr) { if (with_debug) std::cout << "MANTA::readMesh()" << std::endl; if (!mUsingLiquid || !mUsingMesh) - return 0; + return false; std::ostringstream ss; std::vector<std::string> pythonCommands; - char cacheDirMesh[FILE_MAX], targetFile[FILE_MAX]; - cacheDirMesh[0] = '\0'; - targetFile[0] = '\0'; - + std::string directory = getDirectory(mmd, FLUID_DOMAIN_DIR_MESH); std::string mformat = getCacheFileEnding(mmd->domain->cache_mesh_format); std::string dformat = getCacheFileEnding(mmd->domain->cache_data_format); - BLI_path_join(cacheDirMesh, - sizeof(cacheDirMesh), - mmd->domain->cache_directory, - FLUID_DOMAIN_DIR_MESH, - nullptr); - BLI_path_make_safe(cacheDirMesh); - - /* Exit early if there is nothing present in the cache for this frame */ - ss.str(""); - ss << "lMesh_####" << mformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDirMesh, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) - return 0; + /* Sanity check: Are cache files present? */ + if (!hasMesh(mmd, framenr)) + return false; ss.str(""); - ss << "liquid_load_mesh_" << mCurrentID << "('" << escapeSlashes(cacheDirMesh) << "', " - << framenr << ", '" << mformat << "')"; + ss << "liquid_load_mesh_" << mCurrentID << "('" << escapeSlashes(directory) << "', " << framenr + << ", '" << mformat << "')"; pythonCommands.push_back(ss.str()); if (mUsingMVel) { ss.str(""); - ss << "liquid_load_meshvel_" << mCurrentID << "('" << escapeSlashes(cacheDirMesh) << "', " + ss << "liquid_load_meshvel_" << mCurrentID << "('" << escapeSlashes(directory) << "', " << framenr << ", '" << dformat << "')"; pythonCommands.push_back(ss.str()); } - runPythonString(pythonCommands); - return 1; + return runPythonString(pythonCommands); } /* Deprecated! This function reads particle data via the Manta Python API. * MANTA:updateParticleStructures() reads cache files directly from disk * and is preferred due to its better performance. */ -int MANTA::readParticles(FluidModifierData *mmd, int framenr) +bool MANTA::readParticles(FluidModifierData *mmd, int framenr) { if (with_debug) std::cout << "MANTA::readParticles()" << std::endl; if (!mUsingLiquid) - return 0; + return false; if (!mUsingDrops && !mUsingBubbles && !mUsingFloats && !mUsingTracers) - return 0; + return false; std::ostringstream ss; std::vector<std::string> pythonCommands; - char cacheDirParticles[FILE_MAX], targetFile[FILE_MAX]; - cacheDirParticles[0] = '\0'; - targetFile[0] = '\0'; - + std::string directory = getDirectory(mmd, FLUID_DOMAIN_DIR_PARTICLES); std::string pformat = getCacheFileEnding(mmd->domain->cache_particle_format); bool final_cache = (mmd->domain->cache_type == FLUID_DOMAIN_CACHE_FINAL); std::string resumable_cache = (final_cache) ? "False" : "True"; - BLI_path_join(cacheDirParticles, - sizeof(cacheDirParticles), - mmd->domain->cache_directory, - FLUID_DOMAIN_DIR_PARTICLES, - nullptr); - BLI_path_make_safe(cacheDirParticles); - - /* Exit early if there is nothing present in the cache for this frame */ - ss.str(""); - ss << "ppSnd_####" << pformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDirParticles, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) - return 0; + /* Sanity check: Are cache files present? */ + if (!hasParticles(mmd, framenr)) + return false; ss.str(""); - ss << "liquid_load_particles_" << mCurrentID << "('" << escapeSlashes(cacheDirParticles) << "', " + ss << "liquid_load_particles_" << mCurrentID << "('" << escapeSlashes(directory) << "', " << framenr << ", '" << pformat << "', " << resumable_cache << ")"; pythonCommands.push_back(ss.str()); - runPythonString(pythonCommands); - return 1; + return runPythonString(pythonCommands); } -int MANTA::readGuiding(FluidModifierData *mmd, int framenr, bool sourceDomain) +bool MANTA::readGuiding(FluidModifierData *mmd, int framenr, bool sourceDomain) { if (with_debug) std::cout << "MANTA::readGuiding()" << std::endl; if (!mUsingGuiding) - return 0; + return false; if (!mmd->domain) - return 0; + return false; std::ostringstream ss; std::vector<std::string> pythonCommands; - char cacheDirGuiding[FILE_MAX], targetFile[FILE_MAX]; - cacheDirGuiding[0] = '\0'; - targetFile[0] = '\0'; - + std::string directory = (sourceDomain) ? getDirectory(mmd, FLUID_DOMAIN_DIR_DATA) : + getDirectory(mmd, FLUID_DOMAIN_DIR_GUIDE); std::string gformat = getCacheFileEnding(mmd->domain->cache_data_format); - const char *subdir = (sourceDomain) ? FLUID_DOMAIN_DIR_DATA : FLUID_DOMAIN_DIR_GUIDE; - BLI_path_join( - cacheDirGuiding, sizeof(cacheDirGuiding), mmd->domain->cache_directory, subdir, nullptr); - BLI_path_make_safe(cacheDirGuiding); - - /* Exit early if there is nothing present in the cache for this frame */ - ss.str(""); - ss << (sourceDomain ? "vel_####" : "guidevel_####") << gformat; - BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDirGuiding, ss.str().c_str()); - BLI_path_frame(targetFile, framenr, 0); - if (!BLI_exists(targetFile)) - return 0; + /* Sanity check: Are cache files present? */ + if (!hasGuiding(mmd, framenr, sourceDomain)) + return false; if (sourceDomain) { ss.str(""); - ss << "fluid_load_vel_" << mCurrentID << "('" << escapeSlashes(cacheDirGuiding) << "', " - << framenr << ", '" << gformat << "')"; + ss << "fluid_load_vel_" << mCurrentID << "('" << escapeSlashes(directory) << "', " << framenr + << ", '" << gformat << "')"; } else { ss.str(""); - ss << "fluid_load_guiding_" << mCurrentID << "('" << escapeSlashes(cacheDirGuiding) << "', " + ss << "fluid_load_guiding_" << mCurrentID << "('" << escapeSlashes(directory) << "', " << framenr << ", '" << gformat << "')"; } pythonCommands.push_back(ss.str()); - runPythonString(pythonCommands); - return 1; + return runPythonString(pythonCommands); } -int MANTA::bakeData(FluidModifierData *mmd, int framenr) +bool MANTA::bakeData(FluidModifierData *mmd, int framenr) { if (with_debug) std::cout << "MANTA::bakeData()" << std::endl; @@ -1884,11 +1691,10 @@ int MANTA::bakeData(FluidModifierData *mmd, int framenr) << "', '" << gformat << "')"; pythonCommands.push_back(ss.str()); - runPythonString(pythonCommands); - return 1; + return runPythonString(pythonCommands); } -int MANTA::bakeNoise(FluidModifierData *mmd, int framenr) +bool MANTA::bakeNoise(FluidModifierData *mmd, int framenr) { if (with_debug) std::cout << "MANTA::bakeNoise()" << std::endl; @@ -1925,11 +1731,10 @@ int MANTA::bakeNoise(FluidModifierData *mmd, int framenr) << "', " << resumable_cache << ")"; pythonCommands.push_back(ss.str()); - runPythonString(pythonCommands); - return 1; + return runPythonString(pythonCommands); } -int MANTA::bakeMesh(FluidModifierData *mmd, int framenr) +bool MANTA::bakeMesh(FluidModifierData *mmd, int framenr) { if (with_debug) std::cout << "MANTA::bakeMesh()" << std::endl; @@ -1964,11 +1769,10 @@ int MANTA::bakeMesh(FluidModifierData *mmd, int framenr) << "', '" << pformat << "')"; pythonCommands.push_back(ss.str()); - runPythonString(pythonCommands); - return 1; + return runPythonString(pythonCommands); } -int MANTA::bakeParticles(FluidModifierData *mmd, int framenr) +bool MANTA::bakeParticles(FluidModifierData *mmd, int framenr) { if (with_debug) std::cout << "MANTA::bakeParticles()" << std::endl; @@ -2005,11 +1809,10 @@ int MANTA::bakeParticles(FluidModifierData *mmd, int framenr) << pformat << "', " << resumable_cache << ")"; pythonCommands.push_back(ss.str()); - runPythonString(pythonCommands); - return 1; + return runPythonString(pythonCommands); } -int MANTA::bakeGuiding(FluidModifierData *mmd, int framenr) +bool MANTA::bakeGuiding(FluidModifierData *mmd, int framenr) { if (with_debug) std::cout << "MANTA::bakeGuiding()" << std::endl; @@ -2037,11 +1840,10 @@ int MANTA::bakeGuiding(FluidModifierData *mmd, int framenr) << ", '" << gformat << "', " << resumable_cache << ")"; pythonCommands.push_back(ss.str()); - runPythonString(pythonCommands); - return 1; + return runPythonString(pythonCommands); } -void MANTA::updateVariables(FluidModifierData *mmd) +bool MANTA::updateVariables(FluidModifierData *mmd) { std::string tmpString, finalString; std::vector<std::string> pythonCommands; @@ -2068,7 +1870,7 @@ void MANTA::updateVariables(FluidModifierData *mmd) finalString = parseScript(tmpString, mmd); pythonCommands.push_back(finalString); - runPythonString(pythonCommands); + return runPythonString(pythonCommands); } void MANTA::exportSmokeScript(FluidModifierData *mmd) @@ -2436,7 +2238,7 @@ void MANTA::adaptTimestep() runPythonString(pythonCommands); } -void MANTA::updateMeshFromFile(const char *filename) +bool MANTA::updateMeshFromFile(std::string filename) { std::string fname(filename); std::string::size_type idx; @@ -2446,40 +2248,53 @@ void MANTA::updateMeshFromFile(const char *filename) std::string extension = fname.substr(idx + 1); if (extension.compare("gz") == 0) - updateMeshFromBobj(filename); + return updateMeshFromBobj(filename); else if (extension.compare("obj") == 0) - updateMeshFromObj(filename); + return updateMeshFromObj(filename); else if (extension.compare("uni") == 0) - updateMeshFromUni(filename); + return updateMeshFromUni(filename); else - std::cerr << "updateMeshFromFile: invalid file extension in file: " << filename << std::endl; + std::cerr << "Fluid Error -- updateMeshFromFile(): Invalid file extension in file: " + << filename << std::endl; } else { - std::cerr << "updateMeshFromFile: unable to open file: " << filename << std::endl; + std::cerr << "Fluid Error -- updateMeshFromFile(): Unable to open file: " << filename + << std::endl; } + return false; } -void MANTA::updateMeshFromBobj(const char *filename) +bool MANTA::updateMeshFromBobj(std::string filename) { if (with_debug) std::cout << "MANTA::updateMeshFromBobj()" << std::endl; gzFile gzf; - gzf = (gzFile)BLI_gzopen(filename, "rb1"); // do some compression - if (!gzf) - std::cerr << "updateMeshData: unable to open file: " << filename << std::endl; + gzf = (gzFile)BLI_gzopen(filename.c_str(), "rb1"); // do some compression + if (!gzf) { + std::cerr << "Fluid Error -- updateMeshFromBobj(): Unable to open file: " << filename + << std::endl; + return false; + } - int numBuffer = 0; + int numBuffer = 0, readBytes = 0; // Num vertices - gzread(gzf, &numBuffer, sizeof(int)); + readBytes = gzread(gzf, &numBuffer, sizeof(int)); + if (!readBytes) { + std::cerr + << "Fluid Error -- updateMeshFromBobj(): Unable to read number of mesh vertices from " + << filename << std::endl; + gzclose(gzf); + return false; + } if (with_debug) std::cout << "read mesh , num verts: " << numBuffer << " , in file: " << filename << std::endl; int numChunks = (int)(ceil((float)numBuffer / NODE_CHUNK)); - int readLen, readStart, readEnd, readBytes, k; + int readLen, readStart, readEnd, k; if (numBuffer) { // Vertices @@ -2497,11 +2312,11 @@ void MANTA::updateMeshFromBobj(const char *filename) readBytes = gzread(gzf, bufferVerts, readLen * sizeof(float) * 3); if (!readBytes) { - if (with_debug) - std::cerr << "error while reading vertices" << std::endl; + std::cerr << "Fluid Error -- updateMeshFromBobj(): Unable to read mesh vertices from " + << filename << std::endl; MEM_freeN(bufferVerts); gzclose(gzf); - return; + return false; } readStart = (numBuffer - todoVertices); @@ -2521,7 +2336,13 @@ void MANTA::updateMeshFromBobj(const char *filename) } // Num normals - gzread(gzf, &numBuffer, sizeof(int)); + readBytes = gzread(gzf, &numBuffer, sizeof(int)); + if (!readBytes) { + std::cerr << "Fluid Error -- updateMeshFromBobj(): Unable to read number of mesh normals from " + << filename << std::endl; + gzclose(gzf); + return false; + } if (with_debug) std::cout << "read mesh , num normals : " << numBuffer << " , in file: " << filename @@ -2544,11 +2365,11 @@ void MANTA::updateMeshFromBobj(const char *filename) readBytes = gzread(gzf, bufferNormals, readLen * sizeof(float) * 3); if (!readBytes) { - if (with_debug) - std::cerr << "error while reading normals" << std::endl; + std::cerr << "Fluid Error -- updateMeshFromBobj(): Unable to read mesh normals from " + << filename << std::endl; MEM_freeN(bufferNormals); gzclose(gzf); - return; + return false; } readStart = (numBuffer - todoNormals); @@ -2568,10 +2389,17 @@ void MANTA::updateMeshFromBobj(const char *filename) } // Num triangles - gzread(gzf, &numBuffer, sizeof(int)); + readBytes = gzread(gzf, &numBuffer, sizeof(int)); + if (!readBytes) { + std::cerr + << "Fluid Error -- updateMeshFromBobj(): Unable to read number of mesh triangles from " + << filename << std::endl; + gzclose(gzf); + return false; + } if (with_debug) - std::cout << "read mesh , num triangles : " << numBuffer << " , in file: " << filename + std::cout << "Fluid: Read mesh , num triangles : " << numBuffer << " , in file: " << filename << std::endl; numChunks = (int)(ceil((float)numBuffer / TRIANGLE_CHUNK)); @@ -2592,11 +2420,11 @@ void MANTA::updateMeshFromBobj(const char *filename) readBytes = gzread(gzf, bufferTriangles, readLen * sizeof(int) * 3); if (!readBytes) { - if (with_debug) - std::cerr << "error while reading triangles" << std::endl; + std::cerr << "Fluid Error -- updateMeshFromBobj(): Unable to read mesh triangles from " + << filename << std::endl; MEM_freeN(bufferTriangles); gzclose(gzf); - return; + return false; } readStart = (numBuffer - todoTriangles); @@ -2614,10 +2442,10 @@ void MANTA::updateMeshFromBobj(const char *filename) } MEM_freeN(bufferTriangles); } - gzclose(gzf); + return (gzclose(gzf) == Z_OK); } -void MANTA::updateMeshFromObj(const char *filename) +bool MANTA::updateMeshFromObj(std::string filename) { if (with_debug) std::cout << "MANTA::updateMeshFromObj()" << std::endl; @@ -2627,8 +2455,11 @@ void MANTA::updateMeshFromObj(const char *filename) int ibuffer[3]; int cntVerts = 0, cntNormals = 0, cntTris = 0; - if (!ifs.good()) - std::cerr << "updateMeshDataFromObj: unable to open file: " << filename << std::endl; + if (!ifs.good()) { + std::cerr << "Fluid Error -- updateMeshFromObj(): Unable to open file: " << filename + << std::endl; + return false; + } while (ifs.good() && !ifs.eof()) { std::string id; @@ -2644,8 +2475,11 @@ void MANTA::updateMeshFromObj(const char *filename) } else if (id == "vn") { // normals - if (getNumVertices() != cntVerts) - std::cerr << "updateMeshDataFromObj: invalid amount of mesh nodes" << std::endl; + if (getNumVertices() != cntVerts) { + std::cerr << "Fluid Error -- updateMeshFromObj(): Invalid number of mesh nodes in file: " + << filename << std::endl; + return false; + } ifs >> fbuffer[0] >> fbuffer[1] >> fbuffer[2]; MANTA::Node *node = &mMeshNodes->at(cntNormals); @@ -2677,8 +2511,11 @@ void MANTA::updateMeshFromObj(const char *filename) if (face.find('/') != std::string::npos) face = face.substr(0, face.find('/')); // ignore other indices int idx = atoi(face.c_str()) - 1; - if (idx < 0) - std::cerr << "updateMeshDataFromObj: invalid face encountered" << std::endl; + if (idx < 0) { + std::cerr << "Fluid Error -- updateMeshFromObj(): Invalid face encountered in file: " + << filename << std::endl; + return false; + } ibuffer[i] = idx; } MANTA::Triangle triangle; @@ -2695,9 +2532,10 @@ void MANTA::updateMeshFromObj(const char *filename) getline(ifs, id); } ifs.close(); + return true; } -void MANTA::updateMeshFromUni(const char *filename) +bool MANTA::updateMeshFromUni(std::string filename) { if (with_debug) std::cout << "MANTA::updateMeshFromUni()" << std::endl; @@ -2706,12 +2544,22 @@ void MANTA::updateMeshFromUni(const char *filename) float fbuffer[4]; int ibuffer[4]; - gzf = (gzFile)BLI_gzopen(filename, "rb1"); // do some compression - if (!gzf) - std::cout << "updateMeshFromUni: unable to open file" << std::endl; + gzf = (gzFile)BLI_gzopen(filename.c_str(), "rb1"); // do some compression + if (!gzf) { + std::cerr << "Fluid Error -- updateMeshFromUni(): Unable to open file: " << filename + << std::endl; + return false; + } - char ID[5] = {0, 0, 0, 0, 0}; - gzread(gzf, ID, 4); + int readBytes = 0; + char file_magic[5] = {0, 0, 0, 0, 0}; + readBytes = gzread(gzf, file_magic, 4); + if (!readBytes) { + std::cerr << "Fluid Error -- updateMeshFromUni(): Unable to read header in file: " << filename + << std::endl; + gzclose(gzf); + return false; + } std::vector<pVel> *velocityPointer = mMeshVelocities; @@ -2729,26 +2577,29 @@ void MANTA::updateMeshFromUni(const char *filename) gzread(gzf, ×tamp, sizeof(unsigned long long)); if (with_debug) - std::cout << "read " << ibuffer[0] << " vertices in file: " << filename << std::endl; + std::cout << "Fluid: Read " << ibuffer[0] << " vertices in file: " << filename << std::endl; // Sanity checks const int meshSize = sizeof(float) * 3 + sizeof(int); if (!(bytesPerElement == meshSize) && (elementType == 0)) { - std::cout << "particle type doesn't match" << std::endl; + std::cerr << "Fluid Error -- updateMeshFromUni(): Invalid header in file: " << filename + << std::endl; + gzclose(gzf); + return false; } if (!ibuffer[0]) { // Any vertices present? - if (with_debug) - std::cout << "no vertices present yet" << std::endl; + std::cerr << "Fluid Error -- updateMeshFromUni(): No vertices present in file: " << filename + << std::endl; gzclose(gzf); - return; + return false; } // Reading mesh - if (!strcmp(ID, "MB01")) { + if (!strcmp(file_magic, "MB01")) { // TODO (sebbas): Future update could add uni mesh support } // Reading mesh data file v1 with vec3 - else if (!strcmp(ID, "MD01")) { + else if (!strcmp(file_magic, "MD01")) { numParticles = ibuffer[0]; velocityPointer->resize(numParticles); @@ -2762,11 +2613,10 @@ void MANTA::updateMeshFromUni(const char *filename) it->pos[2] = bufferPVel->pos[2]; } } - - gzclose(gzf); + return (gzclose(gzf) == Z_OK); } -void MANTA::updateParticlesFromFile(const char *filename, bool isSecondarySys, bool isVelData) +bool MANTA::updateParticlesFromFile(std::string filename, bool isSecondarySys, bool isVelData) { if (with_debug) std::cout << "MANTA::updateParticlesFromFile()" << std::endl; @@ -2779,17 +2629,20 @@ void MANTA::updateParticlesFromFile(const char *filename, bool isSecondarySys, b std::string extension = fname.substr(idx + 1); if (extension.compare("uni") == 0) - updateParticlesFromUni(filename, isSecondarySys, isVelData); + return updateParticlesFromUni(filename, isSecondarySys, isVelData); else - std::cerr << "updateParticlesFromFile: invalid file extension in file: " << filename - << std::endl; + std::cerr << "Fluid Error -- updateParticlesFromFile(): Invalid file extension in file: " + << filename << std::endl; + return false; } else { - std::cerr << "updateParticlesFromFile: unable to open file: " << filename << std::endl; + std::cerr << "Fluid Error -- updateParticlesFromFile(): Unable to open file: " << filename + << std::endl; + return false; } } -void MANTA::updateParticlesFromUni(const char *filename, bool isSecondarySys, bool isVelData) +bool MANTA::updateParticlesFromUni(std::string filename, bool isSecondarySys, bool isVelData) { if (with_debug) std::cout << "MANTA::updateParticlesFromUni()" << std::endl; @@ -2797,17 +2650,29 @@ void MANTA::updateParticlesFromUni(const char *filename, bool isSecondarySys, bo gzFile gzf; int ibuffer[4]; - gzf = (gzFile)BLI_gzopen(filename, "rb1"); // do some compression - if (!gzf) - std::cerr << "updateParticlesFromUni: unable to open file" << std::endl; + gzf = (gzFile)BLI_gzopen(filename.c_str(), "rb1"); // do some compression + if (!gzf) { + std::cerr << "Fluid Error -- updateParticlesFromUni(): Unable to open file: " << filename + << std::endl; + return false; + } - char ID[5] = {0, 0, 0, 0, 0}; - gzread(gzf, ID, 4); + int readBytes = 0; + char file_magic[5] = {0, 0, 0, 0, 0}; + readBytes = gzread(gzf, file_magic, 4); + if (!readBytes) { + std::cerr << "Fluid Error -- updateParticlesFromUni(): Unable to read header in file: " + << filename << std::endl; + gzclose(gzf); + return false; + } - if (!strcmp(ID, "PB01")) { - std::cerr << "particle uni file format v01 not supported anymore" << std::endl; + if (!strcmp(file_magic, "PB01")) { + std::cerr << "Fluid Error -- updateParticlesFromUni(): Particle uni file format v01 not " + "supported anymore." + << std::endl; gzclose(gzf); - return; + return false; } // Pointer to FLIP system or to secondary particle system @@ -2839,28 +2704,31 @@ void MANTA::updateParticlesFromUni(const char *filename, bool isSecondarySys, bo gzread(gzf, ×tamp, sizeof(unsigned long long)); if (with_debug) - std::cout << "read " << ibuffer[0] << " particles in file: " << filename << std::endl; + std::cout << "Fluid: Read " << ibuffer[0] << " particles in file: " << filename << std::endl; // Sanity checks const int partSysSize = sizeof(float) * 3 + sizeof(int); if (!(bytesPerElement == partSysSize) && (elementType == 0)) { - std::cout << "particle type doesn't match" << std::endl; + std::cerr << "Fluid Error -- updateParticlesFromUni(): Invalid header in file: " << filename + << std::endl; + gzclose(gzf); + return false; } if (!ibuffer[0]) { // Any particles present? - if (with_debug) - std::cerr << "no particles present yet" << std::endl; + std::cerr << "Fluid Error -- updateParticlesFromUni(): No particles present in file: " + << filename << std::endl; gzclose(gzf); - return; + return false; } numParticles = ibuffer[0]; const int numChunks = (int)(ceil((float)numParticles / PARTICLE_CHUNK)); int todoParticles, readLen; - int readStart, readEnd, readBytes; + int readStart, readEnd; // Reading base particle system file v2 - if (!strcmp(ID, "PB02")) { + if (!strcmp(file_magic, "PB02")) { MANTA::pData *bufferPData; todoParticles = numParticles; bufferPData = (MANTA::pData *)MEM_malloc_arrayN( @@ -2876,11 +2744,12 @@ void MANTA::updateParticlesFromUni(const char *filename, bool isSecondarySys, bo readBytes = gzread(gzf, bufferPData, readLen * sizeof(pData)); if (!readBytes) { - if (with_debug) - std::cerr << "error while reading particle data" << std::endl; + std::cerr + << "Fluid Error -- updateParticlesFromUni(): Unable to read particle data in file: " + << filename << std::endl; MEM_freeN(bufferPData); gzclose(gzf); - return; + return false; } readStart = (numParticles - todoParticles); @@ -2900,7 +2769,7 @@ void MANTA::updateParticlesFromUni(const char *filename, bool isSecondarySys, bo MEM_freeN(bufferPData); } // Reading particle data file v1 with velocities - else if (!strcmp(ID, "PD01") && isVelData) { + else if (!strcmp(file_magic, "PD01") && isVelData) { MANTA::pVel *bufferPVel; todoParticles = numParticles; bufferPVel = (MANTA::pVel *)MEM_malloc_arrayN( @@ -2916,11 +2785,12 @@ void MANTA::updateParticlesFromUni(const char *filename, bool isSecondarySys, bo readBytes = gzread(gzf, bufferPVel, readLen * sizeof(pVel)); if (!readBytes) { - if (with_debug) - std::cerr << "error while reading particle velocities" << std::endl; + std::cerr << "Fluid Error -- updateParticlesFromUni(): Unable to read particle velocities " + "in file: " + << filename << std::endl; MEM_freeN(bufferPVel); gzclose(gzf); - return; + return false; } readStart = (numParticles - todoParticles); @@ -2939,7 +2809,7 @@ void MANTA::updateParticlesFromUni(const char *filename, bool isSecondarySys, bo MEM_freeN(bufferPVel); } // Reading particle data file v1 with lifetime - else if (!strcmp(ID, "PD01")) { + else if (!strcmp(file_magic, "PD01")) { float *bufferPLife; todoParticles = numParticles; bufferPLife = (float *)MEM_malloc_arrayN(PARTICLE_CHUNK, sizeof(float), "fluid_particle_life"); @@ -2954,11 +2824,12 @@ void MANTA::updateParticlesFromUni(const char *filename, bool isSecondarySys, bo readBytes = gzread(gzf, bufferPLife, readLen * sizeof(float)); if (!readBytes) { - if (with_debug) - std::cerr << "error while reading particle life" << std::endl; + std::cerr + << "Fluid Error -- updateParticlesFromUni(): Unable to read particle life in file: " + << filename << std::endl; MEM_freeN(bufferPLife); gzclose(gzf); - return; + return false; } readStart = (numParticles - todoParticles); @@ -2974,19 +2845,19 @@ void MANTA::updateParticlesFromUni(const char *filename, bool isSecondarySys, bo } MEM_freeN(bufferPLife); } - - gzclose(gzf); + return (gzclose(gzf) == Z_OK); } -int MANTA::updateGridFromFile(const char *filename, float *grid, bool isNoise) +bool MANTA::updateGridFromFile(std::string filename, float *grid, bool isNoise) { if (with_debug) std::cout << "MANTA::updateGridFromFile()" << std::endl; if (!grid) { - std::cout << "MANTA::updateGridFromFile(): cannot read into uninitialized grid, grid is null" + std::cerr << "Fluid Error -- updateGridFromFile(): Cannot read into uninitialized grid (grid " + "is null)." << std::endl; - return 0; + return false; } std::string fname(filename); @@ -3005,17 +2876,18 @@ int MANTA::updateGridFromFile(const char *filename, float *grid, bool isNoise) else if (extension.compare("raw") == 0) return updateGridFromRaw(filename, grid, isNoise); else - std::cerr << "MANTA::updateGridFromFile(): invalid file extension in file: " << filename - << std::endl; - return 0; + std::cerr << "Fluid Error -- updateGridFromFile(): Invalid file extension in file: " + << filename << std::endl; + return false; } else { - std::cerr << "MANTA::updateGridFromFile(): unable to open file: " << filename << std::endl; - return 0; + std::cerr << "Fluid Error -- updateGridFromFile(): Unable to open file: " << filename + << std::endl; + return false; } } -int MANTA::updateGridFromUni(const char *filename, float *grid, bool isNoise) +bool MANTA::updateGridFromUni(std::string filename, float *grid, bool isNoise) { if (with_debug) std::cout << "MANTA::updateGridFromUni()" << std::endl; @@ -3023,32 +2895,45 @@ int MANTA::updateGridFromUni(const char *filename, float *grid, bool isNoise) gzFile gzf; int ibuffer[4]; - gzf = (gzFile)BLI_gzopen(filename, "rb1"); + gzf = (gzFile)BLI_gzopen(filename.c_str(), "rb1"); if (!gzf) { - std::cout << "MANTA::updateGridFromUni(): unable to open file" << std::endl; - return 0; + std::cerr << "Fluid Error -- updateGridFromUni(): Unable to open file: " << filename + << std::endl; + return false; } - char ID[5] = {0, 0, 0, 0, 0}; - gzread(gzf, ID, 4); - - if (!strcmp(ID, "DDF2")) { - std::cout << "MANTA::updateGridFromUni(): grid uni file format DDF2 not supported anymore" + int readBytes = 0; + char file_magic[5] = {0, 0, 0, 0, 0}; + readBytes = gzread(gzf, file_magic, 4); + if (!readBytes) { + std::cerr << "Fluid Error -- updateGridFromUni(): Unable to read header in file: " << filename << std::endl; gzclose(gzf); - return 0; + return false; } - if (!strcmp(ID, "MNT1")) { - std::cout << "MANTA::updateGridFromUni(): grid uni file format MNT1 not supported anymore" - << std::endl; + + if (!strcmp(file_magic, "DDF2")) { + std::cerr + << "Fluid Error -- updateGridFromUni(): Grid uni file format DDF2 not supported anymore." + << std::endl; gzclose(gzf); - return 0; + return false; } - if (!strcmp(ID, "MNT2")) { - std::cout << "MANTA::updateGridFromUni(): grid uni file format MNT2 not supported anymore" - << std::endl; + + if (!strcmp(file_magic, "MNT1")) { + std::cerr + << "Fluid Error -- updateGridFromUni(): Grid uni file format MNT1 not supported anymore." + << std::endl; gzclose(gzf); - return 0; + return false; + } + + if (!strcmp(file_magic, "MNT2")) { + std::cerr + << "Fluid Error -- updateGridFromUni(): Grid uni file format MNT2 not supported anymore." + << std::endl; + gzclose(gzf); + return false; } // grid uni header @@ -3071,31 +2956,30 @@ int MANTA::updateGridFromUni(const char *filename, float *grid, bool isNoise) int resZ = (isNoise) ? mResZNoise : mResZ; if (with_debug) - std::cout << "read " << ibuffer[3] << " grid type in file: " << filename << std::endl; + std::cout << "Fluid: Read " << ibuffer[3] << " grid type in file: " << filename << std::endl; // Sanity checks if (ibuffer[0] != resX || ibuffer[1] != resY || ibuffer[2] != resZ) { - std::cout << "grid dim doesn't match, read: (" << ibuffer[0] << ", " << ibuffer[1] << ", " - << ibuffer[2] << ") vs setup: (" << resX << ", " << resY << ", " << resZ << ")" - << std::endl; + std::cout << "Fluid: Grid dim doesn't match, read: (" << ibuffer[0] << ", " << ibuffer[1] + << ", " << ibuffer[2] << ") vs setup: (" << resX << ", " << resY << ", " << resZ + << ")" << std::endl; gzclose(gzf); - return 0; + return false; } // Actual data reading - if (!strcmp(ID, "MNT3")) { + if (!strcmp(file_magic, "MNT3")) { gzread(gzf, grid, sizeof(float) * ibuffer[0] * ibuffer[1] * ibuffer[2]); } if (with_debug) - std::cout << "read successfully: " << filename << std::endl; + std::cout << "Fluid: Read successfully: " << filename << std::endl; - gzclose(gzf); - return 1; + return (gzclose(gzf) == Z_OK); } #if OPENVDB == 1 -int MANTA::updateGridFromVDB(const char *filename, float *grid, bool isNoise) +bool MANTA::updateGridFromVDB(std::string filename, float *grid, bool isNoise) { if (with_debug) std::cout << "MANTA::updateGridFromVDB()" << std::endl; @@ -3106,9 +2990,9 @@ int MANTA::updateGridFromVDB(const char *filename, float *grid, bool isNoise) file.open(); } catch (const openvdb::IoError &) { - std::cout << "MANTA::updateGridFromVDB(): IOError, invalid OpenVDB file: " << filename + std::cerr << "Fluid Error -- updateGridFromVDB(): IOError, invalid OpenVDB file: " << filename << std::endl; - return 0; + return false; } openvdb::GridBase::Ptr baseGrid; @@ -3135,11 +3019,11 @@ int MANTA::updateGridFromVDB(const char *filename, float *grid, bool isNoise) } } } - return 1; + return true; } #endif -int MANTA::updateGridFromRaw(const char *filename, float *grid, bool isNoise) +bool MANTA::updateGridFromRaw(std::string filename, float *grid, bool isNoise) { if (with_debug) std::cout << "MANTA::updateGridFromRaw()" << std::endl; @@ -3147,10 +3031,10 @@ int MANTA::updateGridFromRaw(const char *filename, float *grid, bool isNoise) gzFile gzf; int expectedBytes, readBytes; - gzf = (gzFile)BLI_gzopen(filename, "rb"); + gzf = (gzFile)BLI_gzopen(filename.c_str(), "rb"); if (!gzf) { std::cout << "MANTA::updateGridFromRaw(): unable to open file" << std::endl; - return 0; + return false; } int resX = (isNoise) ? mResXNoise : mResX; @@ -3159,12 +3043,16 @@ int MANTA::updateGridFromRaw(const char *filename, float *grid, bool isNoise) expectedBytes = sizeof(float) * resX * resY * resZ; readBytes = gzread(gzf, grid, expectedBytes); + if (!readBytes) { + std::cerr << "Fluid Error -- updateGridFromRaw(): Unable to read raw file: " << filename + << std::endl; + gzclose(gzf); + return false; + } assert(expectedBytes == readBytes); - (void)readBytes; // Unused in release. - gzclose(gzf); - return 1; + return (gzclose(gzf) == Z_OK); } void MANTA::updatePointers() @@ -3303,3 +3191,71 @@ void MANTA::updatePointers() mSmokeFromFile = false; mNoiseFromFile = false; } + +bool MANTA::hasConfig(FluidModifierData *mmd, int framenr) +{ + std::string extension = getCacheFileEnding(mmd->domain->cache_data_format); + return BLI_exists( + getFile(mmd, FLUID_DOMAIN_DIR_CONFIG, FLUID_DOMAIN_FILE_CONFIG, extension, framenr).c_str()); +} + +bool MANTA::hasData(FluidModifierData *mmd, int framenr) +{ + std::string filename = (mUsingSmoke) ? FLUID_DOMAIN_FILE_DENSITY : FLUID_DOMAIN_FILE_PHI; + std::string extension = getCacheFileEnding(mmd->domain->cache_data_format); + return BLI_exists(getFile(mmd, FLUID_DOMAIN_DIR_DATA, filename, extension, framenr).c_str()); +} + +bool MANTA::hasNoise(FluidModifierData *mmd, int framenr) +{ + std::string extension = getCacheFileEnding(mmd->domain->cache_noise_format); + return BLI_exists( + getFile(mmd, FLUID_DOMAIN_DIR_NOISE, FLUID_DOMAIN_FILE_DENSITYNOISE, extension, framenr) + .c_str()); +} + +bool MANTA::hasMesh(FluidModifierData *mmd, int framenr) +{ + std::string extension = getCacheFileEnding(mmd->domain->cache_mesh_format); + return BLI_exists( + getFile(mmd, FLUID_DOMAIN_DIR_MESH, FLUID_DOMAIN_FILE_MESH, extension, framenr).c_str()); +} + +bool MANTA::hasParticles(FluidModifierData *mmd, int framenr) +{ + std::string extension = getCacheFileEnding(mmd->domain->cache_particle_format); + return BLI_exists( + getFile(mmd, FLUID_DOMAIN_DIR_PARTICLES, FLUID_DOMAIN_FILE_PPSND, extension, framenr) + .c_str()); +} + +bool MANTA::hasGuiding(FluidModifierData *mmd, int framenr, bool sourceDomain) +{ + std::string subdirectory = (sourceDomain) ? FLUID_DOMAIN_DIR_DATA : FLUID_DOMAIN_DIR_GUIDE; + std::string filename = (sourceDomain) ? FLUID_DOMAIN_FILE_VEL : FLUID_DOMAIN_FILE_GUIDEVEL; + std::string extension = getCacheFileEnding(mmd->domain->cache_data_format); + return BLI_exists(getFile(mmd, subdirectory, filename, extension, framenr).c_str()); +} + +std::string MANTA::getDirectory(FluidModifierData *mmd, std::string subdirectory) +{ + char directory[FILE_MAX]; + BLI_path_join( + directory, sizeof(directory), mmd->domain->cache_directory, subdirectory.c_str(), nullptr); + BLI_path_make_safe(directory); + return directory; +} + +std::string MANTA::getFile(FluidModifierData *mmd, + std::string subdirectory, + std::string fname, + std::string extension, + int framenr) +{ + char targetFile[FILE_MAX]; + std::string path = getDirectory(mmd, subdirectory); + std::string filename = fname + extension; + BLI_join_dirfile(targetFile, sizeof(targetFile), path.c_str(), filename.c_str()); + BLI_path_frame(targetFile, framenr, 0); + return targetFile; +} |