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