Salome HOME
OCC functionality moving out from the widget
[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 <AIS_Point.hxx>
28 #include <AIS_Line.hxx>
29 #include <AIS_Shape.hxx>
30 #include <BRepBuilderAPI_MakeEdge.hxx>
31 #include <BRepBuilderAPI_MakeWire.hxx>
32 #include <Geom_CartesianPoint.hxx>
33 #include <gp_Pnt.hxx>
34 #include <gp_Lin.hxx>
35 #include <TopoDS_Edge.hxx>
36 #include <TopoDS_Face.hxx>
37 #include <TopoDS_Wire.hxx>
38 #include <TColgp_HArray1OfPnt.hxx>
39 #include <GeomAPI_Interpolate.hxx>
40
41 #include <stdio.h>
42
43 CurveCreator_Profile::CurveCreator_Profile()
44 : CurveCreator_Curve( CurveCreator::Dim2d )
45 {
46   CurveCreator_Section *aSection = new CurveCreator_Section;
47   aSection->myName     = getUniqSectionName();
48   aSection->myType     = CurveCreator::Polyline;
49   aSection->myIsClosed = false;
50
51   mySections.push_back( aSection );
52 }
53
54 CurveCreator_Profile::~CurveCreator_Profile()
55 {
56 }
57
58 bool CurveCreator_Profile::moveSectionInternal( const int theISection,
59                                                 const int theNewIndex )
60 {
61   return false;
62 }
63
64 bool CurveCreator_Profile::moveSection( const int theISection,
65                                         const int theNewIndex )
66 {
67   return false;
68 }
69
70 bool CurveCreator_Profile::clearInternal()
71 {
72   // erase curve from the viewer
73   if( myDisplayer )
74     myDisplayer->erase( true );
75
76   // Delete all allocated data.
77   mySections[ 0 ]->myPoints.clear();
78
79   return true;
80 }
81
82 bool CurveCreator_Profile::joinInternal( const int theISectionTo, 
83                                          const int theISectionFrom )
84 {
85   return false;
86 }
87
88 bool CurveCreator_Profile::join( const int theISectionTo, 
89                                  const int theISectionFrom )
90 {
91   return false;
92 }
93
94 int CurveCreator_Profile::addSectionInternal( const std::string&               theName, 
95                                               const CurveCreator::SectionType  theType,
96                                               const bool                       theIsClosed, 
97                                               const CurveCreator::Coordinates& thePoints )
98 {
99   return -1;
100 }
101
102 int CurveCreator_Profile::addSection( const std::string&              theName,
103                                       const CurveCreator::SectionType theType,
104                                       const bool                      theIsClosed )
105 {
106   return -1;
107 }
108
109 int CurveCreator_Profile::addSection( const std::string&               theName,
110                                       const CurveCreator::SectionType  theType,
111                                       const bool                       theIsClosed, 
112                                       const CurveCreator::Coordinates& thePoints )
113 {
114   return -1;
115 }
116
117 bool CurveCreator_Profile::removeSectionInternal( const int theISection )
118 {
119   return false;
120 }
121   
122 bool CurveCreator_Profile::removeSection( const int theISection )
123 {
124   return false;
125 }
126
127 bool CurveCreator_Profile::setClosedInternal( const int theISection, 
128                                               const bool theIsClosed )
129 {
130   return false;
131 }
132
133 bool CurveCreator_Profile::setClosed( const int theISection, 
134                                       const bool theIsClosed )
135 {
136   return false;
137 }
138
139 bool CurveCreator_Profile::setSectionTypeInternal( const int theISection, 
140                                                    const CurveCreator::SectionType theType )
141 {
142   return false;
143 }
144
145 bool CurveCreator_Profile::setSectionType( const int theISection, 
146                                            const CurveCreator::SectionType theType )
147 {
148   return false;
149 }
150
151 bool CurveCreator_Profile::addPointsInternal( const CurveCreator::SectionsMap &theSectionsMap )
152 {
153   bool res = false;
154
155   CurveCreator_Section* aSection = mySections.at( 0 );
156   if( !aSection )
157     return res;
158
159   CurveCreator::SectionsMap::const_iterator anIt = theSectionsMap.begin();
160   for ( ; anIt != theSectionsMap.end(); anIt++ )
161   {
162     int anISection = anIt->first;
163     if( anISection != 0 )
164       continue;
165
166     const CurveCreator::PosPointsList& aSectionPoints = anIt->second;
167
168     CurveCreator::PosPointsList::const_iterator aPntIt = aSectionPoints.begin();
169     for( ; aPntIt != aSectionPoints.end(); aPntIt++ )
170     {
171       const CurveCreator::Coordinates& aNewCoords = (*aPntIt)->myCoords;
172       CurveCreator::Coordinates::const_iterator aNewPntIt = aNewCoords.begin();
173       for( ; aNewPntIt != aNewCoords.end(); aNewPntIt++ )
174       {
175         const CurveCreator::TypeCoord& aCoordU = *aNewPntIt++;
176         if ( aNewPntIt == aNewCoords.end() )
177           break;
178         
179         const CurveCreator::TypeCoord& aCoordZ = *aNewPntIt;
180
181         CurveCreator::TypeCoord aC = aCoordU - 1;
182         if ( !aSection->myPoints.empty() )
183           aC = *(aSection->myPoints.end() - 2);
184
185         if ( aSection->myPoints.empty() || aCoordU > aC )
186         {
187           aSection->myPoints.push_back( aCoordU );
188           aSection->myPoints.push_back( aCoordZ );
189         }
190         else if ( aCoordU < aSection->myPoints.front() )
191         {
192           aSection->myPoints.push_front( aCoordZ );
193           aSection->myPoints.push_front( aCoordU );
194         }
195         else
196         {
197           CurveCreator::Coordinates::iterator aRefPntIt = aSection->myPoints.begin();
198           for( ; aRefPntIt != aSection->myPoints.end(); aRefPntIt++ )
199           {
200             const CurveCreator::TypeCoord& aRefCoordU = *aRefPntIt++;
201             if ( aCoordU < aRefCoordU )
202               break;
203           }
204
205           aSection->myPoints.insert( aRefPntIt - 1, aNewPntIt - 1, aNewPntIt + 1 );
206         }
207       }
208     }
209
210     res = true;
211   }
212
213   if ( res )
214     redisplayCurve();
215
216   return res;
217 }
218
219 //! For internal use only! Undo/Redo are not used here.
220 bool CurveCreator_Profile::setPointInternal( const CurveCreator::SectionsMap &theSectionsMap )
221 {
222   bool res = false;
223
224   CurveCreator_Section* aSection = mySections.at( 0 );
225   if( !aSection )
226     return res;
227
228   CurveCreator::SectionsMap::const_iterator anIt = theSectionsMap.begin();
229   for ( ; anIt != theSectionsMap.end(); anIt++ )
230   {
231     int anISection = anIt->first;
232     if( anISection != 0 )
233       continue;
234
235     const CurveCreator::PosPointsList& aSectionPoints = anIt->second;
236
237     CurveCreator::PosPointsList::const_iterator aPntIt = aSectionPoints.begin();
238     for( ; aPntIt != aSectionPoints.end(); aPntIt++ )
239     {
240       int anIPnt = (*aPntIt)->myID;
241       aSection->myPoints.erase( aSection->myPoints.begin() + toICoord( anIPnt ), 
242                                 aSection->myPoints.begin() + toICoord( anIPnt ) + 2 );
243     }
244   }
245
246   res = addPointsInternal( theSectionsMap );
247
248   if ( res )
249     redisplayCurve();
250
251   return res;
252 }