OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

PointIndexHit.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     Foam::PointIndexHit
00027 
00028 Description
00029     This class describes the interaction of (usually) a face and a point.
00030     It carries the info of a successful hit and (if successful), 
00031     returns the interaction point.
00032 
00033     like pointHit but carries face (or cell, edge etc.) index
00034 
00035 SourceFiles
00036 
00037 \*---------------------------------------------------------------------------*/
00038 
00039 #ifndef PointIndexHit_H
00040 #define PointIndexHit_H
00041 
00042 #include "bool.H"
00043 #include "point.H"
00044 
00045 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00046 
00047 namespace Foam
00048 {
00049 
00050 /*---------------------------------------------------------------------------*\
00051                            Class PointIndexHit Declaration
00052 \*---------------------------------------------------------------------------*/
00053 
00054 template<class Point>
00055 class PointIndexHit
00056 {
00057     // Private data
00058 
00059         //- Hit success
00060         bool hit_;
00061 
00062         //- Point of hit; invalid for misses
00063         Point hitPoint_;
00064 
00065         //- label of face hit
00066         label index_;
00067 
00068 
00069 public:
00070 
00071     // Constructors
00072 
00073         //- Construct from components
00074         PointIndexHit(const bool success, const Point& p, const label index)
00075         :
00076             hit_(success),
00077             hitPoint_(p),
00078             index_(index)
00079         {}
00080 
00081         //- Construct from point. Hit and distance set later
00082         PointIndexHit(const Point& p)
00083         :
00084             hit_(false),
00085             hitPoint_(p),
00086             index_(-1)
00087         {}
00088 
00089         //- Construct null
00090         PointIndexHit()
00091         :
00092             hit_(false),
00093             hitPoint_(vector::zero),
00094             index_(-1)
00095         {}
00096 
00097         //- Construct from Istream
00098         PointIndexHit(Istream& is)
00099         {
00100             is >> *this;
00101         }
00102 
00103 
00104     // Member Functions
00105 
00106         //- Is there a hit
00107         bool hit() const
00108         {
00109             return hit_;
00110         }
00111 
00112         //- Return index
00113         label index() const
00114         {
00115             return index_;
00116         }
00117 
00118         //- Return hit point
00119         const Point& hitPoint() const
00120         {
00121             if (!hit_)
00122             {
00123                 FatalErrorIn("PointIndexHit::hitPoint() const")
00124                     << "requested a hit point for a miss"
00125                     << abort(FatalError);
00126             }
00127 
00128             return hitPoint_;
00129         }
00130 
00131         //- Return miss point
00132         const Point& missPoint() const
00133         {
00134             if (hit_)
00135             {
00136                 FatalErrorIn("PointIndexHit::missPoint() const")
00137                     << "requested a miss point for a hit"
00138                     << abort(FatalError);
00139             }
00140 
00141             return hitPoint_;
00142         }
00143 
00144         //- Return point with no checking
00145         const Point& rawPoint() const
00146         {
00147             return hitPoint_;
00148         }
00149 
00150         Point& rawPoint()
00151         {
00152             return hitPoint_;
00153         }
00154 
00155         void setHit()
00156         {
00157             hit_ = true;
00158         }
00159 
00160         void setMiss()
00161         {
00162             hit_ = false;
00163         }
00164 
00165         void setPoint(const Point& p)
00166         {
00167             hitPoint_ = p;
00168         }
00169 
00170         void setIndex(const label index)
00171         {
00172             index_ = index;
00173         }
00174 
00175         bool operator==(const PointIndexHit& rhs) const
00176         {
00177             return
00178                 hit_ == rhs.hit()
00179              && hitPoint_ == rhs.rawPoint()
00180              && index_ == rhs.index();
00181         }
00182 
00183         bool operator!=(const PointIndexHit& rhs) const
00184         {
00185             return !operator==(rhs);
00186         }
00187 
00188         void write(Ostream& os)
00189         {
00190             if (hit())
00191             {
00192                 os << "hit:" << hitPoint() << " index:" << index();
00193             }
00194             else
00195             {
00196                 os << "miss:" << missPoint() << " index:" << index();
00197             }
00198         }
00199 
00200         friend Ostream& operator<< (Ostream& os, const PointIndexHit& pHit)
00201         {
00202             if (os.format() == IOstream::ASCII)
00203             {
00204                 os  << pHit.hit_ << token::SPACE << pHit.hitPoint_
00205                     << token::SPACE << pHit.index_;
00206             }
00207             else
00208             {
00209                 os.write
00210                 (
00211                     reinterpret_cast<const char*>(&pHit),
00212                     sizeof(PointIndexHit)
00213                 );
00214             }
00215 
00216             // Check state of Ostream
00217             os.check("Ostream& operator<<(Ostream&, const PointIndexHit&)");
00218 
00219             return os;
00220         }
00221 
00222         friend Istream& operator>>(Istream& is, PointIndexHit& pHit)
00223         {
00224             if (is.format() == IOstream::ASCII)
00225             {
00226                 return is >> pHit.hit_ >> pHit.hitPoint_ >> pHit.index_;
00227             }
00228             else
00229             {
00230                 is.read
00231                 (
00232                     reinterpret_cast<char*>(&pHit),
00233                     sizeof(PointIndexHit)
00234                 );
00235             }
00236 
00237             // Check state of Istream
00238             is.check("Istream& operator>>(Istream&, PointIndexHit&)");
00239 
00240             return is;
00241         }
00242 
00243 };
00244 
00245 
00246 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00247 
00248 } // End namespace Foam
00249 
00250 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00251 
00252 #endif
00253 
00254 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd