PointIndexHit.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation
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 Class
25  Foam::PointIndexHit
26 
27 Description
28  This class describes the interaction of (usually) a face and a point.
29  It carries the info of a successful hit and (if successful),
30  returns the interaction point.
31 
32  like pointHit but carries face (or cell, edge etc.) index
33 
34 SourceFiles
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef PointIndexHit_H
39 #define PointIndexHit_H
40 
41 #include "bool.H"
42 #include "point.H"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 /*---------------------------------------------------------------------------*\
50  Class PointIndexHit Declaration
51 \*---------------------------------------------------------------------------*/
52 
53 template<class Point>
54 class PointIndexHit
55 {
56  // Private Data
57 
58  //- Hit success
59  bool hit_;
60 
61  //- Point of hit; invalid for misses
62  Point hitPoint_;
63 
64  //- Label of face hit
65  label index_;
66 
67 
68 public:
69 
70  // Constructors
71 
72  //- Construct from components
73  PointIndexHit(const bool success, const Point& p, const label index)
74  :
75  hit_(success),
76  hitPoint_(p),
77  index_(index)
78  {}
79 
80  //- Construct from point. Hit and distance set later
81  PointIndexHit(const Point& p)
82  :
83  hit_(false),
84  hitPoint_(p),
85  index_(-1)
86  {}
87 
88  //- Construct null
90  :
91  hit_(false),
92  hitPoint_(Zero),
93  index_(-1)
94  {}
95 
96  //- Construct from Istream
98  {
99  is >> *this;
100  }
101 
102 
103  // Member Functions
104 
105  //- Is there a hit
106  bool hit() const
107  {
108  return hit_;
109  }
110 
111  //- Return index
112  label index() const
113  {
114  return index_;
115  }
116 
117  //- Return hit point
118  const Point& hitPoint() const
119  {
120  if (!hit_)
121  {
123  << "requested a hit point for a miss"
124  << abort(FatalError);
125  }
126 
127  return hitPoint_;
128  }
129 
130  //- Return miss point
131  const Point& missPoint() const
132  {
133  if (hit_)
134  {
136  << "requested a miss point for a hit"
137  << abort(FatalError);
138  }
139 
140  return hitPoint_;
141  }
142 
143  //- Return point with no checking
144  const Point& rawPoint() const
145  {
146  return hitPoint_;
147  }
149  Point& rawPoint()
150  {
151  return hitPoint_;
152  }
154  void setHit()
155  {
156  hit_ = true;
157  }
159  void setMiss()
160  {
161  hit_ = false;
162  }
164  void setPoint(const Point& p)
165  {
166  hitPoint_ = p;
167  }
169  void setIndex(const label index)
170  {
171  index_ = index;
172  }
174  bool operator==(const PointIndexHit& rhs) const
175  {
176  return
177  hit_ == rhs.hit()
178  && hitPoint_ == rhs.rawPoint()
179  && index_ == rhs.index();
180  }
182  bool operator!=(const PointIndexHit& rhs) const
183  {
184  return !operator==(rhs);
185  }
187  void write(Ostream& os)
188  {
189  if (hit())
190  {
191  os << "hit:" << hitPoint() << " index:" << index();
192  }
193  else
194  {
195  os << "miss:" << missPoint() << " index:" << index();
196  }
197  }
199  friend Ostream& operator<< (Ostream& os, const PointIndexHit& pHit)
200  {
201  if (os.format() == IOstream::ASCII)
202  {
203  os << pHit.hit_ << token::SPACE << pHit.hitPoint_
204  << token::SPACE << pHit.index_;
205  }
206  else
207  {
208  os.write
209  (
210  reinterpret_cast<const char*>(&pHit),
211  sizeof(PointIndexHit)
212  );
213  }
214 
215  // Check state of Ostream
216  os.check("Ostream& operator<<(Ostream&, const PointIndexHit&)");
217 
218  return os;
219  }
221  friend Istream& operator>>(Istream& is, PointIndexHit& pHit)
222  {
223  if (is.format() == IOstream::ASCII)
224  {
225  return is >> pHit.hit_ >> pHit.hitPoint_ >> pHit.index_;
226  }
227  else
228  {
229  is.read
230  (
231  reinterpret_cast<char*>(&pHit),
232  sizeof(PointIndexHit)
233  );
234  }
235 
236  // Check state of Istream
237  is.check("Istream& operator>>(Istream&, PointIndexHit&)");
238 
239  return is;
240  }
241 
242 };
243 
244 
245 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
246 
247 } // End namespace Foam
248 
249 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
250 
251 #endif
252 
253 // ************************************************************************* //
PointIndexHit()
Construct null.
Definition: PointIndexHit.H:88
void write(Ostream &os)
virtual Ostream & write(const char)=0
Write character.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
const Point & missPoint() const
Return miss point.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void setIndex(const label index)
This class describes the interaction of (usually) a face and a point. It carries the info of a succes...
Definition: PointIndexHit.H:53
void setPoint(const Point &p)
const Point & hitPoint() const
Return hit point.
virtual Istream & read(token &)=0
Return next token from stream.
bool operator!=(const PointIndexHit &rhs) const
bool hit() const
Is there a hit.
static const zero Zero
Definition: zero.H:97
streamFormat format() const
Return current stream format.
Definition: IOstream.H:374
errorManip< error > abort(error &err)
Definition: errorManip.H:131
const Point & rawPoint() const
Return point with no checking.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
bool success
bool operator==(const PointIndexHit &rhs) const
volScalarField & p
friend Ostream & operator<<(Ostream &os, const PointIndexHit &pHit)
label index() const
Return index.
friend Istream & operator>>(Istream &is, PointIndexHit &pHit)
System bool.
Namespace for OpenFOAM.