Newer
Older
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
\\/ 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 2 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, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\*---------------------------------------------------------------------------*/
#include "treeBoundBox.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
(
vector(-GREAT, -GREAT, -GREAT),
vector(GREAT, GREAT, GREAT)
);
//! @cond - skip documentation : local scope only
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)
);
//! @cond - skip documentation : local scope only
const Foam::label edgesArray[12][2] =
const Foam::edgeList Foam::treeBoundBox::edges
(
initListList<edge, label, 12, 2>(edgesArray)
);
const Foam::FixedList<Foam::vector, 6> Foam::treeBoundBox::faceNormals
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
Foam::FixedList<Foam::vector, 6> Foam::treeBoundBox::calcFaceNormals()
{
FixedList<vector, 6> normals;
normals[LEFT] = vector(-1, 0, 0);
normals[RIGHT] = vector( 1, 0, 0);
normals[BOTTOM] = vector( 0, -1, 0);
normals[TOP] = vector( 0, 1, 0);
normals[BACK] = vector( 0, 0, -1);
normals[FRONT] = vector( 0, 0, 1);
return normals;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
// Construct as the bounding box of the given pointField
Foam::treeBoundBox::treeBoundBox(const UList<point>& points)
:
boundBox()
{
if (points.size() == 0)
{
WarningIn("treeBoundBox::treeBoundBox(const UList<point>&)")
<< "cannot find bounding box for zero sized pointField"
<< "returning zero" << endl;
return;
}
min() = points[0];
max() = points[0];
{
min() = ::Foam::min(min(), points[i]);
max() = ::Foam::max(max(), points[i]);
}
}
// Construct as the bounding box of the given pointField
(
const UList<point>& points,
const labelList& meshPoints
)
"treeBoundBox::treeBoundBox(const UList<point>&, const labelList)"
) << "cannot find bounding box for zero sized pointField"
<< "returning zero" << endl;
min() = points[meshPoints[0]];
max() = points[meshPoints[0]];
for (label i = 1; i < meshPoints.size(); i++)
{
min() = ::Foam::min(min(), points[meshPoints[i]]);
max() = ::Foam::max(max(), points[meshPoints[i]]);
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
{
pointField points(8);
forAll(points, octant)
{
points[octant] = corner(octant);
return points;
}
Foam::treeBoundBox Foam::treeBoundBox::subBbox(const direction octant) const
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
{
if (octant > 7)
{
FatalErrorIn
(
"treeBoundBox::subCube(const direction)"
) << "octant should be [0..7]"
<< abort(FatalError);
}
scalar leftx, lefty, leftz;
scalar rightx, righty, rightz;
scalar midx=0.5*(min().x() + max().x());
scalar midy=0.5*(min().y() + max().y());
scalar midz=0.5*(min().z() + max().z());
// X half
if (octant & treeBoundBox::RIGHTHALF)
{
leftx = midx;
rightx = max().x();
}
else
{
leftx = min().x();
rightx = midx;
}
// Y half
if (octant & treeBoundBox::TOPHALF)
{
lefty = midy;
righty = max().y();
}
else
{
lefty = min().y();
righty = midy;
}
// Z half
if (octant & treeBoundBox::FRONTHALF)
{
leftz = midz;
rightz = max().z();
}
else
{
leftz = min().z();
rightz = midz;
}
return treeBoundBox
(
point(leftx, lefty, leftz),
point(rightx, righty, rightz)
);
}
// Octant to bounding box using permutation only.
Foam::treeBoundBox Foam::treeBoundBox::subBbox
(
const point& mid,
const direction octant
) const
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
{
if (octant > 7)
{
FatalErrorIn
(
"treeBoundBox::subCube(const point&, const direction)"
) << "octant should be [0..7]"
<< abort(FatalError);
}
treeBoundBox subBb;
point& subMin = subBb.min();
point& subMax = subBb.max();
if (octant & treeBoundBox::RIGHTHALF)
{
subMin.x() = mid.x();
subMax.x() = max().x();
}
else
{
subMin.x() = min().x();
subMax.x() = mid.x();
}
if (octant & treeBoundBox::TOPHALF)
{
subMin.y() = mid.y();
subMax.y() = max().y();
}
else
{
subMin.y() = min().y();
subMax.y() = mid.y();
}
if (octant & treeBoundBox::FRONTHALF)
{
subMin.z() = mid.z();
subMax.z() = max().z();
}
else
{
subMin.z() = min().z();
subMax.z() = mid.z();
}
return subBb;
}
// line intersection. Returns true if line (start to end) inside
// bb or intersects bb. Sets pt to intersection.
//
// 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'
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
(
const point& start,
const point& end,
point& pt
) const
{
vector vec(end - start);
pt = start;
const direction endBits = posBits(end);
while(true)
{
direction ptBits = posBits(pt);
if (ptBits == 0)
{
// pt inside bb
return true;
}
if ((ptBits & endBits) != 0)
{
// pt and end in same block outside of bb
return false;
}
if (ptBits & LEFTBIT)
{
// Intersect with plane V=min, n=-1,0,0
if (Foam::mag(vec.x()) > VSMALL)
{
scalar s = (min().x() - pt.x())/vec.x();
pt.x() = min().x();
pt.y() = pt.y() + vec.y()*s;
pt.z() = pt.z() + vec.z()*s;
}
}
if (ptBits & RIGHTBIT)
{
// Intersect with plane V=max, n=1,0,0
if (Foam::mag(vec.x()) > VSMALL)
{
scalar s = (max().x() - pt.x())/vec.x();
pt.x() = max().x();
pt.y() = pt.y() + vec.y()*s;
pt.z() = pt.z() + vec.z()*s;
}
}
{
// Intersect with plane V=min, n=0,-1,0
if (Foam::mag(vec.y()) > VSMALL)
{
scalar s = (min().y() - pt.y())/vec.y();
pt.x() = pt.x() + vec.x()*s;
pt.y() = min().y();
pt.z() = pt.z() + vec.z()*s;
}
}
{
// Intersect with plane V=max, n=0,1,0
if (Foam::mag(vec.y()) > VSMALL)
{
scalar s = (max().y() - pt.y())/vec.y();
pt.x() = pt.x() + vec.x()*s;
pt.y() = max().y();
pt.z() = pt.z() + vec.z()*s;
}
}
{
// Intersect with plane V=min, n=0,0,-1
if (Foam::mag(vec.z()) > VSMALL)
{
scalar s = (min().z() - pt.z())/vec.z();
pt.x() = pt.x() + vec.x()*s;
pt.y() = pt.y() + vec.y()*s;
pt.z() = min().z();
}
}
{
// Intersect with plane V=max, n=0,0,1
if (Foam::mag(vec.z()) > VSMALL)
{
scalar s = (max().z() - pt.z())/vec.z();
pt.x() = pt.x() + vec.x()*s;
pt.y() = pt.y() + vec.y()*s;
pt.z() = max().z();
}
}
}
}
// this.bb fully contains bb
bool Foam::treeBoundBox::contains(const treeBoundBox& bb) const
{
return contains(bb.min()) && contains(bb.max());
}
bool Foam::treeBoundBox::containsNarrow(const point& sample) const
{
return
(
(sample.x() > min().x()) &&
(sample.y() > min().y()) &&
(sample.z() > min().z()) &&
(sample.x() < max().x()) &&
(sample.y() < max().y()) &&
(sample.z() < max().z())
);
}
bool Foam::treeBoundBox::contains(const vector& dir, const point& sample) const
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
{
//
// Compare all components against min and max of bb
//
for (direction cmpt=0; cmpt<3; cmpt++)
{
if (sample[cmpt] < min()[cmpt])
{
return false;
}
else if (sample[cmpt] == min()[cmpt])
{
// On edge. Outside if direction points outwards.
if (dir[cmpt] < 0)
{
return false;
}
}
if (sample[cmpt] > max()[cmpt])
{
return false;
}
else if (sample[cmpt] == max()[cmpt])
{
// On edge. Outside if direction points outwards.
if (dir[cmpt] > 0)
{
return false;
}
}
}
// All components inside bb
return true;
}
// Code position of point relative to box
Foam::direction Foam::treeBoundBox::posBits(const point& pt) const
{
direction posBits = 0;
if (pt.x() < min().x())
{
posBits |= LEFTBIT;
}
if (pt.x() > max().x())
{
posBits |= RIGHTBIT;
}
if (pt.y() < min().y())
{
}
if (pt.y() > max().y())
{
}
if (pt.z() < min().z())
{
}
if (pt.z() > max().z())
{
}
return posBits;
}
// nearest and furthest corner coordinate.
// !names of treeBoundBox::min() and treeBoundBox::max() are confusing!
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
(
const point& sample,
point& nearest,
point& furthest
) const
{
scalar nearX, nearY, nearZ;
scalar farX, farY, farZ;
if (Foam::mag(min().x() - sample.x()) < Foam::mag(max().x() - sample.x()))
{
nearX = min().x();
farX = max().x();
}
else
{
nearX = max().x();
farX = min().x();
}
if (Foam::mag(min().y() - sample.y()) < Foam::mag(max().y() - sample.y()))
{
nearY = min().y();
farY = max().y();
}
else
{
nearY = max().y();
farY = min().y();
}
if (Foam::mag(min().z() - sample.z()) < Foam::mag(max().z() - sample.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& sample) const
{
point near, far;
calcExtremities(sample, near, far);
return Foam::mag(far - sample);
}
// Distance comparator
// Compare all vertices of bounding box against all of other bounding
// box to see if all vertices of one are nearer
(
const point& sample,
const treeBoundBox& other
) const
{
//
// Distance sample <-> nearest and furthest away vertex of this
//
point nearThis, farThis;
// get nearest and furthest away vertex
calcExtremities(sample, nearThis, farThis);
sqr(nearThis.x() - sample.x())
+ sqr(nearThis.y() - sample.y())
+ sqr(nearThis.z() - sample.z());
sqr(farThis.x() - sample.x())
+ sqr(farThis.y() - sample.y())
+ sqr(farThis.z() - sample.z());
//
// Distance sample <-> other
//
point nearOther, farOther;
// get nearest and furthest away vertex
other.calcExtremities(sample, nearOther, farOther);
sqr(nearOther.x() - sample.x())
+ sqr(nearOther.y() - sample.y())
+ sqr(nearOther.z() - sample.z());
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
sqr(farOther.x() - sample.x())
+ sqr(farOther.y() - sample.y())
+ sqr(farOther.z() - sample.z());
//
// Categorize
//
if (maxDistThis < minDistOther)
{
// All vertices of this are nearer to sample than any vertex of other
return -1;
}
else if (minDistThis > maxDistOther)
{
// All vertices of this are further from sample than any vertex of other
return 1;
}
else
{
// Mixed bag
return 0;
}
}
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
bool Foam::operator==(const treeBoundBox& a, const treeBoundBox& b)
{
return (a.min() == b.min()) && (a.max() == b.max());
}
bool Foam::operator!=(const treeBoundBox& a, const treeBoundBox& b)
{
return !(a == b);
}
// * * * * * * * * * * * * * * * IOstream Operator * * * * * * * * * * * * * //
Foam::Istream& Foam::operator>>(Istream& is, treeBoundBox& bb)
{
is >> bb.min() >> bb.max();
return is;
}
// ************************************************************************* //