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