Salome HOME
#refs 522 (very draft) //import only
[modules/hydro.git] / src / HYDROData / HYDROData_IProfilesInterpolator.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_IProfilesInterpolator.h"
24 #include <cstdlib>
25 #include <gp_XYZ.hxx>
26
27 const int DEFAULT_RESULT_PROFILES_NB = 1; ///< default number of results profiles
28
29 HYDROData_IProfilesInterpolator::HYDROData_IProfilesInterpolator()
30   : myResultProfilesNumber( DEFAULT_RESULT_PROFILES_NB ),
31     myErrorCode( OK )
32 {
33 }
34
35 HYDROData_IProfilesInterpolator::~HYDROData_IProfilesInterpolator()
36 {
37 }
38
39 void HYDROData_IProfilesInterpolator::SetProfiles( const std::vector<double> theProfile1, 
40                                                    const std::vector<double> theProfile2 )
41 {
42   myProfile1 = theProfile1;
43   myProfile2 = theProfile2;
44 }
45
46 void HYDROData_IProfilesInterpolator::SetProfiles( const NCollection_Sequence<gp_XYZ>& theProfile1, 
47                                                    const NCollection_Sequence<gp_XYZ>& theProfile2 )
48 {
49   // The first profile
50   for ( int i = theProfile1.Lower(); i <= theProfile1.Upper(); i++ ) {
51     const gp_XYZ& aPnt = theProfile1.Value( i );
52     myProfile1.push_back( aPnt.X() );
53     myProfile1.push_back( aPnt.Y() );
54     myProfile1.push_back( aPnt.Z() );
55   }
56
57   // The second profile
58   for ( int i = theProfile2.Lower(); i <= theProfile2.Upper(); i++ ) {
59     const gp_XYZ& aPnt = theProfile2.Value( i );
60     myProfile2.push_back( aPnt.X() );
61     myProfile2.push_back( aPnt.Y() );
62     myProfile2.push_back( aPnt.Z() );
63   }
64 }
65
66 void HYDROData_IProfilesInterpolator::SetParameter( const TCollection_AsciiString& theName, 
67                                                     const TCollection_AsciiString& theValue )
68 {
69   myParameters[theName.ToCString()] = theValue.ToCString();
70 }
71
72 InterpolationError HYDROData_IProfilesInterpolator::GetErrorCode() const
73 {
74   return myErrorCode;
75 }
76
77 void HYDROData_IProfilesInterpolator::SetErrorCode( const InterpolationError& theError )
78 {
79   myErrorCode = theError;
80 }
81
82 TCollection_AsciiString HYDROData_IProfilesInterpolator::GetErrorMessage() const
83 {
84   return TCollection_AsciiString(myErrorMessage.c_str());
85 }
86
87 void HYDROData_IProfilesInterpolator::SetErrorMessage( const std::string& theMessage )
88 {
89   myErrorMessage = theMessage;
90 }
91
92 void HYDROData_IProfilesInterpolator::SetResultProfilesNumber( const int theNumber )
93 {
94   myResultProfilesNumber = theNumber;
95 }
96
97 int HYDROData_IProfilesInterpolator::GetCalculatedProfilesNumber() const
98 {
99   return myResultProfiles.size();
100 }
101
102 std::vector<double> HYDROData_IProfilesInterpolator::GetResultProfileCoords( const int theProfileIndex ) const
103 {
104   std::vector<double> aResultProfile;
105
106   if ( theProfileIndex >= 0 && theProfileIndex < (int) myResultProfiles.size() ) {
107     aResultProfile = myResultProfiles.at( theProfileIndex );
108   }
109
110   return aResultProfile;
111 }
112
113 NCollection_Sequence<gp_XYZ> HYDROData_IProfilesInterpolator::GetResultProfilePoints( const int theProfileIndex ) const
114 {
115   return GetPoints( GetResultProfileCoords( theProfileIndex ) );
116 }
117
118 void HYDROData_IProfilesInterpolator::Reset()
119 {
120   // Reset input parameters
121   myProfile1.clear();  
122   myProfile2.clear();  
123   myParameters.clear();
124
125   SetResultProfilesNumber( DEFAULT_RESULT_PROFILES_NB );
126   
127   // Reset result data
128   ClearResults();
129 }
130
131 std::vector<double> HYDROData_IProfilesInterpolator::GetFirstProfileCoords() const
132 {
133   return myProfile1;
134 }
135
136 std::vector<double> HYDROData_IProfilesInterpolator::GetSecondProfileCoords() const
137 {
138   return myProfile2;
139 }
140
141 void HYDROData_IProfilesInterpolator::ClearResults()
142 {
143   // Clear result data
144   myResultProfiles.clear();
145
146   // Reset errors
147   SetErrorCode( OK );
148   SetErrorMessage( "" );
149 }
150
151 int HYDROData_IProfilesInterpolator::GetNbProfilesToCompute() const
152 {
153   return myResultProfilesNumber;
154 }
155
156 void HYDROData_IProfilesInterpolator::InsertResultProfile( const std::vector<double>& theProfile )
157 {
158   myResultProfiles.push_back( theProfile );
159 }
160
161 void HYDROData_IProfilesInterpolator::InsertResultProfile( const NCollection_Sequence<gp_XYZ>& theProfile )
162 {
163   std::vector<double> aCoords;
164
165   for ( int i = theProfile.Lower(); i <= theProfile.Upper(); i++ ) {
166     const gp_XYZ& aPnt = theProfile.Value( i );
167     aCoords.push_back( aPnt.X() );
168     aCoords.push_back( aPnt.Y() );
169     aCoords.push_back( aPnt.Z() );
170   }
171
172   myResultProfiles.push_back( aCoords );
173 }
174
175 NCollection_Sequence<gp_XYZ> HYDROData_IProfilesInterpolator::GetFirstProfilePoints() const
176 {
177   return GetPoints( GetFirstProfileCoords() );
178 }
179
180 NCollection_Sequence<gp_XYZ> HYDROData_IProfilesInterpolator::GetSecondProfilePoints() const
181 {
182   return GetPoints( GetSecondProfileCoords() );
183 }
184
185 NCollection_Sequence<gp_XYZ> HYDROData_IProfilesInterpolator::GetPoints( const std::vector<double>& theCoords ) const
186 {
187   NCollection_Sequence<gp_XYZ> aProfilePoints;
188
189   int aNbCoords = (int) theCoords.size();
190   div_t aDiv = std::div( aNbCoords, 3 );
191   if ( aDiv.rem == 0 ) {
192     for ( int i = 0; i < aNbCoords; i += 3 ) {
193       aProfilePoints.Append( gp_XYZ( theCoords[i], theCoords[i+1], theCoords[i+2] ) );
194     }
195   }
196
197   return aProfilePoints;
198 }