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