minor code improvements for the obj code
- make use of range for loops - make use of empty() method for the std::vector - removed unnecessary std::string initialization - use ' ' instead of " " for the find methods (should be "faster") - simplified some function - make use of emplace_back instead of push_back (should be "faster") - other small changespull/3003/head
parent
d38e541e89
commit
fc4dd6455a
|
@ -175,15 +175,15 @@ void ObjFileImporter::CreateDataFromImport(const ObjFile::Model* pModel, aiScene
|
||||||
ai_assert(false);
|
ai_assert(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pModel->m_Objects.size() > 0) {
|
if (!pModel->m_Objects.empty()) {
|
||||||
|
|
||||||
unsigned int meshCount = 0;
|
unsigned int meshCount = 0;
|
||||||
unsigned int childCount = 0;
|
unsigned int childCount = 0;
|
||||||
|
|
||||||
for(size_t index = 0; index < pModel->m_Objects.size(); ++index) {
|
for (auto object : pModel->m_Objects) {
|
||||||
if(pModel->m_Objects[index]) {
|
if(object) {
|
||||||
++childCount;
|
++childCount;
|
||||||
meshCount += (unsigned int)pModel->m_Objects[index]->m_Meshes.size();
|
meshCount += (unsigned int)object->m_Meshes.size();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -365,8 +365,8 @@ aiMesh *ObjFileImporter::createTopology( const ObjFile::Model* pModel, const Obj
|
||||||
unsigned int outIndex( 0 );
|
unsigned int outIndex( 0 );
|
||||||
|
|
||||||
// Copy all data from all stored meshes
|
// Copy all data from all stored meshes
|
||||||
for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++) {
|
for (auto& face : pObjMesh->m_Faces) {
|
||||||
ObjFile::Face* const inp = pObjMesh->m_Faces[ index ];
|
ObjFile::Face* const inp = face;
|
||||||
if (inp->m_PrimitiveType == aiPrimitiveType_LINE) {
|
if (inp->m_PrimitiveType == aiPrimitiveType_LINE) {
|
||||||
for(size_t i = 0; i < inp->m_vertices.size() - 1; ++i) {
|
for(size_t i = 0; i < inp->m_vertices.size() - 1; ++i) {
|
||||||
aiFace& f = pMesh->mFaces[ outIndex++ ];
|
aiFace& f = pMesh->mFaces[ outIndex++ ];
|
||||||
|
@ -385,7 +385,7 @@ aiMesh *ObjFileImporter::createTopology( const ObjFile::Model* pModel, const Obj
|
||||||
}
|
}
|
||||||
|
|
||||||
aiFace *pFace = &pMesh->mFaces[ outIndex++ ];
|
aiFace *pFace = &pMesh->mFaces[ outIndex++ ];
|
||||||
const unsigned int uiNumIndices = (unsigned int) pObjMesh->m_Faces[ index ]->m_vertices.size();
|
const unsigned int uiNumIndices = (unsigned int) face->m_vertices.size();
|
||||||
uiIdxCount += pFace->mNumIndices = (unsigned int) uiNumIndices;
|
uiIdxCount += pFace->mNumIndices = (unsigned int) uiNumIndices;
|
||||||
if (pFace->mNumIndices > 0) {
|
if (pFace->mNumIndices > 0) {
|
||||||
pFace->mIndices = new unsigned int[ uiNumIndices ];
|
pFace->mIndices = new unsigned int[ uiNumIndices ];
|
||||||
|
@ -446,13 +446,10 @@ void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
|
||||||
// Copy vertices, normals and textures into aiMesh instance
|
// Copy vertices, normals and textures into aiMesh instance
|
||||||
bool normalsok = true, uvok = true;
|
bool normalsok = true, uvok = true;
|
||||||
unsigned int newIndex = 0, outIndex = 0;
|
unsigned int newIndex = 0, outIndex = 0;
|
||||||
for ( size_t index=0; index < pObjMesh->m_Faces.size(); index++ ) {
|
for (auto sourceFace : pObjMesh->m_Faces) {
|
||||||
// Get source face
|
|
||||||
ObjFile::Face *pSourceFace = pObjMesh->m_Faces[ index ];
|
|
||||||
|
|
||||||
// Copy all index arrays
|
// Copy all index arrays
|
||||||
for ( size_t vertexIndex = 0, outVertexIndex = 0; vertexIndex < pSourceFace->m_vertices.size(); vertexIndex++ ) {
|
for (size_t vertexIndex = 0, outVertexIndex = 0; vertexIndex < sourceFace->m_vertices.size(); vertexIndex++ ) {
|
||||||
const unsigned int vertex = pSourceFace->m_vertices.at( vertexIndex );
|
const unsigned int vertex = sourceFace->m_vertices.at(vertexIndex );
|
||||||
if ( vertex >= pModel->m_Vertices.size() ) {
|
if ( vertex >= pModel->m_Vertices.size() ) {
|
||||||
throw DeadlyImportError( "OBJ: vertex index out of range" );
|
throw DeadlyImportError( "OBJ: vertex index out of range" );
|
||||||
}
|
}
|
||||||
|
@ -464,8 +461,8 @@ void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
|
||||||
pMesh->mVertices[ newIndex ] = pModel->m_Vertices[ vertex ];
|
pMesh->mVertices[ newIndex ] = pModel->m_Vertices[ vertex ];
|
||||||
|
|
||||||
// Copy all normals
|
// Copy all normals
|
||||||
if ( normalsok && !pModel->m_Normals.empty() && vertexIndex < pSourceFace->m_normals.size()) {
|
if ( normalsok && !pModel->m_Normals.empty() && vertexIndex < sourceFace->m_normals.size()) {
|
||||||
const unsigned int normal = pSourceFace->m_normals.at( vertexIndex );
|
const unsigned int normal = sourceFace->m_normals.at(vertexIndex );
|
||||||
if ( normal >= pModel->m_Normals.size() )
|
if ( normal >= pModel->m_Normals.size() )
|
||||||
{
|
{
|
||||||
normalsok = false;
|
normalsok = false;
|
||||||
|
@ -484,9 +481,9 @@ void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy all texture coordinates
|
// Copy all texture coordinates
|
||||||
if ( uvok && !pModel->m_TextureCoord.empty() && vertexIndex < pSourceFace->m_texturCoords.size())
|
if ( uvok && !pModel->m_TextureCoord.empty() && vertexIndex < sourceFace->m_texturCoords.size())
|
||||||
{
|
{
|
||||||
const unsigned int tex = pSourceFace->m_texturCoords.at( vertexIndex );
|
const unsigned int tex = sourceFace->m_texturCoords.at(vertexIndex );
|
||||||
|
|
||||||
if ( tex >= pModel->m_TextureCoord.size() )
|
if ( tex >= pModel->m_TextureCoord.size() )
|
||||||
{
|
{
|
||||||
|
@ -502,16 +499,16 @@ void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
|
||||||
// Get destination face
|
// Get destination face
|
||||||
aiFace *pDestFace = &pMesh->mFaces[ outIndex ];
|
aiFace *pDestFace = &pMesh->mFaces[ outIndex ];
|
||||||
|
|
||||||
const bool last = ( vertexIndex == pSourceFace->m_vertices.size() - 1 );
|
const bool last = (vertexIndex == sourceFace->m_vertices.size() - 1 );
|
||||||
if (pSourceFace->m_PrimitiveType != aiPrimitiveType_LINE || !last) {
|
if (sourceFace->m_PrimitiveType != aiPrimitiveType_LINE || !last) {
|
||||||
pDestFace->mIndices[ outVertexIndex ] = newIndex;
|
pDestFace->mIndices[ outVertexIndex ] = newIndex;
|
||||||
outVertexIndex++;
|
outVertexIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pSourceFace->m_PrimitiveType == aiPrimitiveType_POINT) {
|
if (sourceFace->m_PrimitiveType == aiPrimitiveType_POINT) {
|
||||||
outIndex++;
|
outIndex++;
|
||||||
outVertexIndex = 0;
|
outVertexIndex = 0;
|
||||||
} else if (pSourceFace->m_PrimitiveType == aiPrimitiveType_LINE) {
|
} else if (sourceFace->m_PrimitiveType == aiPrimitiveType_LINE) {
|
||||||
outVertexIndex = 0;
|
outVertexIndex = 0;
|
||||||
|
|
||||||
if(!last)
|
if(!last)
|
||||||
|
@ -520,7 +517,7 @@ void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
|
||||||
if (vertexIndex) {
|
if (vertexIndex) {
|
||||||
if(!last) {
|
if(!last) {
|
||||||
pMesh->mVertices[ newIndex+1 ] = pMesh->mVertices[ newIndex ];
|
pMesh->mVertices[ newIndex+1 ] = pMesh->mVertices[ newIndex ];
|
||||||
if ( !pSourceFace->m_normals.empty() && !pModel->m_Normals.empty()) {
|
if (!sourceFace->m_normals.empty() && !pModel->m_Normals.empty()) {
|
||||||
pMesh->mNormals[ newIndex+1 ] = pMesh->mNormals[newIndex ];
|
pMesh->mNormals[ newIndex+1 ] = pMesh->mNormals[newIndex ];
|
||||||
}
|
}
|
||||||
if ( !pModel->m_TextureCoord.empty() ) {
|
if ( !pModel->m_TextureCoord.empty() ) {
|
||||||
|
@ -563,13 +560,11 @@ void ObjFileImporter::countObjects(const std::vector<ObjFile::Object*> &rObjects
|
||||||
return;
|
return;
|
||||||
|
|
||||||
iNumMeshes += static_cast<unsigned int>( rObjects.size() );
|
iNumMeshes += static_cast<unsigned int>( rObjects.size() );
|
||||||
for (std::vector<ObjFile::Object*>::const_iterator it = rObjects.begin();
|
for (auto object: rObjects)
|
||||||
it != rObjects.end();
|
|
||||||
++it)
|
|
||||||
{
|
{
|
||||||
if (!(*it)->m_SubObjects.empty())
|
if (!object->m_SubObjects.empty())
|
||||||
{
|
{
|
||||||
countObjects((*it)->m_SubObjects, iNumMeshes);
|
countObjects(object->m_SubObjects, iNumMeshes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -274,7 +274,7 @@ void ObjFileMtlImporter::getFloatValue( ai_real &value )
|
||||||
// Creates a material from loaded data.
|
// Creates a material from loaded data.
|
||||||
void ObjFileMtlImporter::createMaterial()
|
void ObjFileMtlImporter::createMaterial()
|
||||||
{
|
{
|
||||||
std::string line( "" );
|
std::string line;
|
||||||
while( !IsLineEnd( *m_DataIt ) ) {
|
while( !IsLineEnd( *m_DataIt ) ) {
|
||||||
line += *m_DataIt;
|
line += *m_DataIt;
|
||||||
++m_DataIt;
|
++m_DataIt;
|
||||||
|
@ -282,7 +282,7 @@ void ObjFileMtlImporter::createMaterial()
|
||||||
|
|
||||||
std::vector<std::string> token;
|
std::vector<std::string> token;
|
||||||
const unsigned int numToken = tokenize<std::string>( line, token, " \t" );
|
const unsigned int numToken = tokenize<std::string>( line, token, " \t" );
|
||||||
std::string name( "" );
|
std::string name;
|
||||||
if ( numToken == 1 ) {
|
if ( numToken == 1 ) {
|
||||||
name = AI_DEFAULT_MATERIAL_NAME;
|
name = AI_DEFAULT_MATERIAL_NAME;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -96,9 +96,6 @@ ObjFileParser::ObjFileParser( IOStreamBuffer<char> &streamBuffer, const std::str
|
||||||
parseFile( streamBuffer );
|
parseFile( streamBuffer );
|
||||||
}
|
}
|
||||||
|
|
||||||
ObjFileParser::~ObjFileParser() {
|
|
||||||
}
|
|
||||||
|
|
||||||
void ObjFileParser::setBuffer( std::vector<char> &buffer ) {
|
void ObjFileParser::setBuffer( std::vector<char> &buffer ) {
|
||||||
m_DataIt = buffer.begin();
|
m_DataIt = buffer.begin();
|
||||||
m_DataItEnd = buffer.end();
|
m_DataItEnd = buffer.end();
|
||||||
|
@ -182,7 +179,7 @@ void ObjFileParser::parseFile( IOStreamBuffer<char> &streamBuffer ) {
|
||||||
|
|
||||||
getNameNoSpace(m_DataIt, m_DataItEnd, name);
|
getNameNoSpace(m_DataIt, m_DataItEnd, name);
|
||||||
|
|
||||||
size_t nextSpace = name.find(" ");
|
size_t nextSpace = name.find(' ');
|
||||||
if (nextSpace != std::string::npos)
|
if (nextSpace != std::string::npos)
|
||||||
name = name.substr(0, nextSpace);
|
name = name.substr(0, nextSpace);
|
||||||
|
|
||||||
|
@ -199,7 +196,7 @@ void ObjFileParser::parseFile( IOStreamBuffer<char> &streamBuffer ) {
|
||||||
|
|
||||||
getNameNoSpace(m_DataIt, m_DataItEnd, name);
|
getNameNoSpace(m_DataIt, m_DataItEnd, name);
|
||||||
|
|
||||||
size_t nextSpace = name.find(" ");
|
size_t nextSpace = name.find(' ');
|
||||||
if (nextSpace != std::string::npos)
|
if (nextSpace != std::string::npos)
|
||||||
name = name.substr(0, nextSpace);
|
name = name.substr(0, nextSpace);
|
||||||
|
|
||||||
|
@ -274,13 +271,8 @@ static bool isDataDefinitionEnd( const char *tmp ) {
|
||||||
|
|
||||||
static bool isNanOrInf(const char * in) {
|
static bool isNanOrInf(const char * in) {
|
||||||
// Look for "nan" or "inf", case insensitive
|
// Look for "nan" or "inf", case insensitive
|
||||||
if ((in[0] == 'N' || in[0] == 'n') && ASSIMP_strincmp(in, "nan", 3) == 0) {
|
return ((in[0] == 'N' || in[0] == 'n') && ASSIMP_strincmp(in, "nan", 3) == 0) ||
|
||||||
return true;
|
((in[0] == 'I' || in[0] == 'i') && ASSIMP_strincmp(in, "inf", 3) == 0);
|
||||||
}
|
|
||||||
else if ((in[0] == 'I' || in[0] == 'i') && ASSIMP_strincmp(in, "inf", 3) == 0) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t ObjFileParser::getNumComponentsInDataDefinition() {
|
size_t ObjFileParser::getNumComponentsInDataDefinition() {
|
||||||
|
@ -341,7 +333,7 @@ size_t ObjFileParser::getTexCoordVector( std::vector<aiVector3D> &point3d_array
|
||||||
if (!std::isfinite(z))
|
if (!std::isfinite(z))
|
||||||
z = 0;
|
z = 0;
|
||||||
|
|
||||||
point3d_array.push_back( aiVector3D( x, y, z ) );
|
point3d_array.emplace_back( x, y, z );
|
||||||
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
||||||
return numComponents;
|
return numComponents;
|
||||||
}
|
}
|
||||||
|
@ -357,7 +349,7 @@ void ObjFileParser::getVector3( std::vector<aiVector3D> &point3d_array ) {
|
||||||
copyNextWord( m_buffer, Buffersize );
|
copyNextWord( m_buffer, Buffersize );
|
||||||
z = ( ai_real ) fast_atof( m_buffer );
|
z = ( ai_real ) fast_atof( m_buffer );
|
||||||
|
|
||||||
point3d_array.push_back( aiVector3D( x, y, z ) );
|
point3d_array.emplace_back( x, y, z );
|
||||||
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -378,7 +370,7 @@ void ObjFileParser::getHomogeneousVector3( std::vector<aiVector3D> &point3d_arra
|
||||||
if (w == 0)
|
if (w == 0)
|
||||||
throw DeadlyImportError("OBJ: Invalid component in homogeneous vector (Division by zero)");
|
throw DeadlyImportError("OBJ: Invalid component in homogeneous vector (Division by zero)");
|
||||||
|
|
||||||
point3d_array.push_back( aiVector3D( x/w, y/w, z/w ) );
|
point3d_array.emplace_back( x/w, y/w, z/w );
|
||||||
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -393,7 +385,7 @@ void ObjFileParser::getTwoVectors3( std::vector<aiVector3D> &point3d_array_a, st
|
||||||
copyNextWord( m_buffer, Buffersize );
|
copyNextWord( m_buffer, Buffersize );
|
||||||
z = ( ai_real ) fast_atof( m_buffer );
|
z = ( ai_real ) fast_atof( m_buffer );
|
||||||
|
|
||||||
point3d_array_a.push_back( aiVector3D( x, y, z ) );
|
point3d_array_a.emplace_back( x, y, z );
|
||||||
|
|
||||||
copyNextWord(m_buffer, Buffersize);
|
copyNextWord(m_buffer, Buffersize);
|
||||||
x = (ai_real) fast_atof(m_buffer);
|
x = (ai_real) fast_atof(m_buffer);
|
||||||
|
@ -404,7 +396,7 @@ void ObjFileParser::getTwoVectors3( std::vector<aiVector3D> &point3d_array_a, st
|
||||||
copyNextWord( m_buffer, Buffersize );
|
copyNextWord( m_buffer, Buffersize );
|
||||||
z = ( ai_real ) fast_atof( m_buffer );
|
z = ( ai_real ) fast_atof( m_buffer );
|
||||||
|
|
||||||
point3d_array_b.push_back( aiVector3D( x, y, z ) );
|
point3d_array_b.emplace_back( x, y, z );
|
||||||
|
|
||||||
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
||||||
}
|
}
|
||||||
|
@ -417,7 +409,7 @@ void ObjFileParser::getVector2( std::vector<aiVector2D> &point2d_array ) {
|
||||||
copyNextWord(m_buffer, Buffersize);
|
copyNextWord(m_buffer, Buffersize);
|
||||||
y = (ai_real) fast_atof(m_buffer);
|
y = (ai_real) fast_atof(m_buffer);
|
||||||
|
|
||||||
point2d_array.push_back(aiVector2D(x, y));
|
point2d_array.emplace_back(x, y);
|
||||||
|
|
||||||
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
||||||
}
|
}
|
||||||
|
@ -439,7 +431,7 @@ void ObjFileParser::getFace( aiPrimitiveType type ) {
|
||||||
|
|
||||||
const bool vt = (!m_pModel->m_TextureCoord.empty());
|
const bool vt = (!m_pModel->m_TextureCoord.empty());
|
||||||
const bool vn = (!m_pModel->m_Normals.empty());
|
const bool vn = (!m_pModel->m_Normals.empty());
|
||||||
int iStep = 0, iPos = 0;
|
int iStep, iPos = 0;
|
||||||
while ( m_DataIt != m_DataItEnd ) {
|
while ( m_DataIt != m_DataItEnd ) {
|
||||||
iStep = 1;
|
iStep = 1;
|
||||||
|
|
||||||
|
@ -845,18 +837,18 @@ void ObjFileParser::createMesh( const std::string &meshName )
|
||||||
bool ObjFileParser::needsNewMesh( const std::string &materialName )
|
bool ObjFileParser::needsNewMesh( const std::string &materialName )
|
||||||
{
|
{
|
||||||
// If no mesh data yet
|
// If no mesh data yet
|
||||||
if(m_pModel->m_pCurrentMesh == 0)
|
if (m_pModel->m_pCurrentMesh == nullptr)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
bool newMat = false;
|
bool newMat = false;
|
||||||
int matIdx = getMaterialIndex( materialName );
|
int matIdx = getMaterialIndex( materialName );
|
||||||
int curMatIdx = m_pModel->m_pCurrentMesh->m_uiMaterialIndex;
|
unsigned int curMatIdx = m_pModel->m_pCurrentMesh->m_uiMaterialIndex;
|
||||||
if ( curMatIdx != int(ObjFile::Mesh::NoMaterial)
|
if ( curMatIdx != int(ObjFile::Mesh::NoMaterial)
|
||||||
&& curMatIdx != matIdx
|
&& curMatIdx != matIdx
|
||||||
// no need create a new mesh if no faces in current
|
// no need create a new mesh if no faces in current
|
||||||
// lets say 'usemtl' goes straight after 'g'
|
// lets say 'usemtl' goes straight after 'g'
|
||||||
&& m_pModel->m_pCurrentMesh->m_Faces.size() > 0 )
|
&& !m_pModel->m_pCurrentMesh->m_Faces.empty() )
|
||||||
{
|
{
|
||||||
// New material -> only one material per mesh, so we need to create a new
|
// New material -> only one material per mesh, so we need to create a new
|
||||||
// material
|
// material
|
||||||
|
|
|
@ -80,7 +80,7 @@ public:
|
||||||
/// @brief Constructor with data array.
|
/// @brief Constructor with data array.
|
||||||
ObjFileParser( IOStreamBuffer<char> &streamBuffer, const std::string &modelName, IOSystem* io, ProgressHandler* progress, const std::string &originalObjFileName);
|
ObjFileParser( IOStreamBuffer<char> &streamBuffer, const std::string &modelName, IOSystem* io, ProgressHandler* progress, const std::string &originalObjFileName);
|
||||||
/// @brief Destructor
|
/// @brief Destructor
|
||||||
~ObjFileParser();
|
~ObjFileParser() = default;
|
||||||
/// @brief If you want to load in-core data.
|
/// @brief If you want to load in-core data.
|
||||||
void setBuffer( std::vector<char> &buffer );
|
void setBuffer( std::vector<char> &buffer );
|
||||||
/// @brief Model getter.
|
/// @brief Model getter.
|
||||||
|
|
Loading…
Reference in New Issue