1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2016, assimp team
6 All rights reserved.
7 
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
11 
12 * Redistributions of source code must retain the above
13   copyright notice, this list of conditions and the
14   following disclaimer.
15 
16 * Redistributions in binary form must reproduce the above
17   copyright notice, this list of conditions and the
18   following disclaimer in the documentation and/or other
19   materials provided with the distribution.
20 
21 * Neither the name of the assimp team, nor the names of its
22   contributors may be used to endorse or promote products
23   derived from this software without specific prior
24   written permission of the assimp team.
25 
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 
38 ----------------------------------------------------------------------
39 */
40 
41 /** @file  DXFHelper.h
42  *  @brief Internal utilities for the DXF loader.
43  */
44 
45 #ifndef INCLUDED_DXFHELPER_H
46 #define INCLUDED_DXFHELPER_H
47 
48 #include "LineSplitter.h"
49 #include "TinyFormatter.h"
50 #include "StreamReader.h"
51 #include "fast_atof.h"
52 #include <vector>
53 #include <assimp/DefaultLogger.hpp>
54 
55 namespace Assimp {
56     namespace DXF {
57 
58 
59 // read pairs of lines, parse group code and value and provide utilities
60 // to convert the data to the target data type.
61 class LineReader
62 {
63 
64 public:
65 
LineReader(StreamReaderLE & reader)66     LineReader(StreamReaderLE& reader)
67          // do NOT skip empty lines. In DXF files, they count as valid data.
68         : splitter(reader,false,true)
69         , groupcode( 0 )
70         , value()
71         , end()
72     {
73     }
74 
75 public:
76 
77 
78     // -----------------------------------------
Is(int gc,const char * what)79     bool Is(int gc, const char* what) const {
80         return groupcode == gc && !strcmp(what,value.c_str());
81     }
82 
83     // -----------------------------------------
Is(int gc)84     bool Is(int gc) const {
85         return groupcode == gc;
86     }
87 
88     // -----------------------------------------
GroupCode()89     int GroupCode() const {
90         return groupcode;
91     }
92 
93     // -----------------------------------------
Value()94     const std::string& Value() const {
95         return value;
96     }
97 
98     // -----------------------------------------
End()99     bool End() const {
100         return !((bool)*this);
101     }
102 
103 public:
104 
105     // -----------------------------------------
ValueAsUnsignedInt()106     unsigned int ValueAsUnsignedInt() const {
107         return strtoul10(value.c_str());
108     }
109 
110     // -----------------------------------------
ValueAsSignedInt()111     int ValueAsSignedInt() const {
112         return strtol10(value.c_str());
113     }
114 
115     // -----------------------------------------
ValueAsFloat()116     float ValueAsFloat() const {
117         return fast_atof(value.c_str());
118     }
119 
120 public:
121 
122     // -----------------------------------------
123     /** pseudo-iterator increment to advance to the next (groupcode/value) pair */
124     LineReader& operator++() {
125         if (end) {
126             if (end == 1) {
127                 ++end;
128             }
129             return *this;
130         }
131 
132         try {
133             groupcode = strtol10(splitter->c_str());
134             splitter++;
135 
136             value = *splitter;
137             splitter++;
138 
139             // automatically skip over {} meta blocks (these are for application use
140             // and currently not relevant for Assimp).
141             if (value.length() && value[0] == '{') {
142 
143                 size_t cnt = 0;
144                 for(;splitter->length() && splitter->at(0) != '}'; splitter++, cnt++);
145 
146                 splitter++;
147                 DefaultLogger::get()->debug((Formatter::format("DXF: skipped over control group ("),cnt," lines)"));
148             }
catch(std::logic_error &)149         } catch(std::logic_error&) {
150             ai_assert(!splitter);
151         }
152         if (!splitter) {
153             end = 1;
154         }
155         return *this;
156     }
157 
158     // -----------------------------------------
159     LineReader& operator++(int) {
160         return ++(*this);
161     }
162 
163 
164     // -----------------------------------------
165     operator bool() const {
166         return end <= 1;
167     }
168 
169 private:
170 
171     LineSplitter splitter;
172     int groupcode;
173     std::string value;
174     int end;
175 };
176 
177 
178 
179 // represents a POLYLINE or a LWPOLYLINE. or even a 3DFACE The data is converted as needed.
180 struct PolyLine
181 {
PolyLinePolyLine182     PolyLine()
183         : flags()
184     {}
185 
186     std::vector<aiVector3D> positions;
187     std::vector<aiColor4D>  colors;
188     std::vector<unsigned int> indices;
189     std::vector<unsigned int> counts;
190     unsigned int flags;
191 
192     std::string layer;
193     std::string desc;
194 };
195 
196 
197 // reference to a BLOCK. Specifies its own coordinate system.
198 struct InsertBlock
199 {
InsertBlockInsertBlock200     InsertBlock()
201         : scale(1.f,1.f,1.f)
202         , angle()
203     {}
204 
205     aiVector3D pos;
206     aiVector3D scale;
207     float angle;
208 
209     std::string name;
210 };
211 
212 
213 // keeps track of all geometry in a single BLOCK.
214 struct Block
215 {
216     std::vector< std::shared_ptr<PolyLine> > lines;
217     std::vector<InsertBlock> insertions;
218 
219     std::string name;
220     aiVector3D base;
221 };
222 
223 
224 struct FileData
225 {
226     // note: the LAST block always contains the stuff from ENTITIES.
227     std::vector<Block> blocks;
228 };
229 
230 
231 
232 
233 
234 }}
235 #endif
236