IFCBoolean.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2010, 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 IFCBoolean.cpp
  34. * @brief Implements a subset of Ifc boolean operations
  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 <iterator>
  42. namespace Assimp {
  43. namespace IFC {
  44. // ------------------------------------------------------------------------------------------------
  45. enum Intersect {
  46. Intersect_No,
  47. Intersect_LiesOnPlane,
  48. Intersect_Yes
  49. };
  50. // ------------------------------------------------------------------------------------------------
  51. Intersect IntersectSegmentPlane(const IfcVector3& p,const IfcVector3& n, const IfcVector3& e0,
  52. const IfcVector3& e1,
  53. IfcVector3& out)
  54. {
  55. const IfcVector3 pdelta = e0 - p, seg = e1-e0;
  56. const IfcFloat dotOne = n*seg, dotTwo = -(n*pdelta);
  57. if (fabs(dotOne) < 1e-6) {
  58. return fabs(dotTwo) < 1e-6f ? Intersect_LiesOnPlane : Intersect_No;
  59. }
  60. const IfcFloat t = dotTwo/dotOne;
  61. // t must be in [0..1] if the intersection point is within the given segment
  62. if (t > 1.f || t < 0.f) {
  63. return Intersect_No;
  64. }
  65. out = e0+t*seg;
  66. return Intersect_Yes;
  67. }
  68. // ------------------------------------------------------------------------------------------------
  69. void ProcessBooleanHalfSpaceDifference(const IfcHalfSpaceSolid* hs, TempMesh& result,
  70. const TempMesh& first_operand,
  71. ConversionData& conv)
  72. {
  73. ai_assert(hs != NULL);
  74. const IfcPlane* const plane = hs->BaseSurface->ToPtr<IfcPlane>();
  75. if(!plane) {
  76. IFCImporter::LogError("expected IfcPlane as base surface for the IfcHalfSpaceSolid");
  77. return;
  78. }
  79. // extract plane base position vector and normal vector
  80. IfcVector3 p,n(0.f,0.f,1.f);
  81. if (plane->Position->Axis) {
  82. ConvertDirection(n,plane->Position->Axis.Get());
  83. }
  84. ConvertCartesianPoint(p,plane->Position->Location);
  85. if(!IsTrue(hs->AgreementFlag)) {
  86. n *= -1.f;
  87. }
  88. // clip the current contents of `meshout` against the plane we obtained from the second operand
  89. const std::vector<IfcVector3>& in = first_operand.verts;
  90. std::vector<IfcVector3>& outvert = result.verts;
  91. std::vector<unsigned int>::const_iterator begin = first_operand.vertcnt.begin(),
  92. end = first_operand.vertcnt.end(), iit;
  93. outvert.reserve(in.size());
  94. result.vertcnt.reserve(first_operand.vertcnt.size());
  95. unsigned int vidx = 0;
  96. for(iit = begin; iit != end; vidx += *iit++) {
  97. unsigned int newcount = 0;
  98. for(unsigned int i = 0; i < *iit; ++i) {
  99. const IfcVector3& e0 = in[vidx+i], e1 = in[vidx+(i+1)%*iit];
  100. // does the next segment intersect the plane?
  101. IfcVector3 isectpos;
  102. const Intersect isect = IntersectSegmentPlane(p,n,e0,e1,isectpos);
  103. if (isect == Intersect_No || isect == Intersect_LiesOnPlane) {
  104. if ( (e0-p).Normalize()*n > 0 ) {
  105. outvert.push_back(e0);
  106. ++newcount;
  107. }
  108. }
  109. else if (isect == Intersect_Yes) {
  110. if ( (e0-p).Normalize()*n > 0 ) {
  111. // e0 is on the right side, so keep it
  112. outvert.push_back(e0);
  113. outvert.push_back(isectpos);
  114. newcount += 2;
  115. }
  116. else {
  117. // e0 is on the wrong side, so drop it and keep e1 instead
  118. outvert.push_back(isectpos);
  119. ++newcount;
  120. }
  121. }
  122. }
  123. if (!newcount) {
  124. continue;
  125. }
  126. IfcVector3 vmin,vmax;
  127. ArrayBounds(&*(outvert.end()-newcount),newcount,vmin,vmax);
  128. // filter our IfcFloat points - those may happen if a point lies
  129. // directly on the intersection line. However, due to IfcFloat
  130. // precision a bitwise comparison is not feasible to detect
  131. // this case.
  132. const IfcFloat epsilon = (vmax-vmin).SquareLength() / 1e6f;
  133. FuzzyVectorCompare fz(epsilon);
  134. std::vector<IfcVector3>::iterator e = std::unique( outvert.end()-newcount, outvert.end(), fz );
  135. if (e != outvert.end()) {
  136. newcount -= static_cast<unsigned int>(std::distance(e,outvert.end()));
  137. outvert.erase(e,outvert.end());
  138. }
  139. if (fz(*( outvert.end()-newcount),outvert.back())) {
  140. outvert.pop_back();
  141. --newcount;
  142. }
  143. if(newcount > 2) {
  144. result.vertcnt.push_back(newcount);
  145. }
  146. else while(newcount-->0) {
  147. result.verts.pop_back();
  148. }
  149. }
  150. IFCImporter::LogDebug("generating CSG geometry by plane clipping (IfcBooleanClippingResult)");
  151. }
  152. // ------------------------------------------------------------------------------------------------
  153. // Check if e0-e1 intersects a sub-segment of the given boundary line.
  154. // note: this functions works on 3D vectors, but performs its intersection checks solely in xy.
  155. bool IntersectsBoundaryProfile( const IfcVector3& e0, const IfcVector3& e1, const std::vector<IfcVector3>& boundary,
  156. std::vector<size_t>& intersected_boundary_segments,
  157. std::vector<IfcVector3>& intersected_boundary_points,
  158. bool half_open = false,
  159. bool* e0_hits_border = NULL)
  160. {
  161. ai_assert(intersected_boundary_segments.empty());
  162. ai_assert(intersected_boundary_points.empty());
  163. if(e0_hits_border) {
  164. *e0_hits_border = false;
  165. }
  166. const IfcVector3& e = e1 - e0;
  167. for (size_t i = 0, bcount = boundary.size(); i < bcount; ++i) {
  168. // boundary segment i: b0-b1
  169. const IfcVector3& b0 = boundary[i];
  170. const IfcVector3& b1 = boundary[(i+1) % bcount];
  171. const IfcVector3& b = b1 - b0;
  172. // segment-segment intersection
  173. // solve b0 + b*s = e0 + e*t for (s,t)
  174. const IfcFloat det = (-b.x * e.y + e.x * b.y);
  175. if(fabs(det) < 1e-6) {
  176. // no solutions (parallel lines)
  177. continue;
  178. }
  179. const IfcFloat x = b0.x - e0.x;
  180. const IfcFloat y = b0.y - e0.y;
  181. const IfcFloat s = (x*e.y - e.x*y)/det;
  182. const IfcFloat t = (x*b.y - b.x*y)/det;
  183. #ifdef _DEBUG
  184. const IfcVector3 check = b0 + b*s - (e0 + e*t);
  185. ai_assert((IfcVector2(check.x,check.y)).SquareLength() < 1e-5);
  186. #endif
  187. // for a valid intersection, s-t should be in range [0,1].
  188. // note that for t (i.e. the segment point) we only use a
  189. // half-sided epsilon because the next segment should catch
  190. // this case.
  191. const IfcFloat epsilon = 1e-6;
  192. if (t >= -epsilon && (t <= 1.0+epsilon || half_open) && s >= -epsilon && s <= 1.0) {
  193. if (e0_hits_border && !*e0_hits_border) {
  194. *e0_hits_border = fabs(t) < 1e-5f;
  195. }
  196. const IfcVector3& p = e0 + e*t;
  197. // only insert the point into the list if it is sufficiently
  198. // far away from the previous intersection point. This way,
  199. // we avoid duplicate detection if the intersection is
  200. // directly on the vertex between two segments.
  201. if (!intersected_boundary_points.empty() && intersected_boundary_segments.back()==i-1 ) {
  202. const IfcVector3 diff = intersected_boundary_points.back() - p;
  203. if(IfcVector3((diff.x, diff.y)).SquareLength() < 1e-7) {
  204. continue;
  205. }
  206. }
  207. intersected_boundary_segments.push_back(i);
  208. intersected_boundary_points.push_back(p);
  209. }
  210. }
  211. return !intersected_boundary_segments.empty();
  212. }
  213. // ------------------------------------------------------------------------------------------------
  214. // note: this functions works on 3D vectors, but performs its intersection checks solely in xy.
  215. bool PointInPoly(const IfcVector3& p, const std::vector<IfcVector3>& boundary)
  216. {
  217. // even-odd algorithm: take a random vector that extends from p to infinite
  218. // and counts how many times it intersects edges of the boundary.
  219. // because checking for segment intersections is prone to numeric inaccuracies
  220. // or double detections (i.e. when hitting multiple adjacent segments at their
  221. // shared vertices) we do it thrice with different rays and vote on it.
  222. // the even-odd algorithm doesn't work for points which lie directly on
  223. // the border of the polygon. If any of our attempts produces this result,
  224. // we return false immediately.
  225. std::vector<size_t> intersected_boundary_segments;
  226. std::vector<IfcVector3> intersected_boundary_points;
  227. size_t votes = 0;
  228. bool is_border;
  229. IntersectsBoundaryProfile(p, p + IfcVector3(1.0,0,0), boundary,
  230. intersected_boundary_segments,
  231. intersected_boundary_points, true, &is_border);
  232. if(is_border) {
  233. return false;
  234. }
  235. votes += intersected_boundary_segments.size() % 2;
  236. intersected_boundary_segments.clear();
  237. intersected_boundary_points.clear();
  238. IntersectsBoundaryProfile(p, p + IfcVector3(0,1.0,0), boundary,
  239. intersected_boundary_segments,
  240. intersected_boundary_points, true, &is_border);
  241. if(is_border) {
  242. return false;
  243. }
  244. votes += intersected_boundary_segments.size() % 2;
  245. intersected_boundary_segments.clear();
  246. intersected_boundary_points.clear();
  247. IntersectsBoundaryProfile(p, p + IfcVector3(0.6,-0.6,0.0), boundary,
  248. intersected_boundary_segments,
  249. intersected_boundary_points, true, &is_border);
  250. if(is_border) {
  251. return false;
  252. }
  253. votes += intersected_boundary_segments.size() % 2;
  254. //ai_assert(votes == 3 || votes == 0);
  255. return votes > 1;
  256. }
  257. // ------------------------------------------------------------------------------------------------
  258. void ProcessPolygonalBoundedBooleanHalfSpaceDifference(const IfcPolygonalBoundedHalfSpace* hs, TempMesh& result,
  259. const TempMesh& first_operand,
  260. ConversionData& conv)
  261. {
  262. ai_assert(hs != NULL);
  263. const IfcPlane* const plane = hs->BaseSurface->ToPtr<IfcPlane>();
  264. if(!plane) {
  265. IFCImporter::LogError("expected IfcPlane as base surface for the IfcHalfSpaceSolid");
  266. return;
  267. }
  268. // extract plane base position vector and normal vector
  269. IfcVector3 p,n(0.f,0.f,1.f);
  270. if (plane->Position->Axis) {
  271. ConvertDirection(n,plane->Position->Axis.Get());
  272. }
  273. ConvertCartesianPoint(p,plane->Position->Location);
  274. if(!IsTrue(hs->AgreementFlag)) {
  275. n *= -1.f;
  276. }
  277. n.Normalize();
  278. // obtain the polygonal bounding volume
  279. boost::shared_ptr<TempMesh> profile = boost::shared_ptr<TempMesh>(new TempMesh());
  280. if(!ProcessCurve(hs->PolygonalBoundary, *profile.get(), conv)) {
  281. IFCImporter::LogError("expected valid polyline for boundary of boolean halfspace");
  282. return;
  283. }
  284. IfcMatrix4 proj_inv;
  285. ConvertAxisPlacement(proj_inv,hs->Position);
  286. // and map everything into a plane coordinate space so all intersection
  287. // tests can be done in 2D space.
  288. IfcMatrix4 proj = proj_inv;
  289. proj.Inverse();
  290. // clip the current contents of `meshout` against the plane we obtained from the second operand
  291. const std::vector<IfcVector3>& in = first_operand.verts;
  292. std::vector<IfcVector3>& outvert = result.verts;
  293. std::vector<unsigned int>::const_iterator begin = first_operand.vertcnt.begin(),
  294. end = first_operand.vertcnt.end(), iit;
  295. outvert.reserve(in.size());
  296. result.vertcnt.reserve(first_operand.vertcnt.size());
  297. std::vector<size_t> intersected_boundary_segments;
  298. std::vector<IfcVector3> intersected_boundary_points;
  299. // TODO: the following algorithm doesn't handle all cases.
  300. unsigned int vidx = 0;
  301. for(iit = begin; iit != end; vidx += *iit++) {
  302. if (!*iit) {
  303. continue;
  304. }
  305. unsigned int newcount = 0;
  306. bool was_outside_boundary = !PointInPoly(proj * in[vidx], profile->verts);
  307. size_t last_intersected_boundary_segment;
  308. IfcVector3 last_intersected_boundary_point;
  309. bool extra_point_flag = false;
  310. IfcVector3 extra_point;
  311. IfcVector3 enter_volume;
  312. bool entered_volume_flag = false;
  313. for(unsigned int i = 0; i < *iit; ++i) {
  314. // current segment: [i,i+1 mod size] or [*extra_point,i] if extra_point_flag is set
  315. const IfcVector3& e0 = extra_point_flag ? extra_point : in[vidx+i];
  316. const IfcVector3& e1 = extra_point_flag ? in[vidx+i] : in[vidx+(i+1)%*iit];
  317. // does the current segment intersect the polygonal boundary?
  318. const IfcVector3& e0_plane = proj * e0;
  319. const IfcVector3& e1_plane = proj * e1;
  320. intersected_boundary_segments.clear();
  321. intersected_boundary_points.clear();
  322. const bool is_outside_boundary = !PointInPoly(e1_plane, profile->verts);
  323. const bool is_boundary_intersection = is_outside_boundary != was_outside_boundary;
  324. IntersectsBoundaryProfile(e0_plane, e1_plane, profile->verts,
  325. intersected_boundary_segments,
  326. intersected_boundary_points);
  327. ai_assert(!is_boundary_intersection || !intersected_boundary_segments.empty());
  328. // does the current segment intersect the plane?
  329. // (no extra check if this is an extra point)
  330. IfcVector3 isectpos;
  331. const Intersect isect = extra_point_flag ? Intersect_No : IntersectSegmentPlane(p,n,e0,e1,isectpos);
  332. #ifdef _DEBUG
  333. if (isect == Intersect_Yes) {
  334. const IfcFloat f = fabs((isectpos - p)*n);
  335. ai_assert(f < 1e-5);
  336. }
  337. #endif
  338. const bool is_white_side = (e0-p)*n >= -1e-6;
  339. // e0 on good side of plane? (i.e. we should keep all geometry on this side)
  340. if (is_white_side) {
  341. // but is there an intersection in e0-e1 and is e1 in the clipping
  342. // boundary? In this case, generate a line that only goes to the
  343. // intersection point.
  344. if (isect == Intersect_Yes && !is_outside_boundary) {
  345. outvert.push_back(e0);
  346. ++newcount;
  347. outvert.push_back(isectpos);
  348. ++newcount;
  349. /*
  350. // this is, however, only a line that goes to the plane, but not
  351. // necessarily to the point where the bounding volume on the
  352. // black side of the plane is hit. So basically, we need another
  353. // check for [isectpos-e1], which should yield an intersection
  354. // point.
  355. extra_point_flag = true;
  356. extra_point = isectpos;
  357. was_outside_boundary = true;
  358. continue; */
  359. // [isectpos, enter_volume] potentially needs extra points.
  360. // For this, we determine the intersection point with the
  361. // bounding volume and project it onto the plane.
  362. /*
  363. const IfcVector3& enter_volume_proj = proj * enter_volume;
  364. const IfcVector3& enter_isectpos = proj * isectpos;
  365. intersected_boundary_segments.clear();
  366. intersected_boundary_points.clear();
  367. IntersectsBoundaryProfile(enter_volume_proj, enter_isectpos, profile->verts,
  368. intersected_boundary_segments,
  369. intersected_boundary_points);
  370. if(!intersected_boundary_segments.empty()) {
  371. vec = vec + ((p - vec) * n) * n;
  372. }
  373. */
  374. //entered_volume_flag = true;
  375. }
  376. else {
  377. outvert.push_back(e0);
  378. ++newcount;
  379. }
  380. }
  381. // e0 on bad side of plane, e1 on good (i.e. we should remove geometry on this side,
  382. // but only if it is within the bounding volume).
  383. else if (isect == Intersect_Yes) {
  384. // is e0 within the clipping volume? Insert the intersection point
  385. // of [e0,e1] and the plane instead of e0.
  386. if(was_outside_boundary) {
  387. outvert.push_back(e0);
  388. }
  389. else {
  390. if(entered_volume_flag) {
  391. const IfcVector3& fix_point = enter_volume + ((p - enter_volume) * n) * n;
  392. outvert.push_back(fix_point);
  393. ++newcount;
  394. }
  395. outvert.push_back(isectpos);
  396. }
  397. entered_volume_flag = false;
  398. ++newcount;
  399. }
  400. else { // no intersection with plane or parallel; e0,e1 are on the bad side
  401. // did we just pass the boundary line to the poly bounding?
  402. if (is_boundary_intersection) {
  403. // and are now outside the clipping boundary?
  404. if (is_outside_boundary) {
  405. // in this case, get the point where the clipping boundary
  406. // was entered first. Then, get the point where the clipping
  407. // boundary volume was left! These two points with the plane
  408. // normal form another plane that intersects the clipping
  409. // volume. There are two ways to get from the first to the
  410. // second point along the intersection curve, try to pick the
  411. // one that lies within the current polygon.
  412. // TODO this approach doesn't handle all cases
  413. // ...
  414. IfcFloat d = 1e20;
  415. IfcVector3 vclosest;
  416. BOOST_FOREACH(const IfcVector3& v, intersected_boundary_points) {
  417. const IfcFloat dn = (v-e1_plane).SquareLength();
  418. if (dn < d) {
  419. d = dn;
  420. vclosest = v;
  421. }
  422. }
  423. vclosest = proj_inv * vclosest;
  424. if(entered_volume_flag) {
  425. const IfcVector3& fix_point = vclosest + ((p - vclosest) * n) * n;
  426. outvert.push_back(fix_point);
  427. ++newcount;
  428. entered_volume_flag = false;
  429. }
  430. outvert.push_back(vclosest);
  431. ++newcount;
  432. //outvert.push_back(e1);
  433. //++newcount;
  434. }
  435. else {
  436. entered_volume_flag = true;
  437. // we just entered the clipping boundary. Record the point
  438. // and the segment where we entered and also generate this point.
  439. //last_intersected_boundary_segment = intersected_boundary_segments.front();
  440. //last_intersected_boundary_point = intersected_boundary_points.front();
  441. outvert.push_back(e0);
  442. ++newcount;
  443. IfcFloat d = 1e20;
  444. IfcVector3 vclosest;
  445. BOOST_FOREACH(const IfcVector3& v, intersected_boundary_points) {
  446. const IfcFloat dn = (v-e0_plane).SquareLength();
  447. if (dn < d) {
  448. d = dn;
  449. vclosest = v;
  450. }
  451. }
  452. enter_volume = proj_inv * vclosest;
  453. outvert.push_back(enter_volume);
  454. ++newcount;
  455. }
  456. }
  457. // if not, we just keep the vertex
  458. else if (is_outside_boundary) {
  459. outvert.push_back(e0);
  460. ++newcount;
  461. entered_volume_flag = false;
  462. }
  463. }
  464. was_outside_boundary = is_outside_boundary;
  465. extra_point_flag = false;
  466. }
  467. if (!newcount) {
  468. continue;
  469. }
  470. IfcVector3 vmin,vmax;
  471. ArrayBounds(&*(outvert.end()-newcount),newcount,vmin,vmax);
  472. // filter our IfcFloat points - those may happen if a point lies
  473. // directly on the intersection line. However, due to IfcFloat
  474. // precision a bitwise comparison is not feasible to detect
  475. // this case.
  476. const IfcFloat epsilon = (vmax-vmin).SquareLength() / 1e6f;
  477. FuzzyVectorCompare fz(epsilon);
  478. std::vector<IfcVector3>::iterator e = std::unique( outvert.end()-newcount, outvert.end(), fz );
  479. if (e != outvert.end()) {
  480. newcount -= static_cast<unsigned int>(std::distance(e,outvert.end()));
  481. outvert.erase(e,outvert.end());
  482. }
  483. if (fz(*( outvert.end()-newcount),outvert.back())) {
  484. outvert.pop_back();
  485. --newcount;
  486. }
  487. if(newcount > 2) {
  488. result.vertcnt.push_back(newcount);
  489. }
  490. else while(newcount-->0) {
  491. result.verts.pop_back();
  492. }
  493. }
  494. IFCImporter::LogDebug("generating CSG geometry by plane clipping with polygonal bounding (IfcBooleanClippingResult)");
  495. }
  496. // ------------------------------------------------------------------------------------------------
  497. void ProcessBooleanExtrudedAreaSolidDifference(const IfcExtrudedAreaSolid* as, TempMesh& result,
  498. const TempMesh& first_operand,
  499. ConversionData& conv)
  500. {
  501. ai_assert(as != NULL);
  502. // This case is handled by reduction to an instance of the quadrify() algorithm.
  503. // Obviously, this won't work for arbitrarily complex cases. In fact, the first
  504. // operand should be near-planar. Luckily, this is usually the case in Ifc
  505. // buildings.
  506. boost::shared_ptr<TempMesh> meshtmp = boost::shared_ptr<TempMesh>(new TempMesh());
  507. ProcessExtrudedAreaSolid(*as,*meshtmp,conv,false);
  508. std::vector<TempOpening> openings(1, TempOpening(as,IfcVector3(0,0,0),meshtmp,boost::shared_ptr<TempMesh>()));
  509. result = first_operand;
  510. TempMesh temp;
  511. std::vector<IfcVector3>::const_iterator vit = first_operand.verts.begin();
  512. BOOST_FOREACH(unsigned int pcount, first_operand.vertcnt) {
  513. temp.Clear();
  514. temp.verts.insert(temp.verts.end(), vit, vit + pcount);
  515. temp.vertcnt.push_back(pcount);
  516. // The algorithms used to generate mesh geometry sometimes
  517. // spit out lines or other degenerates which must be
  518. // filtered to avoid running into assertions later on.
  519. // ComputePolygonNormal returns the Newell normal, so the
  520. // length of the normal is the area of the polygon.
  521. const IfcVector3& normal = temp.ComputeLastPolygonNormal(false);
  522. if (normal.SquareLength() < static_cast<IfcFloat>(1e-5)) {
  523. IFCImporter::LogWarn("skipping degenerate polygon (ProcessBooleanExtrudedAreaSolidDifference)");
  524. continue;
  525. }
  526. GenerateOpenings(openings, std::vector<IfcVector3>(1,IfcVector3(1,0,0)), temp, false, true);
  527. result.Append(temp);
  528. vit += pcount;
  529. }
  530. IFCImporter::LogDebug("generating CSG geometry by geometric difference to a solid (IfcExtrudedAreaSolid)");
  531. }
  532. // ------------------------------------------------------------------------------------------------
  533. void ProcessBoolean(const IfcBooleanResult& boolean, TempMesh& result, ConversionData& conv)
  534. {
  535. // supported CSG operations:
  536. // DIFFERENCE
  537. if(const IfcBooleanResult* const clip = boolean.ToPtr<IfcBooleanResult>()) {
  538. if(clip->Operator != "DIFFERENCE") {
  539. IFCImporter::LogWarn("encountered unsupported boolean operator: " + (std::string)clip->Operator);
  540. return;
  541. }
  542. // supported cases (1st operand):
  543. // IfcBooleanResult -- call ProcessBoolean recursively
  544. // IfcSweptAreaSolid -- obtain polygonal geometry first
  545. // supported cases (2nd operand):
  546. // IfcHalfSpaceSolid -- easy, clip against plane
  547. // IfcExtrudedAreaSolid -- reduce to an instance of the quadrify() algorithm
  548. const IfcHalfSpaceSolid* const hs = clip->SecondOperand->ResolveSelectPtr<IfcHalfSpaceSolid>(conv.db);
  549. const IfcExtrudedAreaSolid* const as = clip->SecondOperand->ResolveSelectPtr<IfcExtrudedAreaSolid>(conv.db);
  550. if(!hs && !as) {
  551. IFCImporter::LogError("expected IfcHalfSpaceSolid or IfcExtrudedAreaSolid as second clipping operand");
  552. return;
  553. }
  554. TempMesh first_operand;
  555. if(const IfcBooleanResult* const op0 = clip->FirstOperand->ResolveSelectPtr<IfcBooleanResult>(conv.db)) {
  556. ProcessBoolean(*op0,first_operand,conv);
  557. }
  558. else if (const IfcSweptAreaSolid* const swept = clip->FirstOperand->ResolveSelectPtr<IfcSweptAreaSolid>(conv.db)) {
  559. ProcessSweptAreaSolid(*swept,first_operand,conv);
  560. }
  561. else {
  562. IFCImporter::LogError("expected IfcSweptAreaSolid or IfcBooleanResult as first clipping operand");
  563. return;
  564. }
  565. if(hs) {
  566. const IfcPolygonalBoundedHalfSpace* const hs_bounded = clip->SecondOperand->ResolveSelectPtr<IfcPolygonalBoundedHalfSpace>(conv.db);
  567. if (hs_bounded) {
  568. ProcessPolygonalBoundedBooleanHalfSpaceDifference(hs_bounded, result, first_operand, conv);
  569. }
  570. else {
  571. ProcessBooleanHalfSpaceDifference(hs, result, first_operand, conv);
  572. }
  573. }
  574. else {
  575. ProcessBooleanExtrudedAreaSolidDifference(as, result, first_operand, conv);
  576. }
  577. }
  578. else {
  579. IFCImporter::LogWarn("skipping unknown IfcBooleanResult entity, type is " + boolean.GetClassName());
  580. }
  581. }
  582. } // ! IFC
  583. } // ! Assimp
  584. #endif