BatchImporter: make validation configurable and add unittest for class.

This commit is contained in:
Kim Kulling
2016-11-11 12:49:05 +01:00
committed by Alberto Taiuti
parent cf7059f074
commit fdd01bda83
8 changed files with 207 additions and 87 deletions

View File

@@ -481,7 +481,7 @@ namespace Assimp
BatchLoader::PropertyMap map;
unsigned int id;
bool operator== (const std::string& f) {
bool operator== (const std::string& f) const {
return file == f;
}
};
@@ -489,13 +489,22 @@ namespace Assimp
// ------------------------------------------------------------------------------------------------
// BatchLoader::pimpl data structure
struct Assimp::BatchData
{
BatchData()
: pIOSystem()
, pImporter()
, next_id(0xffff)
{}
struct Assimp::BatchData {
BatchData( IOSystem* pIO, bool validate )
: pIOSystem( pIO )
, pImporter( nullptr )
, next_id(0xffff)
, validate( validate ) {
ai_assert( NULL != pIO );
pImporter = new Importer();
pImporter->SetIOHandler( pIO );
}
~BatchData() {
pImporter->SetIOHandler( NULL ); /* get pointer back into our possession */
delete pImporter;
}
// IO system to be used for all imports
IOSystem* pIOSystem;
@@ -511,53 +520,59 @@ struct Assimp::BatchData
// Id for next item
unsigned int next_id;
// Validation enabled state
bool validate;
};
typedef std::list<LoadRequest>::iterator LoadReqIt;
// ------------------------------------------------------------------------------------------------
BatchLoader::BatchLoader(IOSystem* pIO)
BatchLoader::BatchLoader(IOSystem* pIO, bool validate )
{
ai_assert(NULL != pIO);
data = new BatchData();
data->pIOSystem = pIO;
data->pImporter = new Importer();
data->pImporter->SetIOHandler(data->pIOSystem);
m_data = new BatchData( pIO, validate );
}
// ------------------------------------------------------------------------------------------------
BatchLoader::~BatchLoader()
{
// delete all scenes wthat have not been polled by the user
for (std::list<LoadRequest>::iterator it = data->requests.begin();it != data->requests.end(); ++it) {
// delete all scenes what have not been polled by the user
for ( LoadReqIt it = m_data->requests.begin();it != m_data->requests.end(); ++it) {
delete (*it).scene;
}
data->pImporter->SetIOHandler(NULL); /* get pointer back into our possession */
delete data->pImporter;
delete data;
delete m_data;
}
// ------------------------------------------------------------------------------------------------
void BatchLoader::setValidation( bool enabled ) {
m_data->validate = enabled;
}
// ------------------------------------------------------------------------------------------------
unsigned int BatchLoader::AddLoadRequest (const std::string& file,
bool BatchLoader::getValidation() const {
return m_data->validate;
}
// ------------------------------------------------------------------------------------------------
unsigned int BatchLoader::AddLoadRequest(const std::string& file,
unsigned int steps /*= 0*/, const PropertyMap* map /*= NULL*/)
{
ai_assert(!file.empty());
// check whether we have this loading request already
std::list<LoadRequest>::iterator it;
for (it = data->requests.begin();it != data->requests.end(); ++it) {
for ( LoadReqIt it = m_data->requests.begin();it != m_data->requests.end(); ++it) {
// Call IOSystem's path comparison function here
if (data->pIOSystem->ComparePaths((*it).file,file)) {
if ( m_data->pIOSystem->ComparePaths((*it).file,file)) {
if (map) {
if (!((*it).map == *map))
if ( !( ( *it ).map == *map ) ) {
continue;
}
}
else if (!(*it).map.empty())
else if ( !( *it ).map.empty() ) {
continue;
}
(*it).refCnt++;
return (*it).id;
@@ -565,20 +580,18 @@ unsigned int BatchLoader::AddLoadRequest (const std::string& file,
}
// no, we don't have it. So add it to the queue ...
data->requests.push_back(LoadRequest(file,steps,map,data->next_id));
return data->next_id++;
m_data->requests.push_back(LoadRequest(file,steps,map, m_data->next_id));
return m_data->next_id++;
}
// ------------------------------------------------------------------------------------------------
aiScene* BatchLoader::GetImport (unsigned int which)
aiScene* BatchLoader::GetImport( unsigned int which )
{
for (std::list<LoadRequest>::iterator it = data->requests.begin();it != data->requests.end(); ++it) {
for ( LoadReqIt it = m_data->requests.begin();it != m_data->requests.end(); ++it) {
if ((*it).id == which && (*it).loaded) {
aiScene* sc = (*it).scene;
if (!(--(*it).refCnt)) {
data->requests.erase(it);
m_data->requests.erase(it);
}
return sc;
}
@@ -590,14 +603,15 @@ aiScene* BatchLoader::GetImport (unsigned int which)
void BatchLoader::LoadAll()
{
// no threaded implementation for the moment
for (std::list<LoadRequest>::iterator it = data->requests.begin();it != data->requests.end(); ++it) {
for ( LoadReqIt it = m_data->requests.begin();it != m_data->requests.end(); ++it) {
// force validation in debug builds
unsigned int pp = (*it).flags;
#ifdef ASSIMP_BUILD_DEBUG
pp |= aiProcess_ValidateDataStructure;
#endif
if ( m_data->validate ) {
pp |= aiProcess_ValidateDataStructure;
}
// setup config properties if necessary
ImporterPimpl* pimpl = data->pImporter->Pimpl();
ImporterPimpl* pimpl = m_data->pImporter->Pimpl();
pimpl->mFloatProperties = (*it).map.floats;
pimpl->mIntProperties = (*it).map.ints;
pimpl->mStringProperties = (*it).map.strings;
@@ -608,8 +622,8 @@ void BatchLoader::LoadAll()
DefaultLogger::get()->info("%%% BEGIN EXTERNAL FILE %%%");
DefaultLogger::get()->info("File: " + (*it).file);
}
data->pImporter->ReadFile((*it).file,pp);
(*it).scene = data->pImporter->GetOrphanedScene();
m_data->pImporter->ReadFile((*it).file,pp);
(*it).scene = m_data->pImporter->GetOrphanedScene();
(*it).loaded = true;
DefaultLogger::get()->info("%%% END EXTERNAL FILE %%%");

View File

@@ -347,7 +347,12 @@ public: // static utilities
static void ConvertUTF8toISO8859_1(
std::string& data);
enum TextFileMode { ALLOW_EMPTY, FORBID_EMPTY };
// -------------------------------------------------------------------
/// @brief Enum to define, if empty files are ok or not.
enum TextFileMode {
ALLOW_EMPTY,
FORBID_EMPTY
};
// -------------------------------------------------------------------
/** Utility for text file loaders which copies the contents of the
@@ -382,14 +387,10 @@ public: // static utilities
}
}
protected:
/** Error description in case there was one. */
/// Error description in case there was one.
std::string m_ErrorText;
/** Currently set progress handler */
/// Currently set progress handler.
ProgressHandler* m_progress;
};

View File

@@ -39,6 +39,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @file Importer.h mostly internal stuff for use by #Assimp::Importer */
#pragma once
#ifndef INCLUDED_AI_IMPORTER_H
#define INCLUDED_AI_IMPORTER_H
@@ -133,12 +134,11 @@ struct BatchData;
* could, this has not yet been implemented at the moment).
*
* @note The class may not be used by more than one thread*/
class BatchLoader
class ASSIMP_API BatchLoader
{
// friend of Importer
public:
//! @cond never
// -------------------------------------------------------------------
/** Wraps a full list of configuration properties for an importer.
@@ -162,15 +162,29 @@ public:
//! @endcond
public:
// -------------------------------------------------------------------
/** Construct a batch loader from a given IO system to be used
* to access external files */
explicit BatchLoader(IOSystem* pIO);
* to access external files
*/
explicit BatchLoader(IOSystem* pIO, bool validate = false );
// -------------------------------------------------------------------
/** The class destructor.
*/
~BatchLoader();
// -------------------------------------------------------------------
/** Sets the validation step. True for enable validation during postprocess.
* @param enable True for validation.
*/
void setValidation( bool enabled );
// -------------------------------------------------------------------
/** Returns the current validation step.
* @return The current validation step.
*/
bool getValidation() const;
// -------------------------------------------------------------------
/** Add a new file to the list of files to be loaded.
* @param file File to be loaded
@@ -185,7 +199,6 @@ public:
const PropertyMap* map = NULL
);
// -------------------------------------------------------------------
/** Get an imported scene.
* This polls the import from the internal request list.
@@ -199,20 +212,16 @@ public:
unsigned int which
);
// -------------------------------------------------------------------
/** Waits until all scenes have been loaded. This returns
* immediately if no scenes are queued.*/
void LoadAll();
private:
// No need to have that in the public API ...
BatchData* data;
BatchData *m_data;
};
}
} // Namespace Assimp
#endif
#endif // INCLUDED_AI_IMPORTER_H