BUG: pointHitSort: define operator<
[OpenFOAM-1.7.x.git] / src / meshTools / coordinateSystems / coordinateSystem.C
blob9997506af8e209468d25de4d7bab569edbc22f38
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.
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 "IOstream.H"
27 #include "coordinateSystem.H"
28 #include "coordinateSystems.H"
29 #include "addToRunTimeSelectionTable.H"
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 namespace Foam
35     defineTypeNameAndDebug(coordinateSystem, 0);
36     defineRunTimeSelectionTable(coordinateSystem, dictionary);
37     defineRunTimeSelectionTable(coordinateSystem, origRotation);
40 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
42 Foam::coordinateSystem::coordinateSystem()
44     name_(type()),
45     note_(),
46     origin_(point::zero),
47     R_(),
48     Rtr_(sphericalTensor::I)
52 Foam::coordinateSystem::coordinateSystem
54     const word& name,
55     const coordinateSystem& cs
58     name_(name),
59     note_(),
60     origin_(cs.origin_),
61     R_(cs.R_),
62     Rtr_(cs.Rtr_)
66 Foam::coordinateSystem::coordinateSystem
68     const word& name,
69     const point& origin,
70     const coordinateRotation& cr
73     name_(name),
74     note_(),
75     origin_(origin),
76     R_(cr),
77     Rtr_(R_.T())
81 Foam::coordinateSystem::coordinateSystem
83     const word& name,
84     const point& origin,
85     const vector& axis,
86     const vector& dirn
89     name_(name),
90     note_(),
91     origin_(origin),
92     R_(axis, dirn),
93     Rtr_(R_.T())
97 Foam::coordinateSystem::coordinateSystem
99     const word& name,
100     const dictionary& dict
103     name_(name),
104     note_(),
105     origin_(point::zero),
106     R_(),
107     Rtr_(sphericalTensor::I)
109     operator=(dict);
113 Foam::coordinateSystem::coordinateSystem
115     const dictionary& dict
118     name_(type()),
119     note_(),
120     origin_(point::zero),
121     R_(),
122     Rtr_(sphericalTensor::I)
124     operator=(dict);
128 Foam::coordinateSystem::coordinateSystem
130     const dictionary& dict,
131     const objectRegistry& obr
134     name_(type()),
135     note_(),
136     origin_(point::zero),
137     R_(),
138     Rtr_(sphericalTensor::I)
140     const entry* entryPtr = dict.lookupEntryPtr(typeName_(), false, false);
142     // a simple entry is a lookup into global coordinateSystems
143     if (entryPtr && !entryPtr->isDict())
144     {
145         word csName;
146         entryPtr->stream() >> csName;
148         const coordinateSystems& csLst = coordinateSystems::New(obr);
150         label csId = csLst.find(csName);
151         if (debug)
152         {
153             Info<< "coordinateSystem::coordinateSystem"
154                 "(const dictionary&, const objectRegistry&):"
155                 << nl << "using global coordinate system: "
156                 << csName << "=" << csId << endl;
157         }
159         if (csId < 0)
160         {
161             FatalErrorIn
162             (
163                 "coordinateSystem::coordinateSystem"
164                 "(const dictionary&, const objectRegistry&)"
165             )   << "could not find coordinate system: " << csName << nl
166                 << "available coordinate systems: " << csLst.toc() << nl << nl
167                 << exit(FatalError);
168         }
170         // copy coordinateSystem, but assign the name as the typeName
171         // to avoid strange things in writeDict()
172         operator=(csLst[csId]);
173         name_ = typeName_();
174     }
175     else
176     {
177         operator=(dict);
178     }
182 Foam::coordinateSystem::coordinateSystem(Istream& is)
184     name_(is),
185     note_(),
186     origin_(point::zero),
187     R_(),
188     Rtr_(sphericalTensor::I)
190     dictionary dict(is);
191     operator=(dict);
195 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
197 Foam::coordinateSystem::~coordinateSystem()
201 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
203 Foam::dictionary Foam::coordinateSystem::dict(bool ignoreType) const
205     dictionary dict;
207     dict.add("name", name_);
209     // only write type for derived types
210     if (!ignoreType && type() != typeName_())
211     {
212         dict.add("type", type());
213     }
215     // The note entry is optional
216     if (note_.size())
217     {
218         dict.add("note", note_);
219     }
221     dict.add("origin", origin_);
222     dict.add("e1", e1());
223     dict.add("e3", e3());
225     return dict;
229 Foam::vector Foam::coordinateSystem::localToGlobal
231     const vector& local,
232     bool translate
233 ) const
235     if (translate)
236     {
237         return (R_ & local) + origin_;
238     }
239     else
240     {
241         return (R_ & local);
242     }
246 Foam::tmp<Foam::vectorField> Foam::coordinateSystem::localToGlobal
248     const vectorField& local,
249     bool translate
250 ) const
252     if (translate)
253     {
254         return (R_ & local) + origin_;
255     }
256     else
257     {
258         return (R_ & local);
259     }
263 Foam::vector Foam::coordinateSystem::globalToLocal
265     const vector& global,
266     bool translate
267 ) const
269     if (translate)
270     {
271         return (Rtr_ & (global - origin_));
272     }
273     else
274     {
275         return (Rtr_ & global);
276     }
280 Foam::tmp<Foam::vectorField> Foam::coordinateSystem::globalToLocal
282     const vectorField& global,
283     bool translate
284 ) const
286     if (translate)
287     {
288         return (Rtr_ & (global - origin_));
289     }
290     else
291     {
292         return (Rtr_ & global);
293     }
297 void Foam::coordinateSystem::write(Ostream& os) const
299     os  << type()
300         << " origin: " << origin() << " e1: " << e1() << " e3: " << e3();
304 void Foam::coordinateSystem::writeDict(Ostream& os, bool subDict) const
306     if (subDict)
307     {
308         os  << indent << name_ << nl
309             << indent << token::BEGIN_BLOCK << incrIndent << nl;
310     }
312     // only write type for derived types
313     if (type() != typeName_())
314     {
315         os.writeKeyword("type") << type() << token::END_STATEMENT << nl;
316     }
318     // The note entry is optional
319     if (note_.size())
320     {
321         os.writeKeyword("note") << note_ << token::END_STATEMENT << nl;
322     }
324     os.writeKeyword("origin") << origin_  << token::END_STATEMENT << nl;
325     os.writeKeyword("e1")     << e1()     << token::END_STATEMENT << nl;
326     os.writeKeyword("e3")     << e3()     << token::END_STATEMENT << nl;
328     if (subDict)
329     {
330         os << decrIndent << indent << token::END_BLOCK << endl;
331     }
334 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
336 void Foam::coordinateSystem::operator=(const dictionary& rhs)
338     if (debug)
339     {
340         Pout<< "coordinateSystem::operator=(const dictionary&) : "
341             << "assign from " << rhs << endl;
342     }
344     // allow as embedded sub-dictionary "coordinateSystem"
345     const dictionary& dict =
346     (
347         rhs.found(typeName_())
348       ? rhs.subDict(typeName_())
349       : rhs
350     );
352     // unspecified origin is (0 0 0)
353     origin_ = point::zero;
354     dict.readIfPresent("origin", origin_);
356     // The note entry is optional
357     note_.clear();
358     rhs.readIfPresent("note", note_);
360     // specify via coordinateRotation sub-dictionary
361     if (dict.found("coordinateRotation"))
362     {
363         R_  = coordinateRotation::New(dict.subDict("coordinateRotation"))();
364     }
365     else
366     {
367         // let coordinateRotation constructor extract the axes specification
368         R_ = coordinateRotation(dict);
369     }
371     Rtr_ = R_.T();
375 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
377 bool Foam::operator!=(const coordinateSystem& a, const coordinateSystem& b)
379     return (a.origin() != b.origin() || a.R() != b.R() || a.type() != b.type());
383 // * * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * //
385 Foam::Ostream& Foam::operator<<(Ostream& os, const coordinateSystem& cs)
387     cs.write(os);
388     os.check("Ostream& operator<<(Ostream&, const coordinateSystem&");
389     return os;
393 // ************************************************************************* //