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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <HYDROData_PolylineOperator.h>
20 #include <HYDROData_Document.h>
21 #include <HYDROData_TopoCurve.h>
23 #include <CurveCreator_Utils.hxx>
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>
42 #include <TopoDS_Edge.hxx>
43 #include <TopoDS_Wire.hxx>
45 #include <TopExp_Explorer.hxx>
46 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
49 template<class T> void append( std::vector<T>& theList, const std::vector<T>& theList2 )
51 int aSize = theList.size();
52 int aNewSize = aSize + theList2.size();
57 theList.resize( aNewSize );
58 for( int i=aSize, j=0; i<aNewSize; i++, j++ )
59 theList[i] = theList2[j];
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
67 if (thePolyline.IsNull())
72 std::vector<gp_Pnt2d> aPointsList( 1 );
73 aPointsList[0] = thePoint;
74 std::vector<TopoDS_Wire> aCurves;
75 GetWires(thePolyline, aCurves);
77 for( int i=0, n=aCurves.size(); i<n; i++ )
79 std::vector<TopoDS_Shape> aCurvesList;
80 Split(aCurves[i], thePoint, theTolerance, aCurvesList);
81 bool isLocalOK = CreatePolylines( theDoc, thePolyline->GetName(), aCurvesList, true );
82 isOK = isOK && isLocalOK;
87 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
88 const Handle( HYDROData_PolylineXY )& thePolyline,
89 const Handle( HYDROData_PolylineXY )& theTool,
90 double theTolerance ) const
92 if (thePolyline.IsNull() || theTool.IsNull())
97 HYDROData_SequenceOfObjects aSeq;
98 aSeq.Append( theTool );
99 return split( theDoc, thePolyline, aSeq, theTolerance, -1 );
102 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
103 const HYDROData_SequenceOfObjects& thePolylines,
104 double theTolerance )
106 int f = thePolylines.Lower(), l = thePolylines.Upper();
107 for( int i=f; i<=l; i++ )
109 Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
110 if( !split( theDoc, aPolyline, thePolylines, theTolerance, i ) )
116 bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theDoc,
117 const QString& theName,
118 const HYDROData_SequenceOfObjects& thePolylines,
119 bool isConnectByNewSegment,
120 double theTolerance )
122 std::deque<HYDROData_TopoCurve> aMergedCurves;
123 HYDROData_SequenceOfObjects::Iterator aPIt(thePolylines);
124 for (; aPIt.More(); aPIt.Next())
126 Handle(HYDROData_PolylineXY) aPolyline =
127 Handle(HYDROData_PolylineXY)::DownCast(aPIt.Value());
128 std::vector<TopoDS_Wire> aWires;
129 GetWires(aPolyline, aWires);
130 for (std::vector<TopoDS_Wire>::const_iterator aWIt = aWires.begin(),
131 aLastWIt = aWires.end(); aWIt != aLastWIt; ++aWIt)
133 const Standard_Boolean aResult = !isConnectByNewSegment ?
134 HYDROData_TopoCurve::Merge(theTolerance, *aWIt, aMergedCurves) :
135 HYDROData_TopoCurve::Connect(theTolerance, *aWIt, aMergedCurves);
143 TopoDS_Compound aWireSet;
144 BRep_Builder aBuilder;
145 aBuilder.MakeCompound(aWireSet);
146 std::deque<HYDROData_TopoCurve>::iterator aCIt = aMergedCurves.begin();
147 std::deque<HYDROData_TopoCurve>::iterator aLastCIt = aMergedCurves.end();
148 for (; aCIt != aLastCIt; ++aCIt)
150 if (!aCIt->IsEmpty())
152 aBuilder.Add(aWireSet, aCIt->Wire());
156 std::vector<TopoDS_Shape> aPolylines(1);
157 aPolylines[0] = aWireSet;
158 CreatePolylines(theDoc, theName, aPolylines, false);
162 bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theDoc,
163 const Handle( HYDROData_PolylineXY )& thePolyline,
164 const HYDROData_SequenceOfObjects& theTools,
166 int theIgnoreIndex ) const
168 if (thePolyline.IsNull())
173 std::vector<TopoDS_Wire> aCurves;
174 GetWires(thePolyline, aCurves);
175 std::vector<TopoDS_Wire> aToolCurves;
176 for( int i=theTools.Lower(), n=theTools.Upper(); i<=n; i++ )
177 if( i!=theIgnoreIndex )
179 Handle( HYDROData_PolylineXY ) aToolPolyline =
180 Handle( HYDROData_PolylineXY )::DownCast( theTools.Value( i ) );
181 if (!aToolPolyline.IsNull())
183 std::vector<TopoDS_Wire> aTCurves;
184 GetWires(aToolPolyline, aTCurves);
185 append( aToolCurves, aTCurves);
189 if (aToolCurves.empty())
194 const int aPSCount = aCurves.size();
195 const int aTSCount = aToolCurves.size();
196 std::vector<TopoDS_Shape> aResult;
197 for (int aPSI = 0; aPSI < aPSCount; ++aPSI)
199 HYDROData_TopoCurve aCurve;
200 if (!aCurve.Initialize(aCurves[aPSI]))
205 std::deque<std::list<double> > aParams;
206 for (int aTSI = 0; aTSI < aTSCount; ++aTSI)
208 aCurve.Intersect(aToolCurves[aTSI], aParams);
211 std::deque<HYDROData_TopoCurve> aSplittedCurves;
212 aCurve.Cut(aParams, aSplittedCurves);
213 std::deque<HYDROData_TopoCurve>::const_iterator aCIt =
214 aSplittedCurves.begin();
215 std::deque<HYDROData_TopoCurve>::const_iterator aLastCIt =
216 aSplittedCurves.end();
217 for (; aCIt != aLastCIt; ++aCIt)
219 aResult.push_back(aCIt->Wire());
223 CreatePolylines(theDoc, thePolyline->GetName(), aResult, true);
227 void HYDROData_PolylineOperator::GetWires(
228 const Handle( HYDROData_PolylineXY )& thePolyline,
229 std::vector<TopoDS_Wire>& theWires)
231 TopoDS_Shape aShape = thePolyline->GetShape();
232 if( aShape.ShapeType()==TopAbs_WIRE )
234 theWires.push_back( TopoDS::Wire( aShape ) );
238 TopExp_Explorer anExp( aShape, TopAbs_WIRE );
239 for( ; anExp.More(); anExp.Next() )
241 theWires.push_back( TopoDS::Wire( anExp.Current() ) );
246 void HYDROData_PolylineOperator::Split(
247 const TopoDS_Wire& theWire,
248 const gp_Pnt2d& thePoint,
250 std::vector<TopoDS_Shape>& theWires)
252 HYDROData_TopoCurve aCurve;
253 if (!aCurve.Initialize(theWire))
255 theWires.push_back(theWire);
259 const gp_XYZ aP(thePoint.X(), thePoint.Y(), 0);
260 std::list<TopoDS_Edge>::const_iterator aEPos;
262 aCurve.Project(aP, aEPos, aParam);
263 HYDROData_TopoCurve aCurve1, aCurve2;
264 aCurve.Cut(aEPos, aParam, aCurve1, aCurve2);
265 theWires.push_back(aCurve1.Wire());
266 if (!aCurve2.IsEmpty())
268 theWires.push_back(aCurve2.Wire());
272 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
273 const QString& theNamePrefix,
274 const std::vector<TopoDS_Shape>& theShapes,
277 if( theDoc.IsNull() )
280 int n = theShapes.size();
282 for( int i=0; i<n; i++ )
284 Handle( HYDROData_PolylineXY ) aPolyline =
285 Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
286 if( aPolyline.IsNull() )
289 aPolyline->SetShape( theShapes[i] );
293 QString aNewName = theNamePrefix + "_" + QString::number( anIndex );
294 if( theDoc->FindObjectByName( aNewName ).IsNull() ) // the object with such a name is not found
295 aPolyline->SetName( aNewName );
300 aPolyline->SetName( theNamePrefix );
306 double HYDROData_PolylineOperator::ReduceDeflection(
307 const double theDeflection,
308 HYDROData_TopoCurve& theCurve,
311 // Construct the approximating B-spline.
312 std::list<gp_XYZ> aPs;
313 if (!theCurve.ValuesInKnots(aPs))
318 Handle(TColgp_HArray1OfPnt) aPs2 = new TColgp_HArray1OfPnt(1, aPs.size());
320 std::list<gp_XYZ>::const_iterator aLastPIt = aPs.end();
321 std::list<gp_XYZ>::const_iterator aPIt = aPs.begin();
322 for (int aPN = 1; aPIt != aLastPIt; ++aPN, ++aPIt)
324 aPs2->SetValue(aPN, *aPIt);
327 Handle(Geom_BSplineCurve) aBSpline2;
328 const bool isClosed = theCurve.IsClosed();
329 if (!CurveCreator_Utils::constructBSpline(aPs2, isClosed, aBSpline2))
334 // Calculate the piece deflections.
335 std::deque<double> aSqDefls;
336 double aMaxSqDefl = 0;
337 std::list<TopoDS_Edge>& aEdges = theCurve.Edges();
338 std::list<TopoDS_Edge>::const_iterator aLastEIt = aEdges.end();
340 std::list<TopoDS_Edge>::const_iterator aEIt = aEdges.begin();
341 for (int aPrevKCount = 0; aEIt != aLastEIt; ++aEIt)
343 TopLoc_Location aLoc;
345 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
346 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
347 const int aKCount = aBSpline->NbKnots();
348 for (int aKN = 1; aKN < aKCount; ++aKN)
350 const double aParam =
351 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5;
352 const double aParam2 = (aBSpline2->Knot(aPrevKCount + aKN) +
353 aBSpline2->Knot(aPrevKCount + aKN + 1)) * 0.5;
354 const double aSqDefl = Abs(aBSpline->Value(aParam).
355 SquareDistance(aBSpline2->Value(aParam2)));
356 aSqDefls.push_back(aSqDefl);
357 if (aMaxSqDefl < aSqDefl)
359 aMaxSqDefl = aSqDefl;
362 aPrevKCount += aKCount - 1;
366 // Check whether the reducing is necessary.
367 const double aMaxDefl = Sqrt(aMaxSqDefl);
368 if (aMaxDefl <= theDeflection)
373 // Reduce the deflections.
374 const double aThresSqDefl =
375 Max(aMaxSqDefl * 0.25, theDeflection * theDeflection);
376 std::list<TopoDS_Edge>::iterator aEIt = aEdges.begin();
377 std::deque<double>::const_iterator aSqDIt = aSqDefls.begin();
379 for (; aEIt != aLastEIt; ++aEIt)
381 TopLoc_Location aLoc;
383 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
384 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
385 Handle(Geom_BSplineCurve) aBSpline2 =
386 Handle(Geom_BSplineCurve)::DownCast(aBSpline->Copy());
387 const int aKCount = aBSpline->NbKnots();
388 for (int aKN = 1; aKN < aKCount; ++aSqDIt, ++aKN)
390 if (*aSqDIt > aThresSqDefl)
392 aBSpline2->InsertKnot(
393 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5);
397 BRep_Builder().MakeEdge(aEdge, aBSpline2, Precision::Confusion());
398 BRep_Builder().Add(aEdge, TopExp::FirstVertex(*aEIt));
399 BRep_Builder().Add(aEdge, TopExp::LastVertex(*aEIt));
400 thePieceCount += aBSpline2->NbKnots() - 1;