initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / meshTools / searchableSurface / searchableSurfaces.C
blobcec34bd7e792669ace03bad6f68ea2e0b12ffe9b
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     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, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*----------------------------------------------------------------------------*/
27 #include "searchableSurfaces.H"
28 #include "searchableSurfacesQueries.H"
29 #include "ListOps.H"
30 #include "Time.H"
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 namespace Foam
37 defineTypeNameAndDebug(searchableSurfaces, 0);
42 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
44 // Construct with length.
45 Foam::searchableSurfaces::searchableSurfaces(const label size)
47     PtrList<searchableSurface>(size),
48     regionNames_(size),
49     allSurfaces_(identity(size))
53 //Foam::searchableSurfaces::searchableSurfaces
54 //(
55 //    const IOobject& io,
56 //    const PtrList<dictionary>& dicts
57 //)
58 //:
59 //    PtrList<searchableSurface>(dicts.size()),
60 //    regionNames_(dicts.size()),
61 //    allSurfaces_(identity(dicts.size()))
62 //{
63 //    forAll(dicts, surfI)
64 //    {
65 //        const dictionary& dict = dicts[surfI];
67 //        // Make IOobject with correct name
68 //        autoPtr<IOobject> namedIO(io.clone());
69 //        namedIO().rename(dict.lookup("name"));
71 //        // Create and hook surface
72 //        set
73 //        (
74 //            surfI,
75 //            searchableSurface::New
76 //            (
77 //                dict.lookup("type"),
78 //                namedIO(),
79 //                dict
80 //            )
81 //        );
82 //        const searchableSurface& s = operator[](surfI);
84 //        // Construct default region names by prepending surface name
85 //        // to region name.
86 //        const wordList& localNames = s.regions();
88 //        wordList globalNames(localNames.size());
89 //        forAll(localNames, regionI)
90 //        {
91 //            globalNames[regionI] = s.name() + '_' + localNames[regionI];
92 //        }
94 //        // See if dictionary provides any global region names.
95 //        if (dict.found("regions"))
96 //        {
97 //            const dictionary& regionsDict = dict.subDict("regions");
99 //            forAllConstIter(dictionary, regionsDict, iter)
100 //            {
101 //                const word& key = iter().keyword();
103 //                if (regionsDict.isDict(key))
104 //                {
105 //                    // Get the dictionary for region iter.key()
106 //                    const dictionary& regionDict = regionsDict.subDict(key);
108 //                    label index = findIndex(localNames, key);
110 //                    if (index == -1)
111 //                    {
112 //                        FatalErrorIn
113 //                        (
114 //                            "searchableSurfaces::searchableSurfaces"
115 //                            "( const IOobject&, const dictionary&)"
116 //                        )   << "Unknown region name " << key
117 //                            << " for surface " << s.name() << endl
118 //                            << "Valid region names are " << localNames
119 //                            << exit(FatalError);
120 //                    }
122 //                    globalNames[index] = word(regionDict.lookup("name"));
123 //                }
124 //            }
125 //        }
127 //        // Now globalNames contains the names of the regions.
128 //        Info<< "Surface:" << s.name() << " has regions:"
129 //            << endl;
130 //        forAll(globalNames, regionI)
131 //        {
132 //            Info<< "    " << globalNames[regionI] << endl;
133 //        }
135 //        // Create reverse lookup
136 //        forAll(globalNames, regionI)
137 //        {
138 //            regionNames_.insert
139 //            (
140 //                globalNames[regionI],
141 //                labelPair(surfI, regionI)
142 //            );
143 //        }
144 //    }
148 Foam::searchableSurfaces::searchableSurfaces
150     const IOobject& io,
151     const dictionary& topDict
154     PtrList<searchableSurface>(topDict.size()),
155     names_(topDict.size()),
156     regionNames_(topDict.size()),
157     allSurfaces_(identity(topDict.size()))
159     label surfI = 0;
160     forAllConstIter(dictionary, topDict, iter)
161     {
162         const word& key = iter().keyword();
164         if (!topDict.isDict(key))
165         {
166             FatalErrorIn
167             (
168                 "searchableSurfaces::searchableSurfaces"
169                 "( const IOobject&, const dictionary&)"
170             )   << "Found non-dictionary entry " << iter()
171                 << " in top-level dictionary " << topDict
172                 << exit(FatalError);
173         }
175         const dictionary& dict = topDict.subDict(key);
177         names_[surfI] = key;
178         dict.readIfPresent("name", names_[surfI]);
180         // Make IOobject with correct name
181         autoPtr<IOobject> namedIO(io.clone());
182         // Note: we would like to e.g. register triSurface 'sphere.stl' as
183         // 'sphere'. Unfortunately
184         // no support for having object read from different location than
185         // their object name. Maybe have stlTriSurfaceMesh which appends .stl
186         // when reading/writing?
187         namedIO().rename(key);  // names_[surfI]
189         // Create and hook surface
190         set
191         (
192             surfI,
193             searchableSurface::New
194             (
195                 dict.lookup("type"),
196                 namedIO(),
197                 dict
198             )
199         );
200         const searchableSurface& s = operator[](surfI);
202         // Construct default region names by prepending surface name
203         // to region name.
204         const wordList& localNames = s.regions();
206         wordList& rNames = regionNames_[surfI];
207         rNames.setSize(localNames.size());
209         forAll(localNames, regionI)
210         {
211             rNames[regionI] = names_[surfI] + '_' + localNames[regionI];
212         }
214         // See if dictionary provides any global region names.
215         if (dict.found("regions"))
216         {
217             const dictionary& regionsDict = dict.subDict("regions");
219             forAllConstIter(dictionary, regionsDict, iter)
220             {
221                 const word& key = iter().keyword();
223                 if (regionsDict.isDict(key))
224                 {
225                     // Get the dictionary for region iter.keyword()
226                     const dictionary& regionDict = regionsDict.subDict(key);
228                     label index = findIndex(localNames, key);
230                     if (index == -1)
231                     {
232                         FatalErrorIn
233                         (
234                             "searchableSurfaces::searchableSurfaces"
235                             "( const IOobject&, const dictionary&)"
236                         )   << "Unknown region name " << key
237                             << " for surface " << s.name() << endl
238                             << "Valid region names are " << localNames
239                             << exit(FatalError);
240                     }
242                     rNames[index] = word(regionDict.lookup("name"));
243                 }
244             }
245         }
247         surfI++;
248     }
250     // Trim (not really necessary since we don't allow non-dictionary entries)
251     PtrList<searchableSurface>::setSize(surfI);
252     names_.setSize(surfI);
253     regionNames_.setSize(surfI);
254     allSurfaces_.setSize(surfI);
258 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
260 Foam::label Foam::searchableSurfaces::findSurfaceID(const word& wantedName)
261  const
263     return findIndex(names_, wantedName);
267 // Find any intersection
268 void Foam::searchableSurfaces::findAnyIntersection
270     const pointField& start,
271     const pointField& end,
272     labelList& hitSurfaces,
273     List<pointIndexHit>& hitInfo
274 ) const
276     searchableSurfacesQueries::findAnyIntersection
277     (
278         *this,
279         allSurfaces_,
280         start,
281         end,
282         hitSurfaces,
283         hitInfo
284     );
288 // Find intersections of edge nearest to both endpoints.
289 void Foam::searchableSurfaces::findAllIntersections
291     const pointField& start,
292     const pointField& end,
293     labelListList& hitSurfaces,
294     List<List<pointIndexHit> >& hitInfo
295 ) const
297     searchableSurfacesQueries::findAllIntersections
298     (
299         *this,
300         allSurfaces_,
301         start,
302         end,
303         hitSurfaces,
304         hitInfo
305     );
309 // Find nearest. Return -1 or nearest point
310 void Foam::searchableSurfaces::findNearest
312     const pointField& samples,
313     const scalarField& nearestDistSqr,
314     labelList& nearestSurfaces,
315     List<pointIndexHit>& nearestInfo
316 ) const
318     return searchableSurfacesQueries::findNearest
319     (
320         *this,
321         allSurfaces_,
322         samples,
323         nearestDistSqr,
324         nearestSurfaces,
325         nearestInfo
326     );
330 //- Calculate point which is on a set of surfaces.
331 Foam::pointIndexHit Foam::searchableSurfaces::facesIntersection
333     const scalar initDistSqr,
334     const scalar convergenceDistSqr,
335     const point& start
336 ) const
338     return searchableSurfacesQueries::facesIntersection
339     (
340         *this,
341         allSurfaces_,
342         initDistSqr,
343         convergenceDistSqr,
344         start
345     );
349 // ************************************************************************* //