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-------------------------------------------------------------------------------
11License
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
27Application
28 surfaceFind
29
30Group
31 grpSurfaceUtilities
32
33Description
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
44using namespace Foam;
45
46
47// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48
49int main(int argc, char *argv[])
50{
51 argList::addNote
52 (
53 "Find nearest face and vertex."
54 " Uses a zero origin unless otherwise specified"
55 );
56
57 argList::noParallel();
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// ************************************************************************* //
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
const Vector< Cmpt > & centre(const Foam::UList< Vector< Cmpt > > &) const
Return *this (used for point which is a typedef to Vector<scalar>.
Definition: VectorI.H:114
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:124
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:278
T getOrDefault(const word &optName, const T &deflt) const
Get a value from the named option if present, or return default.
Definition: argListI.H:307
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:75
A class for handling file names.
Definition: fileName.H:76
A surface zone on a MeshedSurface.
Definition: surfZone.H:59
const pointField & points
Namespace for OpenFOAM.
messageStream Info
Information stream (stdout output on master, null elsewhere)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
labelList f(nPoints)
Foam::argList args(argc, argv)
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333