1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROData_LinearInterpolator.h"
26 HYDROData_LinearInterpolator::HYDROData_LinearInterpolator()
30 HYDROData_LinearInterpolator::~HYDROData_LinearInterpolator()
34 std::string HYDROData_LinearInterpolator::GetDescription() const
36 std::string aDescription( "Simple linear interpolator" );
41 void HYDROData_LinearInterpolator::Calculate()
47 std::vector<double> aProfile1 = GetFirstProfile();
48 std::vector<double> aProfile2 = GetSecondProfile();
49 int aNbProfilesToCompute = GetNbProfilesToCompute();
51 int aSize1 = aProfile1.size();
52 int aSize2 = aProfile2.size();
54 div_t aDiv1 = std::div( aSize1, 3 );
55 div_t aDiv2 = std::div( aSize2, 3 );
57 if ( aNbProfilesToCompute < 1 ||
58 aSize1 < 2 || aDiv1.rem != 0 ||
59 aSize2 < 2 || aDiv2.rem != 0 ) {
60 SetErrorCode( InvalidParametersError );
65 if ( aSize1 < aSize2 ) {
66 isSame = std::equal( aProfile1.begin(), aProfile1.end(), aProfile2.begin() );
68 isSame = std::equal( aProfile2.begin(), aProfile2.end(), aProfile1.begin() );
72 SetErrorCode( InvalidParametersError );
76 // Linear interpolation
77 InterpolationError aStatus = OK;
79 // the first profile should have the equal or less number of points than the second profile
80 int aNbPoints1 = aDiv1.quot;
81 int aNbPoints2 = aDiv2.quot;
82 if ( aNbPoints1 > aNbPoints2 ) {
83 aProfile1.swap( aProfile2 );
84 std::swap( aNbPoints1, aNbPoints2 );
87 for ( int k = 0; k <= aNbProfilesToCompute - 1; k++ ) {
88 std::vector<double> aResultProfile; ///< the k-th computed profile
90 double aRelParam = (double)( k + 1 ) / ( aNbProfilesToCompute + 1 );
91 for ( int i = 0; i <= aNbPoints1 - 1; i++ ) {
92 double aRel = ( aNbPoints2 - 1 ) / ( aNbPoints1 - 1 );
93 int aPointIndex = (int) std::floor( aRel * i ); ///< point index in the second profile
94 int anXindex1 = i * 3;
95 int anXindex2 = aPointIndex * 3;
97 double anXi = ( aProfile1[anXindex1] * ( 1 - aRelParam ) ) + ( aProfile2[anXindex2] * aRelParam );
98 double anYi = ( aProfile1[anXindex1 + 1] * ( 1 - aRelParam ) ) + ( aProfile2[anXindex2 + 1] * aRelParam );
99 double aZi = ( aProfile1[anXindex1 + 2] * ( 1 - aRelParam ) ) + ( aProfile2[anXindex2 + 2] * aRelParam );
101 aResultProfile.push_back( anXi );
102 aResultProfile.push_back( anYi );
103 aResultProfile.push_back( aZi );
106 InsertResultProfile( aResultProfile );
109 SetErrorCode( aStatus );