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 #include "treeLeaf.H"
00030 #include "treeNode.H"
00031 #include "treeBoundBox.H"
00032 #include "octree.H"
00033 #include "HashSet.H"
00034
00035
00036
00037 template <class Type>
00038 void Foam::treeLeaf<Type>::space(Ostream& os, const label n)
00039 {
00040 for (label i=0; i<n; i++)
00041 {
00042 os<< ' ';
00043 }
00044 }
00045
00046
00047
00048
00049
00050 template <class Type>
00051 Foam::treeLeaf<Type>::treeLeaf(const treeBoundBox& bb, const label size)
00052 :
00053 treeElem<Type>(bb), size_(0), indices_(size)
00054 {}
00055
00056
00057
00058 template <class Type>
00059 Foam::treeLeaf<Type>::treeLeaf(const treeBoundBox& bb, const labelList& indices)
00060 :
00061 treeElem<Type>(bb), size_(indices.size()), indices_(indices)
00062 {
00063 }
00064
00065
00066
00067 template <class Type>
00068 Foam::treeLeaf<Type>::treeLeaf(Istream& is)
00069 {
00070 is >> *this;
00071 }
00072
00073
00074
00075
00076 template <class Type>
00077 Foam::treeLeaf<Type>::~treeLeaf()
00078 {}
00079
00080
00081
00082
00083
00084 template <class Type>
00085 Foam::treeLeaf<Type>* Foam::treeLeaf<Type>::redistribute
00086 (
00087 const label level,
00088 octree<Type>& top,
00089 const Type& shapes
00090 )
00091 {
00092 if (debug & 1)
00093 {
00094 space(Pout, level);
00095 Pout<< "treeLeaf::redistribute with bb:" << this->bb() << endl;
00096 }
00097
00098 if (size_ <= top.maxLeafRatio())
00099 {
00100
00101 if (debug & 1)
00102 {
00103 space(Pout, level);
00104 Pout<< "end of treeLeaf::redistribute : small enough" << endl;
00105 }
00106 return this;
00107 }
00108 else
00109 {
00110
00111 treeNode<Type>* treeNodePtr = new treeNode<Type>(this->bb());
00112
00113 top.setNodes(top.nNodes() + 1);
00114
00115 treeNodePtr->distribute
00116 (
00117 level,
00118 top,
00119 shapes,
00120 indices_
00121 );
00122
00123 if (debug & 1)
00124 {
00125 space(Pout, level);
00126 Pout<< "end of treeLeaf::redistribute : done creating node"
00127 << this->bb() << endl;
00128 }
00129
00130
00131 return reinterpret_cast<treeLeaf<Type>*>(treeNodePtr);
00132 }
00133 }
00134
00135
00136
00137 template <class Type>
00138 Foam::label Foam::treeLeaf<Type>::setSubNodeType
00139 (
00140 const label level,
00141 octree<Type>& top,
00142 const Type& shapes
00143 ) const
00144 {
00145 if (size() == 0)
00146 {
00147 FatalErrorIn
00148 (
00149 "treeLeaf<Type>::setSubNodeType(const label, octree<Type>&, "
00150 "const Type&)"
00151 ) << "empty leaf. bb:" << this->bb()
00152 << abort(FatalError);
00153 }
00154 return octree<Type>::MIXED;
00155 }
00156
00157
00158 template <class Type>
00159 Foam::label Foam::treeLeaf<Type>::getSampleType
00160 (
00161 const label level,
00162 const octree<Type>& top,
00163 const Type& shapes,
00164 const point& sample
00165 ) const
00166 {
00167 return shapes.getSampleType(top, sample);
00168 }
00169
00170
00171 template <class Type>
00172 Foam::label Foam::treeLeaf<Type>::find
00173 (
00174 const Type& shapes,
00175 const point& sample
00176 ) const
00177 {
00178 forAll(indices_, i)
00179 {
00180 if (shapes.contains(indices_[i], sample))
00181 {
00182 return indices_[i];
00183 }
00184 }
00185
00186 return -1;
00187 }
00188
00189
00190 template <class Type>
00191 bool Foam::treeLeaf<Type>::findTightest
00192 (
00193 const Type& shapes,
00194 const point& sample,
00195 treeBoundBox& tightest
00196 ) const
00197 {
00198 bool changed = false;
00199
00200 forAll(indices_, i)
00201 {
00202 changed |= shapes.findTightest
00203 (
00204 indices_[i],
00205 sample,
00206 tightest
00207 );
00208 }
00209
00210 return changed;
00211 }
00212
00213
00214 template <class Type>
00215 bool Foam::treeLeaf<Type>::findNearest
00216 (
00217 const Type& shapes,
00218 const point& sample,
00219 treeBoundBox& tightest,
00220 label& tightestI,
00221 scalar& tightestDist
00222 ) const
00223 {
00224 bool changed = false;
00225
00226 forAll(indices_, i)
00227 {
00228 if (shapes.overlaps(indices_[i], tightest))
00229 {
00230 if (debug & 8)
00231 {
00232
00233 Pout<< "treeLeaf<Type>::findNearest : sample:" << sample
00234 << " shape:" << indices_[i] << " overlaps:" << tightest
00235 << endl;
00236 }
00237 point nearest;
00238 scalar thisDist = shapes.calcNearest(indices_[i], sample, nearest);
00239
00240 if (thisDist < tightestDist)
00241 {
00242
00243 point dist(thisDist, thisDist, thisDist);
00244
00245 tightest.min() = sample - dist;
00246 tightest.max() = sample + dist;
00247
00248
00249 tightestI = indices_[i];
00250
00251 tightestDist = thisDist;
00252
00253 changed = true;
00254
00255 if (debug & 8)
00256 {
00257
00258 Pout<< "treeLeaf<Type>::findNearest : Found nearer : shape:"
00259 << tightestI << " distance:" << tightestDist
00260 << " to sample:" << sample << endl;
00261 }
00262 }
00263 }
00264 }
00265
00266 if (changed)
00267 {
00268 if (debug & 8)
00269 {
00270
00271 Pout<< "treeLeaf<Type>::findNearest : sample:" << sample
00272 << " new nearer:" << tightestDist
00273 << endl;
00274 }
00275 }
00276 return changed;
00277 }
00278
00279
00280 template <class Type>
00281 bool Foam::treeLeaf<Type>::findNearest
00282 (
00283 const Type& shapes,
00284 const linePointRef& ln,
00285 treeBoundBox& tightest,
00286 label& tightestI,
00287 point& linePoint,
00288 point& shapePoint
00289 ) const
00290 {
00291
00292 scalar tightestDist = mag(linePoint - shapePoint);
00293
00294 bool changed = false;
00295
00296 forAll(indices_, i)
00297 {
00298 if (shapes.overlaps(indices_[i], tightest))
00299 {
00300
00301 point linePt, shapePt;
00302 scalar thisDist = shapes.calcNearest
00303 (
00304 indices_[i],
00305 ln,
00306 linePt,
00307 shapePt
00308 );
00309
00310 if (thisDist < tightestDist)
00311 {
00312
00313 tightestDist = thisDist;
00314 tightestI = indices_[i];
00315 linePoint = linePt;
00316 shapePoint = shapePt;
00317
00318
00319 vector span(thisDist, thisDist, thisDist);
00320
00321 tightest.min() = min(ln.start(), ln.end()) - span;
00322 tightest.max() = max(ln.start(), ln.end()) + span;
00323
00324 changed = true;
00325 }
00326 }
00327 }
00328
00329 return changed;
00330 }
00331
00332
00333 template <class Type>
00334 bool Foam::treeLeaf<Type>::findBox
00335 (
00336 const Type& shapes,
00337 const boundBox& box,
00338 labelHashSet& elements
00339 ) const
00340 {
00341 bool changed = false;
00342
00343 forAll(indices_, i)
00344 {
00345 if (shapes.overlaps(indices_[i], box))
00346 {
00347 elements.insert(indices_[i]);
00348
00349 changed = true;
00350 }
00351 }
00352
00353 return changed;
00354 }
00355
00356
00357 template <class Type>
00358 void Foam::treeLeaf<Type>::printLeaf
00359 (
00360 Ostream& os,
00361 const label level
00362 ) const
00363 {
00364 space(os, level);
00365
00366 os << "leaf:" << this->bb()
00367 << " number of entries:" << indices().size() << endl;
00368
00369 space(os, level);
00370
00371 os << indices() << endl;
00372 }
00373
00374
00375
00376 template <class Type>
00377 void Foam::treeLeaf<Type>::writeOBJ
00378 (
00379 Ostream& os,
00380 const label level,
00381 label& vertNo
00382 ) const
00383 {
00384 point min = this->bb().min();
00385 point max = this->bb().max();
00386
00387 os << "v " << min.x() << " " << min.y() << " " << min.z() << endl;
00388 os << "v " << max.x() << " " << min.y() << " " << min.z() << endl;
00389 os << "v " << max.x() << " " << max.y() << " " << min.z() << endl;
00390 os << "v " << min.x() << " " << max.y() << " " << min.z() << endl;
00391
00392 os << "v " << min.x() << " " << min.y() << " " << max.z() << endl;
00393 os << "v " << max.x() << " " << min.y() << " " << max.z() << endl;
00394 os << "v " << max.x() << " " << max.y() << " " << max.z() << endl;
00395 os << "v " << min.x() << " " << max.y() << " " << max.z() << endl;
00396
00397 os << "l " << vertNo << " " << vertNo+1 << endl;
00398 os << "l " << vertNo+1 << " " << vertNo+2 << endl;
00399 os << "l " << vertNo+2 << " " << vertNo+3 << endl;
00400 os << "l " << vertNo+3 << " " << vertNo << endl;
00401
00402 os << "l " << vertNo+4 << " " << vertNo+5 << endl;
00403 os << "l " << vertNo+5 << " " << vertNo+6 << endl;
00404 os << "l " << vertNo+6 << " " << vertNo+7 << endl;
00405 os << "l " << vertNo+7 << " " << vertNo << endl;
00406
00407 os << "l " << vertNo << " " << vertNo+4 << endl;
00408 os << "l " << vertNo+1 << " " << vertNo+5 << endl;
00409 os << "l " << vertNo+2 << " " << vertNo+6 << endl;
00410 os << "l " << vertNo+3 << " " << vertNo+7 << endl;
00411
00412 vertNo += 8;
00413 }
00414
00415
00416 template <class Type>
00417 Foam::label Foam::treeLeaf<Type>::countLeaf
00418 (
00419 Ostream& os,
00420 const label level
00421 ) const
00422 {
00423 label nItems = size();
00424
00425 space(os, level);
00426
00427 os << "leaf:" << this->bb() << " has size:" << nItems << endl;
00428
00429 return nItems;
00430 }
00431
00432
00433
00434
00435 template <class Type>
00436 Foam::Istream& Foam::operator>> (Istream& is, treeLeaf<Type>& leaf)
00437 {
00438 is >> leaf.bb() >> leaf.indices_;
00439
00440
00441 leaf.size_ = leaf.indices_.size();
00442 return is;
00443 }
00444
00445
00446 template <class Type>
00447 Foam::Ostream& Foam::operator<< (Ostream& os, const treeLeaf<Type>& leaf)
00448 {
00449 os << leaf.bb();
00450
00451 if (leaf.indices().size() == leaf.size())
00452 {
00453 os << leaf.indices();
00454 }
00455 else
00456 {
00457
00458 os << token::SPACE << leaf.size() << token::SPACE << token::BEGIN_LIST;
00459 for (label i = 0; i < leaf.size(); i++)
00460 {
00461 os << token::SPACE << leaf.indices()[i];
00462 }
00463 os << token::END_LIST;
00464 }
00465 return os;
00466 }
00467
00468
00469