IFCUtil.cpp 24 KB

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