Salome HOME
refs #493: provide Python API
[modules/hydro.git] / src / HYDROData / HYDROData_LinearInterpolator.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROData_LinearInterpolator.h"
24
25
26 HYDROData_LinearInterpolator::HYDROData_LinearInterpolator()
27 {
28 }
29
30 HYDROData_LinearInterpolator::~HYDROData_LinearInterpolator()
31 {
32 }
33
34 TCollection_AsciiString HYDROData_LinearInterpolator::GetDescription() const
35 {
36   TCollection_AsciiString aDescription( "Simple linear interpolator" );
37
38   return aDescription;
39 }
40
41 void HYDROData_LinearInterpolator::Calculate()
42 {
43   // Reset result data
44   ClearResults();
45
46   // Check input data
47   std::vector<double> aProfile1 = GetFirstProfileCoords();
48   std::vector<double> aProfile2 = GetSecondProfileCoords();
49   int aNbProfilesToCompute = GetNbProfilesToCompute();
50
51   int aSize1 = aProfile1.size();
52   int aSize2 = aProfile2.size();
53
54   div_t aDiv1 = std::div( aSize1, 3 );
55   div_t aDiv2 = std::div( aSize2, 3 );
56   
57   if ( aNbProfilesToCompute < 1 ||
58        aSize1 < 2 || aDiv1.rem != 0 ||
59        aSize2 < 2 || aDiv2.rem != 0 ) {
60     SetErrorCode( InvalidParametersError );
61     return;
62   }
63
64   bool isSame = false;
65   if ( aSize1 < aSize2 ) {
66     isSame = std::equal( aProfile1.begin(), aProfile1.end(), aProfile2.begin() );
67   } else {
68     isSame = std::equal( aProfile2.begin(), aProfile2.end(), aProfile1.begin() );
69   }
70
71   if ( isSame ) {
72     SetErrorCode( InvalidParametersError );
73     return;
74   }
75
76   // Linear interpolation
77   InterpolationError aStatus = OK;
78
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 );
85   }
86
87   for ( int k = 0; k <= aNbProfilesToCompute - 1; k++ ) {
88     std::vector<double> aResultProfile; ///< the k-th computed profile
89
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;
96
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 );
100
101       aResultProfile.push_back( anXi );
102       aResultProfile.push_back( anYi );
103       aResultProfile.push_back( aZi );
104     }
105    
106     InsertResultProfile( aResultProfile );
107   }
108
109   SetErrorCode( aStatus );
110 }