FreeFOAM The Cross-Platform CFD Toolkit
boundBox.C
Go to the documentation of this file.
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.
10 
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.
15 
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.
20 
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/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "boundBox.H"
28 #include <OpenFOAM/tmp.H>
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 const Foam::scalar Foam::boundBox::great(VGREAT);
33 
35 (
36  point(-VGREAT, -VGREAT, -VGREAT),
37  point(VGREAT, VGREAT, VGREAT)
38 );
39 
40 
42 (
43  point(VGREAT, VGREAT, VGREAT),
44  point(-VGREAT, -VGREAT, -VGREAT)
45 );
46 
47 
48 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
49 
50 void Foam::boundBox::calculate(const pointField& points, const bool doReduce)
51 {
52  if (points.empty())
53  {
54  min_ = point::zero;
55  max_ = point::zero;
56 
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];
68 
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  }
75 
76  // Reduce parallel information
77  if (doReduce)
78  {
79  reduce(min_, minOp<point>());
80  reduce(max_, maxOp<point>());
81  }
82 }
83 
84 
85 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
86 
87 Foam::boundBox::boundBox(const pointField& points, const bool doReduce)
88 :
89  min_(point::zero),
90  max_(point::zero)
91 {
92  calculate(points, doReduce);
93 }
94 
95 
96 Foam::boundBox::boundBox(const tmp<pointField>& points, const bool doReduce)
97 :
98  min_(point::zero),
99  max_(point::zero)
100 {
101  calculate(points(), doReduce);
102  points.clear();
103 }
104 
105 
107 {
108  operator>>(is, *this);
109 }
110 
111 
112 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
113 
115 {
116  if (os.format() == IOstream::ASCII)
117  {
118  os << bb.min_ << token::SPACE << bb.max_;
119  }
120  else
121  {
122  os.write
123  (
124  reinterpret_cast<const char*>(&bb.min_),
125  sizeof(boundBox)
126  );
127  }
128 
129  // Check state of Ostream
130  os.check("Ostream& operator<<(Ostream&, const boundBox&)");
131  return os;
132 }
133 
134 
135 Foam::Istream& Foam::operator>>(Istream& is, boundBox& bb)
136 {
137  if (is.format() == IOstream::ASCII)
138  {
139  return is >> bb.min_ >> bb.max_;
140  }
141  else
142  {
143  is.read
144  (
145  reinterpret_cast<char*>(&bb.min_),
146  sizeof(boundBox)
147  );
148  }
149 
150  // Check state of Istream
151  is.check("Istream& operator>>(Istream&, boundBox&)");
152  return is;
153 }
154 
155 // ************************ vim: set sw=4 sts=4 et: ************************ //