Coverage Report

Created: 2026-10-28 07:46

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
613
    explicit LineReader(StreamReaderLE& reader) : splitter(reader,false,true), groupcode( 0 ), end() {
64
        // empty
65
613
    }
66
67
    // -----------------------------------------
68
3.86M
    bool Is(int gc, const char* what) const {
69
3.86M
        return groupcode == gc && !strcmp(what,value.c_str());
70
3.86M
    }
71
72
    // -----------------------------------------
73
967k
    bool Is(int gc) const {
74
967k
        return groupcode == gc;
75
967k
    }
76
77
    // -----------------------------------------
78
1.57M
    int GroupCode() const {
79
1.57M
        return groupcode;
80
1.57M
    }
81
82
    // -----------------------------------------
83
129k
    const std::string& Value() const {
84
129k
        return value;
85
129k
    }
86
87
    // -----------------------------------------
88
2.10M
    bool End() const {
89
2.10M
        return !((bool)*this);
90
2.10M
    }
91
92
    // -----------------------------------------
93
159k
    unsigned int ValueAsUnsignedInt() const {
94
159k
        return strtoul10(value.c_str());
95
159k
    }
96
97
    // -----------------------------------------
98
152k
    int ValueAsSignedInt() const {
99
152k
        return strtol10(value.c_str());
100
152k
    }
101
102
    // -----------------------------------------
103
416k
    ai_real ValueAsFloat() const {
104
416k
        return fast_atof(value.c_str());
105
416k
    }
106
107
    // -----------------------------------------
108
    /** pseudo-iterator increment to advance to the next (groupcode/value) pair */
109
1.95M
    LineReader& operator++() {
110
1.95M
        if (end) {
111
587
            if (end == 1) {
112
587
                ++end;
113
587
            }
114
587
            return *this;
115
587
        }
116
117
1.95M
        try {
118
1.95M
            groupcode = strtol10(splitter->c_str());
119
1.95M
            splitter++;
120
121
1.95M
            value = *splitter;
122
1.95M
            splitter++;
123
124
            // automatically skip over {} meta blocks (these are for application use
125
            // and currently not relevant for Assimp).
126
1.95M
            if (value.length() && value[0] == '{') {
127
128
1.08k
                size_t cnt = 0;
129
36.0k
                for(;splitter->length() && splitter->at(0) != '}'; splitter++, cnt++);
130
131
1.08k
                splitter++;
132
1.08k
                ASSIMP_LOG_VERBOSE_DEBUG("DXF: skipped over control group (",cnt," lines)");
133
1.08k
            }
134
1.95M
        } catch(std::logic_error&) {
135
301
            ai_assert(!splitter);
136
301
        }
137
1.95M
        if (!splitter) {
138
591
            end = 1;
139
591
        }
140
1.95M
        return *this;
141
1.95M
    }
142
143
    // -----------------------------------------
144
1.24M
    LineReader& operator++(int) {
145
1.24M
        return ++(*this);
146
1.24M
    }
147
148
149
    // -----------------------------------------
150
2.10M
    operator bool() const {
151
2.10M
        return end <= 1;
152
2.10M
    }
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.8k
    PolyLine() : flags() {
164
        // empty
165
15.8k
    }
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.10k
    InsertBlock() : pos(0.f, 0.f, 0.f), scale(1.f,1.f,1.f), angle(0.0f) {
180
        // empty
181
3.10k
    }
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