Salome HOME
Merge branch 'BR_LAND_COVER_MAP' of ssh://git.salome-platform.org/modules/hydro into...
[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   CreatePolylines( theDoc, theName, aPolylines, false, QColor() );
167   return true;
168 }
169
170 bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theDoc,
171                                         const Handle( HYDROData_PolylineXY )& thePolyline,
172                                         const HYDROData_SequenceOfObjects& theTools,
173                                         double theTolerance,
174                                         int theIgnoreIndex,
175                                         bool& theIsIntersected) const
176 {
177   theIsIntersected = false;
178
179   if (thePolyline.IsNull())
180   {
181     return false;
182   }
183
184   std::vector<TopoDS_Wire> aCurves;
185   GetWires(thePolyline, aCurves);
186   std::vector<TopoDS_Wire> aToolCurves;
187   for( int i=theTools.Lower(), n=theTools.Upper(); i<=n; i++ )
188     if( i!=theIgnoreIndex )
189     {
190       Handle( HYDROData_PolylineXY ) aToolPolyline = 
191         Handle( HYDROData_PolylineXY )::DownCast( theTools.Value( i ) );
192       if (!aToolPolyline.IsNull())
193       {
194         std::vector<TopoDS_Wire> aTCurves;
195         GetWires(aToolPolyline, aTCurves);
196         append( aToolCurves, aTCurves);
197       }
198     }
199
200   if (aToolCurves.empty())
201   {
202     return false;
203   }
204
205   const int aPSCount = aCurves.size();
206   const int aTSCount = aToolCurves.size();
207   std::vector<TopoDS_Shape> aResult;
208   for (int aPSI = 0; aPSI < aPSCount; ++aPSI)
209   {
210     HYDROData_TopoCurve aCurve;
211     if (!aCurve.Initialize(aCurves[aPSI]))
212     {
213       continue;
214     }
215
216     std::deque<std::list<double> > aParams;
217     for (int aTSI = 0; aTSI < aTSCount; ++aTSI)
218     {
219       aCurve.Intersect(aToolCurves[aTSI], aParams);
220     }
221
222     std::deque<HYDROData_TopoCurve> aSplittedCurves;
223     theIsIntersected |= aCurve.Cut(aParams, aSplittedCurves);
224     std::deque<HYDROData_TopoCurve>::const_iterator aCIt =
225       aSplittedCurves.begin();
226     std::deque<HYDROData_TopoCurve>::const_iterator aLastCIt =
227       aSplittedCurves.end();
228     for (; aCIt != aLastCIt; ++aCIt)
229     {
230       aResult.push_back(aCIt->Wire());
231     }
232   }
233
234   CreatePolylines( theDoc, thePolyline->GetName(), aResult, true, thePolyline->GetWireColor() );
235   return true;
236 }
237
238 void HYDROData_PolylineOperator::GetWires(
239   const Handle( HYDROData_PolylineXY )& thePolyline,
240   std::vector<TopoDS_Wire>& theWires)
241 {
242   TopoDS_Shape aShape = thePolyline->GetShape();
243   if( aShape.ShapeType()==TopAbs_WIRE )
244   {
245     theWires.push_back( TopoDS::Wire( aShape ) );
246   }
247   else
248   {
249     TopExp_Explorer anExp( aShape, TopAbs_WIRE );
250     for( ; anExp.More(); anExp.Next() )
251     {
252       theWires.push_back( TopoDS::Wire( anExp.Current() ) );
253     }
254   }
255 }
256
257 void HYDROData_PolylineOperator::Split(
258   const TopoDS_Wire& theWire,
259   const gp_Pnt2d& thePoint,
260   double theTolerance,
261   std::vector<TopoDS_Shape>& theWires)
262 {
263   HYDROData_TopoCurve aCurve;
264   if (!aCurve.Initialize(theWire))
265   {
266     theWires.push_back(theWire);
267     return;
268   }
269
270   const gp_XYZ aP(thePoint.X(), thePoint.Y(), 0);
271   std::list<TopoDS_Edge>::const_iterator aEPos;
272   double aParam;
273   aCurve.Project(aP, aEPos, aParam);
274   HYDROData_TopoCurve aCurve1, aCurve2;
275   aCurve.Cut(aEPos, aParam, aCurve1, aCurve2);
276   theWires.push_back(aCurve1.Wire());
277   if (!aCurve2.IsEmpty())
278   {
279     theWires.push_back(aCurve2.Wire());
280   }
281 }
282
283 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
284                                                   const QString& theNamePrefix,
285                                                   const std::vector<TopoDS_Shape>& theShapes,
286                                                   bool isUseIndices,
287                                                   const QColor& theColor )
288 {
289   if( theDoc.IsNull() )
290     return false;
291
292   int n = theShapes.size();
293   int anIndex = 1;
294   for( int i=0; i<n; i++ )
295   {
296     Handle( HYDROData_PolylineXY ) aPolyline = 
297       Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
298     if( aPolyline.IsNull() )
299       return false;
300
301     aPolyline->SetShape( theShapes[i] );
302
303     if( isUseIndices )
304     {
305       QString aNewName = theNamePrefix + "_" + QString::number( anIndex );
306       if( theDoc->FindObjectByName( aNewName ).IsNull() )  // the object with such a name is not found
307         aPolyline->SetName( aNewName );
308       anIndex++;
309     }
310     else
311     {
312       aPolyline->SetName( theNamePrefix );
313     }
314
315     if( theColor.isValid() )
316       aPolyline->SetWireColor( theColor );
317   }
318   return true;
319 }
320
321 double HYDROData_PolylineOperator::ReduceDeflection(
322   const double theDeflection,
323   HYDROData_TopoCurve& theCurve,
324   int& thePieceCount)
325 {
326   // Construct the approximating B-spline.
327   std::list<gp_XYZ> aPs;
328   if (!theCurve.ValuesInKnots(aPs))
329   {
330     return -1;
331   }
332
333   Handle(TColgp_HArray1OfPnt) aPs2 = new TColgp_HArray1OfPnt(1, aPs.size());
334   {
335     std::list<gp_XYZ>::const_iterator aLastPIt = aPs.end();
336     std::list<gp_XYZ>::const_iterator aPIt = aPs.begin();
337     for (int aPN = 1; aPIt != aLastPIt; ++aPN, ++aPIt)
338     {
339       aPs2->SetValue(aPN, *aPIt);
340     }
341   }
342   Handle(Geom_BSplineCurve) aBSpline2;
343   const bool isClosed = theCurve.IsClosed();
344 #ifndef LIGHT_MODE
345   if (!CurveCreator_Utils::constructBSpline(aPs2, isClosed, aBSpline2))
346 #endif
347   {
348     return -1;
349   }
350
351   // Calculate the piece deflections.
352   std::deque<double> aSqDefls;
353   double aMaxSqDefl = 0;
354   std::list<TopoDS_Edge>& aEdges = theCurve.Edges();
355   std::list<TopoDS_Edge>::const_iterator aLastEIt = aEdges.end();
356   {
357     std::list<TopoDS_Edge>::const_iterator aEIt = aEdges.begin();
358     for (int aPrevKCount = 0; aEIt != aLastEIt; ++aEIt)
359     {
360       TopLoc_Location aLoc;
361       double aParams[2];
362       Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
363         BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
364       const int aKCount = aBSpline->NbKnots();
365       for (int aKN = 1; aKN < aKCount; ++aKN)
366       {
367         const double aParam =
368           (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5;
369         const double aParam2 = (aBSpline2->Knot(aPrevKCount + aKN) +
370           aBSpline2->Knot(aPrevKCount + aKN + 1)) * 0.5;
371         const double aSqDefl = Abs(aBSpline->Value(aParam).
372           SquareDistance(aBSpline2->Value(aParam2)));
373         aSqDefls.push_back(aSqDefl);
374         if (aMaxSqDefl < aSqDefl)
375         {
376           aMaxSqDefl = aSqDefl;
377         }
378       }
379       aPrevKCount += aKCount - 1;
380     }
381   }
382
383   // Check whether the reducing is necessary.
384   const double aMaxDefl = Sqrt(aMaxSqDefl);
385   if (aMaxDefl <= theDeflection)
386   {
387     return aMaxDefl;
388   }
389
390   // Reduce the deflections.
391   const double aThresSqDefl =
392     Max(aMaxSqDefl * 0.25, theDeflection * theDeflection);
393   std::list<TopoDS_Edge>::iterator aEIt = aEdges.begin();
394   std::deque<double>::const_iterator aSqDIt = aSqDefls.begin();
395   thePieceCount = 0;
396   for (; aEIt != aLastEIt; ++aEIt)
397   {
398     TopLoc_Location aLoc;
399     double aParams[2];
400     Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
401       BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
402     Handle(Geom_BSplineCurve) aBSpline2 =
403       Handle(Geom_BSplineCurve)::DownCast(aBSpline->Copy());
404     const int aKCount = aBSpline->NbKnots();
405     for (int aKN = 1; aKN < aKCount; ++aSqDIt, ++aKN)
406     {
407       if (*aSqDIt > aThresSqDefl)
408       {
409         aBSpline2->InsertKnot(
410           (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5);
411       }
412     }
413     TopoDS_Edge aEdge;
414     BRep_Builder().MakeEdge(aEdge, aBSpline2, Precision::Confusion());
415     BRep_Builder().Add(aEdge, TopExp::FirstVertex(*aEIt));
416     BRep_Builder().Add(aEdge, TopExp::LastVertex(*aEIt));
417     thePieceCount += aBSpline2->NbKnots() - 1;
418     *aEIt = aEdge;
419   }
420   return aMaxDefl;
421 }
422
423 bool HYDROData_PolylineOperator::Extract( const Handle(HYDROData_Document)& theDocument,
424                                           const Handle(HYDROData_Object)& theObject )
425 {
426   if( theObject.IsNull() || theDocument.IsNull() )
427     return false;
428
429   QList<TopoDS_Shape> aBoundShapes;
430   QStringList aBoundNames;
431
432   theObject->GetBoundaries( aBoundShapes, aBoundNames );
433
434   for( int i=0, n=aBoundShapes.size(); i<n; i++ )
435   {
436     TopoDS_Shape aShape = aBoundShapes[i];
437     if( aShape.IsNull() )
438       continue;
439
440     QString aBoundName = i<aBoundNames.size() ? aBoundNames[i] : "";
441
442     Handle( HYDROData_PolylineXY ) aPolyline = 
443       Handle( HYDROData_PolylineXY )::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
444     
445     if( aPolyline.IsNull() )
446       return false;
447
448     aPolyline->SetShape( aShape );
449
450     int anIndex = 0;
451     QString aName = aBoundName;
452     while( !theDocument->FindObjectByName( aName ).IsNull() )
453     {
454       anIndex++;
455       aName = aBoundName + "_" + QString::number( anIndex );
456     }
457     aPolyline->SetName( aName );
458   }
459
460   return true;
461 }