Salome HOME
Merge branch 'BR_LAND_COVER_MAP' into BR_quadtree
[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> aSplitCurves;
226     theIsIntersected |= aCurve.Cut(aParams, aSplitCurves);
227     std::deque<HYDROData_TopoCurve>::const_iterator aCIt =
228       aSplitCurves.begin();
229     std::deque<HYDROData_TopoCurve>::const_iterator aLastCIt =
230       aSplitCurves.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       while( !theDoc->FindObjectByName( aNewName ).IsNull() )  // the object with such a name is not found
310       {
311         anIndex++;
312         aNewName = theNamePrefix + "_" + QString::number( anIndex );
313       }
314       aPolyline->SetName( aNewName );
315     }
316     else
317     {
318       aPolyline->SetName( theNamePrefix );
319     }
320
321     if( theColor.isValid() )
322       aPolyline->SetWireColor( theColor );
323   }
324   return true;
325 }
326
327 double HYDROData_PolylineOperator::ReduceDeflection(
328   const double theDeflection,
329   HYDROData_TopoCurve& theCurve,
330   int& thePieceCount)
331 {
332   // Construct the approximating B-spline.
333   std::list<gp_XYZ> aPs;
334   if (!theCurve.ValuesInKnots(aPs))
335   {
336     return -1;
337   }
338
339   Handle(TColgp_HArray1OfPnt) aPs2 = new TColgp_HArray1OfPnt(1, aPs.size());
340   {
341     std::list<gp_XYZ>::const_iterator aLastPIt = aPs.end();
342     std::list<gp_XYZ>::const_iterator aPIt = aPs.begin();
343     for (int aPN = 1; aPIt != aLastPIt; ++aPN, ++aPIt)
344     {
345       aPs2->SetValue(aPN, *aPIt);
346     }
347   }
348   Handle(Geom_BSplineCurve) aBSpline2;
349   const bool isClosed = theCurve.IsClosed();
350 #ifndef LIGHT_MODE
351   if (!CurveCreator_Utils::constructBSpline(aPs2, isClosed, aBSpline2))
352 #endif
353   {
354     return -1;
355   }
356
357   // Calculate the piece deflections.
358   std::deque<double> aSqDefls;
359   double aMaxSqDefl = 0;
360   std::list<TopoDS_Edge>& aEdges = theCurve.Edges();
361   std::list<TopoDS_Edge>::const_iterator aLastEIt = aEdges.end();
362   {
363     std::list<TopoDS_Edge>::const_iterator aEIt = aEdges.begin();
364     for (int aPrevKCount = 0; aEIt != aLastEIt; ++aEIt)
365     {
366       TopLoc_Location aLoc;
367       double aParams[2];
368       Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
369         BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
370       const int aKCount = aBSpline->NbKnots();
371       for (int aKN = 1; aKN < aKCount; ++aKN)
372       {
373         const double aParam =
374           (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5;
375         const double aParam2 = (aBSpline2->Knot(aPrevKCount + aKN) +
376           aBSpline2->Knot(aPrevKCount + aKN + 1)) * 0.5;
377         const double aSqDefl = Abs(aBSpline->Value(aParam).
378           SquareDistance(aBSpline2->Value(aParam2)));
379         aSqDefls.push_back(aSqDefl);
380         if (aMaxSqDefl < aSqDefl)
381         {
382           aMaxSqDefl = aSqDefl;
383         }
384       }
385       aPrevKCount += aKCount - 1;
386     }
387   }
388
389   // Check whether the reducing is necessary.
390   const double aMaxDefl = Sqrt(aMaxSqDefl);
391   if (aMaxDefl <= theDeflection)
392   {
393     return aMaxDefl;
394   }
395
396   // Reduce the deflections.
397   const double aThresSqDefl =
398     Max(aMaxSqDefl * 0.25, theDeflection * theDeflection);
399   std::list<TopoDS_Edge>::iterator aEIt = aEdges.begin();
400   std::deque<double>::const_iterator aSqDIt = aSqDefls.begin();
401   thePieceCount = 0;
402   for (; aEIt != aLastEIt; ++aEIt)
403   {
404     TopLoc_Location aLoc;
405     double aParams[2];
406     Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
407       BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
408     Handle(Geom_BSplineCurve) aBSpline2 =
409       Handle(Geom_BSplineCurve)::DownCast(aBSpline->Copy());
410     const int aKCount = aBSpline->NbKnots();
411     for (int aKN = 1; aKN < aKCount; ++aSqDIt, ++aKN)
412     {
413       if (*aSqDIt > aThresSqDefl)
414       {
415         aBSpline2->InsertKnot(
416           (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5);
417       }
418     }
419     TopoDS_Edge aEdge;
420     BRep_Builder().MakeEdge(aEdge, aBSpline2, Precision::Confusion());
421     BRep_Builder().Add(aEdge, TopExp::FirstVertex(*aEIt));
422     BRep_Builder().Add(aEdge, TopExp::LastVertex(*aEIt));
423     thePieceCount += aBSpline2->NbKnots() - 1;
424     *aEIt = aEdge;
425   }
426   return aMaxDefl;
427 }
428
429 bool HYDROData_PolylineOperator::Extract( const Handle(HYDROData_Document)& theDocument,
430                                           const Handle(HYDROData_Object)& theObject )
431 {
432   if( theObject.IsNull() || theDocument.IsNull() )
433     return false;
434
435   QList<TopoDS_Shape> aBoundShapes;
436   QStringList aBoundNames;
437
438   theObject->GetBoundaries( aBoundShapes, aBoundNames );
439
440   for( int i=0, n=aBoundShapes.size(); i<n; i++ )
441   {
442     TopoDS_Shape aShape = aBoundShapes[i];
443     if( aShape.IsNull() )
444       continue;
445
446     QString aBoundName = i<aBoundNames.size() ? aBoundNames[i] : "";
447
448     Handle( HYDROData_PolylineXY ) aPolyline = 
449       Handle( HYDROData_PolylineXY )::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
450     
451     if( aPolyline.IsNull() )
452       return false;
453
454     aPolyline->SetShape( aShape );
455
456     int anIndex = 0;
457     QString aName = aBoundName;
458     while( !theDocument->FindObjectByName( aName ).IsNull() )
459     {
460       anIndex++;
461       aName = aBoundName + "_" + QString::number( anIndex );
462     }
463     aPolyline->SetName( aName );
464   }
465
466   return true;
467 }