IFCUtil.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file IFC.cpp
  34. * @brief Implementation of the Industry Foundation Classes loader.
  35. */
  36. #ifndef INCLUDED_IFCUTIL_H
  37. #define INCLUDED_IFCUTIL_H
  38. #include "AssetLib/IFC/IFCReaderGen_2x3.h"
  39. #include "AssetLib/IFC/IFCLoader.h"
  40. #include "AssetLib/Step/STEPFile.h"
  41. #include <assimp/mesh.h>
  42. #include <assimp/material.h>
  43. #include <utility>
  44. struct aiNode;
  45. namespace Assimp {
  46. namespace IFC {
  47. typedef double IfcFloat;
  48. // IfcFloat-precision math data types
  49. typedef aiVector2t<IfcFloat> IfcVector2;
  50. typedef aiVector3t<IfcFloat> IfcVector3;
  51. typedef aiMatrix4x4t<IfcFloat> IfcMatrix4;
  52. typedef aiMatrix3x3t<IfcFloat> IfcMatrix3;
  53. typedef aiColor4t<IfcFloat> IfcColor4;
  54. // ------------------------------------------------------------------------------------------------
  55. // Helper for std::for_each to delete all heap-allocated items in a container
  56. // ------------------------------------------------------------------------------------------------
  57. template<typename T>
  58. struct delete_fun {
  59. void operator()(T* del) {
  60. delete del;
  61. }
  62. };
  63. // ------------------------------------------------------------------------------------------------
  64. // Helper used during mesh construction. Aids at creating aiMesh'es out of relatively few polygons.
  65. // ------------------------------------------------------------------------------------------------
  66. struct TempMesh {
  67. std::vector<IfcVector3> mVerts;
  68. std::vector<unsigned int> mVertcnt;
  69. // utilities
  70. aiMesh* ToMesh();
  71. void Clear();
  72. void Transform(const IfcMatrix4& mat);
  73. IfcVector3 Center() const;
  74. void Append(const TempMesh& other);
  75. bool IsEmpty() const;
  76. void RemoveAdjacentDuplicates();
  77. void RemoveDegenerates();
  78. void FixupFaceOrientation();
  79. static IfcVector3 ComputePolygonNormal(const IfcVector3* vtcs, size_t cnt, bool normalize = true);
  80. IfcVector3 ComputeLastPolygonNormal(bool normalize = true) const;
  81. void ComputePolygonNormals(std::vector<IfcVector3>& normals, bool normalize = true, size_t ofs = 0) const;
  82. void Swap(TempMesh& other);
  83. };
  84. inline
  85. bool TempMesh::IsEmpty() const {
  86. return mVerts.empty() && mVertcnt.empty();
  87. }
  88. // ------------------------------------------------------------------------------------------------
  89. // Temporary representation of an opening in a wall or a floor
  90. // ------------------------------------------------------------------------------------------------
  91. struct TempOpening
  92. {
  93. const IFC::Schema_2x3::IfcSolidModel *solid;
  94. IfcVector3 extrusionDir;
  95. std::shared_ptr<TempMesh> profileMesh;
  96. std::shared_ptr<TempMesh> profileMesh2D;
  97. // list of points generated for this opening. This is used to
  98. // create connections between two opposing holes created
  99. // from a single opening instance (two because walls tend to
  100. // have two sides). If !empty(), the other side of the wall
  101. // has already been processed.
  102. std::vector<IfcVector3> wallPoints;
  103. // ------------------------------------------------------------------------------
  104. TempOpening()
  105. : solid()
  106. , extrusionDir()
  107. , profileMesh()
  108. {
  109. }
  110. // ------------------------------------------------------------------------------
  111. TempOpening(const IFC::Schema_2x3::IfcSolidModel *solid, IfcVector3 extrusionDir,
  112. std::shared_ptr<TempMesh> profileMesh,
  113. std::shared_ptr<TempMesh> profileMesh2D) :
  114. solid(solid), extrusionDir(extrusionDir), profileMesh(std::move(profileMesh)), profileMesh2D(std::move(profileMesh2D)) {
  115. }
  116. // ------------------------------------------------------------------------------
  117. void Transform(const IfcMatrix4& mat); // defined later since TempMesh is not complete yet
  118. // ------------------------------------------------------------------------------
  119. // Helper to sort openings by distance from a given base point
  120. struct DistanceSorter {
  121. DistanceSorter(const IfcVector3& base) : base(base) {}
  122. bool operator () (const TempOpening& a, const TempOpening& b) const {
  123. return (a.profileMesh->Center()-base).SquareLength() < (b.profileMesh->Center()-base).SquareLength();
  124. }
  125. IfcVector3 base;
  126. };
  127. };
  128. // ------------------------------------------------------------------------------------------------
  129. // Intermediate data storage during conversion. Keeps everything and a bit more.
  130. // ------------------------------------------------------------------------------------------------
  131. struct ConversionData
  132. {
  133. ConversionData(const STEP::DB& db, const IFC::Schema_2x3::IfcProject& proj, aiScene* out,const IFCImporter::Settings& settings)
  134. : len_scale(1.0)
  135. , angle_scale(-1.0)
  136. , db(db)
  137. , proj(proj)
  138. , out(out)
  139. , settings(settings)
  140. , apply_openings()
  141. , collect_openings()
  142. {}
  143. ~ConversionData() {
  144. std::for_each(meshes.begin(),meshes.end(),delete_fun<aiMesh>());
  145. std::for_each(materials.begin(),materials.end(),delete_fun<aiMaterial>());
  146. }
  147. IfcFloat len_scale, angle_scale;
  148. bool plane_angle_in_radians;
  149. const STEP::DB& db;
  150. const IFC::Schema_2x3::IfcProject& proj;
  151. aiScene* out;
  152. IfcMatrix4 wcs;
  153. std::vector<aiMesh*> meshes;
  154. std::vector<aiMaterial*> materials;
  155. struct MeshCacheIndex {
  156. const IFC::Schema_2x3::IfcRepresentationItem* item; unsigned int matindex;
  157. MeshCacheIndex() : item(nullptr), matindex(0) { }
  158. MeshCacheIndex(const IFC::Schema_2x3::IfcRepresentationItem* i, unsigned int mi) : item(i), matindex(mi) { }
  159. bool operator == (const MeshCacheIndex& o) const { return item == o.item && matindex == o.matindex; }
  160. bool operator < (const MeshCacheIndex& o) const { return item < o.item || (item == o.item && matindex < o.matindex); }
  161. };
  162. typedef std::map<MeshCacheIndex, std::set<unsigned int> > MeshCache;
  163. MeshCache cached_meshes;
  164. typedef std::map<const IFC::Schema_2x3::IfcSurfaceStyle*, unsigned int> MaterialCache;
  165. MaterialCache cached_materials;
  166. const IFCImporter::Settings& settings;
  167. // Intermediate arrays used to resolve openings in walls: only one of them
  168. // can be given at a time. apply_openings if present if the current element
  169. // is a wall and needs its openings to be poured into its geometry while
  170. // collect_openings is present only if the current element is an
  171. // IfcOpeningElement, for which all the geometry needs to be preserved
  172. // for later processing by a parent, which is a wall.
  173. std::vector<TempOpening>* apply_openings;
  174. std::vector<TempOpening>* collect_openings;
  175. std::set<uint64_t> already_processed;
  176. };
  177. // ------------------------------------------------------------------------------------------------
  178. // Binary predicate to compare vectors with a given, quadratic epsilon.
  179. // ------------------------------------------------------------------------------------------------
  180. struct FuzzyVectorCompare {
  181. FuzzyVectorCompare(IfcFloat epsilon) : epsilon(epsilon) {}
  182. bool operator()(const IfcVector3& a, const IfcVector3& b) {
  183. return std::abs((a-b).SquareLength()) < epsilon;
  184. }
  185. const IfcFloat epsilon;
  186. };
  187. // ------------------------------------------------------------------------------------------------
  188. // Ordering predicate to totally order R^2 vectors first by x and then by y
  189. // ------------------------------------------------------------------------------------------------
  190. struct XYSorter {
  191. // sort first by X coordinates, then by Y coordinates
  192. bool operator () (const IfcVector2&a, const IfcVector2& b) const {
  193. if (a.x == b.x) {
  194. return a.y < b.y;
  195. }
  196. return a.x < b.x;
  197. }
  198. };
  199. // conversion routines for common IFC entities, implemented in IFCUtil.cpp
  200. void ConvertColor(aiColor4D& out, const Schema_2x3::IfcColourRgb& in);
  201. void ConvertColor(aiColor4D& out, const Schema_2x3::IfcColourOrFactor& in,ConversionData& conv,const aiColor4D* base);
  202. void ConvertCartesianPoint(IfcVector3& out, const Schema_2x3::IfcCartesianPoint& in);
  203. void ConvertDirection(IfcVector3& out, const Schema_2x3::IfcDirection& in);
  204. void ConvertVector(IfcVector3& out, const Schema_2x3::IfcVector& in);
  205. void AssignMatrixAxes(IfcMatrix4& out, const IfcVector3& x, const IfcVector3& y, const IfcVector3& z);
  206. void ConvertAxisPlacement(IfcMatrix4& out, const Schema_2x3::IfcAxis2Placement3D& in);
  207. void ConvertAxisPlacement(IfcMatrix4& out, const Schema_2x3::IfcAxis2Placement2D& in);
  208. void ConvertAxisPlacement(IfcVector3& axis, IfcVector3& pos, const IFC::Schema_2x3::IfcAxis1Placement& in);
  209. void ConvertAxisPlacement(IfcMatrix4& out, const Schema_2x3::IfcAxis2Placement& in, ConversionData& conv);
  210. void ConvertTransformOperator(IfcMatrix4& out, const Schema_2x3::IfcCartesianTransformationOperator& op);
  211. bool IsTrue(const Assimp::STEP::EXPRESS::BOOLEAN& in);
  212. IfcFloat ConvertSIPrefix(const std::string& prefix);
  213. // IFCProfile.cpp
  214. bool ProcessProfile(const Schema_2x3::IfcProfileDef& prof, TempMesh& meshout, ConversionData& conv);
  215. bool ProcessCurve(const Schema_2x3::IfcCurve& curve, TempMesh& meshout, ConversionData& conv);
  216. // IFCMaterial.cpp
  217. unsigned int ProcessMaterials(uint64_t id, unsigned int prevMatId, ConversionData& conv, bool forceDefaultMat);
  218. // IFCGeometry.cpp
  219. IfcMatrix3 DerivePlaneCoordinateSpace(const TempMesh& curmesh, bool& ok, IfcVector3& norOut);
  220. bool ProcessRepresentationItem(const Schema_2x3::IfcRepresentationItem& item, unsigned int matid, std::set<unsigned int>& mesh_indices, ConversionData& conv);
  221. void AssignAddedMeshes(std::set<unsigned int>& mesh_indices,aiNode* nd,ConversionData& /*conv*/);
  222. void ProcessSweptAreaSolid(const Schema_2x3::IfcSweptAreaSolid& swept, TempMesh& meshout,
  223. ConversionData& conv);
  224. void ProcessExtrudedAreaSolid(const Schema_2x3::IfcExtrudedAreaSolid& solid, TempMesh& result,
  225. ConversionData& conv, bool collect_openings);
  226. // IFCBoolean.cpp
  227. void ProcessBoolean(const Schema_2x3::IfcBooleanResult& boolean, TempMesh& result, ConversionData& conv);
  228. void ProcessBooleanHalfSpaceDifference(const Schema_2x3::IfcHalfSpaceSolid* hs, TempMesh& result,
  229. const TempMesh& first_operand,
  230. ConversionData& conv);
  231. void ProcessPolygonalBoundedBooleanHalfSpaceDifference(const Schema_2x3::IfcPolygonalBoundedHalfSpace* hs, TempMesh& result,
  232. const TempMesh& first_operand,
  233. ConversionData& conv);
  234. void ProcessBooleanExtrudedAreaSolidDifference(const Schema_2x3::IfcExtrudedAreaSolid* as, TempMesh& result,
  235. const TempMesh& first_operand,
  236. ConversionData& conv);
  237. // IFCOpenings.cpp
  238. bool GenerateOpenings(std::vector<TempOpening>& openings,
  239. TempMesh& curmesh,
  240. bool check_intersection,
  241. bool generate_connection_geometry,
  242. const IfcVector3& wall_extrusion_axis = IfcVector3(0,1,0));
  243. // IFCCurve.cpp
  244. // ------------------------------------------------------------------------------------------------
  245. // Custom exception for use by members of the Curve class
  246. // ------------------------------------------------------------------------------------------------
  247. class CurveError {
  248. public:
  249. CurveError(const std::string& s)
  250. : mStr(s) {
  251. // empty
  252. }
  253. std::string mStr;
  254. };
  255. // ------------------------------------------------------------------------------------------------
  256. // Temporary representation for an arbitrary sub-class of IfcCurve. Used to sample the curves
  257. // to obtain a list of line segments.
  258. // ------------------------------------------------------------------------------------------------
  259. class Curve {
  260. protected:
  261. Curve(const Schema_2x3::IfcCurve& base_entity, ConversionData& conv)
  262. : base_entity(base_entity)
  263. , conv(conv) {
  264. // empty
  265. }
  266. public:
  267. typedef std::pair<IfcFloat, IfcFloat> ParamRange;
  268. virtual ~Curve() = default;
  269. // check if a curve is closed
  270. virtual bool IsClosed() const = 0;
  271. // evaluate the curve at the given parametric position
  272. virtual IfcVector3 Eval(IfcFloat p) const = 0;
  273. // try to match a point on the curve to a given parameter
  274. // for self-intersecting curves, the result is not ambiguous and
  275. // it is undefined which parameter is returned.
  276. virtual bool ReverseEval(const IfcVector3& val, IfcFloat& paramOut) const;
  277. // get the range of the curve (both inclusive).
  278. // +inf and -inf are valid return values, the curve is not bounded in such a case.
  279. virtual std::pair<IfcFloat,IfcFloat> GetParametricRange() const = 0;
  280. IfcFloat GetParametricRangeDelta() const;
  281. // estimate the number of sample points that this curve will require
  282. virtual size_t EstimateSampleCount(IfcFloat start,IfcFloat end) const;
  283. // intelligently sample the curve based on the current settings
  284. // and append the result to the mesh
  285. virtual void SampleDiscrete(TempMesh& out,IfcFloat start,IfcFloat end) const;
  286. #ifdef ASSIMP_BUILD_DEBUG
  287. // check if a particular parameter value lies within the well-defined range
  288. bool InRange(IfcFloat) const;
  289. #endif
  290. static Curve* Convert(const IFC::Schema_2x3::IfcCurve&,ConversionData& conv);
  291. protected:
  292. const Schema_2x3::IfcCurve& base_entity;
  293. ConversionData& conv;
  294. };
  295. // --------------------------------------------------------------------------------
  296. // A BoundedCurve always holds the invariant that GetParametricRange()
  297. // never returns infinite values.
  298. // --------------------------------------------------------------------------------
  299. class BoundedCurve : public Curve {
  300. public:
  301. BoundedCurve(const Schema_2x3::IfcBoundedCurve& entity, ConversionData& conv)
  302. : Curve(entity,conv)
  303. {}
  304. public:
  305. bool IsClosed() const;
  306. public:
  307. // sample the entire curve
  308. void SampleDiscrete(TempMesh& out) const;
  309. using Curve::SampleDiscrete;
  310. };
  311. // IfcProfile.cpp
  312. bool ProcessCurve(const Schema_2x3::IfcCurve& curve, TempMesh& meshout, ConversionData& conv);
  313. }
  314. }
  315. #endif