vrshoot

annotate libs/assimp/IFCOpenings.cpp @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
rev   line source
nuclear@0 1 /*
nuclear@0 2 Open Asset Import Library (assimp)
nuclear@0 3 ----------------------------------------------------------------------
nuclear@0 4
nuclear@0 5 Copyright (c) 2006-2010, assimp team
nuclear@0 6 All rights reserved.
nuclear@0 7
nuclear@0 8 Redistribution and use of this software in source and binary forms,
nuclear@0 9 with or without modification, are permitted provided that the
nuclear@0 10 following conditions are met:
nuclear@0 11
nuclear@0 12 * Redistributions of source code must retain the above
nuclear@0 13 copyright notice, this list of conditions and the
nuclear@0 14 following disclaimer.
nuclear@0 15
nuclear@0 16 * Redistributions in binary form must reproduce the above
nuclear@0 17 copyright notice, this list of conditions and the
nuclear@0 18 following disclaimer in the documentation and/or other
nuclear@0 19 materials provided with the distribution.
nuclear@0 20
nuclear@0 21 * Neither the name of the assimp team, nor the names of its
nuclear@0 22 contributors may be used to endorse or promote products
nuclear@0 23 derived from this software without specific prior
nuclear@0 24 written permission of the assimp team.
nuclear@0 25
nuclear@0 26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
nuclear@0 27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
nuclear@0 28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
nuclear@0 29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
nuclear@0 30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
nuclear@0 31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
nuclear@0 32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
nuclear@0 33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
nuclear@0 34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
nuclear@0 35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
nuclear@0 36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nuclear@0 37
nuclear@0 38 ----------------------------------------------------------------------
nuclear@0 39 */
nuclear@0 40
nuclear@0 41 /** @file IFCOpenings.cpp
nuclear@0 42 * @brief Implements a subset of Ifc CSG operations for pouring
nuclear@0 43 * holes for windows and doors into walls.
nuclear@0 44 */
nuclear@0 45
nuclear@0 46 #include "AssimpPCH.h"
nuclear@0 47
nuclear@0 48 #ifndef ASSIMP_BUILD_NO_IFC_IMPORTER
nuclear@0 49 #include "IFCUtil.h"
nuclear@0 50 #include "PolyTools.h"
nuclear@0 51 #include "ProcessHelper.h"
nuclear@0 52
nuclear@0 53 #include "../contrib/poly2tri/poly2tri/poly2tri.h"
nuclear@0 54 #include "../contrib/clipper/clipper.hpp"
nuclear@0 55
nuclear@0 56 #include <iterator>
nuclear@0 57
nuclear@0 58 namespace Assimp {
nuclear@0 59 namespace IFC {
nuclear@0 60
nuclear@0 61 using ClipperLib::ulong64;
nuclear@0 62 // XXX use full -+ range ...
nuclear@0 63 const ClipperLib::long64 max_ulong64 = 1518500249; // clipper.cpp / hiRange var
nuclear@0 64
nuclear@0 65 //#define to_int64(p) (static_cast<ulong64>( std::max( 0., std::min( static_cast<IfcFloat>((p)), 1.) ) * max_ulong64 ))
nuclear@0 66 #define to_int64(p) (static_cast<ulong64>(static_cast<IfcFloat>((p) ) * max_ulong64 ))
nuclear@0 67 #define from_int64(p) (static_cast<IfcFloat>((p)) / max_ulong64)
nuclear@0 68 #define one_vec (IfcVector2(static_cast<IfcFloat>(1.0),static_cast<IfcFloat>(1.0)))
nuclear@0 69
nuclear@0 70
nuclear@0 71 // fallback method to generate wall openings
nuclear@0 72 bool TryAddOpenings_Poly2Tri(const std::vector<TempOpening>& openings,const std::vector<IfcVector3>& nors,
nuclear@0 73 TempMesh& curmesh);
nuclear@0 74
nuclear@0 75
nuclear@0 76 typedef std::pair< IfcVector2, IfcVector2 > BoundingBox;
nuclear@0 77 typedef std::map<IfcVector2,size_t,XYSorter> XYSortedField;
nuclear@0 78
nuclear@0 79
nuclear@0 80 // ------------------------------------------------------------------------------------------------
nuclear@0 81 void QuadrifyPart(const IfcVector2& pmin, const IfcVector2& pmax, XYSortedField& field,
nuclear@0 82 const std::vector< BoundingBox >& bbs,
nuclear@0 83 std::vector<IfcVector2>& out)
nuclear@0 84 {
nuclear@0 85 if (!(pmin.x-pmax.x) || !(pmin.y-pmax.y)) {
nuclear@0 86 return;
nuclear@0 87 }
nuclear@0 88
nuclear@0 89 IfcFloat xs = 1e10, xe = 1e10;
nuclear@0 90 bool found = false;
nuclear@0 91
nuclear@0 92 // Search along the x-axis until we find an opening
nuclear@0 93 XYSortedField::iterator start = field.begin();
nuclear@0 94 for(; start != field.end(); ++start) {
nuclear@0 95 const BoundingBox& bb = bbs[(*start).second];
nuclear@0 96 if(bb.first.x >= pmax.x) {
nuclear@0 97 break;
nuclear@0 98 }
nuclear@0 99
nuclear@0 100 if (bb.second.x > pmin.x && bb.second.y > pmin.y && bb.first.y < pmax.y) {
nuclear@0 101 xs = bb.first.x;
nuclear@0 102 xe = bb.second.x;
nuclear@0 103 found = true;
nuclear@0 104 break;
nuclear@0 105 }
nuclear@0 106 }
nuclear@0 107
nuclear@0 108 if (!found) {
nuclear@0 109 // the rectangle [pmin,pend] is opaque, fill it
nuclear@0 110 out.push_back(pmin);
nuclear@0 111 out.push_back(IfcVector2(pmin.x,pmax.y));
nuclear@0 112 out.push_back(pmax);
nuclear@0 113 out.push_back(IfcVector2(pmax.x,pmin.y));
nuclear@0 114 return;
nuclear@0 115 }
nuclear@0 116
nuclear@0 117 xs = std::max(pmin.x,xs);
nuclear@0 118 xe = std::min(pmax.x,xe);
nuclear@0 119
nuclear@0 120 // see if there's an offset to fill at the top of our quad
nuclear@0 121 if (xs - pmin.x) {
nuclear@0 122 out.push_back(pmin);
nuclear@0 123 out.push_back(IfcVector2(pmin.x,pmax.y));
nuclear@0 124 out.push_back(IfcVector2(xs,pmax.y));
nuclear@0 125 out.push_back(IfcVector2(xs,pmin.y));
nuclear@0 126 }
nuclear@0 127
nuclear@0 128 // search along the y-axis for all openings that overlap xs and our quad
nuclear@0 129 IfcFloat ylast = pmin.y;
nuclear@0 130 found = false;
nuclear@0 131 for(; start != field.end(); ++start) {
nuclear@0 132 const BoundingBox& bb = bbs[(*start).second];
nuclear@0 133 if (bb.first.x > xs || bb.first.y >= pmax.y) {
nuclear@0 134 break;
nuclear@0 135 }
nuclear@0 136
nuclear@0 137 if (bb.second.y > ylast) {
nuclear@0 138
nuclear@0 139 found = true;
nuclear@0 140 const IfcFloat ys = std::max(bb.first.y,pmin.y), ye = std::min(bb.second.y,pmax.y);
nuclear@0 141 if (ys - ylast > 0.0f) {
nuclear@0 142 QuadrifyPart( IfcVector2(xs,ylast), IfcVector2(xe,ys) ,field,bbs,out);
nuclear@0 143 }
nuclear@0 144
nuclear@0 145 // the following are the window vertices
nuclear@0 146
nuclear@0 147 /*wnd.push_back(IfcVector2(xs,ys));
nuclear@0 148 wnd.push_back(IfcVector2(xs,ye));
nuclear@0 149 wnd.push_back(IfcVector2(xe,ye));
nuclear@0 150 wnd.push_back(IfcVector2(xe,ys));*/
nuclear@0 151 ylast = ye;
nuclear@0 152 }
nuclear@0 153 }
nuclear@0 154 if (!found) {
nuclear@0 155 // the rectangle [pmin,pend] is opaque, fill it
nuclear@0 156 out.push_back(IfcVector2(xs,pmin.y));
nuclear@0 157 out.push_back(IfcVector2(xs,pmax.y));
nuclear@0 158 out.push_back(IfcVector2(xe,pmax.y));
nuclear@0 159 out.push_back(IfcVector2(xe,pmin.y));
nuclear@0 160 return;
nuclear@0 161 }
nuclear@0 162 if (ylast < pmax.y) {
nuclear@0 163 QuadrifyPart( IfcVector2(xs,ylast), IfcVector2(xe,pmax.y) ,field,bbs,out);
nuclear@0 164 }
nuclear@0 165
nuclear@0 166 // now for the whole rest
nuclear@0 167 if (pmax.x-xe) {
nuclear@0 168 QuadrifyPart(IfcVector2(xe,pmin.y), pmax ,field,bbs,out);
nuclear@0 169 }
nuclear@0 170 }
nuclear@0 171
nuclear@0 172 typedef std::vector<IfcVector2> Contour;
nuclear@0 173 typedef std::vector<bool> SkipList; // should probably use int for performance reasons
nuclear@0 174
nuclear@0 175 struct ProjectedWindowContour
nuclear@0 176 {
nuclear@0 177 Contour contour;
nuclear@0 178 BoundingBox bb;
nuclear@0 179 SkipList skiplist;
nuclear@0 180 bool is_rectangular;
nuclear@0 181
nuclear@0 182
nuclear@0 183 ProjectedWindowContour(const Contour& contour, const BoundingBox& bb, bool is_rectangular)
nuclear@0 184 : contour(contour)
nuclear@0 185 , bb(bb)
nuclear@0 186 , is_rectangular(is_rectangular)
nuclear@0 187 {}
nuclear@0 188
nuclear@0 189
nuclear@0 190 bool IsInvalid() const {
nuclear@0 191 return contour.empty();
nuclear@0 192 }
nuclear@0 193
nuclear@0 194 void FlagInvalid() {
nuclear@0 195 contour.clear();
nuclear@0 196 }
nuclear@0 197
nuclear@0 198 void PrepareSkiplist() {
nuclear@0 199 skiplist.resize(contour.size(),false);
nuclear@0 200 }
nuclear@0 201 };
nuclear@0 202
nuclear@0 203 typedef std::vector< ProjectedWindowContour > ContourVector;
nuclear@0 204
nuclear@0 205 // ------------------------------------------------------------------------------------------------
nuclear@0 206 bool BoundingBoxesOverlapping( const BoundingBox &ibb, const BoundingBox &bb )
nuclear@0 207 {
nuclear@0 208 // count the '=' case as non-overlapping but as adjacent to each other
nuclear@0 209 return ibb.first.x < bb.second.x && ibb.second.x > bb.first.x &&
nuclear@0 210 ibb.first.y < bb.second.y && ibb.second.y > bb.first.y;
nuclear@0 211 }
nuclear@0 212
nuclear@0 213 // ------------------------------------------------------------------------------------------------
nuclear@0 214 bool IsDuplicateVertex(const IfcVector2& vv, const std::vector<IfcVector2>& temp_contour)
nuclear@0 215 {
nuclear@0 216 // sanity check for duplicate vertices
nuclear@0 217 BOOST_FOREACH(const IfcVector2& cp, temp_contour) {
nuclear@0 218 if ((cp-vv).SquareLength() < 1e-5f) {
nuclear@0 219 return true;
nuclear@0 220 }
nuclear@0 221 }
nuclear@0 222 return false;
nuclear@0 223 }
nuclear@0 224
nuclear@0 225 // ------------------------------------------------------------------------------------------------
nuclear@0 226 void ExtractVerticesFromClipper(const ClipperLib::Polygon& poly, std::vector<IfcVector2>& temp_contour,
nuclear@0 227 bool filter_duplicates = false)
nuclear@0 228 {
nuclear@0 229 temp_contour.clear();
nuclear@0 230 BOOST_FOREACH(const ClipperLib::IntPoint& point, poly) {
nuclear@0 231 IfcVector2 vv = IfcVector2( from_int64(point.X), from_int64(point.Y));
nuclear@0 232 vv = std::max(vv,IfcVector2());
nuclear@0 233 vv = std::min(vv,one_vec);
nuclear@0 234
nuclear@0 235 if (!filter_duplicates || !IsDuplicateVertex(vv, temp_contour)) {
nuclear@0 236 temp_contour.push_back(vv);
nuclear@0 237 }
nuclear@0 238 }
nuclear@0 239 }
nuclear@0 240
nuclear@0 241 // ------------------------------------------------------------------------------------------------
nuclear@0 242 BoundingBox GetBoundingBox(const ClipperLib::Polygon& poly)
nuclear@0 243 {
nuclear@0 244 IfcVector2 newbb_min, newbb_max;
nuclear@0 245 MinMaxChooser<IfcVector2>()(newbb_min, newbb_max);
nuclear@0 246
nuclear@0 247 BOOST_FOREACH(const ClipperLib::IntPoint& point, poly) {
nuclear@0 248 IfcVector2 vv = IfcVector2( from_int64(point.X), from_int64(point.Y));
nuclear@0 249
nuclear@0 250 // sanity rounding
nuclear@0 251 vv = std::max(vv,IfcVector2());
nuclear@0 252 vv = std::min(vv,one_vec);
nuclear@0 253
nuclear@0 254 newbb_min = std::min(newbb_min,vv);
nuclear@0 255 newbb_max = std::max(newbb_max,vv);
nuclear@0 256 }
nuclear@0 257 return BoundingBox(newbb_min, newbb_max);
nuclear@0 258 }
nuclear@0 259
nuclear@0 260 // ------------------------------------------------------------------------------------------------
nuclear@0 261 void InsertWindowContours(const ContourVector& contours,
nuclear@0 262 const std::vector<TempOpening>& openings,
nuclear@0 263 TempMesh& curmesh)
nuclear@0 264 {
nuclear@0 265 // fix windows - we need to insert the real, polygonal shapes into the quadratic holes that we have now
nuclear@0 266 for(size_t i = 0; i < contours.size();++i) {
nuclear@0 267 const BoundingBox& bb = contours[i].bb;
nuclear@0 268 const std::vector<IfcVector2>& contour = contours[i].contour;
nuclear@0 269 if(contour.empty()) {
nuclear@0 270 continue;
nuclear@0 271 }
nuclear@0 272
nuclear@0 273 // check if we need to do it at all - many windows just fit perfectly into their quadratic holes,
nuclear@0 274 // i.e. their contours *are* already their bounding boxes.
nuclear@0 275 if (contour.size() == 4) {
nuclear@0 276 std::set<IfcVector2,XYSorter> verts;
nuclear@0 277 for(size_t n = 0; n < 4; ++n) {
nuclear@0 278 verts.insert(contour[n]);
nuclear@0 279 }
nuclear@0 280 const std::set<IfcVector2,XYSorter>::const_iterator end = verts.end();
nuclear@0 281 if (verts.find(bb.first)!=end && verts.find(bb.second)!=end
nuclear@0 282 && verts.find(IfcVector2(bb.first.x,bb.second.y))!=end
nuclear@0 283 && verts.find(IfcVector2(bb.second.x,bb.first.y))!=end
nuclear@0 284 ) {
nuclear@0 285 continue;
nuclear@0 286 }
nuclear@0 287 }
nuclear@0 288
nuclear@0 289 const IfcFloat diag = (bb.first-bb.second).Length();
nuclear@0 290 const IfcFloat epsilon = diag/1000.f;
nuclear@0 291
nuclear@0 292 // walk through all contour points and find those that lie on the BB corner
nuclear@0 293 size_t last_hit = -1, very_first_hit = -1;
nuclear@0 294 IfcVector2 edge;
nuclear@0 295 for(size_t n = 0, e=0, size = contour.size();; n=(n+1)%size, ++e) {
nuclear@0 296
nuclear@0 297 // sanity checking
nuclear@0 298 if (e == size*2) {
nuclear@0 299 IFCImporter::LogError("encountered unexpected topology while generating window contour");
nuclear@0 300 break;
nuclear@0 301 }
nuclear@0 302
nuclear@0 303 const IfcVector2& v = contour[n];
nuclear@0 304
nuclear@0 305 bool hit = false;
nuclear@0 306 if (fabs(v.x-bb.first.x)<epsilon) {
nuclear@0 307 edge.x = bb.first.x;
nuclear@0 308 hit = true;
nuclear@0 309 }
nuclear@0 310 else if (fabs(v.x-bb.second.x)<epsilon) {
nuclear@0 311 edge.x = bb.second.x;
nuclear@0 312 hit = true;
nuclear@0 313 }
nuclear@0 314
nuclear@0 315 if (fabs(v.y-bb.first.y)<epsilon) {
nuclear@0 316 edge.y = bb.first.y;
nuclear@0 317 hit = true;
nuclear@0 318 }
nuclear@0 319 else if (fabs(v.y-bb.second.y)<epsilon) {
nuclear@0 320 edge.y = bb.second.y;
nuclear@0 321 hit = true;
nuclear@0 322 }
nuclear@0 323
nuclear@0 324 if (hit) {
nuclear@0 325 if (last_hit != (size_t)-1) {
nuclear@0 326
nuclear@0 327 const size_t old = curmesh.verts.size();
nuclear@0 328 size_t cnt = last_hit > n ? size-(last_hit-n) : n-last_hit;
nuclear@0 329 for(size_t a = last_hit, e = 0; e <= cnt; a=(a+1)%size, ++e) {
nuclear@0 330 // hack: this is to fix cases where opening contours are self-intersecting.
nuclear@0 331 // Clipper doesn't produce such polygons, but as soon as we're back in
nuclear@0 332 // our brave new floating-point world, very small distances are consumed
nuclear@0 333 // by the maximum available precision, leading to self-intersecting
nuclear@0 334 // polygons. This fix makes concave windows fail even worse, but
nuclear@0 335 // anyway, fail is fail.
nuclear@0 336 if ((contour[a] - edge).SquareLength() > diag*diag*0.7) {
nuclear@0 337 continue;
nuclear@0 338 }
nuclear@0 339 curmesh.verts.push_back(IfcVector3(contour[a].x, contour[a].y, 0.0f));
nuclear@0 340 }
nuclear@0 341
nuclear@0 342 if (edge != contour[last_hit]) {
nuclear@0 343
nuclear@0 344 IfcVector2 corner = edge;
nuclear@0 345
nuclear@0 346 if (fabs(contour[last_hit].x-bb.first.x)<epsilon) {
nuclear@0 347 corner.x = bb.first.x;
nuclear@0 348 }
nuclear@0 349 else if (fabs(contour[last_hit].x-bb.second.x)<epsilon) {
nuclear@0 350 corner.x = bb.second.x;
nuclear@0 351 }
nuclear@0 352
nuclear@0 353 if (fabs(contour[last_hit].y-bb.first.y)<epsilon) {
nuclear@0 354 corner.y = bb.first.y;
nuclear@0 355 }
nuclear@0 356 else if (fabs(contour[last_hit].y-bb.second.y)<epsilon) {
nuclear@0 357 corner.y = bb.second.y;
nuclear@0 358 }
nuclear@0 359
nuclear@0 360 curmesh.verts.push_back(IfcVector3(corner.x, corner.y, 0.0f));
nuclear@0 361 }
nuclear@0 362 else if (cnt == 1) {
nuclear@0 363 // avoid degenerate polygons (also known as lines or points)
nuclear@0 364 curmesh.verts.erase(curmesh.verts.begin()+old,curmesh.verts.end());
nuclear@0 365 }
nuclear@0 366
nuclear@0 367 if (const size_t d = curmesh.verts.size()-old) {
nuclear@0 368 curmesh.vertcnt.push_back(d);
nuclear@0 369 std::reverse(curmesh.verts.rbegin(),curmesh.verts.rbegin()+d);
nuclear@0 370 }
nuclear@0 371 if (n == very_first_hit) {
nuclear@0 372 break;
nuclear@0 373 }
nuclear@0 374 }
nuclear@0 375 else {
nuclear@0 376 very_first_hit = n;
nuclear@0 377 }
nuclear@0 378
nuclear@0 379 last_hit = n;
nuclear@0 380 }
nuclear@0 381 }
nuclear@0 382 }
nuclear@0 383 }
nuclear@0 384
nuclear@0 385 // ------------------------------------------------------------------------------------------------
nuclear@0 386 void MergeWindowContours (const std::vector<IfcVector2>& a,
nuclear@0 387 const std::vector<IfcVector2>& b,
nuclear@0 388 ClipperLib::ExPolygons& out)
nuclear@0 389 {
nuclear@0 390 out.clear();
nuclear@0 391
nuclear@0 392 ClipperLib::Clipper clipper;
nuclear@0 393 ClipperLib::Polygon clip;
nuclear@0 394
nuclear@0 395 BOOST_FOREACH(const IfcVector2& pip, a) {
nuclear@0 396 clip.push_back(ClipperLib::IntPoint( to_int64(pip.x), to_int64(pip.y) ));
nuclear@0 397 }
nuclear@0 398
nuclear@0 399 if (ClipperLib::Orientation(clip)) {
nuclear@0 400 std::reverse(clip.begin(), clip.end());
nuclear@0 401 }
nuclear@0 402
nuclear@0 403 clipper.AddPolygon(clip, ClipperLib::ptSubject);
nuclear@0 404 clip.clear();
nuclear@0 405
nuclear@0 406 BOOST_FOREACH(const IfcVector2& pip, b) {
nuclear@0 407 clip.push_back(ClipperLib::IntPoint( to_int64(pip.x), to_int64(pip.y) ));
nuclear@0 408 }
nuclear@0 409
nuclear@0 410 if (ClipperLib::Orientation(clip)) {
nuclear@0 411 std::reverse(clip.begin(), clip.end());
nuclear@0 412 }
nuclear@0 413
nuclear@0 414 clipper.AddPolygon(clip, ClipperLib::ptSubject);
nuclear@0 415 clipper.Execute(ClipperLib::ctUnion, out,ClipperLib::pftNonZero,ClipperLib::pftNonZero);
nuclear@0 416 }
nuclear@0 417
nuclear@0 418 // ------------------------------------------------------------------------------------------------
nuclear@0 419 // Subtract a from b
nuclear@0 420 void MakeDisjunctWindowContours (const std::vector<IfcVector2>& a,
nuclear@0 421 const std::vector<IfcVector2>& b,
nuclear@0 422 ClipperLib::ExPolygons& out)
nuclear@0 423 {
nuclear@0 424 out.clear();
nuclear@0 425
nuclear@0 426 ClipperLib::Clipper clipper;
nuclear@0 427 ClipperLib::Polygon clip;
nuclear@0 428
nuclear@0 429 BOOST_FOREACH(const IfcVector2& pip, a) {
nuclear@0 430 clip.push_back(ClipperLib::IntPoint( to_int64(pip.x), to_int64(pip.y) ));
nuclear@0 431 }
nuclear@0 432
nuclear@0 433 if (ClipperLib::Orientation(clip)) {
nuclear@0 434 std::reverse(clip.begin(), clip.end());
nuclear@0 435 }
nuclear@0 436
nuclear@0 437 clipper.AddPolygon(clip, ClipperLib::ptClip);
nuclear@0 438 clip.clear();
nuclear@0 439
nuclear@0 440 BOOST_FOREACH(const IfcVector2& pip, b) {
nuclear@0 441 clip.push_back(ClipperLib::IntPoint( to_int64(pip.x), to_int64(pip.y) ));
nuclear@0 442 }
nuclear@0 443
nuclear@0 444 if (ClipperLib::Orientation(clip)) {
nuclear@0 445 std::reverse(clip.begin(), clip.end());
nuclear@0 446 }
nuclear@0 447
nuclear@0 448 clipper.AddPolygon(clip, ClipperLib::ptSubject);
nuclear@0 449 clipper.Execute(ClipperLib::ctDifference, out,ClipperLib::pftNonZero,ClipperLib::pftNonZero);
nuclear@0 450 }
nuclear@0 451
nuclear@0 452 // ------------------------------------------------------------------------------------------------
nuclear@0 453 void CleanupWindowContour(ProjectedWindowContour& window)
nuclear@0 454 {
nuclear@0 455 std::vector<IfcVector2> scratch;
nuclear@0 456 std::vector<IfcVector2>& contour = window.contour;
nuclear@0 457
nuclear@0 458 ClipperLib::Polygon subject;
nuclear@0 459 ClipperLib::Clipper clipper;
nuclear@0 460 ClipperLib::ExPolygons clipped;
nuclear@0 461
nuclear@0 462 BOOST_FOREACH(const IfcVector2& pip, contour) {
nuclear@0 463 subject.push_back(ClipperLib::IntPoint( to_int64(pip.x), to_int64(pip.y) ));
nuclear@0 464 }
nuclear@0 465
nuclear@0 466 clipper.AddPolygon(subject,ClipperLib::ptSubject);
nuclear@0 467 clipper.Execute(ClipperLib::ctUnion,clipped,ClipperLib::pftNonZero,ClipperLib::pftNonZero);
nuclear@0 468
nuclear@0 469 // This should yield only one polygon or something went wrong
nuclear@0 470 if (clipped.size() != 1) {
nuclear@0 471
nuclear@0 472 // Empty polygon? drop the contour altogether
nuclear@0 473 if(clipped.empty()) {
nuclear@0 474 IFCImporter::LogError("error during polygon clipping, window contour is degenerate");
nuclear@0 475 window.FlagInvalid();
nuclear@0 476 return;
nuclear@0 477 }
nuclear@0 478
nuclear@0 479 // Else: take the first only
nuclear@0 480 IFCImporter::LogError("error during polygon clipping, window contour is not convex");
nuclear@0 481 }
nuclear@0 482
nuclear@0 483 ExtractVerticesFromClipper(clipped[0].outer, scratch);
nuclear@0 484 // Assume the bounding box doesn't change during this operation
nuclear@0 485 }
nuclear@0 486
nuclear@0 487 // ------------------------------------------------------------------------------------------------
nuclear@0 488 void CleanupWindowContours(ContourVector& contours)
nuclear@0 489 {
nuclear@0 490 // Use PolyClipper to clean up window contours
nuclear@0 491 try {
nuclear@0 492 BOOST_FOREACH(ProjectedWindowContour& window, contours) {
nuclear@0 493 CleanupWindowContour(window);
nuclear@0 494 }
nuclear@0 495 }
nuclear@0 496 catch (const char* sx) {
nuclear@0 497 IFCImporter::LogError("error during polygon clipping, window shape may be wrong: (Clipper: "
nuclear@0 498 + std::string(sx) + ")");
nuclear@0 499 }
nuclear@0 500 }
nuclear@0 501
nuclear@0 502 // ------------------------------------------------------------------------------------------------
nuclear@0 503 void CleanupOuterContour(const std::vector<IfcVector2>& contour_flat, TempMesh& curmesh)
nuclear@0 504 {
nuclear@0 505 std::vector<IfcVector3> vold;
nuclear@0 506 std::vector<unsigned int> iold;
nuclear@0 507
nuclear@0 508 vold.reserve(curmesh.verts.size());
nuclear@0 509 iold.reserve(curmesh.vertcnt.size());
nuclear@0 510
nuclear@0 511 // Fix the outer contour using polyclipper
nuclear@0 512 try {
nuclear@0 513
nuclear@0 514 ClipperLib::Polygon subject;
nuclear@0 515 ClipperLib::Clipper clipper;
nuclear@0 516 ClipperLib::ExPolygons clipped;
nuclear@0 517
nuclear@0 518 ClipperLib::Polygon clip;
nuclear@0 519 clip.reserve(contour_flat.size());
nuclear@0 520 BOOST_FOREACH(const IfcVector2& pip, contour_flat) {
nuclear@0 521 clip.push_back(ClipperLib::IntPoint( to_int64(pip.x), to_int64(pip.y) ));
nuclear@0 522 }
nuclear@0 523
nuclear@0 524 if (!ClipperLib::Orientation(clip)) {
nuclear@0 525 std::reverse(clip.begin(), clip.end());
nuclear@0 526 }
nuclear@0 527
nuclear@0 528 // We need to run polyclipper on every single polygon -- we can't run it one all
nuclear@0 529 // of them at once or it would merge them all together which would undo all
nuclear@0 530 // previous steps
nuclear@0 531 subject.reserve(4);
nuclear@0 532 size_t index = 0;
nuclear@0 533 size_t countdown = 0;
nuclear@0 534 BOOST_FOREACH(const IfcVector3& pip, curmesh.verts) {
nuclear@0 535 if (!countdown) {
nuclear@0 536 countdown = curmesh.vertcnt[index++];
nuclear@0 537 if (!countdown) {
nuclear@0 538 continue;
nuclear@0 539 }
nuclear@0 540 }
nuclear@0 541 subject.push_back(ClipperLib::IntPoint( to_int64(pip.x), to_int64(pip.y) ));
nuclear@0 542 if (--countdown == 0) {
nuclear@0 543 if (!ClipperLib::Orientation(subject)) {
nuclear@0 544 std::reverse(subject.begin(), subject.end());
nuclear@0 545 }
nuclear@0 546
nuclear@0 547 clipper.AddPolygon(subject,ClipperLib::ptSubject);
nuclear@0 548 clipper.AddPolygon(clip,ClipperLib::ptClip);
nuclear@0 549
nuclear@0 550 clipper.Execute(ClipperLib::ctIntersection,clipped,ClipperLib::pftNonZero,ClipperLib::pftNonZero);
nuclear@0 551
nuclear@0 552 BOOST_FOREACH(const ClipperLib::ExPolygon& ex, clipped) {
nuclear@0 553 iold.push_back(ex.outer.size());
nuclear@0 554 BOOST_FOREACH(const ClipperLib::IntPoint& point, ex.outer) {
nuclear@0 555 vold.push_back(IfcVector3(
nuclear@0 556 from_int64(point.X),
nuclear@0 557 from_int64(point.Y),
nuclear@0 558 0.0f));
nuclear@0 559 }
nuclear@0 560 }
nuclear@0 561
nuclear@0 562 subject.clear();
nuclear@0 563 clipped.clear();
nuclear@0 564 clipper.Clear();
nuclear@0 565 }
nuclear@0 566 }
nuclear@0 567 }
nuclear@0 568 catch (const char* sx) {
nuclear@0 569 IFCImporter::LogError("Ifc: error during polygon clipping, wall contour line may be wrong: (Clipper: "
nuclear@0 570 + std::string(sx) + ")");
nuclear@0 571
nuclear@0 572 return;
nuclear@0 573 }
nuclear@0 574
nuclear@0 575 // swap data arrays
nuclear@0 576 std::swap(vold,curmesh.verts);
nuclear@0 577 std::swap(iold,curmesh.vertcnt);
nuclear@0 578 }
nuclear@0 579
nuclear@0 580 typedef std::vector<TempOpening*> OpeningRefs;
nuclear@0 581 typedef std::vector<OpeningRefs > OpeningRefVector;
nuclear@0 582
nuclear@0 583 typedef std::vector<std::pair<
nuclear@0 584 ContourVector::const_iterator,
nuclear@0 585 Contour::const_iterator>
nuclear@0 586 > ContourRefVector;
nuclear@0 587
nuclear@0 588 // ------------------------------------------------------------------------------------------------
nuclear@0 589 bool BoundingBoxesAdjacent(const BoundingBox& bb, const BoundingBox& ibb)
nuclear@0 590 {
nuclear@0 591 // TODO: I'm pretty sure there is a much more compact way to check this
nuclear@0 592 const IfcFloat epsilon = 1e-5f;
nuclear@0 593 return (fabs(bb.second.x - ibb.first.x) < epsilon && bb.first.y <= ibb.second.y && bb.second.y >= ibb.first.y) ||
nuclear@0 594 (fabs(bb.first.x - ibb.second.x) < epsilon && ibb.first.y <= bb.second.y && ibb.second.y >= bb.first.y) ||
nuclear@0 595 (fabs(bb.second.y - ibb.first.y) < epsilon && bb.first.x <= ibb.second.x && bb.second.x >= ibb.first.x) ||
nuclear@0 596 (fabs(bb.first.y - ibb.second.y) < epsilon && ibb.first.x <= bb.second.x && ibb.second.x >= bb.first.x);
nuclear@0 597 }
nuclear@0 598
nuclear@0 599 // ------------------------------------------------------------------------------------------------
nuclear@0 600 // Check if m0,m1 intersects n0,n1 assuming same ordering of the points in the line segments
nuclear@0 601 // output the intersection points on n0,n1
nuclear@0 602 bool IntersectingLineSegments(const IfcVector2& n0, const IfcVector2& n1,
nuclear@0 603 const IfcVector2& m0, const IfcVector2& m1,
nuclear@0 604 IfcVector2& out0, IfcVector2& out1)
nuclear@0 605 {
nuclear@0 606 const IfcVector2& n0_to_n1 = n1 - n0;
nuclear@0 607
nuclear@0 608 const IfcVector2& n0_to_m0 = m0 - n0;
nuclear@0 609 const IfcVector2& n1_to_m1 = m1 - n1;
nuclear@0 610
nuclear@0 611 const IfcVector2& n0_to_m1 = m1 - n0;
nuclear@0 612
nuclear@0 613 const IfcFloat e = 1e-5f;
nuclear@0 614 const IfcFloat smalle = 1e-9f;
nuclear@0 615
nuclear@0 616 static const IfcFloat inf = std::numeric_limits<IfcFloat>::infinity();
nuclear@0 617
nuclear@0 618 if (!(n0_to_m0.SquareLength() < e*e || fabs(n0_to_m0 * n0_to_n1) / (n0_to_m0.Length() * n0_to_n1.Length()) > 1-1e-5 )) {
nuclear@0 619 return false;
nuclear@0 620 }
nuclear@0 621
nuclear@0 622 if (!(n1_to_m1.SquareLength() < e*e || fabs(n1_to_m1 * n0_to_n1) / (n1_to_m1.Length() * n0_to_n1.Length()) > 1-1e-5 )) {
nuclear@0 623 return false;
nuclear@0 624 }
nuclear@0 625
nuclear@0 626 IfcFloat s0;
nuclear@0 627 IfcFloat s1;
nuclear@0 628
nuclear@0 629 // pick the axis with the higher absolute difference so the result
nuclear@0 630 // is more accurate. Since we cannot guarantee that the axis with
nuclear@0 631 // the higher absolute difference is big enough as to avoid
nuclear@0 632 // divisions by zero, the case 0/0 ~ infinity is detected and
nuclear@0 633 // handled separately.
nuclear@0 634 if(fabs(n0_to_n1.x) > fabs(n0_to_n1.y)) {
nuclear@0 635 s0 = n0_to_m0.x / n0_to_n1.x;
nuclear@0 636 s1 = n0_to_m1.x / n0_to_n1.x;
nuclear@0 637
nuclear@0 638 if (fabs(s0) == inf && fabs(n0_to_m0.x) < smalle) {
nuclear@0 639 s0 = 0.;
nuclear@0 640 }
nuclear@0 641 if (fabs(s1) == inf && fabs(n0_to_m1.x) < smalle) {
nuclear@0 642 s1 = 0.;
nuclear@0 643 }
nuclear@0 644 }
nuclear@0 645 else {
nuclear@0 646 s0 = n0_to_m0.y / n0_to_n1.y;
nuclear@0 647 s1 = n0_to_m1.y / n0_to_n1.y;
nuclear@0 648
nuclear@0 649 if (fabs(s0) == inf && fabs(n0_to_m0.y) < smalle) {
nuclear@0 650 s0 = 0.;
nuclear@0 651 }
nuclear@0 652 if (fabs(s1) == inf && fabs(n0_to_m1.y) < smalle) {
nuclear@0 653 s1 = 0.;
nuclear@0 654 }
nuclear@0 655 }
nuclear@0 656
nuclear@0 657 if (s1 < s0) {
nuclear@0 658 std::swap(s1,s0);
nuclear@0 659 }
nuclear@0 660
nuclear@0 661 s0 = std::max(0.0,s0);
nuclear@0 662 s1 = std::max(0.0,s1);
nuclear@0 663
nuclear@0 664 s0 = std::min(1.0,s0);
nuclear@0 665 s1 = std::min(1.0,s1);
nuclear@0 666
nuclear@0 667 if (fabs(s1-s0) < e) {
nuclear@0 668 return false;
nuclear@0 669 }
nuclear@0 670
nuclear@0 671 out0 = n0 + s0 * n0_to_n1;
nuclear@0 672 out1 = n0 + s1 * n0_to_n1;
nuclear@0 673
nuclear@0 674 return true;
nuclear@0 675 }
nuclear@0 676
nuclear@0 677 // ------------------------------------------------------------------------------------------------
nuclear@0 678 void FindAdjacentContours(ContourVector::iterator current, const ContourVector& contours)
nuclear@0 679 {
nuclear@0 680 const IfcFloat sqlen_epsilon = static_cast<IfcFloat>(1e-8);
nuclear@0 681 const BoundingBox& bb = (*current).bb;
nuclear@0 682
nuclear@0 683 // What is to be done here is to populate the skip lists for the contour
nuclear@0 684 // and to add necessary padding points when needed.
nuclear@0 685 SkipList& skiplist = (*current).skiplist;
nuclear@0 686
nuclear@0 687 // First step to find possible adjacent contours is to check for adjacent bounding
nuclear@0 688 // boxes. If the bounding boxes are not adjacent, the contours lines cannot possibly be.
nuclear@0 689 for (ContourVector::const_iterator it = contours.begin(), end = contours.end(); it != end; ++it) {
nuclear@0 690 if ((*it).IsInvalid()) {
nuclear@0 691 continue;
nuclear@0 692 }
nuclear@0 693
nuclear@0 694 // this left here to make clear we also run on the current contour
nuclear@0 695 // to check for overlapping contour segments (which can happen due
nuclear@0 696 // to projection artifacts).
nuclear@0 697 //if(it == current) {
nuclear@0 698 // continue;
nuclear@0 699 //}
nuclear@0 700
nuclear@0 701 const bool is_me = it == current;
nuclear@0 702
nuclear@0 703 const BoundingBox& ibb = (*it).bb;
nuclear@0 704
nuclear@0 705 // Assumption: the bounding boxes are pairwise disjoint or identical
nuclear@0 706 ai_assert(is_me || !BoundingBoxesOverlapping(bb, ibb));
nuclear@0 707
nuclear@0 708 if (is_me || BoundingBoxesAdjacent(bb, ibb)) {
nuclear@0 709
nuclear@0 710 // Now do a each-against-everyone check for intersecting contour
nuclear@0 711 // lines. This obviously scales terribly, but in typical real
nuclear@0 712 // world Ifc files it will not matter since most windows that
nuclear@0 713 // are adjacent to each others are rectangular anyway.
nuclear@0 714
nuclear@0 715 Contour& ncontour = (*current).contour;
nuclear@0 716 const Contour& mcontour = (*it).contour;
nuclear@0 717
nuclear@0 718 for (size_t n = 0; n < ncontour.size(); ++n) {
nuclear@0 719 const IfcVector2& n0 = ncontour[n];
nuclear@0 720 const IfcVector2& n1 = ncontour[(n+1) % ncontour.size()];
nuclear@0 721
nuclear@0 722 for (size_t m = 0, mend = (is_me ? n : mcontour.size()); m < mend; ++m) {
nuclear@0 723 ai_assert(&mcontour != &ncontour || m < n);
nuclear@0 724
nuclear@0 725 const IfcVector2& m0 = mcontour[m];
nuclear@0 726 const IfcVector2& m1 = mcontour[(m+1) % mcontour.size()];
nuclear@0 727
nuclear@0 728 IfcVector2 isect0, isect1;
nuclear@0 729 if (IntersectingLineSegments(n0,n1, m0, m1, isect0, isect1)) {
nuclear@0 730
nuclear@0 731 if ((isect0 - n0).SquareLength() > sqlen_epsilon) {
nuclear@0 732 ++n;
nuclear@0 733
nuclear@0 734 ncontour.insert(ncontour.begin() + n, isect0);
nuclear@0 735 skiplist.insert(skiplist.begin() + n, true);
nuclear@0 736 }
nuclear@0 737 else {
nuclear@0 738 skiplist[n] = true;
nuclear@0 739 }
nuclear@0 740
nuclear@0 741 if ((isect1 - n1).SquareLength() > sqlen_epsilon) {
nuclear@0 742 ++n;
nuclear@0 743
nuclear@0 744 ncontour.insert(ncontour.begin() + n, isect1);
nuclear@0 745 skiplist.insert(skiplist.begin() + n, false);
nuclear@0 746 }
nuclear@0 747 }
nuclear@0 748 }
nuclear@0 749 }
nuclear@0 750 }
nuclear@0 751 }
nuclear@0 752 }
nuclear@0 753
nuclear@0 754 // ------------------------------------------------------------------------------------------------
nuclear@0 755 AI_FORCE_INLINE bool LikelyBorder(const IfcVector2& vdelta)
nuclear@0 756 {
nuclear@0 757 const IfcFloat dot_point_epsilon = static_cast<IfcFloat>(1e-5);
nuclear@0 758 return fabs(vdelta.x * vdelta.y) < dot_point_epsilon;
nuclear@0 759 }
nuclear@0 760
nuclear@0 761 // ------------------------------------------------------------------------------------------------
nuclear@0 762 void FindBorderContours(ContourVector::iterator current)
nuclear@0 763 {
nuclear@0 764 const IfcFloat border_epsilon_upper = static_cast<IfcFloat>(1-1e-4);
nuclear@0 765 const IfcFloat border_epsilon_lower = static_cast<IfcFloat>(1e-4);
nuclear@0 766
nuclear@0 767 bool outer_border = false;
nuclear@0 768 bool start_on_outer_border = false;
nuclear@0 769
nuclear@0 770 SkipList& skiplist = (*current).skiplist;
nuclear@0 771 IfcVector2 last_proj_point;
nuclear@0 772
nuclear@0 773 const Contour::const_iterator cbegin = (*current).contour.begin(), cend = (*current).contour.end();
nuclear@0 774
nuclear@0 775 for (Contour::const_iterator cit = cbegin; cit != cend; ++cit) {
nuclear@0 776 const IfcVector2& proj_point = *cit;
nuclear@0 777
nuclear@0 778 // Check if this connection is along the outer boundary of the projection
nuclear@0 779 // plane. In such a case we better drop it because such 'edges' should
nuclear@0 780 // not have any geometry to close them (think of door openings).
nuclear@0 781 if (proj_point.x <= border_epsilon_lower || proj_point.x >= border_epsilon_upper ||
nuclear@0 782 proj_point.y <= border_epsilon_lower || proj_point.y >= border_epsilon_upper) {
nuclear@0 783
nuclear@0 784 if (outer_border) {
nuclear@0 785 ai_assert(cit != cbegin);
nuclear@0 786 if (LikelyBorder(proj_point - last_proj_point)) {
nuclear@0 787 skiplist[std::distance(cbegin, cit) - 1] = true;
nuclear@0 788 }
nuclear@0 789 }
nuclear@0 790 else if (cit == cbegin) {
nuclear@0 791 start_on_outer_border = true;
nuclear@0 792 }
nuclear@0 793
nuclear@0 794 outer_border = true;
nuclear@0 795 }
nuclear@0 796 else {
nuclear@0 797 outer_border = false;
nuclear@0 798 }
nuclear@0 799
nuclear@0 800 last_proj_point = proj_point;
nuclear@0 801 }
nuclear@0 802
nuclear@0 803 // handle last segment
nuclear@0 804 if (outer_border && start_on_outer_border) {
nuclear@0 805 const IfcVector2& proj_point = *cbegin;
nuclear@0 806 if (LikelyBorder(proj_point - last_proj_point)) {
nuclear@0 807 skiplist[skiplist.size()-1] = true;
nuclear@0 808 }
nuclear@0 809 }
nuclear@0 810 }
nuclear@0 811
nuclear@0 812 // ------------------------------------------------------------------------------------------------
nuclear@0 813 AI_FORCE_INLINE bool LikelyDiagonal(IfcVector2 vdelta)
nuclear@0 814 {
nuclear@0 815 vdelta.x = fabs(vdelta.x);
nuclear@0 816 vdelta.y = fabs(vdelta.y);
nuclear@0 817 return (fabs(vdelta.x-vdelta.y) < 0.8 * std::max(vdelta.x, vdelta.y));
nuclear@0 818 }
nuclear@0 819
nuclear@0 820 // ------------------------------------------------------------------------------------------------
nuclear@0 821 void FindLikelyCrossingLines(ContourVector::iterator current)
nuclear@0 822 {
nuclear@0 823 SkipList& skiplist = (*current).skiplist;
nuclear@0 824 IfcVector2 last_proj_point;
nuclear@0 825
nuclear@0 826 const Contour::const_iterator cbegin = (*current).contour.begin(), cend = (*current).contour.end();
nuclear@0 827 for (Contour::const_iterator cit = cbegin; cit != cend; ++cit) {
nuclear@0 828 const IfcVector2& proj_point = *cit;
nuclear@0 829
nuclear@0 830 if (cit != cbegin) {
nuclear@0 831 IfcVector2 vdelta = proj_point - last_proj_point;
nuclear@0 832 if (LikelyDiagonal(vdelta)) {
nuclear@0 833 skiplist[std::distance(cbegin, cit) - 1] = true;
nuclear@0 834 }
nuclear@0 835 }
nuclear@0 836
nuclear@0 837 last_proj_point = proj_point;
nuclear@0 838 }
nuclear@0 839
nuclear@0 840 // handle last segment
nuclear@0 841 if (LikelyDiagonal(*cbegin - last_proj_point)) {
nuclear@0 842 skiplist[skiplist.size()-1] = true;
nuclear@0 843 }
nuclear@0 844 }
nuclear@0 845
nuclear@0 846 // ------------------------------------------------------------------------------------------------
nuclear@0 847 size_t CloseWindows(ContourVector& contours,
nuclear@0 848 const IfcMatrix4& minv,
nuclear@0 849 OpeningRefVector& contours_to_openings,
nuclear@0 850 TempMesh& curmesh)
nuclear@0 851 {
nuclear@0 852 size_t closed = 0;
nuclear@0 853 // For all contour points, check if one of the assigned openings does
nuclear@0 854 // already have points assigned to it. In this case, assume this is
nuclear@0 855 // the other side of the wall and generate connections between
nuclear@0 856 // the two holes in order to close the window.
nuclear@0 857
nuclear@0 858 // All this gets complicated by the fact that contours may pertain to
nuclear@0 859 // multiple openings(due to merging of adjacent or overlapping openings).
nuclear@0 860 // The code is based on the assumption that this happens symmetrically
nuclear@0 861 // on both sides of the wall. If it doesn't (which would be a bug anyway)
nuclear@0 862 // wrong geometry may be generated.
nuclear@0 863 for (ContourVector::iterator it = contours.begin(), end = contours.end(); it != end; ++it) {
nuclear@0 864 if ((*it).IsInvalid()) {
nuclear@0 865 continue;
nuclear@0 866 }
nuclear@0 867 OpeningRefs& refs = contours_to_openings[std::distance(contours.begin(), it)];
nuclear@0 868
nuclear@0 869 bool has_other_side = false;
nuclear@0 870 BOOST_FOREACH(const TempOpening* opening, refs) {
nuclear@0 871 if(!opening->wallPoints.empty()) {
nuclear@0 872 has_other_side = true;
nuclear@0 873 break;
nuclear@0 874 }
nuclear@0 875 }
nuclear@0 876
nuclear@0 877 if (has_other_side) {
nuclear@0 878
nuclear@0 879 ContourRefVector adjacent_contours;
nuclear@0 880
nuclear@0 881 // prepare a skiplist for this contour. The skiplist is used to
nuclear@0 882 // eliminate unwanted contour lines for adjacent windows and
nuclear@0 883 // those bordering the outer frame.
nuclear@0 884 (*it).PrepareSkiplist();
nuclear@0 885
nuclear@0 886 FindAdjacentContours(it, contours);
nuclear@0 887 FindBorderContours(it);
nuclear@0 888
nuclear@0 889 // if the window is the result of a finite union or intersection of rectangles,
nuclear@0 890 // there shouldn't be any crossing or diagonal lines in it. Such lines would
nuclear@0 891 // be artifacts caused by numerical inaccuracies or other bugs in polyclipper
nuclear@0 892 // and our own code. Since rectangular openings are by far the most frequent
nuclear@0 893 // case, it is worth filtering for this corner case.
nuclear@0 894 if((*it).is_rectangular) {
nuclear@0 895 FindLikelyCrossingLines(it);
nuclear@0 896 }
nuclear@0 897
nuclear@0 898 ai_assert((*it).skiplist.size() == (*it).contour.size());
nuclear@0 899
nuclear@0 900 SkipList::const_iterator skipbegin = (*it).skiplist.begin();
nuclear@0 901
nuclear@0 902 curmesh.verts.reserve(curmesh.verts.size() + (*it).contour.size() * 4);
nuclear@0 903 curmesh.vertcnt.reserve(curmesh.vertcnt.size() + (*it).contour.size());
nuclear@0 904
nuclear@0 905 // XXX this algorithm is really a bit inefficient - both in terms
nuclear@0 906 // of constant factor and of asymptotic runtime.
nuclear@0 907 std::vector<bool>::const_iterator skipit = skipbegin;
nuclear@0 908
nuclear@0 909 IfcVector3 start0;
nuclear@0 910 IfcVector3 start1;
nuclear@0 911
nuclear@0 912 IfcVector2 last_proj;
nuclear@0 913 //const IfcVector2& first_proj;
nuclear@0 914
nuclear@0 915 const Contour::const_iterator cbegin = (*it).contour.begin(), cend = (*it).contour.end();
nuclear@0 916
nuclear@0 917 bool drop_this_edge = false;
nuclear@0 918 for (Contour::const_iterator cit = cbegin; cit != cend; ++cit, drop_this_edge = *skipit++) {
nuclear@0 919 const IfcVector2& proj_point = *cit;
nuclear@0 920
nuclear@0 921 // Locate the closest opposite point. This should be a good heuristic to
nuclear@0 922 // connect only the points that are really intended to be connected.
nuclear@0 923 IfcFloat best = static_cast<IfcFloat>(1e10);
nuclear@0 924 IfcVector3 bestv;
nuclear@0 925
nuclear@0 926 /* debug code to check for unwanted diagonal lines in window contours
nuclear@0 927 if (cit != cbegin) {
nuclear@0 928 const IfcVector2& vdelta = proj_point - last_proj;
nuclear@0 929 if (fabs(vdelta.x-vdelta.y) < 0.5 * std::max(vdelta.x, vdelta.y)) {
nuclear@0 930 //continue;
nuclear@0 931 }
nuclear@0 932 } */
nuclear@0 933
nuclear@0 934 const IfcVector3& world_point = minv * IfcVector3(proj_point.x,proj_point.y,0.0f);
nuclear@0 935
nuclear@0 936 last_proj = proj_point;
nuclear@0 937
nuclear@0 938 BOOST_FOREACH(const TempOpening* opening, refs) {
nuclear@0 939 BOOST_FOREACH(const IfcVector3& other, opening->wallPoints) {
nuclear@0 940 const IfcFloat sqdist = (world_point - other).SquareLength();
nuclear@0 941
nuclear@0 942 if (sqdist < best) {
nuclear@0 943 // avoid self-connections
nuclear@0 944 if(sqdist < 1e-5) {
nuclear@0 945 continue;
nuclear@0 946 }
nuclear@0 947
nuclear@0 948 bestv = other;
nuclear@0 949 best = sqdist;
nuclear@0 950 }
nuclear@0 951 }
nuclear@0 952 }
nuclear@0 953
nuclear@0 954 if (drop_this_edge) {
nuclear@0 955 curmesh.verts.pop_back();
nuclear@0 956 curmesh.verts.pop_back();
nuclear@0 957 }
nuclear@0 958 else {
nuclear@0 959 curmesh.verts.push_back(cit == cbegin ? world_point : bestv);
nuclear@0 960 curmesh.verts.push_back(cit == cbegin ? bestv : world_point);
nuclear@0 961
nuclear@0 962 curmesh.vertcnt.push_back(4);
nuclear@0 963 ++closed;
nuclear@0 964 }
nuclear@0 965
nuclear@0 966 if (cit == cbegin) {
nuclear@0 967 start0 = world_point;
nuclear@0 968 start1 = bestv;
nuclear@0 969 continue;
nuclear@0 970 }
nuclear@0 971
nuclear@0 972 curmesh.verts.push_back(world_point);
nuclear@0 973 curmesh.verts.push_back(bestv);
nuclear@0 974
nuclear@0 975 if (cit == cend - 1) {
nuclear@0 976 drop_this_edge = *skipit;
nuclear@0 977
nuclear@0 978 // Check if the final connection (last to first element) is itself
nuclear@0 979 // a border edge that needs to be dropped.
nuclear@0 980 if (drop_this_edge) {
nuclear@0 981 --closed;
nuclear@0 982 curmesh.vertcnt.pop_back();
nuclear@0 983 curmesh.verts.pop_back();
nuclear@0 984 curmesh.verts.pop_back();
nuclear@0 985 }
nuclear@0 986 else {
nuclear@0 987 curmesh.verts.push_back(start1);
nuclear@0 988 curmesh.verts.push_back(start0);
nuclear@0 989 }
nuclear@0 990 }
nuclear@0 991 }
nuclear@0 992 /*
nuclear@0 993 BOOST_FOREACH(TempOpening* opening, refs) {
nuclear@0 994 //opening->wallPoints.clear();
nuclear@0 995 }*/
nuclear@0 996
nuclear@0 997 }
nuclear@0 998 else {
nuclear@0 999
nuclear@0 1000 const Contour::const_iterator cbegin = (*it).contour.begin(), cend = (*it).contour.end();
nuclear@0 1001 BOOST_FOREACH(TempOpening* opening, refs) {
nuclear@0 1002 ai_assert(opening->wallPoints.empty());
nuclear@0 1003 opening->wallPoints.reserve(opening->wallPoints.capacity() + (*it).contour.size());
nuclear@0 1004 for (Contour::const_iterator cit = cbegin; cit != cend; ++cit) {
nuclear@0 1005
nuclear@0 1006 const IfcVector2& proj_point = *cit;
nuclear@0 1007 opening->wallPoints.push_back(minv * IfcVector3(proj_point.x,proj_point.y,0.0f));
nuclear@0 1008 }
nuclear@0 1009 }
nuclear@0 1010 }
nuclear@0 1011 }
nuclear@0 1012 return closed;
nuclear@0 1013 }
nuclear@0 1014
nuclear@0 1015 // ------------------------------------------------------------------------------------------------
nuclear@0 1016 void Quadrify(const std::vector< BoundingBox >& bbs, TempMesh& curmesh)
nuclear@0 1017 {
nuclear@0 1018 ai_assert(curmesh.IsEmpty());
nuclear@0 1019
nuclear@0 1020 std::vector<IfcVector2> quads;
nuclear@0 1021 quads.reserve(bbs.size()*4);
nuclear@0 1022
nuclear@0 1023 // sort openings by x and y axis as a preliminiary to the QuadrifyPart() algorithm
nuclear@0 1024 XYSortedField field;
nuclear@0 1025 for (std::vector<BoundingBox>::const_iterator it = bbs.begin(); it != bbs.end(); ++it) {
nuclear@0 1026 if (field.find((*it).first) != field.end()) {
nuclear@0 1027 IFCImporter::LogWarn("constraint failure during generation of wall openings, results may be faulty");
nuclear@0 1028 }
nuclear@0 1029 field[(*it).first] = std::distance(bbs.begin(),it);
nuclear@0 1030 }
nuclear@0 1031
nuclear@0 1032 QuadrifyPart(IfcVector2(),one_vec,field,bbs,quads);
nuclear@0 1033 ai_assert(!(quads.size() % 4));
nuclear@0 1034
nuclear@0 1035 curmesh.vertcnt.resize(quads.size()/4,4);
nuclear@0 1036 curmesh.verts.reserve(quads.size());
nuclear@0 1037 BOOST_FOREACH(const IfcVector2& v2, quads) {
nuclear@0 1038 curmesh.verts.push_back(IfcVector3(v2.x, v2.y, static_cast<IfcFloat>(0.0)));
nuclear@0 1039 }
nuclear@0 1040 }
nuclear@0 1041
nuclear@0 1042 // ------------------------------------------------------------------------------------------------
nuclear@0 1043 void Quadrify(const ContourVector& contours, TempMesh& curmesh)
nuclear@0 1044 {
nuclear@0 1045 std::vector<BoundingBox> bbs;
nuclear@0 1046 bbs.reserve(contours.size());
nuclear@0 1047
nuclear@0 1048 BOOST_FOREACH(const ContourVector::value_type& val, contours) {
nuclear@0 1049 bbs.push_back(val.bb);
nuclear@0 1050 }
nuclear@0 1051
nuclear@0 1052 Quadrify(bbs, curmesh);
nuclear@0 1053 }
nuclear@0 1054
nuclear@0 1055 // ------------------------------------------------------------------------------------------------
nuclear@0 1056 IfcMatrix4 ProjectOntoPlane(std::vector<IfcVector2>& out_contour, const TempMesh& in_mesh,
nuclear@0 1057 bool &ok, IfcVector3& nor_out)
nuclear@0 1058 {
nuclear@0 1059 const std::vector<IfcVector3>& in_verts = in_mesh.verts;
nuclear@0 1060 ok = true;
nuclear@0 1061
nuclear@0 1062 IfcMatrix4 m = IfcMatrix4(DerivePlaneCoordinateSpace(in_mesh, ok, nor_out));
nuclear@0 1063 if(!ok) {
nuclear@0 1064 return IfcMatrix4();
nuclear@0 1065 }
nuclear@0 1066 #ifdef _DEBUG
nuclear@0 1067 const IfcFloat det = m.Determinant();
nuclear@0 1068 ai_assert(fabs(det-1) < 1e-5);
nuclear@0 1069 #endif
nuclear@0 1070
nuclear@0 1071 IfcFloat zcoord = 0;
nuclear@0 1072 out_contour.reserve(in_verts.size());
nuclear@0 1073
nuclear@0 1074
nuclear@0 1075 IfcVector3 vmin, vmax;
nuclear@0 1076 MinMaxChooser<IfcVector3>()(vmin, vmax);
nuclear@0 1077
nuclear@0 1078 // Project all points into the new coordinate system, collect min/max verts on the way
nuclear@0 1079 BOOST_FOREACH(const IfcVector3& x, in_verts) {
nuclear@0 1080 const IfcVector3& vv = m * x;
nuclear@0 1081 // keep Z offset in the plane coordinate system. Ignoring precision issues
nuclear@0 1082 // (which are present, of course), this should be the same value for
nuclear@0 1083 // all polygon vertices (assuming the polygon is planar).
nuclear@0 1084
nuclear@0 1085 // XXX this should be guarded, but we somehow need to pick a suitable
nuclear@0 1086 // epsilon
nuclear@0 1087 // if(coord != -1.0f) {
nuclear@0 1088 // assert(fabs(coord - vv.z) < 1e-3f);
nuclear@0 1089 // }
nuclear@0 1090 zcoord += vv.z;
nuclear@0 1091 vmin = std::min(vv, vmin);
nuclear@0 1092 vmax = std::max(vv, vmax);
nuclear@0 1093
nuclear@0 1094 out_contour.push_back(IfcVector2(vv.x,vv.y));
nuclear@0 1095 }
nuclear@0 1096
nuclear@0 1097 zcoord /= in_verts.size();
nuclear@0 1098
nuclear@0 1099 // Further improve the projection by mapping the entire working set into
nuclear@0 1100 // [0,1] range. This gives us a consistent data range so all epsilons
nuclear@0 1101 // used below can be constants.
nuclear@0 1102 vmax -= vmin;
nuclear@0 1103 BOOST_FOREACH(IfcVector2& vv, out_contour) {
nuclear@0 1104 vv.x = (vv.x - vmin.x) / vmax.x;
nuclear@0 1105 vv.y = (vv.y - vmin.y) / vmax.y;
nuclear@0 1106
nuclear@0 1107 // sanity rounding
nuclear@0 1108 vv = std::max(vv,IfcVector2());
nuclear@0 1109 vv = std::min(vv,one_vec);
nuclear@0 1110 }
nuclear@0 1111
nuclear@0 1112 IfcMatrix4 mult;
nuclear@0 1113 mult.a1 = static_cast<IfcFloat>(1.0) / vmax.x;
nuclear@0 1114 mult.b2 = static_cast<IfcFloat>(1.0) / vmax.y;
nuclear@0 1115
nuclear@0 1116 mult.a4 = -vmin.x * mult.a1;
nuclear@0 1117 mult.b4 = -vmin.y * mult.b2;
nuclear@0 1118 mult.c4 = -zcoord;
nuclear@0 1119 m = mult * m;
nuclear@0 1120
nuclear@0 1121 // debug code to verify correctness
nuclear@0 1122 #ifdef _DEBUG
nuclear@0 1123 std::vector<IfcVector2> out_contour2;
nuclear@0 1124 BOOST_FOREACH(const IfcVector3& x, in_verts) {
nuclear@0 1125 const IfcVector3& vv = m * x;
nuclear@0 1126
nuclear@0 1127 out_contour2.push_back(IfcVector2(vv.x,vv.y));
nuclear@0 1128 ai_assert(fabs(vv.z) < vmax.z + 1e-8);
nuclear@0 1129 }
nuclear@0 1130
nuclear@0 1131 for(size_t i = 0; i < out_contour.size(); ++i) {
nuclear@0 1132 ai_assert((out_contour[i]-out_contour2[i]).SquareLength() < 1e-6);
nuclear@0 1133 }
nuclear@0 1134 #endif
nuclear@0 1135
nuclear@0 1136 return m;
nuclear@0 1137 }
nuclear@0 1138
nuclear@0 1139 // ------------------------------------------------------------------------------------------------
nuclear@0 1140 bool GenerateOpenings(std::vector<TempOpening>& openings,
nuclear@0 1141 const std::vector<IfcVector3>& nors,
nuclear@0 1142 TempMesh& curmesh,
nuclear@0 1143 bool check_intersection,
nuclear@0 1144 bool generate_connection_geometry,
nuclear@0 1145 const IfcVector3& wall_extrusion_axis)
nuclear@0 1146 {
nuclear@0 1147 OpeningRefVector contours_to_openings;
nuclear@0 1148
nuclear@0 1149 // Try to derive a solid base plane within the current surface for use as
nuclear@0 1150 // working coordinate system. Map all vertices onto this plane and
nuclear@0 1151 // rescale them to [0,1] range. This normalization means all further
nuclear@0 1152 // epsilons need not be scaled.
nuclear@0 1153 bool ok = true;
nuclear@0 1154
nuclear@0 1155 std::vector<IfcVector2> contour_flat;
nuclear@0 1156
nuclear@0 1157 IfcVector3 nor;
nuclear@0 1158 const IfcMatrix4& m = ProjectOntoPlane(contour_flat, curmesh, ok, nor);
nuclear@0 1159 if(!ok) {
nuclear@0 1160 return false;
nuclear@0 1161 }
nuclear@0 1162
nuclear@0 1163 // Obtain inverse transform for getting back to world space later on
nuclear@0 1164 const IfcMatrix4 minv = IfcMatrix4(m).Inverse();
nuclear@0 1165
nuclear@0 1166 // Compute bounding boxes for all 2D openings in projection space
nuclear@0 1167 ContourVector contours;
nuclear@0 1168
nuclear@0 1169 std::vector<IfcVector2> temp_contour;
nuclear@0 1170 std::vector<IfcVector2> temp_contour2;
nuclear@0 1171
nuclear@0 1172 IfcVector3 wall_extrusion_axis_norm = wall_extrusion_axis;
nuclear@0 1173 wall_extrusion_axis_norm.Normalize();
nuclear@0 1174
nuclear@0 1175 BOOST_FOREACH(TempOpening& opening,openings) {
nuclear@0 1176
nuclear@0 1177 // extrusionDir may be 0,0,0 on case where the opening mesh is not an
nuclear@0 1178 // IfcExtrudedAreaSolid but something else (i.e. a brep)
nuclear@0 1179 IfcVector3 norm_extrusion_dir = opening.extrusionDir;
nuclear@0 1180 if (norm_extrusion_dir.SquareLength() > 1e-10) {
nuclear@0 1181 norm_extrusion_dir.Normalize();
nuclear@0 1182 }
nuclear@0 1183 else {
nuclear@0 1184 norm_extrusion_dir = IfcVector3();
nuclear@0 1185 }
nuclear@0 1186
nuclear@0 1187 TempMesh* profile_data = opening.profileMesh.get();
nuclear@0 1188 bool is_2d_source = false;
nuclear@0 1189 if (opening.profileMesh2D && norm_extrusion_dir.SquareLength() > 0) {
nuclear@0 1190
nuclear@0 1191 if(fabs(norm_extrusion_dir * wall_extrusion_axis_norm) < 0.1) {
nuclear@0 1192 // horizontal extrusion
nuclear@0 1193 if (fabs(norm_extrusion_dir * nor) > 0.9) {
nuclear@0 1194 profile_data = opening.profileMesh2D.get();
nuclear@0 1195 is_2d_source = true;
nuclear@0 1196 }
nuclear@0 1197 else {
nuclear@0 1198 //continue;
nuclear@0 1199 }
nuclear@0 1200 }
nuclear@0 1201 else {
nuclear@0 1202 // vertical extrusion
nuclear@0 1203 if (fabs(norm_extrusion_dir * nor) > 0.9) {
nuclear@0 1204 continue;
nuclear@0 1205 }
nuclear@0 1206 continue;
nuclear@0 1207 }
nuclear@0 1208 }
nuclear@0 1209 std::vector<IfcVector3> profile_verts = profile_data->verts;
nuclear@0 1210 std::vector<unsigned int> profile_vertcnts = profile_data->vertcnt;
nuclear@0 1211 if(profile_verts.size() <= 2) {
nuclear@0 1212 continue;
nuclear@0 1213 }
nuclear@0 1214
nuclear@0 1215 // The opening meshes are real 3D meshes so skip over all faces
nuclear@0 1216 // clearly facing into the wrong direction. Also, we need to check
nuclear@0 1217 // whether the meshes do actually intersect the base surface plane.
nuclear@0 1218 // This is done by recording minimum and maximum values for the
nuclear@0 1219 // d component of the plane equation for all polys and checking
nuclear@0 1220 // against surface d.
nuclear@0 1221
nuclear@0 1222 // Use the sign of the dot product of the face normal to the plane
nuclear@0 1223 // normal to determine to which side of the difference mesh a
nuclear@0 1224 // triangle belongs. Get independent bounding boxes and vertex
nuclear@0 1225 // sets for both sides and take the better one (we can't just
nuclear@0 1226 // take both - this would likely cause major screwup of vertex
nuclear@0 1227 // winding, producing errors as late as in CloseWindows()).
nuclear@0 1228 IfcFloat dmin, dmax;
nuclear@0 1229 MinMaxChooser<IfcFloat>()(dmin,dmax);
nuclear@0 1230
nuclear@0 1231 temp_contour.clear();
nuclear@0 1232 temp_contour2.clear();
nuclear@0 1233
nuclear@0 1234 IfcVector2 vpmin,vpmax;
nuclear@0 1235 MinMaxChooser<IfcVector2>()(vpmin,vpmax);
nuclear@0 1236
nuclear@0 1237 IfcVector2 vpmin2,vpmax2;
nuclear@0 1238 MinMaxChooser<IfcVector2>()(vpmin2,vpmax2);
nuclear@0 1239
nuclear@0 1240 for (size_t f = 0, vi_total = 0, fend = profile_vertcnts.size(); f < fend; ++f) {
nuclear@0 1241
nuclear@0 1242 bool side_flag = true;
nuclear@0 1243 if (!is_2d_source) {
nuclear@0 1244 const IfcVector3& face_nor = ((profile_verts[vi_total+2] - profile_verts[vi_total]) ^
nuclear@0 1245 (profile_verts[vi_total+1] - profile_verts[vi_total])).Normalize();
nuclear@0 1246
nuclear@0 1247 const IfcFloat abs_dot_face_nor = abs(nor * face_nor);
nuclear@0 1248 if (abs_dot_face_nor < 0.9) {
nuclear@0 1249 vi_total += profile_vertcnts[f];
nuclear@0 1250 continue;
nuclear@0 1251 }
nuclear@0 1252
nuclear@0 1253 side_flag = nor * face_nor > 0;
nuclear@0 1254 }
nuclear@0 1255
nuclear@0 1256 for (unsigned int vi = 0, vend = profile_vertcnts[f]; vi < vend; ++vi, ++vi_total) {
nuclear@0 1257 const IfcVector3& x = profile_verts[vi_total];
nuclear@0 1258
nuclear@0 1259 const IfcVector3& v = m * x;
nuclear@0 1260 IfcVector2 vv(v.x, v.y);
nuclear@0 1261
nuclear@0 1262 //if(check_intersection) {
nuclear@0 1263 dmin = std::min(dmin, v.z);
nuclear@0 1264 dmax = std::max(dmax, v.z);
nuclear@0 1265 //}
nuclear@0 1266
nuclear@0 1267 // sanity rounding
nuclear@0 1268 vv = std::max(vv,IfcVector2());
nuclear@0 1269 vv = std::min(vv,one_vec);
nuclear@0 1270
nuclear@0 1271 if(side_flag) {
nuclear@0 1272 vpmin = std::min(vpmin,vv);
nuclear@0 1273 vpmax = std::max(vpmax,vv);
nuclear@0 1274 }
nuclear@0 1275 else {
nuclear@0 1276 vpmin2 = std::min(vpmin2,vv);
nuclear@0 1277 vpmax2 = std::max(vpmax2,vv);
nuclear@0 1278 }
nuclear@0 1279
nuclear@0 1280 std::vector<IfcVector2>& store = side_flag ? temp_contour : temp_contour2;
nuclear@0 1281
nuclear@0 1282 if (!IsDuplicateVertex(vv, store)) {
nuclear@0 1283 store.push_back(vv);
nuclear@0 1284 }
nuclear@0 1285 }
nuclear@0 1286 }
nuclear@0 1287
nuclear@0 1288 if (temp_contour2.size() > 2) {
nuclear@0 1289 ai_assert(!is_2d_source);
nuclear@0 1290 const IfcVector2 area = vpmax-vpmin;
nuclear@0 1291 const IfcVector2 area2 = vpmax2-vpmin2;
nuclear@0 1292 if (temp_contour.size() <= 2 || fabs(area2.x * area2.y) > fabs(area.x * area.y)) {
nuclear@0 1293 temp_contour.swap(temp_contour2);
nuclear@0 1294
nuclear@0 1295 vpmax = vpmax2;
nuclear@0 1296 vpmin = vpmin2;
nuclear@0 1297 }
nuclear@0 1298 }
nuclear@0 1299 if(temp_contour.size() <= 2) {
nuclear@0 1300 continue;
nuclear@0 1301 }
nuclear@0 1302
nuclear@0 1303 // TODO: This epsilon may be too large
nuclear@0 1304 const IfcFloat epsilon = fabs(dmax-dmin) * 0.0001;
nuclear@0 1305 if (!is_2d_source && check_intersection && (0 < dmin-epsilon || 0 > dmax+epsilon)) {
nuclear@0 1306 continue;
nuclear@0 1307 }
nuclear@0 1308
nuclear@0 1309 BoundingBox bb = BoundingBox(vpmin,vpmax);
nuclear@0 1310
nuclear@0 1311 // Skip over very small openings - these are likely projection errors
nuclear@0 1312 // (i.e. they don't belong to this side of the wall)
nuclear@0 1313 if(fabs(vpmax.x - vpmin.x) * fabs(vpmax.y - vpmin.y) < static_cast<IfcFloat>(1e-10)) {
nuclear@0 1314 continue;
nuclear@0 1315 }
nuclear@0 1316 std::vector<TempOpening*> joined_openings(1, &opening);
nuclear@0 1317
nuclear@0 1318 bool is_rectangle = temp_contour.size() == 4;
nuclear@0 1319
nuclear@0 1320 // See if this BB intersects or is in close adjacency to any other BB we have so far.
nuclear@0 1321 for (ContourVector::iterator it = contours.begin(); it != contours.end(); ) {
nuclear@0 1322 const BoundingBox& ibb = (*it).bb;
nuclear@0 1323
nuclear@0 1324 if (BoundingBoxesOverlapping(ibb, bb)) {
nuclear@0 1325
nuclear@0 1326 if (!(*it).is_rectangular) {
nuclear@0 1327 is_rectangle = false;
nuclear@0 1328 }
nuclear@0 1329
nuclear@0 1330 const std::vector<IfcVector2>& other = (*it).contour;
nuclear@0 1331 ClipperLib::ExPolygons poly;
nuclear@0 1332
nuclear@0 1333 // First check whether subtracting the old contour (to which ibb belongs)
nuclear@0 1334 // from the new contour (to which bb belongs) yields an updated bb which
nuclear@0 1335 // no longer overlaps ibb
nuclear@0 1336 MakeDisjunctWindowContours(other, temp_contour, poly);
nuclear@0 1337 if(poly.size() == 1) {
nuclear@0 1338
nuclear@0 1339 const BoundingBox& newbb = GetBoundingBox(poly[0].outer);
nuclear@0 1340 if (!BoundingBoxesOverlapping(ibb, newbb )) {
nuclear@0 1341 // Good guy bounding box
nuclear@0 1342 bb = newbb ;
nuclear@0 1343
nuclear@0 1344 ExtractVerticesFromClipper(poly[0].outer, temp_contour, false);
nuclear@0 1345 continue;
nuclear@0 1346 }
nuclear@0 1347 }
nuclear@0 1348
nuclear@0 1349 // Take these two overlapping contours and try to merge them. If they
nuclear@0 1350 // overlap (which should not happen, but in fact happens-in-the-real-
nuclear@0 1351 // world [tm] ), resume using a single contour and a single bounding box.
nuclear@0 1352 MergeWindowContours(temp_contour, other, poly);
nuclear@0 1353
nuclear@0 1354 if (poly.size() > 1) {
nuclear@0 1355 return TryAddOpenings_Poly2Tri(openings, nors, curmesh);
nuclear@0 1356 }
nuclear@0 1357 else if (poly.size() == 0) {
nuclear@0 1358 IFCImporter::LogWarn("ignoring duplicate opening");
nuclear@0 1359 temp_contour.clear();
nuclear@0 1360 break;
nuclear@0 1361 }
nuclear@0 1362 else {
nuclear@0 1363 IFCImporter::LogDebug("merging overlapping openings");
nuclear@0 1364 ExtractVerticesFromClipper(poly[0].outer, temp_contour, false);
nuclear@0 1365
nuclear@0 1366 // Generate the union of the bounding boxes
nuclear@0 1367 bb.first = std::min(bb.first, ibb.first);
nuclear@0 1368 bb.second = std::max(bb.second, ibb.second);
nuclear@0 1369
nuclear@0 1370 // Update contour-to-opening tables accordingly
nuclear@0 1371 if (generate_connection_geometry) {
nuclear@0 1372 std::vector<TempOpening*>& t = contours_to_openings[std::distance(contours.begin(),it)];
nuclear@0 1373 joined_openings.insert(joined_openings.end(), t.begin(), t.end());
nuclear@0 1374
nuclear@0 1375 contours_to_openings.erase(contours_to_openings.begin() + std::distance(contours.begin(),it));
nuclear@0 1376 }
nuclear@0 1377
nuclear@0 1378 contours.erase(it);
nuclear@0 1379
nuclear@0 1380 // Restart from scratch because the newly formed BB might now
nuclear@0 1381 // overlap any other BB which its constituent BBs didn't
nuclear@0 1382 // previously overlap.
nuclear@0 1383 it = contours.begin();
nuclear@0 1384 continue;
nuclear@0 1385 }
nuclear@0 1386 }
nuclear@0 1387 ++it;
nuclear@0 1388 }
nuclear@0 1389
nuclear@0 1390 if(!temp_contour.empty()) {
nuclear@0 1391 if (generate_connection_geometry) {
nuclear@0 1392 contours_to_openings.push_back(std::vector<TempOpening*>(
nuclear@0 1393 joined_openings.begin(),
nuclear@0 1394 joined_openings.end()));
nuclear@0 1395 }
nuclear@0 1396
nuclear@0 1397 contours.push_back(ProjectedWindowContour(temp_contour, bb, is_rectangle));
nuclear@0 1398 }
nuclear@0 1399 }
nuclear@0 1400
nuclear@0 1401 // Check if we still have any openings left - it may well be that this is
nuclear@0 1402 // not the cause, for example if all the opening candidates don't intersect
nuclear@0 1403 // this surface or point into a direction perpendicular to it.
nuclear@0 1404 if (contours.empty()) {
nuclear@0 1405 return false;
nuclear@0 1406 }
nuclear@0 1407
nuclear@0 1408 curmesh.Clear();
nuclear@0 1409
nuclear@0 1410 // Generate a base subdivision into quads to accommodate the given list
nuclear@0 1411 // of window bounding boxes.
nuclear@0 1412 Quadrify(contours,curmesh);
nuclear@0 1413
nuclear@0 1414 // Run a sanity cleanup pass on the window contours to avoid generating
nuclear@0 1415 // artifacts during the contour generation phase later on.
nuclear@0 1416 CleanupWindowContours(contours);
nuclear@0 1417
nuclear@0 1418 // Previously we reduced all windows to rectangular AABBs in projection
nuclear@0 1419 // space, now it is time to fill the gaps between the BBs and the real
nuclear@0 1420 // window openings.
nuclear@0 1421 InsertWindowContours(contours,openings, curmesh);
nuclear@0 1422
nuclear@0 1423 // Clip the entire outer contour of our current result against the real
nuclear@0 1424 // outer contour of the surface. This is necessary because the result
nuclear@0 1425 // of the Quadrify() algorithm is always a square area spanning
nuclear@0 1426 // over [0,1]^2 (i.e. entire projection space).
nuclear@0 1427 CleanupOuterContour(contour_flat, curmesh);
nuclear@0 1428
nuclear@0 1429 // Undo the projection and get back to world (or local object) space
nuclear@0 1430 BOOST_FOREACH(IfcVector3& v3, curmesh.verts) {
nuclear@0 1431 v3 = minv * v3;
nuclear@0 1432 }
nuclear@0 1433
nuclear@0 1434 // Generate window caps to connect the symmetric openings on both sides
nuclear@0 1435 // of the wall.
nuclear@0 1436 if (generate_connection_geometry) {
nuclear@0 1437 CloseWindows(contours, minv, contours_to_openings, curmesh);
nuclear@0 1438 }
nuclear@0 1439 return true;
nuclear@0 1440 }
nuclear@0 1441
nuclear@0 1442 // ------------------------------------------------------------------------------------------------
nuclear@0 1443 bool TryAddOpenings_Poly2Tri(const std::vector<TempOpening>& openings,const std::vector<IfcVector3>& nors,
nuclear@0 1444 TempMesh& curmesh)
nuclear@0 1445 {
nuclear@0 1446 IFCImporter::LogWarn("forced to use poly2tri fallback method to generate wall openings");
nuclear@0 1447 std::vector<IfcVector3>& out = curmesh.verts;
nuclear@0 1448
nuclear@0 1449 bool result = false;
nuclear@0 1450
nuclear@0 1451 // Try to derive a solid base plane within the current surface for use as
nuclear@0 1452 // working coordinate system.
nuclear@0 1453 bool ok;
nuclear@0 1454 IfcVector3 nor;
nuclear@0 1455 const IfcMatrix3& m = DerivePlaneCoordinateSpace(curmesh, ok, nor);
nuclear@0 1456 if (!ok) {
nuclear@0 1457 return false;
nuclear@0 1458 }
nuclear@0 1459
nuclear@0 1460 const IfcMatrix3 minv = IfcMatrix3(m).Inverse();
nuclear@0 1461
nuclear@0 1462
nuclear@0 1463 IfcFloat coord = -1;
nuclear@0 1464
nuclear@0 1465 std::vector<IfcVector2> contour_flat;
nuclear@0 1466 contour_flat.reserve(out.size());
nuclear@0 1467
nuclear@0 1468 IfcVector2 vmin, vmax;
nuclear@0 1469 MinMaxChooser<IfcVector2>()(vmin, vmax);
nuclear@0 1470
nuclear@0 1471 // Move all points into the new coordinate system, collecting min/max verts on the way
nuclear@0 1472 BOOST_FOREACH(IfcVector3& x, out) {
nuclear@0 1473 const IfcVector3 vv = m * x;
nuclear@0 1474
nuclear@0 1475 // keep Z offset in the plane coordinate system. Ignoring precision issues
nuclear@0 1476 // (which are present, of course), this should be the same value for
nuclear@0 1477 // all polygon vertices (assuming the polygon is planar).
nuclear@0 1478
nuclear@0 1479
nuclear@0 1480 // XXX this should be guarded, but we somehow need to pick a suitable
nuclear@0 1481 // epsilon
nuclear@0 1482 // if(coord != -1.0f) {
nuclear@0 1483 // assert(fabs(coord - vv.z) < 1e-3f);
nuclear@0 1484 // }
nuclear@0 1485
nuclear@0 1486 coord = vv.z;
nuclear@0 1487
nuclear@0 1488 vmin = std::min(IfcVector2(vv.x, vv.y), vmin);
nuclear@0 1489 vmax = std::max(IfcVector2(vv.x, vv.y), vmax);
nuclear@0 1490
nuclear@0 1491 contour_flat.push_back(IfcVector2(vv.x,vv.y));
nuclear@0 1492 }
nuclear@0 1493
nuclear@0 1494 // With the current code in DerivePlaneCoordinateSpace,
nuclear@0 1495 // vmin,vmax should always be the 0...1 rectangle (+- numeric inaccuracies)
nuclear@0 1496 // but here we won't rely on this.
nuclear@0 1497
nuclear@0 1498 vmax -= vmin;
nuclear@0 1499
nuclear@0 1500 // If this happens then the projection must have been wrong.
nuclear@0 1501 assert(vmax.Length());
nuclear@0 1502
nuclear@0 1503 ClipperLib::ExPolygons clipped;
nuclear@0 1504 ClipperLib::Polygons holes_union;
nuclear@0 1505
nuclear@0 1506
nuclear@0 1507 IfcVector3 wall_extrusion;
nuclear@0 1508 bool do_connections = false, first = true;
nuclear@0 1509
nuclear@0 1510 try {
nuclear@0 1511
nuclear@0 1512 ClipperLib::Clipper clipper_holes;
nuclear@0 1513 size_t c = 0;
nuclear@0 1514
nuclear@0 1515 BOOST_FOREACH(const TempOpening& t,openings) {
nuclear@0 1516 const IfcVector3& outernor = nors[c++];
nuclear@0 1517 const IfcFloat dot = nor * outernor;
nuclear@0 1518 if (fabs(dot)<1.f-1e-6f) {
nuclear@0 1519 continue;
nuclear@0 1520 }
nuclear@0 1521
nuclear@0 1522 const std::vector<IfcVector3>& va = t.profileMesh->verts;
nuclear@0 1523 if(va.size() <= 2) {
nuclear@0 1524 continue;
nuclear@0 1525 }
nuclear@0 1526
nuclear@0 1527 std::vector<IfcVector2> contour;
nuclear@0 1528
nuclear@0 1529 BOOST_FOREACH(const IfcVector3& xx, t.profileMesh->verts) {
nuclear@0 1530 IfcVector3 vv = m * xx, vv_extr = m * (xx + t.extrusionDir);
nuclear@0 1531
nuclear@0 1532 const bool is_extruded_side = fabs(vv.z - coord) > fabs(vv_extr.z - coord);
nuclear@0 1533 if (first) {
nuclear@0 1534 first = false;
nuclear@0 1535 if (dot > 0.f) {
nuclear@0 1536 do_connections = true;
nuclear@0 1537 wall_extrusion = t.extrusionDir;
nuclear@0 1538 if (is_extruded_side) {
nuclear@0 1539 wall_extrusion = - wall_extrusion;
nuclear@0 1540 }
nuclear@0 1541 }
nuclear@0 1542 }
nuclear@0 1543
nuclear@0 1544 // XXX should not be necessary - but it is. Why? For precision reasons?
nuclear@0 1545 vv = is_extruded_side ? vv_extr : vv;
nuclear@0 1546 contour.push_back(IfcVector2(vv.x,vv.y));
nuclear@0 1547 }
nuclear@0 1548
nuclear@0 1549 ClipperLib::Polygon hole;
nuclear@0 1550 BOOST_FOREACH(IfcVector2& pip, contour) {
nuclear@0 1551 pip.x = (pip.x - vmin.x) / vmax.x;
nuclear@0 1552 pip.y = (pip.y - vmin.y) / vmax.y;
nuclear@0 1553
nuclear@0 1554 hole.push_back(ClipperLib::IntPoint( to_int64(pip.x), to_int64(pip.y) ));
nuclear@0 1555 }
nuclear@0 1556
nuclear@0 1557 if (!ClipperLib::Orientation(hole)) {
nuclear@0 1558 std::reverse(hole.begin(), hole.end());
nuclear@0 1559 // assert(ClipperLib::Orientation(hole));
nuclear@0 1560 }
nuclear@0 1561
nuclear@0 1562 /*ClipperLib::Polygons pol_temp(1), pol_temp2(1);
nuclear@0 1563 pol_temp[0] = hole;
nuclear@0 1564
nuclear@0 1565 ClipperLib::OffsetPolygons(pol_temp,pol_temp2,5.0);
nuclear@0 1566 hole = pol_temp2[0];*/
nuclear@0 1567
nuclear@0 1568 clipper_holes.AddPolygon(hole,ClipperLib::ptSubject);
nuclear@0 1569 }
nuclear@0 1570
nuclear@0 1571 clipper_holes.Execute(ClipperLib::ctUnion,holes_union,
nuclear@0 1572 ClipperLib::pftNonZero,
nuclear@0 1573 ClipperLib::pftNonZero);
nuclear@0 1574
nuclear@0 1575 if (holes_union.empty()) {
nuclear@0 1576 return false;
nuclear@0 1577 }
nuclear@0 1578
nuclear@0 1579 // Now that we have the big union of all holes, subtract it from the outer contour
nuclear@0 1580 // to obtain the final polygon to feed into the triangulator.
nuclear@0 1581 {
nuclear@0 1582 ClipperLib::Polygon poly;
nuclear@0 1583 BOOST_FOREACH(IfcVector2& pip, contour_flat) {
nuclear@0 1584 pip.x = (pip.x - vmin.x) / vmax.x;
nuclear@0 1585 pip.y = (pip.y - vmin.y) / vmax.y;
nuclear@0 1586
nuclear@0 1587 poly.push_back(ClipperLib::IntPoint( to_int64(pip.x), to_int64(pip.y) ));
nuclear@0 1588 }
nuclear@0 1589
nuclear@0 1590 if (ClipperLib::Orientation(poly)) {
nuclear@0 1591 std::reverse(poly.begin(), poly.end());
nuclear@0 1592 }
nuclear@0 1593 clipper_holes.Clear();
nuclear@0 1594 clipper_holes.AddPolygon(poly,ClipperLib::ptSubject);
nuclear@0 1595
nuclear@0 1596 clipper_holes.AddPolygons(holes_union,ClipperLib::ptClip);
nuclear@0 1597 clipper_holes.Execute(ClipperLib::ctDifference,clipped,
nuclear@0 1598 ClipperLib::pftNonZero,
nuclear@0 1599 ClipperLib::pftNonZero);
nuclear@0 1600 }
nuclear@0 1601
nuclear@0 1602 }
nuclear@0 1603 catch (const char* sx) {
nuclear@0 1604 IFCImporter::LogError("Ifc: error during polygon clipping, skipping openings for this face: (Clipper: "
nuclear@0 1605 + std::string(sx) + ")");
nuclear@0 1606
nuclear@0 1607 return false;
nuclear@0 1608 }
nuclear@0 1609
nuclear@0 1610 std::vector<IfcVector3> old_verts;
nuclear@0 1611 std::vector<unsigned int> old_vertcnt;
nuclear@0 1612
nuclear@0 1613 old_verts.swap(curmesh.verts);
nuclear@0 1614 old_vertcnt.swap(curmesh.vertcnt);
nuclear@0 1615
nuclear@0 1616
nuclear@0 1617 // add connection geometry to close the adjacent 'holes' for the openings
nuclear@0 1618 // this should only be done from one side of the wall or the polygons
nuclear@0 1619 // would be emitted twice.
nuclear@0 1620 if (false && do_connections) {
nuclear@0 1621
nuclear@0 1622 std::vector<IfcVector3> tmpvec;
nuclear@0 1623 BOOST_FOREACH(ClipperLib::Polygon& opening, holes_union) {
nuclear@0 1624
nuclear@0 1625 assert(ClipperLib::Orientation(opening));
nuclear@0 1626
nuclear@0 1627 tmpvec.clear();
nuclear@0 1628
nuclear@0 1629 BOOST_FOREACH(ClipperLib::IntPoint& point, opening) {
nuclear@0 1630
nuclear@0 1631 tmpvec.push_back( minv * IfcVector3(
nuclear@0 1632 vmin.x + from_int64(point.X) * vmax.x,
nuclear@0 1633 vmin.y + from_int64(point.Y) * vmax.y,
nuclear@0 1634 coord));
nuclear@0 1635 }
nuclear@0 1636
nuclear@0 1637 for(size_t i = 0, size = tmpvec.size(); i < size; ++i) {
nuclear@0 1638 const size_t next = (i+1)%size;
nuclear@0 1639
nuclear@0 1640 curmesh.vertcnt.push_back(4);
nuclear@0 1641
nuclear@0 1642 const IfcVector3& in_world = tmpvec[i];
nuclear@0 1643 const IfcVector3& next_world = tmpvec[next];
nuclear@0 1644
nuclear@0 1645 // Assumptions: no 'partial' openings, wall thickness roughly the same across the wall
nuclear@0 1646 curmesh.verts.push_back(in_world);
nuclear@0 1647 curmesh.verts.push_back(in_world+wall_extrusion);
nuclear@0 1648 curmesh.verts.push_back(next_world+wall_extrusion);
nuclear@0 1649 curmesh.verts.push_back(next_world);
nuclear@0 1650 }
nuclear@0 1651 }
nuclear@0 1652 }
nuclear@0 1653
nuclear@0 1654 std::vector< std::vector<p2t::Point*> > contours;
nuclear@0 1655 BOOST_FOREACH(ClipperLib::ExPolygon& clip, clipped) {
nuclear@0 1656
nuclear@0 1657 contours.clear();
nuclear@0 1658
nuclear@0 1659 // Build the outer polygon contour line for feeding into poly2tri
nuclear@0 1660 std::vector<p2t::Point*> contour_points;
nuclear@0 1661 BOOST_FOREACH(ClipperLib::IntPoint& point, clip.outer) {
nuclear@0 1662 contour_points.push_back( new p2t::Point(from_int64(point.X), from_int64(point.Y)) );
nuclear@0 1663 }
nuclear@0 1664
nuclear@0 1665 p2t::CDT* cdt ;
nuclear@0 1666 try {
nuclear@0 1667 // Note: this relies on custom modifications in poly2tri to raise runtime_error's
nuclear@0 1668 // instead if assertions. These failures are not debug only, they can actually
nuclear@0 1669 // happen in production use if the input data is broken. An assertion would be
nuclear@0 1670 // inappropriate.
nuclear@0 1671 cdt = new p2t::CDT(contour_points);
nuclear@0 1672 }
nuclear@0 1673 catch(const std::exception& e) {
nuclear@0 1674 IFCImporter::LogError("Ifc: error during polygon triangulation, skipping some openings: (poly2tri: "
nuclear@0 1675 + std::string(e.what()) + ")");
nuclear@0 1676 continue;
nuclear@0 1677 }
nuclear@0 1678
nuclear@0 1679
nuclear@0 1680 // Build the poly2tri inner contours for all holes we got from ClipperLib
nuclear@0 1681 BOOST_FOREACH(ClipperLib::Polygon& opening, clip.holes) {
nuclear@0 1682
nuclear@0 1683 contours.push_back(std::vector<p2t::Point*>());
nuclear@0 1684 std::vector<p2t::Point*>& contour = contours.back();
nuclear@0 1685
nuclear@0 1686 BOOST_FOREACH(ClipperLib::IntPoint& point, opening) {
nuclear@0 1687 contour.push_back( new p2t::Point(from_int64(point.X), from_int64(point.Y)) );
nuclear@0 1688 }
nuclear@0 1689
nuclear@0 1690 cdt->AddHole(contour);
nuclear@0 1691 }
nuclear@0 1692
nuclear@0 1693 try {
nuclear@0 1694 // Note: See above
nuclear@0 1695 cdt->Triangulate();
nuclear@0 1696 }
nuclear@0 1697 catch(const std::exception& e) {
nuclear@0 1698 IFCImporter::LogError("Ifc: error during polygon triangulation, skipping some openings: (poly2tri: "
nuclear@0 1699 + std::string(e.what()) + ")");
nuclear@0 1700 continue;
nuclear@0 1701 }
nuclear@0 1702
nuclear@0 1703 const std::vector<p2t::Triangle*>& tris = cdt->GetTriangles();
nuclear@0 1704
nuclear@0 1705 // Collect the triangles we just produced
nuclear@0 1706 BOOST_FOREACH(p2t::Triangle* tri, tris) {
nuclear@0 1707 for(int i = 0; i < 3; ++i) {
nuclear@0 1708
nuclear@0 1709 const IfcVector2& v = IfcVector2(
nuclear@0 1710 static_cast<IfcFloat>( tri->GetPoint(i)->x ),
nuclear@0 1711 static_cast<IfcFloat>( tri->GetPoint(i)->y )
nuclear@0 1712 );
nuclear@0 1713
nuclear@0 1714 assert(v.x <= 1.0 && v.x >= 0.0 && v.y <= 1.0 && v.y >= 0.0);
nuclear@0 1715 const IfcVector3 v3 = minv * IfcVector3(vmin.x + v.x * vmax.x, vmin.y + v.y * vmax.y,coord) ;
nuclear@0 1716
nuclear@0 1717 curmesh.verts.push_back(v3);
nuclear@0 1718 }
nuclear@0 1719 curmesh.vertcnt.push_back(3);
nuclear@0 1720 }
nuclear@0 1721
nuclear@0 1722 result = true;
nuclear@0 1723 }
nuclear@0 1724
nuclear@0 1725 if (!result) {
nuclear@0 1726 // revert -- it's a shame, but better than nothing
nuclear@0 1727 curmesh.verts.insert(curmesh.verts.end(),old_verts.begin(), old_verts.end());
nuclear@0 1728 curmesh.vertcnt.insert(curmesh.vertcnt.end(),old_vertcnt.begin(), old_vertcnt.end());
nuclear@0 1729
nuclear@0 1730 IFCImporter::LogError("Ifc: revert, could not generate openings for this wall");
nuclear@0 1731 }
nuclear@0 1732
nuclear@0 1733 return result;
nuclear@0 1734 }
nuclear@0 1735
nuclear@0 1736
nuclear@0 1737 } // ! IFC
nuclear@0 1738 } // ! Assimp
nuclear@0 1739
nuclear@0 1740 #undef to_int64
nuclear@0 1741 #undef from_int64
nuclear@0 1742 #undef one_vec
nuclear@0 1743
nuclear@0 1744 #endif