FreeFOAM The Cross-Platform CFD Toolkit
surfaceToPoint.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) 1991-2010 OpenCFD Ltd.
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 \*---------------------------------------------------------------------------*/
25 
26 #include "surfaceToPoint.H"
27 #include <OpenFOAM/polyMesh.H>
28 #include <meshTools/meshSearch.H>
30 
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 
38 defineTypeNameAndDebug(surfaceToPoint, 0);
39 
40 addToRunTimeSelectionTable(topoSetSource, surfaceToPoint, word);
41 
42 addToRunTimeSelectionTable(topoSetSource, surfaceToPoint, istream);
43 
44 }
45 
46 
47 Foam::topoSetSource::addToUsageTable Foam::surfaceToPoint::usage_
48 (
49  surfaceToPoint::typeName,
50  "\n Usage: surfaceToPoint <surface> <near> <inside> <outside>\n\n"
51  " <surface> name of triSurface\n"
52  " <near> scalar; include points with coordinate <= near to surface\n"
53  " <inside> boolean; whether to include points on opposite side of"
54  " surface normal\n"
55  " <outside> boolean; whether to include points on this side of"
56  " surface normal\n\n"
57 );
58 
59 
60 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
61 
62 void Foam::surfaceToPoint::combine(topoSet& set, const bool add) const
63 {
64  cpuTime timer;
65 
66  triSurface surf(surfName_);
67 
68  Info<< " Read surface from " << surfName_
69  << " in = "<< timer.cpuTimeIncrement() << " s" << endl << endl;
70 
71  // Construct search engine on surface
72  triSurfaceSearch querySurf(surf);
73 
74  if (includeInside_ || includeOutside_)
75  {
76  boolList pointInside(querySurf.calcInside(mesh_.points()));
77 
78  forAll(pointInside, pointI)
79  {
80  bool isInside = pointInside[pointI];
81 
82  if ((isInside && includeInside_) || (!isInside && includeOutside_))
83  {
84  addOrDelete(set, pointI, add);
85  }
86  }
87  }
88 
89  if (nearDist_ > 0)
90  {
91  const pointField& meshPoints = mesh_.points();
92 
93  // Box dimensions to search in octree.
94  const vector span(nearDist_, nearDist_, nearDist_);
95 
96  forAll(meshPoints, pointI)
97  {
98  const point& pt = meshPoints[pointI];
99 
100  pointIndexHit inter = querySurf.nearest(pt, span);
101 
102  if (inter.hit() && (mag(inter.hitPoint() - pt) < nearDist_))
103  {
104  addOrDelete(set, pointI, add);
105  }
106  }
107  }
108 }
109 
110 
111 void Foam::surfaceToPoint::checkSettings() const
112 {
113  if (nearDist_ < 0 && !includeInside_ && !includeOutside_)
114  {
115  FatalErrorIn("surfaceToPoint:checkSettings()")
116  << "Illegal point selection specification."
117  << " Result would be either all or no points." << endl
118  << "Please set one of includeInside or includeOutside"
119  << " to true, set nearDistance to a value > 0"
120  << exit(FatalError);
121  }
122 }
123 
124 
125 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
126 
127 // Construct from components
129 (
130  const polyMesh& mesh,
131  const fileName& surfName,
132  const scalar nearDist,
133  const bool includeInside,
134  const bool includeOutside
135 )
136 :
137  topoSetSource(mesh),
138  surfName_(surfName),
139  nearDist_(nearDist),
140  includeInside_(includeInside),
141  includeOutside_(includeOutside)
142 {
143  checkSettings();
144 }
145 
146 
147 // Construct from dictionary
149 (
150  const polyMesh& mesh,
151  const dictionary& dict
152 )
153 :
154  topoSetSource(mesh),
155  surfName_(dict.lookup("file")),
156  nearDist_(readScalar(dict.lookup("nearDistance"))),
157  includeInside_(readBool(dict.lookup("includeInside"))),
158  includeOutside_(readBool(dict.lookup("includeOutside")))
159 {
160  checkSettings();
161 }
162 
163 
164 // Construct from Istream
166 (
167  const polyMesh& mesh,
168  Istream& is
169 )
170 :
171  topoSetSource(mesh),
172  surfName_(checkIs(is)),
173  nearDist_(readScalar(checkIs(is))),
174  includeInside_(readBool(checkIs(is))),
175  includeOutside_(readBool(checkIs(is)))
176 {
177  checkSettings();
178 }
179 
180 
181 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
182 
184 {}
185 
186 
187 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
188 
190 (
191  const topoSetSource::setAction action,
192  topoSet& set
193 ) const
194 {
195  if ( (action == topoSetSource::NEW) || (action == topoSetSource::ADD))
196  {
197  Info<< " Adding points in relation to surface " << surfName_
198  << " ..." << endl;
199 
200  combine(set, true);
201  }
202  else if (action == topoSetSource::DELETE)
203  {
204  Info<< " Removing points in relation to surface " << surfName_
205  << " ..." << endl;
206 
207  combine(set, false);
208  }
209 }
210 
211 
212 // ************************ vim: set sw=4 sts=4 et: ************************ //