ENH: autoLayerDriver: better layering information message
[OpenFOAM-2.0.x.git] / src / meshTools / searchableSurface / searchableSurfaces.C
blobd9436d3df9a9246261a00558d08aa6dda6fa8d89
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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"
28 #include "ListOps.H"
29 #include "Time.H"
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 defineTypeNameAndDebug(Foam::searchableSurfaces, 0);
36 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
38 // Construct with length.
39 Foam::searchableSurfaces::searchableSurfaces(const label size)
41     PtrList<searchableSurface>(size),
42     regionNames_(size),
43     allSurfaces_(identity(size))
47 //Foam::searchableSurfaces::searchableSurfaces
48 //(
49 //    const IOobject& io,
50 //    const PtrList<dictionary>& dicts
51 //)
52 //:
53 //    PtrList<searchableSurface>(dicts.size()),
54 //    regionNames_(dicts.size()),
55 //    allSurfaces_(identity(dicts.size()))
56 //{
57 //    forAll(dicts, surfI)
58 //    {
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
66 //        set
67 //        (
68 //            surfI,
69 //            searchableSurface::New
70 //            (
71 //                dict.lookup("type"),
72 //                namedIO(),
73 //                dict
74 //            )
75 //        );
76 //        const searchableSurface& s = operator[](surfI);
78 //        // Construct default region names by prepending surface name
79 //        // to region name.
80 //        const wordList& localNames = s.regions();
82 //        wordList globalNames(localNames.size());
83 //        forAll(localNames, regionI)
84 //        {
85 //            globalNames[regionI] = s.name() + '_' + localNames[regionI];
86 //        }
88 //        // See if dictionary provides any global region names.
89 //        if (dict.found("regions"))
90 //        {
91 //            const dictionary& regionsDict = dict.subDict("regions");
93 //            forAllConstIter(dictionary, regionsDict, iter)
94 //            {
95 //                const word& key = iter().keyword();
97 //                if (regionsDict.isDict(key))
98 //                {
99 //                    // Get the dictionary for region iter.key()
100 //                    const dictionary& regionDict = regionsDict.subDict(key);
102 //                    label index = findIndex(localNames, key);
104 //                    if (index == -1)
105 //                    {
106 //                        FatalErrorIn
107 //                        (
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);
114 //                    }
116 //                    globalNames[index] = word(regionDict.lookup("name"));
117 //                }
118 //            }
119 //        }
121 //        // Now globalNames contains the names of the regions.
122 //        Info<< "Surface:" << s.name() << " has regions:"
123 //            << endl;
124 //        forAll(globalNames, regionI)
125 //        {
126 //            Info<< "    " << globalNames[regionI] << endl;
127 //        }
129 //        // Create reverse lookup
130 //        forAll(globalNames, regionI)
131 //        {
132 //            regionNames_.insert
133 //            (
134 //                globalNames[regionI],
135 //                labelPair(surfI, regionI)
136 //            );
137 //        }
138 //    }
142 Foam::searchableSurfaces::searchableSurfaces
144     const IOobject& io,
145     const dictionary& topDict
148     PtrList<searchableSurface>(topDict.size()),
149     names_(topDict.size()),
150     regionNames_(topDict.size()),
151     allSurfaces_(identity(topDict.size()))
153     label surfI = 0;
154     forAllConstIter(dictionary, topDict, iter)
155     {
156         const word& key = iter().keyword();
158         if (!topDict.isDict(key))
159         {
160             FatalErrorIn
161             (
162                 "searchableSurfaces::searchableSurfaces"
163                 "( const IOobject&, const dictionary&)"
164             )   << "Found non-dictionary entry " << iter()
165                 << " in top-level dictionary " << topDict
166                 << exit(FatalError);
167         }
169         const dictionary& dict = topDict.subDict(key);
171         names_[surfI] = 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
184         set
185         (
186             surfI,
187             searchableSurface::New
188             (
189                 dict.lookup("type"),
190                 namedIO(),
191                 dict
192             )
193         );
194         const searchableSurface& s = operator[](surfI);
196         // Construct default region names by prepending surface name
197         // to region name.
198         const wordList& localNames = s.regions();
200         wordList& rNames = regionNames_[surfI];
201         rNames.setSize(localNames.size());
203         forAll(localNames, regionI)
204         {
205             rNames[regionI] = names_[surfI] + '_' + localNames[regionI];
206         }
208         // See if dictionary provides any global region names.
209         if (dict.found("regions"))
210         {
211             const dictionary& regionsDict = dict.subDict("regions");
213             forAllConstIter(dictionary, regionsDict, iter)
214             {
215                 const word& key = iter().keyword();
217                 if (regionsDict.isDict(key))
218                 {
219                     // Get the dictionary for region iter.keyword()
220                     const dictionary& regionDict = regionsDict.subDict(key);
222                     label index = findIndex(localNames, key);
224                     if (index == -1)
225                     {
226                         FatalErrorIn
227                         (
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
233                             << exit(FatalError);
234                     }
236                     rNames[index] = word(regionDict.lookup("name"));
237                 }
238             }
239         }
241         surfI++;
242     }
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
257 ) const
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
270 ) const
272     searchableSurfacesQueries::findAnyIntersection
273     (
274         *this,
275         allSurfaces_,
276         start,
277         end,
278         hitSurfaces,
279         hitInfo
280     );
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
291 ) const
293     searchableSurfacesQueries::findAllIntersections
294     (
295         *this,
296         allSurfaces_,
297         start,
298         end,
299         hitSurfaces,
300         hitInfo
301     );
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
312 ) const
314     return searchableSurfacesQueries::findNearest
315     (
316         *this,
317         allSurfaces_,
318         samples,
319         nearestDistSqr,
320         nearestSurfaces,
321         nearestInfo
322     );
326 //- Calculate point which is on a set of surfaces.
327 Foam::pointIndexHit Foam::searchableSurfaces::facesIntersection
329     const scalar initDistSqr,
330     const scalar convergenceDistSqr,
331     const point& start
332 ) const
334     return searchableSurfacesQueries::facesIntersection
335     (
336         *this,
337         allSurfaces_,
338         initDistSqr,
339         convergenceDistSqr,
340         start
341     );
344 // * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * * //
346 const Foam::searchableSurface& Foam::searchableSurfaces::operator[]
348     const word& surfName
349 ) const
351     const label surfI = findSurfaceID(surfName);
353     if (surfI < 0)
354     {
355         FatalErrorIn
356         (
357             "searchableSurfaces::operator[](const word&) const"
358         )   << "Surface named " << surfName << " not found." << nl
359             << "Available surface names: " << names_ << endl
360             << abort(FatalError);
361     }
363     return operator[](surfI);
367 Foam::searchableSurface& Foam::searchableSurfaces::operator[]
369     const word& surfName
372     const label surfI = findSurfaceID(surfName);
374     if (surfI < 0)
375     {
376         FatalErrorIn
377         (
378             "searchableSurfaces::operator[](const word&)"
379         )   << "Surface named " << surfName << " not found." << nl
380             << "Available surface names: " << names_ << endl
381             << abort(FatalError);
382     }
384     return operator[](surfI);
388 // ************************************************************************* //