You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, 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. #include "AssimpPCH.h"
  37. #ifndef ASSIMP_BUILD_NO_IFC_IMPORTER
  38. #include "IFCUtil.h"
  39. #include "PolyTools.h"
  40. #include "ProcessHelper.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 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-5f) {
  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. void TempMesh::ComputePolygonNormals(std::vector<IfcVector3>& normals,
  136. bool normalize,
  137. size_t ofs) const
  138. {
  139. size_t max_vcount = 0;
  140. std::vector<unsigned int>::const_iterator begin = vertcnt.begin()+ofs, end = vertcnt.end(), iit;
  141. for(iit = begin; iit != end; ++iit) {
  142. max_vcount = std::max(max_vcount,static_cast<size_t>(*iit));
  143. }
  144. std::vector<IfcFloat> temp((max_vcount+2)*4);
  145. normals.reserve( normals.size() + vertcnt.size()-ofs );
  146. // `NewellNormal()` currently has a relatively strange interface and need to
  147. // re-structure things a bit to meet them.
  148. size_t vidx = std::accumulate(vertcnt.begin(),begin,0);
  149. for(iit = begin; iit != end; vidx += *iit++) {
  150. if (!*iit) {
  151. normals.push_back(IfcVector3());
  152. continue;
  153. }
  154. for(size_t vofs = 0, cnt = 0; vofs < *iit; ++vofs) {
  155. const IfcVector3& v = verts[vidx+vofs];
  156. temp[cnt++] = v.x;
  157. temp[cnt++] = v.y;
  158. temp[cnt++] = v.z;
  159. #ifdef ASSIMP_BUILD_DEBUG
  160. temp[cnt] = std::numeric_limits<IfcFloat>::quiet_NaN();
  161. #endif
  162. ++cnt;
  163. }
  164. normals.push_back(IfcVector3());
  165. NewellNormal<4,4,4>(normals.back(),*iit,&temp[0],&temp[1],&temp[2]);
  166. }
  167. if(normalize) {
  168. BOOST_FOREACH(IfcVector3& n, normals) {
  169. n.Normalize();
  170. }
  171. }
  172. }
  173. // ------------------------------------------------------------------------------------------------
  174. // Compute the normal of the last polygon in the given mesh
  175. IfcVector3 TempMesh::ComputeLastPolygonNormal(bool normalize) const
  176. {
  177. size_t total = vertcnt.back(), vidx = verts.size() - total;
  178. std::vector<IfcFloat> temp((total+2)*3);
  179. for(size_t vofs = 0, cnt = 0; vofs < total; ++vofs) {
  180. const IfcVector3& v = verts[vidx+vofs];
  181. temp[cnt++] = v.x;
  182. temp[cnt++] = v.y;
  183. temp[cnt++] = v.z;
  184. }
  185. IfcVector3 nor;
  186. NewellNormal<3,3,3>(nor,total,&temp[0],&temp[1],&temp[2]);
  187. return normalize ? nor.Normalize() : nor;
  188. }
  189. // ------------------------------------------------------------------------------------------------
  190. void TempMesh::FixupFaceOrientation()
  191. {
  192. const IfcVector3 vavg = Center();
  193. std::vector<IfcVector3> normals;
  194. ComputePolygonNormals(normals);
  195. size_t c = 0, ofs = 0;
  196. BOOST_FOREACH(unsigned int cnt, vertcnt) {
  197. if (cnt>2){
  198. const IfcVector3& thisvert = verts[c];
  199. if (normals[ofs]*(thisvert-vavg) < 0) {
  200. std::reverse(verts.begin()+c,verts.begin()+cnt+c);
  201. }
  202. }
  203. c += cnt;
  204. ++ofs;
  205. }
  206. }
  207. // ------------------------------------------------------------------------------------------------
  208. void TempMesh::RemoveAdjacentDuplicates()
  209. {
  210. bool drop = false;
  211. std::vector<IfcVector3>::iterator base = verts.begin();
  212. BOOST_FOREACH(unsigned int& cnt, vertcnt) {
  213. if (cnt < 2){
  214. base += cnt;
  215. continue;
  216. }
  217. IfcVector3 vmin,vmax;
  218. ArrayBounds(&*base, cnt ,vmin,vmax);
  219. const IfcFloat epsilon = (vmax-vmin).SquareLength() / static_cast<IfcFloat>(1e9);
  220. //const IfcFloat dotepsilon = 1e-9;
  221. //// look for vertices that lie directly on the line between their predecessor and their
  222. //// successor and replace them with either of them.
  223. //for(size_t i = 0; i < cnt; ++i) {
  224. // IfcVector3& v1 = *(base+i), &v0 = *(base+(i?i-1:cnt-1)), &v2 = *(base+(i+1)%cnt);
  225. // const IfcVector3& d0 = (v1-v0), &d1 = (v2-v1);
  226. // const IfcFloat l0 = d0.SquareLength(), l1 = d1.SquareLength();
  227. // if (!l0 || !l1) {
  228. // continue;
  229. // }
  230. // const IfcFloat d = (d0/sqrt(l0))*(d1/sqrt(l1));
  231. // if ( d >= 1.f-dotepsilon ) {
  232. // v1 = v0;
  233. // }
  234. // else if ( d < -1.f+dotepsilon ) {
  235. // v2 = v1;
  236. // continue;
  237. // }
  238. //}
  239. // drop any identical, adjacent vertices. this pass will collect the dropouts
  240. // of the previous pass as a side-effect.
  241. FuzzyVectorCompare fz(epsilon);
  242. std::vector<IfcVector3>::iterator end = base+cnt, e = std::unique( base, end, fz );
  243. if (e != end) {
  244. cnt -= static_cast<unsigned int>(std::distance(e, end));
  245. verts.erase(e,end);
  246. drop = true;
  247. }
  248. // check front and back vertices for this polygon
  249. if (cnt > 1 && fz(*base,*(base+cnt-1))) {
  250. verts.erase(base+ --cnt);
  251. drop = true;
  252. }
  253. // removing adjacent duplicates shouldn't erase everything :-)
  254. ai_assert(cnt>0);
  255. base += cnt;
  256. }
  257. if(drop) {
  258. IFCImporter::LogDebug("removing duplicate vertices");
  259. }
  260. }
  261. // ------------------------------------------------------------------------------------------------
  262. void TempMesh::Swap(TempMesh& other)
  263. {
  264. vertcnt.swap(other.vertcnt);
  265. verts.swap(other.verts);
  266. }
  267. // ------------------------------------------------------------------------------------------------
  268. bool IsTrue(const EXPRESS::BOOLEAN& in)
  269. {
  270. return (std::string)in == "TRUE" || (std::string)in == "T";
  271. }
  272. // ------------------------------------------------------------------------------------------------
  273. IfcFloat ConvertSIPrefix(const std::string& prefix)
  274. {
  275. if (prefix == "EXA") {
  276. return 1e18f;
  277. }
  278. else if (prefix == "PETA") {
  279. return 1e15f;
  280. }
  281. else if (prefix == "TERA") {
  282. return 1e12f;
  283. }
  284. else if (prefix == "GIGA") {
  285. return 1e9f;
  286. }
  287. else if (prefix == "MEGA") {
  288. return 1e6f;
  289. }
  290. else if (prefix == "KILO") {
  291. return 1e3f;
  292. }
  293. else if (prefix == "HECTO") {
  294. return 1e2f;
  295. }
  296. else if (prefix == "DECA") {
  297. return 1e-0f;
  298. }
  299. else if (prefix == "DECI") {
  300. return 1e-1f;
  301. }
  302. else if (prefix == "CENTI") {
  303. return 1e-2f;
  304. }
  305. else if (prefix == "MILLI") {
  306. return 1e-3f;
  307. }
  308. else if (prefix == "MICRO") {
  309. return 1e-6f;
  310. }
  311. else if (prefix == "NANO") {
  312. return 1e-9f;
  313. }
  314. else if (prefix == "PICO") {
  315. return 1e-12f;
  316. }
  317. else if (prefix == "FEMTO") {
  318. return 1e-15f;
  319. }
  320. else if (prefix == "ATTO") {
  321. return 1e-18f;
  322. }
  323. else {
  324. IFCImporter::LogError("Unrecognized SI prefix: " + prefix);
  325. return 1;
  326. }
  327. }
  328. // ------------------------------------------------------------------------------------------------
  329. void ConvertColor(aiColor4D& out, const IfcColourRgb& in)
  330. {
  331. out.r = static_cast<float>( in.Red );
  332. out.g = static_cast<float>( in.Green );
  333. out.b = static_cast<float>( in.Blue );
  334. out.a = static_cast<float>( 1.f );
  335. }
  336. // ------------------------------------------------------------------------------------------------
  337. void ConvertColor(aiColor4D& out, const IfcColourOrFactor& in,ConversionData& conv,const aiColor4D* base)
  338. {
  339. if (const EXPRESS::REAL* const r = in.ToPtr<EXPRESS::REAL>()) {
  340. out.r = out.g = out.b = static_cast<float>(*r);
  341. if(base) {
  342. out.r *= static_cast<float>( base->r );
  343. out.g *= static_cast<float>( base->g );
  344. out.b *= static_cast<float>( base->b );
  345. out.a = static_cast<float>( base->a );
  346. }
  347. else out.a = 1.0;
  348. }
  349. else if (const IfcColourRgb* const rgb = in.ResolveSelectPtr<IfcColourRgb>(conv.db)) {
  350. ConvertColor(out,*rgb);
  351. }
  352. else {
  353. IFCImporter::LogWarn("skipping unknown IfcColourOrFactor entity");
  354. }
  355. }
  356. // ------------------------------------------------------------------------------------------------
  357. void ConvertCartesianPoint(IfcVector3& out, const IfcCartesianPoint& in)
  358. {
  359. out = IfcVector3();
  360. for(size_t i = 0; i < in.Coordinates.size(); ++i) {
  361. out[i] = in.Coordinates[i];
  362. }
  363. }
  364. // ------------------------------------------------------------------------------------------------
  365. void ConvertVector(IfcVector3& out, const IfcVector& in)
  366. {
  367. ConvertDirection(out,in.Orientation);
  368. out *= in.Magnitude;
  369. }
  370. // ------------------------------------------------------------------------------------------------
  371. void ConvertDirection(IfcVector3& out, const IfcDirection& in)
  372. {
  373. out = IfcVector3();
  374. for(size_t i = 0; i < in.DirectionRatios.size(); ++i) {
  375. out[i] = in.DirectionRatios[i];
  376. }
  377. const IfcFloat len = out.Length();
  378. if (len<1e-6) {
  379. IFCImporter::LogWarn("direction vector magnitude too small, normalization would result in a division by zero");
  380. return;
  381. }
  382. out /= len;
  383. }
  384. // ------------------------------------------------------------------------------------------------
  385. void AssignMatrixAxes(IfcMatrix4& out, const IfcVector3& x, const IfcVector3& y, const IfcVector3& z)
  386. {
  387. out.a1 = x.x;
  388. out.b1 = x.y;
  389. out.c1 = x.z;
  390. out.a2 = y.x;
  391. out.b2 = y.y;
  392. out.c2 = y.z;
  393. out.a3 = z.x;
  394. out.b3 = z.y;
  395. out.c3 = z.z;
  396. }
  397. // ------------------------------------------------------------------------------------------------
  398. void ConvertAxisPlacement(IfcMatrix4& out, const IfcAxis2Placement3D& in)
  399. {
  400. IfcVector3 loc;
  401. ConvertCartesianPoint(loc,in.Location);
  402. IfcVector3 z(0.f,0.f,1.f),r(1.f,0.f,0.f),x;
  403. if (in.Axis) {
  404. ConvertDirection(z,*in.Axis.Get());
  405. }
  406. if (in.RefDirection) {
  407. ConvertDirection(r,*in.RefDirection.Get());
  408. }
  409. IfcVector3 v = r.Normalize();
  410. IfcVector3 tmpx = z * (v*z);
  411. x = (v-tmpx).Normalize();
  412. IfcVector3 y = (z^x);
  413. IfcMatrix4::Translation(loc,out);
  414. AssignMatrixAxes(out,x,y,z);
  415. }
  416. // ------------------------------------------------------------------------------------------------
  417. void ConvertAxisPlacement(IfcMatrix4& out, const IfcAxis2Placement2D& in)
  418. {
  419. IfcVector3 loc;
  420. ConvertCartesianPoint(loc,in.Location);
  421. IfcVector3 x(1.f,0.f,0.f);
  422. if (in.RefDirection) {
  423. ConvertDirection(x,*in.RefDirection.Get());
  424. }
  425. const IfcVector3 y = IfcVector3(x.y,-x.x,0.f);
  426. IfcMatrix4::Translation(loc,out);
  427. AssignMatrixAxes(out,x,y,IfcVector3(0.f,0.f,1.f));
  428. }
  429. // ------------------------------------------------------------------------------------------------
  430. void ConvertAxisPlacement(IfcVector3& axis, IfcVector3& pos, const IfcAxis1Placement& in)
  431. {
  432. ConvertCartesianPoint(pos,in.Location);
  433. if (in.Axis) {
  434. ConvertDirection(axis,in.Axis.Get());
  435. }
  436. else {
  437. axis = IfcVector3(0.f,0.f,1.f);
  438. }
  439. }
  440. // ------------------------------------------------------------------------------------------------
  441. void ConvertAxisPlacement(IfcMatrix4& out, const IfcAxis2Placement& in, ConversionData& conv)
  442. {
  443. if(const IfcAxis2Placement3D* pl3 = in.ResolveSelectPtr<IfcAxis2Placement3D>(conv.db)) {
  444. ConvertAxisPlacement(out,*pl3);
  445. }
  446. else if(const IfcAxis2Placement2D* pl2 = in.ResolveSelectPtr<IfcAxis2Placement2D>(conv.db)) {
  447. ConvertAxisPlacement(out,*pl2);
  448. }
  449. else {
  450. IFCImporter::LogWarn("skipping unknown IfcAxis2Placement entity");
  451. }
  452. }
  453. // ------------------------------------------------------------------------------------------------
  454. void ConvertTransformOperator(IfcMatrix4& out, const IfcCartesianTransformationOperator& op)
  455. {
  456. IfcVector3 loc;
  457. ConvertCartesianPoint(loc,op.LocalOrigin);
  458. IfcVector3 x(1.f,0.f,0.f),y(0.f,1.f,0.f),z(0.f,0.f,1.f);
  459. if (op.Axis1) {
  460. ConvertDirection(x,*op.Axis1.Get());
  461. }
  462. if (op.Axis2) {
  463. ConvertDirection(y,*op.Axis2.Get());
  464. }
  465. if (const IfcCartesianTransformationOperator3D* op2 = op.ToPtr<IfcCartesianTransformationOperator3D>()) {
  466. if(op2->Axis3) {
  467. ConvertDirection(z,*op2->Axis3.Get());
  468. }
  469. }
  470. IfcMatrix4 locm;
  471. IfcMatrix4::Translation(loc,locm);
  472. AssignMatrixAxes(out,x,y,z);
  473. IfcVector3 vscale;
  474. if (const IfcCartesianTransformationOperator3DnonUniform* nuni = op.ToPtr<IfcCartesianTransformationOperator3DnonUniform>()) {
  475. vscale.x = nuni->Scale?op.Scale.Get():1.f;
  476. vscale.y = nuni->Scale2?nuni->Scale2.Get():1.f;
  477. vscale.z = nuni->Scale3?nuni->Scale3.Get():1.f;
  478. }
  479. else {
  480. const IfcFloat sc = op.Scale?op.Scale.Get():1.f;
  481. vscale = IfcVector3(sc,sc,sc);
  482. }
  483. IfcMatrix4 s;
  484. IfcMatrix4::Scaling(vscale,s);
  485. out = locm * out * s;
  486. }
  487. } // ! IFC
  488. } // ! Assimp
  489. #endif