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

BSpline.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     BSpline : cubic spline going through all the knots
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "error.H"
00031 
00032 #include "BSpline.H"
00033 #include "simpleMatrix.H"
00034 
00035 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00036 
00037 namespace Foam
00038 {
00039 
00040 pointField BSpline::findKnots
00041 (
00042     const pointField& allknots,
00043     const vector& fstend,
00044     const vector& sndend
00045 )
00046 {
00047     label newnKnots(allknots.size() + 2);
00048     label NKnots(allknots.size());
00049     pointField newknots(newnKnots);
00050 
00051     // set up 1/6 and 2/3 which are the matrix elements throughout most
00052     // of the matrix
00053 
00054     register scalar oneSixth = 1.0/6.0;
00055     register scalar twoThird = 2.0/3.0;
00056 
00057     simpleMatrix<vector> M(newnKnots);
00058 
00059     // set up the matrix
00060 
00061     M[0][0] = -0.5*scalar(NKnots - 1);
00062     M[0][2] =  0.5*scalar(NKnots - 1);
00063 
00064     for (register label i=1; i<newnKnots-1; i++)
00065     {
00066         M[i][i-1] = oneSixth;
00067         M[i][i] = twoThird;
00068         M[i][i+1] = oneSixth;
00069     }
00070 
00071     M[newnKnots - 1][newnKnots - 3] = -0.5*scalar(NKnots - 1);
00072     M[newnKnots - 1][newnKnots - 1] =  0.5*scalar(NKnots - 1);
00073 
00074     // set up the vector
00075 
00076     for (label i=1; i<=NKnots; i++)
00077     {
00078         M.source()[i] = allknots[i-1];
00079     }
00080 
00081     // set the gradients at the two ends
00082 
00083     if (mag(fstend)<1e-8)
00084     {
00085         // set to the default : forward differences on the end knots
00086         M.source()[0] = allknots[1] - allknots[0];
00087         M.source()[0] /= mag(M.source()[0]);
00088 
00089         M.source()[NKnots+1] = M.source()[NKnots-1] - M.source()[NKnots];
00090         M.source()[NKnots+1] /= mag(M.source()[NKnots+1]);
00091     }
00092     else
00093     {
00094         // set to the gradient vectors provided
00095         M.source()[0] = fstend/mag(fstend);
00096         M.source()[NKnots+1] = sndend/mag(sndend);
00097     }
00098 
00099     // invert the equation to find the control knots
00100 
00101     newknots = M.solve();
00102 
00103     return newknots;
00104 }
00105 
00106 
00107 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00108 
00109 // Construct from components
00110 BSpline::BSpline(const pointField& Knots)
00111 :
00112     spline(findKnots(Knots))
00113 {}
00114 
00115 
00116 // Construct from components
00117 BSpline::BSpline
00118 (
00119     const pointField& Knots,
00120     const vector& fstend,
00121     const vector& sndend
00122 )
00123 :
00124     spline(findKnots(Knots, fstend, sndend))
00125 {}
00126 
00127 
00128 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00129 
00130 //- Return the real position of a point on the curve given by
00131 //  the parameter 0 <= lambda <= 1
00132 vector BSpline::realPosition(scalar mu)
00133 {
00134     return spline::position(mu);
00135 }
00136 
00137 
00138 //- Return the position of a point on the curve given by
00139 //  the parameter 0 <= lambda <= 1
00140 vector BSpline::position(const scalar mu) const
00141 {
00142     return spline::position((1.0/(nKnots() - 1))*(1.0 + mu*(nKnots() - 3)));
00143 }
00144 
00145 
00146 //- Return the length of the curve
00147 scalar BSpline::length() const
00148 {
00149     notImplemented("BSpline::length() const");
00150     return 1.0;
00151 }
00152 
00153 
00154 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00155 
00156 } // End namespace Foam
00157 
00158 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd