vrshoot
view libs/assimp/DXFLoader.cpp @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children |
line source
1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
6 Copyright (c) 2006-2012, assimp team
8 All rights reserved.
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the following
12 conditions are met:
14 * Redistributions of source code must retain the above
15 copyright notice, this list of conditions and the
16 following disclaimer.
18 * Redistributions in binary form must reproduce the above
19 copyright notice, this list of conditions and the
20 following disclaimer in the documentation and/or other
21 materials provided with the distribution.
23 * Neither the name of the assimp team, nor the names of its
24 contributors may be used to endorse or promote products
25 derived from this software without specific prior
26 written permission of the assimp team.
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 ---------------------------------------------------------------------------
40 */
42 /** @file DXFLoader.cpp
43 * @brief Implementation of the DXF importer class
44 */
46 #include "AssimpPCH.h"
47 #ifndef ASSIMP_BUILD_NO_DXF_IMPORTER
49 #include "DXFLoader.h"
50 #include "ParsingUtils.h"
51 #include "ConvertToLHProcess.h"
52 #include "fast_atof.h"
54 #include "DXFHelper.h"
56 using namespace Assimp;
58 // AutoCAD Binary DXF<CR><LF><SUB><NULL>
59 #define AI_DXF_BINARY_IDENT ("AutoCAD Binary DXF\r\n\x1a\0")
60 #define AI_DXF_BINARY_IDENT_LEN (24)
62 // default vertex color that all uncolored vertices will receive
63 #define AI_DXF_DEFAULT_COLOR aiColor4D(0.6f,0.6f,0.6f,0.6f)
65 // color indices for DXF - 16 are supported, the table is
66 // taken directly from the DXF spec.
67 static aiColor4D g_aclrDxfIndexColors[] =
68 {
69 aiColor4D (0.6f, 0.6f, 0.6f, 1.0f),
70 aiColor4D (1.0f, 0.0f, 0.0f, 1.0f), // red
71 aiColor4D (0.0f, 1.0f, 0.0f, 1.0f), // green
72 aiColor4D (0.0f, 0.0f, 1.0f, 1.0f), // blue
73 aiColor4D (0.3f, 1.0f, 0.3f, 1.0f), // light green
74 aiColor4D (0.3f, 0.3f, 1.0f, 1.0f), // light blue
75 aiColor4D (1.0f, 0.3f, 0.3f, 1.0f), // light red
76 aiColor4D (1.0f, 0.0f, 1.0f, 1.0f), // pink
77 aiColor4D (1.0f, 0.6f, 0.0f, 1.0f), // orange
78 aiColor4D (0.6f, 0.3f, 0.0f, 1.0f), // dark orange
79 aiColor4D (1.0f, 1.0f, 0.0f, 1.0f), // yellow
80 aiColor4D (0.3f, 0.3f, 0.3f, 1.0f), // dark gray
81 aiColor4D (0.8f, 0.8f, 0.8f, 1.0f), // light gray
82 aiColor4D (0.0f, 00.f, 0.0f, 1.0f), // black
83 aiColor4D (1.0f, 1.0f, 1.0f, 1.0f), // white
84 aiColor4D (0.6f, 0.0f, 1.0f, 1.0f) // violet
85 };
86 #define AI_DXF_NUM_INDEX_COLORS (sizeof(g_aclrDxfIndexColors)/sizeof(g_aclrDxfIndexColors[0]))
87 #define AI_DXF_ENTITIES_MAGIC_BLOCK "$ASSIMP_ENTITIES_MAGIC"
90 static const aiImporterDesc desc = {
91 "Drawing Interchange Format (DXF) Importer",
92 "",
93 "",
94 "",
95 aiImporterFlags_SupportTextFlavour | aiImporterFlags_LimitedSupport,
96 0,
97 0,
98 0,
99 0,
100 "dxf"
101 };
103 // ------------------------------------------------------------------------------------------------
104 // Constructor to be privately used by Importer
105 DXFImporter::DXFImporter()
106 {}
108 // ------------------------------------------------------------------------------------------------
109 // Destructor, private as well
110 DXFImporter::~DXFImporter()
111 {}
113 // ------------------------------------------------------------------------------------------------
114 // Returns whether the class can handle the format of the given file.
115 bool DXFImporter::CanRead( const std::string& pFile, IOSystem* /*pIOHandler*/, bool /*checkSig*/) const
116 {
117 return SimpleExtensionCheck(pFile,"dxf");
118 }
120 // ------------------------------------------------------------------------------------------------
121 // Get a list of all supported file extensions
122 const aiImporterDesc* DXFImporter::GetInfo () const
123 {
124 return &desc;
125 }
127 // ------------------------------------------------------------------------------------------------
128 // Imports the given file into the given scene structure.
129 void DXFImporter::InternReadFile( const std::string& pFile,
130 aiScene* pScene,
131 IOSystem* pIOHandler)
132 {
133 boost::shared_ptr<IOStream> file = boost::shared_ptr<IOStream>( pIOHandler->Open( pFile) );
135 // Check whether we can read the file
136 if( file.get() == NULL) {
137 throw DeadlyImportError( "Failed to open DXF file " + pFile + "");
138 }
140 // check whether this is a binaray DXF file - we can't read binary DXF files :-(
141 char buff[AI_DXF_BINARY_IDENT_LEN+1] = {0};
142 file->Read(buff,AI_DXF_BINARY_IDENT_LEN,1);
144 if (!strncmp(AI_DXF_BINARY_IDENT,buff,AI_DXF_BINARY_IDENT_LEN)) {
145 throw DeadlyImportError("DXF: Binary files are not supported at the moment");
146 }
148 // DXF files can grow very large, so read them via the StreamReader,
149 // which will choose a suitable strategy.
150 file->Seek(0,aiOrigin_SET);
151 StreamReaderLE stream( file );
153 DXF::LineReader reader (stream);
154 DXF::FileData output;
156 // now get all lines of the file and process top-level sections
157 bool eof = false;
158 while(!reader.End()) {
160 // blocks table - these 'build blocks' are later (in ENTITIES)
161 // referenced an included via INSERT statements.
162 if (reader.Is(2,"BLOCKS")) {
163 ParseBlocks(reader,output);
164 continue;
165 }
167 // primary entity table
168 if (reader.Is(2,"ENTITIES")) {
169 ParseEntities(reader,output);
170 continue;
171 }
173 // skip unneeded sections entirely to avoid any problems with them
174 // alltogether.
175 else if (reader.Is(2,"CLASSES") || reader.Is(2,"TABLES")) {
176 SkipSection(reader);
177 continue;
178 }
180 else if (reader.Is(2,"HEADER")) {
181 ParseHeader(reader,output);
182 continue;
183 }
185 // comments
186 else if (reader.Is(999)) {
187 DefaultLogger::get()->info("DXF Comment: " + reader.Value());
188 }
190 // don't read past the official EOF sign
191 else if (reader.Is(0,"EOF")) {
192 eof = true;
193 break;
194 }
196 ++reader;
197 }
198 if (!eof) {
199 DefaultLogger::get()->warn("DXF: EOF reached, but did not encounter DXF EOF marker");
200 }
202 ConvertMeshes(pScene,output);
204 // Now rotate the whole scene by 90 degrees around the x axis to convert from AutoCAD's to Assimp's coordinate system
205 pScene->mRootNode->mTransformation = aiMatrix4x4(
206 1.f,0.f,0.f,0.f,
207 0.f,0.f,1.f,0.f,
208 0.f,-1.f,0.f,0.f,
209 0.f,0.f,0.f,1.f) * pScene->mRootNode->mTransformation;
210 }
212 // ------------------------------------------------------------------------------------------------
213 void DXFImporter::ConvertMeshes(aiScene* pScene, DXF::FileData& output)
214 {
215 // the process of resolving all the INSERT statements can grow the
216 // polycount excessively, so log the original number.
217 // XXX Option to import blocks as separate nodes?
218 if (!DefaultLogger::isNullLogger()) {
220 unsigned int vcount = 0, icount = 0;
221 BOOST_FOREACH (const DXF::Block& bl, output.blocks) {
222 BOOST_FOREACH (boost::shared_ptr<const DXF::PolyLine> pl, bl.lines) {
223 vcount += pl->positions.size();
224 icount += pl->counts.size();
225 }
226 }
228 DefaultLogger::get()->debug((Formatter::format("DXF: Unexpanded polycount is "),
229 icount,", vertex count is ",vcount
230 ));
231 }
233 if (! output.blocks.size() ) {
234 throw DeadlyImportError("DXF: no data blocks loaded");
235 }
237 DXF::Block* entities = 0;
239 // index blocks by name
240 DXF::BlockMap blocks_by_name;
241 BOOST_FOREACH (DXF::Block& bl, output.blocks) {
242 blocks_by_name[bl.name] = &bl;
243 if ( !entities && bl.name == AI_DXF_ENTITIES_MAGIC_BLOCK ) {
244 entities = &bl;
245 }
246 }
248 if (!entities) {
249 throw DeadlyImportError("DXF: no ENTITIES data block loaded");
250 }
252 typedef std::map<std::string, unsigned int> LayerMap;
254 LayerMap layers;
255 std::vector< std::vector< const DXF::PolyLine*> > corr;
257 // now expand all block references in the primary ENTITIES block
258 // XXX this involves heavy memory copying, consider a faster solution for future versions.
259 ExpandBlockReferences(*entities,blocks_by_name);
261 unsigned int cur = 0;
262 BOOST_FOREACH (boost::shared_ptr<const DXF::PolyLine> pl, entities->lines) {
263 if (pl->positions.size()) {
265 std::map<std::string, unsigned int>::iterator it = layers.find(pl->layer);
266 if (it == layers.end()) {
267 ++pScene->mNumMeshes;
269 layers[pl->layer] = cur++;
271 std::vector< const DXF::PolyLine* > pv;
272 pv.push_back(&*pl);
274 corr.push_back(pv);
275 }
276 else {
277 corr[(*it).second].push_back(&*pl);
278 }
279 }
280 }
282 if (!pScene->mNumMeshes) {
283 throw DeadlyImportError("DXF: this file contains no 3d data");
284 }
286 pScene->mMeshes = new aiMesh*[ pScene->mNumMeshes ] ();
288 BOOST_FOREACH(const LayerMap::value_type& elem, layers){
289 aiMesh* const mesh = pScene->mMeshes[elem.second] = new aiMesh();
290 mesh->mName.Set(elem.first);
292 unsigned int cvert = 0,cface = 0;
293 BOOST_FOREACH(const DXF::PolyLine* pl, corr[elem.second]){
294 // sum over all faces since we need to 'verbosify' them.
295 cvert += std::accumulate(pl->counts.begin(),pl->counts.end(),0);
296 cface += pl->counts.size();
297 }
299 aiVector3D* verts = mesh->mVertices = new aiVector3D[cvert];
300 aiColor4D* colors = mesh->mColors[0] = new aiColor4D[cvert];
301 aiFace* faces = mesh->mFaces = new aiFace[cface];
303 mesh->mNumVertices = cvert;
304 mesh->mNumFaces = cface;
306 unsigned int prims = 0;
307 unsigned int overall_indices = 0;
308 BOOST_FOREACH(const DXF::PolyLine* pl, corr[elem.second]){
310 std::vector<unsigned int>::const_iterator it = pl->indices.begin();
311 BOOST_FOREACH(unsigned int facenumv,pl->counts) {
312 aiFace& face = *faces++;
313 face.mIndices = new unsigned int[face.mNumIndices = facenumv];
315 for (unsigned int i = 0; i < facenumv; ++i) {
316 face.mIndices[i] = overall_indices++;
318 ai_assert(pl->positions.size() == pl->colors.size());
319 if (*it >= pl->positions.size()) {
320 throw DeadlyImportError("DXF: vertex index out of bounds");
321 }
323 *verts++ = pl->positions[*it];
324 *colors++ = pl->colors[*it++];
325 }
327 // set primitive flags now, this saves the extra pass in ScenePreprocessor.
328 switch(face.mNumIndices) {
329 case 1:
330 prims |= aiPrimitiveType_POINT;
331 break;
332 case 2:
333 prims |= aiPrimitiveType_LINE;
334 break;
335 case 3:
336 prims |= aiPrimitiveType_TRIANGLE;
337 break;
338 default:
339 prims |= aiPrimitiveType_POLYGON;
340 break;
341 }
342 }
343 }
345 mesh->mPrimitiveTypes = prims;
346 mesh->mMaterialIndex = 0;
347 }
349 GenerateHierarchy(pScene,output);
350 GenerateMaterials(pScene,output);
351 }
354 // ------------------------------------------------------------------------------------------------
355 void DXFImporter::ExpandBlockReferences(DXF::Block& bl,const DXF::BlockMap& blocks_by_name)
356 {
357 BOOST_FOREACH (const DXF::InsertBlock& insert, bl.insertions) {
359 // first check if the referenced blocks exists ...
360 const DXF::BlockMap::const_iterator it = blocks_by_name.find(insert.name);
361 if (it == blocks_by_name.end()) {
362 DefaultLogger::get()->error((Formatter::format("DXF: Failed to resolve block reference: "),
363 insert.name,"; skipping"
364 ));
365 continue;
366 }
368 // XXX this would be the place to implement recursive expansion if needed.
369 const DXF::Block& bl_src = *(*it).second;
371 BOOST_FOREACH (boost::shared_ptr<const DXF::PolyLine> pl_in, bl_src.lines) {
372 boost::shared_ptr<DXF::PolyLine> pl_out = boost::shared_ptr<DXF::PolyLine>(new DXF::PolyLine(*pl_in));
374 if (bl_src.base.Length() || insert.scale.x!=1.f || insert.scale.y!=1.f || insert.scale.z!=1.f || insert.angle || insert.pos.Length()) {
375 // manual coordinate system transformation
376 // XXX order
377 aiMatrix4x4 trafo, tmp;
378 aiMatrix4x4::Translation(-bl_src.base,trafo);
379 trafo *= aiMatrix4x4::Scaling(insert.scale,tmp);
380 trafo *= aiMatrix4x4::Translation(insert.pos,tmp);
382 // XXX rotation currently ignored - I didn't find an appropriate sample model.
383 if (insert.angle != 0.f) {
384 DefaultLogger::get()->warn("DXF: BLOCK rotation not currently implemented");
385 }
387 BOOST_FOREACH (aiVector3D& v, pl_out->positions) {
388 v *= trafo;
389 }
390 }
392 bl.lines.push_back(pl_out);
393 }
394 }
395 }
398 // ------------------------------------------------------------------------------------------------
399 void DXFImporter::GenerateMaterials(aiScene* pScene, DXF::FileData& /*output*/)
400 {
401 // generate an almost-white default material. Reason:
402 // the default vertex color is GREY, so we are
403 // already at Assimp's usual default color.
404 // generate a default material
405 aiMaterial* pcMat = new aiMaterial();
406 aiString s;
407 s.Set(AI_DEFAULT_MATERIAL_NAME);
408 pcMat->AddProperty(&s, AI_MATKEY_NAME);
410 aiColor4D clrDiffuse(0.9f,0.9f,0.9f,1.0f);
411 pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_DIFFUSE);
413 clrDiffuse = aiColor4D(1.0f,1.0f,1.0f,1.0f);
414 pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_SPECULAR);
416 clrDiffuse = aiColor4D(0.05f,0.05f,0.05f,1.0f);
417 pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_AMBIENT);
419 pScene->mNumMaterials = 1;
420 pScene->mMaterials = new aiMaterial*[1];
421 pScene->mMaterials[0] = pcMat;
422 }
425 // ------------------------------------------------------------------------------------------------
426 void DXFImporter::GenerateHierarchy(aiScene* pScene, DXF::FileData& /*output*/)
427 {
428 // generate the output scene graph, which is just the root node with a single child for each layer.
429 pScene->mRootNode = new aiNode();
430 pScene->mRootNode->mName.Set("<DXF_ROOT>");
432 if (1 == pScene->mNumMeshes) {
433 pScene->mRootNode->mMeshes = new unsigned int[ pScene->mRootNode->mNumMeshes = 1 ];
434 pScene->mRootNode->mMeshes[0] = 0;
435 }
436 else
437 {
438 pScene->mRootNode->mChildren = new aiNode*[ pScene->mRootNode->mNumChildren = pScene->mNumMeshes ];
439 for (unsigned int m = 0; m < pScene->mRootNode->mNumChildren;++m) {
440 aiNode* p = pScene->mRootNode->mChildren[m] = new aiNode();
441 p->mName = pScene->mMeshes[m]->mName;
443 p->mMeshes = new unsigned int[p->mNumMeshes = 1];
444 p->mMeshes[0] = m;
445 p->mParent = pScene->mRootNode;
446 }
447 }
448 }
451 // ------------------------------------------------------------------------------------------------
452 void DXFImporter::SkipSection(DXF::LineReader& reader)
453 {
454 for( ;!reader.End() && !reader.Is(0,"ENDSEC"); reader++);
455 }
458 // ------------------------------------------------------------------------------------------------
459 void DXFImporter::ParseHeader(DXF::LineReader& reader, DXF::FileData& /*output*/)
460 {
461 for( ;!reader.End() && !reader.Is(0,"ENDSEC"); reader++);
462 }
465 // ------------------------------------------------------------------------------------------------
466 void DXFImporter::ParseBlocks(DXF::LineReader& reader, DXF::FileData& output)
467 {
468 while( !reader.End() && !reader.Is(0,"ENDSEC")) {
469 if (reader.Is(0,"BLOCK")) {
470 ParseBlock(++reader,output);
471 continue;
472 }
473 ++reader;
474 }
476 DefaultLogger::get()->debug((Formatter::format("DXF: got "),
477 output.blocks.size()," entries in BLOCKS"
478 ));
479 }
482 // ------------------------------------------------------------------------------------------------
483 void DXFImporter::ParseBlock(DXF::LineReader& reader, DXF::FileData& output)
484 {
485 // push a new block onto the stack.
486 output.blocks.push_back( DXF::Block() );
487 DXF::Block& block = output.blocks.back();
489 while( !reader.End() && !reader.Is(0,"ENDBLK")) {
491 switch(reader.GroupCode()) {
492 case 2:
493 block.name = reader.Value();
494 break;
496 case 10:
497 block.base.x = reader.ValueAsFloat();
498 break;
499 case 20:
500 block.base.y = reader.ValueAsFloat();
501 break;
502 case 30:
503 block.base.z = reader.ValueAsFloat();
504 break;
505 }
507 if (reader.Is(0,"POLYLINE")) {
508 ParsePolyLine(++reader,output);
509 continue;
510 }
512 // XXX is this a valid case?
513 if (reader.Is(0,"INSERT")) {
514 DefaultLogger::get()->warn("DXF: INSERT within a BLOCK not currently supported; skipping");
515 for( ;!reader.End() && !reader.Is(0,"ENDBLK"); ++reader);
516 break;
517 }
519 else if (reader.Is(0,"3DFACE") || reader.Is(0,"LINE") || reader.Is(0,"3DLINE")) {
520 //http://sourceforge.net/tracker/index.php?func=detail&aid=2970566&group_id=226462&atid=1067632
521 Parse3DFace(++reader, output);
522 continue;
523 }
524 ++reader;
525 }
526 }
529 // ------------------------------------------------------------------------------------------------
530 void DXFImporter::ParseEntities(DXF::LineReader& reader, DXF::FileData& output)
531 {
532 // push a new block onto the stack.
533 output.blocks.push_back( DXF::Block() );
534 DXF::Block& block = output.blocks.back();
536 block.name = AI_DXF_ENTITIES_MAGIC_BLOCK;
538 while( !reader.End() && !reader.Is(0,"ENDSEC")) {
539 if (reader.Is(0,"POLYLINE")) {
540 ParsePolyLine(++reader,output);
541 continue;
542 }
544 else if (reader.Is(0,"INSERT")) {
545 ParseInsertion(++reader,output);
546 continue;
547 }
549 else if (reader.Is(0,"3DFACE") || reader.Is(0,"LINE") || reader.Is(0,"3DLINE")) {
550 //http://sourceforge.net/tracker/index.php?func=detail&aid=2970566&group_id=226462&atid=1067632
551 Parse3DFace(++reader, output);
552 continue;
553 }
555 ++reader;
556 }
558 DefaultLogger::get()->debug((Formatter::format("DXF: got "),
559 block.lines.size()," polylines and ", block.insertions.size() ," inserted blocks in ENTITIES"
560 ));
561 }
564 void DXFImporter::ParseInsertion(DXF::LineReader& reader, DXF::FileData& output)
565 {
566 output.blocks.back().insertions.push_back( DXF::InsertBlock() );
567 DXF::InsertBlock& bl = output.blocks.back().insertions.back();
569 while( !reader.End() && !reader.Is(0)) {
571 switch(reader.GroupCode())
572 {
573 // name of referenced block
574 case 2:
575 bl.name = reader.Value();
576 break;
578 // translation
579 case 10:
580 bl.pos.x = reader.ValueAsFloat();
581 break;
582 case 20:
583 bl.pos.y = reader.ValueAsFloat();
584 break;
585 case 30:
586 bl.pos.z = reader.ValueAsFloat();
587 break;
589 // scaling
590 case 41:
591 bl.scale.x = reader.ValueAsFloat();
592 break;
593 case 42:
594 bl.scale.y = reader.ValueAsFloat();
595 break;
596 case 43:
597 bl.scale.z = reader.ValueAsFloat();
598 break;
600 // rotation angle
601 case 50:
602 bl.angle = reader.ValueAsFloat();
603 break;
604 }
605 reader++;
606 }
607 }
609 #define DXF_POLYLINE_FLAG_CLOSED 0x1
610 #define DXF_POLYLINE_FLAG_3D_POLYLINE 0x8
611 #define DXF_POLYLINE_FLAG_3D_POLYMESH 0x10
612 #define DXF_POLYLINE_FLAG_POLYFACEMESH 0x40
614 // ------------------------------------------------------------------------------------------------
615 void DXFImporter::ParsePolyLine(DXF::LineReader& reader, DXF::FileData& output)
616 {
617 output.blocks.back().lines.push_back( boost::shared_ptr<DXF::PolyLine>( new DXF::PolyLine() ) );
618 DXF::PolyLine& line = *output.blocks.back().lines.back();
620 unsigned int iguess = 0, vguess = 0;
621 while( !reader.End() && !reader.Is(0,"ENDSEC")) {
623 if (reader.Is(0,"VERTEX")) {
624 ParsePolyLineVertex(++reader,line);
625 if (reader.Is(0,"SEQEND")) {
626 break;
627 }
628 continue;
629 }
631 switch(reader.GroupCode())
632 {
633 // flags --- important that we know whether it is a
634 // polyface mesh or 'just' a line.
635 case 70:
636 if (!line.flags) {
637 line.flags = reader.ValueAsSignedInt();
638 }
639 break;
641 // optional number of vertices
642 case 71:
643 vguess = reader.ValueAsSignedInt();
644 line.positions.reserve(vguess);
645 break;
647 // optional number of faces
648 case 72:
649 iguess = reader.ValueAsSignedInt();
650 line.indices.reserve(iguess);
651 break;
653 // 8 specifies the layer on which this line is placed on
654 case 8:
655 line.layer = reader.Value();
656 break;
657 }
659 reader++;
660 }
662 //if (!(line.flags & DXF_POLYLINE_FLAG_POLYFACEMESH)) {
663 // DefaultLogger::get()->warn((Formatter::format("DXF: polyline not currently supported: "),line.flags));
664 // output.blocks.back().lines.pop_back();
665 // return;
666 //}
668 if (vguess && line.positions.size() != vguess) {
669 DefaultLogger::get()->warn((Formatter::format("DXF: unexpected vertex count in polymesh: "),
670 line.positions.size(),", expected ", vguess
671 ));
672 }
674 if (line.flags & DXF_POLYLINE_FLAG_POLYFACEMESH ) {
675 if (line.positions.size() < 3 || line.indices.size() < 3) {
676 DefaultLogger::get()->warn("DXF: not enough vertices for polymesh; ignoring");
677 output.blocks.back().lines.pop_back();
678 return;
679 }
681 // if these numbers are wrong, parsing might have gone wild.
682 // however, the docs state that applications are not required
683 // to set the 71 and 72 fields, respectively, to valid values.
684 // So just fire a warning.
685 if (iguess && line.counts.size() != iguess) {
686 DefaultLogger::get()->warn((Formatter::format("DXF: unexpected face count in polymesh: "),
687 line.counts.size(),", expected ", iguess
688 ));
689 }
690 }
691 else if (!line.indices.size() && !line.counts.size()) {
692 // a polyline - so there are no indices yet.
693 size_t guess = line.positions.size() + (line.flags & DXF_POLYLINE_FLAG_CLOSED ? 1 : 0);
694 line.indices.reserve(guess);
696 line.counts.reserve(guess/2);
697 for (unsigned int i = 0; i < line.positions.size()/2; ++i) {
698 line.indices.push_back(i*2);
699 line.indices.push_back(i*2+1);
700 line.counts.push_back(2);
701 }
703 // closed polyline?
704 if (line.flags & DXF_POLYLINE_FLAG_CLOSED) {
705 line.indices.push_back(line.positions.size()-1);
706 line.indices.push_back(0);
707 line.counts.push_back(2);
708 }
709 }
710 }
712 #define DXF_VERTEX_FLAG_PART_OF_POLYFACE 0x80
713 #define DXF_VERTEX_FLAG_HAS_POSITIONS 0x40
715 // ------------------------------------------------------------------------------------------------
716 void DXFImporter::ParsePolyLineVertex(DXF::LineReader& reader, DXF::PolyLine& line)
717 {
718 unsigned int cnti = 0, flags = 0;
719 unsigned int indices[4];
721 aiVector3D out;
722 aiColor4D clr = AI_DXF_DEFAULT_COLOR;
724 while( !reader.End() ) {
726 if (reader.Is(0)) { // SEQEND or another VERTEX
727 break;
728 }
730 switch (reader.GroupCode())
731 {
732 case 8:
733 // layer to which the vertex belongs to - assume that
734 // this is always the layer the top-level polyline
735 // entity resides on as well.
736 if(reader.Value() != line.layer) {
737 DefaultLogger::get()->warn("DXF: expected vertex to be part of a polyface but the 0x128 flag isn't set");
738 }
739 break;
741 case 70:
742 flags = reader.ValueAsUnsignedInt();
743 break;
745 // VERTEX COORDINATES
746 case 10: out.x = reader.ValueAsFloat();break;
747 case 20: out.y = reader.ValueAsFloat();break;
748 case 30: out.z = reader.ValueAsFloat();break;
750 // POLYFACE vertex indices
751 case 71:
752 case 72:
753 case 73:
754 case 74:
755 if (cnti == 4) {
756 DefaultLogger::get()->warn("DXF: more than 4 indices per face not supported; ignoring");
757 break;
758 }
759 indices[cnti++] = reader.ValueAsUnsignedInt();
760 break;
762 // color
763 case 62:
764 clr = g_aclrDxfIndexColors[reader.ValueAsUnsignedInt() % AI_DXF_NUM_INDEX_COLORS];
765 break;
766 };
768 reader++;
769 }
771 if (line.flags & DXF_POLYLINE_FLAG_POLYFACEMESH && !(flags & DXF_VERTEX_FLAG_PART_OF_POLYFACE)) {
772 DefaultLogger::get()->warn("DXF: expected vertex to be part of a polyface but the 0x128 flag isn't set");
773 }
775 if (cnti) {
776 line.counts.push_back(cnti);
777 for (unsigned int i = 0; i < cnti; ++i) {
778 // IMPORTANT NOTE: POLYMESH indices are ONE-BASED
779 if (indices[i] == 0) {
780 DefaultLogger::get()->warn("DXF: invalid vertex index, indices are one-based.");
781 --line.counts.back();
782 continue;
783 }
784 line.indices.push_back(indices[i]-1);
785 }
786 }
787 else {
788 line.positions.push_back(out);
789 line.colors.push_back(clr);
790 }
791 }
793 // ------------------------------------------------------------------------------------------------
794 void DXFImporter::Parse3DFace(DXF::LineReader& reader, DXF::FileData& output)
795 {
796 // (note) this is also used for for parsing line entities, so we
797 // must handle the vertex_count == 2 case as well.
799 output.blocks.back().lines.push_back( boost::shared_ptr<DXF::PolyLine>( new DXF::PolyLine() ) );
800 DXF::PolyLine& line = *output.blocks.back().lines.back();
802 aiVector3D vip[4];
803 aiColor4D clr = AI_DXF_DEFAULT_COLOR;
805 bool b[4] = {false,false,false,false};
806 while( !reader.End() ) {
808 // next entity with a groupcode == 0 is probably already the next vertex or polymesh entity
809 if (reader.GroupCode() == 0) {
810 break;
811 }
812 switch (reader.GroupCode())
813 {
815 // 8 specifies the layer
816 case 8:
817 line.layer = reader.Value();
818 break;
820 // x position of the first corner
821 case 10: vip[0].x = reader.ValueAsFloat();
822 b[2] = true;
823 break;
825 // y position of the first corner
826 case 20: vip[0].y = reader.ValueAsFloat();
827 b[2] = true;
828 break;
830 // z position of the first corner
831 case 30: vip[0].z = reader.ValueAsFloat();
832 b[2] = true;
833 break;
835 // x position of the second corner
836 case 11: vip[1].x = reader.ValueAsFloat();
837 b[3] = true;
838 break;
840 // y position of the second corner
841 case 21: vip[1].y = reader.ValueAsFloat();
842 b[3] = true;
843 break;
845 // z position of the second corner
846 case 31: vip[1].z = reader.ValueAsFloat();
847 b[3] = true;
848 break;
850 // x position of the third corner
851 case 12: vip[2].x = reader.ValueAsFloat();
852 b[0] = true;
853 break;
855 // y position of the third corner
856 case 22: vip[2].y = reader.ValueAsFloat();
857 b[0] = true;
858 break;
860 // z position of the third corner
861 case 32: vip[2].z = reader.ValueAsFloat();
862 b[0] = true;
863 break;
865 // x position of the fourth corner
866 case 13: vip[3].x = reader.ValueAsFloat();
867 b[1] = true;
868 break;
870 // y position of the fourth corner
871 case 23: vip[3].y = reader.ValueAsFloat();
872 b[1] = true;
873 break;
875 // z position of the fourth corner
876 case 33: vip[3].z = reader.ValueAsFloat();
877 b[1] = true;
878 break;
880 // color
881 case 62:
882 clr = g_aclrDxfIndexColors[reader.ValueAsUnsignedInt() % AI_DXF_NUM_INDEX_COLORS];
883 break;
884 };
886 ++reader;
887 }
889 // the fourth corner may even be identical to the third,
890 // in this case we treat it as if it didn't exist.
891 if (vip[3] == vip[2]) {
892 b[1] = false;
893 }
895 // sanity checks to see if we got something meaningful
896 if ((b[1] && !b[0]) || !b[2] || !b[3]) {
897 DefaultLogger::get()->warn("DXF: unexpected vertex setup in 3DFACE/LINE/FACE entity; ignoring");
898 output.blocks.back().lines.pop_back();
899 return;
900 }
902 const unsigned int cnt = (2+(b[0]?1:0)+(b[1]?1:0));
903 line.counts.push_back(cnt);
905 for (unsigned int i = 0; i < cnt; ++i) {
906 line.indices.push_back(line.positions.size());
907 line.positions.push_back(vip[i]);
908 line.colors.push_back(clr);
909 }
910 }
912 #endif // !! ASSIMP_BUILD_NO_DXF_IMPORTER