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 | Copyright (C) 2011-2016 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  surfaceFind
26 
27 Description
28  Finds nearest face and vertex.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #include "argList.H"
33 #include "OFstream.H"
34 
35 #include "MeshedSurfaces.H"
36 
37 using namespace Foam;
38 
39 
40 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
41 
42 int main(int argc, char *argv[])
43 {
45  argList::validArgs.append("surfaceFile");
46  argList::addOption("x", "X", "The point x-coordinate (if non-zero)");
47  argList::addOption("y", "Y", "The point y-coordinate (if non-zero)");
48  argList::addOption("z", "Z", "The point y-coordinate (if non-zero)");
49 
50  argList args(argc, argv);
51 
52  const point samplePt
53  (
54  args.optionLookupOrDefault<scalar>("x", 0),
55  args.optionLookupOrDefault<scalar>("y", 0),
56  args.optionLookupOrDefault<scalar>("z", 0)
57  );
58  Info<< "Looking for nearest face/vertex to " << samplePt << endl;
59 
60 
61  Info<< "Reading surf ..." << endl;
62  meshedSurface surf1(args[1]);
63 
64  //
65  // Nearest vertex
66  //
67 
68  const pointField& localPoints = surf1.localPoints();
69 
70  label minIndex = -1;
71  scalar minDist = GREAT;
72 
73  forAll(localPoints, pointi)
74  {
75  const scalar dist = mag(localPoints[pointi] - samplePt);
76  if (dist < minDist)
77  {
78  minDist = dist;
79  minIndex = pointi;
80  }
81  }
82 
83  Info<< "Nearest vertex:" << nl
84  << " index :" << minIndex << " (in localPoints)" << nl
85  << " index :" << surf1.meshPoints()[minIndex]
86  << " (in points)" << nl
87  << " coordinates:" << localPoints[minIndex] << nl
88  << endl;
89 
90  //
91  // Nearest face
92  //
93 
94  const pointField& points = surf1.points();
95 
96  minIndex = -1;
97  minDist = GREAT;
98 
99  forAll(surf1, facei)
100  {
101  const point centre = surf1[facei].centre(points);
102 
103  const scalar dist = mag(centre - samplePt);
104  if (dist < minDist)
105  {
106  minDist = dist;
107  minIndex = facei;
108  }
109  }
110 
111  const face& f = surf1[minIndex];
112 
113  Info<< "Face with nearest centre:" << nl
114  << " index :" << minIndex << nl
115  << " centre :" << f.centre(points) << nl
116  << " face :" << f << nl
117  << " vertex coords:\n";
118  forAll(f, fp)
119  {
120  Info<< " " << points[f[fp]] << "\n";
121  }
122 
123  Info<< endl;
124 
125  Info<< "End\n" << endl;
126 
127  return 0;
128 }
129 
130 
131 // ************************************************************************* //
point centre(const pointField &) const
Centre point of face.
Definition: face.C:488
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:75
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
const Vector< Cmpt > & centre(const Foam::List< Vector< Cmpt >> &) const
Return *this (used for point which is a typedef to Vector<scalar>.
Definition: VectorI.H:116
static void noParallel()
Remove the parallel options.
Definition: argList.C:146
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:154
T optionLookupOrDefault(const word &opt, const T &deflt) const
Read a value from the named option if present.
Definition: argListI.H:237
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:102
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:93
static const char nl
Definition: Ostream.H:262
messageStream Info
dimensioned< scalar > mag(const dimensioned< Type > &)
Foam::argList args(argc, argv)
Namespace for OpenFOAM.