surfaceFind.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2020-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Application
28  surfaceFind
29 
30 Group
31  grpSurfaceUtilities
32 
33 Description
34  Finds nearest face and vertex.
35  Uses a zero origin unless otherwise specified.
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #include "argList.H"
40 #include "OFstream.H"
41 
42 #include "MeshedSurfaces.H"
43 
44 using namespace Foam;
45 
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 int main(int argc, char *argv[])
50 {
52  (
53  "Find nearest face and vertex."
54  " Uses a zero origin unless otherwise specified"
55  );
56 
58  argList::addArgument("input", "The input surface file");
59  argList::addOption("x", "X", "The point x-coordinate (if non-zero)");
60  argList::addOption("y", "Y", "The point y-coordinate (if non-zero)");
61  argList::addOption("z", "Z", "The point y-coordinate (if non-zero)");
62 
63  argList args(argc, argv);
64 
65  const point samplePt
66  (
67  args.getOrDefault<scalar>("x", 0),
68  args.getOrDefault<scalar>("y", 0),
69  args.getOrDefault<scalar>("z", 0)
70  );
71  Info<< "Looking for nearest face/vertex to " << samplePt << endl;
72 
73 
74  Info<< "Reading surf ..." << endl;
75  meshedSurface surf1(args.get<fileName>(1));
76 
77  //
78  // Nearest vertex
79  //
80 
81  const pointField& localPoints = surf1.localPoints();
82 
83  label minIndex = -1;
84  scalar minDist = GREAT;
85 
86  forAll(localPoints, pointi)
87  {
88  const scalar dist = mag(localPoints[pointi] - samplePt);
89  if (dist < minDist)
90  {
91  minDist = dist;
92  minIndex = pointi;
93  }
94  }
95 
96  Info<< "Nearest vertex:" << nl
97  << " index : " << minIndex << " (in localPoints)" << nl
98  << " index : " << surf1.meshPoints()[minIndex]
99  << " (in points)" << nl
100  << " coordinates: " << localPoints[minIndex] << nl
101  << endl;
102 
103  //
104  // Nearest face
105  //
106 
107  const pointField& points = surf1.points();
108 
109  minIndex = -1;
110  minDist = GREAT;
111 
112  forAll(surf1, facei)
113  {
114  const point centre = surf1[facei].centre(points);
115 
116  const scalar dist = mag(centre - samplePt);
117  if (dist < minDist)
118  {
119  minDist = dist;
120  minIndex = facei;
121  }
122  }
123 
124  const face& f = surf1[minIndex];
125 
126  Info<< "Face with nearest centre:" << nl
127  << " index : " << minIndex << nl
128  << " centre : " << f.centre(points) << nl
129  << " face : " << f << nl
130  << " vertex coords:" << endl;
131 
132  forAll(f, fp)
133  {
134  Info<< " " << points[f[fp]] << nl;
135  }
136 
137  const List<surfZone>& surfZones = surf1.surfZones();
138  label surfZone = -1;
139  forAll(surfZones, zonei)
140  {
141  if (minIndex >= surfZones[zonei].start())
142  {
143  surfZone = zonei;
144  }
145  }
146  Info<< " zone/region : " << surfZone << endl;
147 
148  Info<< nl
149  << "End\n" << endl;
150 
151  return 0;
152 }
153 
154 
155 // ************************************************************************* //
Foam::fileName
A class for handling file names.
Definition: fileName.H:71
Foam::argList::getOrDefault
T getOrDefault(const word &optName, const T &deflt) const
Definition: argListI.H:300
Foam::argList::addNote
static void addNote(const string &note)
Definition: argList.C:405
Foam::argList
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:119
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
Foam::argList::get
T get(const label index) const
Definition: argListI.H:271
forAll
#define forAll(list, i)
Definition: stdFoam.H:349
OFstream.H
Foam::argList::addArgument
static void addArgument(const string &argName, const string &usage="")
Definition: argList.C:294
Foam::Field
Generic templated field type.
Definition: Field.H:59
Foam::Info
messageStream Info
argList.H
Foam::Vector::centre
const Vector< Cmpt > & centre(const Foam::List< Vector< Cmpt >> &) const
Definition: VectorI.H:107
Foam
Definition: atmBoundaryLayer.C:26
Foam::nl
constexpr char nl
Definition: Ostream.H:424
f
labelList f(nPoints)
Foam::Vector< scalar >
Foam::List< surfZone >
Foam::surfZone
A surface zone on a MeshedSurface.
Definition: surfZone.H:52
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
MeshedSurfaces.H
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::face
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:68
Foam::argList::noParallel
static void noParallel()
Definition: argList.C:503
Foam::MeshedSurface
A surface geometry mesh with zone information, not to be confused with the similarly named surfaceMes...
Definition: triSurfaceTools.H:76
Foam::argList::addOption
static void addOption(const word &optName, const string &param="", const string &usage="", bool advanced=false)
Definition: argList.C:328
args
Foam::argList args(argc, argv)