Salome HOME
First implementation of automatic mode of regions creation in the calculation case...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CurveCreatorProfile.cxx
1 // Copyright (C) 2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <HYDROGUI_CurveCreatorProfile.h>
21 #include <CurveCreator_Displayer.hxx>
22 #include <CurveCreator_Section.hxx>
23
24 HYDROGUI_CurveCreatorProfile::HYDROGUI_CurveCreatorProfile()
25 : CurveCreator_Curve( CurveCreator::Dim2d )
26 {
27   CurveCreator_Section *aSection = new CurveCreator_Section;
28   aSection->myName     = getUniqSectionName();
29   aSection->myType     = CurveCreator::Polyline;
30   aSection->myIsClosed = false;
31
32   mySections.push_back( aSection );
33 }
34
35 HYDROGUI_CurveCreatorProfile::~HYDROGUI_CurveCreatorProfile()
36 {
37 }
38
39 bool HYDROGUI_CurveCreatorProfile::moveSectionInternal( const int theISection,
40                                                         const int theNewIndex )
41 {
42   return false;
43 }
44
45 bool HYDROGUI_CurveCreatorProfile::moveSection( const int theISection,
46                                                 const int theNewIndex )
47 {
48   return false;
49 }
50
51 bool HYDROGUI_CurveCreatorProfile::clearInternal()
52 {
53   // erase curve from the viewer
54   if( myDisplayer )
55     myDisplayer->eraseAll( true );
56
57   // Delete all allocated data.
58   CurveCreator_Section* aSection = getSection( 0 );
59   if ( aSection )
60     aSection->myPoints.clear();
61
62   return true;
63 }
64
65 bool HYDROGUI_CurveCreatorProfile::joinInternal( const std::list<int>& theSections )
66 {
67   return false;
68 }
69
70 bool HYDROGUI_CurveCreatorProfile::join( const std::list<int>& theSections )
71 {
72   return false;
73 }
74
75 int HYDROGUI_CurveCreatorProfile::addSectionInternal( const std::string&               theName, 
76                                                       const CurveCreator::SectionType  theType,
77                                                       const bool                       theIsClosed, 
78                                                       const CurveCreator::Coordinates& thePoints )
79 {
80   return -1;
81 }
82
83 int HYDROGUI_CurveCreatorProfile::addSection( const std::string&              theName,
84                                               const CurveCreator::SectionType theType,
85                                               const bool                      theIsClosed )
86 {
87   return -1;
88 }
89
90 int HYDROGUI_CurveCreatorProfile::addSection( const std::string&               theName,
91                                               const CurveCreator::SectionType  theType,
92                                               const bool                       theIsClosed, 
93                                               const CurveCreator::Coordinates& thePoints )
94 {
95   return -1;
96 }
97
98 bool HYDROGUI_CurveCreatorProfile::removeSectionInternal( const int theISection )
99 {
100   return false;
101 }
102   
103 bool HYDROGUI_CurveCreatorProfile::removeSection( const int theISection )
104 {
105   return false;
106 }
107
108 bool HYDROGUI_CurveCreatorProfile::setClosedInternal( const int theISection, const bool theIsClosed )
109 {
110   return false;
111 }
112
113 bool HYDROGUI_CurveCreatorProfile::setClosed( const int theISection, const bool theIsClosed )
114 {
115   return false;
116 }
117
118 bool HYDROGUI_CurveCreatorProfile::addPointsInternal( const CurveCreator::SectionsMap &theSectionsMap )
119 {
120   bool res = false;
121
122   CurveCreator_Section* aSection = getSection( 0 );
123   if( !aSection )
124     return res;
125
126   CurveCreator::SectionsMap::const_iterator anIt = theSectionsMap.begin();
127   for ( ; anIt != theSectionsMap.end(); anIt++ )
128   {
129     int anISection = anIt->first;
130     if( anISection != 0 )
131       continue;
132
133     const CurveCreator::PosPointsList& aSectionPoints = anIt->second;
134
135     CurveCreator::PosPointsList::const_iterator aPntIt = aSectionPoints.begin();
136     for( ; aPntIt != aSectionPoints.end(); aPntIt++ )
137     {
138       const CurveCreator::Coordinates& aNewCoords = (*aPntIt)->myCoords;
139       CurveCreator::Coordinates::const_iterator aNewPntIt = aNewCoords.begin();
140       for( ; aNewPntIt != aNewCoords.end(); aNewPntIt++ )
141       {
142         const CurveCreator::TypeCoord& aCoordU = *aNewPntIt++;
143         if ( aNewPntIt == aNewCoords.end() )
144           break;
145         
146         const CurveCreator::TypeCoord& aCoordZ = *aNewPntIt;
147
148         CurveCreator::TypeCoord aC = aCoordU - 1;
149         if ( !aSection->myPoints.empty() )
150           aC = *(aSection->myPoints.end() - 2);
151
152         if ( aSection->myPoints.empty() || aCoordU >= aC )
153         {
154           aSection->myPoints.push_back( aCoordU );
155           aSection->myPoints.push_back( aCoordZ );
156         }
157         else if ( aCoordU < aSection->myPoints.front() )
158         {
159           aSection->myPoints.push_front( aCoordZ );
160           aSection->myPoints.push_front( aCoordU );
161         }
162         else
163         {
164           CurveCreator::Coordinates::iterator aRefPntIt = aSection->myPoints.begin();
165           for( ; aRefPntIt != aSection->myPoints.end(); aRefPntIt++ )
166           {
167             const CurveCreator::TypeCoord& aRefCoordU = *aRefPntIt++;
168             if ( aCoordU < aRefCoordU )
169               break;
170           }
171
172           aSection->myPoints.insert( aRefPntIt - 1, aNewPntIt - 1, aNewPntIt + 1 );
173         }
174       }
175     }
176
177     res = true;
178   }
179
180   if ( res )
181     redisplayCurve();
182
183   return res;
184 }
185
186 //! For internal use only! Undo/Redo are not used here.
187 bool HYDROGUI_CurveCreatorProfile::setPointInternal( const CurveCreator::SectionsMap &theSectionsMap )
188 {
189   bool aRes = false;
190
191   if ( mySkipSorting ) {
192     return CurveCreator_Curve::setPointInternal( theSectionsMap );
193   }
194
195   int anISection = 0;
196   CurveCreator_Section* aSection = getSection( anISection );
197   if( !aSection )
198     return aRes;
199
200   CurveCreator::SectionsMap::const_iterator anIt = theSectionsMap.begin();
201   if ( anIt == theSectionsMap.end() )
202     return aRes;
203
204   const CurveCreator::PosPointsList& aSectionPoints = anIt->second;
205
206   std::list<int> aConvPoints;
207   convert( aSectionPoints, aConvPoints );
208   removeSectionPoints( anISection, aConvPoints );
209
210   aRes = addPointsInternal( theSectionsMap );
211   if ( aRes )
212     redisplayCurve();
213
214   return aRes;
215 }
216
217 void HYDROGUI_CurveCreatorProfile::convert( const CurveCreator::PosPointsList& thePoints,
218                                             std::list<int>& theConvPoints )
219 {
220   theConvPoints.clear();
221
222   CurveCreator::PosPointsList::const_iterator aPntIt = thePoints.begin(),
223                                               aPntLast = thePoints.end();
224   for( ; aPntIt != aPntLast; aPntIt++ )
225   {
226     int anIPnt = (*aPntIt)->myID;
227     theConvPoints.push_back( anIPnt );
228   }
229 }
230
231 bool HYDROGUI_CurveCreatorProfile::canPointsBeSorted()
232 {
233   return true;
234 }
235
236 /**
237  *  Add one point to the specified section starting from the given theIPnt index
238  *  (or at the end of points if \a theIPnt is -1).
239  */
240 bool HYDROGUI_CurveCreatorProfile::addPoints( const CurveCreator::Coordinates& theCoords,
241                                               const int theISection,
242                                               const int theIPnt )
243 {
244   int anIPnt = theIPnt;
245
246   if ( anIPnt == - 1 && theCoords.size() > 1 ) {
247     CurveCreator::Coordinates aCoords;
248     for ( int i = 0, aNb = getNbPoints( theISection ); i < aNb; i++ ) {
249       aCoords = getPoint( theISection, i );
250       if ( aCoords.size() < 2 ) {
251         continue;
252       }
253
254       if ( theCoords[0] < aCoords[0] ) {
255         anIPnt = i;
256         break;
257       }
258     }
259   }
260   
261   return CurveCreator_Curve::addPoints( theCoords, theISection, anIPnt );
262 }