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