1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
24 \*----------------------------------------------------------------------------*/
26 #include "searchableSurfaces.H"
27 #include "searchableSurfacesQueries.H"
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 defineTypeNameAndDebug(Foam::searchableSurfaces, 0);
36 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
38 // Construct with length.
39 Foam::searchableSurfaces::searchableSurfaces(const label size)
41 PtrList<searchableSurface>(size),
43 allSurfaces_(identity(size))
47 //Foam::searchableSurfaces::searchableSurfaces
49 // const IOobject& io,
50 // const PtrList<dictionary>& dicts
53 // PtrList<searchableSurface>(dicts.size()),
54 // regionNames_(dicts.size()),
55 // allSurfaces_(identity(dicts.size()))
57 // forAll(dicts, surfI)
59 // const dictionary& dict = dicts[surfI];
61 // // Make IOobject with correct name
62 // autoPtr<IOobject> namedIO(io.clone());
63 // namedIO().rename(dict.lookup("name"));
65 // // Create and hook surface
69 // searchableSurface::New
71 // dict.lookup("type"),
76 // const searchableSurface& s = operator[](surfI);
78 // // Construct default region names by prepending surface name
80 // const wordList& localNames = s.regions();
82 // wordList globalNames(localNames.size());
83 // forAll(localNames, regionI)
85 // globalNames[regionI] = s.name() + '_' + localNames[regionI];
88 // // See if dictionary provides any global region names.
89 // if (dict.found("regions"))
91 // const dictionary& regionsDict = dict.subDict("regions");
93 // forAllConstIter(dictionary, regionsDict, iter)
95 // const word& key = iter().keyword();
97 // if (regionsDict.isDict(key))
99 // // Get the dictionary for region iter.key()
100 // const dictionary& regionDict = regionsDict.subDict(key);
102 // label index = findIndex(localNames, key);
108 // "searchableSurfaces::searchableSurfaces"
109 // "( const IOobject&, const dictionary&)"
110 // ) << "Unknown region name " << key
111 // << " for surface " << s.name() << endl
112 // << "Valid region names are " << localNames
113 // << exit(FatalError);
116 // globalNames[index] = word(regionDict.lookup("name"));
121 // // Now globalNames contains the names of the regions.
122 // Info<< "Surface:" << s.name() << " has regions:"
124 // forAll(globalNames, regionI)
126 // Info<< " " << globalNames[regionI] << endl;
129 // // Create reverse lookup
130 // forAll(globalNames, regionI)
132 // regionNames_.insert
134 // globalNames[regionI],
135 // labelPair(surfI, regionI)
142 Foam::searchableSurfaces::searchableSurfaces
145 const dictionary& topDict
148 PtrList<searchableSurface>(topDict.size()),
149 names_(topDict.size()),
150 regionNames_(topDict.size()),
151 allSurfaces_(identity(topDict.size()))
154 forAllConstIter(dictionary, topDict, iter)
156 const word& key = iter().keyword();
158 if (!topDict.isDict(key))
162 "searchableSurfaces::searchableSurfaces"
163 "( const IOobject&, const dictionary&)"
164 ) << "Found non-dictionary entry " << iter()
165 << " in top-level dictionary " << topDict
169 const dictionary& dict = topDict.subDict(key);
172 dict.readIfPresent("name", names_[surfI]);
174 // Make IOobject with correct name
175 autoPtr<IOobject> namedIO(io.clone());
176 // Note: we would like to e.g. register triSurface 'sphere.stl' as
177 // 'sphere'. Unfortunately
178 // no support for having object read from different location than
179 // their object name. Maybe have stlTriSurfaceMesh which appends .stl
180 // when reading/writing?
181 namedIO().rename(key); // names_[surfI]
183 // Create and hook surface
187 searchableSurface::New
194 const searchableSurface& s = operator[](surfI);
196 // Construct default region names by prepending surface name
198 const wordList& localNames = s.regions();
200 wordList& rNames = regionNames_[surfI];
201 rNames.setSize(localNames.size());
203 forAll(localNames, regionI)
205 rNames[regionI] = names_[surfI] + '_' + localNames[regionI];
208 // See if dictionary provides any global region names.
209 if (dict.found("regions"))
211 const dictionary& regionsDict = dict.subDict("regions");
213 forAllConstIter(dictionary, regionsDict, iter)
215 const word& key = iter().keyword();
217 if (regionsDict.isDict(key))
219 // Get the dictionary for region iter.keyword()
220 const dictionary& regionDict = regionsDict.subDict(key);
222 label index = findIndex(localNames, key);
228 "searchableSurfaces::searchableSurfaces"
229 "( const IOobject&, const dictionary&)"
230 ) << "Unknown region name " << key
231 << " for surface " << s.name() << endl
232 << "Valid region names are " << localNames
236 rNames[index] = word(regionDict.lookup("name"));
244 // Trim (not really necessary since we don't allow non-dictionary entries)
245 PtrList<searchableSurface>::setSize(surfI);
246 names_.setSize(surfI);
247 regionNames_.setSize(surfI);
248 allSurfaces_.setSize(surfI);
252 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
254 Foam::label Foam::searchableSurfaces::findSurfaceID
256 const word& wantedName
259 return findIndex(names_, wantedName);
263 // Find any intersection
264 void Foam::searchableSurfaces::findAnyIntersection
266 const pointField& start,
267 const pointField& end,
268 labelList& hitSurfaces,
269 List<pointIndexHit>& hitInfo
272 searchableSurfacesQueries::findAnyIntersection
284 // Find intersections of edge nearest to both endpoints.
285 void Foam::searchableSurfaces::findAllIntersections
287 const pointField& start,
288 const pointField& end,
289 labelListList& hitSurfaces,
290 List<List<pointIndexHit> >& hitInfo
293 searchableSurfacesQueries::findAllIntersections
305 // Find nearest. Return -1 or nearest point
306 void Foam::searchableSurfaces::findNearest
308 const pointField& samples,
309 const scalarField& nearestDistSqr,
310 labelList& nearestSurfaces,
311 List<pointIndexHit>& nearestInfo
314 return searchableSurfacesQueries::findNearest
326 //- Calculate point which is on a set of surfaces.
327 Foam::pointIndexHit Foam::searchableSurfaces::facesIntersection
329 const scalar initDistSqr,
330 const scalar convergenceDistSqr,
334 return searchableSurfacesQueries::facesIntersection
344 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
346 const Foam::searchableSurface& Foam::searchableSurfaces::operator[]
351 const label surfI = findSurfaceID(surfName);
357 "searchableSurfaces::operator[](const word&) const"
358 ) << "Surface named " << surfName << " not found." << nl
359 << "Available surface names: " << names_ << endl
360 << abort(FatalError);
363 return operator[](surfI);
367 Foam::searchableSurface& Foam::searchableSurfaces::operator[]
372 const label surfI = findSurfaceID(surfName);
378 "searchableSurfaces::operator[](const word&)"
379 ) << "Surface named " << surfName << " not found." << nl
380 << "Available surface names: " << names_ << endl
381 << abort(FatalError);
384 return operator[](surfI);
388 // ************************************************************************* //