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 "searchableSurfacesQueries.H"
00028 #include "ListOps.H"
00029 #include "OFstream.H"
00030 #include "meshTools.H"
00031
00032
00033
00034 defineTypeNameAndDebug(Foam::searchableSurfacesQueries, 0);
00035
00036
00037
00038 Foam::pointIndexHit Foam::searchableSurfacesQueries::tempFindNearest
00039 (
00040 const searchableSurface& surf,
00041 const point& pt,
00042 const scalar initDistSqr
00043 )
00044 {
00045 pointField onePoint(1, pt);
00046 scalarField oneDist(1, initDistSqr);
00047 List<pointIndexHit> oneHit(1);
00048 surf.findNearest(onePoint, oneDist, oneHit);
00049 return oneHit[0];
00050 }
00051
00052
00053
00054 Foam::scalar Foam::searchableSurfacesQueries::sumDistSqr
00055 (
00056 const PtrList<searchableSurface>& allSurfaces,
00057 const labelList& surfacesToTest,
00058 const scalar initDistSqr,
00059 const point& pt
00060 )
00061 {
00062 scalar sum = 0;
00063
00064 forAll(surfacesToTest, testI)
00065 {
00066 label surfI = surfacesToTest[testI];
00067
00068 pointIndexHit hit
00069 (
00070 tempFindNearest(allSurfaces[surfI], pt, initDistSqr)
00071 );
00072
00073
00074 sum += magSqr(hit.hitPoint()-pt);
00075 }
00076 return sum;
00077 }
00078
00079
00080
00081
00082
00083 Foam::scalar Foam::searchableSurfacesQueries::tryMorphTet
00084 (
00085 const PtrList<searchableSurface>& allSurfaces,
00086 const labelList& surfacesToTest,
00087 const scalar initDistSqr,
00088 List<vector>& p,
00089 List<scalar>& y,
00090 vector& pSum,
00091 const label ihi,
00092 const scalar fac
00093 )
00094 {
00095 scalar fac1 = (1.0-fac)/vector::nComponents;
00096 scalar fac2 = fac1-fac;
00097
00098 vector ptry = pSum*fac1-p[ihi]*fac2;
00099
00100 scalar ytry = sumDistSqr(allSurfaces, surfacesToTest, initDistSqr, ptry);
00101
00102 if (ytry < y[ihi])
00103 {
00104 y[ihi] = ytry;
00105 pSum += ptry - p[ihi];
00106 p[ihi] = ptry;
00107 }
00108 return ytry;
00109 }
00110
00111
00112 bool Foam::searchableSurfacesQueries::morphTet
00113 (
00114 const PtrList<searchableSurface>& allSurfaces,
00115 const labelList& surfacesToTest,
00116 const scalar initDistSqr,
00117 const scalar convergenceDistSqr,
00118 const label maxIter,
00119 List<vector>& p,
00120 List<scalar>& y
00121 )
00122 {
00123 vector pSum = sum(p);
00124
00125 autoPtr<OFstream> str;
00126 label vertI = 0;
00127 if (debug)
00128 {
00129 wordList names(surfacesToTest.size());
00130 forAll(surfacesToTest, i)
00131 {
00132 names[i] = allSurfaces[surfacesToTest[i]].name();
00133 }
00134 Pout<< "searchableSurfacesQueries::morphTet : intersection of "
00135 << names << " starting from points:" << p << endl;
00136 str.reset(new OFstream("track.obj"));
00137 meshTools::writeOBJ(str(), p[0]);
00138 vertI++;
00139 }
00140
00141 for (label iter = 0; iter < maxIter; iter++)
00142 {
00143
00144 label ilo, ihi, inhi;
00145 {
00146 labelList sortedIndices;
00147 sortedOrder(y, sortedIndices);
00148 ilo = sortedIndices[0];
00149 ihi = sortedIndices[sortedIndices.size()-1];
00150 inhi = sortedIndices[sortedIndices.size()-2];
00151 }
00152
00153 if (debug)
00154 {
00155 Pout<< "Iteration:" << iter
00156 << " lowest:" << y[ilo] << " highest:" << y[ihi]
00157 << " points:" << p << endl;
00158
00159 meshTools::writeOBJ(str(), p[ilo]);
00160 vertI++;
00161 str()<< "l " << vertI-1 << ' ' << vertI << nl;
00162 }
00163
00164 if (y[ihi] < convergenceDistSqr)
00165 {
00166
00167 Swap(p[0], p[ilo]);
00168 Swap(y[0], y[ilo]);
00169 return true;
00170 }
00171
00172
00173 scalar ytry = tryMorphTet
00174 (
00175 allSurfaces,
00176 surfacesToTest,
00177 10*y[ihi],
00178 p,
00179 y,
00180 pSum,
00181 ihi,
00182 -1.0
00183 );
00184
00185 if (ytry <= y[ilo])
00186 {
00187
00188 ytry = tryMorphTet
00189 (
00190 allSurfaces,
00191 surfacesToTest,
00192 10*y[ihi],
00193 p,
00194 y,
00195 pSum,
00196 ihi,
00197 2.0
00198 );
00199 }
00200 else if (ytry >= y[inhi])
00201 {
00202
00203
00204 scalar ysave = y[ihi];
00205
00206 ytry = tryMorphTet
00207 (
00208 allSurfaces,
00209 surfacesToTest,
00210 10*y[ihi],
00211 p,
00212 y,
00213 pSum,
00214 ihi,
00215 0.5
00216 );
00217
00218 if (ytry >= ysave)
00219 {
00220
00221 forAll(p, i)
00222 {
00223 if (i != ilo)
00224 {
00225 p[i] = 0.5*(p[i] + p[ilo]);
00226 y[i] = sumDistSqr
00227 (
00228 allSurfaces,
00229 surfacesToTest,
00230 y[ihi],
00231 p[i]
00232 );
00233 }
00234 }
00235 pSum = sum(p);
00236 }
00237 }
00238 }
00239
00240 if (debug)
00241 {
00242 meshTools::writeOBJ(str(), p[0]);
00243 vertI++;
00244 str()<< "l " << vertI-1 << ' ' << vertI << nl;
00245 }
00246
00247
00248 label ilo = findMin(y);
00249 Swap(p[0], p[ilo]);
00250 Swap(y[0], y[ilo]);
00251 return false;
00252 }
00253
00254
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 void Foam::searchableSurfacesQueries::mergeHits
00321 (
00322 const point& start,
00323 const scalar mergeDist,
00324
00325 const label testI,
00326 const List<pointIndexHit>& surfHits,
00327
00328 labelList& allSurfaces,
00329 List<pointIndexHit>& allInfo,
00330 scalarList& allDistSqr
00331 )
00332 {
00333
00334 scalarList surfDistSqr(surfHits.size());
00335 forAll(surfHits, i)
00336 {
00337 surfDistSqr[i] = magSqr(surfHits[i].hitPoint()-start);
00338 }
00339
00340 forAll(surfDistSqr, i)
00341 {
00342 label index = findLower(allDistSqr, surfDistSqr[i]);
00343
00344
00345 if
00346 (
00347 index >= 0
00348 && (mag(allDistSqr[index]-surfDistSqr[i]) < mergeDist)
00349 )
00350 {
00351
00352
00353
00354
00355
00356 }
00357 else
00358 {
00359
00360 label next = index+1;
00361 if
00362 (
00363 next < allDistSqr.size()
00364 && (mag(allDistSqr[next]-surfDistSqr[i]) < mergeDist)
00365 )
00366 {
00367
00368
00369
00370
00371 }
00372 else
00373 {
00374
00375 label sz = allSurfaces.size();
00376 allSurfaces.setSize(sz+1);
00377 allInfo.setSize(allSurfaces.size());
00378 allDistSqr.setSize(allSurfaces.size());
00379
00380 for (label j = sz-1; j > index; --j)
00381 {
00382 allSurfaces[j+1] = allSurfaces[j];
00383 allInfo[j+1] = allInfo[j];
00384 allDistSqr[j+1] = allDistSqr[j];
00385 }
00386
00387 allSurfaces[index+1] = testI;
00388 allInfo[index+1] = surfHits[i];
00389 allDistSqr[index+1] = surfDistSqr[i];
00390 }
00391 }
00392 }
00393 }
00394
00395
00396
00397
00398
00399 void Foam::searchableSurfacesQueries::findAnyIntersection
00400 (
00401 const PtrList<searchableSurface>& allSurfaces,
00402 const labelList& surfacesToTest,
00403 const pointField& start,
00404 const pointField& end,
00405 labelList& hitSurfaces,
00406 List<pointIndexHit>& hitInfo
00407 )
00408 {
00409 hitSurfaces.setSize(start.size());
00410 hitSurfaces = -1;
00411 hitInfo.setSize(start.size());
00412
00413
00414 labelList hitMap(identity(start.size()));
00415 pointField p0(start);
00416 pointField p1(end);
00417 List<pointIndexHit> intersectInfo(start.size());
00418
00419 forAll(surfacesToTest, testI)
00420 {
00421
00422 allSurfaces[surfacesToTest[testI]].findLineAny(p0, p1, intersectInfo);
00423
00424
00425 label newI = 0;
00426 forAll(intersectInfo, i)
00427 {
00428 if (intersectInfo[i].hit())
00429 {
00430 hitInfo[hitMap[i]] = intersectInfo[i];
00431 hitSurfaces[hitMap[i]] = testI;
00432 }
00433 else
00434 {
00435 if (i != newI)
00436 {
00437 hitMap[newI] = hitMap[i];
00438 p0[newI] = p0[i];
00439 p1[newI] = p1[i];
00440 }
00441 newI++;
00442 }
00443 }
00444
00445
00446 if (newI == 0)
00447 {
00448 break;
00449 }
00450
00451
00452 hitMap.setSize(newI);
00453 p0.setSize(newI);
00454 p1.setSize(newI);
00455 intersectInfo.setSize(newI);
00456 }
00457 }
00458
00459
00460 void Foam::searchableSurfacesQueries::findAllIntersections
00461 (
00462 const PtrList<searchableSurface>& allSurfaces,
00463 const labelList& surfacesToTest,
00464 const pointField& start,
00465 const pointField& end,
00466 labelListList& hitSurfaces,
00467 List<List<pointIndexHit> >& hitInfo
00468 )
00469 {
00470
00471
00472
00473
00474
00475
00476
00477 hitSurfaces.setSize(start.size());
00478 hitInfo.setSize(start.size());
00479
00480 if (surfacesToTest.empty())
00481 {
00482 return;
00483 }
00484
00485
00486 allSurfaces[surfacesToTest[0]].findLineAll(start, end, hitInfo);
00487
00488
00489 List<scalarList> hitDistSqr(hitInfo.size());
00490 forAll(hitInfo, pointI)
00491 {
00492 const List<pointIndexHit>& pHits = hitInfo[pointI];
00493
00494 labelList& pSurfaces = hitSurfaces[pointI];
00495 pSurfaces.setSize(pHits.size());
00496 pSurfaces = 0;
00497
00498 scalarList& pDistSqr = hitDistSqr[pointI];
00499 pDistSqr.setSize(pHits.size());
00500 forAll(pHits, i)
00501 {
00502 pDistSqr[i] = magSqr(pHits[i].hitPoint() - start[pointI]);
00503 }
00504 }
00505
00506
00507 if (surfacesToTest.size() > 1)
00508 {
00509
00510
00511
00512
00513 const vectorField smallVec
00514 (
00515 1E2*SMALL*(end-start)
00516 + vector(ROOTVSMALL,ROOTVSMALL,ROOTVSMALL)
00517 );
00518
00519
00520
00521
00522 const scalarField mergeDist(2*mag(smallVec)*mag(end-start));
00523
00524
00525 for (label testI = 1; testI < surfacesToTest.size(); testI++)
00526 {
00527 List<List<pointIndexHit> > surfHits;
00528 allSurfaces[surfacesToTest[testI]].findLineAll
00529 (
00530 start,
00531 end,
00532 surfHits
00533 );
00534
00535 forAll(surfHits, pointI)
00536 {
00537 mergeHits
00538 (
00539 start[pointI],
00540 mergeDist[pointI],
00541
00542 testI,
00543 surfHits[pointI],
00544
00545 hitSurfaces[pointI],
00546 hitInfo[pointI],
00547 hitDistSqr[pointI]
00548 );
00549 }
00550 }
00551 }
00552 }
00553
00554
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645 void Foam::searchableSurfacesQueries::findNearest
00646 (
00647 const PtrList<searchableSurface>& allSurfaces,
00648 const labelList& surfacesToTest,
00649 const pointField& samples,
00650 const scalarField& nearestDistSqr,
00651 labelList& nearestSurfaces,
00652 List<pointIndexHit>& nearestInfo
00653 )
00654 {
00655
00656 nearestSurfaces.setSize(samples.size());
00657 nearestSurfaces = -1;
00658 nearestInfo.setSize(samples.size());
00659
00660
00661 scalarField minDistSqr(nearestDistSqr);
00662 List<pointIndexHit> hitInfo(samples.size());
00663
00664 forAll(surfacesToTest, testI)
00665 {
00666 allSurfaces[surfacesToTest[testI]].findNearest
00667 (
00668 samples,
00669 minDistSqr,
00670 hitInfo
00671 );
00672
00673
00674 forAll(hitInfo, pointI)
00675 {
00676 if (hitInfo[pointI].hit())
00677 {
00678 minDistSqr[pointI] = magSqr
00679 (
00680 hitInfo[pointI].hitPoint()
00681 - samples[pointI]
00682 );
00683 nearestInfo[pointI] = hitInfo[pointI];
00684 nearestSurfaces[pointI] = testI;
00685 }
00686 }
00687 }
00688 }
00689
00690
00691
00692 Foam::pointIndexHit Foam::searchableSurfacesQueries::facesIntersection
00693 (
00694 const PtrList<searchableSurface>& allSurfaces,
00695 const labelList& surfacesToTest,
00696 const scalar initDistSqr,
00697 const scalar convergenceDistSqr,
00698 const point& start
00699 )
00700 {
00701
00702
00703 List<point> nearest(surfacesToTest.size()+1);
00704
00705 point sumNearest = vector::zero;
00706
00707 forAll(surfacesToTest, i)
00708 {
00709 pointIndexHit hit
00710 (
00711 tempFindNearest(allSurfaces[surfacesToTest[i]], start, initDistSqr)
00712 );
00713
00714 if (hit.hit())
00715 {
00716 nearest[i] = hit.hitPoint();
00717 sumNearest += nearest[i];
00718 }
00719 else
00720 {
00721 FatalErrorIn
00722 (
00723 "searchableSurfacesQueries::facesIntersection"
00724 "(const labelList&, const scalar, const scalar, const point&)"
00725 ) << "Did not find point within distance "
00726 << initDistSqr << " of starting point " << start
00727 << " on surface "
00728 << allSurfaces[surfacesToTest[i]].IOobject::name()
00729 << abort(FatalError);
00730 }
00731 }
00732
00733 nearest[nearest.size()-1] = sumNearest / surfacesToTest.size();
00734
00735
00736
00737 List<scalar> nearestDist(nearest.size());
00738
00739 forAll(nearestDist, i)
00740 {
00741 nearestDist[i] = sumDistSqr
00742 (
00743 allSurfaces,
00744 surfacesToTest,
00745 initDistSqr,
00746 nearest[i]
00747 );
00748 }
00749
00750
00751
00752
00753 bool converged = morphTet
00754 (
00755 allSurfaces,
00756 surfacesToTest,
00757 initDistSqr,
00758 convergenceDistSqr,
00759 2000,
00760 nearest,
00761 nearestDist
00762 );
00763
00764
00765 pointIndexHit intersection;
00766
00767 if (converged)
00768 {
00769
00770 intersection = tempFindNearest
00771 (
00772 allSurfaces[surfacesToTest[0]],
00773 nearest[0],
00774 nearestDist[0]
00775 );
00776 }
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812 return intersection;
00813 }
00814
00815
00816
00817