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