summaryrefslogtreecommitdiff
path: root/src/mesh/assimp-master/code/AssetLib/IFC/IFCUtil.h
blob: b5f72a56d1690084926c6257877626b61f23ddc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------

Copyright (c) 2006-2022, assimp team


All rights reserved.

Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:

* Redistributions of source code must retain the above
  copyright notice, this list of conditions and the
  following disclaimer.

* Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the
  following disclaimer in the documentation and/or other
  materials provided with the distribution.

* Neither the name of the assimp team, nor the names of its
  contributors may be used to endorse or promote products
  derived from this software without specific prior
  written permission of the assimp team.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

----------------------------------------------------------------------
*/

/** @file  IFC.cpp
 *  @brief Implementation of the Industry Foundation Classes loader.
 */

#ifndef INCLUDED_IFCUTIL_H
#define INCLUDED_IFCUTIL_H

#include "AssetLib/IFC/IFCReaderGen_2x3.h"
#include "AssetLib/IFC/IFCLoader.h"
#include "AssetLib/Step/STEPFile.h"

#include <assimp/mesh.h>
#include <assimp/material.h>

#include <utility>

struct aiNode;

namespace Assimp {
namespace IFC {

    typedef double IfcFloat;

    // IfcFloat-precision math data types
    typedef aiVector2t<IfcFloat> IfcVector2;
    typedef aiVector3t<IfcFloat> IfcVector3;
    typedef aiMatrix4x4t<IfcFloat> IfcMatrix4;
    typedef aiMatrix3x3t<IfcFloat> IfcMatrix3;
    typedef aiColor4t<IfcFloat> IfcColor4;


// ------------------------------------------------------------------------------------------------
// Helper for std::for_each to delete all heap-allocated items in a container
// ------------------------------------------------------------------------------------------------
template<typename T>
struct delete_fun {
    void operator()(T* del) {
        delete del;
    }
};



// ------------------------------------------------------------------------------------------------
// Helper used during mesh construction. Aids at creating aiMesh'es out of relatively few polygons.
// ------------------------------------------------------------------------------------------------
struct TempMesh {
    std::vector<IfcVector3> mVerts;
    std::vector<unsigned int> mVertcnt;

    // utilities
    aiMesh* ToMesh();
    void Clear();
    void Transform(const IfcMatrix4& mat);
    IfcVector3 Center() const;
    void Append(const TempMesh& other);
    bool IsEmpty() const;
    void RemoveAdjacentDuplicates();
    void RemoveDegenerates();
    void FixupFaceOrientation();
    static IfcVector3 ComputePolygonNormal(const IfcVector3* vtcs, size_t cnt, bool normalize = true);
    IfcVector3 ComputeLastPolygonNormal(bool normalize = true) const;
    void ComputePolygonNormals(std::vector<IfcVector3>& normals, bool normalize = true, size_t ofs = 0) const;
    void Swap(TempMesh& other);
};

inline
bool TempMesh::IsEmpty() const {
    return mVerts.empty() && mVertcnt.empty();
}


// ------------------------------------------------------------------------------------------------
// Temporary representation of an opening in a wall or a floor
// ------------------------------------------------------------------------------------------------
struct TempOpening
{
    const IFC::Schema_2x3::IfcSolidModel *solid;
    IfcVector3 extrusionDir;

    std::shared_ptr<TempMesh> profileMesh;
    std::shared_ptr<TempMesh> profileMesh2D;

    // list of points generated for this opening. This is used to
    // create connections between two opposing holes created
    // from a single opening instance (two because walls tend to
    // have two sides). If !empty(), the other side of the wall
    // has already been processed.
    std::vector<IfcVector3> wallPoints;

    // ------------------------------------------------------------------------------
    TempOpening()
        : solid()
        , extrusionDir()
        , profileMesh()
    {
    }

    // ------------------------------------------------------------------------------
    TempOpening(const IFC::Schema_2x3::IfcSolidModel *solid, IfcVector3 extrusionDir,
            std::shared_ptr<TempMesh> profileMesh,
            std::shared_ptr<TempMesh> profileMesh2D) :
            solid(solid), extrusionDir(extrusionDir), profileMesh(std::move(profileMesh)), profileMesh2D(std::move(profileMesh2D)) {
    }

    // ------------------------------------------------------------------------------
    void Transform(const IfcMatrix4& mat); // defined later since TempMesh is not complete yet



    // ------------------------------------------------------------------------------
    // Helper to sort openings by distance from a given base point
    struct DistanceSorter {

        DistanceSorter(const IfcVector3& base) : base(base) {}

        bool operator () (const TempOpening& a, const TempOpening& b) const {
            return (a.profileMesh->Center()-base).SquareLength() < (b.profileMesh->Center()-base).SquareLength();
        }

        IfcVector3 base;
    };
};


// ------------------------------------------------------------------------------------------------
// Intermediate data storage during conversion. Keeps everything and a bit more.
// ------------------------------------------------------------------------------------------------
struct ConversionData
{
    ConversionData(const STEP::DB& db, const IFC::Schema_2x3::IfcProject& proj, aiScene* out,const IFCImporter::Settings& settings)
        : len_scale(1.0)
        , angle_scale(-1.0)
        , db(db)
        , proj(proj)
        , out(out)
        , settings(settings)
        , apply_openings()
        , collect_openings()
    {}

    ~ConversionData() {
        std::for_each(meshes.begin(),meshes.end(),delete_fun<aiMesh>());
        std::for_each(materials.begin(),materials.end(),delete_fun<aiMaterial>());
    }

    IfcFloat len_scale, angle_scale;
    bool plane_angle_in_radians;

    const STEP::DB& db;
    const IFC::Schema_2x3::IfcProject& proj;
    aiScene* out;

    IfcMatrix4 wcs;
    std::vector<aiMesh*> meshes;
    std::vector<aiMaterial*> materials;

    struct MeshCacheIndex {
        const IFC::Schema_2x3::IfcRepresentationItem* item; unsigned int matindex;
        MeshCacheIndex() : item(nullptr), matindex(0) { }
        MeshCacheIndex(const IFC::Schema_2x3::IfcRepresentationItem* i, unsigned int mi) : item(i), matindex(mi) { }
        bool operator == (const MeshCacheIndex& o) const { return item == o.item && matindex == o.matindex; }
        bool operator < (const MeshCacheIndex& o) const { return item < o.item || (item == o.item && matindex < o.matindex); }
    };
    typedef std::map<MeshCacheIndex, std::set<unsigned int> > MeshCache;
    MeshCache cached_meshes;

    typedef std::map<const IFC::Schema_2x3::IfcSurfaceStyle*, unsigned int> MaterialCache;
    MaterialCache cached_materials;

    const IFCImporter::Settings& settings;

    // Intermediate arrays used to resolve openings in walls: only one of them
    // can be given at a time. apply_openings if present if the current element
    // is a wall and needs its openings to be poured into its geometry while
    // collect_openings is present only if the current element is an
    // IfcOpeningElement, for which all the geometry needs to be preserved
    // for later processing by a parent, which is a wall.
    std::vector<TempOpening>* apply_openings;
    std::vector<TempOpening>* collect_openings;

    std::set<uint64_t> already_processed;
};


// ------------------------------------------------------------------------------------------------
// Binary predicate to compare vectors with a given, quadratic epsilon.
// ------------------------------------------------------------------------------------------------
struct FuzzyVectorCompare {

    FuzzyVectorCompare(IfcFloat epsilon) : epsilon(epsilon) {}
    bool operator()(const IfcVector3& a, const IfcVector3& b) {
        return std::abs((a-b).SquareLength()) < epsilon;
    }

    const IfcFloat epsilon;
};


// ------------------------------------------------------------------------------------------------
// Ordering predicate to totally order R^2 vectors first by x and then by y
// ------------------------------------------------------------------------------------------------
struct XYSorter {

    // sort first by X coordinates, then by Y coordinates
    bool operator () (const IfcVector2&a, const IfcVector2& b) const {
        if (a.x == b.x) {
            return a.y < b.y;
        }
        return a.x < b.x;
    }
};



// conversion routines for common IFC entities, implemented in IFCUtil.cpp
void ConvertColor(aiColor4D& out, const Schema_2x3::IfcColourRgb& in);
void ConvertColor(aiColor4D& out, const Schema_2x3::IfcColourOrFactor& in,ConversionData& conv,const aiColor4D* base);
void ConvertCartesianPoint(IfcVector3& out, const Schema_2x3::IfcCartesianPoint& in);
void ConvertDirection(IfcVector3& out, const Schema_2x3::IfcDirection& in);
void ConvertVector(IfcVector3& out, const Schema_2x3::IfcVector& in);
void AssignMatrixAxes(IfcMatrix4& out, const IfcVector3& x, const IfcVector3& y, const IfcVector3& z);
void ConvertAxisPlacement(IfcMatrix4& out, const Schema_2x3::IfcAxis2Placement3D& in);
void ConvertAxisPlacement(IfcMatrix4& out, const Schema_2x3::IfcAxis2Placement2D& in);
void ConvertAxisPlacement(IfcVector3& axis, IfcVector3& pos, const IFC::Schema_2x3::IfcAxis1Placement& in);
void ConvertAxisPlacement(IfcMatrix4& out, const Schema_2x3::IfcAxis2Placement& in, ConversionData& conv);
void ConvertTransformOperator(IfcMatrix4& out, const Schema_2x3::IfcCartesianTransformationOperator& op);
bool IsTrue(const Assimp::STEP::EXPRESS::BOOLEAN& in);
IfcFloat ConvertSIPrefix(const std::string& prefix);


// IFCProfile.cpp
bool ProcessProfile(const Schema_2x3::IfcProfileDef& prof, TempMesh& meshout, ConversionData& conv);
bool ProcessCurve(const Schema_2x3::IfcCurve& curve,  TempMesh& meshout, ConversionData& conv);

// IFCMaterial.cpp
unsigned int ProcessMaterials(uint64_t id, unsigned int prevMatId, ConversionData& conv, bool forceDefaultMat);

// IFCGeometry.cpp
IfcMatrix3 DerivePlaneCoordinateSpace(const TempMesh& curmesh, bool& ok, IfcVector3& norOut);
bool ProcessRepresentationItem(const Schema_2x3::IfcRepresentationItem& item, unsigned int matid, std::set<unsigned int>& mesh_indices, ConversionData& conv);
void AssignAddedMeshes(std::set<unsigned int>& mesh_indices,aiNode* nd,ConversionData& /*conv*/);

void ProcessSweptAreaSolid(const Schema_2x3::IfcSweptAreaSolid& swept, TempMesh& meshout,
                           ConversionData& conv);

void ProcessExtrudedAreaSolid(const Schema_2x3::IfcExtrudedAreaSolid& solid, TempMesh& result,
                              ConversionData& conv, bool collect_openings);

// IFCBoolean.cpp

void ProcessBoolean(const Schema_2x3::IfcBooleanResult& boolean, TempMesh& result, ConversionData& conv);
void ProcessBooleanHalfSpaceDifference(const Schema_2x3::IfcHalfSpaceSolid* hs, TempMesh& result,
                                       const TempMesh& first_operand,
                                       ConversionData& conv);

void ProcessPolygonalBoundedBooleanHalfSpaceDifference(const Schema_2x3::IfcPolygonalBoundedHalfSpace* hs, TempMesh& result,
                                                       const TempMesh& first_operand,
                                                       ConversionData& conv);
void ProcessBooleanExtrudedAreaSolidDifference(const Schema_2x3::IfcExtrudedAreaSolid* as, TempMesh& result,
                                               const TempMesh& first_operand,
                                               ConversionData& conv);


// IFCOpenings.cpp

bool GenerateOpenings(std::vector<TempOpening>& openings,
                      TempMesh& curmesh,
                      bool check_intersection,
                      bool generate_connection_geometry,
                      const IfcVector3& wall_extrusion_axis = IfcVector3(0,1,0));



// IFCCurve.cpp

// ------------------------------------------------------------------------------------------------
// Custom exception for use by members of the Curve class
// ------------------------------------------------------------------------------------------------
class CurveError {
public:
    CurveError(const std::string& s)
    : mStr(s) {
        // empty
    }

    std::string mStr;
};

// ------------------------------------------------------------------------------------------------
// Temporary representation for an arbitrary sub-class of IfcCurve. Used to sample the curves
// to obtain a list of line segments.
// ------------------------------------------------------------------------------------------------
class Curve {
protected:
    Curve(const Schema_2x3::IfcCurve& base_entity, ConversionData& conv)
    : base_entity(base_entity)
    , conv(conv) {
        // empty
    }

public:
    typedef std::pair<IfcFloat, IfcFloat> ParamRange;

    virtual ~Curve() {}


    // check if a curve is closed
    virtual bool IsClosed() const = 0;

    // evaluate the curve at the given parametric position
    virtual IfcVector3 Eval(IfcFloat p) const = 0;

    // try to match a point on the curve to a given parameter
    // for self-intersecting curves, the result is not ambiguous and
    // it is undefined which parameter is returned.
    virtual bool ReverseEval(const IfcVector3& val, IfcFloat& paramOut) const;

    // get the range of the curve (both inclusive).
    // +inf and -inf are valid return values, the curve is not bounded in such a case.
    virtual std::pair<IfcFloat,IfcFloat> GetParametricRange() const = 0;
    IfcFloat GetParametricRangeDelta() const;

    // estimate the number of sample points that this curve will require
    virtual size_t EstimateSampleCount(IfcFloat start,IfcFloat end) const;

    // intelligently sample the curve based on the current settings
    // and append the result to the mesh
    virtual void SampleDiscrete(TempMesh& out,IfcFloat start,IfcFloat end) const;

#ifdef ASSIMP_BUILD_DEBUG
    // check if a particular parameter value lies within the well-defined range
    bool InRange(IfcFloat) const;
#endif
    static Curve* Convert(const IFC::Schema_2x3::IfcCurve&,ConversionData& conv);

protected:
    const Schema_2x3::IfcCurve& base_entity;
    ConversionData& conv;
};


// --------------------------------------------------------------------------------
// A BoundedCurve always holds the invariant that GetParametricRange()
// never returns infinite values.
// --------------------------------------------------------------------------------
class BoundedCurve : public Curve {
public:
    BoundedCurve(const Schema_2x3::IfcBoundedCurve& entity, ConversionData& conv)
        : Curve(entity,conv)
    {}

public:

    bool IsClosed() const;

public:

    // sample the entire curve
    void SampleDiscrete(TempMesh& out) const;
    using Curve::SampleDiscrete;
};

// IfcProfile.cpp
bool ProcessCurve(const Schema_2x3::IfcCurve& curve,  TempMesh& meshout, ConversionData& conv);
}
}

#endif