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