]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_PolylineOperator.cxx
Salome HOME
merge BR_v14_rc from origin
[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 <HYDROData_TopoCurve.h>
22
23 #include <CurveCreator_Utils.hxx>
24
25 #include <BRepAdaptor_Curve.hxx>
26 #include <BRep_Builder.hxx>
27 #include <BRep_Tool.hxx>
28 #include <BRepBuilderAPI_MakeEdge2d.hxx>
29 #include <BRepBuilderAPI_MakeEdge.hxx>
30 #include <BRepBuilderAPI_MakeWire.hxx>
31 #include <Extrema_ExtCC.hxx>
32 #include <Extrema_ExtPC.hxx>
33 #include <GeomAPI_Interpolate.hxx>
34 #include <NCollection_Vector.hxx>
35 #include <Precision.hxx>
36 #include <ShapeAnalysis_TransferParametersProj.hxx>
37 #include <ShapeBuild_Edge.hxx>
38 #include <TColgp_Array1OfVec.hxx>
39 #include <TColgp_HArray1OfPnt.hxx>
40 #include <TColStd_HArray1OfBoolean.hxx>
41 #include <TopoDS.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <TopoDS_Wire.hxx>
44 #include <TopExp.hxx>
45 #include <TopExp_Explorer.hxx>
46 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
47 #include <QString>
48
49 template<class T> void append( std::vector<T>& theList, const std::vector<T>& theList2 )
50 {
51   int aSize = theList.size();
52   int aNewSize = aSize + theList2.size();
53
54   if( aSize==aNewSize )
55     return;
56
57   theList.resize( aNewSize );
58   for( int i=aSize, j=0; i<aNewSize; i++, j++ )
59     theList[i] = theList2[j];
60 }
61
62 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
63                                         const Handle( HYDROData_PolylineXY )& thePolyline,
64                                         const gp_Pnt2d& thePoint,
65                                         double theTolerance ) const
66 {
67   if (thePolyline.IsNull())
68   {
69     return false;
70   }
71
72   std::vector<gp_Pnt2d> aPointsList( 1 );
73   aPointsList[0] = thePoint;
74   std::vector<TopoDS_Wire> aCurves;
75   GetWires(thePolyline, aCurves);
76   bool isOK = true;
77   for( int i=0, n=aCurves.size(); i<n; i++ )
78   {
79     std::vector<TopoDS_Shape> aCurvesList;
80     Split(aCurves[i], thePoint, theTolerance, aCurvesList);
81     bool isLocalOK = CreatePolylines( theDoc, thePolyline->GetName(), aCurvesList, true );
82     isOK = isOK && isLocalOK;
83   }
84   return isOK;
85 }
86
87 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
88               const Handle( HYDROData_PolylineXY )& thePolyline,
89               const Handle( HYDROData_PolylineXY )& theTool,
90               double theTolerance ) const
91 {
92   if (thePolyline.IsNull() || theTool.IsNull())
93   {
94     return false;
95   }
96
97   HYDROData_SequenceOfObjects aSeq;
98   aSeq.Append( theTool );
99   return split( theDoc, thePolyline, aSeq, theTolerance, -1 );
100 }
101
102 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
103                                         const HYDROData_SequenceOfObjects& thePolylines,
104                                         double theTolerance )
105 {
106   int f = thePolylines.Lower(), l = thePolylines.Upper();
107   for( int i=f; i<=l; i++ )
108   {
109     Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
110     if( !split( theDoc, aPolyline, thePolylines, theTolerance, i ) )
111       return false;
112   }
113   return true;
114 }
115
116 bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theDoc,
117                                         const QString& theName,
118                                         const HYDROData_SequenceOfObjects& thePolylines,
119                                         bool isConnectByNewSegment,
120                                         double theTolerance )
121 {
122   std::deque<HYDROData_TopoCurve> aMergedCurves;
123   HYDROData_SequenceOfObjects::Iterator aPIt(thePolylines);
124   for (; aPIt.More(); aPIt.Next())
125   {
126     Handle(HYDROData_PolylineXY) aPolyline =
127       Handle(HYDROData_PolylineXY)::DownCast(aPIt.Value());
128     std::vector<TopoDS_Wire> aWires;
129     GetWires(aPolyline, aWires);
130     for (std::vector<TopoDS_Wire>::const_iterator aWIt = aWires.begin(),
131       aLastWIt = aWires.end(); aWIt != aLastWIt; ++aWIt)
132     {
133       const Standard_Boolean aResult = !isConnectByNewSegment ?
134         HYDROData_TopoCurve::Merge(theTolerance, *aWIt, aMergedCurves) :
135         HYDROData_TopoCurve::Connect(theTolerance, *aWIt, aMergedCurves);
136       if (!aResult)
137       {
138         return false;
139       }
140     }
141   }
142
143   TopoDS_Compound aWireSet;
144   BRep_Builder aBuilder;
145   aBuilder.MakeCompound(aWireSet);
146   std::deque<HYDROData_TopoCurve>::iterator aCIt = aMergedCurves.begin();
147   std::deque<HYDROData_TopoCurve>::iterator aLastCIt = aMergedCurves.end();
148   for (; aCIt != aLastCIt; ++aCIt)
149   {
150     if (!aCIt->IsEmpty())
151     {
152       aBuilder.Add(aWireSet, aCIt->Wire());
153     }
154   }
155
156   std::vector<TopoDS_Shape> aPolylines(1);
157   aPolylines[0] = aWireSet;
158   CreatePolylines(theDoc, theName, aPolylines, false);
159   return true;
160 }
161
162 bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theDoc,
163                                         const Handle( HYDROData_PolylineXY )& thePolyline,
164                                         const HYDROData_SequenceOfObjects& theTools,
165                                         double theTolerance,
166                                         int theIgnoreIndex ) const
167 {
168   if (thePolyline.IsNull())
169   {
170     return false;
171   }
172
173   std::vector<TopoDS_Wire> aCurves;
174   GetWires(thePolyline, aCurves);
175   std::vector<TopoDS_Wire> aToolCurves;
176   for( int i=theTools.Lower(), n=theTools.Upper(); i<=n; i++ )
177     if( i!=theIgnoreIndex )
178     {
179       Handle( HYDROData_PolylineXY ) aToolPolyline = 
180         Handle( HYDROData_PolylineXY )::DownCast( theTools.Value( i ) );
181       if (!aToolPolyline.IsNull())
182       {
183         std::vector<TopoDS_Wire> aTCurves;
184         GetWires(aToolPolyline, aTCurves);
185         append( aToolCurves, aTCurves);
186       }
187     }
188
189   if (aToolCurves.empty())
190   {
191     return false;
192   }
193
194   const int aPSCount = aCurves.size();
195   const int aTSCount = aToolCurves.size();
196   std::vector<TopoDS_Shape> aResult;
197   for (int aPSI = 0; aPSI < aPSCount; ++aPSI)
198   {
199     HYDROData_TopoCurve aCurve;
200     if (!aCurve.Initialize(aCurves[aPSI]))
201     {
202       continue;
203     }
204
205     std::deque<std::list<double> > aParams;
206     for (int aTSI = 0; aTSI < aTSCount; ++aTSI)
207     {
208       aCurve.Intersect(aToolCurves[aTSI], aParams);
209     }
210
211     std::deque<HYDROData_TopoCurve> aSplittedCurves;
212     aCurve.Cut(aParams, aSplittedCurves);
213     std::deque<HYDROData_TopoCurve>::const_iterator aCIt =
214       aSplittedCurves.begin();
215     std::deque<HYDROData_TopoCurve>::const_iterator aLastCIt =
216       aSplittedCurves.end();
217     for (; aCIt != aLastCIt; ++aCIt)
218     {
219       aResult.push_back(aCIt->Wire());
220     }
221   }
222
223   CreatePolylines(theDoc, thePolyline->GetName(), aResult, true);
224   return true;
225 }
226
227 void HYDROData_PolylineOperator::GetWires(
228   const Handle( HYDROData_PolylineXY )& thePolyline,
229   std::vector<TopoDS_Wire>& theWires)
230 {
231   TopoDS_Shape aShape = thePolyline->GetShape();
232   if( aShape.ShapeType()==TopAbs_WIRE )
233   {
234     theWires.push_back( TopoDS::Wire( aShape ) );
235   }
236   else
237   {
238     TopExp_Explorer anExp( aShape, TopAbs_WIRE );
239     for( ; anExp.More(); anExp.Next() )
240     {
241       theWires.push_back( TopoDS::Wire( anExp.Current() ) );
242     }
243   }
244 }
245
246 void HYDROData_PolylineOperator::Split(
247   const TopoDS_Wire& theWire,
248   const gp_Pnt2d& thePoint,
249   double theTolerance,
250   std::vector<TopoDS_Shape>& theWires)
251 {
252   HYDROData_TopoCurve aCurve;
253   if (!aCurve.Initialize(theWire))
254   {
255     theWires.push_back(theWire);
256     return;
257   }
258
259   const gp_XYZ aP(thePoint.X(), thePoint.Y(), 0);
260   std::list<TopoDS_Edge>::const_iterator aEPos;
261   double aParam;
262   aCurve.Project(aP, aEPos, aParam);
263   HYDROData_TopoCurve aCurve1, aCurve2;
264   aCurve.Cut(aEPos, aParam, aCurve1, aCurve2);
265   theWires.push_back(aCurve1.Wire());
266   if (!aCurve2.IsEmpty())
267   {
268     theWires.push_back(aCurve2.Wire());
269   }
270 }
271
272 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
273                                                   const QString& theNamePrefix,
274                                                   const std::vector<TopoDS_Shape>& theShapes,
275                                                   bool isUseIndices )
276 {
277   if( theDoc.IsNull() )
278     return false;
279
280   int n = theShapes.size();
281   int anIndex = 1;
282   for( int i=0; i<n; i++ )
283   {
284     Handle( HYDROData_PolylineXY ) aPolyline = 
285       Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
286     if( aPolyline.IsNull() )
287       return false;
288
289     aPolyline->SetShape( theShapes[i] );
290
291     if( isUseIndices )
292     {
293       QString aNewName = theNamePrefix + "_" + QString::number( anIndex );
294       if( theDoc->FindObjectByName( aNewName ).IsNull() )  // the object with such a name is not found
295         aPolyline->SetName( aNewName );
296       anIndex++;
297     }
298     else
299     {
300       aPolyline->SetName( theNamePrefix );
301     }
302   }
303   return true;
304 }
305
306 double HYDROData_PolylineOperator::ReduceDeflection(
307   const double theDeflection,
308   HYDROData_TopoCurve& theCurve,
309   int& thePieceCount)
310 {
311   // Construct the approximating B-spline.
312   std::list<gp_XYZ> aPs;
313   if (!theCurve.ValuesInKnots(aPs))
314   {
315     return -1;
316   }
317
318   Handle(TColgp_HArray1OfPnt) aPs2 = new TColgp_HArray1OfPnt(1, aPs.size());
319   {
320     std::list<gp_XYZ>::const_iterator aLastPIt = aPs.end();
321     std::list<gp_XYZ>::const_iterator aPIt = aPs.begin();
322     for (int aPN = 1; aPIt != aLastPIt; ++aPN, ++aPIt)
323     {
324       aPs2->SetValue(aPN, *aPIt);
325     }
326   }
327   Handle(Geom_BSplineCurve) aBSpline2;
328   const bool isClosed = theCurve.IsClosed();
329   if (!CurveCreator_Utils::constructBSpline(aPs2, isClosed, aBSpline2))
330   {
331     return -1;
332   }
333
334   // Calculate the piece deflections.
335   std::deque<double> aSqDefls;
336   double aMaxSqDefl = 0;
337   std::list<TopoDS_Edge>& aEdges = theCurve.Edges();
338   std::list<TopoDS_Edge>::const_iterator aLastEIt = aEdges.end();
339   {
340     std::list<TopoDS_Edge>::const_iterator aEIt = aEdges.begin();
341     for (int aPrevKCount = 0; aEIt != aLastEIt; ++aEIt)
342     {
343       TopLoc_Location aLoc;
344       double aParams[2];
345       Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
346         BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
347       const int aKCount = aBSpline->NbKnots();
348       for (int aKN = 1; aKN < aKCount; ++aKN)
349       {
350         const double aParam =
351           (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5;
352         const double aParam2 = (aBSpline2->Knot(aPrevKCount + aKN) +
353           aBSpline2->Knot(aPrevKCount + aKN + 1)) * 0.5;
354         const double aSqDefl = Abs(aBSpline->Value(aParam).
355           SquareDistance(aBSpline2->Value(aParam2)));
356         aSqDefls.push_back(aSqDefl);
357         if (aMaxSqDefl < aSqDefl)
358         {
359           aMaxSqDefl = aSqDefl;
360         }
361       }
362       aPrevKCount += aKCount - 1;
363     }
364   }
365
366   // Check whether the reducing is necessary.
367   const double aMaxDefl = Sqrt(aMaxSqDefl);
368   if (aMaxDefl <= theDeflection)
369   {
370     return aMaxDefl;
371   }
372
373   // Reduce the deflections.
374   const double aThresSqDefl =
375     Max(aMaxSqDefl * 0.25, theDeflection * theDeflection);
376   std::list<TopoDS_Edge>::iterator aEIt = aEdges.begin();
377   std::deque<double>::const_iterator aSqDIt = aSqDefls.begin();
378   thePieceCount = 0;
379   for (; aEIt != aLastEIt; ++aEIt)
380   {
381     TopLoc_Location aLoc;
382     double aParams[2];
383     Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
384       BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
385     Handle(Geom_BSplineCurve) aBSpline2 =
386       Handle(Geom_BSplineCurve)::DownCast(aBSpline->Copy());
387     const int aKCount = aBSpline->NbKnots();
388     for (int aKN = 1; aKN < aKCount; ++aSqDIt, ++aKN)
389     {
390       if (*aSqDIt > aThresSqDefl)
391       {
392         aBSpline2->InsertKnot(
393           (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5);
394       }
395     }
396     TopoDS_Edge aEdge;
397     BRep_Builder().MakeEdge(aEdge, aBSpline2, Precision::Confusion());
398     BRep_Builder().Add(aEdge, TopExp::FirstVertex(*aEIt));
399     BRep_Builder().Add(aEdge, TopExp::LastVertex(*aEIt));
400     thePieceCount += aBSpline2->NbKnots() - 1;
401     *aEIt = aEdge;
402   }
403   return aMaxDefl;
404 }