File size: 15,729 Bytes
5a600cf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 | /*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "treeBoundBox.H"
#include "ListOps.H"
#include "OFstream.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const Foam::treeBoundBox Foam::treeBoundBox::greatBox
(
vector::rootMin/2,
vector::rootMax/2
);
const Foam::treeBoundBox Foam::treeBoundBox::invertedBox
(
vector::rootMax/2,
vector::rootMin/2
);
const Foam::label facesArray[6][4] =
{
{0, 4, 6, 2}, // left
{1, 3, 7, 5}, // right
{0, 1, 5, 4}, // bottom
{2, 6, 7, 3}, // top
{0, 2, 3, 1}, // back
{4, 5, 7, 6} // front
};
const Foam::faceList Foam::treeBoundBox::faces
(
initListList<face, label, 6, 4>(facesArray)
);
const Foam::label edgesArray[12][2] =
{
{0, 1}, // 0
{1, 3},
{2, 3}, // 2
{0, 2},
{4, 5}, // 4
{5, 7},
{6, 7}, // 6
{4, 6},
{0, 4}, // 8
{1, 5},
{3, 7}, // 10
{2, 6}
};
const Foam::edgeList Foam::treeBoundBox::edges
(
calcEdges(edgesArray)
);
const Foam::FixedList<Foam::vector, 6> Foam::treeBoundBox::faceNormals
(
calcFaceNormals()
);
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
Foam::edgeList Foam::treeBoundBox::calcEdges(const label edgesArray[12][2])
{
edgeList edges(12);
forAll(edges, edgeI)
{
edges[edgeI][0] = edgesArray[edgeI][0];
edges[edgeI][1] = edgesArray[edgeI][1];
}
return edges;
}
Foam::FixedList<Foam::vector, 6> Foam::treeBoundBox::calcFaceNormals()
{
FixedList<vector, 6> normals;
normals[faceId::left] = vector(-1, 0, 0);
normals[faceId::right] = vector( 1, 0, 0);
normals[faceId::bottom] = vector( 0, -1, 0);
normals[faceId::top] = vector( 0, 1, 0);
normals[faceId::back] = vector( 0, 0, -1);
normals[faceId::front] = vector( 0, 0, 1);
return normals;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::treeBoundBox::treeBoundBox(const UList<point>& points)
:
boundBox(points, false)
{
if (points.empty())
{
WarningInFunction
<< "cannot find bounding box for zero-sized pointField, "
<< "returning zero" << endl;
return;
}
}
Foam::treeBoundBox::treeBoundBox
(
const UList<point>& points,
const labelUList& indices
)
:
boundBox(points, indices, false)
{
if (points.empty() || indices.empty())
{
WarningInFunction
<< "cannot find bounding box for zero-sized pointField, "
<< "returning zero" << endl;
return;
}
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::tmp<Foam::pointField> Foam::treeBoundBox::points() const
{
tmp<pointField> tPts = tmp<pointField>(new pointField(8));
pointField& points = tPts.ref();
forAll(points, octant)
{
points[octant] = corner(octant);
}
return tPts;
}
Foam::treeBoundBox Foam::treeBoundBox::subBbox(const direction octant) const
{
return subBbox(midpoint(), octant);
}
Foam::treeBoundBox Foam::treeBoundBox::subBbox
(
const point& mid,
const direction octant
) const
{
if (octant > 7)
{
FatalErrorInFunction
<< "octant should be [0..7]"
<< abort(FatalError);
}
// start with a copy of this bounding box and adjust limits accordingly
treeBoundBox subBb(*this);
point& bbMin = subBb.min();
point& bbMax = subBb.max();
if (octant & octantBit::rightHalf)
{
bbMin.x() = mid.x(); // mid -> max
}
else
{
bbMax.x() = mid.x(); // min -> mid
}
if (octant & octantBit::topHalf)
{
bbMin.y() = mid.y(); // mid -> max
}
else
{
bbMax.y() = mid.y(); // min -> mid
}
if (octant & octantBit::frontHalf)
{
bbMin.z() = mid.z(); // mid -> max
}
else
{
bbMax.z() = mid.z(); // min -> mid
}
return subBb;
}
bool Foam::treeBoundBox::intersects
(
const point& overallStart,
const vector& overallVec,
const point& start,
const point& end,
point& pt,
direction& ptOnFaces
) const
{
// Sutherlands algorithm:
// loop
// - start = intersection of line with one of the planes bounding
// the bounding box
// - stop if start inside bb (return true)
// - stop if start and end in same 'half' (e.g. both above bb)
// (return false)
//
// Uses posBits to efficiently determine 'half' in which start and end
// point are.
//
// Note:
// - sets coordinate to exact position: e.g. pt.x() = min().x()
// since plane intersect routine might have truncation error.
// This makes sure that posBits tests 'inside'
const direction endBits = posBits(end);
pt = start;
// Allow maximum of 3 clips.
for (label i = 0; i < 4; ++i)
{
direction ptBits = posBits(pt);
if (ptBits == 0)
{
// pt inside bb
ptOnFaces = faceBits(pt);
return true;
}
if ((ptBits & endBits) != 0)
{
// pt and end in same block outside of bb
ptOnFaces = faceBits(pt);
return false;
}
if (ptBits & faceBit::left)
{
// Intersect with plane V=min, n=-1,0,0
if (Foam::mag(overallVec.x()) > vSmall)
{
scalar s = (min().x() - overallStart.x())/overallVec.x();
pt.x() = min().x();
pt.y() = overallStart.y() + overallVec.y()*s;
pt.z() = overallStart.z() + overallVec.z()*s;
}
else
{
// Vector not in x-direction. But still intersecting bb planes.
// So must be close - just snap to bb.
pt.x() = min().x();
}
}
else if (ptBits & faceBit::right)
{
// Intersect with plane V=max, n=1,0,0
if (Foam::mag(overallVec.x()) > vSmall)
{
scalar s = (max().x() - overallStart.x())/overallVec.x();
pt.x() = max().x();
pt.y() = overallStart.y() + overallVec.y()*s;
pt.z() = overallStart.z() + overallVec.z()*s;
}
else
{
pt.x() = max().x();
}
}
else if (ptBits & faceBit::bottom)
{
// Intersect with plane V=min, n=0,-1,0
if (Foam::mag(overallVec.y()) > vSmall)
{
scalar s = (min().y() - overallStart.y())/overallVec.y();
pt.x() = overallStart.x() + overallVec.x()*s;
pt.y() = min().y();
pt.z() = overallStart.z() + overallVec.z()*s;
}
else
{
pt.x() = min().y();
}
}
else if (ptBits & faceBit::top)
{
// Intersect with plane V=max, n=0,1,0
if (Foam::mag(overallVec.y()) > vSmall)
{
scalar s = (max().y() - overallStart.y())/overallVec.y();
pt.x() = overallStart.x() + overallVec.x()*s;
pt.y() = max().y();
pt.z() = overallStart.z() + overallVec.z()*s;
}
else
{
pt.y() = max().y();
}
}
else if (ptBits & faceBit::back)
{
// Intersect with plane V=min, n=0,0,-1
if (Foam::mag(overallVec.z()) > vSmall)
{
scalar s = (min().z() - overallStart.z())/overallVec.z();
pt.x() = overallStart.x() + overallVec.x()*s;
pt.y() = overallStart.y() + overallVec.y()*s;
pt.z() = min().z();
}
else
{
pt.z() = min().z();
}
}
else if (ptBits & faceBit::front)
{
// Intersect with plane V=max, n=0,0,1
if (Foam::mag(overallVec.z()) > vSmall)
{
scalar s = (max().z() - overallStart.z())/overallVec.z();
pt.x() = overallStart.x() + overallVec.x()*s;
pt.y() = overallStart.y() + overallVec.y()*s;
pt.z() = max().z();
}
else
{
pt.z() = max().z();
}
}
}
// Can end up here if the end point is on the edge of the boundBox
return true;
}
bool Foam::treeBoundBox::intersects
(
const point& start,
const point& end,
point& pt
) const
{
direction ptBits;
return intersects(start, end-start, start, end, pt, ptBits);
}
bool Foam::treeBoundBox::contains(const vector& dir, const point& pt) const
{
// Compare all components against min and max of bb
for (direction cmpt=0; cmpt<3; cmpt++)
{
if (pt[cmpt] < min()[cmpt])
{
return false;
}
else if (pt[cmpt] == min()[cmpt])
{
// On edge. Outside if direction points outwards.
if (dir[cmpt] < 0)
{
return false;
}
}
if (pt[cmpt] > max()[cmpt])
{
return false;
}
else if (pt[cmpt] == max()[cmpt])
{
// On edge. Outside if direction points outwards.
if (dir[cmpt] > 0)
{
return false;
}
}
}
// All components inside bb
return true;
}
Foam::direction Foam::treeBoundBox::faceBits(const point& pt) const
{
direction faceBits = 0;
if (pt.x() == min().x())
{
faceBits |= faceBit::left;
}
else if (pt.x() == max().x())
{
faceBits |= faceBit::right;
}
if (pt.y() == min().y())
{
faceBits |= faceBit::bottom;
}
else if (pt.y() == max().y())
{
faceBits |= faceBit::top;
}
if (pt.z() == min().z())
{
faceBits |= faceBit::back;
}
else if (pt.z() == max().z())
{
faceBits |= faceBit::front;
}
return faceBits;
}
Foam::direction Foam::treeBoundBox::posBits(const point& pt) const
{
direction posBits = 0;
if (pt.x() < min().x())
{
posBits |= faceBit::left;
}
else if (pt.x() > max().x())
{
posBits |= faceBit::right;
}
if (pt.y() < min().y())
{
posBits |= faceBit::bottom;
}
else if (pt.y() > max().y())
{
posBits |= faceBit::top;
}
if (pt.z() < min().z())
{
posBits |= faceBit::back;
}
else if (pt.z() > max().z())
{
posBits |= faceBit::front;
}
return posBits;
}
void Foam::treeBoundBox::calcExtremities
(
const point& pt,
point& nearest,
point& furthest
) const
{
scalar nearX, nearY, nearZ;
scalar farX, farY, farZ;
if (Foam::mag(min().x() - pt.x()) < Foam::mag(max().x() - pt.x()))
{
nearX = min().x();
farX = max().x();
}
else
{
nearX = max().x();
farX = min().x();
}
if (Foam::mag(min().y() - pt.y()) < Foam::mag(max().y() - pt.y()))
{
nearY = min().y();
farY = max().y();
}
else
{
nearY = max().y();
farY = min().y();
}
if (Foam::mag(min().z() - pt.z()) < Foam::mag(max().z() - pt.z()))
{
nearZ = min().z();
farZ = max().z();
}
else
{
nearZ = max().z();
farZ = min().z();
}
nearest = point(nearX, nearY, nearZ);
furthest = point(farX, farY, farZ);
}
Foam::scalar Foam::treeBoundBox::maxDist(const point& pt) const
{
point near, far;
calcExtremities(pt, near, far);
return Foam::mag(far - pt);
}
Foam::label Foam::treeBoundBox::distanceCmp
(
const point& pt,
const treeBoundBox& other
) const
{
//
// Distance point <-> nearest and furthest away vertex of this
//
point nearThis, farThis;
// get nearest and furthest away vertex
calcExtremities(pt, nearThis, farThis);
const scalar minDistThis =
sqr(nearThis.x() - pt.x())
+ sqr(nearThis.y() - pt.y())
+ sqr(nearThis.z() - pt.z());
const scalar maxDistThis =
sqr(farThis.x() - pt.x())
+ sqr(farThis.y() - pt.y())
+ sqr(farThis.z() - pt.z());
//
// Distance point <-> other
//
point nearOther, farOther;
// get nearest and furthest away vertex
other.calcExtremities(pt, nearOther, farOther);
const scalar minDistOther =
sqr(nearOther.x() - pt.x())
+ sqr(nearOther.y() - pt.y())
+ sqr(nearOther.z() - pt.z());
const scalar maxDistOther =
sqr(farOther.x() - pt.x())
+ sqr(farOther.y() - pt.y())
+ sqr(farOther.z() - pt.z());
//
// Categorise
//
if (maxDistThis < minDistOther)
{
// All vertices of this are nearer to point than any vertex of other
return -1;
}
else if (minDistThis > maxDistOther)
{
// All vertices of this are further from point than any vertex of other
return 1;
}
else
{
// Mixed bag
return 0;
}
}
void Foam::treeBoundBox::writeOBJ(const fileName& fName) const
{
OFstream str(fName);
Info<< "Dumping bounding box " << *this << " as lines to obj file "
<< str.name() << endl;
pointField bbPoints(points());
forAll(bbPoints, i)
{
const point& pt = bbPoints[i];
str<< "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << endl;
}
forAll(treeBoundBox::edges, i)
{
const edge& e = treeBoundBox::edges[i];
str<< "l " << e[0]+1 << ' ' << e[1]+1 << nl;
}
}
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
bool Foam::operator==(const treeBoundBox& a, const treeBoundBox& b)
{
return operator==
(
static_cast<const boundBox&>(a),
static_cast<const boundBox&>(b)
);
}
bool Foam::operator!=(const treeBoundBox& a, const treeBoundBox& b)
{
return !(a == b);
}
// * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
Foam::Ostream& Foam::operator<<(Ostream& os, const treeBoundBox& bb)
{
return os << static_cast<const boundBox&>(bb);
}
Foam::Istream& Foam::operator>>(Istream& is, treeBoundBox& bb)
{
return is >> static_cast<boundBox&>(bb);
}
// ************************************************************************* //
|