conformationSurfaces.C 32.4 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
laurence's avatar
laurence committed
5
    \\  /    A nd           | Copyright (C) 2012-2013 OpenFOAM Foundation
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
     \\/     M anipulation  |
-------------------------------------------------------------------------------
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
28
#include "triSurface.H"
29
#include "searchableSurfaceFeatures.H"
30

31
32
33
34
35
36
37
38
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
defineTypeNameAndDebug(conformationSurfaces, 0);
}


39
40
41
42
// * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //

void Foam::conformationSurfaces::hasBoundedVolume
(
43
    List<volumeType>& referenceVolumeTypes
44
45
) const
{
46
    vector sum(vector::zero);
47
48
49
50
51
52
    label totalTriangles = 0;

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

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

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

            surface.getVolumeType(pts, vTypes);

            referenceVolumeTypes[s] = vTypes[0];

            Info<< "    is "
75
                << volumeType::names[referenceVolumeTypes[s]]
76
77
78
79
80
81
82
83
84
85
86
87
88
89
                << " surface " << surface.name()
                << endl;
        }

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

            const pointField& surfPts = triSurf.points();

            forAll(triSurf, sI)
            {
                const label patchID =
                    triSurf[sI].region()
laurence's avatar
laurence committed
90
                  + regionOffset_[s];
91
92

                // Don't include baffle surfaces in the calculation
laurence's avatar
laurence committed
93
94
95
96
97
                if
                (
                    normalVolumeTypes_[patchID]
                 != extendedFeatureEdgeMesh::BOTH
                )
98
99
100
101
102
103
104
105
106
107
108
                {
                    sum += triSurf[sI].normal(surfPts);
                }
            }

            totalTriangles += triSurf.size();
        }
    }

    Info<< "    Sum of all the surface normals (if near zero, surface is"
        << " probably closed):" << nl
109
        << "    Note: Does not include baffle surfaces in calculation" << nl
laurence's avatar
laurence committed
110
111
        << "        Sum = " << sum/(totalTriangles + SMALL) << nl
        << "        mag(Sum) = " << mag(sum)/(totalTriangles + SMALL)
112
113
114
115
116
117
        << endl;
}


void Foam::conformationSurfaces::readFeatures
(
118
    const label surfI,
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    const dictionary& featureDict,
    const word& surfaceName,
    label& featureIndex
)
{
    word featureMethod =
        featureDict.lookupOrDefault<word>("featureMethod", "none");

    if (featureMethod == "extendedFeatureEdgeMesh")
    {
        fileName feMeshName(featureDict.lookup("extendedFeatureEdgeMesh"));

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

        features_.set
        (
laurence's avatar
laurence committed
135
            featureIndex,
136
137
138
139
140
141
142
143
144
145
146
147
148
            new extendedFeatureEdgeMesh
            (
                IOobject
                (
                    feMeshName,
                    runTime_.time().constant(),
                    "extendedFeatureEdgeMesh",
                    runTime_.time(),
                    IOobject::MUST_READ,
                    IOobject::NO_WRITE
                )
            )
        );
laurence's avatar
laurence committed
149
150

        featureIndex++;
151
152
153
    }
    else if (featureMethod == "extractFeatures")
    {
154
155
156
157
158
159
        const searchableSurface& surface = allGeometry_[surfaces_[surfI]];

        Info<< "    features: " << surface.name() << " of type "
            << surface.type() << endl;

        autoPtr<searchableSurfaceFeatures> ssFeatures
160
        (
161
            searchableSurfaceFeatures::New(surface, featureDict)
162
        );
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232

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

            featureIndex++;
        }
        else
        {
            WarningIn
            (
                "Foam::conformationSurfaces::readFeatures"
                "(const label, const dictionary&, const word&, label&)"
            )   << surface.name() << " of type "
                << surface.type() << " does not have features"
                << endl;
        }
    }
    else if (featureMethod == "none")
    {
        // Currently nothing to do
    }
    else
    {
        FatalErrorIn("Foam::conformationSurfaces::readFeatures")
            << "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
)
{
    word featureMethod =
        featureDict.lookupOrDefault<word>("featureMethod", "none");

    if (featureMethod == "extendedFeatureEdgeMesh")
    {
        fileName feMeshName(featureDict.lookup("extendedFeatureEdgeMesh"));

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

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

        featureIndex++;
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
    }
    else if (featureMethod == "none")
    {
        // Currently nothing to do
    }
    else
    {
        FatalErrorIn("Foam::conformationSurfaces::readFeatures")
            << "No valid featureMethod found for surface " << surfaceName
            << nl << "Use \"extendedFeatureEdgeMesh\" "
            << "or \"extractFeatures\"."
            << exit(FatalError);
    }
}


249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::conformationSurfaces::conformationSurfaces
(
    const Time& runTime,
    Random& rndGen,
    const searchableSurfaces& allGeometry,
    const dictionary& surfaceConformationDict
)
:
    runTime_(runTime),
    rndGen_(rndGen),
    allGeometry_(allGeometry),
    features_(),
    locationInMesh_(surfaceConformationDict.lookup("locationInMesh")),
    surfaces_(),
    allGeometryToSurfaces_(),
laurence's avatar
laurence committed
266
    normalVolumeTypes_(),
267
    patchNames_(),
268
    surfZones_(),
269
270
    regionOffset_(),
    patchInfo_(),
271
272
273
274
275
276
277
278
279
280
281
282
283
    globalBounds_(),
    referenceVolumeTypes_(0)
{
    const dictionary& surfacesDict
    (
        surfaceConformationDict.subDict("geometryToConformTo")
    );

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

284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

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

    // Count number of surfaces.
    label surfI = 0;
    forAll(allGeometry.names(), geomI)
    {
        const word& geomName = allGeometry_.names()[geomI];

        if (surfacesDict.found(geomName))
        {
            surfI++;
        }
    }

300
301
    const label nAddFeat = additionalFeaturesDict.size();

302
303
304
305
    Info<< nl << "Reading geometryToConformTo" << endl;

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

306
307
308
    normalVolumeTypes_.setSize(surfI);
    surfaces_.setSize(surfI);
    surfZones_.setSize(surfI);
309
310

    // Features may be attached to host surfaces or independent
311
    features_.setSize(surfI + nAddFeat);
312
313
314

    label featureI = 0;

315
    regionOffset_.setSize(surfI, 0);
316

317
318
319
320
    PtrList<dictionary> globalPatchInfo(surfI);
    List<Map<autoPtr<dictionary> > > regionPatchInfo(surfI);
    List<sideVolumeType> globalVolumeTypes(surfI);
    List<Map<sideVolumeType> > regionVolumeTypes(surfI);
321

322
    HashSet<word> unmatchedKeys(surfacesDict.toc());
323

324
325
    surfI = 0;
    forAll(allGeometry_.names(), geomI)
326
    {
327
        const word& geomName = allGeometry_.names()[geomI];
328

329
        const entry* ePtr = surfacesDict.lookupEntryPtr(geomName, false, true);
330

331
        if (ePtr)
332
        {
333
334
            const dictionary& dict = ePtr->dict();
            unmatchedKeys.erase(ePtr->keyword());
335

336
            surfaces_[surfI] = geomI;
337

338
            const searchableSurface& surface = allGeometry_[surfaces_[surfI]];
339

340
341
342
343
344
            // Surface zones
            if (dict.found("faceZone"))
            {
                surfZones_.set(surfI, new surfaceZonesInfo(surface, dict));
            }
345

346
            allGeometryToSurfaces_[surfaces_[surfI]] = surfI;
347

348
            Info<< nl << "    " << geomName << endl;
349

350
351
            const wordList& regionNames =
                allGeometry_.regionNames()[surfaces_[surfI]];
352

353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
            patchNames_.append(regionNames);

            globalVolumeTypes[surfI] =
            (
                extendedFeatureEdgeMesh::sideVolumeTypeNames_
                [
                    dict.lookupOrDefault<word>
                    (
                        "meshableSide",
                        "inside"
                    )
                ]
            );

            if (!globalVolumeTypes[surfI])
368
            {
369
370
371
372
373
374
375
376
                if (!surface.hasVolumeType())
                {
                    WarningIn("conformationSurfaces::conformationSurfaces(..)")
                        << "Non-baffle surface "
                        << surface.name()
                        << " does not allow inside/outside queries."
                        << " This usually is an error." << endl;
                }
377
378
            }

379
380
381
382
383
384
385
386
387
388
389
            // Load patch info
            if (dict.found("patchInfo"))
            {
                globalPatchInfo.set
                (
                    surfI,
                    dict.subDict("patchInfo").clone()
                );
            }

            readFeatures
390
            (
391
                surfI,
392
393
394
                dict,
                geomName,
                featureI
395
396
            );

397
            const wordList& rNames = surface.regions();
398

399
            if (dict.found("regions"))
400
            {
401
                const dictionary& regionsDict = dict.subDict("regions");
402

403
                forAll(rNames, regionI)
404
                {
405
                    const word& regionName = rNames[regionI];
406

407
                    if (regionsDict.found(regionName))
408
                    {
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
                        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
427
428
                        (
                            regionI,
429
430
431
432
433
434
435
436
                            extendedFeatureEdgeMesh::sideVolumeTypeNames_
                            [
                                 regionDict.lookupOrDefault<word>
                                 (
                                     "meshableSide",
                                     "inside"
                                 )
                            ]
437
438
                        );

439
440
                        readFeatures(regionDict, regionName, featureI);
                    }
441
442
                }
            }
443
444

            surfI++;
445
        }
446
    }
447

448
449
450
451
452
453
454
455
456
457
458

    if (unmatchedKeys.size() > 0)
    {
        IOWarningIn
        (
            "conformationSurfaces::conformationSurfaces(..)",
            surfacesDict
        )   << "Not all entries in conformationSurfaces dictionary were used."
            << " The following entries were not used : "
            << unmatchedKeys.sortedToc()
            << endl;
459
460
    }

461

462
463
464
465
466
467
    // Calculate local to global region offset
    label nRegions = 0;

    forAll(surfaces_, surfI)
    {
        regionOffset_[surfI] = nRegions;
468
469
470

        const searchableSurface& surface = allGeometry_[surfaces_[surfI]];
        nRegions += surface.regions().size();
471
472
473
474
    }

    // Rework surface specific information into information per global region
    patchInfo_.setSize(nRegions);
laurence's avatar
laurence committed
475
    normalVolumeTypes_.setSize(nRegions);
476
477
478

    forAll(surfaces_, surfI)
    {
479
480
481
        const searchableSurface& surface = allGeometry_[surfaces_[surfI]];

        label nRegions = surface.regions().size();
482
483
484

        // Initialise to global (i.e. per surface)
        for (label i = 0; i < nRegions; i++)
485
        {
486
            label globalRegionI = regionOffset_[surfI] + i;
laurence's avatar
laurence committed
487
            normalVolumeTypes_[globalRegionI] = globalVolumeTypes[surfI];
488
489
490
491
492
493
494
495
            if (globalPatchInfo.set(surfI))
            {
                patchInfo_.set
                (
                    globalRegionI,
                    globalPatchInfo[surfI].clone()
                );
            }
496
        }
497

laurence's avatar
laurence committed
498
        forAllConstIter(Map<sideVolumeType>, regionVolumeTypes[surfI], iter)
499
        {
500
501
            label globalRegionI = regionOffset_[surfI] + iter.key();

laurence's avatar
laurence committed
502
503
            normalVolumeTypes_[globalRegionI] =
                regionVolumeTypes[surfI][iter.key()];
504
        }
505
506
507

        const Map<autoPtr<dictionary> >& localInfo = regionPatchInfo[surfI];
        forAllConstIter(Map<autoPtr<dictionary> >, localInfo, iter)
508
        {
509
            label globalRegionI = regionOffset_[surfI] + iter.key();
510

511
512
            patchInfo_.set(globalRegionI, iter()().clone());
        }
513
514
    }

515
516


517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
    if (!additionalFeaturesDict.empty())
    {
        Info<< nl << "Reading additionalFeatures" << endl;
    }

    forAllConstIter(dictionary, additionalFeaturesDict, iter)
    {
        word featureName = iter().keyword();

        Info<< nl << "    " << iter().keyword() << endl;

        const dictionary& featureSubDict
        (
            additionalFeaturesDict.subDict(featureName)
        );

533
        readFeatures(featureSubDict, featureName, featureI);
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
    }

    // 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(),
560
        volumeType::UNKNOWN
561
562
563
564
565
    );

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

566
    hasBoundedVolume(referenceVolumeTypes_);
laurence's avatar
laurence committed
567
568
569
570
571
572

    if (debug)
    {
        Info<< "Names = " << allGeometry_.names() << endl;
        Info<< "Surfaces = " << surfaces_ << endl;
        Info<< "AllGeom to Surfaces = " << allGeometryToSurfaces_ << endl;
laurence's avatar
laurence committed
573
        Info<< "Volume types = " << normalVolumeTypes_ << endl;
laurence's avatar
laurence committed
574
575
576
577
578
579
580
581
        Info<< "Patch names = " << patchNames_ << endl;
        Info<< "Region Offset = " << regionOffset_ << endl;

        forAll(features_, fI)
        {
            Info<< features_[fI].name() << endl;
        }
    }
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
}


// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

Foam::conformationSurfaces::~conformationSurfaces()
{}


// * * * * * * * * * * * * * * 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
{
    return wellInside(samplePts, scalarField(samplePts.size(), 0.0));
}


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


Foam::Field<bool> Foam::conformationSurfaces::outside
(
    const pointField& samplePts
) const
{
    return wellOutside(samplePts, scalarField(samplePts.size(), 0.0));
}


bool Foam::conformationSurfaces::outside
(
    const point& samplePt
) const
{
639
640
    return wellOutside(pointField(1, samplePt), scalarField(1, 0))[0];
    //return !inside(samplePt);
641
642
643
}


644
Foam::Field<bool> Foam::conformationSurfaces::wellInOutSide
645
646
(
    const pointField& samplePts,
647
648
    const scalarField& testDistSqr,
    const bool testForInside
649
650
) const
{
651
    List<List<volumeType> > surfaceVolumeTests
652
653
    (
        surfaces_.size(),
654
        List<volumeType>
655
656
        (
            samplePts.size(),
657
            volumeType::UNKNOWN
658
659
660
661
662
663
664
665
        )
    );

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

666
667
        const label regionI = regionOffset_[s];

laurence's avatar
laurence committed
668
        if (normalVolumeTypes_[regionI] != extendedFeatureEdgeMesh::BOTH)
669
        {
670
            surface.getVolumeType(samplePts, surfaceVolumeTests[s]);
671
672
673
674
675
676
677
678
        }
    }

    // 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.
679
    Field<bool> insideOutsidePoint(samplePts.size(), testForInside);
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702

    //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
703
            insideOutsidePoint[i] = false;
704
705
706
707
708
709

            continue;
        }

        forAll(surfaces_, s)
        {
710
711
            const label regionI = regionOffset_[s];

laurence's avatar
laurence committed
712
            if (normalVolumeTypes_[regionI] == extendedFeatureEdgeMesh::BOTH)
713
714
715
716
            {
                continue;
            }

717
718
719
720
721
722
723
724
725
726
727
728
729
            const searchableSurface& surface(allGeometry_[surfaces_[s]]);

            if (!surface.hasVolumeType())
            {
                pointField sample(1, samplePts[i]);
                scalarField nearestDistSqr(1, GREAT);
                List<pointIndexHit> info;

                surface.findNearest(sample, nearestDistSqr, info);

                vector hitDir = info[0].rawPoint() - samplePts[i];
                hitDir /= mag(hitDir) + SMALL;

730
731
732
733
                pointIndexHit surfHit;
                label hitSurface;

                findSurfaceNearestIntersection
734
                (
735
736
737
738
739
740
741
                    samplePts[i],
                    info[0].rawPoint(),
                    surfHit,
                    hitSurface
                );

                if (surfHit.hit() && hitSurface != surfaces_[s])
742
743
744
745
746
                {
                    continue;
                }
            }

747
            if (surfaceVolumeTests[s][i] == volumeType::OUTSIDE)
748
            {
laurence's avatar
laurence committed
749
750
751
752
753
754
                if
                (
                    normalVolumeTypes_[regionI]
                 == extendedFeatureEdgeMesh::INSIDE
                )
                {
755
                    insideOutsidePoint[i] = !testForInside;
laurence's avatar
laurence committed
756
757
758
759
760
761
762
763
764
765
766
                    break;
                }
            }
            else if (surfaceVolumeTests[s][i] == volumeType::INSIDE)
            {
                if
                (
                    normalVolumeTypes_[regionI]
                 == extendedFeatureEdgeMesh::OUTSIDE
                )
                {
767
                    insideOutsidePoint[i] = !testForInside;
laurence's avatar
laurence committed
768
769
                    break;
                }
770
771
772
773
            }
        }
    }

774
775
776
    return insideOutsidePoint;
}

777

778
779
780
781
782
783
784
Foam::Field<bool> Foam::conformationSurfaces::wellInside
(
    const pointField& samplePts,
    const scalarField& testDistSqr
) const
{
    return wellInOutSide(samplePts, testDistSqr, true);
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
}


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
{
804
    return wellInOutSide(samplePts, testDistSqr, false);
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
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
}


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]];
    }
}


875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
void Foam::conformationSurfaces::findSurfaceAllIntersections
(
    const point& start,
    const point& end,
    List<pointIndexHit>& surfHit,
    labelList& hitSurface
) const
{
    labelListList hitSurfaces;
    List<List<pointIndexHit> > hitInfo;

    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]];
    }
}


911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
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
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
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
1184
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
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
        forAll(hitInfo, pointI)
        {
            if (hitInfo[pointI].hit())
            {
                minDistSqr[pointI] = magSqr
                (
                    hitInfo[pointI].hitPoint()
                  - samples[pointI]
                );
                edgeHits[pointI] = hitInfo[pointI];
                featuresHit[pointI] = testI;
            }
        }
    }
}


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,
    List<List<pointIndexHit> >& edgeHitsByFeature,
    List<label>& featuresHit
) const
{
    // Initialise
    //featuresHit.setSize(features_.size());
    //featuresHit = -1;
    //edgeHitsByFeature.setSize(features_.size());

    // Work arrays
    List<pointIndexHit> hitInfo(extendedFeatureEdgeMesh::nEdgeTypes);

    forAll(features_, testI)
    {
        features_[testI].allNearestFeatureEdges
        (
            sample,
            searchRadiusSqr,
            hitInfo
        );

        bool anyHit = false;
        forAll(hitInfo, hitI)
        {
            if (hitInfo[hitI].hit())
            {
                anyHit = true;
            }
        }

        if (anyHit)
        {
            edgeHitsByFeature.append(hitInfo);
            featuresHit.append(testI);
        }
    }
}


void Foam::conformationSurfaces::writeFeatureObj(const fileName& prefix) const
{
    OFstream ftStr(runTime_.time().path()/prefix + "_allFeatures.obj");

    Pout<< nl << "Writing all features to " << ftStr.name() << endl;

    label verti = 0;

    forAll(features_, i)
    {
        const extendedFeatureEdgeMesh& fEM(features_[i]);
        const pointField pts(fEM.points());
        const edgeList eds(fEM.edges());

        ftStr << "g " << fEM.name() << endl;

        forAll(eds, j)
        {
            const edge& e = eds[j];

            meshTools::writeOBJ(ftStr, pts[e[0]]); verti++;
            meshTools::writeOBJ(ftStr, pts[e[1]]); verti++;
            ftStr << "l " << verti-1 << ' ' << verti << endl;
        }
    }
}


Foam::label Foam::conformationSurfaces::findPatch
(
    const point& ptA,
    const point& ptB
) const
{
    pointIndexHit surfHit;
    label hitSurface;

    findSurfaceAnyIntersection(ptA, ptB, surfHit, hitSurface);

1239
    return getPatchID(hitSurface, surfHit);
1240
1241
1242
1243
1244
1245
1246
1247
}


Foam::label Foam::conformationSurfaces::findPatch(const point& pt) const
{
    pointIndexHit surfHit;
    label hitSurface;

1248
    findSurfaceNearest(pt, sqr(GREAT), surfHit, hitSurface);
1249

1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
    return getPatchID(hitSurface, surfHit);
}


Foam::label Foam::conformationSurfaces::getPatchID
(
    const label hitSurface,
    const pointIndexHit& surfHit
) const
{
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
    if (!surfHit.hit())
    {
        return -1;
    }

    labelList surfLocalRegion;

    allGeometry_[hitSurface].getRegion
    (
        List<pointIndexHit>(1, surfHit),
        surfLocalRegion
    );

1273
1274
1275
1276
1277
1278
1279
1280
    const label patchID =
        surfLocalRegion[0]
      + regionOffset_[allGeometryToSurfaces_[hitSurface]];

    return patchID;
}


laurence's avatar
laurence committed
1281
1282
Foam::extendedFeatureEdgeMesh::sideVolumeType
Foam::conformationSurfaces::meshableSide
1283
1284
1285
1286
1287
1288
1289
1290
1291
(
    const label hitSurface,
    const pointIndexHit& surfHit
) const
{
    const label patchID = getPatchID(hitSurface, surfHit);

    if (patchID == -1)
    {
laurence's avatar
laurence committed
1292
        return extendedFeatureEdgeMesh::NEITHER;
1293
1294
    }

laurence's avatar
laurence committed
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
    return normalVolumeTypes_[patchID];
}


void Foam::conformationSurfaces::getNormal
(
    const label hitSurface,
    const List<pointIndexHit>& surfHit,
    vectorField& normal
) const
{
1306
    allGeometry_[hitSurface].getNormal(surfHit, normal);
laurence's avatar
laurence committed
1307
1308
1309
1310
1311
1312
1313
1314

    const label patchID = regionOffset_[allGeometryToSurfaces_[hitSurface]];

    // Now flip sign of normal depending on mesh side
    if (normalVolumeTypes_[patchID] == extendedFeatureEdgeMesh::OUTSIDE)
    {
        normal *= -1;
    }
1315
1316
1317
1318
}


// ************************************************************************* //