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 "searchableCylinder.H"
00028 #include "addToRunTimeSelectionTable.H"
00029
00030
00031
00032 namespace Foam
00033 {
00034
00035 defineTypeNameAndDebug(searchableCylinder, 0);
00036 addToRunTimeSelectionTable(searchableSurface, searchableCylinder, dict);
00037
00038 }
00039
00040
00041
00042
00043 Foam::pointIndexHit Foam::searchableCylinder::findNearest
00044 (
00045 const point& sample,
00046 const scalar nearestDistSqr
00047 ) const
00048 {
00049 pointIndexHit info(false, sample, -1);
00050
00051 vector v(sample - point1_);
00052
00053
00054 scalar parallel = (v & unitDir_);
00055
00056
00057 v -= parallel*unitDir_;
00058 scalar magV = mag(v);
00059
00060 if (parallel <= 0)
00061 {
00062
00063 if (magV < ROOTVSMALL)
00064 {
00065 info.setPoint(point1_);
00066 }
00067 else
00068 {
00069
00070 info.setPoint(point1_ + radius_*(v/magV));
00071 }
00072 }
00073 else if (parallel >= magDir_)
00074 {
00075
00076 if (magV < ROOTVSMALL)
00077 {
00078 info.setPoint(point2_);
00079 }
00080 else
00081 {
00082 info.setPoint(point2_ + min(magV, radius_)*v/magV);
00083 }
00084 }
00085 else
00086 {
00087 if (magV < ROOTVSMALL)
00088 {
00089 info.setPoint(point1_ + parallel*unitDir_);
00090 }
00091 else
00092 {
00093
00094 info.setPoint(point1_ + parallel*unitDir_ + radius_*v/magV);
00095 }
00096 }
00097
00098 if (magSqr(sample - info.rawPoint()) < nearestDistSqr)
00099 {
00100 info.setHit();
00101 info.setIndex(0);
00102 }
00103
00104 return info;
00105 }
00106
00107
00108 Foam::scalar Foam::searchableCylinder::radius2(const point& pt) const
00109 {
00110 const vector x = (pt-point1_) ^ unitDir_;
00111 return x&x;
00112 }
00113
00114
00115
00116
00117 void Foam::searchableCylinder::findLineAll
00118 (
00119 const point& start,
00120 const point& end,
00121 pointIndexHit& near,
00122 pointIndexHit& far
00123 ) const
00124 {
00125 near.setMiss();
00126 far.setMiss();
00127
00128 vector point1Start(start-point1_);
00129 vector point2Start(start-point2_);
00130 vector point1End(end-point1_);
00131
00132
00133 scalar s1 = point1Start&unitDir_;
00134 scalar s2 = point1End&unitDir_;
00135
00136 if ((s1 < 0 && s2 < 0) || (s1 > magDir_ && s2 > magDir_))
00137 {
00138 return;
00139 }
00140
00141
00142 vector V(end-start);
00143 scalar magV = mag(V);
00144 if (magV < ROOTVSMALL)
00145 {
00146 return;
00147 }
00148 V /= magV;
00149
00150
00151
00152
00153
00154
00155
00156 scalar tPoint1;
00157 scalar tPoint2;
00158
00159
00160 scalar tNear = VGREAT;
00161 scalar tFar = VGREAT;
00162
00163 {
00164 scalar s = (V&unitDir_);
00165 if (mag(s) > VSMALL)
00166 {
00167 tPoint1 = -s1/s;
00168 tPoint2 = -(point2Start&unitDir_)/s;
00169 if (tPoint2 < tPoint1)
00170 {
00171 Swap(tPoint1, tPoint2);
00172 }
00173 if (tPoint1 > magV || tPoint2 < 0)
00174 {
00175 return;
00176 }
00177
00178
00179 if (tPoint1 >= 0 && tPoint1 <= magV)
00180 {
00181 if (radius2(start+tPoint1*V) <= sqr(radius_))
00182 {
00183 tNear = tPoint1;
00184 }
00185 }
00186 if (tPoint2 >= 0 && tPoint2 <= magV)
00187 {
00188 if (radius2(start+tPoint2*V) <= sqr(radius_))
00189 {
00190
00191 if (tNear <= 1)
00192 {
00193 tFar = tPoint2;
00194 }
00195 else
00196 {
00197 tNear = tPoint2;
00198 }
00199 }
00200 }
00201 }
00202 else
00203 {
00204
00205
00206 tPoint1 = -VGREAT;
00207 tPoint2 = VGREAT;
00208 }
00209 }
00210
00211
00212 const vector x = point1Start ^ unitDir_;
00213 const vector y = V ^ unitDir_;
00214 const scalar d = sqr(radius_);
00215
00216
00217 const scalar a = (y&y);
00218 const scalar b = 2*(x&y);
00219 const scalar c = (x&x)-d;
00220
00221 const scalar disc = b*b-4*a*c;
00222
00223 scalar t1 = -VGREAT;
00224 scalar t2 = VGREAT;
00225
00226 if (disc < 0)
00227 {
00228
00229 return;
00230 }
00231 else if (disc < ROOTVSMALL)
00232 {
00233
00234 if (mag(a) > ROOTVSMALL)
00235 {
00236 t1 = -b/(2*a);
00237
00238
00239
00240
00241
00242 if (t1 >= 0 && t1 <= magV && t1 >= tPoint1 && t1 <= tPoint2)
00243 {
00244
00245 if (t1 < tNear)
00246 {
00247 tFar = tNear;
00248 tNear = t1;
00249 }
00250 else if (t1 < tFar)
00251 {
00252 tFar = t1;
00253 }
00254 }
00255 else
00256 {
00257 return;
00258 }
00259 }
00260 else
00261 {
00262
00263
00264
00265
00266
00267 if (c > 0)
00268 {
00269 return;
00270 }
00271 }
00272 }
00273 else
00274 {
00275 if (mag(a) > ROOTVSMALL)
00276 {
00277 scalar sqrtDisc = sqrt(disc);
00278
00279 t1 = (-b - sqrtDisc)/(2*a);
00280 t2 = (-b + sqrtDisc)/(2*a);
00281 if (t2 < t1)
00282 {
00283 Swap(t1, t2);
00284 }
00285
00286 if (t1 >= 0 && t1 <= magV && t1 >= tPoint1 && t1 <= tPoint2)
00287 {
00288
00289 if (t1 < tNear)
00290 {
00291 tFar = tNear;
00292 tNear = t1;
00293 }
00294 else if (t1 < tFar)
00295 {
00296 tFar = t1;
00297 }
00298 }
00299 if (t2 >= 0 && t2 <= magV && t2 >= tPoint1 && t2 <= tPoint2)
00300 {
00301
00302 if (t2 < tNear)
00303 {
00304 tFar = tNear;
00305 tNear = t2;
00306 }
00307 else if (t2 < tFar)
00308 {
00309 tFar = t2;
00310 }
00311 }
00312
00313
00314
00315
00316 }
00317 else
00318 {
00319
00320
00321
00322
00323
00324
00325 if (c > 0)
00326 {
00327 return;
00328 }
00329 }
00330 }
00331
00332
00333 if (tNear >= 0 && tNear <= magV)
00334 {
00335 near.setPoint(start+tNear*V);
00336 near.setHit();
00337 near.setIndex(0);
00338
00339 if (tFar <= magV)
00340 {
00341 far.setPoint(start+tFar*V);
00342 far.setHit();
00343 far.setIndex(0);
00344 }
00345 }
00346 else if (tFar >= 0 && tFar <= magV)
00347 {
00348 near.setPoint(start+tFar*V);
00349 near.setHit();
00350 near.setIndex(0);
00351 }
00352 }
00353
00354
00355
00356
00357 Foam::searchableCylinder::searchableCylinder
00358 (
00359 const IOobject& io,
00360 const point& point1,
00361 const point& point2,
00362 const scalar radius
00363 )
00364 :
00365 searchableSurface(io),
00366 point1_(point1),
00367 point2_(point2),
00368 magDir_(mag(point2_-point1_)),
00369 unitDir_((point2_-point1_)/magDir_),
00370 radius_(radius)
00371 {}
00372
00373
00374 Foam::searchableCylinder::searchableCylinder
00375 (
00376 const IOobject& io,
00377 const dictionary& dict
00378 )
00379 :
00380 searchableSurface(io),
00381 point1_(dict.lookup("point1")),
00382 point2_(dict.lookup("point2")),
00383 magDir_(mag(point2_-point1_)),
00384 unitDir_((point2_-point1_)/magDir_),
00385 radius_(readScalar(dict.lookup("radius")))
00386 {}
00387
00388
00389
00390
00391 Foam::searchableCylinder::~searchableCylinder()
00392 {}
00393
00394
00395
00396
00397 const Foam::wordList& Foam::searchableCylinder::regions() const
00398 {
00399 if (regions_.empty())
00400 {
00401 regions_.setSize(1);
00402 regions_[0] = "region0";
00403 }
00404 return regions_;
00405 }
00406
00407
00408 void Foam::searchableCylinder::findNearest
00409 (
00410 const pointField& samples,
00411 const scalarField& nearestDistSqr,
00412 List<pointIndexHit>& info
00413 ) const
00414 {
00415 info.setSize(samples.size());
00416
00417 forAll(samples, i)
00418 {
00419 info[i] = findNearest(samples[i], nearestDistSqr[i]);
00420 }
00421 }
00422
00423
00424 void Foam::searchableCylinder::findLine
00425 (
00426 const pointField& start,
00427 const pointField& end,
00428 List<pointIndexHit>& info
00429 ) const
00430 {
00431 info.setSize(start.size());
00432
00433 forAll(start, i)
00434 {
00435
00436 pointIndexHit b;
00437 findLineAll(start[i], end[i], info[i], b);
00438 if (!info[i].hit() && b.hit())
00439 {
00440 info[i] = b;
00441 }
00442 }
00443 }
00444
00445
00446 void Foam::searchableCylinder::findLineAny
00447 (
00448 const pointField& start,
00449 const pointField& end,
00450 List<pointIndexHit>& info
00451 ) const
00452 {
00453 info.setSize(start.size());
00454
00455 forAll(start, i)
00456 {
00457
00458 pointIndexHit b;
00459 findLineAll(start[i], end[i], info[i], b);
00460 if (!info[i].hit() && b.hit())
00461 {
00462 info[i] = b;
00463 }
00464 }
00465 }
00466
00467
00468 void Foam::searchableCylinder::findLineAll
00469 (
00470 const pointField& start,
00471 const pointField& end,
00472 List<List<pointIndexHit> >& info
00473 ) const
00474 {
00475 info.setSize(start.size());
00476
00477 forAll(start, i)
00478 {
00479 pointIndexHit near, far;
00480 findLineAll(start[i], end[i], near, far);
00481
00482 if (near.hit())
00483 {
00484 if (far.hit())
00485 {
00486 info[i].setSize(2);
00487 info[i][0] = near;
00488 info[i][1] = far;
00489 }
00490 else
00491 {
00492 info[i].setSize(1);
00493 info[i][0] = near;
00494 }
00495 }
00496 else
00497 {
00498 if (far.hit())
00499 {
00500 info[i].setSize(1);
00501 info[i][0] = far;
00502 }
00503 else
00504 {
00505 info[i].clear();
00506 }
00507 }
00508 }
00509 }
00510
00511
00512 void Foam::searchableCylinder::getRegion
00513 (
00514 const List<pointIndexHit>& info,
00515 labelList& region
00516 ) const
00517 {
00518 region.setSize(info.size());
00519 region = 0;
00520 }
00521
00522
00523 void Foam::searchableCylinder::getNormal
00524 (
00525 const List<pointIndexHit>& info,
00526 vectorField& normal
00527 ) const
00528 {
00529 normal.setSize(info.size());
00530 normal = vector::zero;
00531
00532 forAll(info, i)
00533 {
00534 if (info[i].hit())
00535 {
00536 vector v(info[i].hitPoint() - point1_);
00537
00538
00539 scalar parallel = v & unitDir_;
00540
00541 if (parallel < 0)
00542 {
00543 normal[i] = -unitDir_;
00544 }
00545 else if (parallel > magDir_)
00546 {
00547 normal[i] = -unitDir_;
00548 }
00549 else
00550 {
00551
00552 v -= parallel*unitDir_;
00553 normal[i] = v/mag(v);
00554 }
00555 }
00556 }
00557 }
00558
00559
00560 void Foam::searchableCylinder::getVolumeType
00561 (
00562 const pointField& points,
00563 List<volumeType>& volType
00564 ) const
00565 {
00566 volType.setSize(points.size());
00567 volType = INSIDE;
00568
00569 forAll(points, pointI)
00570 {
00571 const point& pt = points[pointI];
00572
00573 vector v(pt - point1_);
00574
00575
00576 scalar parallel = v & unitDir_;
00577
00578 if (parallel < 0)
00579 {
00580
00581 volType[pointI] = OUTSIDE;
00582 }
00583 else if (parallel > magDir_)
00584 {
00585
00586 volType[pointI] = OUTSIDE;
00587 }
00588 else
00589 {
00590
00591 v -= parallel*unitDir_;
00592
00593 if (mag(v) > radius_)
00594 {
00595 volType[pointI] = OUTSIDE;
00596 }
00597 else
00598 {
00599 volType[pointI] = INSIDE;
00600 }
00601 }
00602 }
00603 }
00604
00605
00606