]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_PolylineOperator.cxx
Salome HOME
4326e5254186a3d8d7c5a9961dd638e57ede02ee
[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_TopoCurve.h>
21 #include <HYDROData_Document.h>
22 #include <BRepAdaptor_Curve.hxx>
23 #include <BRep_Builder.hxx>
24 #include <BRep_Tool.hxx>
25 #include <BRepBuilderAPI_MakeEdge2d.hxx>
26 #include <BRepBuilderAPI_MakeEdge.hxx>
27 #include <BRepBuilderAPI_MakeWire.hxx>
28 #include <Extrema_ExtCC.hxx>
29 #include <Extrema_ExtPC.hxx>
30 #include <GeomAPI_Interpolate.hxx>
31 #include <NCollection_Vector.hxx>
32 #include <Precision.hxx>
33 #include <ShapeAnalysis_TransferParametersProj.hxx>
34 #include <ShapeBuild_Edge.hxx>
35 #include <TColgp_Array1OfVec.hxx>
36 #include <TColgp_HArray1OfPnt.hxx>
37 #include <TColStd_HArray1OfBoolean.hxx>
38 #include <TopoDS.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Wire.hxx>
41 #include <TopExp.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
44 #include <QString>
45
46 template<class T> void append( std::vector<T>& theList, const std::vector<T>& theList2 )
47 {
48   int aSize = theList.size();
49   int aNewSize = aSize + theList2.size();
50
51   if( aSize==aNewSize )
52     return;
53
54   theList.resize( aNewSize );
55   for( int i=aSize, j=0; i<aNewSize; i++, j++ )
56     theList[i] = theList2[j];
57 }
58
59 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
60                                         const Handle( HYDROData_PolylineXY )& thePolyline,
61                                         const gp_Pnt2d& thePoint,
62                                         double theTolerance ) const
63 {
64   std::vector<gp_Pnt2d> aPointsList( 1 );
65   aPointsList[0] = thePoint;
66   std::vector<TopoDS_Wire> aCurves;
67   GetWires(thePolyline, aCurves);
68   bool isOK = true;
69   for( int i=0, n=aCurves.size(); i<n; i++ )
70   {
71     std::vector<TopoDS_Shape> aCurvesList;
72     Split(aCurves[i], thePoint, theTolerance, aCurvesList);
73     bool isLocalOK = CreatePolylines( theDoc, thePolyline->GetName(), aCurvesList, true );
74     isOK = isOK && isLocalOK;
75   }
76   return isOK;
77 }
78
79 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
80               const Handle( HYDROData_PolylineXY )& thePolyline,
81               const Handle( HYDROData_PolylineXY )& theTool,
82               double theTolerance ) const
83 {
84   HYDROData_SequenceOfObjects aSeq;
85   aSeq.Append( theTool );
86   return split( theDoc, thePolyline, aSeq, theTolerance, -1 );
87 }
88
89 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
90                                         const HYDROData_SequenceOfObjects& thePolylines,
91                                         double theTolerance )
92 {
93   int f = thePolylines.Lower(), l = thePolylines.Upper();
94   for( int i=f; i<=l; i++ )
95   {
96     Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
97     if( !split( theDoc, aPolyline, thePolylines, theTolerance, i ) )
98       return false;
99   }
100   return true;
101 }
102
103 bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theDoc,
104                                         const QString& theName,
105                                         const HYDROData_SequenceOfObjects& thePolylines,
106                                         bool isConnectByNewSegment,
107                                         double theTolerance )
108 {
109   std::deque<HYDROData_TopoCurve> aMergedCurves;
110   HYDROData_SequenceOfObjects::Iterator aPIt(thePolylines);
111   for (; aPIt.More(); aPIt.Next())
112   {
113     Handle(HYDROData_PolylineXY) aPolyline =
114       Handle(HYDROData_PolylineXY)::DownCast(aPIt.Value());
115     std::vector<TopoDS_Wire> aWires;
116     GetWires(aPolyline, aWires);
117     for (std::vector<TopoDS_Wire>::const_iterator aWIt = aWires.begin(),
118       aLastWIt = aWires.end(); aWIt != aLastWIt; ++aWIt)
119     {
120       const Standard_Boolean aResult = !isConnectByNewSegment ?
121         HYDROData_TopoCurve::Merge(theTolerance, *aWIt, aMergedCurves) :
122         HYDROData_TopoCurve::Connect(theTolerance, *aWIt, aMergedCurves);
123       if (!aResult)
124       {
125         return false;
126       }
127     }
128   }
129
130   TopoDS_Compound aWireSet;
131   BRep_Builder aBuilder;
132   aBuilder.MakeCompound(aWireSet);
133   std::deque<HYDROData_TopoCurve>::iterator aCIt = aMergedCurves.begin();
134   std::deque<HYDROData_TopoCurve>::iterator aLastCIt = aMergedCurves.end();
135   for (; aCIt != aLastCIt; ++aCIt)
136   {
137     if (!aCIt->IsEmpty())
138     {
139       aBuilder.Add(aWireSet, aCIt->Wire());
140     }
141   }
142
143   std::vector<TopoDS_Shape> aPolylines(1);
144   aPolylines[0] = aWireSet;
145   CreatePolylines(theDoc, theName, aPolylines, false);
146   return true;
147 }
148
149 bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theDoc,
150                                         const Handle( HYDROData_PolylineXY )& thePolyline,
151                                         const HYDROData_SequenceOfObjects& theTools,
152                                         double theTolerance,
153                                         int theIgnoreIndex ) const
154 {
155   std::vector<TopoDS_Wire> aCurves;
156   GetWires(thePolyline, aCurves);
157   std::vector<TopoDS_Wire> aToolCurves;
158   for( int i=theTools.Lower(), n=theTools.Upper(); i<=n; i++ )
159     if( i!=theIgnoreIndex )
160     {
161       Handle( HYDROData_PolylineXY ) aToolPolyline = 
162         Handle( HYDROData_PolylineXY )::DownCast( theTools.Value( i ) );
163       std::vector<TopoDS_Wire> aTCurves;
164       GetWires(aToolPolyline, aTCurves);
165       append( aToolCurves, aTCurves);
166     }
167
168   const int aPSCount = aCurves.size();
169   const int aTSCount = aToolCurves.size();
170   std::vector<TopoDS_Shape> aResult;
171   for (int aPSI = 0; aPSI < aPSCount; ++aPSI)
172   {
173     HYDROData_TopoCurve aCurve;
174     if (!aCurve.Initialize(aCurves[aPSI]))
175     {
176       continue;
177     }
178
179     std::deque<std::list<double> > aParams;
180     for (int aTSI = 0; aTSI < aTSCount; ++aTSI)
181     {
182       aCurve.Intersect(aToolCurves[aTSI], aParams);
183     }
184
185     std::deque<HYDROData_TopoCurve> aSplittedCurves;
186     aCurve.Cut(aParams, aSplittedCurves);
187     std::deque<HYDROData_TopoCurve>::const_iterator aCIt =
188       aSplittedCurves.begin();
189     std::deque<HYDROData_TopoCurve>::const_iterator aLastCIt =
190       aSplittedCurves.end();
191     for (; aCIt != aLastCIt; ++aCIt)
192     {
193       aResult.push_back(aCIt->Wire());
194     }
195   }
196
197   CreatePolylines(theDoc, thePolyline->GetName(), aResult, true);
198   return true;
199 }
200
201 void HYDROData_PolylineOperator::GetWires(
202   const Handle( HYDROData_PolylineXY )& thePolyline,
203   std::vector<TopoDS_Wire>& theWires)
204 {
205   TopoDS_Shape aShape = thePolyline->GetShape();
206   if( aShape.ShapeType()==TopAbs_WIRE )
207   {
208     theWires.push_back( TopoDS::Wire( aShape ) );
209   }
210   else
211   {
212     TopExp_Explorer anExp( aShape, TopAbs_WIRE );
213     for( ; anExp.More(); anExp.Next() )
214     {
215       theWires.push_back( TopoDS::Wire( anExp.Current() ) );
216     }
217   }
218 }
219
220 void HYDROData_PolylineOperator::Split(
221   const TopoDS_Wire& theWire,
222   const gp_Pnt2d& thePoint,
223   double theTolerance,
224   std::vector<TopoDS_Shape>& theWires)
225 {
226   HYDROData_TopoCurve aCurve;
227   if (!aCurve.Initialize(theWire))
228   {
229     theWires.push_back(theWire);
230     return;
231   }
232
233   const gp_XYZ aP(thePoint.X(), thePoint.Y(), 0);
234   std::list<TopoDS_Edge>::const_iterator aEPos;
235   double aParam;
236   aCurve.Project(aP, aEPos, aParam);
237   HYDROData_TopoCurve aCurve1, aCurve2;
238   aCurve.Cut(aEPos, aParam, aCurve1, aCurve2);
239   theWires.push_back(aCurve1.Wire());
240   if (!aCurve2.IsEmpty())
241   {
242     theWires.push_back(aCurve2.Wire());
243   }
244 }
245
246 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
247                                                   const QString& theNamePrefix,
248                                                   const std::vector<TopoDS_Shape>& theShapes,
249                                                   bool isUseIndices )
250 {
251   if( theDoc.IsNull() )
252     return false;
253
254   int n = theShapes.size();
255   int anIndex = 1;
256   for( int i=0; i<n; i++ )
257   {
258     Handle( HYDROData_PolylineXY ) aPolyline = 
259       Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
260     if( aPolyline.IsNull() )
261       return false;
262
263     aPolyline->SetShape( theShapes[i] );
264
265     if( isUseIndices )
266     {
267       QString aNewName = theNamePrefix + "_" + QString::number( anIndex );
268       if( theDoc->FindObjectByName( aNewName ).IsNull() )  // the object with such a name is not found
269         aPolyline->SetName( aNewName );
270       anIndex++;
271     }
272     else
273     {
274       aPolyline->SetName( theNamePrefix );
275     }
276   }
277   return true;
278 }