PointIndexHit.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::PointIndexHit
29 
30 Description
31  This class describes the interaction of (usually) a face and a point.
32  It carries the info of a successful hit and (if successful),
33  returns the interaction point.
34 
35  like pointHit but carries face (or cell, edge etc.) index
36 
37 SourceFiles
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #ifndef PointIndexHit_H
42 #define PointIndexHit_H
43 
44 #include "bool.H"
45 #include "point.H"
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 // Forward Declarations
53 template<class PointType> class PointIndexHit;
54 
55 template<class PointType>
57 
58 template<class PointType>
60 
61 /*---------------------------------------------------------------------------*\
62  Class PointIndexHit Declaration
63 \*---------------------------------------------------------------------------*/
64 
65 template<class PointType>
66 class PointIndexHit
67 {
68  // Private Data
69 
70  //- Hit success
71  bool hit_;
72 
73  //- Point of hit; invalid for misses
74  PointType point_;
75 
76  //- Label of face hit
77  label index_;
78 
79 
80 public:
81 
82  // Public Typedefs
83 
84  //- The point type
85  typedef PointType point_type;
86 
87 
88  // Constructors
89 
90  //- Default construct. A zero point, with no hit and index = -1
92  :
93  hit_(false),
94  point_(Zero),
95  index_(-1)
96  {}
97 
98  //- Construct from a point, with no hit and index = -1
99  explicit PointIndexHit(const point_type& p)
100  :
101  hit_(false),
102  point_(p),
103  index_(-1)
104  {}
105 
106  //- Construct from components
108  (
109  const bool success,
110  const point_type& p,
111  const label index
112  )
113  :
114  hit_(success),
115  point_(p),
116  index_(index)
117  {}
118 
119  //- Construct from Istream
120  explicit PointIndexHit(Istream& is)
121  {
122  is >> *this;
123  }
124 
125 
126  // Member Functions
127 
128  // Access
129 
130  //- Is there a hit?
131  bool hit() const noexcept
132  {
133  return hit_;
134  }
135 
136  //- Return the hit index
137  label index() const noexcept
138  {
139  return index_;
140  }
141 
142  //- Return point, no checks
143  const point_type& point() const noexcept
144  {
145  return point_;
146  }
147 
148  //- Access the point, no checks
149  point_type& point() noexcept
150  {
151  return point_;
152  }
153 
154  //- Return hit point. Fatal if not hit.
155  const point_type& hitPoint() const
156  {
157  if (!hit_)
158  {
160  << "Requested a hit point, but it was not hit"
161  << abort(FatalError);
162  }
163  return point_;
164  }
165 
166  //- Return miss point. Fatal if hit.
167  const point_type& missPoint() const
168  {
169  if (hit_)
170  {
172  << "Requested a miss point, but it was hit"
173  << abort(FatalError);
174  }
175  return point_;
176  }
177 
178  //- The point, no checks. Same as point()
179  const point_type& rawPoint() const noexcept
180  {
181  return point_;
182  }
183 
184  //- The point, no checks. Same as point()
185  point_type& rawPoint() noexcept
186  {
187  return point_;
188  }
189 
190 
191  // Edit
192 
193  //- Set the hit status \em on
194  void setHit() noexcept
195  {
196  hit_ = true;
197  }
198 
199  //- Set the hit status \em off
200  void setMiss() noexcept
201  {
202  hit_ = false;
203  }
204 
205  //- Set the point
206  void setPoint(const point_type& p)
207  {
208  point_ = p;
209  }
210 
211  //- Set the index
212  void setIndex(const label index) noexcept
213  {
214  index_ = index;
215  }
216 
217  //- Set the point as \em hit and the hit-index
218  void hitPoint(const point_type& p, const label index)
219  {
220  point_ = p;
221  hit_ = true;
222  index_ = index;
223  }
224 
225 
226  // Write
227 
228  void write(Ostream& os)
229  {
230  os << (hit_ ? "hit:" : "miss:")
231  << point_ << " index:" << index_;
232  }
233 
234 
235  // Member Operators
236 
237  //- Test for equality of all components
238  bool operator==(const PointIndexHit& rhs) const
239  {
240  return
241  (
242  hit_ == rhs.hit_
243  && index_ == rhs.index_
244  && point_ == rhs.point_
245  );
246  }
247 
248  //- Test for inequality of components
249  bool operator!=(const PointIndexHit& rhs) const
250  {
251  return !(*this == rhs);
252  }
253 
254 
255  // IO Operators
256 
257  friend Ostream& operator<<(Ostream& os, const PointIndexHit& pHit)
258  {
259  if (os.format() == IOstream::ASCII)
260  {
261  os << pHit.hit_ << token::SPACE
262  << pHit.point_ << token::SPACE
263  << pHit.index_;
264  }
265  else
266  {
267  os.write
268  (
269  reinterpret_cast<const char*>(&pHit),
270  sizeof(PointIndexHit)
271  );
272  }
273 
274  os.check(FUNCTION_NAME);
275  return os;
276  }
277 
278 
279  friend Istream& operator>>(Istream& is, PointIndexHit& pHit)
280  {
281  if (is.format() == IOstream::ASCII)
282  {
283  is >> pHit.hit_ >> pHit.point_ >> pHit.index_;
284  }
285  else
286  {
287  // TODO (2019-08-06):
288  // cannot properly handle mixed-precision reading
289  // owing to bool and Point type.
290 
291  is.read
292  (
293  reinterpret_cast<char*>(&pHit),
294  sizeof(PointIndexHit)
295  );
296  }
297 
298  is.check(FUNCTION_NAME);
299  return is;
300  }
301 };
302 
303 
304 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
305 
306 } // End namespace Foam
307 
308 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
309 
310 #endif
311 
312 // ************************************************************************* //
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::PointIndexHit::setIndex
void setIndex(const label index) noexcept
Set the index.
Definition: PointIndexHit.H:211
success
bool success
Definition: LISASMDCalcMethod1.H:16
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::PointIndexHit::PointIndexHit
PointIndexHit(Istream &is)
Construct from Istream.
Definition: PointIndexHit.H:119
Foam::PointIndexHit::operator>>
friend Istream & operator>>(Istream &is, PointIndexHit &pHit)
Definition: PointIndexHit.H:278
point.H
Foam::PointIndexHit::rawPoint
point_type & rawPoint() noexcept
The point, no checks. Same as point()
Definition: PointIndexHit.H:184
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:286
Foam::PointIndexHit::setMiss
void setMiss() noexcept
Set the hit status off.
Definition: PointIndexHit.H:199
Foam::PointIndexHit::operator!=
bool operator!=(const PointIndexHit &rhs) const
Test for inequality of components.
Definition: PointIndexHit.H:248
Foam::PointIndexHit::missPoint
const point_type & missPoint() const
Return miss point. Fatal if hit.
Definition: PointIndexHit.H:166
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::PointIndexHit::hitPoint
const point_type & hitPoint() const
Return hit point. Fatal if not hit.
Definition: PointIndexHit.H:154
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::PointIndexHit::operator<<
friend Ostream & operator<<(Ostream &os, const PointIndexHit &pHit)
Definition: PointIndexHit.H:256
Foam::PointIndexHit
This class describes the interaction of (usually) a face and a point. It carries the info of a succes...
Definition: PointIndexHit.H:52
Foam::PointIndexHit::hit
bool hit() const noexcept
Is there a hit?
Definition: PointIndexHit.H:130
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::PointIndexHit::point_type
PointType point_type
The point type.
Definition: PointIndexHit.H:84
bool.H
System bool.
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
Foam::FatalError
error FatalError
Foam::PointIndexHit::rawPoint
const point_type & rawPoint() const noexcept
The point, no checks. Same as point()
Definition: PointIndexHit.H:178
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::PointIndexHit::setPoint
void setPoint(const point_type &p)
Set the point.
Definition: PointIndexHit.H:205
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::PointIndexHit::point
point_type & point() noexcept
Access the point, no checks.
Definition: PointIndexHit.H:148
Foam::PointIndexHit::index
label index() const noexcept
Return the hit index.
Definition: PointIndexHit.H:136
Foam::PointIndexHit::setHit
void setHit() noexcept
Set the hit status on.
Definition: PointIndexHit.H:193
Foam::token::SPACE
Space [isspace].
Definition: token.H:125
Foam::PointIndexHit::point
const point_type & point() const noexcept
Return point, no checks.
Definition: PointIndexHit.H:142
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::PointIndexHit::hitPoint
void hitPoint(const point_type &p, const label index)
Set the point as hit and the hit-index.
Definition: PointIndexHit.H:217
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::PointIndexHit::PointIndexHit
PointIndexHit(const point_type &p)
Construct from a point, with no hit and index = -1.
Definition: PointIndexHit.H:98
Foam::PointIndexHit::operator==
bool operator==(const PointIndexHit &rhs) const
Test for equality of all components.
Definition: PointIndexHit.H:237
Foam::PointIndexHit::write
void write(Ostream &os)
Definition: PointIndexHit.H:227
Foam::PointIndexHit::PointIndexHit
PointIndexHit()
Default construct. A zero point, with no hit and index = -1.
Definition: PointIndexHit.H:90
Foam::Istream::read
virtual Istream & read(token &)=0
Return next token from stream.