IFCGeometry.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2010, ASSIMP Development Team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the ASSIMP team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the ASSIMP Development Team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file IFCGeometry.cpp
  34. * @brief Geometry conversion and synthesis for IFC
  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. #include "../contrib/poly2tri/poly2tri/poly2tri.h"
  42. #include "../contrib/clipper/clipper.hpp"
  43. #include <iterator>
  44. namespace Assimp {
  45. namespace IFC {
  46. // ------------------------------------------------------------------------------------------------
  47. bool ProcessPolyloop(const IfcPolyLoop& loop, TempMesh& meshout, ConversionData& /*conv*/)
  48. {
  49. size_t cnt = 0;
  50. BOOST_FOREACH(const IfcCartesianPoint& c, loop.Polygon) {
  51. aiVector3D tmp;
  52. ConvertCartesianPoint(tmp,c);
  53. meshout.verts.push_back(tmp);
  54. ++cnt;
  55. }
  56. meshout.vertcnt.push_back(cnt);
  57. // zero- or one- vertex polyloops simply ignored
  58. if (meshout.vertcnt.back() > 1) {
  59. return true;
  60. }
  61. if (meshout.vertcnt.back()==1) {
  62. meshout.vertcnt.pop_back();
  63. meshout.verts.pop_back();
  64. }
  65. return false;
  66. }
  67. // ------------------------------------------------------------------------------------------------
  68. void ComputePolygonNormals(const TempMesh& meshout, std::vector<aiVector3D>& normals, bool normalize = true, size_t ofs = 0)
  69. {
  70. size_t max_vcount = 0;
  71. std::vector<unsigned int>::const_iterator begin=meshout.vertcnt.begin()+ofs, end=meshout.vertcnt.end(), iit;
  72. for(iit = begin; iit != end; ++iit) {
  73. max_vcount = std::max(max_vcount,static_cast<size_t>(*iit));
  74. }
  75. std::vector<float> temp((max_vcount+2)*4);
  76. normals.reserve( normals.size() + meshout.vertcnt.size()-ofs );
  77. // `NewellNormal()` currently has a relatively strange interface and need to
  78. // re-structure things a bit to meet them.
  79. size_t vidx = std::accumulate(meshout.vertcnt.begin(),begin,0);
  80. for(iit = begin; iit != end; vidx += *iit++) {
  81. if (!*iit) {
  82. normals.push_back(aiVector3D());
  83. continue;
  84. }
  85. for(size_t vofs = 0, cnt = 0; vofs < *iit; ++vofs) {
  86. const aiVector3D& v = meshout.verts[vidx+vofs];
  87. temp[cnt++] = v.x;
  88. temp[cnt++] = v.y;
  89. temp[cnt++] = v.z;
  90. #ifdef _DEBUG
  91. temp[cnt] = std::numeric_limits<float>::quiet_NaN();
  92. #endif
  93. ++cnt;
  94. }
  95. normals.push_back(aiVector3D());
  96. NewellNormal<4,4,4>(normals.back(),*iit,&temp[0],&temp[1],&temp[2]);
  97. }
  98. if(normalize) {
  99. BOOST_FOREACH(aiVector3D& n, normals) {
  100. n.Normalize();
  101. }
  102. }
  103. }
  104. // ------------------------------------------------------------------------------------------------
  105. // Compute the normal of the last polygon in the given mesh
  106. aiVector3D ComputePolygonNormal(const TempMesh& inmesh, bool normalize = true)
  107. {
  108. size_t total = inmesh.vertcnt.back(), vidx = inmesh.verts.size() - total;
  109. std::vector<float> temp((total+2)*3);
  110. for(size_t vofs = 0, cnt = 0; vofs < total; ++vofs) {
  111. const aiVector3D& v = inmesh.verts[vidx+vofs];
  112. temp[cnt++] = v.x;
  113. temp[cnt++] = v.y;
  114. temp[cnt++] = v.z;
  115. }
  116. aiVector3D nor;
  117. NewellNormal<3,3,3>(nor,total,&temp[0],&temp[1],&temp[2]);
  118. return normalize ? nor.Normalize() : nor;
  119. }
  120. // ------------------------------------------------------------------------------------------------
  121. void FixupFaceOrientation(TempMesh& result)
  122. {
  123. const aiVector3D vavg = result.Center();
  124. std::vector<aiVector3D> normals;
  125. ComputePolygonNormals(result,normals);
  126. size_t c = 0, ofs = 0;
  127. BOOST_FOREACH(unsigned int cnt, result.vertcnt) {
  128. if (cnt>2){
  129. const aiVector3D& thisvert = result.verts[c];
  130. if (normals[ofs]*(thisvert-vavg) < 0) {
  131. std::reverse(result.verts.begin()+c,result.verts.begin()+cnt+c);
  132. }
  133. }
  134. c += cnt;
  135. ++ofs;
  136. }
  137. }
  138. // ------------------------------------------------------------------------------------------------
  139. void RecursiveMergeBoundaries(TempMesh& final_result, const TempMesh& in, const TempMesh& boundary, std::vector<aiVector3D>& normals, const aiVector3D& nor_boundary)
  140. {
  141. ai_assert(in.vertcnt.size() >= 1);
  142. ai_assert(boundary.vertcnt.size() == 1);
  143. std::vector<unsigned int>::const_iterator end = in.vertcnt.end(), begin=in.vertcnt.begin(), iit, best_iit;
  144. TempMesh out;
  145. // iterate through all other bounds and find the one for which the shortest connection
  146. // to the outer boundary is actually the shortest possible.
  147. size_t vidx = 0, best_vidx_start = 0;
  148. size_t best_ofs, best_outer = boundary.verts.size();
  149. float best_dist = 1e10;
  150. for(std::vector<unsigned int>::const_iterator iit = begin; iit != end; vidx += *iit++) {
  151. for(size_t vofs = 0; vofs < *iit; ++vofs) {
  152. const aiVector3D& v = in.verts[vidx+vofs];
  153. for(size_t outer = 0; outer < boundary.verts.size(); ++outer) {
  154. const aiVector3D& o = boundary.verts[outer];
  155. const float d = (o-v).SquareLength();
  156. if (d < best_dist) {
  157. best_dist = d;
  158. best_ofs = vofs;
  159. best_outer = outer;
  160. best_iit = iit;
  161. best_vidx_start = vidx;
  162. }
  163. }
  164. }
  165. }
  166. ai_assert(best_outer != boundary.verts.size());
  167. // now that we collected all vertex connections to be added, build the output polygon
  168. const size_t cnt = boundary.verts.size() + *best_iit+2;
  169. out.verts.reserve(cnt);
  170. for(size_t outer = 0; outer < boundary.verts.size(); ++outer) {
  171. const aiVector3D& o = boundary.verts[outer];
  172. out.verts.push_back(o);
  173. if (outer == best_outer) {
  174. for(size_t i = best_ofs; i < *best_iit; ++i) {
  175. out.verts.push_back(in.verts[best_vidx_start + i]);
  176. }
  177. // we need the first vertex of the inner polygon twice as we return to the
  178. // outer loop through the very same connection through which we got there.
  179. for(size_t i = 0; i <= best_ofs; ++i) {
  180. out.verts.push_back(in.verts[best_vidx_start + i]);
  181. }
  182. // reverse face winding if the normal of the sub-polygon points in the
  183. // same direction as the normal of the outer polygonal boundary
  184. if (normals[std::distance(begin,best_iit)] * nor_boundary > 0) {
  185. std::reverse(out.verts.rbegin(),out.verts.rbegin()+*best_iit+1);
  186. }
  187. // also append a copy of the initial insertion point to be able to continue the outer polygon
  188. out.verts.push_back(o);
  189. }
  190. }
  191. out.vertcnt.push_back(cnt);
  192. ai_assert(out.verts.size() == cnt);
  193. if (in.vertcnt.size()-std::count(begin,end,0) > 1) {
  194. // Recursively apply the same algorithm if there are more boundaries to merge. The
  195. // current implementation is relatively inefficient, though.
  196. TempMesh temp;
  197. // drop the boundary that we just processed
  198. const size_t dist = std::distance(begin, best_iit);
  199. TempMesh remaining = in;
  200. remaining.vertcnt.erase(remaining.vertcnt.begin() + dist);
  201. remaining.verts.erase(remaining.verts.begin()+best_vidx_start,remaining.verts.begin()+best_vidx_start+*best_iit);
  202. normals.erase(normals.begin() + dist);
  203. RecursiveMergeBoundaries(temp,remaining,out,normals,nor_boundary);
  204. final_result.Append(temp);
  205. }
  206. else final_result.Append(out);
  207. }
  208. // ------------------------------------------------------------------------------------------------
  209. void MergePolygonBoundaries(TempMesh& result, const TempMesh& inmesh, size_t master_bounds = -1)
  210. {
  211. // standard case - only one boundary, just copy it to the result vector
  212. if (inmesh.vertcnt.size() <= 1) {
  213. result.Append(inmesh);
  214. return;
  215. }
  216. result.vertcnt.reserve(inmesh.vertcnt.size()+result.vertcnt.size());
  217. // XXX get rid of the extra copy if possible
  218. TempMesh meshout = inmesh;
  219. // handle polygons with holes. Our built in triangulation won't handle them as is, but
  220. // the ear cutting algorithm is solid enough to deal with them if we join the inner
  221. // holes with the outer boundaries by dummy connections.
  222. IFCImporter::LogDebug("fixing polygon with holes for triangulation via ear-cutting");
  223. std::vector<unsigned int>::iterator outer_polygon = meshout.vertcnt.end(), begin=meshout.vertcnt.begin(), end=outer_polygon, iit;
  224. // each hole results in two extra vertices
  225. result.verts.reserve(meshout.verts.size()+meshout.vertcnt.size()*2+result.verts.size());
  226. size_t outer_polygon_start = 0;
  227. // do not normalize 'normals', we need the original length for computing the polygon area
  228. std::vector<aiVector3D> normals;
  229. ComputePolygonNormals(meshout,normals,false);
  230. // see if one of the polygons is a IfcFaceOuterBound (in which case `master_bounds` is its index).
  231. // sadly we can't rely on it, the docs say 'At most one of the bounds shall be of the type IfcFaceOuterBound'
  232. float area_outer_polygon = 1e-10f;
  233. if (master_bounds != (size_t)-1) {
  234. outer_polygon = begin + master_bounds;
  235. outer_polygon_start = std::accumulate(begin,outer_polygon,0);
  236. area_outer_polygon = normals[master_bounds].SquareLength();
  237. }
  238. else {
  239. size_t vidx = 0;
  240. for(iit = begin; iit != meshout.vertcnt.end(); vidx += *iit++) {
  241. // find the polygon with the largest area, it must be the outer bound.
  242. aiVector3D& n = normals[std::distance(begin,iit)];
  243. const float area = n.SquareLength();
  244. if (area > area_outer_polygon) {
  245. area_outer_polygon = area;
  246. outer_polygon = iit;
  247. outer_polygon_start = vidx;
  248. }
  249. }
  250. }
  251. ai_assert(outer_polygon != meshout.vertcnt.end());
  252. std::vector<aiVector3D>& in = meshout.verts;
  253. // skip over extremely small boundaries - this is a workaround to fix cases
  254. // in which the number of holes is so extremely large that the
  255. // triangulation code fails.
  256. #define IFC_VERTICAL_HOLE_SIZE_THRESHOLD 0.000001f
  257. size_t vidx = 0, removed = 0, index = 0;
  258. const float threshold = area_outer_polygon * IFC_VERTICAL_HOLE_SIZE_THRESHOLD;
  259. for(iit = begin; iit != end ;++index) {
  260. const float sqlen = normals[index].SquareLength();
  261. if (sqlen < threshold) {
  262. std::vector<aiVector3D>::iterator inbase = in.begin()+vidx;
  263. in.erase(inbase,inbase+*iit);
  264. outer_polygon_start -= outer_polygon_start>vidx ? *iit : 0;
  265. *iit++ = 0;
  266. ++removed;
  267. IFCImporter::LogDebug("skip small hole below threshold");
  268. }
  269. else {
  270. normals[index] /= sqrt(sqlen);
  271. vidx += *iit++;
  272. }
  273. }
  274. // see if one or more of the hole has a face that lies directly on an outer bound.
  275. // this happens for doors, for example.
  276. vidx = 0;
  277. for(iit = begin; ; vidx += *iit++) {
  278. next_loop:
  279. if (iit == end) {
  280. break;
  281. }
  282. if (iit == outer_polygon) {
  283. continue;
  284. }
  285. for(size_t vofs = 0; vofs < *iit; ++vofs) {
  286. if (!*iit) {
  287. continue;
  288. }
  289. const size_t next = (vofs+1)%*iit;
  290. const aiVector3D& v = in[vidx+vofs], &vnext = in[vidx+next],&vd = (vnext-v).Normalize();
  291. for(size_t outer = 0; outer < *outer_polygon; ++outer) {
  292. const aiVector3D& o = in[outer_polygon_start+outer], &onext = in[outer_polygon_start+(outer+1)%*outer_polygon], &od = (onext-o).Normalize();
  293. if (fabs(vd * od) > 1.f-1e-6f && (onext-v).Normalize() * vd > 1.f-1e-6f && (onext-v)*(o-v) < 0) {
  294. IFCImporter::LogDebug("got an inner hole that lies partly on the outer polygonal boundary, merging them to a single contour");
  295. // in between outer and outer+1 insert all vertices of this loop, then drop the original altogether.
  296. std::vector<aiVector3D> tmp(*iit);
  297. const size_t start = (v-o).SquareLength() > (vnext-o).SquareLength() ? vofs : next;
  298. std::vector<aiVector3D>::iterator inbase = in.begin()+vidx, it = std::copy(inbase+start, inbase+*iit,tmp.begin());
  299. std::copy(inbase, inbase+start,it);
  300. std::reverse(tmp.begin(),tmp.end());
  301. in.insert(in.begin()+outer_polygon_start+(outer+1)%*outer_polygon,tmp.begin(),tmp.end());
  302. vidx += outer_polygon_start<vidx ? *iit : 0;
  303. inbase = in.begin()+vidx;
  304. in.erase(inbase,inbase+*iit);
  305. outer_polygon_start -= outer_polygon_start>vidx ? *iit : 0;
  306. *outer_polygon += tmp.size();
  307. *iit++ = 0;
  308. ++removed;
  309. goto next_loop;
  310. }
  311. }
  312. }
  313. }
  314. if ( meshout.vertcnt.size() - removed <= 1) {
  315. result.Append(meshout);
  316. return;
  317. }
  318. // extract the outer boundary and move it to a separate mesh
  319. TempMesh boundary;
  320. boundary.vertcnt.resize(1,*outer_polygon);
  321. boundary.verts.resize(*outer_polygon);
  322. std::vector<aiVector3D>::iterator b = in.begin()+outer_polygon_start;
  323. std::copy(b,b+*outer_polygon,boundary.verts.begin());
  324. in.erase(b,b+*outer_polygon);
  325. std::vector<aiVector3D>::iterator norit = normals.begin()+std::distance(meshout.vertcnt.begin(),outer_polygon);
  326. const aiVector3D nor_boundary = *norit;
  327. normals.erase(norit);
  328. meshout.vertcnt.erase(outer_polygon);
  329. // keep merging the closest inner boundary with the outer boundary until no more boundaries are left
  330. RecursiveMergeBoundaries(result,meshout,boundary,normals,nor_boundary);
  331. }
  332. // ------------------------------------------------------------------------------------------------
  333. void ProcessConnectedFaceSet(const IfcConnectedFaceSet& fset, TempMesh& result, ConversionData& conv)
  334. {
  335. BOOST_FOREACH(const IfcFace& face, fset.CfsFaces) {
  336. // size_t ob = -1, cnt = 0;
  337. TempMesh meshout;
  338. BOOST_FOREACH(const IfcFaceBound& bound, face.Bounds) {
  339. // XXX implement proper merging for polygonal loops
  340. if(const IfcPolyLoop* const polyloop = bound.Bound->ToPtr<IfcPolyLoop>()) {
  341. if(ProcessPolyloop(*polyloop, meshout,conv)) {
  342. //if(bound.ToPtr<IfcFaceOuterBound>()) {
  343. // ob = cnt;
  344. //}
  345. //++cnt;
  346. }
  347. }
  348. else {
  349. IFCImporter::LogWarn("skipping unknown IfcFaceBound entity, type is " + bound.Bound->GetClassName());
  350. continue;
  351. }
  352. /*if(!IsTrue(bound.Orientation)) {
  353. size_t c = 0;
  354. BOOST_FOREACH(unsigned int& c, meshout.vertcnt) {
  355. std::reverse(result.verts.begin() + cnt,result.verts.begin() + cnt + c);
  356. cnt += c;
  357. }
  358. }*/
  359. }
  360. MergePolygonBoundaries(result,meshout);
  361. }
  362. }
  363. // ------------------------------------------------------------------------------------------------
  364. void ProcessRevolvedAreaSolid(const IfcRevolvedAreaSolid& solid, TempMesh& result, ConversionData& conv)
  365. {
  366. TempMesh meshout;
  367. // first read the profile description
  368. if(!ProcessProfile(*solid.SweptArea,meshout,conv) || meshout.verts.size()<=1) {
  369. return;
  370. }
  371. aiVector3D axis, pos;
  372. ConvertAxisPlacement(axis,pos,solid.Axis);
  373. aiMatrix4x4 tb0,tb1;
  374. aiMatrix4x4::Translation(pos,tb0);
  375. aiMatrix4x4::Translation(-pos,tb1);
  376. const std::vector<aiVector3D>& in = meshout.verts;
  377. const size_t size=in.size();
  378. bool has_area = solid.SweptArea->ProfileType == "AREA" && size>2;
  379. const float max_angle = solid.Angle*conv.angle_scale;
  380. if(fabs(max_angle) < 1e-3) {
  381. if(has_area) {
  382. result = meshout;
  383. }
  384. return;
  385. }
  386. const unsigned int cnt_segments = std::max(2u,static_cast<unsigned int>(16 * fabs(max_angle)/AI_MATH_HALF_PI_F));
  387. const float delta = max_angle/cnt_segments;
  388. has_area = has_area && fabs(max_angle) < AI_MATH_TWO_PI_F*0.99;
  389. result.verts.reserve(size*((cnt_segments+1)*4+(has_area?2:0)));
  390. result.vertcnt.reserve(size*cnt_segments+2);
  391. aiMatrix4x4 rot;
  392. rot = tb0 * aiMatrix4x4::Rotation(delta,axis,rot) * tb1;
  393. size_t base = 0;
  394. std::vector<aiVector3D>& out = result.verts;
  395. // dummy data to simplify later processing
  396. for(size_t i = 0; i < size; ++i) {
  397. out.insert(out.end(),4,in[i]);
  398. }
  399. for(unsigned int seg = 0; seg < cnt_segments; ++seg) {
  400. for(size_t i = 0; i < size; ++i) {
  401. const size_t next = (i+1)%size;
  402. result.vertcnt.push_back(4);
  403. const aiVector3D& base_0 = out[base+i*4+3],base_1 = out[base+next*4+3];
  404. out.push_back(base_0);
  405. out.push_back(base_1);
  406. out.push_back(rot*base_1);
  407. out.push_back(rot*base_0);
  408. }
  409. base += size*4;
  410. }
  411. out.erase(out.begin(),out.begin()+size*4);
  412. if(has_area) {
  413. // leave the triangulation of the profile area to the ear cutting
  414. // implementation in aiProcess_Triangulate - for now we just
  415. // feed in two huge polygons.
  416. base -= size*8;
  417. for(size_t i = size; i--; ) {
  418. out.push_back(out[base+i*4+3]);
  419. }
  420. for(size_t i = 0; i < size; ++i ) {
  421. out.push_back(out[i*4]);
  422. }
  423. result.vertcnt.push_back(size);
  424. result.vertcnt.push_back(size);
  425. }
  426. aiMatrix4x4 trafo;
  427. ConvertAxisPlacement(trafo, solid.Position);
  428. result.Transform(trafo);
  429. IFCImporter::LogDebug("generate mesh procedurally by radial extrusion (IfcRevolvedAreaSolid)");
  430. }
  431. // ------------------------------------------------------------------------------------------------
  432. aiMatrix3x3 DerivePlaneCoordinateSpace(const TempMesh& curmesh) {
  433. const std::vector<aiVector3D>& out = curmesh.verts;
  434. aiMatrix3x3 m;
  435. const size_t s = out.size();
  436. assert(curmesh.vertcnt.size() == 1 && curmesh.vertcnt.back() == s);
  437. const aiVector3D any_point = out[s-1];
  438. aiVector3D nor;
  439. // The input polygon is arbitrarily shaped, so we might need some tries
  440. // until we find a suitable normal (and it does not even need to be
  441. // right in all cases, Newell's algorithm would be the correct one ... ).
  442. size_t base = s-curmesh.vertcnt.back(), t = base, i, j;
  443. for (i = base; i < s-1; ++i) {
  444. for (j = i+1; j < s; ++j) {
  445. nor = ((out[i]-any_point)^(out[j]-any_point));
  446. if(fabs(nor.Length()) > 1e-8f) {
  447. goto out;
  448. }
  449. }
  450. }
  451. assert(0);
  452. out:
  453. nor.Normalize();
  454. aiVector3D r = (out[i]-any_point);
  455. r.Normalize();
  456. // reconstruct orthonormal basis
  457. aiVector3D u = r ^ nor;
  458. u.Normalize();
  459. m.a1 = r.x;
  460. m.a2 = r.y;
  461. m.a3 = r.z;
  462. m.b1 = u.x;
  463. m.b2 = u.y;
  464. m.b3 = u.z;
  465. m.c1 = nor.x;
  466. m.c2 = nor.y;
  467. m.c3 = nor.z;
  468. return m;
  469. }
  470. // ------------------------------------------------------------------------------------------------
  471. bool TryAddOpenings_Poly2Tri(const std::vector<TempOpening>& openings,const std::vector<aiVector3D>& nors, TempMesh& curmesh)
  472. {
  473. std::vector<aiVector3D>& out = curmesh.verts;
  474. bool result = false;
  475. // Try to derive a solid base plane within the current surface for use as
  476. // working coordinate system.
  477. const aiMatrix3x3& m = DerivePlaneCoordinateSpace(curmesh);
  478. const aiMatrix3x3 minv = aiMatrix3x3(m).Inverse();
  479. const aiVector3D& nor = aiVector3D(m.c1, m.c2, m.c3);
  480. float coord = -1;
  481. std::vector<aiVector2D> contour_flat;
  482. contour_flat.reserve(out.size());
  483. aiVector2D vmin, vmax;
  484. MinMaxChooser<aiVector2D>()(vmin, vmax);
  485. // Move all points into the new coordinate system, collecting min/max verts on the way
  486. BOOST_FOREACH(aiVector3D& x, out) {
  487. const aiVector3D vv = m * x;
  488. // keep Z offset in the plane coordinate system. Ignoring precision issues
  489. // (which are present, of course), this should be the same value for
  490. // all polygon vertices (assuming the polygon is planar).
  491. // XXX this should be guarded, but we somehow need to pick a suitable
  492. // epsilon
  493. // if(coord != -1.0f) {
  494. // assert(fabs(coord - vv.z) < 1e-3f);
  495. // }
  496. coord = vv.z;
  497. vmin = std::min(aiVector2D(vv.x, vv.y), vmin);
  498. vmax = std::max(aiVector2D(vv.x, vv.y), vmax);
  499. contour_flat.push_back(aiVector2D(vv.x,vv.y));
  500. }
  501. // With the current code in DerivePlaneCoordinateSpace,
  502. // vmin,vmax should always be the 0...1 rectangle (+- numeric inaccuracies)
  503. // but here we won't rely on this.
  504. vmax -= vmin;
  505. // If this happens then the projection must have been wrong.
  506. assert(vmax.Length());
  507. using ClipperLib::ulong64;
  508. // XXX use full -+ range ...
  509. const ClipperLib::long64 max_ulong64 = 1518500249; // clipper.cpp / hiRange var
  510. //#define to_int64(p) (static_cast<ulong64>( std::max( 0., std::min( static_cast<double>((p)), 1.) ) * max_ulong64 ))
  511. #define to_int64(p) (static_cast<ulong64>(static_cast<double>((p) ) * max_ulong64 ))
  512. #define from_int64(p) (static_cast<double>((p)) / max_ulong64)
  513. #define from_int64_f(p) (static_cast<float>(from_int64((p))))
  514. ClipperLib::ExPolygons clipped;
  515. ClipperLib::Polygons holes_union;
  516. aiVector3D wall_extrusion;
  517. bool do_connections = false, first = true;
  518. try {
  519. ClipperLib::Clipper clipper_holes;
  520. size_t c = 0;
  521. BOOST_FOREACH(const TempOpening& t,openings) {
  522. const aiVector3D& outernor = nors[c++];
  523. const float dot = nor * outernor;
  524. if (fabs(dot)<1.f-1e-6f) {
  525. continue;
  526. }
  527. const std::vector<aiVector3D>& va = t.profileMesh->verts;
  528. if(va.size() <= 2) {
  529. continue;
  530. }
  531. std::vector<aiVector2D> contour;
  532. BOOST_FOREACH(const aiVector3D& xx, t.profileMesh->verts) {
  533. aiVector3D vv = m * xx, vv_extr = m * (xx + t.extrusionDir);
  534. const bool is_extruded_side = fabs(vv.z - coord) > fabs(vv_extr.z - coord);
  535. if (first) {
  536. first = false;
  537. if (dot > 0.f) {
  538. do_connections = true;
  539. wall_extrusion = t.extrusionDir;
  540. if (is_extruded_side) {
  541. wall_extrusion = - wall_extrusion;
  542. }
  543. }
  544. }
  545. // XXX should not be necessary - but it is. Why? For precision reasons?
  546. vv = is_extruded_side ? vv_extr : vv;
  547. contour.push_back(aiVector2D(vv.x,vv.y));
  548. }
  549. ClipperLib::Polygon hole;
  550. BOOST_FOREACH(aiVector2D& pip, contour) {
  551. pip.x = (pip.x - vmin.x) / vmax.x;
  552. pip.y = (pip.y - vmin.y) / vmax.y;
  553. hole.push_back(ClipperLib::IntPoint( to_int64(pip.x), to_int64(pip.y) ));
  554. }
  555. if (!ClipperLib::Orientation(hole)) {
  556. std::reverse(hole.begin(), hole.end());
  557. // assert(ClipperLib::Orientation(hole));
  558. }
  559. clipper_holes.AddPolygon(hole,ClipperLib::ptSubject);
  560. }
  561. clipper_holes.Execute(ClipperLib::ctUnion,holes_union,
  562. ClipperLib::pftNonZero,
  563. ClipperLib::pftNonZero);
  564. if (holes_union.empty()) {
  565. return false;
  566. }
  567. // Now that we have the big union of all holes, subtract it from the outer contour
  568. // to obtain the final polygon to feed into the triangulator.
  569. {
  570. ClipperLib::Polygon poly;
  571. BOOST_FOREACH(aiVector2D& pip, contour_flat) {
  572. pip.x = (pip.x - vmin.x) / vmax.x;
  573. pip.y = (pip.y - vmin.y) / vmax.y;
  574. poly.push_back(ClipperLib::IntPoint( to_int64(pip.x), to_int64(pip.y) ));
  575. }
  576. if (ClipperLib::Orientation(poly)) {
  577. std::reverse(poly.begin(), poly.end());
  578. }
  579. clipper_holes.Clear();
  580. clipper_holes.AddPolygon(poly,ClipperLib::ptSubject);
  581. clipper_holes.AddPolygons(holes_union,ClipperLib::ptClip);
  582. clipper_holes.Execute(ClipperLib::ctDifference,clipped,
  583. ClipperLib::pftNonZero,
  584. ClipperLib::pftNonZero);
  585. }
  586. }
  587. catch (const char* sx) {
  588. IFCImporter::LogError("Ifc: error during polygon clipping, skipping openings for this face: (Clipper: "
  589. + std::string(sx) + ")");
  590. return false;
  591. }
  592. std::vector<aiVector3D> old_verts;
  593. std::vector<unsigned int> old_vertcnt;
  594. old_verts.swap(curmesh.verts);
  595. old_vertcnt.swap(curmesh.vertcnt);
  596. // add connection geometry to close the adjacent 'holes' for the openings
  597. // this should only be done from one side of the wall or the polygons
  598. // would be emitted twice.
  599. if (do_connections) {
  600. std::vector<aiVector3D> tmpvec;
  601. BOOST_FOREACH(ClipperLib::Polygon& opening, holes_union) {
  602. assert(ClipperLib::Orientation(opening));
  603. tmpvec.clear();
  604. BOOST_FOREACH(ClipperLib::IntPoint& point, opening) {
  605. tmpvec.push_back( minv * aiVector3D(
  606. vmin.x + from_int64_f(point.X) * vmax.x,
  607. vmin.y + from_int64_f(point.Y) * vmax.y,
  608. coord));
  609. }
  610. for(size_t i = 0, size = tmpvec.size(); i < size; ++i) {
  611. const size_t next = (i+1)%size;
  612. curmesh.vertcnt.push_back(4);
  613. const aiVector3D& in_world = tmpvec[i];
  614. const aiVector3D& next_world = tmpvec[next];
  615. // Assumptions: no 'partial' openings, wall thickness roughly the same across the wall
  616. curmesh.verts.push_back(in_world);
  617. curmesh.verts.push_back(in_world+wall_extrusion);
  618. curmesh.verts.push_back(next_world+wall_extrusion);
  619. curmesh.verts.push_back(next_world);
  620. }
  621. }
  622. }
  623. std::vector< std::vector<p2t::Point*> > contours;
  624. BOOST_FOREACH(ClipperLib::ExPolygon& clip, clipped) {
  625. contours.clear();
  626. // Build the outer polygon contour line for feeding into poly2tri
  627. std::vector<p2t::Point*> contour_points;
  628. BOOST_FOREACH(ClipperLib::IntPoint& point, clip.outer) {
  629. contour_points.push_back( new p2t::Point(from_int64(point.X), from_int64(point.Y)) );
  630. }
  631. p2t::CDT* cdt ;
  632. try {
  633. // Note: this relies on custom modifications in poly2tri to raise runtime_error's
  634. // instead if assertions. These failures are not debug only, they can actually
  635. // happen in production use if the input data is broken. An assertion would be
  636. // inappropriate.
  637. cdt = new p2t::CDT(contour_points);
  638. }
  639. catch(const std::exception& e) {
  640. IFCImporter::LogError("Ifc: error during polygon triangulation, skipping some openings: (poly2tri: "
  641. + std::string(e.what()) + ")");
  642. continue;
  643. }
  644. // Build the poly2tri inner contours for all holes we got from ClipperLib
  645. BOOST_FOREACH(ClipperLib::Polygon& opening, clip.holes) {
  646. contours.push_back(std::vector<p2t::Point*>());
  647. std::vector<p2t::Point*>& contour = contours.back();
  648. BOOST_FOREACH(ClipperLib::IntPoint& point, opening) {
  649. contour.push_back( new p2t::Point(from_int64(point.X), from_int64(point.Y)) );
  650. }
  651. cdt->AddHole(contour);
  652. }
  653. try {
  654. // Note: See above
  655. cdt->Triangulate();
  656. }
  657. catch(const std::exception& e) {
  658. IFCImporter::LogError("Ifc: error during polygon triangulation, skipping some openings: (poly2tri: "
  659. + std::string(e.what()) + ")");
  660. continue;
  661. }
  662. const std::vector<p2t::Triangle*>& tris = cdt->GetTriangles();
  663. // Collect the triangles we just produced
  664. BOOST_FOREACH(p2t::Triangle* tri, tris) {
  665. for(int i = 0; i < 3; ++i) {
  666. const aiVector2D& v = aiVector2D(
  667. static_cast<float>( tri->GetPoint(i)->x ),
  668. static_cast<float>( tri->GetPoint(i)->y )
  669. );
  670. assert(v.x <= 1.0 && v.x >= 0.0 && v.y <= 1.0 && v.y >= 0.0);
  671. const aiVector3D v3 = minv * aiVector3D(vmin.x + v.x * vmax.x, vmin.y + v.y * vmax.y,coord) ;
  672. curmesh.verts.push_back(v3);
  673. }
  674. curmesh.vertcnt.push_back(3);
  675. }
  676. result = true;
  677. }
  678. #undef to_int64
  679. #undef from_int64
  680. #undef from_int64_f
  681. if (!result) {
  682. // revert -- it's a shame, but better than nothing
  683. curmesh.verts.insert(curmesh.verts.end(),old_verts.begin(), old_verts.end());
  684. curmesh.vertcnt.insert(curmesh.vertcnt.end(),old_vertcnt.begin(), old_vertcnt.end());
  685. IFCImporter::LogError("Ifc: revert, could not generate openings for this wall");
  686. }
  687. return result;
  688. }
  689. // ------------------------------------------------------------------------------------------------
  690. void ProcessExtrudedAreaSolid(const IfcExtrudedAreaSolid& solid, TempMesh& result, ConversionData& conv)
  691. {
  692. TempMesh meshout;
  693. // First read the profile description
  694. if(!ProcessProfile(*solid.SweptArea,meshout,conv) || meshout.verts.size()<=1) {
  695. return;
  696. }
  697. aiVector3D dir;
  698. ConvertDirection(dir,solid.ExtrudedDirection);
  699. dir *= solid.Depth;
  700. // Outline: assuming that `meshout.verts` is now a list of vertex points forming
  701. // the underlying profile, extrude along the given axis, forming new
  702. // triangles.
  703. std::vector<aiVector3D>& in = meshout.verts;
  704. const size_t size=in.size();
  705. const bool has_area = solid.SweptArea->ProfileType == "AREA" && size>2;
  706. if(solid.Depth < 1e-3) {
  707. if(has_area) {
  708. meshout = result;
  709. }
  710. return;
  711. }
  712. result.verts.reserve(size*(has_area?4:2));
  713. result.vertcnt.reserve(meshout.vertcnt.size()+2);
  714. // First step: transform all vertices into the target coordinate space
  715. aiMatrix4x4 trafo;
  716. ConvertAxisPlacement(trafo, solid.Position);
  717. BOOST_FOREACH(aiVector3D& v,in) {
  718. v *= trafo;
  719. }
  720. aiVector3D min = in[0];
  721. dir *= aiMatrix3x3(trafo);
  722. std::vector<aiVector3D> nors;
  723. const bool openings = !!conv.apply_openings && conv.apply_openings->size();
  724. // Compute the normal vectors for all opening polygons as a prerequisite
  725. // to TryAddOpenings_Poly2Tri()
  726. if (openings) {
  727. nors.reserve(conv.apply_openings->size());
  728. BOOST_FOREACH(TempOpening& t,*conv.apply_openings) {
  729. TempMesh& bounds = *t.profileMesh.get();
  730. if (bounds.verts.size() <= 2) {
  731. nors.push_back(aiVector3D());
  732. continue;
  733. }
  734. nors.push_back(((bounds.verts[2]-bounds.verts[0])^(bounds.verts[1]-bounds.verts[0]) ).Normalize());
  735. }
  736. }
  737. TempMesh temp;
  738. TempMesh& curmesh = openings ? temp : result;
  739. std::vector<aiVector3D>& out = curmesh.verts;
  740. size_t sides_with_openings = 0;
  741. for(size_t i = 0; i < size; ++i) {
  742. const size_t next = (i+1)%size;
  743. curmesh.vertcnt.push_back(4);
  744. out.push_back(in[i]);
  745. out.push_back(in[i]+dir);
  746. out.push_back(in[next]+dir);
  747. out.push_back(in[next]);
  748. if(openings) {
  749. if(TryAddOpenings_Poly2Tri(*conv.apply_openings,nors,temp)) {
  750. ++sides_with_openings;
  751. }
  752. result.Append(temp);
  753. temp.Clear();
  754. }
  755. }
  756. size_t sides_with_v_openings = 0;
  757. if(has_area) {
  758. for(size_t n = 0; n < 2; ++n) {
  759. for(size_t i = size; i--; ) {
  760. out.push_back(in[i]+(n?dir:aiVector3D()));
  761. }
  762. curmesh.vertcnt.push_back(size);
  763. if(openings && size > 2) {
  764. if(TryAddOpenings_Poly2Tri(*conv.apply_openings,nors,temp)) {
  765. ++sides_with_v_openings;
  766. }
  767. result.Append(temp);
  768. temp.Clear();
  769. }
  770. }
  771. }
  772. if(openings && ((sides_with_openings != 2 && sides_with_openings) || (sides_with_v_openings != 2 && sides_with_v_openings))) {
  773. IFCImporter::LogWarn("failed to resolve all openings, presumably their topology is not supported by Assimp");
  774. }
  775. IFCImporter::LogDebug("generate mesh procedurally by extrusion (IfcExtrudedAreaSolid)");
  776. }
  777. // ------------------------------------------------------------------------------------------------
  778. void ProcessSweptAreaSolid(const IfcSweptAreaSolid& swept, TempMesh& meshout, ConversionData& conv)
  779. {
  780. if(const IfcExtrudedAreaSolid* const solid = swept.ToPtr<IfcExtrudedAreaSolid>()) {
  781. // Do we just collect openings for a parent element (i.e. a wall)?
  782. // In this case we don't extrude the surface yet, just keep the profile and transform it correctly
  783. if(conv.collect_openings) {
  784. boost::shared_ptr<TempMesh> meshtmp(new TempMesh());
  785. ProcessProfile(swept.SweptArea,*meshtmp,conv);
  786. aiMatrix4x4 m;
  787. ConvertAxisPlacement(m,solid->Position);
  788. meshtmp->Transform(m);
  789. aiVector3D dir;
  790. ConvertDirection(dir,solid->ExtrudedDirection);
  791. conv.collect_openings->push_back(TempOpening(solid, aiMatrix3x3(m) * (dir*solid->Depth),meshtmp));
  792. return;
  793. }
  794. ProcessExtrudedAreaSolid(*solid,meshout,conv);
  795. }
  796. else if(const IfcRevolvedAreaSolid* const rev = swept.ToPtr<IfcRevolvedAreaSolid>()) {
  797. ProcessRevolvedAreaSolid(*rev,meshout,conv);
  798. }
  799. else {
  800. IFCImporter::LogWarn("skipping unknown IfcSweptAreaSolid entity, type is " + swept.GetClassName());
  801. }
  802. }
  803. // ------------------------------------------------------------------------------------------------
  804. enum Intersect {
  805. Intersect_No,
  806. Intersect_LiesOnPlane,
  807. Intersect_Yes
  808. };
  809. // ------------------------------------------------------------------------------------------------
  810. Intersect IntersectSegmentPlane(const aiVector3D& p,const aiVector3D& n, const aiVector3D& e0, const aiVector3D& e1, aiVector3D& out)
  811. {
  812. const aiVector3D pdelta = e0 - p, seg = e1-e0;
  813. const float dotOne = n*seg, dotTwo = -(n*pdelta);
  814. if (fabs(dotOne) < 1e-6) {
  815. return fabs(dotTwo) < 1e-6f ? Intersect_LiesOnPlane : Intersect_No;
  816. }
  817. const float t = dotTwo/dotOne;
  818. // t must be in [0..1] if the intersection point is within the given segment
  819. if (t > 1.f || t < 0.f) {
  820. return Intersect_No;
  821. }
  822. out = e0+t*seg;
  823. return Intersect_Yes;
  824. }
  825. // ------------------------------------------------------------------------------------------------
  826. void ProcessBoolean(const IfcBooleanResult& boolean, TempMesh& result, ConversionData& conv)
  827. {
  828. if(const IfcBooleanResult* const clip = boolean.ToPtr<IfcBooleanResult>()) {
  829. if(clip->Operator != "DIFFERENCE") {
  830. IFCImporter::LogWarn("encountered unsupported boolean operator: " + (std::string)clip->Operator);
  831. return;
  832. }
  833. TempMesh meshout;
  834. const IfcHalfSpaceSolid* const hs = clip->SecondOperand->ResolveSelectPtr<IfcHalfSpaceSolid>(conv.db);
  835. if(!hs) {
  836. IFCImporter::LogError("expected IfcHalfSpaceSolid as second clipping operand");
  837. return;
  838. }
  839. const IfcPlane* const plane = hs->BaseSurface->ToPtr<IfcPlane>();
  840. if(!plane) {
  841. IFCImporter::LogError("expected IfcPlane as base surface for the IfcHalfSpaceSolid");
  842. return;
  843. }
  844. if(const IfcBooleanResult* const op0 = clip->FirstOperand->ResolveSelectPtr<IfcBooleanResult>(conv.db)) {
  845. ProcessBoolean(*op0,meshout,conv);
  846. }
  847. else if (const IfcSweptAreaSolid* const swept = clip->FirstOperand->ResolveSelectPtr<IfcSweptAreaSolid>(conv.db)) {
  848. ProcessSweptAreaSolid(*swept,meshout,conv);
  849. }
  850. else {
  851. IFCImporter::LogError("expected IfcSweptAreaSolid or IfcBooleanResult as first clipping operand");
  852. return;
  853. }
  854. // extract plane base position vector and normal vector
  855. aiVector3D p,n(0.f,0.f,1.f);
  856. if (plane->Position->Axis) {
  857. ConvertDirection(n,plane->Position->Axis.Get());
  858. }
  859. ConvertCartesianPoint(p,plane->Position->Location);
  860. if(!IsTrue(hs->AgreementFlag)) {
  861. n *= -1.f;
  862. }
  863. // clip the current contents of `meshout` against the plane we obtained from the second operand
  864. const std::vector<aiVector3D>& in = meshout.verts;
  865. std::vector<aiVector3D>& outvert = result.verts;
  866. std::vector<unsigned int>::const_iterator begin=meshout.vertcnt.begin(), end=meshout.vertcnt.end(), iit;
  867. outvert.reserve(in.size());
  868. result.vertcnt.reserve(meshout.vertcnt.size());
  869. unsigned int vidx = 0;
  870. for(iit = begin; iit != end; vidx += *iit++) {
  871. unsigned int newcount = 0;
  872. for(unsigned int i = 0; i < *iit; ++i) {
  873. const aiVector3D& e0 = in[vidx+i], e1 = in[vidx+(i+1)%*iit];
  874. // does the next segment intersect the plane?
  875. aiVector3D isectpos;
  876. const Intersect isect = IntersectSegmentPlane(p,n,e0,e1,isectpos);
  877. if (isect == Intersect_No || isect == Intersect_LiesOnPlane) {
  878. if ( (e0-p).Normalize()*n > 0 ) {
  879. outvert.push_back(e0);
  880. ++newcount;
  881. }
  882. }
  883. else if (isect == Intersect_Yes) {
  884. if ( (e0-p).Normalize()*n > 0 ) {
  885. // e0 is on the right side, so keep it
  886. outvert.push_back(e0);
  887. outvert.push_back(isectpos);
  888. newcount += 2;
  889. }
  890. else {
  891. // e0 is on the wrong side, so drop it and keep e1 instead
  892. outvert.push_back(isectpos);
  893. ++newcount;
  894. }
  895. }
  896. }
  897. if (!newcount) {
  898. continue;
  899. }
  900. aiVector3D vmin,vmax;
  901. ArrayBounds(&*(outvert.end()-newcount),newcount,vmin,vmax);
  902. // filter our double points - those may happen if a point lies
  903. // directly on the intersection line. However, due to float
  904. // precision a bitwise comparison is not feasible to detect
  905. // this case.
  906. const float epsilon = (vmax-vmin).SquareLength() / 1e6f;
  907. FuzzyVectorCompare fz(epsilon);
  908. std::vector<aiVector3D>::iterator e = std::unique( outvert.end()-newcount, outvert.end(), fz );
  909. if (e != outvert.end()) {
  910. newcount -= static_cast<unsigned int>(std::distance(e,outvert.end()));
  911. outvert.erase(e,outvert.end());
  912. }
  913. if (fz(*( outvert.end()-newcount),outvert.back())) {
  914. outvert.pop_back();
  915. --newcount;
  916. }
  917. if(newcount > 2) {
  918. result.vertcnt.push_back(newcount);
  919. }
  920. else while(newcount-->0)result.verts.pop_back();
  921. }
  922. IFCImporter::LogDebug("generating CSG geometry by plane clipping (IfcBooleanClippingResult)");
  923. }
  924. else {
  925. IFCImporter::LogWarn("skipping unknown IfcBooleanResult entity, type is " + boolean.GetClassName());
  926. }
  927. }
  928. // ------------------------------------------------------------------------------------------------
  929. bool ProcessGeometricItem(const IfcRepresentationItem& geo, std::vector<unsigned int>& mesh_indices, ConversionData& conv)
  930. {
  931. TempMesh meshtmp;
  932. if(const IfcShellBasedSurfaceModel* shellmod = geo.ToPtr<IfcShellBasedSurfaceModel>()) {
  933. BOOST_FOREACH(boost::shared_ptr<const IfcShell> shell,shellmod->SbsmBoundary) {
  934. try {
  935. const EXPRESS::ENTITY& e = shell->To<ENTITY>();
  936. const IfcConnectedFaceSet& fs = conv.db.MustGetObject(e).To<IfcConnectedFaceSet>();
  937. ProcessConnectedFaceSet(fs,meshtmp,conv);
  938. }
  939. catch(std::bad_cast&) {
  940. IFCImporter::LogWarn("unexpected type error, IfcShell ought to inherit from IfcConnectedFaceSet");
  941. }
  942. }
  943. }
  944. else if(const IfcConnectedFaceSet* fset = geo.ToPtr<IfcConnectedFaceSet>()) {
  945. ProcessConnectedFaceSet(*fset,meshtmp,conv);
  946. }
  947. else if(const IfcSweptAreaSolid* swept = geo.ToPtr<IfcSweptAreaSolid>()) {
  948. ProcessSweptAreaSolid(*swept,meshtmp,conv);
  949. }
  950. else if(const IfcManifoldSolidBrep* brep = geo.ToPtr<IfcManifoldSolidBrep>()) {
  951. ProcessConnectedFaceSet(brep->Outer,meshtmp,conv);
  952. }
  953. else if(const IfcFaceBasedSurfaceModel* surf = geo.ToPtr<IfcFaceBasedSurfaceModel>()) {
  954. BOOST_FOREACH(const IfcConnectedFaceSet& fc, surf->FbsmFaces) {
  955. ProcessConnectedFaceSet(fc,meshtmp,conv);
  956. }
  957. }
  958. else if(const IfcBooleanResult* boolean = geo.ToPtr<IfcBooleanResult>()) {
  959. ProcessBoolean(*boolean,meshtmp,conv);
  960. }
  961. else if(geo.ToPtr<IfcBoundingBox>()) {
  962. // silently skip over bounding boxes
  963. return false;
  964. }
  965. else {
  966. IFCImporter::LogWarn("skipping unknown IfcGeometricRepresentationItem entity, type is " + geo.GetClassName());
  967. return false;
  968. }
  969. meshtmp.RemoveAdjacentDuplicates();
  970. FixupFaceOrientation(meshtmp);
  971. aiMesh* const mesh = meshtmp.ToMesh();
  972. if(mesh) {
  973. mesh->mMaterialIndex = ProcessMaterials(geo,conv);
  974. mesh_indices.push_back(conv.meshes.size());
  975. conv.meshes.push_back(mesh);
  976. return true;
  977. }
  978. return false;
  979. }
  980. // ------------------------------------------------------------------------------------------------
  981. void AssignAddedMeshes(std::vector<unsigned int>& mesh_indices,aiNode* nd,ConversionData& /*conv*/)
  982. {
  983. if (!mesh_indices.empty()) {
  984. // make unique
  985. std::sort(mesh_indices.begin(),mesh_indices.end());
  986. std::vector<unsigned int>::iterator it_end = std::unique(mesh_indices.begin(),mesh_indices.end());
  987. const size_t size = std::distance(mesh_indices.begin(),it_end);
  988. nd->mNumMeshes = size;
  989. nd->mMeshes = new unsigned int[nd->mNumMeshes];
  990. for(unsigned int i = 0; i < nd->mNumMeshes; ++i) {
  991. nd->mMeshes[i] = mesh_indices[i];
  992. }
  993. }
  994. }
  995. // ------------------------------------------------------------------------------------------------
  996. bool TryQueryMeshCache(const IfcRepresentationItem& item, std::vector<unsigned int>& mesh_indices, ConversionData& conv)
  997. {
  998. ConversionData::MeshCache::const_iterator it = conv.cached_meshes.find(&item);
  999. if (it != conv.cached_meshes.end()) {
  1000. std::copy((*it).second.begin(),(*it).second.end(),std::back_inserter(mesh_indices));
  1001. return true;
  1002. }
  1003. return false;
  1004. }
  1005. // ------------------------------------------------------------------------------------------------
  1006. void PopulateMeshCache(const IfcRepresentationItem& item, const std::vector<unsigned int>& mesh_indices, ConversionData& conv)
  1007. {
  1008. conv.cached_meshes[&item] = mesh_indices;
  1009. }
  1010. // ------------------------------------------------------------------------------------------------
  1011. bool ProcessRepresentationItem(const IfcRepresentationItem& item, std::vector<unsigned int>& mesh_indices, ConversionData& conv)
  1012. {
  1013. if (!TryQueryMeshCache(item,mesh_indices,conv)) {
  1014. if(ProcessGeometricItem(item,mesh_indices,conv)) {
  1015. if(mesh_indices.size()) {
  1016. PopulateMeshCache(item,mesh_indices,conv);
  1017. }
  1018. }
  1019. else return false;
  1020. }
  1021. return true;
  1022. }
  1023. } // ! IFC
  1024. } // ! Assimp
  1025. #endif