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