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 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::PointIndexHit
28 
29 Description
30  This class describes the interaction of (usually) a face and a point.
31  It carries the info of a successful hit and (if successful),
32  returns the interaction point.
33 
34  like pointHit but carries face (or cell, edge etc.) index
35 
36 SourceFiles
37 
38 \*---------------------------------------------------------------------------*/
39 
40 #ifndef PointIndexHit_H
41 #define PointIndexHit_H
42 
43 #include "bool.H"
44 #include "point.H"
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 
51 /*---------------------------------------------------------------------------*\
52  Class PointIndexHit Declaration
53 \*---------------------------------------------------------------------------*/
54 
55 template<class Point>
56 class PointIndexHit
57 {
58  // Private data
59 
60  //- Hit success
61  bool hit_;
62 
63  //- Point of hit; invalid for misses
64  Point hitPoint_;
65 
66  //- Label of face hit
67  label index_;
68 
69 
70 public:
71 
72  // Constructors
73 
74  //- Construct from components
75  PointIndexHit(const bool success, const Point& p, const label index)
76  :
77  hit_(success),
78  hitPoint_(p),
79  index_(index)
80  {}
81 
82  //- Construct from point. Hit and distance set later
83  PointIndexHit(const Point& p)
84  :
85  hit_(false),
86  hitPoint_(p),
87  index_(-1)
88  {}
89 
90  //- Construct null
92  :
93  hit_(false),
94  hitPoint_(Zero),
95  index_(-1)
96  {}
97 
98  //- Construct from Istream
100  {
101  is >> *this;
102  }
103 
104 
105  // Member Functions
106 
107  //- Is there a hit
108  bool hit() const
109  {
110  return hit_;
111  }
112 
113  //- Return index
114  label index() const
115  {
116  return index_;
117  }
118 
119  //- Return hit point
120  const Point& hitPoint() const
121  {
122  if (!hit_)
123  {
125  << "requested a hit point for a miss"
126  << abort(FatalError);
127  }
128 
129  return hitPoint_;
130  }
131 
132  //- Return miss point
133  const Point& missPoint() const
134  {
135  if (hit_)
136  {
138  << "requested a miss point for a hit"
139  << abort(FatalError);
140  }
141 
142  return hitPoint_;
143  }
144 
145  //- Return point with no checking
146  const Point& rawPoint() const
147  {
148  return hitPoint_;
149  }
150 
151  Point& rawPoint()
152  {
153  return hitPoint_;
154  }
155 
156  void setHit()
157  {
158  hit_ = true;
159  }
160 
161  void setMiss()
162  {
163  hit_ = false;
164  }
165 
166  void setPoint(const Point& p)
167  {
168  hitPoint_ = p;
169  }
170 
171  void setIndex(const label index)
172  {
173  index_ = index;
174  }
175 
176  bool operator==(const PointIndexHit& rhs) const
177  {
178  return
179  hit_ == rhs.hit()
180  && hitPoint_ == rhs.rawPoint()
181  && index_ == rhs.index();
182  }
183 
184  bool operator!=(const PointIndexHit& rhs) const
185  {
186  return !operator==(rhs);
187  }
188 
189  void write(Ostream& os)
190  {
191  if (hit())
192  {
193  os << "hit:" << hitPoint() << " index:" << index();
194  }
195  else
196  {
197  os << "miss:" << missPoint() << " index:" << index();
198  }
199  }
200 
201  friend Ostream& operator<< (Ostream& os, const PointIndexHit& pHit)
202  {
203  if (os.format() == IOstream::ASCII)
204  {
205  os << pHit.hit_ << token::SPACE << pHit.hitPoint_
206  << token::SPACE << pHit.index_;
207  }
208  else
209  {
210  os.write
211  (
212  reinterpret_cast<const char*>(&pHit),
213  sizeof(PointIndexHit)
214  );
215  }
216 
217  os.check(FUNCTION_NAME);
218  return os;
219  }
220 
221 
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  // TODO (2019-08-06):
231  // cannot properly handle mixed-precision reading
232  // owning to bool and Point type.
233 
234  is.read
235  (
236  reinterpret_cast<char*>(&pHit),
237  sizeof(PointIndexHit)
238  );
239  }
240 
241  is.check(FUNCTION_NAME);
242  return is;
243  }
244 
245 };
246 
247 
248 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
249 
250 } // End namespace Foam
251 
252 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
253 
254 #endif
255 
256 // ************************************************************************* //
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::PointIndexHit::index
label index() const
Return index.
Definition: PointIndexHit.H:113
Foam::PointIndexHit::setIndex
void setIndex(const label index)
Definition: PointIndexHit.H:170
success
bool success
Definition: LISASMDCalcMethod1.H:16
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::PointIndexHit::PointIndexHit
PointIndexHit(Istream &is)
Construct from Istream.
Definition: PointIndexHit.H:98
Foam::PointIndexHit::operator>>
friend Istream & operator>>(Istream &is, PointIndexHit &pHit)
Definition: PointIndexHit.H:221
point.H
Foam::PointIndexHit::hit
bool hit() const
Is there a hit.
Definition: PointIndexHit.H:107
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:273
Foam::PointIndexHit::operator!=
bool operator!=(const PointIndexHit &rhs) const
Definition: PointIndexHit.H:183
Foam::PointIndexHit::setMiss
void setMiss()
Definition: PointIndexHit.H:160
Foam::PointIndexHit::operator<<
friend Ostream & operator<<(Ostream &os, const PointIndexHit &pHit)
Definition: PointIndexHit.H:200
Foam::PointIndexHit
This class describes the interaction of (usually) a face and a point. It carries the info of a succes...
Definition: PointIndexHit.H:55
Foam::PointIndexHit::rawPoint
const Point & rawPoint() const
Return point with no checking.
Definition: PointIndexHit.H:145
Foam::label
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:62
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::PointIndexHit::hitPoint
const Point & hitPoint() const
Return hit point.
Definition: PointIndexHit.H:119
Foam::PointIndexHit::setHit
void setHit()
Definition: PointIndexHit.H:155
Foam::PointIndexHit::rawPoint
Point & rawPoint()
Definition: PointIndexHit.H:150
bool.H
System bool.
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
Foam::FatalError
error FatalError
Foam::PointIndexHit::PointIndexHit
PointIndexHit(const Point &p)
Construct from point. Hit and distance set later.
Definition: PointIndexHit.H:82
Foam::Ostream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
Foam::PointIndexHit::setPoint
void setPoint(const Point &p)
Definition: PointIndexHit.H:165
Foam::PointIndexHit::missPoint
const Point & missPoint() const
Return miss point.
Definition: PointIndexHit.H:132
Foam::IOstreamOption::ASCII
"ascii"
Definition: IOstreamOption.H:66
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::token::SPACE
Space [isspace].
Definition: token.H:112
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:261
Point
CGAL::Point_3< K > Point
Definition: CGALIndexedPolyhedron.H:53
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::PointIndexHit::operator==
bool operator==(const PointIndexHit &rhs) const
Definition: PointIndexHit.H:175
Foam::PointIndexHit::PointIndexHit
PointIndexHit(const bool success, const Point &p, const label index)
Construct from components.
Definition: PointIndexHit.H:74
Foam::PointIndexHit::write
void write(Ostream &os)
Definition: PointIndexHit.H:188
Foam::PointIndexHit::PointIndexHit
PointIndexHit()
Construct null.
Definition: PointIndexHit.H:90
Foam::Istream::read
virtual Istream & read(token &)=0
Return next token from stream.