surfaceFind.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 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 int main(int argc, char *argv[])
42 {
43  #include "removeCaseOptions.H"
44 
45  argList::validArgs.append("surface file");
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 pointHit hit =
102  surf1[facei].nearestPoint(samplePt, points);
103 
104  if (hit.distance() < minDist)
105  {
106  minDist = hit.distance();;
107  minIndex = facei;
108  }
109  }
110 
111  const face& f = surf1[minIndex];
112 
113  Info<< "Nearest face:" << nl
114  << " index : " << minIndex << nl
115  << " centre : " << f.centre(points) << nl
116  << " face : " << f << nl
117  << " vertex coords:" << endl;
118  forAll(f, fp)
119  {
120  Info<< " " << points[f[fp]] << nl;
121  }
122 
123  const List<surfZone>& surfZones = surf1.surfZones();
124  label surfZone = -1;
125  forAll(surfZones, zonei)
126  {
127  if (minIndex >= surfZones[zonei].start())
128  {
129  surfZone = zonei;
130  }
131  }
132  Info<< " zone/region : " << surfZone << endl;
133 
134  Info<< endl;
135 
136  Info<< "End\n" << endl;
137 
138  return 0;
139 }
140 
141 
142 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
scalar distance() const
Return distance to hit.
Definition: PointHit.H:139
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:103
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:128
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:153
T optionLookupOrDefault(const word &opt, const T &deflt) const
Read a value from the named option if present.
Definition: argListI.H:243
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:76
A surface zone on a MeshedSurface.
Definition: surfZone.H:65
int main(int argc, char *argv[])
Definition: financialFoam.C:44
const pointField & points
Namespace for OpenFOAM.
scalar minDist(const List< pointIndexHit > &hitList)
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
messageStream Info
dimensioned< scalar > mag(const dimensioned< Type > &)
static const char nl
Definition: Ostream.H:260
labelList f(nPoints)
Foam::argList args(argc, argv)