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 #include "treeDataFace.H"
00028 #include "polyMesh.H"
00029 #include "triangleFuncs.H"
00030
00031
00032
00033 defineTypeNameAndDebug(Foam::treeDataFace, 0);
00034
00035 Foam::scalar Foam::treeDataFace::tolSqr = sqr(1E-6);
00036
00037
00038
00039
00040 Foam::treeBoundBox Foam::treeDataFace::calcBb(const label faceI) const
00041 {
00042 const pointField& points = mesh_.points();
00043
00044 const face& f = mesh_.faces()[faceI];
00045
00046 treeBoundBox bb(points[f[0]], points[f[0]]);
00047
00048 for (label fp = 1; fp < f.size(); fp++)
00049 {
00050 const point& p = points[f[fp]];
00051
00052 bb.min() = min(bb.min(), p);
00053 bb.max() = max(bb.max(), p);
00054 }
00055 return bb;
00056 }
00057
00058
00059 void Foam::treeDataFace::update()
00060 {
00061 forAll(faceLabels_, i)
00062 {
00063 isTreeFace_.set(faceLabels_[i], 1);
00064 }
00065
00066 if (cacheBb_)
00067 {
00068 bbs_.setSize(faceLabels_.size());
00069
00070 forAll(faceLabels_, i)
00071 {
00072 bbs_[i] = calcBb(faceLabels_[i]);
00073 }
00074 }
00075 }
00076
00077
00078
00079
00080
00081 Foam::treeDataFace::treeDataFace
00082 (
00083 const bool cacheBb,
00084 const primitiveMesh& mesh,
00085 const labelList& faceLabels
00086 )
00087 :
00088 mesh_(mesh),
00089 faceLabels_(faceLabels),
00090 isTreeFace_(mesh.nFaces(), 0),
00091 cacheBb_(cacheBb)
00092 {
00093 update();
00094 }
00095
00096
00097 Foam::treeDataFace::treeDataFace
00098 (
00099 const bool cacheBb,
00100 const primitiveMesh& mesh
00101 )
00102 :
00103 mesh_(mesh),
00104 faceLabels_(identity(mesh_.nFaces())),
00105 isTreeFace_(mesh.nFaces(), 0),
00106 cacheBb_(cacheBb)
00107 {
00108 update();
00109 }
00110
00111
00112 Foam::treeDataFace::treeDataFace
00113 (
00114 const bool cacheBb,
00115 const polyPatch& patch
00116 )
00117 :
00118 mesh_(patch.boundaryMesh().mesh()),
00119 faceLabels_
00120 (
00121 identity(patch.size())
00122 + patch.start()
00123 ),
00124 isTreeFace_(mesh_.nFaces(), 0),
00125 cacheBb_(cacheBb)
00126 {
00127 update();
00128 }
00129
00130
00131
00132
00133 Foam::pointField Foam::treeDataFace::points() const
00134 {
00135 pointField cc(faceLabels_.size());
00136
00137 forAll(faceLabels_, i)
00138 {
00139 cc[i] = mesh_.faceCentres()[faceLabels_[i]];
00140 }
00141
00142 return cc;
00143 }
00144
00145
00146
00147
00148 Foam::label Foam::treeDataFace::getVolumeType
00149 (
00150 const indexedOctree<treeDataFace>& oc,
00151 const point& sample
00152 ) const
00153 {
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 pointIndexHit info = oc.findNearest(sample, sqr(GREAT));
00167
00168 if (info.index() == -1)
00169 {
00170 FatalErrorIn
00171 (
00172 "treeDataFace::getSampleType"
00173 "(indexedOctree<treeDataFace>&, const point&)"
00174 ) << "Could not find " << sample << " in octree."
00175 << abort(FatalError);
00176 }
00177
00178
00179
00180 label faceI = faceLabels_[info.index()];
00181
00182 if (debug & 2)
00183 {
00184 Pout<< "getSampleType : sample:" << sample
00185 << " nearest face:" << faceI;
00186 }
00187
00188 const pointField& points = mesh_.points();
00189
00190
00191
00192
00193 const face& f = mesh_.faces()[faceI];
00194 const vector& area = mesh_.faceAreas()[faceI];
00195 const point& fc = mesh_.faceCentres()[faceI];
00196
00197 pointHit curHit = f.nearestPoint(sample, points);
00198 const point& curPt = curHit.rawPoint();
00199
00200
00201
00202
00203
00204 if (curHit.hit())
00205 {
00206
00207
00208 if (debug & 2)
00209 {
00210 Pout<< " -> face hit:" << curPt
00211 << " comparing to face normal " << area << endl;
00212 }
00213 return indexedOctree<treeDataFace>::getSide(area, sample - curPt);
00214 }
00215
00216 if (debug & 2)
00217 {
00218 Pout<< " -> face miss:" << curPt;
00219 }
00220
00221
00222
00223
00224
00225
00226 const scalar typDimSqr = mag(area) + VSMALL;
00227
00228 forAll(f, fp)
00229 {
00230 if ((magSqr(points[f[fp]] - curPt)/typDimSqr) < tolSqr)
00231 {
00232
00233
00234
00235
00236 const labelList& pFaces = mesh_.pointFaces()[f[fp]];
00237
00238 vector pointNormal(vector::zero);
00239
00240 forAll(pFaces, i)
00241 {
00242 if (isTreeFace_.get(pFaces[i]) == 1)
00243 {
00244 vector n = mesh_.faceAreas()[pFaces[i]];
00245 n /= mag(n) + VSMALL;
00246
00247 pointNormal += n;
00248 }
00249 }
00250
00251 if (debug & 2)
00252 {
00253 Pout<< " -> face point hit :" << points[f[fp]]
00254 << " point normal:" << pointNormal
00255 << " distance:"
00256 << magSqr(points[f[fp]] - curPt)/typDimSqr << endl;
00257 }
00258 return indexedOctree<treeDataFace>::getSide
00259 (
00260 pointNormal,
00261 sample - curPt
00262 );
00263 }
00264 }
00265 if ((magSqr(fc - curPt)/typDimSqr) < tolSqr)
00266 {
00267
00268
00269
00270 if (debug & 2)
00271 {
00272 Pout<< " -> centre hit:" << fc
00273 << " distance:" << magSqr(fc - curPt)/typDimSqr << endl;
00274 }
00275
00276 return indexedOctree<treeDataFace>::getSide(area, sample - curPt);
00277 }
00278
00279
00280
00281
00282
00283
00284
00285 const labelList& myEdges = mesh_.faceEdges()[faceI];
00286
00287 forAll(myEdges, myEdgeI)
00288 {
00289 const edge& e = mesh_.edges()[myEdges[myEdgeI]];
00290
00291 pointHit edgeHit =
00292 line<point, const point&>
00293 (
00294 points[e.start()],
00295 points[e.end()]
00296 ).nearestDist(sample);
00297
00298
00299 if ((magSqr(edgeHit.rawPoint() - curPt)/typDimSqr) < tolSqr)
00300 {
00301
00302
00303
00304
00305 const labelList& eFaces = mesh_.edgeFaces()[myEdges[myEdgeI]];
00306
00307 vector edgeNormal(vector::zero);
00308
00309 forAll(eFaces, i)
00310 {
00311 if (isTreeFace_.get(eFaces[i]) == 1)
00312 {
00313 vector n = mesh_.faceAreas()[eFaces[i]];
00314 n /= mag(n) + VSMALL;
00315
00316 edgeNormal += n;
00317 }
00318 }
00319
00320 if (debug & 2)
00321 {
00322 Pout<< " -> real edge hit point:" << edgeHit.rawPoint()
00323 << " comparing to edge normal:" << edgeNormal
00324 << endl;
00325 }
00326
00327
00328
00329 return indexedOctree<treeDataFace>::getSide
00330 (
00331 edgeNormal,
00332 sample - curPt
00333 );
00334 }
00335 }
00336
00337
00338
00339
00340
00341
00342 forAll(f, fp)
00343 {
00344 pointHit edgeHit = line<point, const point&>
00345 (
00346 points[f[fp]],
00347 fc
00348 ).nearestDist(sample);
00349
00350 if ((magSqr(edgeHit.rawPoint() - curPt)/typDimSqr) < tolSqr)
00351 {
00352
00353
00354
00355 vector e = points[f[fp]] - fc;
00356 vector ePrev = points[f[f.rcIndex(fp)]] - fc;
00357 vector eNext = points[f[f.fcIndex(fp)]] - fc;
00358
00359 vector nLeft = ePrev ^ e;
00360 nLeft /= mag(nLeft) + VSMALL;
00361
00362 vector nRight = e ^ eNext;
00363 nRight /= mag(nRight) + VSMALL;
00364
00365 if (debug & 2)
00366 {
00367 Pout<< " -> internal edge hit point:" << edgeHit.rawPoint()
00368 << " comparing to edge normal "
00369 << 0.5*(nLeft + nRight)
00370 << endl;
00371 }
00372
00373
00374
00375 return indexedOctree<treeDataFace>::getSide
00376 (
00377 0.5*(nLeft + nRight),
00378 sample - curPt
00379 );
00380 }
00381 }
00382
00383 if (debug & 2)
00384 {
00385 Pout<< "Did not find sample " << sample
00386 << " anywhere related to nearest face " << faceI << endl
00387 << "Face:";
00388
00389 forAll(f, fp)
00390 {
00391 Pout<< " vertex:" << f[fp] << " coord:" << points[f[fp]]
00392 << endl;
00393 }
00394 }
00395
00396
00397
00398
00399
00400
00401 return indexedOctree<treeDataFace>::UNKNOWN;
00402 }
00403
00404
00405
00406 bool Foam::treeDataFace::overlaps
00407 (
00408 const label index,
00409 const treeBoundBox& cubeBb
00410 ) const
00411 {
00412
00413 if (cacheBb_)
00414 {
00415 if (!cubeBb.overlaps(bbs_[index]))
00416 {
00417 return false;
00418 }
00419 }
00420 else
00421 {
00422 if (!cubeBb.overlaps(calcBb(faceLabels_[index])))
00423 {
00424 return false;
00425 }
00426 }
00427
00428 const pointField& points = mesh_.points();
00429
00430
00431
00432 label faceI = faceLabels_[index];
00433
00434 const face& f = mesh_.faces()[faceI];
00435
00436 forAll(f, fp)
00437 {
00438 if (cubeBb.contains(points[f[fp]]))
00439 {
00440 return true;
00441 }
00442 }
00443
00444
00445
00446 const point& fc = mesh_.faceCentres()[faceI];
00447
00448 forAll(f, fp)
00449 {
00450 bool triIntersects = triangleFuncs::intersectBb
00451 (
00452 points[f[fp]],
00453 points[f[f.fcIndex(fp)]],
00454 fc,
00455 cubeBb
00456 );
00457
00458 if (triIntersects)
00459 {
00460 return true;
00461 }
00462 }
00463 return false;
00464 }
00465
00466
00467
00468
00469 void Foam::treeDataFace::findNearest
00470 (
00471 const labelList& indices,
00472 const point& sample,
00473
00474 scalar& nearestDistSqr,
00475 label& minIndex,
00476 point& nearestPoint
00477 ) const
00478 {
00479 forAll(indices, i)
00480 {
00481 label index = indices[i];
00482
00483 const face& f = mesh_.faces()[faceLabels_[index]];
00484
00485 pointHit nearHit = f.nearestPoint(sample, mesh_.points());
00486 scalar distSqr = sqr(nearHit.distance());
00487
00488 if (distSqr < nearestDistSqr)
00489 {
00490 nearestDistSqr = distSqr;
00491 minIndex = index;
00492 nearestPoint = nearHit.rawPoint();
00493 }
00494 }
00495 }
00496
00497
00498 bool Foam::treeDataFace::intersects
00499 (
00500 const label index,
00501 const point& start,
00502 const point& end,
00503 point& intersectionPoint
00504 ) const
00505 {
00506
00507 if (cacheBb_)
00508 {
00509 const treeBoundBox& faceBb = bbs_[index];
00510
00511 if ((faceBb.posBits(start) & faceBb.posBits(end)) != 0)
00512 {
00513
00514 return false;
00515 }
00516 }
00517
00518 label faceI = faceLabels_[index];
00519
00520 const vector dir(end - start);
00521
00522 pointHit inter = mesh_.faces()[faceI].intersection
00523 (
00524 start,
00525 dir,
00526 mesh_.faceCentres()[faceI],
00527 mesh_.points(),
00528 intersection::HALF_RAY
00529 );
00530
00531 if (inter.hit() && inter.distance() <= 1)
00532 {
00533
00534
00535 intersectionPoint = inter.hitPoint();
00536 return true;
00537 }
00538 else
00539 {
00540 return false;
00541 }
00542 }
00543
00544
00545