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

polyLine.C

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 Description
00026     polyLineEdge class : defines a curvedEdge in terms of a series of
00027     straight line segments
00028 
00029 \*---------------------------------------------------------------------------*/
00030 
00031 #include "error.H"
00032 
00033 #include "polyLine.H"
00034 
00035 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00036 
00037 namespace Foam
00038 {
00039 
00040 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00041 
00042 // calcDistances generates the distances_ lookup table (cumulative
00043 // distance along the line) from the individual vectors to the points
00044 
00045 void polyLine::calcDistances()
00046 {
00047     distances_[0] = 0.0;
00048 
00049     for (label i=1; i<distances_.size(); i++)
00050     {
00051         distances_[i] =
00052             mag(controlPoints_[i] - controlPoints_[i-1])
00053           + distances_[i-1];
00054     }
00055 
00056     lineLength_ = distances_[distances_.size()-1];
00057 
00058     for (label i=1; i<distances_.size(); i++)
00059     {
00060         distances_[i] /= lineLength_;
00061     }
00062 }
00063 
00064 
00065 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00066 
00067 polyLine::polyLine(const pointField& ps)
00068 :
00069     controlPoints_(ps),
00070     distances_(ps.size())
00071 {
00072     if (ps.size())
00073     {
00074         calcDistances();
00075     }
00076 }
00077 
00078 
00079 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00080 
00081 vector polyLine::position(const scalar lambda) const
00082 {
00083     // check range of lambda
00084 
00085     if (lambda < 0 || lambda > 1)
00086     {
00087         FatalErrorIn("polyLine::position(const scalar)")
00088             << "Parameter out of range, "
00089             << "lambda = " << lambda
00090             << abort(FatalError);
00091     }
00092 
00093     // Quick calc of endpoints
00094 
00095     if (lambda < SMALL)
00096     {
00097         return controlPoints_[0];
00098     }
00099     else if (lambda > 1 - SMALL)
00100     {
00101         return controlPoints_[controlPoints_.size()-1];
00102     }
00103 
00104 
00105     // search table of cumulative distance to find which linesegment we
00106     // are on
00107 
00108     label i(0);
00109     do
00110     {
00111         i++;
00112     } while (distances_[i] < lambda);
00113 
00114     i--;               // we overshot!
00115 
00116     // construct position vector
00117     scalar offsetDist =
00118         (lambda - distances_[i])
00119        /(distances_[i+1] - distances_[i]);
00120 
00121     vector offsetV = controlPoints_[i+1] - controlPoints_[i];
00122 
00123     return controlPoints_[i] + offsetDist*offsetV;
00124 }
00125 
00126 
00127 scalar polyLine::length() const
00128 {
00129     return lineLength_;
00130 }
00131 
00132 
00133 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00134 
00135 } // End namespace Foam
00136 
00137 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd