FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 Class
00025     Foam::PointIndexHit
00026 
00027 Description
00028     This class describes the interaction of (usually) a face and a point.
00029     It carries the info of a successful hit and (if successful), 
00030     returns the interaction point.
00031 
00032     like pointHit but carries face (or cell, edge etc.) index
00033 
00034 SourceFiles
00035 
00036 \*---------------------------------------------------------------------------*/
00037 
00038 #ifndef PointIndexHit_H
00039 #define PointIndexHit_H
00040 
00041 #include <OpenFOAM/bool.H>
00042 #include <OpenFOAM/point.H>
00043 
00044 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00045 
00046 namespace Foam
00047 {
00048 
00049 /*---------------------------------------------------------------------------*\
00050                            Class PointIndexHit Declaration
00051 \*---------------------------------------------------------------------------*/
00052 
00053 template<class Point>
00054 class PointIndexHit
00055 {
00056     // Private data
00057 
00058         //- Hit success
00059         bool hit_;
00060 
00061         //- Point of hit; invalid for misses
00062         Point hitPoint_;
00063 
00064         //- label of face hit
00065         label index_;
00066 
00067 
00068 public:
00069 
00070     // Constructors
00071 
00072         //- Construct from components
00073         PointIndexHit(const bool success, const Point& p, const label index)
00074         :
00075             hit_(success),
00076             hitPoint_(p),
00077             index_(index)
00078         {}
00079 
00080         //- Construct from point. Hit and distance set later
00081         PointIndexHit(const Point& p)
00082         :
00083             hit_(false),
00084             hitPoint_(p),
00085             index_(-1)
00086         {}
00087 
00088         //- Construct null
00089         PointIndexHit()
00090         :
00091             hit_(false),
00092             hitPoint_(vector::zero),
00093             index_(-1)
00094         {}
00095 
00096         //- Construct from Istream
00097         PointIndexHit(Istream& is)
00098         {
00099             is >> *this;
00100         }
00101 
00102 
00103     // Member Functions
00104 
00105         //- Is there a hit
00106         bool hit() const
00107         {
00108             return hit_;
00109         }
00110 
00111         //- Return index
00112         label index() const
00113         {
00114             return index_;
00115         }
00116 
00117         //- Return hit point
00118         const Point& hitPoint() const
00119         {
00120             if (!hit_)
00121             {
00122                 FatalErrorIn("PointIndexHit::hitPoint() const")
00123                     << "requested a hit point for a miss"
00124                     << abort(FatalError);
00125             }
00126 
00127             return hitPoint_;
00128         }
00129 
00130         //- Return miss point
00131         const Point& missPoint() const
00132         {
00133             if (hit_)
00134             {
00135                 FatalErrorIn("PointIndexHit::missPoint() const")
00136                     << "requested a miss point for a hit"
00137                     << abort(FatalError);
00138             }
00139 
00140             return hitPoint_;
00141         }
00142 
00143         //- Return point with no checking
00144         const Point& rawPoint() const
00145         {
00146             return hitPoint_;
00147         }
00148 
00149         Point& rawPoint()
00150         {
00151             return hitPoint_;
00152         }
00153 
00154         void setHit()
00155         {
00156             hit_ = true;
00157         }
00158 
00159         void setMiss()
00160         {
00161             hit_ = false;
00162         }
00163 
00164         void setPoint(const Point& p)
00165         {
00166             hitPoint_ = p;
00167         }
00168 
00169         void setIndex(const label index)
00170         {
00171             index_ = index;
00172         }
00173 
00174         bool operator==(const PointIndexHit& rhs) const
00175         {
00176             return
00177                 hit_ == rhs.hit()
00178              && hitPoint_ == rhs.rawPoint()
00179              && index_ == rhs.index();
00180         }
00181 
00182         bool operator!=(const PointIndexHit& rhs) const
00183         {
00184             return !operator==(rhs);
00185         }
00186 
00187         void write(Ostream& os)
00188         {
00189             if (hit())
00190             {
00191                 os << "hit:" << hitPoint() << " index:" << index();
00192             }
00193             else
00194             {
00195                 os << "miss:" << missPoint() << " index:" << index();
00196             }
00197         }
00198 
00199         friend Ostream& operator<< (Ostream& os, const PointIndexHit& pHit)
00200         {
00201             if (os.format() == IOstream::ASCII)
00202             {
00203                 os  << pHit.hit_ << token::SPACE << pHit.hitPoint_
00204                     << token::SPACE << pHit.index_;
00205             }
00206             else
00207             {
00208                 os.write
00209                 (
00210                     reinterpret_cast<const char*>(&pHit),
00211                     sizeof(PointIndexHit)
00212                 );
00213             }
00214 
00215             // Check state of Ostream
00216             os.check("Ostream& operator<<(Ostream&, const PointIndexHit&)");
00217 
00218             return os;
00219         }
00220 
00221         friend Istream& operator>>(Istream& is, PointIndexHit& pHit)
00222         {
00223             if (is.format() == IOstream::ASCII)
00224             {
00225                 return is >> pHit.hit_ >> pHit.hitPoint_ >> pHit.index_;
00226             }
00227             else
00228             {
00229                 is.read
00230                 (
00231                     reinterpret_cast<char*>(&pHit),
00232                     sizeof(PointIndexHit)
00233                 );
00234             }
00235 
00236             // Check state of Istream
00237             is.check("Istream& operator>>(Istream&, PointIndexHit&)");
00238 
00239             return is;
00240         }
00241 
00242 };
00243 
00244 
00245 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00246 
00247 } // End namespace Foam
00248 
00249 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00250 
00251 #endif
00252 
00253 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines