Coverage Report

Created: 2026-11-12 07:30

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