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