initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / meshTools / octree / PointIndexHit.H
blob8af18ccc6358c543755e828240bbeed8b75b27fb
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 Class
26     Foam::PointIndexHit
28 Description
29     This class describes the interaction of (usually) a face and a point.
30     It carries the info of a successful hit and (if successful), 
31     returns the interaction point.
33     like pointHit but carries face (or cell, edge etc.) index
35 SourceFiles
37 \*---------------------------------------------------------------------------*/
39 #ifndef PointIndexHit_H
40 #define PointIndexHit_H
42 #include "bool.H"
43 #include "point.H"
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 namespace Foam
50 /*---------------------------------------------------------------------------*\
51                            Class PointIndexHit Declaration
52 \*---------------------------------------------------------------------------*/
54 template<class Point>
55 class PointIndexHit
57     // Private data
59         //- Hit success
60         bool hit_;
62         //- Point of hit; invalid for misses
63         Point hitPoint_;
65         //- label of face hit
66         label index_;
69 public:
71     // Constructors
73         //- Construct from components
74         PointIndexHit(const bool success, const Point& p, const label index)
75         :
76             hit_(success),
77             hitPoint_(p),
78             index_(index)
79         {}
81         //- Construct from point. Hit and distance set later
82         PointIndexHit(const Point& p)
83         :
84             hit_(false),
85             hitPoint_(p),
86             index_(-1)
87         {}
89         //- Construct null
90         PointIndexHit()
91         :
92             hit_(false),
93             hitPoint_(vector::zero),
94             index_(-1)
95         {}
97         //- Construct from Istream
98         PointIndexHit(Istream& is)
99         {
100             is >> *this;
101         }
104     // Member Functions
106         //- Is there a hit
107         bool hit() const
108         {
109             return hit_;
110         }
112         //- Return index
113         label index() const
114         {
115             return index_;
116         }
118         //- Return hit point
119         const Point& hitPoint() const
120         {
121             if (!hit_)
122             {
123                 FatalErrorIn("PointIndexHit::hitPoint() const")
124                     << "requested a hit point for a miss"
125                     << abort(FatalError);
126             }
128             return hitPoint_;
129         }
131         //- Return miss point
132         const Point& missPoint() const
133         {
134             if (hit_)
135             {
136                 FatalErrorIn("PointIndexHit::missPoint() const")
137                     << "requested a miss point for a hit"
138                     << abort(FatalError);
139             }
141             return hitPoint_;
142         }
144         //- Return point with no checking
145         const Point& rawPoint() const
146         {
147             return hitPoint_;
148         }
150         Point& rawPoint()
151         {
152             return hitPoint_;
153         }
155         void setHit()
156         {
157             hit_ = true;
158         }
160         void setMiss()
161         {
162             hit_ = false;
163         }
165         void setPoint(const Point& p)
166         {
167             hitPoint_ = p;
168         }
170         void setIndex(const label index)
171         {
172             index_ = index;
173         }
175         bool operator==(const PointIndexHit& rhs) const
176         {
177             return
178                 hit_ == rhs.hit()
179              && hitPoint_ == rhs.rawPoint()
180              && index_ == rhs.index();
181         }
183         bool operator!=(const PointIndexHit& rhs) const
184         {
185             return !operator==(rhs);
186         }
188         void write(Ostream& os)
189         {
190             if (hit())
191             {
192                 os << "hit:" << hitPoint() << " index:" << index();
193             }
194             else
195             {
196                 os << "miss:" << missPoint() << " index:" << index();
197             }
198         }
200         friend Ostream& operator<< (Ostream& os, const PointIndexHit& pHit)
201         {
202             if (os.format() == IOstream::ASCII)
203             {
204                 os  << pHit.hit_ << token::SPACE << pHit.hitPoint_
205                     << token::SPACE << pHit.index_;
206             }
207             else
208             {
209                 os.write
210                 (
211                     reinterpret_cast<const char*>(&pHit),
212                     sizeof(PointIndexHit)
213                 );
214             }
216             // Check state of Ostream
217             os.check("Ostream& operator<<(Ostream&, const PointIndexHit&)");
219             return os;
220         }
222         friend Istream& operator>>(Istream& is, PointIndexHit& pHit)
223         {
224             if (is.format() == IOstream::ASCII)
225             {
226                 return is >> pHit.hit_ >> pHit.hitPoint_ >> pHit.index_;
227             }
228             else
229             {
230                 is.read
231                 (
232                     reinterpret_cast<char*>(&pHit),
233                     sizeof(PointIndexHit)
234                 );
235             }
237             // Check state of Istream
238             is.check("Istream& operator>>(Istream&, PointIndexHit&)");
240             return is;
241         }
246 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
248 } // End namespace Foam
250 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
252 #endif
254 // ************************************************************************* //