2008-08-28 17:35:36 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (ASSIMP)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Copyright (c) 2006-2008, ASSIMP Development Team
|
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the following
|
|
|
|
conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the ASSIMP team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the ASSIMP Development Team.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file Implementation of the DXF importer class */
|
2008-10-13 16:45:48 +00:00
|
|
|
|
|
|
|
#include "AssimpPCH.h"
|
|
|
|
|
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
#include "DXFLoader.h"
|
2008-09-05 13:21:01 +00:00
|
|
|
#include "ParsingUtils.h"
|
|
|
|
#include "fast_atof.h"
|
2008-08-28 17:35:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
2008-09-16 16:17:32 +00:00
|
|
|
// AutoCAD Binary DXF<CR><LF><SUB><NULL>
|
|
|
|
#define AI_DXF_BINARY_IDENT ("AutoCAD Binary DXF\r\n\x1a\0")
|
|
|
|
#define AI_DXF_BINARY_IDENT_LEN (24)
|
|
|
|
|
|
|
|
|
2008-09-20 15:55:51 +00:00
|
|
|
// color indices for DXF - 16 are supported
|
|
|
|
static aiColor4D g_aclrDxfIndexColors[] =
|
|
|
|
{
|
|
|
|
aiColor4D (0.6f, 0.6f, 0.6f, 1.0f),
|
|
|
|
aiColor4D (1.0f, 0.0f, 0.0f, 1.0f), // red
|
|
|
|
aiColor4D (0.0f, 1.0f, 0.0f, 1.0f), // green
|
|
|
|
aiColor4D (0.0f, 0.0f, 1.0f, 1.0f), // blue
|
|
|
|
aiColor4D (0.3f, 1.0f, 0.3f, 1.0f), // light green
|
|
|
|
aiColor4D (0.3f, 0.3f, 1.0f, 1.0f), // light blue
|
|
|
|
aiColor4D (1.0f, 0.3f, 0.3f, 1.0f), // light red
|
|
|
|
aiColor4D (1.0f, 0.0f, 1.0f, 1.0f), // pink
|
|
|
|
aiColor4D (1.0f, 0.6f, 0.0f, 1.0f), // orange
|
|
|
|
aiColor4D (0.6f, 0.3f, 0.0f, 1.0f), // dark orange
|
|
|
|
aiColor4D (1.0f, 1.0f, 0.0f, 1.0f), // yellow
|
|
|
|
aiColor4D (0.3f, 0.3f, 0.3f, 1.0f), // dark gray
|
|
|
|
aiColor4D (0.8f, 0.8f, 0.8f, 1.0f), // light gray
|
|
|
|
aiColor4D (0.0f, 00.f, 0.0f, 1.0f), // black
|
|
|
|
aiColor4D (1.0f, 1.0f, 1.0f, 1.0f), // white
|
|
|
|
aiColor4D (0.6f, 0.0f, 1.0f, 1.0f) // violet
|
|
|
|
};
|
|
|
|
#define AI_DXF_NUM_INDEX_COLORS (sizeof(g_aclrDxfIndexColors)/sizeof(g_aclrDxfIndexColors[0]))
|
|
|
|
|
|
|
|
// invalid/unassigned color value
|
|
|
|
aiColor4D g_clrInvalid = aiColor4D(std::numeric_limits<float>::quiet_NaN(),0.f,0.f,1.f);
|
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by Importer
|
|
|
|
DXFImporter::DXFImporter()
|
|
|
|
{
|
2008-09-05 13:21:01 +00:00
|
|
|
// nothing to do here
|
2008-08-28 17:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor, private as well
|
|
|
|
DXFImporter::~DXFImporter()
|
|
|
|
{
|
|
|
|
// nothing to do here
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns whether the class can handle the format of the given file.
|
|
|
|
bool DXFImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
|
|
|
|
{
|
|
|
|
// simple check of file extension is enough for the moment
|
|
|
|
std::string::size_type pos = pFile.find_last_of('.');
|
|
|
|
// no file extension - can't read
|
|
|
|
if( pos == std::string::npos)
|
|
|
|
return false;
|
|
|
|
std::string extension = pFile.substr( pos);
|
|
|
|
|
|
|
|
return !(extension.length() != 4 || extension[0] != '.' ||
|
|
|
|
extension[1] != 'd' && extension[1] != 'D' ||
|
|
|
|
extension[2] != 'x' && extension[2] != 'X' ||
|
|
|
|
extension[3] != 'f' && extension[3] != 'F');
|
|
|
|
}
|
2008-09-05 13:21:01 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool DXFImporter::GetNextLine()
|
|
|
|
{
|
2008-09-27 16:46:05 +00:00
|
|
|
if(!SkipLine(&buffer))
|
|
|
|
return false;
|
2008-09-07 16:51:05 +00:00
|
|
|
if(!SkipSpaces(&buffer))return GetNextLine();
|
2008-10-31 19:32:00 +00:00
|
|
|
else if (*buffer == '{')
|
|
|
|
{
|
|
|
|
// some strange meta data ...
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if(!SkipLine(&buffer))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(SkipSpaces(&buffer) && *buffer == '}')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return GetNextLine();
|
|
|
|
}
|
2008-09-05 13:21:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool DXFImporter::GetNextToken()
|
|
|
|
{
|
2008-09-16 16:17:32 +00:00
|
|
|
if (bRepeat)
|
|
|
|
{
|
|
|
|
bRepeat = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-09-07 16:51:05 +00:00
|
|
|
SkipSpaces(&buffer);
|
2008-09-05 13:21:01 +00:00
|
|
|
groupCode = strtol10s(buffer,&buffer);
|
|
|
|
if(!GetNextLine())return false;
|
|
|
|
|
|
|
|
// copy the data line to a separate buffer
|
2008-09-07 16:51:05 +00:00
|
|
|
char* m = cursor, *end = &cursor[4096];
|
|
|
|
while (!IsLineEnd ( *buffer ) && m < end)
|
2008-09-05 13:21:01 +00:00
|
|
|
*m++ = *buffer++;
|
|
|
|
|
|
|
|
*m = '\0';
|
|
|
|
GetNextLine();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Imports the given file into the given scene structure.
|
|
|
|
void DXFImporter::InternReadFile( const std::string& pFile,
|
|
|
|
aiScene* pScene, IOSystem* pIOHandler)
|
|
|
|
{
|
|
|
|
boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
|
|
|
|
|
|
|
|
// Check whether we can read from the file
|
|
|
|
if( file.get() == NULL)
|
|
|
|
throw new ImportErrorException( "Failed to open DXF file " + pFile + "");
|
|
|
|
|
2008-09-05 13:21:01 +00:00
|
|
|
// read the contents of the file in a buffer
|
|
|
|
unsigned int m = (unsigned int)file->FileSize();
|
|
|
|
std::vector<char> buffer2(m+1);
|
|
|
|
buffer = &buffer2[0];
|
|
|
|
file->Read( &buffer2[0], m,1);
|
|
|
|
buffer2[m] = '\0';
|
|
|
|
|
2008-09-16 16:17:32 +00:00
|
|
|
bRepeat = false;
|
2008-09-05 13:21:01 +00:00
|
|
|
mDefaultLayer = NULL;
|
|
|
|
|
2008-09-16 16:17:32 +00:00
|
|
|
// check whether this is a binaray DXF file - we can't read binary DXF files :-(
|
|
|
|
if (!strncmp(AI_DXF_BINARY_IDENT,buffer,AI_DXF_BINARY_IDENT_LEN))
|
|
|
|
throw new ImportErrorException("DXF: Binary files are not supported at the moment");
|
|
|
|
|
2008-09-05 13:21:01 +00:00
|
|
|
// now get all lines of the file
|
|
|
|
while (GetNextToken())
|
|
|
|
{
|
|
|
|
if (2 == groupCode)
|
|
|
|
{
|
2008-09-16 16:17:32 +00:00
|
|
|
// ENTITIES and BLOCKS sections - skip the whole rest, no need to waste our time with them
|
|
|
|
if (!::strcmp(cursor,"ENTITIES") || !::strcmp(cursor,"BLOCKS"))
|
2008-09-20 15:55:51 +00:00
|
|
|
if (!ParseEntities())break; else bRepeat = true;
|
2008-09-05 13:21:01 +00:00
|
|
|
|
2008-09-16 16:17:32 +00:00
|
|
|
// other sections - skip them to make sure there will be no name conflicts
|
2008-09-05 13:21:01 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
while (GetNextToken())
|
|
|
|
{
|
2008-09-27 16:46:05 +00:00
|
|
|
if (!groupCode && !::strcmp(cursor,"ENDSEC"))break;
|
2008-09-05 13:21:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// print comment strings
|
|
|
|
else if (999 == groupCode)
|
|
|
|
{
|
|
|
|
DefaultLogger::get()->info(std::string( cursor ));
|
|
|
|
}
|
|
|
|
else if (!groupCode && !::strcmp(cursor,"EOF"))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-09-16 16:17:32 +00:00
|
|
|
// find out how many valud layers we have
|
|
|
|
for (std::vector<LayerInfo>::const_iterator it = mLayers.begin(),end = mLayers.end();
|
|
|
|
it != end;++it)
|
|
|
|
{
|
|
|
|
if (!(*it).vPositions.empty())++pScene->mNumMeshes;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pScene->mNumMeshes)
|
2008-09-05 13:21:01 +00:00
|
|
|
throw new ImportErrorException("DXF: this file contains no 3d data");
|
|
|
|
|
2008-09-16 16:17:32 +00:00
|
|
|
pScene->mMeshes = new aiMesh*[ pScene->mNumMeshes ];
|
2008-09-05 13:21:01 +00:00
|
|
|
m = 0;
|
|
|
|
for (std::vector<LayerInfo>::const_iterator it = mLayers.begin(),end = mLayers.end();
|
|
|
|
it != end;++it)
|
|
|
|
{
|
2008-09-16 16:17:32 +00:00
|
|
|
if ((*it).vPositions.empty())continue;
|
|
|
|
|
2008-09-05 13:21:01 +00:00
|
|
|
// generate the output mesh
|
|
|
|
aiMesh* pMesh = pScene->mMeshes[m++] = new aiMesh();
|
|
|
|
const std::vector<aiVector3D>& vPositions = (*it).vPositions;
|
2008-09-20 15:55:51 +00:00
|
|
|
const std::vector<aiColor4D>& vColors = (*it).vColors;
|
|
|
|
|
|
|
|
// check whether we need vertex colors here
|
2009-01-08 22:15:05 +00:00
|
|
|
aiColor4D* clrOut = NULL;
|
2008-09-20 15:55:51 +00:00
|
|
|
const aiColor4D* clr = NULL;
|
|
|
|
for (std::vector<aiColor4D>::const_iterator it2 = (*it).vColors.begin(), end2 = (*it).vColors.end();
|
|
|
|
it2 != end2; ++it2)
|
|
|
|
{
|
2008-09-27 16:46:05 +00:00
|
|
|
if ((*it2).r == (*it2).r) // check against qnan
|
2008-09-20 15:55:51 +00:00
|
|
|
{
|
|
|
|
clrOut = pMesh->mColors[0] = new aiColor4D[vPositions.size()];
|
2008-09-27 16:46:05 +00:00
|
|
|
for (unsigned int i = 0; i < vPositions.size();++i)
|
|
|
|
clrOut[i] = aiColor4D(0.6f,0.6f,0.6f,1.0f);
|
|
|
|
|
2008-09-20 15:55:51 +00:00
|
|
|
clr = &vColors[0];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-09-05 13:21:01 +00:00
|
|
|
|
|
|
|
pMesh->mNumFaces = (unsigned int)vPositions.size() / 4u;
|
|
|
|
pMesh->mFaces = new aiFace[pMesh->mNumFaces];
|
|
|
|
|
|
|
|
aiVector3D* vpOut = pMesh->mVertices = new aiVector3D[vPositions.size()];
|
|
|
|
const aiVector3D* vp = &vPositions[0];
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < pMesh->mNumFaces;++i)
|
|
|
|
{
|
|
|
|
aiFace& face = pMesh->mFaces[i];
|
|
|
|
|
2008-10-24 20:37:54 +00:00
|
|
|
// check whether we need four, three or two indices here
|
2008-10-19 11:32:33 +00:00
|
|
|
if (vp[1] == vp[2])
|
2008-09-05 13:21:01 +00:00
|
|
|
{
|
2008-10-19 11:32:33 +00:00
|
|
|
face.mNumIndices = 2;
|
2008-09-05 13:21:01 +00:00
|
|
|
}
|
2008-10-19 11:32:33 +00:00
|
|
|
else if (vp[3] == vp[2])
|
|
|
|
{
|
|
|
|
face.mNumIndices = 3;
|
|
|
|
}
|
|
|
|
else face.mNumIndices = 4;
|
2008-09-05 13:21:01 +00:00
|
|
|
face.mIndices = new unsigned int[face.mNumIndices];
|
|
|
|
|
|
|
|
for (unsigned int a = 0; a < face.mNumIndices;++a)
|
|
|
|
{
|
|
|
|
*vpOut++ = vp[a];
|
2008-09-27 16:46:05 +00:00
|
|
|
if (clr)
|
|
|
|
{
|
2008-10-24 20:37:54 +00:00
|
|
|
if (is_not_qnan( clr[a].r ))
|
2008-09-27 16:46:05 +00:00
|
|
|
*clrOut = clr[a];
|
|
|
|
|
|
|
|
++clrOut;
|
|
|
|
}
|
2008-09-05 13:21:01 +00:00
|
|
|
face.mIndices[a] = pMesh->mNumVertices++;
|
|
|
|
}
|
|
|
|
vp += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate the output scene graph
|
|
|
|
pScene->mRootNode = new aiNode();
|
|
|
|
pScene->mRootNode->mName.Set("<DXF_ROOT>");
|
|
|
|
|
|
|
|
if (1 == pScene->mNumMeshes)
|
|
|
|
{
|
|
|
|
pScene->mRootNode->mMeshes = new unsigned int[ pScene->mRootNode->mNumMeshes = 1 ];
|
|
|
|
pScene->mRootNode->mMeshes[0] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pScene->mRootNode->mChildren = new aiNode*[ pScene->mRootNode->mNumChildren = pScene->mNumMeshes ];
|
|
|
|
for (m = 0; m < pScene->mRootNode->mNumChildren;++m)
|
|
|
|
{
|
|
|
|
aiNode* p = pScene->mRootNode->mChildren[m] = new aiNode();
|
|
|
|
p->mName.length = ::strlen( mLayers[m].name );
|
|
|
|
::strcpy(p->mName.data, mLayers[m].name);
|
|
|
|
|
|
|
|
p->mMeshes = new unsigned int[p->mNumMeshes = 1];
|
|
|
|
p->mMeshes[0] = m;
|
|
|
|
p->mParent = pScene->mRootNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate a default material
|
|
|
|
MaterialHelper* pcMat = new MaterialHelper();
|
|
|
|
aiString s;
|
|
|
|
s.Set(AI_DEFAULT_MATERIAL_NAME);
|
|
|
|
pcMat->AddProperty(&s, AI_MATKEY_NAME);
|
|
|
|
|
|
|
|
aiColor4D clrDiffuse(0.6f,0.6f,0.6f,1.0f);
|
|
|
|
pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_DIFFUSE);
|
|
|
|
|
|
|
|
clrDiffuse = aiColor4D(1.0f,1.0f,1.0f,1.0f);
|
|
|
|
pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_SPECULAR);
|
|
|
|
|
|
|
|
clrDiffuse = aiColor4D(0.05f,0.05f,0.05f,1.0f);
|
|
|
|
pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_AMBIENT);
|
|
|
|
|
|
|
|
pScene->mNumMaterials = 1;
|
|
|
|
pScene->mMaterials = new aiMaterial*[1];
|
|
|
|
pScene->mMaterials[0] = pcMat;
|
|
|
|
|
|
|
|
// --- everything destructs automatically ---
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool DXFImporter::ParseEntities()
|
|
|
|
{
|
|
|
|
while (GetNextToken())
|
|
|
|
{
|
|
|
|
if (!groupCode)
|
2008-09-16 16:17:32 +00:00
|
|
|
{
|
2008-10-19 11:32:33 +00:00
|
|
|
if (!::strcmp(cursor,"3DFACE") || !::strcmp(cursor,"LINE") || !::strcmp(cursor,"3DLINE"))
|
2008-09-16 16:17:32 +00:00
|
|
|
if (!Parse3DFace()) return false; else bRepeat = true;
|
|
|
|
|
2008-09-27 16:46:05 +00:00
|
|
|
if (!::strcmp(cursor,"POLYLINE") || !::strcmp(cursor,"LWPOLYLINE"))
|
2008-09-16 16:17:32 +00:00
|
|
|
if (!ParsePolyLine()) return false; else bRepeat = true;
|
|
|
|
|
2008-09-05 13:21:01 +00:00
|
|
|
if (!::strcmp(cursor,"ENDSEC"))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-09-16 16:17:32 +00:00
|
|
|
void DXFImporter::SetLayer(LayerInfo*& out)
|
|
|
|
{
|
|
|
|
for (std::vector<LayerInfo>::iterator it = mLayers.begin(),end = mLayers.end();
|
|
|
|
it != end;++it)
|
|
|
|
{
|
|
|
|
if (!::strcmp( (*it).name, cursor ))
|
|
|
|
{
|
|
|
|
out = &(*it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!out)
|
|
|
|
{
|
|
|
|
// we don't have this layer yet
|
|
|
|
mLayers.push_back(LayerInfo());
|
|
|
|
out = &mLayers.back();
|
|
|
|
::strcpy(out->name,cursor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void DXFImporter::SetDefaultLayer(LayerInfo*& out)
|
|
|
|
{
|
|
|
|
if (!mDefaultLayer)
|
|
|
|
{
|
|
|
|
mLayers.push_back(LayerInfo());
|
|
|
|
mDefaultLayer = &mLayers.back();
|
|
|
|
}
|
|
|
|
out = mDefaultLayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool DXFImporter::ParsePolyLine()
|
2008-09-05 13:21:01 +00:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
LayerInfo* out = NULL;
|
|
|
|
|
2008-09-16 16:17:32 +00:00
|
|
|
std::vector<aiVector3D> positions;
|
2008-09-20 15:55:51 +00:00
|
|
|
std::vector<aiColor4D> colors;
|
2008-09-16 16:17:32 +00:00
|
|
|
std::vector<unsigned int> indices;
|
|
|
|
unsigned int flags = 0;
|
|
|
|
|
2008-09-05 13:21:01 +00:00
|
|
|
while (GetNextToken())
|
|
|
|
{
|
|
|
|
switch (groupCode)
|
|
|
|
{
|
2008-09-16 16:17:32 +00:00
|
|
|
case 0:
|
2008-09-05 13:21:01 +00:00
|
|
|
{
|
2008-09-16 16:17:32 +00:00
|
|
|
if (!::strcmp(cursor,"VERTEX"))
|
2008-09-05 13:21:01 +00:00
|
|
|
{
|
2008-09-20 15:55:51 +00:00
|
|
|
aiVector3D v;aiColor4D clr(g_clrInvalid);
|
2008-09-16 16:17:32 +00:00
|
|
|
unsigned int idx[4] = {0xffffffff,0xffffffff,0xffffffff,0xffffffff};
|
2008-09-20 15:55:51 +00:00
|
|
|
ParsePolyLineVertex(v, clr, idx);
|
|
|
|
if (0xffffffff == idx[0])
|
|
|
|
{
|
|
|
|
positions.push_back(v);
|
|
|
|
colors.push_back(clr);
|
|
|
|
}
|
2008-09-16 16:17:32 +00:00
|
|
|
else
|
2008-09-05 13:21:01 +00:00
|
|
|
{
|
2008-09-16 16:17:32 +00:00
|
|
|
// check whether we have a fourth coordinate
|
|
|
|
if (0xffffffff == idx[3])
|
|
|
|
{
|
|
|
|
idx[3] = idx[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
indices.reserve(indices.size()+4);
|
|
|
|
for (unsigned int m = 0; m < 4;++m)
|
|
|
|
indices.push_back(idx[m]);
|
2008-09-05 13:21:01 +00:00
|
|
|
}
|
2008-09-16 16:17:32 +00:00
|
|
|
bRepeat = true;
|
2008-09-05 13:21:01 +00:00
|
|
|
}
|
2008-09-16 16:17:32 +00:00
|
|
|
else if (!::strcmp(cursor,"ENDSEQ"))
|
2008-09-05 13:21:01 +00:00
|
|
|
{
|
2008-09-16 16:17:32 +00:00
|
|
|
ret = true;
|
2008-09-05 13:21:01 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-09-16 16:17:32 +00:00
|
|
|
// flags --- important that we know whether it is a polyface mesh
|
|
|
|
case 70:
|
|
|
|
{
|
2008-10-31 19:32:00 +00:00
|
|
|
if (!flags)
|
|
|
|
{
|
|
|
|
flags = strtol10(cursor);
|
|
|
|
}
|
2008-09-16 16:17:32 +00:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
// optional number of vertices
|
|
|
|
case 71:
|
|
|
|
{
|
2008-10-22 20:06:16 +00:00
|
|
|
positions.reserve(strtol10(cursor));
|
2008-09-16 16:17:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// optional number of faces
|
|
|
|
case 72:
|
|
|
|
{
|
2008-10-24 20:37:54 +00:00
|
|
|
indices.reserve(strtol10(cursor));
|
2008-09-16 16:17:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 8 specifies the layer
|
|
|
|
case 8:
|
|
|
|
{
|
|
|
|
SetLayer(out);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(flags & 64))
|
|
|
|
{
|
|
|
|
DefaultLogger::get()->warn("DXF: Only polyface meshes are currently supported");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (positions.size() < 3 || indices.size() < 3)
|
|
|
|
{
|
|
|
|
DefaultLogger::get()->warn("DXF: Unable to parse POLYLINE element - not enough vertices");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// use a default layer if necessary
|
|
|
|
if (!out)SetDefaultLayer(out);
|
|
|
|
|
2008-09-20 15:55:51 +00:00
|
|
|
flags = (unsigned int)(out->vPositions.size()+indices.size());
|
|
|
|
out->vPositions.reserve(flags);
|
|
|
|
out->vColors.reserve(flags);
|
|
|
|
|
2008-09-16 16:17:32 +00:00
|
|
|
// generate unique vertices
|
|
|
|
for (std::vector<unsigned int>::const_iterator it = indices.begin(), end = indices.end();
|
|
|
|
it != end; ++it)
|
|
|
|
{
|
|
|
|
unsigned int idx = *it;
|
2008-10-31 19:32:00 +00:00
|
|
|
if (idx > positions.size() || !idx)
|
2008-09-16 16:17:32 +00:00
|
|
|
{
|
|
|
|
DefaultLogger::get()->error("DXF: Polyface mesh index os out of range");
|
|
|
|
idx = (unsigned int) positions.size();
|
|
|
|
}
|
|
|
|
out->vPositions.push_back(positions[idx-1]); // indices are one-based.
|
2008-09-20 15:55:51 +00:00
|
|
|
out->vColors.push_back(colors[idx-1]); // indices are one-based.
|
2008-09-16 16:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-09-20 15:55:51 +00:00
|
|
|
bool DXFImporter::ParsePolyLineVertex(aiVector3D& out,aiColor4D& clr, unsigned int* outIdx)
|
2008-09-16 16:17:32 +00:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
while (GetNextToken())
|
|
|
|
{
|
|
|
|
switch (groupCode)
|
|
|
|
{
|
|
|
|
case 0: ret = true;break;
|
|
|
|
|
|
|
|
// todo - handle the correct layer for the vertex
|
2008-10-22 20:06:16 +00:00
|
|
|
// At the moment it is assumed that all vertices of
|
|
|
|
// a polyline are placed on the same global layer.
|
2008-09-16 16:17:32 +00:00
|
|
|
|
|
|
|
// x position of the first corner
|
|
|
|
case 10: out.x = fast_atof(cursor);break;
|
|
|
|
|
|
|
|
// y position of the first corner
|
|
|
|
case 20: out.y = -fast_atof(cursor);break;
|
|
|
|
|
|
|
|
// z position of the first corner
|
|
|
|
case 30: out.z = fast_atof(cursor);break;
|
|
|
|
|
|
|
|
// POLYFACE vertex indices
|
|
|
|
case 71: outIdx[0] = strtol10(cursor);break;
|
|
|
|
case 72: outIdx[1] = strtol10(cursor);break;
|
|
|
|
case 73: outIdx[2] = strtol10(cursor);break;
|
2008-10-31 19:32:00 +00:00
|
|
|
// case 74: outIdx[3] = strtol10(cursor);break;
|
2008-09-20 15:55:51 +00:00
|
|
|
|
|
|
|
// color
|
|
|
|
case 62: clr = g_aclrDxfIndexColors[strtol10(cursor) % AI_DXF_NUM_INDEX_COLORS]; break;
|
2008-09-16 16:17:32 +00:00
|
|
|
};
|
|
|
|
if (ret)break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool DXFImporter::Parse3DFace()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
LayerInfo* out = NULL;
|
|
|
|
|
|
|
|
aiVector3D vip[4]; // -- vectors are initialized to zero
|
2008-09-20 15:55:51 +00:00
|
|
|
aiColor4D clr(g_clrInvalid);
|
2008-10-19 11:32:33 +00:00
|
|
|
|
|
|
|
// this is also used for for parsing line entities
|
|
|
|
bool bThird = false;
|
|
|
|
|
2008-09-16 16:17:32 +00:00
|
|
|
while (GetNextToken())
|
|
|
|
{
|
|
|
|
switch (groupCode)
|
|
|
|
{
|
|
|
|
case 0: ret = true;break;
|
|
|
|
|
|
|
|
// 8 specifies the layer
|
|
|
|
case 8:
|
|
|
|
{
|
|
|
|
SetLayer(out);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-09-05 13:21:01 +00:00
|
|
|
// x position of the first corner
|
|
|
|
case 10: vip[0].x = fast_atof(cursor);break;
|
|
|
|
|
|
|
|
// y position of the first corner
|
|
|
|
case 20: vip[0].y = -fast_atof(cursor);break;
|
|
|
|
|
|
|
|
// z position of the first corner
|
|
|
|
case 30: vip[0].z = fast_atof(cursor);break;
|
|
|
|
|
|
|
|
// x position of the second corner
|
|
|
|
case 11: vip[1].x = fast_atof(cursor);break;
|
|
|
|
|
|
|
|
// y position of the second corner
|
|
|
|
case 21: vip[1].y = -fast_atof(cursor);break;
|
|
|
|
|
|
|
|
// z position of the second corner
|
|
|
|
case 31: vip[1].z = fast_atof(cursor);break;
|
|
|
|
|
|
|
|
// x position of the third corner
|
2008-10-19 11:32:33 +00:00
|
|
|
case 12: vip[2].x = fast_atof(cursor);
|
|
|
|
bThird = true;break;
|
2008-09-05 13:21:01 +00:00
|
|
|
|
|
|
|
// y position of the third corner
|
2008-10-19 11:32:33 +00:00
|
|
|
case 22: vip[2].y = -fast_atof(cursor);
|
|
|
|
bThird = true;break;
|
2008-09-05 13:21:01 +00:00
|
|
|
|
|
|
|
// z position of the third corner
|
2008-10-19 11:32:33 +00:00
|
|
|
case 32: vip[2].z = fast_atof(cursor);
|
|
|
|
bThird = true;break;
|
2008-09-05 13:21:01 +00:00
|
|
|
|
|
|
|
// x position of the fourth corner
|
2008-10-19 11:32:33 +00:00
|
|
|
case 13: vip[3].x = fast_atof(cursor);
|
|
|
|
bThird = true;break;
|
2008-09-05 13:21:01 +00:00
|
|
|
|
|
|
|
// y position of the fourth corner
|
2008-10-19 11:32:33 +00:00
|
|
|
case 23: vip[3].y = -fast_atof(cursor);
|
|
|
|
bThird = true;break;
|
2008-09-05 13:21:01 +00:00
|
|
|
|
|
|
|
// z position of the fourth corner
|
2008-10-19 11:32:33 +00:00
|
|
|
case 33: vip[3].z = fast_atof(cursor);
|
|
|
|
bThird = true;break;
|
2008-09-20 15:55:51 +00:00
|
|
|
|
|
|
|
// color
|
|
|
|
case 62: clr = g_aclrDxfIndexColors[strtol10(cursor) % AI_DXF_NUM_INDEX_COLORS]; break;
|
2008-09-05 13:21:01 +00:00
|
|
|
};
|
|
|
|
if (ret)break;
|
|
|
|
}
|
|
|
|
|
2008-10-19 11:32:33 +00:00
|
|
|
if (!bThird)vip[2] = vip[1];
|
|
|
|
|
2008-09-05 13:21:01 +00:00
|
|
|
// use a default layer if necessary
|
2008-09-16 16:17:32 +00:00
|
|
|
if (!out)SetDefaultLayer(out);
|
2008-09-05 13:21:01 +00:00
|
|
|
|
|
|
|
// add the faces to the face list for this layer
|
|
|
|
out->vPositions.push_back(vip[0]);
|
|
|
|
out->vPositions.push_back(vip[1]);
|
|
|
|
out->vPositions.push_back(vip[2]);
|
2008-09-16 16:17:32 +00:00
|
|
|
out->vPositions.push_back(vip[3]); // might be equal to the third
|
2008-09-20 15:55:51 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < 4;++i)
|
|
|
|
out->vColors.push_back(clr);
|
2008-09-05 13:21:01 +00:00
|
|
|
return ret;
|
2008-08-28 17:35:36 +00:00
|
|
|
}
|
2008-10-19 11:32:33 +00:00
|
|
|
|
|
|
|
|