Coverage Report

Created: 2026-04-01 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/Common/BaseProcess.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 Base class of all import post processing steps */
43
#ifndef INCLUDED_AI_BASEPROCESS_H
44
#define INCLUDED_AI_BASEPROCESS_H
45
46
#include <assimp/GenericProperty.h>
47
48
#include <map>
49
50
struct aiScene;
51
52
namespace Assimp {
53
54
class Importer;
55
56
// ---------------------------------------------------------------------------
57
/** Helper class to allow post-processing steps to interact with each other.
58
 *
59
 *  The class maintains a simple property list that can be used by pp-steps
60
 *  to provide additional information to other steps. This is primarily
61
 *  intended for cross-step optimizations.
62
 */
63
class SharedPostProcessInfo {
64
public:
65
    struct Base {
66
2.22k
        virtual ~Base() = default;
67
    };
68
69
    //! Represents data that is allocated on the heap, thus needs to be deleted
70
    template <typename T>
71
    struct THeapData final : Base {
72
        explicit THeapData(T *in) :
73
2.22k
                data(in) {}
74
75
2.22k
        ~THeapData() override {
76
2.22k
            delete data;
77
2.22k
        }
78
        T *data;
79
    };
80
81
    //! Represents static, by-value data not allocated on the heap
82
    template <typename T>
83
    struct TStaticData final : Base {
84
        explicit TStaticData(T in) :
85
                data(in) {}
86
87
        ~TStaticData() override= default;
88
89
        T data;
90
    };
91
92
    // some typedefs for cleaner code
93
    typedef unsigned int KeyType;
94
    typedef std::map<KeyType, Base *> PropertyMap;
95
96
public:
97
    //! Destructor
98
13.1k
    ~SharedPostProcessInfo() {
99
13.1k
        Clean();
100
13.1k
    }
101
102
    //! Remove all stored properties from the table
103
29.1k
    void Clean() {
104
        // invoke the virtual destructor for all stored properties
105
29.1k
        for (PropertyMap::iterator it = pmap.begin(), end = pmap.end();
106
29.1k
                it != end; ++it) {
107
0
            delete (*it).second;
108
0
        }
109
29.1k
        pmap.clear();
110
29.1k
    }
111
112
    //! Add a heap property to the list
113
    template <typename T>
114
2.22k
    void AddProperty(const char *name, T *in) {
115
2.22k
        AddProperty(name, (Base *)new THeapData<T>(in));
116
2.22k
    }
117
118
    //! Add a static by-value property to the list
119
    template <typename T>
120
    void AddProperty(const char *name, T in) {
121
        AddProperty(name, (Base *)new TStaticData<T>(in));
122
    }
123
124
    //! Get a heap property
125
    template <typename T>
126
2.30k
    bool GetProperty(const char *name, T *&out) const {
127
2.30k
        THeapData<T> *t = (THeapData<T> *)GetPropertyInternal(name);
128
2.30k
        if (!t) {
129
0
            out = nullptr;
130
0
            return false;
131
0
        }
132
2.30k
        out = t->data;
133
2.30k
        return true;
134
2.30k
    }
135
136
    //! Get a static, by-value property
137
    template <typename T>
138
    bool GetProperty(const char *name, T &out) const {
139
        TStaticData<T> *t = (TStaticData<T> *)GetPropertyInternal(name);
140
        if ( nullptr == t) {
141
            return false;
142
        }
143
        out = t->data;
144
        return true;
145
    }
146
147
    //! Remove a property of a specific type
148
2.22k
    void RemoveProperty(const char *name) {
149
2.22k
        SetGenericPropertyPtr<Base>(pmap, name, nullptr );
150
2.22k
    }
151
152
private:
153
2.22k
    void AddProperty(const char *name, Base *data) {
154
2.22k
        SetGenericPropertyPtr<Base>(pmap, name, data);
155
2.22k
    }
156
157
2.30k
    Base *GetPropertyInternal(const char *name) const {
158
2.30k
        return GetGenericProperty<Base *>(pmap, name, nullptr );
159
2.30k
    }
160
161
private:
162
    //! Map of all stored properties
163
    PropertyMap pmap;
164
};
165
166
6.75k
#define AI_SPP_SPATIAL_SORT "$Spat"
167
168
// ---------------------------------------------------------------------------
169
/** The BaseProcess defines a common interface for all post processing steps.
170
 * A post processing step is run after a successful import if the caller
171
 * specified the corresponding flag when calling ReadFile().
172
 * Enum #aiPostProcessSteps defines which flags are available.
173
 * After a successful import the Importer iterates over its internal array
174
 * of processes and calls IsActive() on each process to evaluate if the step
175
 * should be executed. If the function returns true, the class' Execute()
176
 * function is called subsequently.
177
 */
178
class ASSIMP_API BaseProcess {
179
    friend class Importer;
180
181
public:
182
    /** @brief Constructor to be privately used by Importer */
183
    BaseProcess() AI_NO_EXCEPT;
184
185
    /** @brief Destructor */
186
438k
    virtual ~BaseProcess() = default;
187
188
    // -------------------------------------------------------------------
189
    /**
190
     * @brief Returns whether the processing step is present in the given flag.
191
     * @param pFlags The processing flags the importer was called with. A
192
     *   bitwise combination of #aiPostProcessSteps.
193
     * @return true if the process is present in this flag fields,
194
     *   false if not.
195
     */
196
    virtual bool IsActive(unsigned int pFlags) const = 0;
197
198
    // -------------------------------------------------------------------
199
    /** Check whether this step expects its input vertex data to be
200
     *  in verbose format. */
201
    virtual bool RequireVerboseFormat() const;
202
203
    // -------------------------------------------------------------------
204
    /**
205
     * @brief Executes the post processing step on the given imported data.
206
     * The function deletes the scene if the post-process step fails (
207
     * the object pointer will be set to nullptr).
208
     * @param pImp Importer instance (pImp->mScene must be valid)
209
     */
210
    void ExecuteOnScene(Importer *pImp);
211
212
    // -------------------------------------------------------------------
213
    /**
214
     * @brief Called prior to ExecuteOnScene().
215
     * The function is a request to the process to update its configuration
216
     * basing on the Importer's configuration property list.
217
     */
218
    virtual void SetupProperties(const Importer *pImp);
219
220
    // -------------------------------------------------------------------
221
    /**
222
     * @brief Executes the post processing step on the given imported data.
223
     * A process should throw an ImportErrorException* if it fails.
224
     * This method must be implemented by deriving classes.
225
     * @param pScene The imported data to work at.
226
     */
227
    virtual void Execute(aiScene *pScene) = 0;
228
229
    // -------------------------------------------------------------------
230
    /** Assign a new SharedPostProcessInfo to the step. This object
231
     *  allows multiple post-process steps to share data.
232
     * @param sh May be nullptr
233
     */
234
435k
    inline void SetSharedData(SharedPostProcessInfo *sh) {
235
435k
        shared = sh;
236
435k
    }
237
238
    // -------------------------------------------------------------------
239
    /** Get the shared data that is assigned to the step.
240
    */
241
0
    inline SharedPostProcessInfo *GetSharedData() {
242
0
        return shared;
243
0
    }
244
245
protected:
246
    /** See the doc of #SharedPostProcessInfo for more details */
247
    SharedPostProcessInfo *shared;
248
249
    /** Currently active progress handler */
250
    ProgressHandler *progress;
251
};
252
253
} // end of namespace Assimp
254
255
#endif // AI_BASEPROCESS_H_INC