Salome HOME
Merge branch 'master' of ssh://git.salome-platform.org/modules/hydro
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CurveCreatorProfile.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 <HYDROGUI_CurveCreatorProfile.h>
24 #include <CurveCreator_Displayer.hxx>
25 #include <CurveCreator_Section.hxx>
26
27 HYDROGUI_CurveCreatorProfile::HYDROGUI_CurveCreatorProfile()
28 : CurveCreator_Curve( CurveCreator::Dim2d )
29 {
30   CurveCreator_Section *aSection = new CurveCreator_Section;
31   aSection->myName     = getUniqSectionName();
32   aSection->myType     = CurveCreator::Polyline;
33   aSection->myIsClosed = false;
34
35   mySections.push_back( aSection );
36 }
37
38 HYDROGUI_CurveCreatorProfile::~HYDROGUI_CurveCreatorProfile()
39 {
40 }
41
42 bool HYDROGUI_CurveCreatorProfile::moveSectionInternal( const int theISection,
43                                                         const int theNewIndex )
44 {
45   return false;
46 }
47
48 bool HYDROGUI_CurveCreatorProfile::moveSection( const int theISection,
49                                                 const int theNewIndex )
50 {
51   return false;
52 }
53
54 bool HYDROGUI_CurveCreatorProfile::clearInternal()
55 {
56   // erase curve from the viewer
57   if( myDisplayer )
58     myDisplayer->eraseAll( true );
59
60   // Delete all allocated data.
61   CurveCreator_Section* aSection = getSection( 0 );
62   if ( aSection )
63     aSection->myPoints.clear();
64
65   return true;
66 }
67
68 bool HYDROGUI_CurveCreatorProfile::joinInternal( const std::list<int>& theSections )
69 {
70   return false;
71 }
72
73 bool HYDROGUI_CurveCreatorProfile::join( const std::list<int>& theSections )
74 {
75   return false;
76 }
77
78 int HYDROGUI_CurveCreatorProfile::addSectionInternal( const std::string&               theName, 
79                                                       const CurveCreator::SectionType  theType,
80                                                       const bool                       theIsClosed, 
81                                                       const CurveCreator::Coordinates& thePoints )
82 {
83   return -1;
84 }
85
86 int HYDROGUI_CurveCreatorProfile::addSection( const std::string&              theName,
87                                               const CurveCreator::SectionType theType,
88                                               const bool                      theIsClosed )
89 {
90   return -1;
91 }
92
93 int HYDROGUI_CurveCreatorProfile::addSection( const std::string&               theName,
94                                               const CurveCreator::SectionType  theType,
95                                               const bool                       theIsClosed, 
96                                               const CurveCreator::Coordinates& thePoints )
97 {
98   return -1;
99 }
100
101 bool HYDROGUI_CurveCreatorProfile::removeSectionInternal( const int theISection )
102 {
103   return false;
104 }
105   
106 bool HYDROGUI_CurveCreatorProfile::removeSection( const int theISection )
107 {
108   return false;
109 }
110
111 bool HYDROGUI_CurveCreatorProfile::setClosedInternal( const int theISection, const bool theIsClosed )
112 {
113   return false;
114 }
115
116 bool HYDROGUI_CurveCreatorProfile::setClosed( const int theISection, const bool theIsClosed )
117 {
118   return false;
119 }
120
121 bool HYDROGUI_CurveCreatorProfile::addPointsInternal( const CurveCreator::SectionsMap &theSectionsMap )
122 {
123   bool res = false;
124
125   CurveCreator_Section* aSection = getSection( 0 );
126   if( !aSection )
127     return res;
128
129   CurveCreator::SectionsMap::const_iterator anIt = theSectionsMap.begin();
130   for ( ; anIt != theSectionsMap.end(); anIt++ )
131   {
132     int anISection = anIt->first;
133     if( anISection != 0 )
134       continue;
135
136     const CurveCreator::PosPointsList& aSectionPoints = anIt->second;
137
138     CurveCreator::PosPointsList::const_iterator aPntIt = aSectionPoints.begin();
139     for( ; aPntIt != aSectionPoints.end(); aPntIt++ )
140     {
141       const CurveCreator::Coordinates& aNewCoords = (*aPntIt)->myCoords;
142       CurveCreator::Coordinates::const_iterator aNewPntIt = aNewCoords.begin();
143       for( ; aNewPntIt != aNewCoords.end(); aNewPntIt++ )
144       {
145         const CurveCreator::TypeCoord& aCoordU = *aNewPntIt++;
146         if ( aNewPntIt == aNewCoords.end() )
147           break;
148         
149         const CurveCreator::TypeCoord& aCoordZ = *aNewPntIt;
150
151         CurveCreator::TypeCoord aC = aCoordU - 1;
152         if ( !aSection->myPoints.empty() )
153           aC = *(aSection->myPoints.end() - 2);
154
155         if ( aSection->myPoints.empty() || aCoordU >= aC )
156         {
157           aSection->myPoints.push_back( aCoordU );
158           aSection->myPoints.push_back( aCoordZ );
159         }
160         else if ( aCoordU < aSection->myPoints.front() )
161         {
162           aSection->myPoints.push_front( aCoordZ );
163           aSection->myPoints.push_front( aCoordU );
164         }
165         else
166         {
167           CurveCreator::Coordinates::iterator aRefPntIt = aSection->myPoints.begin();
168           for( ; aRefPntIt != aSection->myPoints.end(); aRefPntIt++ )
169           {
170             const CurveCreator::TypeCoord& aRefCoordU = *aRefPntIt++;
171             if ( aCoordU < aRefCoordU )
172               break;
173           }
174
175           aSection->myPoints.insert( aRefPntIt - 1, aNewPntIt - 1, aNewPntIt + 1 );
176         }
177       }
178     }
179
180     res = true;
181   }
182
183   if ( res )
184     redisplayCurve();
185
186   return res;
187 }
188
189 //! For internal use only! Undo/Redo are not used here.
190 bool HYDROGUI_CurveCreatorProfile::setPointInternal( const CurveCreator::SectionsMap &theSectionsMap )
191 {
192   bool aRes = false;
193
194   if ( mySkipSorting ) {
195     return CurveCreator_Curve::setPointInternal( theSectionsMap );
196   }
197
198   int anISection = 0;
199   CurveCreator_Section* aSection = getSection( anISection );
200   if( !aSection )
201     return aRes;
202
203   CurveCreator::SectionsMap::const_iterator anIt = theSectionsMap.begin();
204   if ( anIt == theSectionsMap.end() )
205     return aRes;
206
207   const CurveCreator::PosPointsList& aSectionPoints = anIt->second;
208
209   std::list<int> aConvPoints;
210   convert( aSectionPoints, aConvPoints );
211   removeSectionPoints( anISection, aConvPoints );
212
213   aRes = addPointsInternal( theSectionsMap );
214   if ( aRes )
215     redisplayCurve();
216
217   return aRes;
218 }
219
220 void HYDROGUI_CurveCreatorProfile::convert( const CurveCreator::PosPointsList& thePoints,
221                                             std::list<int>& theConvPoints )
222 {
223   theConvPoints.clear();
224
225   CurveCreator::PosPointsList::const_iterator aPntIt = thePoints.begin(),
226                                               aPntLast = thePoints.end();
227   for( ; aPntIt != aPntLast; aPntIt++ )
228   {
229     int anIPnt = (*aPntIt)->myID;
230     theConvPoints.push_back( anIPnt );
231   }
232 }
233
234 bool HYDROGUI_CurveCreatorProfile::canPointsBeSorted()
235 {
236   return true;
237 }
238
239 /**
240  *  Add one point to the specified section starting from the given theIPnt index
241  *  (or at the end of points if \a theIPnt is -1).
242  */
243 bool HYDROGUI_CurveCreatorProfile::addPoints( const CurveCreator::Coordinates& theCoords,
244                                               const int theISection,
245                                               const int theIPnt )
246 {
247   int anIPnt = theIPnt;
248
249   if ( anIPnt == - 1 && theCoords.size() > 1 ) {
250     CurveCreator::Coordinates aCoords;
251     for ( int i = 0, aNb = getNbPoints( theISection ); i < aNb; i++ ) {
252       aCoords = getPoint( theISection, i );
253       if ( aCoords.size() < 2 ) {
254         continue;
255       }
256
257       if ( theCoords[0] < aCoords[0] ) {
258         anIPnt = i;
259         break;
260       }
261     }
262   }
263   
264   return CurveCreator_Curve::addPoints( theCoords, theISection, anIPnt );
265 }