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,
91 bool& theIsIntersected) const
93 if (thePolyline.IsNull() || theTool.IsNull())
98 HYDROData_SequenceOfObjects aSeq;
99 aSeq.Append( theTool );
100 return split( theDoc, thePolyline, aSeq, theTolerance, -1, theIsIntersected);
103 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
104 const HYDROData_SequenceOfObjects& thePolylines,
105 double theTolerance )
107 int f = thePolylines.Lower(), l = thePolylines.Upper();
108 for( int i=f; i<=l; i++ )
110 Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
112 if( !split( theDoc, aPolyline, thePolylines, theTolerance, i, isIntersected) )
118 bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theDoc,
119 const QString& theName,
120 const HYDROData_SequenceOfObjects& thePolylines,
121 bool isConnectByNewSegment,
122 double theTolerance )
124 std::deque<HYDROData_TopoCurve> aMergedCurves;
125 HYDROData_SequenceOfObjects::Iterator aPIt(thePolylines);
126 for (; aPIt.More(); aPIt.Next())
128 Handle(HYDROData_PolylineXY) aPolyline =
129 Handle(HYDROData_PolylineXY)::DownCast(aPIt.Value());
130 std::vector<TopoDS_Wire> aWires;
131 GetWires(aPolyline, aWires);
132 for (std::vector<TopoDS_Wire>::const_iterator aWIt = aWires.begin(),
133 aLastWIt = aWires.end(); aWIt != aLastWIt; ++aWIt)
135 const Standard_Boolean aResult = !isConnectByNewSegment ?
136 HYDROData_TopoCurve::Merge(theTolerance, *aWIt, aMergedCurves) :
137 HYDROData_TopoCurve::Connect(theTolerance, *aWIt, aMergedCurves);
145 TopoDS_Compound aWireSet;
146 BRep_Builder aBuilder;
147 aBuilder.MakeCompound(aWireSet);
148 std::deque<HYDROData_TopoCurve>::iterator aCIt = aMergedCurves.begin();
149 std::deque<HYDROData_TopoCurve>::iterator aLastCIt = aMergedCurves.end();
150 for (; aCIt != aLastCIt; ++aCIt)
152 if (!aCIt->IsEmpty())
154 aBuilder.Add(aWireSet, aCIt->Wire());
158 std::vector<TopoDS_Shape> aPolylines(1);
159 aPolylines[0] = aWireSet;
160 CreatePolylines(theDoc, theName, aPolylines, false);
164 bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theDoc,
165 const Handle( HYDROData_PolylineXY )& thePolyline,
166 const HYDROData_SequenceOfObjects& theTools,
169 bool& theIsIntersected) const
171 theIsIntersected = false;
173 if (thePolyline.IsNull())
178 std::vector<TopoDS_Wire> aCurves;
179 GetWires(thePolyline, aCurves);
180 std::vector<TopoDS_Wire> aToolCurves;
181 for( int i=theTools.Lower(), n=theTools.Upper(); i<=n; i++ )
182 if( i!=theIgnoreIndex )
184 Handle( HYDROData_PolylineXY ) aToolPolyline =
185 Handle( HYDROData_PolylineXY )::DownCast( theTools.Value( i ) );
186 if (!aToolPolyline.IsNull())
188 std::vector<TopoDS_Wire> aTCurves;
189 GetWires(aToolPolyline, aTCurves);
190 append( aToolCurves, aTCurves);
194 if (aToolCurves.empty())
199 const int aPSCount = aCurves.size();
200 const int aTSCount = aToolCurves.size();
201 std::vector<TopoDS_Shape> aResult;
202 for (int aPSI = 0; aPSI < aPSCount; ++aPSI)
204 HYDROData_TopoCurve aCurve;
205 if (!aCurve.Initialize(aCurves[aPSI]))
210 std::deque<std::list<double> > aParams;
211 for (int aTSI = 0; aTSI < aTSCount; ++aTSI)
213 aCurve.Intersect(aToolCurves[aTSI], aParams);
216 std::deque<HYDROData_TopoCurve> aSplittedCurves;
217 theIsIntersected |= aCurve.Cut(aParams, aSplittedCurves);
218 std::deque<HYDROData_TopoCurve>::const_iterator aCIt =
219 aSplittedCurves.begin();
220 std::deque<HYDROData_TopoCurve>::const_iterator aLastCIt =
221 aSplittedCurves.end();
222 for (; aCIt != aLastCIt; ++aCIt)
224 aResult.push_back(aCIt->Wire());
228 CreatePolylines(theDoc, thePolyline->GetName(), aResult, true);
232 void HYDROData_PolylineOperator::GetWires(
233 const Handle( HYDROData_PolylineXY )& thePolyline,
234 std::vector<TopoDS_Wire>& theWires)
236 TopoDS_Shape aShape = thePolyline->GetShape();
237 if( aShape.ShapeType()==TopAbs_WIRE )
239 theWires.push_back( TopoDS::Wire( aShape ) );
243 TopExp_Explorer anExp( aShape, TopAbs_WIRE );
244 for( ; anExp.More(); anExp.Next() )
246 theWires.push_back( TopoDS::Wire( anExp.Current() ) );
251 void HYDROData_PolylineOperator::Split(
252 const TopoDS_Wire& theWire,
253 const gp_Pnt2d& thePoint,
255 std::vector<TopoDS_Shape>& theWires)
257 HYDROData_TopoCurve aCurve;
258 if (!aCurve.Initialize(theWire))
260 theWires.push_back(theWire);
264 const gp_XYZ aP(thePoint.X(), thePoint.Y(), 0);
265 std::list<TopoDS_Edge>::const_iterator aEPos;
267 aCurve.Project(aP, aEPos, aParam);
268 HYDROData_TopoCurve aCurve1, aCurve2;
269 aCurve.Cut(aEPos, aParam, aCurve1, aCurve2);
270 theWires.push_back(aCurve1.Wire());
271 if (!aCurve2.IsEmpty())
273 theWires.push_back(aCurve2.Wire());
277 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
278 const QString& theNamePrefix,
279 const std::vector<TopoDS_Shape>& theShapes,
282 if( theDoc.IsNull() )
285 int n = theShapes.size();
287 for( int i=0; i<n; i++ )
289 Handle( HYDROData_PolylineXY ) aPolyline =
290 Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
291 if( aPolyline.IsNull() )
294 aPolyline->SetShape( theShapes[i] );
298 QString aNewName = theNamePrefix + "_" + QString::number( anIndex );
299 if( theDoc->FindObjectByName( aNewName ).IsNull() ) // the object with such a name is not found
300 aPolyline->SetName( aNewName );
305 aPolyline->SetName( theNamePrefix );
311 double HYDROData_PolylineOperator::ReduceDeflection(
312 const double theDeflection,
313 HYDROData_TopoCurve& theCurve,
316 // Construct the approximating B-spline.
317 std::list<gp_XYZ> aPs;
318 if (!theCurve.ValuesInKnots(aPs))
323 Handle(TColgp_HArray1OfPnt) aPs2 = new TColgp_HArray1OfPnt(1, aPs.size());
325 std::list<gp_XYZ>::const_iterator aLastPIt = aPs.end();
326 std::list<gp_XYZ>::const_iterator aPIt = aPs.begin();
327 for (int aPN = 1; aPIt != aLastPIt; ++aPN, ++aPIt)
329 aPs2->SetValue(aPN, *aPIt);
332 Handle(Geom_BSplineCurve) aBSpline2;
333 const bool isClosed = theCurve.IsClosed();
334 if (!CurveCreator_Utils::constructBSpline(aPs2, isClosed, aBSpline2))
339 // Calculate the piece deflections.
340 std::deque<double> aSqDefls;
341 double aMaxSqDefl = 0;
342 std::list<TopoDS_Edge>& aEdges = theCurve.Edges();
343 std::list<TopoDS_Edge>::const_iterator aLastEIt = aEdges.end();
345 std::list<TopoDS_Edge>::const_iterator aEIt = aEdges.begin();
346 for (int aPrevKCount = 0; aEIt != aLastEIt; ++aEIt)
348 TopLoc_Location aLoc;
350 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
351 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
352 const int aKCount = aBSpline->NbKnots();
353 for (int aKN = 1; aKN < aKCount; ++aKN)
355 const double aParam =
356 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5;
357 const double aParam2 = (aBSpline2->Knot(aPrevKCount + aKN) +
358 aBSpline2->Knot(aPrevKCount + aKN + 1)) * 0.5;
359 const double aSqDefl = Abs(aBSpline->Value(aParam).
360 SquareDistance(aBSpline2->Value(aParam2)));
361 aSqDefls.push_back(aSqDefl);
362 if (aMaxSqDefl < aSqDefl)
364 aMaxSqDefl = aSqDefl;
367 aPrevKCount += aKCount - 1;
371 // Check whether the reducing is necessary.
372 const double aMaxDefl = Sqrt(aMaxSqDefl);
373 if (aMaxDefl <= theDeflection)
378 // Reduce the deflections.
379 const double aThresSqDefl =
380 Max(aMaxSqDefl * 0.25, theDeflection * theDeflection);
381 std::list<TopoDS_Edge>::iterator aEIt = aEdges.begin();
382 std::deque<double>::const_iterator aSqDIt = aSqDefls.begin();
384 for (; aEIt != aLastEIt; ++aEIt)
386 TopLoc_Location aLoc;
388 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
389 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
390 Handle(Geom_BSplineCurve) aBSpline2 =
391 Handle(Geom_BSplineCurve)::DownCast(aBSpline->Copy());
392 const int aKCount = aBSpline->NbKnots();
393 for (int aKN = 1; aKN < aKCount; ++aSqDIt, ++aKN)
395 if (*aSqDIt > aThresSqDefl)
397 aBSpline2->InsertKnot(
398 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5);
402 BRep_Builder().MakeEdge(aEdge, aBSpline2, Precision::Confusion());
403 BRep_Builder().Add(aEdge, TopExp::FirstVertex(*aEIt));
404 BRep_Builder().Add(aEdge, TopExp::LastVertex(*aEIt));
405 thePieceCount += aBSpline2->NbKnots() - 1;