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