2
0

IFCUtil.cpp 24 KB

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