IFCBoolean.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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 ASSIMP_BUILD_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(IfcVector2(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. // used any more?
  308. //size_t last_intersected_boundary_segment;
  309. IfcVector3 last_intersected_boundary_point;
  310. bool extra_point_flag = false;
  311. IfcVector3 extra_point;
  312. IfcVector3 enter_volume;
  313. bool entered_volume_flag = false;
  314. for(unsigned int i = 0; i < *iit; ++i) {
  315. // current segment: [i,i+1 mod size] or [*extra_point,i] if extra_point_flag is set
  316. const IfcVector3& e0 = extra_point_flag ? extra_point : in[vidx+i];
  317. const IfcVector3& e1 = extra_point_flag ? in[vidx+i] : in[vidx+(i+1)%*iit];
  318. // does the current segment intersect the polygonal boundary?
  319. const IfcVector3& e0_plane = proj * e0;
  320. const IfcVector3& e1_plane = proj * e1;
  321. intersected_boundary_segments.clear();
  322. intersected_boundary_points.clear();
  323. const bool is_outside_boundary = !PointInPoly(e1_plane, profile->verts);
  324. const bool is_boundary_intersection = is_outside_boundary != was_outside_boundary;
  325. IntersectsBoundaryProfile(e0_plane, e1_plane, profile->verts,
  326. intersected_boundary_segments,
  327. intersected_boundary_points);
  328. ai_assert(!is_boundary_intersection || !intersected_boundary_segments.empty());
  329. // does the current segment intersect the plane?
  330. // (no extra check if this is an extra point)
  331. IfcVector3 isectpos;
  332. const Intersect isect = extra_point_flag ? Intersect_No : IntersectSegmentPlane(p,n,e0,e1,isectpos);
  333. #ifdef ASSIMP_BUILD_DEBUG
  334. if (isect == Intersect_Yes) {
  335. const IfcFloat f = fabs((isectpos - p)*n);
  336. ai_assert(f < 1e-5);
  337. }
  338. #endif
  339. const bool is_white_side = (e0-p)*n >= -1e-6;
  340. // e0 on good side of plane? (i.e. we should keep all geometry on this side)
  341. if (is_white_side) {
  342. // but is there an intersection in e0-e1 and is e1 in the clipping
  343. // boundary? In this case, generate a line that only goes to the
  344. // intersection point.
  345. if (isect == Intersect_Yes && !is_outside_boundary) {
  346. outvert.push_back(e0);
  347. ++newcount;
  348. outvert.push_back(isectpos);
  349. ++newcount;
  350. /*
  351. // this is, however, only a line that goes to the plane, but not
  352. // necessarily to the point where the bounding volume on the
  353. // black side of the plane is hit. So basically, we need another
  354. // check for [isectpos-e1], which should yield an intersection
  355. // point.
  356. extra_point_flag = true;
  357. extra_point = isectpos;
  358. was_outside_boundary = true;
  359. continue; */
  360. // [isectpos, enter_volume] potentially needs extra points.
  361. // For this, we determine the intersection point with the
  362. // bounding volume and project it onto the plane.
  363. /*
  364. const IfcVector3& enter_volume_proj = proj * enter_volume;
  365. const IfcVector3& enter_isectpos = proj * isectpos;
  366. intersected_boundary_segments.clear();
  367. intersected_boundary_points.clear();
  368. IntersectsBoundaryProfile(enter_volume_proj, enter_isectpos, profile->verts,
  369. intersected_boundary_segments,
  370. intersected_boundary_points);
  371. if(!intersected_boundary_segments.empty()) {
  372. vec = vec + ((p - vec) * n) * n;
  373. }
  374. */
  375. //entered_volume_flag = true;
  376. }
  377. else {
  378. outvert.push_back(e0);
  379. ++newcount;
  380. }
  381. }
  382. // e0 on bad side of plane, e1 on good (i.e. we should remove geometry on this side,
  383. // but only if it is within the bounding volume).
  384. else if (isect == Intersect_Yes) {
  385. // is e0 within the clipping volume? Insert the intersection point
  386. // of [e0,e1] and the plane instead of e0.
  387. if(was_outside_boundary) {
  388. outvert.push_back(e0);
  389. }
  390. else {
  391. if(entered_volume_flag) {
  392. const IfcVector3& fix_point = enter_volume + ((p - enter_volume) * n) * n;
  393. outvert.push_back(fix_point);
  394. ++newcount;
  395. }
  396. outvert.push_back(isectpos);
  397. }
  398. entered_volume_flag = false;
  399. ++newcount;
  400. }
  401. else { // no intersection with plane or parallel; e0,e1 are on the bad side
  402. // did we just pass the boundary line to the poly bounding?
  403. if (is_boundary_intersection) {
  404. // and are now outside the clipping boundary?
  405. if (is_outside_boundary) {
  406. // in this case, get the point where the clipping boundary
  407. // was entered first. Then, get the point where the clipping
  408. // boundary volume was left! These two points with the plane
  409. // normal form another plane that intersects the clipping
  410. // volume. There are two ways to get from the first to the
  411. // second point along the intersection curve, try to pick the
  412. // one that lies within the current polygon.
  413. // TODO this approach doesn't handle all cases
  414. // ...
  415. IfcFloat d = 1e20;
  416. IfcVector3 vclosest;
  417. BOOST_FOREACH(const IfcVector3& v, intersected_boundary_points) {
  418. const IfcFloat dn = (v-e1_plane).SquareLength();
  419. if (dn < d) {
  420. d = dn;
  421. vclosest = v;
  422. }
  423. }
  424. vclosest = proj_inv * vclosest;
  425. if(entered_volume_flag) {
  426. const IfcVector3& fix_point = vclosest + ((p - vclosest) * n) * n;
  427. outvert.push_back(fix_point);
  428. ++newcount;
  429. entered_volume_flag = false;
  430. }
  431. outvert.push_back(vclosest);
  432. ++newcount;
  433. //outvert.push_back(e1);
  434. //++newcount;
  435. }
  436. else {
  437. entered_volume_flag = true;
  438. // we just entered the clipping boundary. Record the point
  439. // and the segment where we entered and also generate this point.
  440. //last_intersected_boundary_segment = intersected_boundary_segments.front();
  441. //last_intersected_boundary_point = intersected_boundary_points.front();
  442. outvert.push_back(e0);
  443. ++newcount;
  444. IfcFloat d = 1e20;
  445. IfcVector3 vclosest;
  446. BOOST_FOREACH(const IfcVector3& v, intersected_boundary_points) {
  447. const IfcFloat dn = (v-e0_plane).SquareLength();
  448. if (dn < d) {
  449. d = dn;
  450. vclosest = v;
  451. }
  452. }
  453. enter_volume = proj_inv * vclosest;
  454. outvert.push_back(enter_volume);
  455. ++newcount;
  456. }
  457. }
  458. // if not, we just keep the vertex
  459. else if (is_outside_boundary) {
  460. outvert.push_back(e0);
  461. ++newcount;
  462. entered_volume_flag = false;
  463. }
  464. }
  465. was_outside_boundary = is_outside_boundary;
  466. extra_point_flag = false;
  467. }
  468. if (!newcount) {
  469. continue;
  470. }
  471. IfcVector3 vmin,vmax;
  472. ArrayBounds(&*(outvert.end()-newcount),newcount,vmin,vmax);
  473. // filter our IfcFloat points - those may happen if a point lies
  474. // directly on the intersection line. However, due to IfcFloat
  475. // precision a bitwise comparison is not feasible to detect
  476. // this case.
  477. const IfcFloat epsilon = (vmax-vmin).SquareLength() / 1e6f;
  478. FuzzyVectorCompare fz(epsilon);
  479. std::vector<IfcVector3>::iterator e = std::unique( outvert.end()-newcount, outvert.end(), fz );
  480. if (e != outvert.end()) {
  481. newcount -= static_cast<unsigned int>(std::distance(e,outvert.end()));
  482. outvert.erase(e,outvert.end());
  483. }
  484. if (fz(*( outvert.end()-newcount),outvert.back())) {
  485. outvert.pop_back();
  486. --newcount;
  487. }
  488. if(newcount > 2) {
  489. result.vertcnt.push_back(newcount);
  490. }
  491. else while(newcount-->0) {
  492. result.verts.pop_back();
  493. }
  494. }
  495. IFCImporter::LogDebug("generating CSG geometry by plane clipping with polygonal bounding (IfcBooleanClippingResult)");
  496. }
  497. // ------------------------------------------------------------------------------------------------
  498. void ProcessBooleanExtrudedAreaSolidDifference(const IfcExtrudedAreaSolid* as, TempMesh& result,
  499. const TempMesh& first_operand,
  500. ConversionData& conv)
  501. {
  502. ai_assert(as != NULL);
  503. // This case is handled by reduction to an instance of the quadrify() algorithm.
  504. // Obviously, this won't work for arbitrarily complex cases. In fact, the first
  505. // operand should be near-planar. Luckily, this is usually the case in Ifc
  506. // buildings.
  507. boost::shared_ptr<TempMesh> meshtmp = boost::shared_ptr<TempMesh>(new TempMesh());
  508. ProcessExtrudedAreaSolid(*as,*meshtmp,conv,false);
  509. std::vector<TempOpening> openings(1, TempOpening(as,IfcVector3(0,0,0),meshtmp,boost::shared_ptr<TempMesh>()));
  510. result = first_operand;
  511. TempMesh temp;
  512. std::vector<IfcVector3>::const_iterator vit = first_operand.verts.begin();
  513. BOOST_FOREACH(unsigned int pcount, first_operand.vertcnt) {
  514. temp.Clear();
  515. temp.verts.insert(temp.verts.end(), vit, vit + pcount);
  516. temp.vertcnt.push_back(pcount);
  517. // The algorithms used to generate mesh geometry sometimes
  518. // spit out lines or other degenerates which must be
  519. // filtered to avoid running into assertions later on.
  520. // ComputePolygonNormal returns the Newell normal, so the
  521. // length of the normal is the area of the polygon.
  522. const IfcVector3& normal = temp.ComputeLastPolygonNormal(false);
  523. if (normal.SquareLength() < static_cast<IfcFloat>(1e-5)) {
  524. IFCImporter::LogWarn("skipping degenerate polygon (ProcessBooleanExtrudedAreaSolidDifference)");
  525. continue;
  526. }
  527. GenerateOpenings(openings, std::vector<IfcVector3>(1,IfcVector3(1,0,0)), temp, false, true);
  528. result.Append(temp);
  529. vit += pcount;
  530. }
  531. IFCImporter::LogDebug("generating CSG geometry by geometric difference to a solid (IfcExtrudedAreaSolid)");
  532. }
  533. // ------------------------------------------------------------------------------------------------
  534. void ProcessBoolean(const IfcBooleanResult& boolean, TempMesh& result, ConversionData& conv)
  535. {
  536. // supported CSG operations:
  537. // DIFFERENCE
  538. if(const IfcBooleanResult* const clip = boolean.ToPtr<IfcBooleanResult>()) {
  539. if(clip->Operator != "DIFFERENCE") {
  540. IFCImporter::LogWarn("encountered unsupported boolean operator: " + (std::string)clip->Operator);
  541. return;
  542. }
  543. // supported cases (1st operand):
  544. // IfcBooleanResult -- call ProcessBoolean recursively
  545. // IfcSweptAreaSolid -- obtain polygonal geometry first
  546. // supported cases (2nd operand):
  547. // IfcHalfSpaceSolid -- easy, clip against plane
  548. // IfcExtrudedAreaSolid -- reduce to an instance of the quadrify() algorithm
  549. const IfcHalfSpaceSolid* const hs = clip->SecondOperand->ResolveSelectPtr<IfcHalfSpaceSolid>(conv.db);
  550. const IfcExtrudedAreaSolid* const as = clip->SecondOperand->ResolveSelectPtr<IfcExtrudedAreaSolid>(conv.db);
  551. if(!hs && !as) {
  552. IFCImporter::LogError("expected IfcHalfSpaceSolid or IfcExtrudedAreaSolid as second clipping operand");
  553. return;
  554. }
  555. TempMesh first_operand;
  556. if(const IfcBooleanResult* const op0 = clip->FirstOperand->ResolveSelectPtr<IfcBooleanResult>(conv.db)) {
  557. ProcessBoolean(*op0,first_operand,conv);
  558. }
  559. else if (const IfcSweptAreaSolid* const swept = clip->FirstOperand->ResolveSelectPtr<IfcSweptAreaSolid>(conv.db)) {
  560. ProcessSweptAreaSolid(*swept,first_operand,conv);
  561. }
  562. else {
  563. IFCImporter::LogError("expected IfcSweptAreaSolid or IfcBooleanResult as first clipping operand");
  564. return;
  565. }
  566. if(hs) {
  567. const IfcPolygonalBoundedHalfSpace* const hs_bounded = clip->SecondOperand->ResolveSelectPtr<IfcPolygonalBoundedHalfSpace>(conv.db);
  568. if (hs_bounded) {
  569. ProcessPolygonalBoundedBooleanHalfSpaceDifference(hs_bounded, result, first_operand, conv);
  570. }
  571. else {
  572. ProcessBooleanHalfSpaceDifference(hs, result, first_operand, conv);
  573. }
  574. }
  575. else {
  576. ProcessBooleanExtrudedAreaSolidDifference(as, result, first_operand, conv);
  577. }
  578. }
  579. else {
  580. IFCImporter::LogWarn("skipping unknown IfcBooleanResult entity, type is " + boolean.GetClassName());
  581. }
  582. }
  583. } // ! IFC
  584. } // ! Assimp
  585. #endif