conformationSurfaces.C 33.1 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
6
     \\/     M anipulation  |
OpenFOAM bot's avatar
OpenFOAM bot committed
7
-------------------------------------------------------------------------------
OpenFOAM bot's avatar
OpenFOAM bot committed
8
    Copyright (C) 2012-2017 OpenFOAM Foundation
9
    Copyright (C) 2020 OpenCFD Ltd.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "conformationSurfaces.H"
#include "conformalVoronoiMesh.H"
laurence's avatar
laurence committed
31
#include "triSurface.H"
32
#include "searchableSurfaceFeatures.H"
33

34 35 36 37
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
38
    defineTypeNameAndDebug(conformationSurfaces, 0);
39 40 41
}


42 43 44 45
// * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //

void Foam::conformationSurfaces::hasBoundedVolume
(
46
    List<volumeType>& referenceVolumeTypes
47 48
) const
{
Henry Weller's avatar
Henry Weller committed
49
    vector sum(Zero);
50 51 52 53 54 55
    label totalTriangles = 0;

    forAll(surfaces_, s)
    {
        const searchableSurface& surface(allGeometry_[surfaces_[s]]);

56 57 58
        if
        (
            surface.hasVolumeType()
laurence's avatar
laurence committed
59 60 61 62
         && (
                normalVolumeTypes_[regionOffset_[s]]
             != extendedFeatureEdgeMesh::BOTH
            )
63
        )
64 65 66
        {
            pointField pts(1, locationInMesh_);

67
            List<volumeType> vTypes
68 69
            (
                pts.size(),
70
                volumeType::UNKNOWN
71 72 73 74 75 76
            );

            surface.getVolumeType(pts, vTypes);

            referenceVolumeTypes[s] = vTypes[0];

77
            Info<< "    is " << referenceVolumeTypes[s].str()
78 79 80 81 82 83 84 85 86 87
                << " surface " << surface.name()
                << endl;
        }

        if (isA<triSurface>(surface))
        {
            const triSurface& triSurf = refCast<const triSurface>(surface);

            const pointField& surfPts = triSurf.points();

88 89 90 91 92 93 94
            Info<< "    Checking " << surface.name() << endl;

            label nBaffles = 0;

            Info<< "        Index = " << surfaces_[s] << endl;
            Info<< "        Offset = " << regionOffset_[s] << endl;

95
            for (const labelledTri& f : triSurf)
96
            {
97
                const label patchID = f.region() + regionOffset_[s];
98 99

                // Don't include baffle surfaces in the calculation
laurence's avatar
laurence committed
100 101 102 103 104
                if
                (
                    normalVolumeTypes_[patchID]
                 != extendedFeatureEdgeMesh::BOTH
                )
105
                {
106
                    sum += f.areaNormal(surfPts);
107
                }
108 109
                else
                {
110
                    ++nBaffles;
111
                }
112
            }
113
            Info<< "        has " << nBaffles << " baffles out of "
114
                << triSurf.size() << " triangles" << nl;
115 116 117 118 119 120 121

            totalTriangles += triSurf.size();
        }
    }

    Info<< "    Sum of all the surface normals (if near zero, surface is"
        << " probably closed):" << nl
122
        << "    Note: Does not include baffle surfaces in calculation" << nl
laurence's avatar
laurence committed
123 124
        << "        Sum = " << sum/(totalTriangles + SMALL) << nl
        << "        mag(Sum) = " << mag(sum)/(totalTriangles + SMALL)
125 126 127 128 129 130
        << endl;
}


void Foam::conformationSurfaces::readFeatures
(
131
    const label surfI,
132 133 134 135 136
    const dictionary& featureDict,
    const word& surfaceName,
    label& featureIndex
)
{
137
    const word featureMethod =
138
        featureDict.getOrDefault<word>("featureMethod", "none");
139 140 141

    if (featureMethod == "extendedFeatureEdgeMesh")
    {
142 143 144 145
        fileName feMeshName
        (
            featureDict.get<fileName>("extendedFeatureEdgeMesh")
        );
146 147 148 149 150

        Info<< "    features: " << feMeshName << endl;

        features_.set
        (
laurence's avatar
laurence committed
151
            featureIndex,
152 153 154 155 156 157 158 159 160 161 162 163 164
            new extendedFeatureEdgeMesh
            (
                IOobject
                (
                    feMeshName,
                    runTime_.time().constant(),
                    "extendedFeatureEdgeMesh",
                    runTime_.time(),
                    IOobject::MUST_READ,
                    IOobject::NO_WRITE
                )
            )
        );
laurence's avatar
laurence committed
165 166

        featureIndex++;
167 168 169
    }
    else if (featureMethod == "extractFeatures")
    {
170 171
        const searchableSurface& surface = allGeometry_[surfaces_[surfI]];

172 173 174
        Info<< "    features: " << surface.name()
            << " of type " << surface.type()
            << ", id: " << featureIndex << endl;
175 176

        autoPtr<searchableSurfaceFeatures> ssFeatures
177
        (
178
            searchableSurfaceFeatures::New(surface, featureDict)
179
        );
180 181 182 183 184 185 186 187 188 189 190 191 192

        if (ssFeatures().hasFeatures())
        {
            features_.set
            (
                featureIndex,
                ssFeatures().features()
            );

            featureIndex++;
        }
        else
        {
193 194
            WarningInFunction
                << surface.name() << " of type "
195 196 197 198 199 200 201 202 203 204
                << surface.type() << " does not have features"
                << endl;
        }
    }
    else if (featureMethod == "none")
    {
        // Currently nothing to do
    }
    else
    {
205
        FatalErrorInFunction
206 207 208 209 210 211 212 213 214 215 216 217 218 219
            << "No valid featureMethod found for surface " << surfaceName
            << nl << "Use \"extendedFeatureEdgeMesh\" "
            << "or \"extractFeatures\"."
            << exit(FatalError);
    }
}

void Foam::conformationSurfaces::readFeatures
(
    const dictionary& featureDict,
    const word& surfaceName,
    label& featureIndex
)
{
220
    const word featureMethod =
221
        featureDict.getOrDefault<word>("featureMethod", "none");
222 223 224

    if (featureMethod == "extendedFeatureEdgeMesh")
    {
225 226 227 228
        fileName feMeshName
        (
            featureDict.get<fileName>("extendedFeatureEdgeMesh")
        );
229

230 231
        Info<< "    features: " << feMeshName << ", id: " << featureIndex
            << endl;
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

        features_.set
        (
            featureIndex,
            new extendedFeatureEdgeMesh
            (
                IOobject
                (
                    feMeshName,
                    runTime_.time().constant(),
                    "extendedFeatureEdgeMesh",
                    runTime_.time(),
                    IOobject::MUST_READ,
                    IOobject::NO_WRITE
                )
            )
        );

        featureIndex++;
251 252 253 254 255 256 257
    }
    else if (featureMethod == "none")
    {
        // Currently nothing to do
    }
    else
    {
258
        FatalErrorInFunction
259 260 261 262 263 264 265 266
            << "No valid featureMethod found for surface " << surfaceName
            << nl << "Use \"extendedFeatureEdgeMesh\" "
            << "or \"extractFeatures\"."
            << exit(FatalError);
    }
}


267 268 269 270 271 272 273 274 275 276 277 278 279 280
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::conformationSurfaces::conformationSurfaces
(
    const Time& runTime,
    Random& rndGen,
    const searchableSurfaces& allGeometry,
    const dictionary& surfaceConformationDict
)
:
    runTime_(runTime),
    rndGen_(rndGen),
    allGeometry_(allGeometry),
    features_(),
281
    locationInMesh_(surfaceConformationDict.get<point>("locationInMesh")),
282 283
    surfaces_(),
    allGeometryToSurfaces_(),
laurence's avatar
laurence committed
284
    normalVolumeTypes_(),
285
    patchNames_(),
286
    surfZones_(),
287 288
    regionOffset_(),
    patchInfo_(),
289 290 291 292 293 294 295 296 297 298 299 300 301
    globalBounds_(),
    referenceVolumeTypes_(0)
{
    const dictionary& surfacesDict
    (
        surfaceConformationDict.subDict("geometryToConformTo")
    );

    const dictionary& additionalFeaturesDict
    (
        surfaceConformationDict.subDict("additionalFeatures")
    );

302 303 304 305 306 307

    // Wildcard specification : loop over all surface, all regions
    // and try to find a match.

    // Count number of surfaces.
    label surfI = 0;
308
    for (const word& geomName : allGeometry_.names())
309 310 311
    {
        if (surfacesDict.found(geomName))
        {
312
            ++surfI;
313 314 315
        }
    }

316 317
    const label nAddFeat = additionalFeaturesDict.size();

318 319 320 321
    Info<< nl << "Reading geometryToConformTo" << endl;

    allGeometryToSurfaces_.setSize(allGeometry_.size(), -1);

322 323 324
    normalVolumeTypes_.setSize(surfI);
    surfaces_.setSize(surfI);
    surfZones_.setSize(surfI);
325 326

    // Features may be attached to host surfaces or independent
327
    features_.setSize(surfI + nAddFeat);
328 329 330

    label featureI = 0;

331
    regionOffset_.setSize(surfI, 0);
332

333
    PtrList<dictionary> globalPatchInfo(surfI);
334
    List<Map<autoPtr<dictionary>>> regionPatchInfo(surfI);
335
    List<sideVolumeType> globalVolumeTypes(surfI);
336
    List<Map<sideVolumeType>> regionVolumeTypes(surfI);
337

338
    wordHashSet unmatchedKeys(surfacesDict.toc());
339

340 341
    surfI = 0;
    forAll(allGeometry_.names(), geomI)
342
    {
343
        const word& geomName = allGeometry_.names()[geomI];
344

345
        const entry* ePtr = surfacesDict.findEntry(geomName, keyType::REGEX);
346

347
        if (ePtr)
348
        {
349 350
            const dictionary& dict = ePtr->dict();
            unmatchedKeys.erase(ePtr->keyword());
351

352
            surfaces_[surfI] = geomI;
353

354
            const searchableSurface& surface = allGeometry_[surfaces_[surfI]];
355

356 357 358
            // Surface zones
            if (dict.found("faceZone"))
            {
359 360 361 362 363 364 365 366 367 368
                surfZones_.set
                (
                    surfI,
                    new surfaceZonesInfo
                    (
                        surface,
                        dict,
                        allGeometry_.regionNames()[surfaces_[surfI]]
                    )
                );
369
            }
370

371
            allGeometryToSurfaces_[surfaces_[surfI]] = surfI;
372

373
            Info<< nl << "    " << geomName << endl;
374

375 376
            const wordList& regionNames =
                allGeometry_.regionNames()[surfaces_[surfI]];
377

378 379 380 381 382 383
            patchNames_.append(regionNames);

            globalVolumeTypes[surfI] =
            (
                extendedFeatureEdgeMesh::sideVolumeTypeNames_
                [
384
                    dict.getOrDefault<word>
385 386 387 388 389 390 391 392
                    (
                        "meshableSide",
                        "inside"
                    )
                ]
            );

            if (!globalVolumeTypes[surfI])
393
            {
394 395
                if (!surface.hasVolumeType())
                {
396
                    WarningInFunction
397 398 399 400 401
                        << "Non-baffle surface "
                        << surface.name()
                        << " does not allow inside/outside queries."
                        << " This usually is an error." << endl;
                }
402 403
            }

404 405 406 407 408 409 410 411 412 413 414
            // Load patch info
            if (dict.found("patchInfo"))
            {
                globalPatchInfo.set
                (
                    surfI,
                    dict.subDict("patchInfo").clone()
                );
            }

            readFeatures
415
            (
416
                surfI,
417 418 419
                dict,
                geomName,
                featureI
420 421
            );

422
            const wordList& rNames = surface.regions();
423

424
            if (dict.found("regions"))
425
            {
426
                const dictionary& regionsDict = dict.subDict("regions");
427

428
                forAll(rNames, regionI)
429
                {
430
                    const word& regionName = rNames[regionI];
431

432
                    if (regionsDict.found(regionName))
433
                    {
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
                        Info<< "        region " << regionName << endl;

                        // Get the dictionary for region
                        const dictionary& regionDict = regionsDict.subDict
                        (
                            regionName
                        );

                        if (regionDict.found("patchInfo"))
                        {
                            regionPatchInfo[surfI].insert
                            (
                                regionI,
                                regionDict.subDict("patchInfo").clone()
                            );
                        }

                        regionVolumeTypes[surfI].insert
452 453
                        (
                            regionI,
454 455
                            extendedFeatureEdgeMesh::sideVolumeTypeNames_
                            [
456
                                 regionDict.getOrDefault<word>
457 458
                                 (
                                     "meshableSide",
459 460 461 462 463
                                     extendedFeatureEdgeMesh::
                                     sideVolumeTypeNames_
                                     [
                                        globalVolumeTypes[surfI]
                                     ]
464 465
                                 )
                            ]
466 467
                        );

468 469
                        readFeatures(regionDict, regionName, featureI);
                    }
470 471
                }
            }
472 473

            surfI++;
474
        }
475
    }
476

477 478 479

    if (unmatchedKeys.size() > 0)
    {
480 481
        IOWarningInFunction(surfacesDict)
            << "Not all entries in conformationSurfaces dictionary were used."
482 483 484
            << " The following entries were not used : "
            << unmatchedKeys.sortedToc()
            << endl;
485 486
    }

487

488 489 490 491 492 493
    // Calculate local to global region offset
    label nRegions = 0;

    forAll(surfaces_, surfI)
    {
        regionOffset_[surfI] = nRegions;
494 495 496

        const searchableSurface& surface = allGeometry_[surfaces_[surfI]];
        nRegions += surface.regions().size();
497 498 499 500
    }

    // Rework surface specific information into information per global region
    patchInfo_.setSize(nRegions);
laurence's avatar
laurence committed
501
    normalVolumeTypes_.setSize(nRegions);
502 503 504

    forAll(surfaces_, surfI)
    {
505 506 507
        const searchableSurface& surface = allGeometry_[surfaces_[surfI]];

        label nRegions = surface.regions().size();
508 509 510

        // Initialise to global (i.e. per surface)
        for (label i = 0; i < nRegions; i++)
511
        {
512
            label globalRegionI = regionOffset_[surfI] + i;
laurence's avatar
laurence committed
513
            normalVolumeTypes_[globalRegionI] = globalVolumeTypes[surfI];
514 515 516 517 518 519 520 521
            if (globalPatchInfo.set(surfI))
            {
                patchInfo_.set
                (
                    globalRegionI,
                    globalPatchInfo[surfI].clone()
                );
            }
522
        }
523

524
        forAllConstIters(regionVolumeTypes[surfI], iter)
525
        {
526 527
            label globalRegionI = regionOffset_[surfI] + iter.key();

laurence's avatar
laurence committed
528 529
            normalVolumeTypes_[globalRegionI] =
                regionVolumeTypes[surfI][iter.key()];
530
        }
531

532
        const Map<autoPtr<dictionary>>& localInfo = regionPatchInfo[surfI];
533
        forAllConstIters(localInfo, iter)
534
        {
535
            label globalRegionI = regionOffset_[surfI] + iter.key();
536

537 538
            patchInfo_.set(globalRegionI, iter()().clone());
        }
539 540
    }

541 542


543 544 545 546 547
    if (!additionalFeaturesDict.empty())
    {
        Info<< nl << "Reading additionalFeatures" << endl;
    }

548
    for (const entry& dEntry : additionalFeaturesDict)
549
    {
550 551
        const word& featureName = dEntry.keyword();
        const dictionary& featureSubDict = dEntry.dict();
552

553
        Info<< nl << "    " << featureName << endl;
554

555
        readFeatures(featureSubDict, featureName, featureI);
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
    }

    // Remove unnecessary space from the features list
    features_.setSize(featureI);

    globalBounds_ = treeBoundBox
    (
        searchableSurfacesQueries::bounds(allGeometry_, surfaces_)
    );

    // Extend the global bounds to stop the bound box sitting on the surfaces
    // to be conformed to
    //globalBounds_ = globalBounds_.extend(rndGen_, 1e-4);

    vector newSpan = 1e-4*globalBounds_.span();

    globalBounds_.min() -= newSpan;
    globalBounds_.max() += newSpan;

    // Look at all surfaces at determine whether the locationInMesh point is
    // inside or outside each, to establish a signature for the domain to be
    // meshed.

    referenceVolumeTypes_.setSize
    (
        surfaces_.size(),
582
        volumeType::UNKNOWN
583 584 585 586 587
    );

    Info<< endl
        << "Testing for locationInMesh " << locationInMesh_ << endl;

588
    hasBoundedVolume(referenceVolumeTypes_);
laurence's avatar
laurence committed
589 590 591 592 593 594

    if (debug)
    {
        Info<< "Names = " << allGeometry_.names() << endl;
        Info<< "Surfaces = " << surfaces_ << endl;
        Info<< "AllGeom to Surfaces = " << allGeometryToSurfaces_ << endl;
laurence's avatar
laurence committed
595
        Info<< "Volume types = " << normalVolumeTypes_ << endl;
laurence's avatar
laurence committed
596 597 598 599 600 601 602 603
        Info<< "Patch names = " << patchNames_ << endl;
        Info<< "Region Offset = " << regionOffset_ << endl;

        forAll(features_, fI)
        {
            Info<< features_[fI].name() << endl;
        }
    }
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
}


// * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //

bool Foam::conformationSurfaces::overlaps(const treeBoundBox& bb) const
{
    forAll(surfaces_, s)
    {
        if (allGeometry_[surfaces_[s]].overlaps(bb))
        {
            return true;
        }
    }

    return false;
}


Foam::Field<bool> Foam::conformationSurfaces::inside
(
    const pointField& samplePts
) const
{
628
    return wellInside(samplePts, scalarField(samplePts.size(), Zero));
629 630 631 632 633 634 635 636
}


bool Foam::conformationSurfaces::inside
(
    const point& samplePt
) const
{
637
    return wellInside(pointField(1, samplePt), scalarField(1, Zero))[0];
638 639 640 641 642 643 644 645
}


Foam::Field<bool> Foam::conformationSurfaces::outside
(
    const pointField& samplePts
) const
{
646
    return wellOutside(samplePts, scalarField(samplePts.size(), Zero));
647 648 649 650 651 652 653 654
}


bool Foam::conformationSurfaces::outside
(
    const point& samplePt
) const
{
655
    return wellOutside(pointField(1, samplePt), scalarField(1, Zero))[0];
656
    //return !inside(samplePt);
657 658 659
}


660
Foam::Field<bool> Foam::conformationSurfaces::wellInOutSide
661 662
(
    const pointField& samplePts,
663 664
    const scalarField& testDistSqr,
    const bool testForInside
665 666
) const
{
667
    List<List<volumeType>> surfaceVolumeTests
668 669
    (
        surfaces_.size(),
670
        List<volumeType>
671 672
        (
            samplePts.size(),
673
            volumeType::UNKNOWN
674 675 676 677 678 679 680 681
        )
    );

    // Get lists for the volumeTypes for each sample wrt each surface
    forAll(surfaces_, s)
    {
        const searchableSurface& surface(allGeometry_[surfaces_[s]]);

682 683
        const label regionI = regionOffset_[s];

laurence's avatar
laurence committed
684
        if (normalVolumeTypes_[regionI] != extendedFeatureEdgeMesh::BOTH)
685
        {
686
            surface.getVolumeType(samplePts, surfaceVolumeTests[s]);
687 688 689 690 691 692 693 694
        }
    }

    // Compare the volumeType result for each point wrt to each surface with the
    // reference value and if the points are inside the surface by a given
    // distanceSquared

    // Assume that the point is wellInside until demonstrated otherwise.
695
    Field<bool> insideOutsidePoint(samplePts.size(), testForInside);
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718

    //Check if the points are inside the surface by the given distance squared

    labelList hitSurfaces;
    List<pointIndexHit> hitInfo;
    searchableSurfacesQueries::findNearest
    (
        allGeometry_,
        surfaces_,
        samplePts,
        testDistSqr,
        hitSurfaces,
        hitInfo
    );

    forAll(samplePts, i)
    {
        const pointIndexHit& pHit = hitInfo[i];

        if (pHit.hit())
        {
            // If the point is within range of the surface, then it can't be
            // well (in|out)side
719
            insideOutsidePoint[i] = false;
720 721 722 723 724 725

            continue;
        }

        forAll(surfaces_, s)
        {
726 727
            const label regionI = regionOffset_[s];

laurence's avatar
laurence committed
728
            if (normalVolumeTypes_[regionI] == extendedFeatureEdgeMesh::BOTH)
729 730 731 732
            {
                continue;
            }

733 734
            const searchableSurface& surface(allGeometry_[surfaces_[s]]);

735 736 737 738 739
            if
            (
                !surface.hasVolumeType()
             //&& surfaceVolumeTests[s][i] == volumeType::UNKNOWN
            )
740 741 742 743 744 745 746
            {
                pointField sample(1, samplePts[i]);
                scalarField nearestDistSqr(1, GREAT);
                List<pointIndexHit> info;

                surface.findNearest(sample, nearestDistSqr, info);

747 748 749 750 751
                const vector hitDir =
                    normalised
                    (
                        info[0].rawPoint() - samplePts[i]
                    );
752

753 754 755 756
                pointIndexHit surfHit;
                label hitSurface;

                findSurfaceNearestIntersection
757
                (
758
                    samplePts[i],
759
                    info[0].rawPoint() - 1e-3*mag(hitDir)*hitDir,
760 761 762 763 764
                    surfHit,
                    hitSurface
                );

                if (surfHit.hit() && hitSurface != surfaces_[s])
765 766 767 768 769
                {
                    continue;
                }
            }

770
            if (surfaceVolumeTests[s][i] == volumeType::OUTSIDE)
771
            {
laurence's avatar
laurence committed
772 773 774 775 776 777
                if
                (
                    normalVolumeTypes_[regionI]
                 == extendedFeatureEdgeMesh::INSIDE
                )
                {
778
                    insideOutsidePoint[i] = !testForInside;
laurence's avatar
laurence committed
779 780 781 782 783 784 785 786 787 788 789
                    break;
                }
            }
            else if (surfaceVolumeTests[s][i] == volumeType::INSIDE)
            {
                if
                (
                    normalVolumeTypes_[regionI]
                 == extendedFeatureEdgeMesh::OUTSIDE
                )
                {
790
                    insideOutsidePoint[i] = !testForInside;
laurence's avatar
laurence committed
791 792
                    break;
                }
793 794 795 796
            }
        }
    }

797 798 799
    return insideOutsidePoint;
}

800

801 802 803 804 805 806 807
Foam::Field<bool> Foam::conformationSurfaces::wellInside
(
    const pointField& samplePts,
    const scalarField& testDistSqr
) const
{
    return wellInOutSide(samplePts, testDistSqr, true);
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
}


bool Foam::conformationSurfaces::wellInside
(
    const point& samplePt,
    scalar testDistSqr
) const
{
    return wellInside(pointField(1, samplePt), scalarField(1, testDistSqr))[0];
}


Foam::Field<bool> Foam::conformationSurfaces::wellOutside
(
    const pointField& samplePts,
    const scalarField& testDistSqr
) const
{
827
    return wellInOutSide(samplePts, testDistSqr, false);
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
}


bool Foam::conformationSurfaces::wellOutside
(
    const point& samplePt,
    scalar testDistSqr
) const
{
    return wellOutside(pointField(1, samplePt), scalarField(1, testDistSqr))[0];
}


bool Foam::conformationSurfaces::findSurfaceAnyIntersection
(
    const point& start,
    const point& end
) const
{
    labelList hitSurfaces;
    List<pointIndexHit> hitInfo;

    searchableSurfacesQueries::findAnyIntersection
    (
        allGeometry_,
        surfaces_,
        pointField(1, start),
        pointField(1, end),
        hitSurfaces,
        hitInfo
    );

    return hitInfo[0].hit();
}


void Foam::conformationSurfaces::findSurfaceAnyIntersection
(
    const point& start,
    const point& end,
    pointIndexHit& surfHit,
    label& hitSurface
) const
{
    labelList hitSurfaces;
    List<pointIndexHit> hitInfo;

    searchableSurfacesQueries::findAnyIntersection
    (
        allGeometry_,
        surfaces_,
        pointField(1, start),
        pointField(1, end),
        hitSurfaces,
        hitInfo
    );

    surfHit = hitInfo[0];

    if (surfHit.hit())
    {
        // hitSurfaces has returned the index of the entry in surfaces_ that was
        // found, not the index of the surface in allGeometry_, translating this
        // to allGeometry_

        hitSurface = surfaces_[hitSurfaces[0]];
    }
}


898 899 900 901 902 903 904 905 906
void Foam::conformationSurfaces::findSurfaceAllIntersections
(
    const point& start,
    const point& end,
    List<pointIndexHit>& surfHit,
    labelList& hitSurface
) const
{
    labelListList hitSurfaces;
907
    List<List<pointIndexHit>> hitInfo;
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933

    searchableSurfacesQueries::findAllIntersections
    (
        allGeometry_,
        surfaces_,
        pointField(1, start),
        pointField(1, end),
        hitSurfaces,
        hitInfo
    );

    surfHit = hitInfo[0];

    hitSurface.setSize(hitSurfaces[0].size());

    forAll(hitSurfaces[0], surfI)
    {
        // hitSurfaces has returned the index of the entry in surfaces_ that was
        // found, not the index of the surface in allGeometry_, translating this
        // to allGeometry_

        hitSurface[surfI] = surfaces_[hitSurfaces[0][surfI]];
    }
}


934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
void Foam::conformationSurfaces::findSurfaceNearestIntersection
(
    const point& start,
    const point& end,
    pointIndexHit& surfHit,
    label& hitSurface
) const
{
    labelList hitSurfacesStart;
    List<pointIndexHit> hitInfoStart;
    labelList hitSurfacesEnd;
    List<pointIndexHit> hitInfoEnd;

    searchableSurfacesQueries::findNearestIntersection
    (
        allGeometry_,
        surfaces_,
        pointField(1, start),
        pointField(1, end),
        hitSurfacesStart,
        hitInfoStart,
        hitSurfacesEnd,
        hitInfoEnd
    );

    surfHit = hitInfoStart[0];

    if (surfHit.hit())
    {
        // hitSurfaces has returned the index of the entry in surfaces_ that was
        // found, not the index of the surface in allGeometry_, translating this
        // to allGeometry_

        hitSurface = surfaces_[hitSurfacesStart[0]];
    }
}


void Foam::conformationSurfaces::findSurfaceNearest
(
    const point& sample,
    scalar nearestDistSqr,
    pointIndexHit& surfHit,
    label& hitSurface
) const
{
    labelList hitSurfaces;
    List<pointIndexHit> surfaceHits;

    searchableSurfacesQueries::findNearest
    (
        allGeometry_,
        surfaces_,
        pointField(1, sample),
        scalarField(1, nearestDistSqr),
        hitSurfaces,
        surfaceHits
    );

    surfHit = surfaceHits[0];

    if (surfHit.hit())
    {
        // hitSurfaces has returned the index of the entry in surfaces_ that was
        // found, not the index of the surface in allGeometry_, translating this
        // to allGeometry_

        hitSurface = surfaces_[hitSurfaces[0]];
    }
}


void Foam::conformationSurfaces::findSurfaceNearest
(
    const pointField& samples,
    const scalarField& nearestDistSqr,
    List<pointIndexHit>& surfaceHits,
    labelList& hitSurfaces
) const
{
    searchableSurfacesQueries::findNearest
    (
        allGeometry_,
        surfaces_,
        samples,
        nearestDistSqr,
        hitSurfaces,
        surfaceHits
    );

    forAll(surfaceHits, i)
    {
        if (surfaceHits[i].hit())
        {
            // hitSurfaces has returned the index of the entry in surfaces_ that
            // was found, not the index of the surface in allGeometry_,
            // translating this to the surface in allGeometry_.

            hitSurfaces[i] = surfaces_[hitSurfaces[i]];
        }
    }
}


void Foam::conformationSurfaces::findFeaturePointNearest
(
    const point& sample,
    scalar nearestDistSqr,
    pointIndexHit& fpHit,
    label& featureHit
) const
{
    // Work arrays
    scalar minDistSqr = nearestDistSqr;
    pointIndexHit hitInfo;

    forAll(features_, testI)
    {
        features_[testI].nearestFeaturePoint
        (
            sample,
            minDistSqr,
            hitInfo
        );

        if (hitInfo.hit())
        {
            minDistSqr = magSqr(hitInfo.hitPoint()- sample);
            fpHit = hitInfo;
            featureHit = testI;
        }
    }
}


void Foam::conformationSurfaces::findEdgeNearest
(
    const point& sample,
    scalar nearestDistSqr,
    pointIndexHit& edgeHit,
    label& featureHit
) const
{
    pointField samples(1, sample);
    scalarField nearestDistsSqr(1, nearestDistSqr);

    List<pointIndexHit> edgeHits;
    labelList featuresHit;

    findEdgeNearest
    (
        samples,
        nearestDistsSqr,
        edgeHits,
        featuresHit
    );

    edgeHit = edgeHits[0];
    featureHit = featuresHit[0];
}


void Foam::conformationSurfaces::findEdgeNearest
(
    const pointField& samples,
    const scalarField& nearestDistsSqr,
    List<pointIndexHit>& edgeHits,
    labelList& featuresHit
) const
{
    // Initialise
    featuresHit.setSize(samples.size());
    featuresHit = -1;
    edgeHits.setSize(samples.size());

    // Work arrays
    scalarField minDistSqr(nearestDistsSqr);
    List<pointIndexHit> hitInfo(samples.size());

    forAll(features_, testI)
    {
        features_[testI].nearestFeatureEdge
        (
            samples,
            minDistSqr,
            hitInfo
        );

        // Update minDistSqr and arguments
1123
        forAll(hitInfo, pointi)
1124
        {
1125
            if (hitInfo[pointi].hit())
1126
            {
1127
                minDistSqr[pointi] = magSqr
1128
                (
1129 1130
                    hitInfo[pointi].hitPoint()
                  - samples[pointi]
1131
                );
1132 1133
                edgeHits[pointi] = hitInfo[pointi];
                featuresHit[pointi] = testI;
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
            }
        }
    }
}


void Foam::conformationSurfaces::findEdgeNearestByType
(
    const point& sample,
    scalar nearestDistSqr,
    List<pointIndexHit>& edgeHits,
    List<label>& featuresHit
) const
{
    // Initialise
    featuresHit.setSize(extendedFeatureEdgeMesh::nEdgeTypes);
    featuresHit = -1;
    edgeHits.setSize(extendedFeatureEdgeMesh::nEdgeTypes);

    // Work arrays
    scalarField minDistSqr(extendedFeatureEdgeMesh::nEdgeTypes, nearestDistSqr);
    List<pointIndexHit> hitInfo(extendedFeatureEdgeMesh::nEdgeTypes);

    forAll(features_, testI)
    {
        features_[testI].nearestFeatureEdgeByType
        (
            sample,
            minDistSqr,
            hitInfo
        );

        // Update minDistSqr and arguments
        forAll(hitInfo, typeI)
        {
            if (hitInfo[typeI].hit())
            {
                minDistSqr[typeI] = magSqr(hitInfo[typeI].hitPoint() - sample);
                edgeHits[typeI] = hitInfo[typeI];
                featuresHit[typeI] = testI;
            }
        }
    }
}


void Foam::conformationSurfaces::findAllNearestEdges
(
    const point& sample,
    const scalar searchRadiusSqr,
1184
    List<List<pointIndexHit>>& edgeHitsByFeature,
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240