IFCUtil.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, 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 "IFCReaderGen.h"
  39. #include "IFCLoader.h"
  40. namespace Assimp {
  41. namespace IFC {
  42. typedef double IfcFloat;
  43. // IfcFloat-precision math data types
  44. typedef aiVector2t<IfcFloat> IfcVector2;
  45. typedef aiVector3t<IfcFloat> IfcVector3;
  46. typedef aiMatrix4x4t<IfcFloat> IfcMatrix4;
  47. typedef aiMatrix3x3t<IfcFloat> IfcMatrix3;
  48. typedef aiColor4t<IfcFloat> IfcColor4;
  49. // helper for std::for_each to delete all heap-allocated items in a container
  50. template<typename T>
  51. struct delete_fun
  52. {
  53. void operator()(T* del) {
  54. delete del;
  55. }
  56. };
  57. // ------------------------------------------------------------------------------------------------
  58. // Temporary representation of an opening in a wall or a floor
  59. // ------------------------------------------------------------------------------------------------
  60. struct TempMesh;
  61. struct TempOpening
  62. {
  63. const IFC::IfcSolidModel* solid;
  64. IfcVector3 extrusionDir;
  65. boost::shared_ptr<TempMesh> profileMesh;
  66. // list of points generated for this opening. This is used to
  67. // create connections between two opposing holes created
  68. // from a single opening instance (two because walls tend to
  69. // have two sides). If !empty(), the other side of the wall
  70. // has already been processed.
  71. std::vector<IfcVector3> wallPoints;
  72. // ------------------------------------------------------------------------------
  73. TempOpening()
  74. : solid()
  75. , extrusionDir()
  76. , profileMesh()
  77. {
  78. }
  79. // ------------------------------------------------------------------------------
  80. TempOpening(const IFC::IfcSolidModel* solid,IfcVector3 extrusionDir,boost::shared_ptr<TempMesh> profileMesh)
  81. : solid(solid)
  82. , extrusionDir(extrusionDir)
  83. , profileMesh(profileMesh)
  84. {
  85. }
  86. // ------------------------------------------------------------------------------
  87. void Transform(const IfcMatrix4& mat); // defined later since TempMesh is not complete yet
  88. };
  89. // ------------------------------------------------------------------------------------------------
  90. // Intermediate data storage during conversion. Keeps everything and a bit more.
  91. // ------------------------------------------------------------------------------------------------
  92. struct ConversionData
  93. {
  94. ConversionData(const STEP::DB& db, const IFC::IfcProject& proj, aiScene* out,const IFCImporter::Settings& settings)
  95. : len_scale(1.0)
  96. , angle_scale(-1.0)
  97. , db(db)
  98. , proj(proj)
  99. , out(out)
  100. , settings(settings)
  101. , apply_openings()
  102. , collect_openings()
  103. {}
  104. ~ConversionData() {
  105. std::for_each(meshes.begin(),meshes.end(),delete_fun<aiMesh>());
  106. std::for_each(materials.begin(),materials.end(),delete_fun<aiMaterial>());
  107. }
  108. IfcFloat len_scale, angle_scale;
  109. bool plane_angle_in_radians;
  110. const STEP::DB& db;
  111. const IFC::IfcProject& proj;
  112. aiScene* out;
  113. IfcMatrix4 wcs;
  114. std::vector<aiMesh*> meshes;
  115. std::vector<aiMaterial*> materials;
  116. typedef std::map<const IFC::IfcRepresentationItem*, std::vector<unsigned int> > MeshCache;
  117. MeshCache cached_meshes;
  118. const IFCImporter::Settings& settings;
  119. // Intermediate arrays used to resolve openings in walls: only one of them
  120. // can be given at a time. apply_openings if present if the current element
  121. // is a wall and needs its openings to be poured into its geometry while
  122. // collect_openings is present only if the current element is an
  123. // IfcOpeningElement, for which all the geometry needs to be preserved
  124. // for later processing by a parent, which is a wall.
  125. std::vector<TempOpening>* apply_openings;
  126. std::vector<TempOpening>* collect_openings;
  127. };
  128. // ------------------------------------------------------------------------------------------------
  129. // Binary predicate to compare vectors with a given, quadratic epsilon.
  130. // ------------------------------------------------------------------------------------------------
  131. struct FuzzyVectorCompare {
  132. FuzzyVectorCompare(IfcFloat epsilon) : epsilon(epsilon) {}
  133. bool operator()(const IfcVector3& a, const IfcVector3& b) {
  134. return fabs((a-b).SquareLength()) < epsilon;
  135. }
  136. const IfcFloat epsilon;
  137. };
  138. // ------------------------------------------------------------------------------------------------
  139. // Helper used during mesh construction. Aids at creating aiMesh'es out of relatively few polygons.
  140. // ------------------------------------------------------------------------------------------------
  141. struct TempMesh
  142. {
  143. std::vector<IfcVector3> verts;
  144. std::vector<unsigned int> vertcnt;
  145. // utilities
  146. aiMesh* ToMesh();
  147. void Clear();
  148. void Transform(const IfcMatrix4& mat);
  149. IfcVector3 Center() const;
  150. void Append(const TempMesh& other);
  151. bool IsEmpty() const {
  152. return verts.empty() && vertcnt.empty();
  153. }
  154. void RemoveAdjacentDuplicates();
  155. void RemoveDegenerates();
  156. void FixupFaceOrientation();
  157. IfcVector3 ComputeLastPolygonNormal(bool normalize = true) const;
  158. void ComputePolygonNormals(std::vector<IfcVector3>& normals,
  159. bool normalize = true,
  160. size_t ofs = 0) const;
  161. };
  162. // conversion routines for common IFC entities, implemented in IFCUtil.cpp
  163. void ConvertColor(aiColor4D& out, const IfcColourRgb& in);
  164. void ConvertColor(aiColor4D& out, const IfcColourOrFactor& in,ConversionData& conv,const aiColor4D* base);
  165. void ConvertCartesianPoint(IfcVector3& out, const IfcCartesianPoint& in);
  166. void ConvertDirection(IfcVector3& out, const IfcDirection& in);
  167. void ConvertVector(IfcVector3& out, const IfcVector& in);
  168. void AssignMatrixAxes(IfcMatrix4& out, const IfcVector3& x, const IfcVector3& y, const IfcVector3& z);
  169. void ConvertAxisPlacement(IfcMatrix4& out, const IfcAxis2Placement3D& in);
  170. void ConvertAxisPlacement(IfcMatrix4& out, const IfcAxis2Placement2D& in);
  171. void ConvertAxisPlacement(IfcVector3& axis, IfcVector3& pos, const IFC::IfcAxis1Placement& in);
  172. void ConvertAxisPlacement(IfcMatrix4& out, const IfcAxis2Placement& in, ConversionData& conv);
  173. void ConvertTransformOperator(IfcMatrix4& out, const IfcCartesianTransformationOperator& op);
  174. bool IsTrue(const EXPRESS::BOOLEAN& in);
  175. IfcFloat ConvertSIPrefix(const std::string& prefix);
  176. // IFCProfile.cpp
  177. bool ProcessProfile(const IfcProfileDef& prof, TempMesh& meshout, ConversionData& conv);
  178. // IFCMaterial.cpp
  179. unsigned int ProcessMaterials(const IFC::IfcRepresentationItem& item, ConversionData& conv);
  180. // IFCGeometry.cpp
  181. bool ProcessRepresentationItem(const IfcRepresentationItem& item, std::vector<unsigned int>& mesh_indices, ConversionData& conv);
  182. void AssignAddedMeshes(std::vector<unsigned int>& mesh_indices,aiNode* nd,ConversionData& /*conv*/);
  183. // IFCCurve.cpp
  184. // ------------------------------------------------------------------------------------------------
  185. // Custom exception for use by members of the Curve class
  186. // ------------------------------------------------------------------------------------------------
  187. class CurveError
  188. {
  189. public:
  190. CurveError(const std::string& s)
  191. : s(s)
  192. {
  193. }
  194. std::string s;
  195. };
  196. // ------------------------------------------------------------------------------------------------
  197. // Temporary representation for an arbitrary sub-class of IfcCurve. Used to sample the curves
  198. // to obtain a list of line segments.
  199. // ------------------------------------------------------------------------------------------------
  200. class Curve
  201. {
  202. protected:
  203. Curve(const IfcCurve& base_entity, ConversionData& conv)
  204. : base_entity(base_entity)
  205. , conv(conv)
  206. {}
  207. public:
  208. typedef std::pair<IfcFloat, IfcFloat> ParamRange;
  209. public:
  210. virtual ~Curve() {}
  211. // check if a curve is closed
  212. virtual bool IsClosed() const = 0;
  213. // evaluate the curve at the given parametric position
  214. virtual IfcVector3 Eval(IfcFloat p) const = 0;
  215. // try to match a point on the curve to a given parameter
  216. // for self-intersecting curves, the result is not ambiguous and
  217. // it is undefined which parameter is returned.
  218. virtual bool ReverseEval(const IfcVector3& val, IfcFloat& paramOut) const;
  219. // get the range of the curve (both inclusive).
  220. // +inf and -inf are valid return values, the curve is not bounded in such a case.
  221. virtual std::pair<IfcFloat,IfcFloat> GetParametricRange() const = 0;
  222. IfcFloat GetParametricRangeDelta() const;
  223. // estimate the number of sample points that this curve will require
  224. virtual size_t EstimateSampleCount(IfcFloat start,IfcFloat end) const;
  225. // intelligently sample the curve based on the current settings
  226. // and append the result to the mesh
  227. virtual void SampleDiscrete(TempMesh& out,IfcFloat start,IfcFloat end) const;
  228. #ifdef _DEBUG
  229. // check if a particular parameter value lies within the well-defined range
  230. bool InRange(IfcFloat) const;
  231. #endif
  232. public:
  233. static Curve* Convert(const IFC::IfcCurve&,ConversionData& conv);
  234. protected:
  235. const IfcCurve& base_entity;
  236. ConversionData& conv;
  237. };
  238. // --------------------------------------------------------------------------------
  239. // A BoundedCurve always holds the invariant that GetParametricRange()
  240. // never returns infinite values.
  241. // --------------------------------------------------------------------------------
  242. class BoundedCurve : public Curve
  243. {
  244. public:
  245. BoundedCurve(const IfcBoundedCurve& entity, ConversionData& conv)
  246. : Curve(entity,conv)
  247. {}
  248. public:
  249. bool IsClosed() const;
  250. public:
  251. // sample the entire curve
  252. void SampleDiscrete(TempMesh& out) const;
  253. using Curve::SampleDiscrete;
  254. };
  255. }
  256. }
  257. #endif