Forward compatibility: flex
[foam-extend-3.2.git] / src / mesh / cfMesh / utilities / anisotropicMeshing / coordinateModification / boxScaling.C
blob020e69b5b26ccebdecbda8d5a5d2164b9bea7782
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | cfMesh: A library for mesh generation
4    \\    /   O peration     |
5     \\  /    A nd           | Author: Franjo Juretic (franjo.juretic@c-fields.com)
6      \\/     M anipulation  | Copyright (C) Creative Fields, Ltd.
7 -------------------------------------------------------------------------------
8 License
9     This file is part of cfMesh.
11     cfMesh 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 3 of the License, or (at your
14     option) any later version.
16     cfMesh 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 cfMesh.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "boxScaling.H"
27 #include "addToRunTimeSelectionTable.H"
28 #include "boundBox.H"
29 #include "plane.H"
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 namespace Foam
36 defineTypeNameAndDebug(boxScaling, 0);
37 addToRunTimeSelectionTable(coordinateModification, boxScaling, dictionary);
39 // * * * * * * * * * * * * * * Private member functions* * * * * * * * * * * //
41 void boxScaling::calculateBndBox()
43     pMin_ = centre_ - 0.5 * lengthVec_;
44     pMax_ = centre_ + 0.5 * lengthVec_;
47 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
49 boxScaling::boxScaling()
51     coordinateModification(),
52     centre_(),
53     lengthVec_(0.0, 0.0, 0.0),
54     scaleVec_(1.0, 1.0, 1.0),
55     pMin_(),
56     pMax_()
58     calculateBndBox();
61 boxScaling::boxScaling
63     const word& name,
64     const point& centre,
65     const scalar lengthX,
66     const scalar lengthY,
67     const scalar lengthZ,
68     const scalar scaleX,
69     const scalar scaleY,
70     const scalar scaleZ
73     coordinateModification(),
74     centre_(centre),
75     lengthVec_(lengthX, lengthY, lengthZ),
76     scaleVec_(scaleX, scaleY, scaleZ),
77     pMin_(),
78     pMax_()
80     calculateBndBox();
81     setName(name);
84 boxScaling::boxScaling
86     const word& name,
87     const dictionary& dict
90     coordinateModification(name, dict)
92     this->operator=(dict);
95 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
97 point boxScaling::origin() const
99     return centre_;
102 void boxScaling::translateAndModifyObject(const vector& disp)
104     centre_ += disp;
106     for(direction i=0;i<vector::nComponents;++i)
107         lengthVec_[i] /= scaleVec_[i];
109     calculateBndBox();
112 vector boxScaling::displacement(const point& p) const
114     vector disp;
116     for(direction i=0;i<vector::nComponents;++i)
117     {
118         const scalar dispVec = lengthVec_[i] * ((1.0/scaleVec_[i]) - 1.0);
119         const scalar t = ((p[i] - pMin_[i]) / lengthVec_[i]);
121         const scalar tBnd = Foam::max(0.0, Foam::min(t, 1.0));
123         disp[i] = tBnd * dispVec;
124     }
126     return disp;
129 vector boxScaling::backwardDisplacement(const point& p) const
131     vector disp;
133     for(direction i=0;i<vector::nComponents;++i)
134     {
135         const scalar dispVec = lengthVec_[i] * (scaleVec_[i] - 1.0);
137         const scalar t = ((p[i] - pMin_[i]) / lengthVec_[i]);
139         const scalar tBnd = Foam::max(0.0, Foam::min(t, 1.0));
141         disp[i] = tBnd * dispVec;
142     }
144     return disp;
147 bool boxScaling::combiningPossible() const
149     return true;
152 void boxScaling::boundingPlanes(PtrList<plane>&pl) const
154     pl.setSize(6);
155     label counter(0);
156     if( Foam::mag(scaleVec_.x() - 1.0) > VSMALL )
157     {
158         pl.set(counter++, new plane(pMin_, vector(1, 0, 0)));
159         pl.set(counter++, new plane(pMax_, vector(1, 0, 1)));
160     }
162     if( Foam::mag(scaleVec_.y() - 1.0) > VSMALL )
163     {
164         pl.set(counter++, new plane(pMin_, vector(0, 1, 0)));
165         pl.set(counter++, new plane(pMax_, vector(0, 1, 0)));
166     }
168     if( Foam::mag(scaleVec_.z() - 1.0) > VSMALL )
169     {
170         pl.set(counter++, new plane(pMin_, vector(0, 0, 1)));
171         pl.set(counter++, new plane(pMax_, vector(0, 0, 1)));
172     }
174     pl.setSize(counter);
177 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
179 dictionary boxScaling::dict(bool ignoreType) const
181     dictionary dict;
183     dict.add("type", type());
185     dict.add("centre", centre_);
186     dict.add("lengthX", lengthVec_.x());
187     dict.add("lengthY", lengthVec_.y());
188     dict.add("lengthZ", lengthVec_.z());
190     dict.add("scaleX", scaleVec_.x());
191     dict.add("scaleY", scaleVec_.y());
192     dict.add("scaleZ", scaleVec_.z());
194     return dict;
197 void boxScaling::write(Ostream& os) const
199     os  << " type:   " << type()
200         << " centre: " << centre_
201         << " lengthX: " << lengthVec_.x()
202         << " lengthY: " << lengthVec_.y()
203         << " lengthZ: " << lengthVec_.z()
204         << " scaleX:  " << scaleVec_.x()
205         << " scaleY:  " << scaleVec_.y()
206         << " scaleZ:  " << scaleVec_.z()
207         << endl;
210 void boxScaling::writeDict(Ostream& os, bool subDict) const
212     if( subDict )
213     {
214         os << indent << token::BEGIN_BLOCK << incrIndent << nl;
215     }
217     // only write type for derived types
218     if( type() != typeName_() )
219     {
220         os.writeKeyword("type") << type() << token::END_STATEMENT << nl;
221     }
223     os.writeKeyword("centre") << centre_ << token::END_STATEMENT << nl;
224     os.writeKeyword("lengthX") << lengthVec_.x() << token::END_STATEMENT << nl;
225     os.writeKeyword("lengthY") << lengthVec_.y() << token::END_STATEMENT << nl;
226     os.writeKeyword("lengthZ") << lengthVec_.z() << token::END_STATEMENT << nl;
227     os.writeKeyword("scaleX") << scaleVec_.x() << token::END_STATEMENT << nl;
228     os.writeKeyword("scaleY") << scaleVec_.y() << token::END_STATEMENT << nl;
229     os.writeKeyword("scaleZ") << scaleVec_.z() << token::END_STATEMENT << nl;
231     if( subDict )
232     {
233         os << decrIndent << indent << token::END_BLOCK << endl;
234     }
237 void boxScaling::operator=(const dictionary& d)
239     // allow as embedded sub-dictionary "coordinateSystem"
240     const dictionary& dict =
241     (
242         d.found(typeName_())
243       ? d.subDict(typeName_())
244       : d
245     );
247     // unspecified centre is (0 0 0)
248     if( dict.found("centre") )
249     {
250         dict.lookup("centre") >> centre_;
251     }
252     else
253     {
254         FatalErrorIn
255         (
256             "void boxScaling::operator=(const dictionary& d)"
257         ) << "Entry centre is not specified!" << exit(FatalError);
258         centre_ = vector::zero;
259     }
261     // specify lengthX
262     if( dict.found("lengthX") )
263     {
264         lengthVec_.x() = readScalar(dict.lookup("lengthX"));
265     }
266     else
267     {
268         FatalErrorIn
269         (
270             "void boxScaling::operator=(const dictionary& d)"
271         ) << "Entry lengthX is not specified!" << exit(FatalError);
272         lengthVec_.x() = 0.0;
273     }
275     // specify lengthY
276     if( dict.found("lengthY") )
277     {
278         lengthVec_.y() = readScalar(dict.lookup("lengthY"));
279     }
280     else
281     {
282         FatalErrorIn
283         (
284             "void boxScaling::operator=(const dictionary& d)"
285         ) << "Entry lengthY is not specified!" << exit(FatalError);
286         lengthVec_.y() = 0.0;
287     }
289     // specify lengthZ
290     if( dict.found("lengthZ") )
291     {
292         lengthVec_.z() = readScalar(dict.lookup("lengthZ"));
293     }
294     else
295     {
296         FatalErrorIn
297         (
298             "void boxScaling::operator=(const dictionary& d)"
299         ) << "Entry lengthZ is not specified!" << exit(FatalError);
300         lengthVec_.z() = 0.0;
301     }
303     // specify scaleX
304     if( dict.found("scaleX") )
305     {
306         scaleVec_.x() = readScalar(dict.lookup("scaleX"));
307     }
308     else
309     {
310         scaleVec_.x() = 1.0;
311     }
313     // specify scaleY
314     if( dict.found("scaleY") )
315     {
316         scaleVec_.y() = readScalar(dict.lookup("scaleY"));
317     }
318     else
319     {
320         scaleVec_.y() = 1.0;
321     }
323     // specify scaleX
324     if( dict.found("scaleZ") )
325     {
326         scaleVec_.z() = readScalar(dict.lookup("scaleZ"));
327     }
328     else
329     {
330         scaleVec_.z() = 1.0;
331     }
333     calculateBndBox();
336 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
338 Ostream& boxScaling::operator<<(Ostream& os) const
340     os << "name " << name() << nl;
341     write(os);
342     return os;
345 Ostream& operator<<(Ostream& os, const boxScaling& bs)
347     return bs.operator<<(os);
350 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
352 } // End namespace Foam
354 // ************************************************************************* //