2
0

IFCUtil.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, 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 IFCUtil.cpp
  34. * @brief Implementation of conversion routines for some common Ifc helper entities.
  35. */
  36. #ifndef ASSIMP_BUILD_NO_IFC_IMPORTER
  37. #include "AssetLib/IFC/IFCUtil.h"
  38. #include "Common/PolyTools.h"
  39. #include "PostProcessing/ProcessHelper.h"
  40. #include <assimp/Defines.h>
  41. namespace Assimp {
  42. namespace IFC {
  43. // ------------------------------------------------------------------------------------------------
  44. void TempOpening::Transform(const IfcMatrix4& mat) {
  45. if(profileMesh) {
  46. profileMesh->Transform(mat);
  47. }
  48. if(profileMesh2D) {
  49. profileMesh2D->Transform(mat);
  50. }
  51. extrusionDir *= IfcMatrix3(mat);
  52. }
  53. // ------------------------------------------------------------------------------------------------
  54. aiMesh* TempMesh::ToMesh()
  55. {
  56. ai_assert(mVerts.size() == std::accumulate(mVertcnt.begin(),mVertcnt.end(),size_t(0)));
  57. if (mVerts.empty()) {
  58. return nullptr;
  59. }
  60. std::unique_ptr<aiMesh> mesh(new aiMesh());
  61. // copy vertices
  62. mesh->mNumVertices = static_cast<unsigned int>(mVerts.size());
  63. mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  64. std::copy(mVerts.begin(),mVerts.end(),mesh->mVertices);
  65. // and build up faces
  66. mesh->mNumFaces = static_cast<unsigned int>(mVertcnt.size());
  67. mesh->mFaces = new aiFace[mesh->mNumFaces];
  68. for(unsigned int i = 0,n=0, acc = 0; i < mesh->mNumFaces; ++n) {
  69. aiFace& f = mesh->mFaces[i];
  70. if (!mVertcnt[n]) {
  71. --mesh->mNumFaces;
  72. continue;
  73. }
  74. f.mNumIndices = mVertcnt[n];
  75. f.mIndices = new unsigned int[f.mNumIndices];
  76. for(unsigned int a = 0; a < f.mNumIndices; ++a) {
  77. f.mIndices[a] = acc++;
  78. }
  79. ++i;
  80. }
  81. return mesh.release();
  82. }
  83. // ------------------------------------------------------------------------------------------------
  84. void TempMesh::Clear()
  85. {
  86. mVerts.clear();
  87. mVertcnt.clear();
  88. }
  89. // ------------------------------------------------------------------------------------------------
  90. void TempMesh::Transform(const IfcMatrix4& mat)
  91. {
  92. for(IfcVector3& v : mVerts) {
  93. v *= mat;
  94. }
  95. }
  96. // ------------------------------------------------------------------------------
  97. IfcVector3 TempMesh::Center() const
  98. {
  99. return (mVerts.size() == 0) ? IfcVector3(0.0f, 0.0f, 0.0f) : (std::accumulate(mVerts.begin(),mVerts.end(),IfcVector3()) / static_cast<IfcFloat>(mVerts.size()));
  100. }
  101. // ------------------------------------------------------------------------------------------------
  102. void TempMesh::Append(const TempMesh& other)
  103. {
  104. mVerts.insert(mVerts.end(),other.mVerts.begin(),other.mVerts.end());
  105. mVertcnt.insert(mVertcnt.end(),other.mVertcnt.begin(),other.mVertcnt.end());
  106. }
  107. // ------------------------------------------------------------------------------------------------
  108. void TempMesh::RemoveDegenerates()
  109. {
  110. // The strategy is simple: walk the mesh and compute normals using
  111. // Newell's algorithm. The length of the normals gives the area
  112. // of the polygons, which is close to zero for lines.
  113. std::vector<IfcVector3> normals;
  114. ComputePolygonNormals(normals, false);
  115. bool drop = false;
  116. size_t inor = 0;
  117. std::vector<IfcVector3>::iterator vit = mVerts.begin();
  118. for (std::vector<unsigned int>::iterator it = mVertcnt.begin(); it != mVertcnt.end(); ++inor) {
  119. const unsigned int pcount = *it;
  120. if (normals[inor].SquareLength() < 1e-10f) {
  121. it = mVertcnt.erase(it);
  122. vit = mVerts.erase(vit, vit + pcount);
  123. drop = true;
  124. continue;
  125. }
  126. vit += pcount;
  127. ++it;
  128. }
  129. if(drop) {
  130. IFCImporter::LogVerboseDebug("removing degenerate faces");
  131. }
  132. }
  133. // ------------------------------------------------------------------------------------------------
  134. IfcVector3 TempMesh::ComputePolygonNormal(const IfcVector3* vtcs, size_t cnt, bool normalize)
  135. {
  136. std::vector<IfcFloat> temp((cnt+2)*3);
  137. for( size_t vofs = 0, i = 0; vofs < cnt; ++vofs )
  138. {
  139. const IfcVector3& v = vtcs[vofs];
  140. temp[i++] = v.x;
  141. temp[i++] = v.y;
  142. temp[i++] = v.z;
  143. }
  144. IfcVector3 nor;
  145. NewellNormal<3, 3, 3>(nor, static_cast<int>(cnt), &temp[0], &temp[1], &temp[2]);
  146. return normalize ? nor.Normalize() : nor;
  147. }
  148. // ------------------------------------------------------------------------------------------------
  149. void TempMesh::ComputePolygonNormals(std::vector<IfcVector3>& normals,
  150. bool normalize,
  151. size_t ofs) const
  152. {
  153. size_t max_vcount = 0;
  154. std::vector<unsigned int>::const_iterator begin = mVertcnt.begin()+ofs, end = mVertcnt.end(), iit;
  155. for(iit = begin; iit != end; ++iit) {
  156. max_vcount = std::max(max_vcount,static_cast<size_t>(*iit));
  157. }
  158. std::vector<IfcFloat> temp((max_vcount+2)*4);
  159. normals.reserve( normals.size() + mVertcnt.size()-ofs );
  160. // `NewellNormal()` currently has a relatively strange interface and need to
  161. // re-structure things a bit to meet them.
  162. size_t vidx = std::accumulate(mVertcnt.begin(),begin,0);
  163. for(iit = begin; iit != end; vidx += *iit++) {
  164. if (!*iit) {
  165. normals.push_back(IfcVector3());
  166. continue;
  167. }
  168. for(size_t vofs = 0, cnt = 0; vofs < *iit; ++vofs) {
  169. const IfcVector3& v = mVerts[vidx+vofs];
  170. temp[cnt++] = v.x;
  171. temp[cnt++] = v.y;
  172. temp[cnt++] = v.z;
  173. #ifdef ASSIMP_BUILD_DEBUG
  174. temp[cnt] = std::numeric_limits<IfcFloat>::quiet_NaN();
  175. #endif
  176. ++cnt;
  177. }
  178. normals.push_back(IfcVector3());
  179. NewellNormal<4,4,4>(normals.back(),*iit,&temp[0],&temp[1],&temp[2]);
  180. }
  181. if(normalize) {
  182. for(IfcVector3& n : normals) {
  183. n.Normalize();
  184. }
  185. }
  186. }
  187. // ------------------------------------------------------------------------------------------------
  188. // Compute the normal of the last polygon in the given mesh
  189. IfcVector3 TempMesh::ComputeLastPolygonNormal(bool normalize) const
  190. {
  191. return ComputePolygonNormal(&mVerts[mVerts.size() - mVertcnt.back()], mVertcnt.back(), normalize);
  192. }
  193. struct CompareVector
  194. {
  195. bool operator () (const IfcVector3& a, const IfcVector3& b) const
  196. {
  197. IfcVector3 d = a - b;
  198. IfcFloat eps = 1e-6;
  199. return d.x < -eps || (std::abs(d.x) < eps && d.y < -eps) || (std::abs(d.x) < eps && std::abs(d.y) < eps && d.z < -eps);
  200. }
  201. };
  202. struct FindVector
  203. {
  204. IfcVector3 v;
  205. FindVector(const IfcVector3& p) : v(p) { }
  206. bool operator () (const IfcVector3& p) { return FuzzyVectorCompare(1e-6)(p, v); }
  207. };
  208. // ------------------------------------------------------------------------------------------------
  209. void TempMesh::FixupFaceOrientation()
  210. {
  211. const IfcVector3 vavg = Center();
  212. // create a list of start indices for all faces to allow random access to faces
  213. std::vector<size_t> faceStartIndices(mVertcnt.size());
  214. for( size_t i = 0, a = 0; a < mVertcnt.size(); i += mVertcnt[a], ++a )
  215. faceStartIndices[a] = i;
  216. // list all faces on a vertex
  217. std::map<IfcVector3, std::vector<size_t>, CompareVector> facesByVertex;
  218. for( size_t a = 0; a < mVertcnt.size(); ++a )
  219. {
  220. for( size_t b = 0; b < mVertcnt[a]; ++b )
  221. facesByVertex[mVerts[faceStartIndices[a] + b]].push_back(a);
  222. }
  223. // determine neighbourhood for all polys
  224. std::vector<size_t> neighbour(mVerts.size(), SIZE_MAX);
  225. std::vector<size_t> tempIntersect(10);
  226. for( size_t a = 0; a < mVertcnt.size(); ++a )
  227. {
  228. for( size_t b = 0; b < mVertcnt[a]; ++b )
  229. {
  230. size_t ib = faceStartIndices[a] + b, nib = faceStartIndices[a] + (b + 1) % mVertcnt[a];
  231. const std::vector<size_t>& facesOnB = facesByVertex[mVerts[ib]];
  232. const std::vector<size_t>& facesOnNB = facesByVertex[mVerts[nib]];
  233. // there should be exactly one or two faces which appear in both lists. Our face and the other side
  234. std::vector<size_t>::iterator sectstart = tempIntersect.begin();
  235. std::vector<size_t>::iterator sectend = std::set_intersection(
  236. facesOnB.begin(), facesOnB.end(), facesOnNB.begin(), facesOnNB.end(), sectstart);
  237. if( std::distance(sectstart, sectend) != 2 )
  238. continue;
  239. if( *sectstart == a )
  240. ++sectstart;
  241. neighbour[ib] = *sectstart;
  242. }
  243. }
  244. // now we're getting started. We take the face which is the farthest away from the center. This face is most probably
  245. // facing outwards. So we reverse this face to point outwards in relation to the center. Then we adapt neighbouring
  246. // faces to have the same winding until all faces have been tested.
  247. std::vector<bool> faceDone(mVertcnt.size(), false);
  248. while( std::count(faceDone.begin(), faceDone.end(), false) != 0 )
  249. {
  250. // find the farthest of the remaining faces
  251. size_t farthestIndex = SIZE_MAX;
  252. IfcFloat farthestDistance = -1.0;
  253. for( size_t a = 0; a < mVertcnt.size(); ++a )
  254. {
  255. if( faceDone[a] )
  256. continue;
  257. IfcVector3 faceCenter = std::accumulate(mVerts.begin() + faceStartIndices[a],
  258. mVerts.begin() + faceStartIndices[a] + mVertcnt[a], IfcVector3(0.0)) / IfcFloat(mVertcnt[a]);
  259. IfcFloat dst = (faceCenter - vavg).SquareLength();
  260. if( dst > farthestDistance ) { farthestDistance = dst; farthestIndex = a; }
  261. }
  262. // calculate its normal and reverse the poly if its facing towards the mesh center
  263. IfcVector3 farthestNormal = ComputePolygonNormal(mVerts.data() + faceStartIndices[farthestIndex], mVertcnt[farthestIndex]);
  264. IfcVector3 farthestCenter = std::accumulate(mVerts.begin() + faceStartIndices[farthestIndex],
  265. mVerts.begin() + faceStartIndices[farthestIndex] + mVertcnt[farthestIndex], IfcVector3(0.0))
  266. / IfcFloat(mVertcnt[farthestIndex]);
  267. // We accept a bit of negative orientation without reversing. In case of doubt, prefer the orientation given in
  268. // the file.
  269. if( (farthestNormal * (farthestCenter - vavg).Normalize()) < -0.4 )
  270. {
  271. size_t fsi = faceStartIndices[farthestIndex], fvc = mVertcnt[farthestIndex];
  272. std::reverse(mVerts.begin() + fsi, mVerts.begin() + fsi + fvc);
  273. std::reverse(neighbour.begin() + fsi, neighbour.begin() + fsi + fvc);
  274. // because of the neighbour index belonging to the edge starting with the point at the same index, we need to
  275. // cycle the neighbours through to match the edges again.
  276. // Before: points A - B - C - D with edge neighbour p - q - r - s
  277. // After: points D - C - B - A, reversed neighbours are s - r - q - p, but the should be
  278. // r q p s
  279. for( size_t a = 0; a < fvc - 1; ++a )
  280. std::swap(neighbour[fsi + a], neighbour[fsi + a + 1]);
  281. }
  282. faceDone[farthestIndex] = true;
  283. std::vector<size_t> todo;
  284. todo.push_back(farthestIndex);
  285. // go over its neighbour faces recursively and adapt their winding order to match the farthest face
  286. while( !todo.empty() )
  287. {
  288. size_t tdf = todo.back();
  289. size_t vsi = faceStartIndices[tdf], vc = mVertcnt[tdf];
  290. todo.pop_back();
  291. // check its neighbours
  292. for( size_t a = 0; a < vc; ++a )
  293. {
  294. // ignore neighbours if we already checked them
  295. size_t nbi = neighbour[vsi + a];
  296. if( nbi == SIZE_MAX || faceDone[nbi] )
  297. continue;
  298. const IfcVector3& vp = mVerts[vsi + a];
  299. size_t nbvsi = faceStartIndices[nbi], nbvc = mVertcnt[nbi];
  300. std::vector<IfcVector3>::iterator it = std::find_if(mVerts.begin() + nbvsi, mVerts.begin() + nbvsi + nbvc, FindVector(vp));
  301. ai_assert(it != mVerts.begin() + nbvsi + nbvc);
  302. size_t nb_vidx = std::distance(mVerts.begin() + nbvsi, it);
  303. // two faces winded in the same direction should have a crossed edge, where one face has p0->p1 and the other
  304. // has p1'->p0'. If the next point on the neighbouring face is also the next on the current face, we need
  305. // to reverse the neighbour
  306. nb_vidx = (nb_vidx + 1) % nbvc;
  307. size_t oursideidx = (a + 1) % vc;
  308. if( FuzzyVectorCompare(1e-6)(mVerts[vsi + oursideidx], mVerts[nbvsi + nb_vidx]) )
  309. {
  310. std::reverse(mVerts.begin() + nbvsi, mVerts.begin() + nbvsi + nbvc);
  311. std::reverse(neighbour.begin() + nbvsi, neighbour.begin() + nbvsi + nbvc);
  312. for (size_t aa = 0; aa < nbvc - 1; ++aa) {
  313. std::swap(neighbour[nbvsi + aa], neighbour[nbvsi + aa + 1]);
  314. }
  315. }
  316. // either way we're done with the neighbour. Mark it as done and continue checking from there recursively
  317. faceDone[nbi] = true;
  318. todo.push_back(nbi);
  319. }
  320. }
  321. // no more faces reachable from this part of the surface, start over with a disjunct part and its farthest face
  322. }
  323. }
  324. // ------------------------------------------------------------------------------------------------
  325. void TempMesh::RemoveAdjacentDuplicates() {
  326. bool drop = false;
  327. std::vector<IfcVector3>::iterator base = mVerts.begin();
  328. for(unsigned int& cnt : mVertcnt) {
  329. if (cnt < 2){
  330. base += cnt;
  331. continue;
  332. }
  333. IfcVector3 vmin,vmax;
  334. ArrayBounds(&*base, cnt ,vmin,vmax);
  335. const IfcFloat epsilon = (vmax-vmin).SquareLength() / static_cast<IfcFloat>(1e9);
  336. //const IfcFloat dotepsilon = 1e-9;
  337. //// look for vertices that lie directly on the line between their predecessor and their
  338. //// successor and replace them with either of them.
  339. //for(size_t i = 0; i < cnt; ++i) {
  340. // IfcVector3& v1 = *(base+i), &v0 = *(base+(i?i-1:cnt-1)), &v2 = *(base+(i+1)%cnt);
  341. // const IfcVector3& d0 = (v1-v0), &d1 = (v2-v1);
  342. // const IfcFloat l0 = d0.SquareLength(), l1 = d1.SquareLength();
  343. // if (!l0 || !l1) {
  344. // continue;
  345. // }
  346. // const IfcFloat d = (d0/std::sqrt(l0))*(d1/std::sqrt(l1));
  347. // if ( d >= 1.f-dotepsilon ) {
  348. // v1 = v0;
  349. // }
  350. // else if ( d < -1.f+dotepsilon ) {
  351. // v2 = v1;
  352. // continue;
  353. // }
  354. //}
  355. // drop any identical, adjacent vertices. this pass will collect the dropouts
  356. // of the previous pass as a side-effect.
  357. FuzzyVectorCompare fz(epsilon);
  358. std::vector<IfcVector3>::iterator end = base+cnt, e = std::unique( base, end, fz );
  359. if (e != end) {
  360. cnt -= static_cast<unsigned int>(std::distance(e, end));
  361. mVerts.erase(e,end);
  362. drop = true;
  363. }
  364. // check front and back vertices for this polygon
  365. if (cnt > 1 && fz(*base,*(base+cnt-1))) {
  366. mVerts.erase(base+ --cnt);
  367. drop = true;
  368. }
  369. // removing adjacent duplicates shouldn't erase everything :-)
  370. ai_assert(cnt>0);
  371. base += cnt;
  372. }
  373. if(drop) {
  374. IFCImporter::LogVerboseDebug("removing duplicate vertices");
  375. }
  376. }
  377. // ------------------------------------------------------------------------------------------------
  378. void TempMesh::Swap(TempMesh& other)
  379. {
  380. mVertcnt.swap(other.mVertcnt);
  381. mVerts.swap(other.mVerts);
  382. }
  383. // ------------------------------------------------------------------------------------------------
  384. bool IsTrue(const ::Assimp::STEP::EXPRESS::BOOLEAN& in)
  385. {
  386. return (std::string)in == "TRUE" || (std::string)in == "T";
  387. }
  388. // ------------------------------------------------------------------------------------------------
  389. IfcFloat ConvertSIPrefix(const std::string& prefix)
  390. {
  391. if (prefix == "EXA") {
  392. return 1e18f;
  393. }
  394. else if (prefix == "PETA") {
  395. return 1e15f;
  396. }
  397. else if (prefix == "TERA") {
  398. return 1e12f;
  399. }
  400. else if (prefix == "GIGA") {
  401. return 1e9f;
  402. }
  403. else if (prefix == "MEGA") {
  404. return 1e6f;
  405. }
  406. else if (prefix == "KILO") {
  407. return 1e3f;
  408. }
  409. else if (prefix == "HECTO") {
  410. return 1e2f;
  411. }
  412. else if (prefix == "DECA") {
  413. return 1e-0f;
  414. }
  415. else if (prefix == "DECI") {
  416. return 1e-1f;
  417. }
  418. else if (prefix == "CENTI") {
  419. return 1e-2f;
  420. }
  421. else if (prefix == "MILLI") {
  422. return 1e-3f;
  423. }
  424. else if (prefix == "MICRO") {
  425. return 1e-6f;
  426. }
  427. else if (prefix == "NANO") {
  428. return 1e-9f;
  429. }
  430. else if (prefix == "PICO") {
  431. return 1e-12f;
  432. }
  433. else if (prefix == "FEMTO") {
  434. return 1e-15f;
  435. }
  436. else if (prefix == "ATTO") {
  437. return 1e-18f;
  438. }
  439. else {
  440. IFCImporter::LogError("Unrecognized SI prefix: " + prefix);
  441. return 1;
  442. }
  443. }
  444. // ------------------------------------------------------------------------------------------------
  445. void ConvertColor(aiColor4D& out, const Schema_2x3::IfcColourRgb& in)
  446. {
  447. out.r = static_cast<float>( in.Red );
  448. out.g = static_cast<float>( in.Green );
  449. out.b = static_cast<float>( in.Blue );
  450. out.a = static_cast<float>( 1.f );
  451. }
  452. // ------------------------------------------------------------------------------------------------
  453. void ConvertColor(aiColor4D& out, const Schema_2x3::IfcColourOrFactor& in,ConversionData& conv,const aiColor4D* base)
  454. {
  455. if (const ::Assimp::STEP::EXPRESS::REAL* const r = in.ToPtr<::Assimp::STEP::EXPRESS::REAL>()) {
  456. out.r = out.g = out.b = static_cast<float>(*r);
  457. if(base) {
  458. out.r *= static_cast<float>( base->r );
  459. out.g *= static_cast<float>( base->g );
  460. out.b *= static_cast<float>( base->b );
  461. out.a = static_cast<float>( base->a );
  462. }
  463. else out.a = 1.0;
  464. }
  465. else if (const Schema_2x3::IfcColourRgb* const rgb = in.ResolveSelectPtr<Schema_2x3::IfcColourRgb>(conv.db)) {
  466. ConvertColor(out,*rgb);
  467. }
  468. else {
  469. IFCImporter::LogWarn("skipping unknown IfcColourOrFactor entity");
  470. }
  471. }
  472. // ------------------------------------------------------------------------------------------------
  473. void ConvertCartesianPoint(IfcVector3& out, const Schema_2x3::IfcCartesianPoint& in)
  474. {
  475. out = IfcVector3();
  476. for(size_t i = 0; i < in.Coordinates.size(); ++i) {
  477. out[static_cast<unsigned int>(i)] = in.Coordinates[i];
  478. }
  479. }
  480. // ------------------------------------------------------------------------------------------------
  481. void ConvertVector(IfcVector3& out, const Schema_2x3::IfcVector& in)
  482. {
  483. ConvertDirection(out,in.Orientation);
  484. out *= in.Magnitude;
  485. }
  486. // ------------------------------------------------------------------------------------------------
  487. void ConvertDirection(IfcVector3& out, const Schema_2x3::IfcDirection& in)
  488. {
  489. out = IfcVector3();
  490. for(size_t i = 0; i < in.DirectionRatios.size(); ++i) {
  491. out[static_cast<unsigned int>(i)] = in.DirectionRatios[i];
  492. }
  493. const IfcFloat len = out.Length();
  494. if (len<1e-6) {
  495. IFCImporter::LogWarn("direction vector magnitude too small, normalization would result in a division by zero");
  496. return;
  497. }
  498. out /= len;
  499. }
  500. // ------------------------------------------------------------------------------------------------
  501. void AssignMatrixAxes(IfcMatrix4& out, const IfcVector3& x, const IfcVector3& y, const IfcVector3& z)
  502. {
  503. out.a1 = x.x;
  504. out.b1 = x.y;
  505. out.c1 = x.z;
  506. out.a2 = y.x;
  507. out.b2 = y.y;
  508. out.c2 = y.z;
  509. out.a3 = z.x;
  510. out.b3 = z.y;
  511. out.c3 = z.z;
  512. }
  513. // ------------------------------------------------------------------------------------------------
  514. void ConvertAxisPlacement(IfcMatrix4& out, const Schema_2x3::IfcAxis2Placement3D& in)
  515. {
  516. IfcVector3 loc;
  517. ConvertCartesianPoint(loc,in.Location);
  518. IfcVector3 z(0.f,0.f,1.f),r(1.f,0.f,0.f),x;
  519. if (in.Axis) {
  520. ConvertDirection(z,*in.Axis.Get());
  521. }
  522. if (in.RefDirection) {
  523. ConvertDirection(r,*in.RefDirection.Get());
  524. }
  525. IfcVector3 v = r.Normalize();
  526. IfcVector3 tmpx = z * (v*z);
  527. x = (v-tmpx).Normalize();
  528. IfcVector3 y = (z^x);
  529. IfcMatrix4::Translation(loc,out);
  530. AssignMatrixAxes(out,x,y,z);
  531. }
  532. // ------------------------------------------------------------------------------------------------
  533. void ConvertAxisPlacement(IfcMatrix4& out, const Schema_2x3::IfcAxis2Placement2D& in)
  534. {
  535. IfcVector3 loc;
  536. ConvertCartesianPoint(loc,in.Location);
  537. IfcVector3 x(1.f,0.f,0.f);
  538. if (in.RefDirection) {
  539. ConvertDirection(x,*in.RefDirection.Get());
  540. }
  541. const IfcVector3 y = IfcVector3(x.y,-x.x,0.f);
  542. IfcMatrix4::Translation(loc,out);
  543. AssignMatrixAxes(out,x,y,IfcVector3(0.f,0.f,1.f));
  544. }
  545. // ------------------------------------------------------------------------------------------------
  546. void ConvertAxisPlacement(IfcVector3& axis, IfcVector3& pos, const Schema_2x3::IfcAxis1Placement& in)
  547. {
  548. ConvertCartesianPoint(pos,in.Location);
  549. if (in.Axis) {
  550. ConvertDirection(axis,in.Axis.Get());
  551. }
  552. else {
  553. axis = IfcVector3(0.f,0.f,1.f);
  554. }
  555. }
  556. // ------------------------------------------------------------------------------------------------
  557. void ConvertAxisPlacement(IfcMatrix4& out, const Schema_2x3::IfcAxis2Placement& in, ConversionData& conv)
  558. {
  559. if(const Schema_2x3::IfcAxis2Placement3D* pl3 = in.ResolveSelectPtr<Schema_2x3::IfcAxis2Placement3D>(conv.db)) {
  560. ConvertAxisPlacement(out,*pl3);
  561. }
  562. else if(const Schema_2x3::IfcAxis2Placement2D* pl2 = in.ResolveSelectPtr<Schema_2x3::IfcAxis2Placement2D>(conv.db)) {
  563. ConvertAxisPlacement(out,*pl2);
  564. }
  565. else {
  566. IFCImporter::LogWarn("skipping unknown IfcAxis2Placement entity");
  567. }
  568. }
  569. // ------------------------------------------------------------------------------------------------
  570. void ConvertTransformOperator(IfcMatrix4& out, const Schema_2x3::IfcCartesianTransformationOperator& op)
  571. {
  572. IfcVector3 loc;
  573. ConvertCartesianPoint(loc,op.LocalOrigin);
  574. IfcVector3 x(1.f,0.f,0.f),y(0.f,1.f,0.f),z(0.f,0.f,1.f);
  575. if (op.Axis1) {
  576. ConvertDirection(x,*op.Axis1.Get());
  577. }
  578. if (op.Axis2) {
  579. ConvertDirection(y,*op.Axis2.Get());
  580. }
  581. if (const Schema_2x3::IfcCartesianTransformationOperator3D* op2 = op.ToPtr<Schema_2x3::IfcCartesianTransformationOperator3D>()) {
  582. if(op2->Axis3) {
  583. ConvertDirection(z,*op2->Axis3.Get());
  584. }
  585. }
  586. IfcMatrix4 locm;
  587. IfcMatrix4::Translation(loc,locm);
  588. AssignMatrixAxes(out,x,y,z);
  589. IfcVector3 vscale;
  590. if (const Schema_2x3::IfcCartesianTransformationOperator3DnonUniform* nuni = op.ToPtr<Schema_2x3::IfcCartesianTransformationOperator3DnonUniform>()) {
  591. vscale.x = nuni->Scale?op.Scale.Get():1.f;
  592. vscale.y = nuni->Scale2?nuni->Scale2.Get():1.f;
  593. vscale.z = nuni->Scale3?nuni->Scale3.Get():1.f;
  594. }
  595. else {
  596. const IfcFloat sc = op.Scale?op.Scale.Get():1.f;
  597. vscale = IfcVector3(sc,sc,sc);
  598. }
  599. IfcMatrix4 s;
  600. IfcMatrix4::Scaling(vscale,s);
  601. out = locm * out * s;
  602. }
  603. } // ! IFC
  604. } // ! Assimp
  605. #endif