Salome HOME
quick optimization patch (bytearray for images)
[modules/hydro.git] / src / HYDROData / HYDROData_IProfilesInterpolator.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROData_IProfilesInterpolator.h"
20 #include <cstdlib>
21 #include <gp_XYZ.hxx>
22
23 const int DEFAULT_RESULT_PROFILES_NB = 1; ///< default number of results profiles
24
25 HYDROData_IProfilesInterpolator::HYDROData_IProfilesInterpolator()
26   : myResultProfilesNumber( DEFAULT_RESULT_PROFILES_NB ),
27     myErrorCode( OK )
28 {
29 }
30
31 HYDROData_IProfilesInterpolator::~HYDROData_IProfilesInterpolator()
32 {
33 }
34
35 void HYDROData_IProfilesInterpolator::SetProfiles( const std::vector<double> theProfile1, 
36                                                    const std::vector<double> theProfile2 )
37 {
38   myProfile1 = theProfile1;
39   myProfile2 = theProfile2;
40 }
41
42 void HYDROData_IProfilesInterpolator::SetProfiles( const NCollection_Sequence<gp_XYZ>& theProfile1, 
43                                                    const NCollection_Sequence<gp_XYZ>& theProfile2 )
44 {
45   // The first profile
46   for ( int i = theProfile1.Lower(); i <= theProfile1.Upper(); i++ ) {
47     const gp_XYZ& aPnt = theProfile1.Value( i );
48     myProfile1.push_back( aPnt.X() );
49     myProfile1.push_back( aPnt.Y() );
50     myProfile1.push_back( aPnt.Z() );
51   }
52
53   // The second profile
54   for ( int i = theProfile2.Lower(); i <= theProfile2.Upper(); i++ ) {
55     const gp_XYZ& aPnt = theProfile2.Value( i );
56     myProfile2.push_back( aPnt.X() );
57     myProfile2.push_back( aPnt.Y() );
58     myProfile2.push_back( aPnt.Z() );
59   }
60 }
61
62 void HYDROData_IProfilesInterpolator::SetParameter( const TCollection_AsciiString& theName, 
63                                                     const TCollection_AsciiString& theValue )
64 {
65   myParameters[theName.ToCString()] = theValue.ToCString();
66 }
67
68 InterpolationError HYDROData_IProfilesInterpolator::GetErrorCode() const
69 {
70   return myErrorCode;
71 }
72
73 void HYDROData_IProfilesInterpolator::SetErrorCode( const InterpolationError& theError )
74 {
75   myErrorCode = theError;
76 }
77
78 TCollection_AsciiString HYDROData_IProfilesInterpolator::GetErrorMessage() const
79 {
80   return TCollection_AsciiString(myErrorMessage.c_str());
81 }
82
83 void HYDROData_IProfilesInterpolator::SetErrorMessage( const std::string& theMessage )
84 {
85   myErrorMessage = theMessage;
86 }
87
88 void HYDROData_IProfilesInterpolator::SetResultProfilesNumber( const int theNumber )
89 {
90   myResultProfilesNumber = theNumber;
91 }
92
93 int HYDROData_IProfilesInterpolator::GetCalculatedProfilesNumber() const
94 {
95   return myResultProfiles.size();
96 }
97
98 std::vector<double> HYDROData_IProfilesInterpolator::GetResultProfileCoords( const int theProfileIndex ) const
99 {
100   std::vector<double> aResultProfile;
101
102   if ( theProfileIndex >= 0 && theProfileIndex < (int) myResultProfiles.size() ) {
103     aResultProfile = myResultProfiles.at( theProfileIndex );
104   }
105
106   return aResultProfile;
107 }
108
109 NCollection_Sequence<gp_XYZ> HYDROData_IProfilesInterpolator::GetResultProfilePoints( const int theProfileIndex ) const
110 {
111   return GetPoints( GetResultProfileCoords( theProfileIndex ) );
112 }
113
114 void HYDROData_IProfilesInterpolator::Reset()
115 {
116   // Reset input parameters
117   myProfile1.clear();  
118   myProfile2.clear();  
119   myParameters.clear();
120
121   SetResultProfilesNumber( DEFAULT_RESULT_PROFILES_NB );
122   
123   // Reset result data
124   ClearResults();
125 }
126
127 std::vector<double> HYDROData_IProfilesInterpolator::GetFirstProfileCoords() const
128 {
129   return myProfile1;
130 }
131
132 std::vector<double> HYDROData_IProfilesInterpolator::GetSecondProfileCoords() const
133 {
134   return myProfile2;
135 }
136
137 void HYDROData_IProfilesInterpolator::ClearResults()
138 {
139   // Clear result data
140   myResultProfiles.clear();
141
142   // Reset errors
143   SetErrorCode( OK );
144   SetErrorMessage( "" );
145 }
146
147 int HYDROData_IProfilesInterpolator::GetNbProfilesToCompute() const
148 {
149   return myResultProfilesNumber;
150 }
151
152 void HYDROData_IProfilesInterpolator::InsertResultProfile( const std::vector<double>& theProfile )
153 {
154   myResultProfiles.push_back( theProfile );
155 }
156
157 void HYDROData_IProfilesInterpolator::InsertResultProfile( const NCollection_Sequence<gp_XYZ>& theProfile )
158 {
159   std::vector<double> aCoords;
160
161   for ( int i = theProfile.Lower(); i <= theProfile.Upper(); i++ ) {
162     const gp_XYZ& aPnt = theProfile.Value( i );
163     aCoords.push_back( aPnt.X() );
164     aCoords.push_back( aPnt.Y() );
165     aCoords.push_back( aPnt.Z() );
166   }
167
168   myResultProfiles.push_back( aCoords );
169 }
170
171 NCollection_Sequence<gp_XYZ> HYDROData_IProfilesInterpolator::GetFirstProfilePoints() const
172 {
173   return GetPoints( GetFirstProfileCoords() );
174 }
175
176 NCollection_Sequence<gp_XYZ> HYDROData_IProfilesInterpolator::GetSecondProfilePoints() const
177 {
178   return GetPoints( GetSecondProfileCoords() );
179 }
180
181 NCollection_Sequence<gp_XYZ> HYDROData_IProfilesInterpolator::GetPoints( const std::vector<double>& theCoords ) const
182 {
183   NCollection_Sequence<gp_XYZ> aProfilePoints;
184
185   int aNbCoords = (int) theCoords.size();
186   div_t aDiv = std::div( aNbCoords, 3 );
187   if ( aDiv.rem == 0 ) {
188     for ( int i = 0; i < aNbCoords; i += 3 ) {
189       aProfilePoints.Append( gp_XYZ( theCoords[i], theCoords[i+1], theCoords[i+2] ) );
190     }
191   }
192
193   return aProfilePoints;
194 }