00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef indexedOctree_H
00037 #define indexedOctree_H
00038
00039 #include "treeBoundBox.H"
00040 #include "pointIndexHit.H"
00041 #include "FixedList.H"
00042 #include "Ostream.H"
00043 #include "HashSet.H"
00044 #include "labelBits.H"
00045 #include "PackedList.H"
00046
00047
00048
00049 namespace Foam
00050 {
00051
00052
00053 template<class Type> class indexedOctree;
00054 template<class Type> Ostream& operator<<(Ostream&, const indexedOctree<Type>&);
00055 class Istream;
00056
00057
00058
00059
00060
00061
00062 TemplateName(indexedOctree);
00063
00064
00065
00066
00067
00068
00069 template <class Type>
00070 class indexedOctree
00071 :
00072 public indexedOctreeName
00073 {
00074 public:
00075
00076
00077
00078
00079 enum volumeType
00080 {
00081 UNKNOWN = 0,
00082 MIXED = 1,
00083 INSIDE = 2,
00084 OUTSIDE = 3
00085 };
00086
00087
00088
00089
00090 class node
00091 {
00092 public:
00093
00094
00095 treeBoundBox bb_;
00096
00097
00098 label parent_;
00099
00100
00101 FixedList<labelBits, 8> subNodes_;
00102
00103 friend Ostream& operator<< (Ostream& os, const node& n)
00104 {
00105 return os << n.bb_ << token::SPACE
00106 << n.parent_ << token::SPACE << n.subNodes_;
00107 }
00108
00109 friend Istream& operator>> (Istream& is, node& n)
00110 {
00111 return is >> n.bb_ >> n.parent_ >> n.subNodes_;
00112 }
00113
00114 friend bool operator==(const node& a, const node& b)
00115 {
00116 return
00117 a.bb_ == b.bb_
00118 && a.parent_ == b.parent_
00119 && a.subNodes_ == b.subNodes_;
00120 }
00121
00122 friend bool operator!=(const node& a, const node& b)
00123 {
00124 return !(a == b);
00125 }
00126 };
00127
00128
00129 private:
00130
00131
00132
00133
00134
00135
00136
00137 static scalar perturbTol_;
00138
00139
00140
00141
00142
00143 const Type shapes_;
00144
00145
00146 List<node> nodes_;
00147
00148
00149 labelListList contents_;
00150
00151
00152 mutable PackedList<2> nodeTypes_;
00153
00154
00155
00156
00157
00158
00159 static bool overlaps
00160 (
00161 const treeBoundBox& parentBb,
00162 const direction octant,
00163 const scalar nearestDistSqr,
00164 const point& sample
00165 );
00166
00167
00168
00169
00170
00171 void divide
00172 (
00173 const labelList& indices,
00174 const treeBoundBox& bb,
00175 labelListList& result
00176 ) const;
00177
00178
00179
00180 node divide
00181 (
00182 const treeBoundBox& bb,
00183 DynamicList<labelList>& contents,
00184 const label contentI
00185 ) const;
00186
00187
00188 void splitNodes
00189 (
00190 const label minSize,
00191 DynamicList<node>& nodes,
00192 DynamicList<labelList>& contents
00193 ) const;
00194
00195 static label compactContents
00196 (
00197 DynamicList<node>& nodes,
00198 DynamicList<labelList>& contents,
00199 const label compactLevel,
00200 const label nodeI,
00201 const label level,
00202 List<labelList>& compactedContents,
00203 label& compactI
00204 );
00205
00206
00207
00208 volumeType calcVolumeType(const label nodeI) const;
00209
00210
00211 volumeType getVolumeType(const label nodeI, const point&) const;
00212
00213
00214
00215
00216
00217 void findNearest
00218 (
00219 const label nodeI,
00220 const linePointRef& ln,
00221
00222 treeBoundBox& tightest,
00223 label& nearestShapeI,
00224 point& linePoint,
00225 point& nearestPoint
00226 ) const;
00227
00228
00229 treeBoundBox subBbox
00230 (
00231 const label parentNodeI,
00232 const direction octant
00233 ) const;
00234
00235
00236
00237 static point pushPoint
00238 (
00239 const treeBoundBox&,
00240 const point&,
00241 const bool pushInside
00242 );
00243
00244
00245
00246 static point pushPoint
00247 (
00248 const treeBoundBox&,
00249 const direction,
00250 const point&,
00251 const bool pushInside
00252 );
00253
00254
00255
00256 static point pushPointIntoFace
00257 (
00258 const treeBoundBox& bb,
00259 const vector& dir,
00260 const point& pt
00261 );
00262
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 bool walkToParent
00274 (
00275 const label nodeI,
00276 const direction octant,
00277
00278 label& parentNodeI,
00279 label& parentOctant
00280 ) const;
00281
00282
00283
00284 bool walkToNeighbour
00285 (
00286 const point& facePoint,
00287 const direction faceID,
00288 label& nodeI,
00289 direction& octant
00290 ) const;
00291
00292
00293 static word faceString(const direction faceID);
00294
00295
00296
00297
00298
00299 void traverseNode
00300 (
00301 const bool findAny,
00302 const point& treeStart,
00303 const vector& treeVec,
00304
00305 const point& start,
00306 const point& end,
00307 const label nodeI,
00308 const direction octantI,
00309
00310 pointIndexHit& hitInfo,
00311 direction& faceID
00312 ) const;
00313
00314
00315 pointIndexHit findLine
00316 (
00317 const bool findAny,
00318 const point& treeStart,
00319 const point& treeEnd,
00320 const label startNodeI,
00321 const direction startOctantI,
00322 const bool verbose = false
00323 ) const;
00324
00325
00326 pointIndexHit findLine
00327 (
00328 const bool findAny,
00329 const point& start,
00330 const point& end
00331 ) const;
00332
00333
00334 void findBox
00335 (
00336 const label nodeI,
00337 const treeBoundBox& searchBox,
00338 labelHashSet& elements
00339 ) const;
00340
00341
00342
00343
00344 label countElements(const labelBits index) const;
00345
00346
00347 void writeOBJ(const label nodeI, const direction octant) const;
00348
00349
00350 static labelBits contentPlusOctant
00351 (
00352 const label i,
00353 const direction octant
00354 )
00355 {
00356 return labelBits(-i - 1, octant);
00357 }
00358
00359
00360 static labelBits nodePlusOctant
00361 (
00362 const label i,
00363 const direction octant
00364 )
00365 {
00366 return labelBits(i + 1, octant);
00367 }
00368
00369
00370 static labelBits emptyPlusOctant
00371 (
00372 const direction octant
00373 )
00374 {
00375 return labelBits(0, octant);
00376 }
00377
00378 public:
00379
00380
00381 static scalar& perturbTol();
00382
00383
00384
00385
00386
00387 indexedOctree(const Type& shapes);
00388
00389
00390 indexedOctree
00391 (
00392 const Type& shapes,
00393 const List<node>& nodes,
00394 const labelListList& contents
00395 );
00396
00397
00398 indexedOctree
00399 (
00400 const Type& shapes,
00401 const treeBoundBox& bb,
00402 const label maxLevels,
00403 const scalar maxLeafRatio,
00404 const scalar maxDuplicity
00405
00406 );
00407
00408
00409 indexedOctree(const Type& shapes, Istream& is);
00410
00411
00412 autoPtr<indexedOctree<Type> > clone() const
00413 {
00414 return autoPtr<indexedOctree<Type> >
00415 (
00416 new indexedOctree<Type>(*this)
00417 );
00418 }
00419
00420
00421
00422
00423
00424
00425 const Type& shapes() const
00426 {
00427 return shapes_;
00428 }
00429
00430
00431 const List<node>& nodes() const
00432 {
00433 return nodes_;
00434 }
00435
00436
00437
00438 const labelListList& contents() const
00439 {
00440 return contents_;
00441 }
00442
00443
00444 const treeBoundBox& bb() const
00445 {
00446 if (nodes_.empty())
00447 {
00448 FatalErrorIn("indexedOctree<Type>::bb() const")
00449 << "Tree is empty" << abort(FatalError);
00450 }
00451 return nodes_[0].bb_;
00452 }
00453
00454
00455
00456
00457 static bool isContent(const labelBits i)
00458 {
00459 return i.val() < 0;
00460 }
00461
00462 static bool isEmpty(const labelBits i)
00463 {
00464 return i.val() == 0;
00465 }
00466
00467 static bool isNode(const labelBits i)
00468 {
00469 return i.val() > 0;
00470 }
00471
00472 static label getContent(const labelBits i)
00473 {
00474 if (!isContent(i))
00475 {
00476 FatalErrorIn("getContent(const label)")
00477 << abort(FatalError);
00478 }
00479 return -i.val()-1;
00480 }
00481
00482 static label getNode(const labelBits i)
00483 {
00484 if (!isNode(i))
00485 {
00486 FatalErrorIn("getNode(const label)")
00487 << abort(FatalError);
00488 }
00489 return i.val() - 1;
00490 }
00491
00492 static direction getOctant(const labelBits i)
00493 {
00494 return i.bits();
00495 }
00496
00497
00498
00499
00500
00501
00502
00503
00504 pointIndexHit findNearest
00505 (
00506 const point& sample,
00507 const scalar nearestDistSqr
00508 ) const;
00509
00510
00511 void findNearest
00512 (
00513 const label nodeI,
00514 const point&,
00515
00516 scalar& nearestDistSqr,
00517 label& nearestShapeI,
00518 point& nearestPoint
00519 ) const;
00520
00521
00522
00523
00524
00525
00526
00527 pointIndexHit findNearest
00528 (
00529 const linePointRef& ln,
00530 treeBoundBox& tightest,
00531 point& linePoint
00532 ) const;
00533
00534
00535 pointIndexHit findLine
00536 (
00537 const point& start,
00538 const point& end
00539 ) const;
00540
00541
00542 pointIndexHit findLineAny
00543 (
00544 const point& start,
00545 const point& end
00546 ) const;
00547
00548
00549
00550 labelList findBox(const boundBox& bb) const;
00551
00552
00553
00554
00555 labelBits findNode(const label nodeI, const point&) const;
00556
00557
00558
00559 volumeType getVolumeType(const point&) const;
00560
00561
00562
00563 static volumeType getSide
00564 (
00565 const vector& outsideNormal,
00566 const vector& vec
00567 );
00568
00569
00570
00571 static bool overlaps
00572 (
00573 const point& bbMin,
00574 const point& bbMax,
00575 const scalar nearestDistSqr,
00576 const point& sample
00577 );
00578
00579
00580
00581
00582
00583
00584 void print
00585 (
00586 prefixOSstream&,
00587 const bool printContents,
00588 const label
00589 ) const;
00590
00591 bool write(Ostream& os) const;
00592
00593
00594
00595 friend Ostream& operator<< <Type>(Ostream&, const indexedOctree<Type>&);
00596
00597 };
00598
00599
00600
00601
00602 }
00603
00604
00605
00606 #ifdef NoRepository
00607 # include "indexedOctree.C"
00608 #endif
00609
00610
00611
00612 #endif
00613
00614