IFCUtil.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 IFCUtil.cpp
  34. * @brief Implementation of conversion routines for some common Ifc helper entities.
  35. */
  36. #include "AssimpPCH.h"
  37. #ifndef ASSIMP_BUILD_NO_IFC_IMPORTER
  38. #include "IFCUtil.h"
  39. #include "ProcessHelper.h"
  40. namespace Assimp {
  41. namespace IFC {
  42. // ------------------------------------------------------------------------------------------------
  43. void TempOpening::Transform(const aiMatrix4x4& mat)
  44. {
  45. if(profileMesh) {
  46. profileMesh->Transform(mat);
  47. }
  48. extrusionDir *= aiMatrix3x3(mat);
  49. }
  50. // ------------------------------------------------------------------------------------------------
  51. aiMesh* TempMesh::ToMesh()
  52. {
  53. ai_assert(verts.size() == std::accumulate(vertcnt.begin(),vertcnt.end(),0));
  54. if (verts.empty()) {
  55. return NULL;
  56. }
  57. std::auto_ptr<aiMesh> mesh(new aiMesh());
  58. // copy vertices
  59. mesh->mNumVertices = static_cast<unsigned int>(verts.size());
  60. mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  61. std::copy(verts.begin(),verts.end(),mesh->mVertices);
  62. // and build up faces
  63. mesh->mNumFaces = static_cast<unsigned int>(vertcnt.size());
  64. mesh->mFaces = new aiFace[mesh->mNumFaces];
  65. for(unsigned int i = 0,n=0, acc = 0; i < mesh->mNumFaces; ++n) {
  66. aiFace& f = mesh->mFaces[i];
  67. if (!vertcnt[n]) {
  68. --mesh->mNumFaces;
  69. continue;
  70. }
  71. f.mNumIndices = vertcnt[n];
  72. f.mIndices = new unsigned int[f.mNumIndices];
  73. for(unsigned int a = 0; a < f.mNumIndices; ++a) {
  74. f.mIndices[a] = acc++;
  75. }
  76. ++i;
  77. }
  78. return mesh.release();
  79. }
  80. // ------------------------------------------------------------------------------------------------
  81. void TempMesh::Clear()
  82. {
  83. verts.clear();
  84. vertcnt.clear();
  85. }
  86. // ------------------------------------------------------------------------------------------------
  87. void TempMesh::Transform(const aiMatrix4x4& mat)
  88. {
  89. BOOST_FOREACH(aiVector3D& v, verts) {
  90. v *= mat;
  91. }
  92. }
  93. // ------------------------------------------------------------------------------
  94. aiVector3D TempMesh::Center() const
  95. {
  96. return std::accumulate(verts.begin(),verts.end(),aiVector3D(0.f,0.f,0.f)) / static_cast<float>(verts.size());
  97. }
  98. // ------------------------------------------------------------------------------------------------
  99. void TempMesh::Append(const TempMesh& other)
  100. {
  101. verts.insert(verts.end(),other.verts.begin(),other.verts.end());
  102. vertcnt.insert(vertcnt.end(),other.vertcnt.begin(),other.vertcnt.end());
  103. }
  104. // ------------------------------------------------------------------------------------------------
  105. void TempMesh::RemoveAdjacentDuplicates()
  106. {
  107. bool drop = false;
  108. std::vector<aiVector3D>::iterator base = verts.begin();
  109. BOOST_FOREACH(unsigned int& cnt, vertcnt) {
  110. if (cnt < 2){
  111. base += cnt;
  112. continue;
  113. }
  114. aiVector3D vmin,vmax;
  115. ArrayBounds(&*base, cnt ,vmin,vmax);
  116. const float epsilon = (vmax-vmin).SquareLength() / 1e9f;
  117. //const float dotepsilon = 1e-9;
  118. //// look for vertices that lie directly on the line between their predecessor and their
  119. //// successor and replace them with either of them.
  120. //for(size_t i = 0; i < cnt; ++i) {
  121. // aiVector3D& v1 = *(base+i), &v0 = *(base+(i?i-1:cnt-1)), &v2 = *(base+(i+1)%cnt);
  122. // const aiVector3D& d0 = (v1-v0), &d1 = (v2-v1);
  123. // const float l0 = d0.SquareLength(), l1 = d1.SquareLength();
  124. // if (!l0 || !l1) {
  125. // continue;
  126. // }
  127. // const float d = (d0/sqrt(l0))*(d1/sqrt(l1));
  128. // if ( d >= 1.f-dotepsilon ) {
  129. // v1 = v0;
  130. // }
  131. // else if ( d < -1.f+dotepsilon ) {
  132. // v2 = v1;
  133. // continue;
  134. // }
  135. //}
  136. // drop any identical, adjacent vertices. this pass will collect the dropouts
  137. // of the previous pass as a side-effect.
  138. FuzzyVectorCompare fz(epsilon);
  139. std::vector<aiVector3D>::iterator end = base+cnt, e = std::unique( base, end, fz );
  140. if (e != end) {
  141. cnt -= static_cast<unsigned int>(std::distance(e, end));
  142. verts.erase(e,end);
  143. drop = true;
  144. }
  145. // check front and back vertices for this polygon
  146. if (cnt > 1 && fz(*base,*(base+cnt-1))) {
  147. verts.erase(base+ --cnt);
  148. drop = true;
  149. }
  150. // removing adjacent duplicates shouldn't erase everything :-)
  151. ai_assert(cnt>0);
  152. base += cnt;
  153. }
  154. if(drop) {
  155. IFCImporter::LogDebug("removed duplicate vertices");
  156. }
  157. }
  158. // ------------------------------------------------------------------------------------------------
  159. bool IsTrue(const EXPRESS::BOOLEAN& in)
  160. {
  161. return (std::string)in == "TRUE" || (std::string)in == "T";
  162. }
  163. // ------------------------------------------------------------------------------------------------
  164. float ConvertSIPrefix(const std::string& prefix)
  165. {
  166. if (prefix == "EXA") {
  167. return 1e18f;
  168. }
  169. else if (prefix == "PETA") {
  170. return 1e15f;
  171. }
  172. else if (prefix == "TERA") {
  173. return 1e12f;
  174. }
  175. else if (prefix == "GIGA") {
  176. return 1e9f;
  177. }
  178. else if (prefix == "MEGA") {
  179. return 1e6f;
  180. }
  181. else if (prefix == "KILO") {
  182. return 1e3f;
  183. }
  184. else if (prefix == "HECTO") {
  185. return 1e2f;
  186. }
  187. else if (prefix == "DECA") {
  188. return 1e-0f;
  189. }
  190. else if (prefix == "DECI") {
  191. return 1e-1f;
  192. }
  193. else if (prefix == "CENTI") {
  194. return 1e-2f;
  195. }
  196. else if (prefix == "MILLI") {
  197. return 1e-3f;
  198. }
  199. else if (prefix == "MICRO") {
  200. return 1e-6f;
  201. }
  202. else if (prefix == "NANO") {
  203. return 1e-9f;
  204. }
  205. else if (prefix == "PICO") {
  206. return 1e-12f;
  207. }
  208. else if (prefix == "FEMTO") {
  209. return 1e-15f;
  210. }
  211. else if (prefix == "ATTO") {
  212. return 1e-18f;
  213. }
  214. else {
  215. IFCImporter::LogError("Unrecognized SI prefix: " + prefix);
  216. return 1;
  217. }
  218. }
  219. // ------------------------------------------------------------------------------------------------
  220. void ConvertColor(aiColor4D& out, const IfcColourRgb& in)
  221. {
  222. out.r = in.Red;
  223. out.g = in.Green;
  224. out.b = in.Blue;
  225. out.a = 1.f;
  226. }
  227. // ------------------------------------------------------------------------------------------------
  228. void ConvertColor(aiColor4D& out, const IfcColourOrFactor& in,ConversionData& conv,const aiColor4D* base)
  229. {
  230. if (const EXPRESS::REAL* const r = in.ToPtr<EXPRESS::REAL>()) {
  231. out.r = out.g = out.b = *r;
  232. if(base) {
  233. out.r *= base->r;
  234. out.g *= base->g;
  235. out.b *= base->b;
  236. out.a = base->a;
  237. }
  238. else out.a = 1.0;
  239. }
  240. else if (const IfcColourRgb* const rgb = in.ResolveSelectPtr<IfcColourRgb>(conv.db)) {
  241. ConvertColor(out,*rgb);
  242. }
  243. else {
  244. IFCImporter::LogWarn("skipping unknown IfcColourOrFactor entity");
  245. }
  246. }
  247. // ------------------------------------------------------------------------------------------------
  248. void ConvertCartesianPoint(aiVector3D& out, const IfcCartesianPoint& in)
  249. {
  250. out = aiVector3D();
  251. for(size_t i = 0; i < in.Coordinates.size(); ++i) {
  252. out[i] = in.Coordinates[i];
  253. }
  254. }
  255. // ------------------------------------------------------------------------------------------------
  256. void ConvertVector(aiVector3D& out, const IfcVector& in)
  257. {
  258. ConvertDirection(out,in.Orientation);
  259. out *= in.Magnitude;
  260. }
  261. // ------------------------------------------------------------------------------------------------
  262. void ConvertDirection(aiVector3D& out, const IfcDirection& in)
  263. {
  264. out = aiVector3D();
  265. for(size_t i = 0; i < in.DirectionRatios.size(); ++i) {
  266. out[i] = in.DirectionRatios[i];
  267. }
  268. const float len = out.Length();
  269. if (len<1e-6) {
  270. IFCImporter::LogWarn("direction vector magnitude too small, normalization would result in a division by zero");
  271. return;
  272. }
  273. out /= len;
  274. }
  275. // ------------------------------------------------------------------------------------------------
  276. void AssignMatrixAxes(aiMatrix4x4& out, const aiVector3D& x, const aiVector3D& y, const aiVector3D& z)
  277. {
  278. out.a1 = x.x;
  279. out.b1 = x.y;
  280. out.c1 = x.z;
  281. out.a2 = y.x;
  282. out.b2 = y.y;
  283. out.c2 = y.z;
  284. out.a3 = z.x;
  285. out.b3 = z.y;
  286. out.c3 = z.z;
  287. }
  288. // ------------------------------------------------------------------------------------------------
  289. void ConvertAxisPlacement(aiMatrix4x4& out, const IfcAxis2Placement3D& in)
  290. {
  291. aiVector3D loc;
  292. ConvertCartesianPoint(loc,in.Location);
  293. aiVector3D z(0.f,0.f,1.f),r(1.f,0.f,0.f),x;
  294. if (in.Axis) {
  295. ConvertDirection(z,*in.Axis.Get());
  296. }
  297. if (in.RefDirection) {
  298. ConvertDirection(r,*in.RefDirection.Get());
  299. }
  300. aiVector3D v = r.Normalize();
  301. aiVector3D tmpx = z * (v*z);
  302. x = (v-tmpx).Normalize();
  303. aiVector3D y = (z^x);
  304. aiMatrix4x4::Translation(loc,out);
  305. AssignMatrixAxes(out,x,y,z);
  306. }
  307. // ------------------------------------------------------------------------------------------------
  308. void ConvertAxisPlacement(aiMatrix4x4& out, const IfcAxis2Placement2D& in)
  309. {
  310. aiVector3D loc;
  311. ConvertCartesianPoint(loc,in.Location);
  312. aiVector3D x(1.f,0.f,0.f);
  313. if (in.RefDirection) {
  314. ConvertDirection(x,*in.RefDirection.Get());
  315. }
  316. const aiVector3D y = aiVector3D(x.y,-x.x,0.f);
  317. aiMatrix4x4::Translation(loc,out);
  318. AssignMatrixAxes(out,x,y,aiVector3D(0.f,0.f,1.f));
  319. }
  320. // ------------------------------------------------------------------------------------------------
  321. void ConvertAxisPlacement(aiVector3D& axis, aiVector3D& pos, const IfcAxis1Placement& in)
  322. {
  323. ConvertCartesianPoint(pos,in.Location);
  324. if (in.Axis) {
  325. ConvertDirection(axis,in.Axis.Get());
  326. }
  327. else {
  328. axis = aiVector3D(0.f,0.f,1.f);
  329. }
  330. }
  331. // ------------------------------------------------------------------------------------------------
  332. void ConvertAxisPlacement(aiMatrix4x4& out, const IfcAxis2Placement& in, ConversionData& conv)
  333. {
  334. if(const IfcAxis2Placement3D* pl3 = in.ResolveSelectPtr<IfcAxis2Placement3D>(conv.db)) {
  335. ConvertAxisPlacement(out,*pl3);
  336. }
  337. else if(const IfcAxis2Placement2D* pl2 = in.ResolveSelectPtr<IfcAxis2Placement2D>(conv.db)) {
  338. ConvertAxisPlacement(out,*pl2);
  339. }
  340. else {
  341. IFCImporter::LogWarn("skipping unknown IfcAxis2Placement entity");
  342. }
  343. }
  344. // ------------------------------------------------------------------------------------------------
  345. void ConvertTransformOperator(aiMatrix4x4& out, const IfcCartesianTransformationOperator& op)
  346. {
  347. aiVector3D loc;
  348. ConvertCartesianPoint(loc,op.LocalOrigin);
  349. aiVector3D x(1.f,0.f,0.f),y(0.f,1.f,0.f),z(0.f,0.f,1.f);
  350. if (op.Axis1) {
  351. ConvertDirection(x,*op.Axis1.Get());
  352. }
  353. if (op.Axis2) {
  354. ConvertDirection(y,*op.Axis2.Get());
  355. }
  356. if (const IfcCartesianTransformationOperator3D* op2 = op.ToPtr<IfcCartesianTransformationOperator3D>()) {
  357. if(op2->Axis3) {
  358. ConvertDirection(z,*op2->Axis3.Get());
  359. }
  360. }
  361. aiMatrix4x4 locm;
  362. aiMatrix4x4::Translation(loc,locm);
  363. AssignMatrixAxes(out,x,y,z);
  364. aiVector3D vscale;
  365. if (const IfcCartesianTransformationOperator3DnonUniform* nuni = op.ToPtr<IfcCartesianTransformationOperator3DnonUniform>()) {
  366. vscale.x = nuni->Scale?op.Scale.Get():1.f;
  367. vscale.y = nuni->Scale2?nuni->Scale2.Get():1.f;
  368. vscale.z = nuni->Scale3?nuni->Scale3.Get():1.f;
  369. }
  370. else {
  371. const float sc = op.Scale?op.Scale.Get():1.f;
  372. vscale = aiVector3D(sc,sc,sc);
  373. }
  374. aiMatrix4x4 s;
  375. aiMatrix4x4::Scaling(vscale,s);
  376. out = locm * out * s;
  377. }
  378. } // ! IFC
  379. } // ! Assimp
  380. #endif