Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / src / OpenFOAM / meshes / boundBox / boundBox.C
blob6f1bc61221dabdd4be28b71ff7107a92c070ac8d
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2004-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 "boundBox.H"
27 #include "PstreamReduceOps.H"
28 #include "tmp.H"
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 const Foam::scalar Foam::boundBox::great(VGREAT);
34 const Foam::boundBox Foam::boundBox::greatBox
36     point(-VGREAT, -VGREAT, -VGREAT),
37     point(VGREAT, VGREAT, VGREAT)
41 const Foam::boundBox Foam::boundBox::invertedBox
43     point(VGREAT, VGREAT, VGREAT),
44     point(-VGREAT, -VGREAT, -VGREAT)
48 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
50 void Foam::boundBox::calculate(const UList<point>& points, const bool doReduce)
52     if (points.empty())
53     {
54         min_ = point::zero;
55         max_ = point::zero;
57         if (doReduce && Pstream::parRun())
58         {
59             // Use values that get overwritten by reduce minOp, maxOp below
60             min_ = point(VGREAT, VGREAT, VGREAT);
61             max_ = point(-VGREAT, -VGREAT, -VGREAT);
62         }
63     }
64     else
65     {
66         min_ = points[0];
67         max_ = points[0];
69         for (label i = 1; i < points.size(); i++)
70         {
71             min_ = ::Foam::min(min_, points[i]);
72             max_ = ::Foam::max(max_, points[i]);
73         }
74     }
76     // Reduce parallel information
77     if (doReduce)
78     {
79         reduce(min_, minOp<point>());
80         reduce(max_, maxOp<point>());
81     }
85 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
87 Foam::boundBox::boundBox(const UList<point>& points, const bool doReduce)
89     min_(point::zero),
90     max_(point::zero)
92     calculate(points, doReduce);
96 Foam::boundBox::boundBox(const tmp<pointField>& points, const bool doReduce)
98     min_(point::zero),
99     max_(point::zero)
101     calculate(points(), doReduce);
102     points.clear();
106 Foam::boundBox::boundBox
108     const UList<point>& points,
109     const labelUList& indices,
110     const bool doReduce
113     min_(point::zero),
114     max_(point::zero)
116     if (points.empty() || indices.empty())
117     {
118         if (doReduce && Pstream::parRun())
119         {
120             // Use values that get overwritten by reduce minOp, maxOp below
121             min_ = point(VGREAT, VGREAT, VGREAT);
122             max_ = point(-VGREAT, -VGREAT, -VGREAT);
123         }
124     }
125     else
126     {
127         min_ = points[indices[0]];
128         max_ = points[indices[0]];
130         for (label i=1; i < indices.size(); ++i)
131         {
132             min_ = ::Foam::min(min_, points[indices[i]]);
133             max_ = ::Foam::max(max_, points[indices[i]]);
134         }
135     }
137     // Reduce parallel information
138     if (doReduce)
139     {
140         reduce(min_, minOp<point>());
141         reduce(max_, maxOp<point>());
142     }
146 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
148 Foam::tmp<Foam::pointField> Foam::boundBox::points() const
150     tmp<pointField> tPts = tmp<pointField>(new pointField(8));
151     pointField& pt = tPts();
153     pt[0] = min_;                                   // min-x, min-y, min-z
154     pt[1] = point(max_.x(), min_.y(), min_.z());    // max-x, min-y, min-z
155     pt[2] = point(max_.x(), max_.y(), min_.z());    // max-x, max-y, min-z
156     pt[3] = point(min_.x(), max_.y(), min_.z());    // min-x, max-y, min-z
157     pt[4] = point(min_.x(), min_.y(), max_.z());    // min-x, min-y, max-z
158     pt[5] = point(max_.x(), min_.y(), max_.z());    // max-x, min-y, max-z
159     pt[6] = max_;                                   // max-x, max-y, max-z
160     pt[7] = point(min_.x(), max_.y(), max_.z());    // min-x, max-y, max-z
162     return tPts;
166 bool Foam::boundBox::contains(const UList<point>& points) const
168     if (points.empty())
169     {
170         return true;
171     }
173     forAll(points, i)
174     {
175         if (!contains(points[i]))
176         {
177             return false;
178         }
179     }
181     return true;
185 bool Foam::boundBox::contains
187     const UList<point>& points,
188     const labelUList& indices
189 ) const
191     if (points.empty() || indices.empty())
192     {
193         return true;
194     }
196     forAll(indices, i)
197     {
198         if (!contains(points[indices[i]]))
199         {
200             return false;
201         }
202     }
204     return true;
208 bool Foam::boundBox::containsAny(const UList<point>& points) const
210     if (points.empty())
211     {
212         return true;
213     }
215     forAll(points, i)
216     {
217         if (contains(points[i]))
218         {
219             return true;
220         }
221     }
223     return false;
227 bool Foam::boundBox::containsAny
229     const UList<point>& points,
230     const labelUList& indices
231 ) const
233     if (points.empty() || indices.empty())
234     {
235         return true;
236     }
238     forAll(indices, i)
239     {
240         if (contains(points[indices[i]]))
241         {
242             return true;
243         }
244     }
246     return false;
250 // * * * * * * * * * * * * * * * Ostream Operator  * * * * * * * * * * * * * //
252 Foam::Ostream& Foam::operator<<(Ostream& os, const boundBox& bb)
254     if (os.format() == IOstream::ASCII)
255     {
256         os << bb.min_ << token::SPACE << bb.max_;
257     }
258     else
259     {
260         os.write
261         (
262             reinterpret_cast<const char*>(&bb.min_),
263             sizeof(boundBox)
264         );
265     }
267     // Check state of Ostream
268     os.check("Ostream& operator<<(Ostream&, const boundBox&)");
269     return os;
273 Foam::Istream& Foam::operator>>(Istream& is, boundBox& bb)
275     if (is.format() == IOstream::ASCII)
276     {
277         is >> bb.min_ >> bb.max_;
278     }
279     else
280     {
281         is.read
282         (
283             reinterpret_cast<char*>(&bb.min_),
284             sizeof(boundBox)
285         );
286     }
288     // Check state of Istream
289     is.check("Istream& operator>>(Istream&, boundBox&)");
290     return is;
294 // ************************************************************************* //