]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_PolylineOperator.cxx
Salome HOME
refs #585: split/merge implementation
[modules/hydro.git] / src / HYDROData / HYDROData_PolylineOperator.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 <HYDROData_PolylineOperator.h>
20 #include <HYDROData_Document.h>
21 #include <BRepBuilderAPI_MakeEdge2d.hxx>
22 #include <BRepBuilderAPI_MakeWire.hxx>
23 #include <TopoDS.hxx>
24 #include <TopoDS_Edge.hxx>
25 #include <TopoDS_Wire.hxx>
26 #include <TopExp_Explorer.hxx>
27 #include <QString>
28
29 template<class T> void append( std::vector<T>& theList, const std::vector<T>& theList2 )
30 {
31   int aSize = theList.size();
32   int aNewSize = aSize + theList2.size();
33
34   if( aSize==aNewSize )
35     return;
36
37   theList.resize( aNewSize );
38   for( int i=aSize, j=0; i<aNewSize; i++, j++ )
39     theList[i] = theList2[j];
40 }
41
42 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
43                                         const Handle( HYDROData_PolylineXY )& thePolyline,
44                                         const gp_Pnt2d& thePoint,
45                                         double theTolerance ) const
46 {
47   std::vector<gp_Pnt2d> aPointsList( 1 );
48   aPointsList[0] = thePoint;
49   std::vector<TopoDS_Wire> aCurves = GetWires( thePolyline );
50   bool isOK = true;
51   for( int i=0, n=aCurves.size(); i<n; i++ )
52   {
53     std::vector<TopoDS_Shape> aCurvesList = Split( aCurves[i], thePoint, theTolerance );
54     bool isLocalOK = CreatePolylines( theDoc, thePolyline->GetName(), aCurvesList );
55     isOK = isOK && isLocalOK;
56   }
57   return isOK;
58 }
59
60 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
61               const Handle( HYDROData_PolylineXY )& thePolyline,
62               const Handle( HYDROData_PolylineXY )& theTool,
63               double theTolerance ) const
64 {
65   HYDROData_SequenceOfObjects aSeq;
66   aSeq.Append( theTool );
67   return split( theDoc, thePolyline, aSeq, theTolerance, -1 );
68 }
69
70 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
71                                         const HYDROData_SequenceOfObjects& thePolylines,
72                                         double theTolerance )
73 {
74   int f = thePolylines.Lower(), l = thePolylines.Upper();
75   for( int i=f; i<=l; i++ )
76   {
77     Handle( HYDROData_PolylineXY ) aPolylineI = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
78     if( !split( theDoc, aPolylineI, thePolylines, theTolerance, i ) )
79       return false;
80   }
81   return true;
82 }
83
84 bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theDoc,
85                                         const QString& theName,
86                                         const HYDROData_SequenceOfObjects& thePolylines,
87                                         bool isConnectByNewSegment,
88                                         double theTolerance )
89 {
90   TopoDS_Shape aMergedPolyline;
91
92   //TODO
93
94   std::vector<TopoDS_Shape> aShapes( 1 );
95   aShapes[0] = aMergedPolyline;
96   CreatePolylines( theDoc, theName, aShapes );
97
98   return true;
99 }
100
101 bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theDoc,
102                                         const Handle( HYDROData_PolylineXY )& thePolyline,
103                                         const HYDROData_SequenceOfObjects& theTools,
104                                         double theTolerance,
105                                         int theIgnoreIndex ) const
106 {
107   std::vector<TopoDS_Wire> aCurves = GetWires( thePolyline );
108   std::vector<TopoDS_Wire> aToolCurves;
109   for( int i=theTools.Lower(), n=theTools.Upper(); i<=n; i++ )
110     if( i!=theIgnoreIndex )
111     {
112       Handle( HYDROData_PolylineXY ) aToolPolyline = 
113         Handle( HYDROData_PolylineXY )::DownCast( theTools.Value( i ) );
114       append( aToolCurves, GetWires( aToolPolyline ) );
115     }
116
117   bool isOK = true;
118
119   int n = aCurves.size();
120   std::vector<TopoDS_Shape> aResults( n );
121   for( int i=0; i<n; i++ )
122     aResults[i] = aCurves[i];
123
124   for( int j=0, m=aToolCurves.size(); j<m; j++ )
125   {
126     std::vector<TopoDS_Shape> aNewResults;
127     for( int k=0, q=aResults.size(); k<q; k++ )
128     {
129       std::vector<TopoDS_Shape> aCurvesList = Split( TopoDS::Wire( aResults[k] ), aToolCurves[j], theTolerance );
130       append( aNewResults, aCurvesList );
131     }
132     aResults = aNewResults;
133   }
134
135   CreatePolylines( theDoc, thePolyline->GetName(), aResults );
136
137   return isOK;
138 }
139
140 std::vector<TopoDS_Wire> HYDROData_PolylineOperator::GetWires( const Handle( HYDROData_PolylineXY )& thePolyline )
141 {
142   std::vector<TopoDS_Wire> aResult;
143
144   TopoDS_Shape aShape = thePolyline->GetShape();
145
146   if( aShape.ShapeType()==TopAbs_WIRE )
147   {
148     aResult.push_back( TopoDS::Wire( aShape ) );
149   }
150   else
151   {
152     TopExp_Explorer anExp( aShape, TopAbs_WIRE );
153     for( ; anExp.More(); anExp.Next() )
154     {
155       aResult.push_back( TopoDS::Wire( anExp.Current() ) );
156     }
157   }
158   return aResult;
159 }
160
161 std::vector<TopoDS_Shape> HYDROData_PolylineOperator::Split( const TopoDS_Wire& theWire,
162                                                              const gp_Pnt2d& thePoint,
163                                                              double theTolerance )
164 {
165   std::vector<TopoDS_Shape> aResult;
166   //TODO
167   return aResult;
168 }
169
170 std::vector<TopoDS_Shape> HYDROData_PolylineOperator::Split( const TopoDS_Wire& theWire,
171                                                              const TopoDS_Wire& theTool,
172                                                              double theTolerance )
173 {
174   std::vector<TopoDS_Shape> aResult;
175   //TODO
176   return aResult;
177 }
178
179 std::vector<TopoDS_Shape> HYDROData_PolylineOperator::Split( const std::vector<TopoDS_Wire>& theWires,
180                                                              double theTolerance )
181 {
182   std::vector<TopoDS_Shape> aResult;
183   //TODO
184   return aResult;
185 }
186
187 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
188                                                   const QString& theNamePrefix,
189                                                   const std::vector<TopoDS_Shape>& theShapes )
190 {
191   if( theDoc.IsNull() )
192     return false;
193
194   int n = theShapes.size();
195   for( int i=0; i<n; i++ )
196   {
197     Handle( HYDROData_PolylineXY ) aPolyline = 
198       Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
199     if( aPolyline.IsNull() )
200       return false;
201
202     aPolyline->SetShape( theShapes[i] );
203     //TODO: set name
204   }
205   return true;
206 }