IFCUtil.h 10 KB

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