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>
22 #include <HYDROData_Object.h>
25 #include <CurveCreator_Utils.hxx>
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>
45 #include <TopoDS_Edge.hxx>
46 #include <TopoDS_Wire.hxx>
48 #include <TopExp_Explorer.hxx>
49 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
50 #include <QStringList>
51 #include <Geom_BSplineCurve.hxx>
53 template<class T> void append( std::vector<T>& theList, const std::vector<T>& theList2 )
55 int aSize = theList.size();
56 int aNewSize = aSize + theList2.size();
61 theList.resize( aNewSize );
62 for( int i=aSize, j=0; i<aNewSize; i++, j++ )
63 theList[i] = theList2[j];
66 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
67 const Handle( HYDROData_PolylineXY )& thePolyline,
68 const gp_Pnt2d& thePoint,
69 double theTolerance ) const
71 if (thePolyline.IsNull())
76 std::vector<gp_Pnt2d> aPointsList( 1 );
77 aPointsList[0] = thePoint;
78 std::vector<TopoDS_Wire> aCurves;
79 GetWires(thePolyline, aCurves);
81 for( int i=0, n=aCurves.size(); i<n; i++ )
83 std::vector<TopoDS_Shape> aCurvesList;
84 Split(aCurves[i], thePoint, theTolerance, aCurvesList);
85 bool isLocalOK = CreatePolylines( theDoc, thePolyline->GetName(), aCurvesList, true );
86 isOK = isOK && isLocalOK;
91 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
92 const Handle( HYDROData_PolylineXY )& thePolyline,
93 const Handle( HYDROData_PolylineXY )& theTool,
95 bool& theIsIntersected) const
97 if (thePolyline.IsNull() || theTool.IsNull())
102 HYDROData_SequenceOfObjects aSeq;
103 aSeq.Append( theTool );
104 return split( theDoc, thePolyline, aSeq, theTolerance, -1, theIsIntersected);
107 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
108 const HYDROData_SequenceOfObjects& thePolylines,
109 double theTolerance )
111 int f = thePolylines.Lower(), l = thePolylines.Upper();
112 for( int i=f; i<=l; i++ )
114 Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
116 if( !split( theDoc, aPolyline, thePolylines, theTolerance, i, isIntersected) )
122 bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theDoc,
123 const QString& theName,
124 const HYDROData_SequenceOfObjects& thePolylines,
125 bool isConnectByNewSegment,
126 double theTolerance )
128 std::deque<HYDROData_TopoCurve> aMergedCurves;
129 HYDROData_SequenceOfObjects::Iterator aPIt(thePolylines);
130 for (; aPIt.More(); aPIt.Next())
132 Handle(HYDROData_PolylineXY) aPolyline =
133 Handle(HYDROData_PolylineXY)::DownCast(aPIt.Value());
134 std::vector<TopoDS_Wire> aWires;
135 GetWires(aPolyline, aWires);
136 for (std::vector<TopoDS_Wire>::const_iterator aWIt = aWires.begin(),
137 aLastWIt = aWires.end(); aWIt != aLastWIt; ++aWIt)
139 const Standard_Boolean aResult = !isConnectByNewSegment ?
140 HYDROData_TopoCurve::Merge(theTolerance, *aWIt, aMergedCurves) :
141 HYDROData_TopoCurve::Connect(theTolerance, *aWIt, aMergedCurves);
149 TopoDS_Compound aWireSet;
150 BRep_Builder aBuilder;
151 aBuilder.MakeCompound(aWireSet);
152 std::deque<HYDROData_TopoCurve>::iterator aCIt = aMergedCurves.begin();
153 std::deque<HYDROData_TopoCurve>::iterator aLastCIt = aMergedCurves.end();
154 for (; aCIt != aLastCIt; ++aCIt)
156 if (!aCIt->IsEmpty())
158 aBuilder.Add(aWireSet, aCIt->Wire());
162 std::vector<TopoDS_Shape> aPolylines(1);
163 aPolylines[0] = aWireSet;
164 CreatePolylines(theDoc, theName, aPolylines, false);
168 bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theDoc,
169 const Handle( HYDROData_PolylineXY )& thePolyline,
170 const HYDROData_SequenceOfObjects& theTools,
173 bool& theIsIntersected) const
175 theIsIntersected = false;
177 if (thePolyline.IsNull())
182 std::vector<TopoDS_Wire> aCurves;
183 GetWires(thePolyline, aCurves);
184 std::vector<TopoDS_Wire> aToolCurves;
185 for( int i=theTools.Lower(), n=theTools.Upper(); i<=n; i++ )
186 if( i!=theIgnoreIndex )
188 Handle( HYDROData_PolylineXY ) aToolPolyline =
189 Handle( HYDROData_PolylineXY )::DownCast( theTools.Value( i ) );
190 if (!aToolPolyline.IsNull())
192 std::vector<TopoDS_Wire> aTCurves;
193 GetWires(aToolPolyline, aTCurves);
194 append( aToolCurves, aTCurves);
198 if (aToolCurves.empty())
203 const int aPSCount = aCurves.size();
204 const int aTSCount = aToolCurves.size();
205 std::vector<TopoDS_Shape> aResult;
206 for (int aPSI = 0; aPSI < aPSCount; ++aPSI)
208 HYDROData_TopoCurve aCurve;
209 if (!aCurve.Initialize(aCurves[aPSI]))
214 std::deque<std::list<double> > aParams;
215 for (int aTSI = 0; aTSI < aTSCount; ++aTSI)
217 aCurve.Intersect(aToolCurves[aTSI], aParams);
220 std::deque<HYDROData_TopoCurve> aSplittedCurves;
221 theIsIntersected |= aCurve.Cut(aParams, aSplittedCurves);
222 std::deque<HYDROData_TopoCurve>::const_iterator aCIt =
223 aSplittedCurves.begin();
224 std::deque<HYDROData_TopoCurve>::const_iterator aLastCIt =
225 aSplittedCurves.end();
226 for (; aCIt != aLastCIt; ++aCIt)
228 aResult.push_back(aCIt->Wire());
232 CreatePolylines(theDoc, thePolyline->GetName(), aResult, true);
236 void HYDROData_PolylineOperator::GetWires(
237 const Handle( HYDROData_PolylineXY )& thePolyline,
238 std::vector<TopoDS_Wire>& theWires)
240 TopoDS_Shape aShape = thePolyline->GetShape();
241 if( aShape.ShapeType()==TopAbs_WIRE )
243 theWires.push_back( TopoDS::Wire( aShape ) );
247 TopExp_Explorer anExp( aShape, TopAbs_WIRE );
248 for( ; anExp.More(); anExp.Next() )
250 theWires.push_back( TopoDS::Wire( anExp.Current() ) );
255 void HYDROData_PolylineOperator::Split(
256 const TopoDS_Wire& theWire,
257 const gp_Pnt2d& thePoint,
259 std::vector<TopoDS_Shape>& theWires)
261 HYDROData_TopoCurve aCurve;
262 if (!aCurve.Initialize(theWire))
264 theWires.push_back(theWire);
268 const gp_XYZ aP(thePoint.X(), thePoint.Y(), 0);
269 std::list<TopoDS_Edge>::const_iterator aEPos;
271 aCurve.Project(aP, aEPos, aParam);
272 HYDROData_TopoCurve aCurve1, aCurve2;
273 aCurve.Cut(aEPos, aParam, aCurve1, aCurve2);
274 theWires.push_back(aCurve1.Wire());
275 if (!aCurve2.IsEmpty())
277 theWires.push_back(aCurve2.Wire());
281 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
282 const QString& theNamePrefix,
283 const std::vector<TopoDS_Shape>& theShapes,
286 if( theDoc.IsNull() )
289 int n = theShapes.size();
291 for( int i=0; i<n; i++ )
293 Handle( HYDROData_PolylineXY ) aPolyline =
294 Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
295 if( aPolyline.IsNull() )
298 aPolyline->SetShape( theShapes[i] );
302 QString aNewName = theNamePrefix + "_" + QString::number( anIndex );
303 if( theDoc->FindObjectByName( aNewName ).IsNull() ) // the object with such a name is not found
304 aPolyline->SetName( aNewName );
309 aPolyline->SetName( theNamePrefix );
315 double HYDROData_PolylineOperator::ReduceDeflection(
316 const double theDeflection,
317 HYDROData_TopoCurve& theCurve,
320 // Construct the approximating B-spline.
321 std::list<gp_XYZ> aPs;
322 if (!theCurve.ValuesInKnots(aPs))
327 Handle(TColgp_HArray1OfPnt) aPs2 = new TColgp_HArray1OfPnt(1, aPs.size());
329 std::list<gp_XYZ>::const_iterator aLastPIt = aPs.end();
330 std::list<gp_XYZ>::const_iterator aPIt = aPs.begin();
331 for (int aPN = 1; aPIt != aLastPIt; ++aPN, ++aPIt)
333 aPs2->SetValue(aPN, *aPIt);
336 Handle(Geom_BSplineCurve) aBSpline2;
337 const bool isClosed = theCurve.IsClosed();
339 if (!CurveCreator_Utils::constructBSpline(aPs2, isClosed, aBSpline2))
345 // Calculate the piece deflections.
346 std::deque<double> aSqDefls;
347 double aMaxSqDefl = 0;
348 std::list<TopoDS_Edge>& aEdges = theCurve.Edges();
349 std::list<TopoDS_Edge>::const_iterator aLastEIt = aEdges.end();
351 std::list<TopoDS_Edge>::const_iterator aEIt = aEdges.begin();
352 for (int aPrevKCount = 0; aEIt != aLastEIt; ++aEIt)
354 TopLoc_Location aLoc;
356 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
357 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
358 const int aKCount = aBSpline->NbKnots();
359 for (int aKN = 1; aKN < aKCount; ++aKN)
361 const double aParam =
362 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5;
363 const double aParam2 = (aBSpline2->Knot(aPrevKCount + aKN) +
364 aBSpline2->Knot(aPrevKCount + aKN + 1)) * 0.5;
365 const double aSqDefl = Abs(aBSpline->Value(aParam).
366 SquareDistance(aBSpline2->Value(aParam2)));
367 aSqDefls.push_back(aSqDefl);
368 if (aMaxSqDefl < aSqDefl)
370 aMaxSqDefl = aSqDefl;
373 aPrevKCount += aKCount - 1;
377 // Check whether the reducing is necessary.
378 const double aMaxDefl = Sqrt(aMaxSqDefl);
379 if (aMaxDefl <= theDeflection)
384 // Reduce the deflections.
385 const double aThresSqDefl =
386 Max(aMaxSqDefl * 0.25, theDeflection * theDeflection);
387 std::list<TopoDS_Edge>::iterator aEIt = aEdges.begin();
388 std::deque<double>::const_iterator aSqDIt = aSqDefls.begin();
390 for (; aEIt != aLastEIt; ++aEIt)
392 TopLoc_Location aLoc;
394 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
395 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
396 Handle(Geom_BSplineCurve) aBSpline2 =
397 Handle(Geom_BSplineCurve)::DownCast(aBSpline->Copy());
398 const int aKCount = aBSpline->NbKnots();
399 for (int aKN = 1; aKN < aKCount; ++aSqDIt, ++aKN)
401 if (*aSqDIt > aThresSqDefl)
403 aBSpline2->InsertKnot(
404 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5);
408 BRep_Builder().MakeEdge(aEdge, aBSpline2, Precision::Confusion());
409 BRep_Builder().Add(aEdge, TopExp::FirstVertex(*aEIt));
410 BRep_Builder().Add(aEdge, TopExp::LastVertex(*aEIt));
411 thePieceCount += aBSpline2->NbKnots() - 1;
417 bool HYDROData_PolylineOperator::Extract( const Handle(HYDROData_Document)& theDocument,
418 const Handle(HYDROData_Object)& theObject )
420 if( theObject.IsNull() || theDocument.IsNull() )
423 QList<TopoDS_Shape> aBoundShapes;
424 QStringList aBoundNames;
426 theObject->GetBoundaries( aBoundShapes, aBoundNames );
428 for( int i=0, n=aBoundShapes.size(); i<n; i++ )
430 TopoDS_Shape aShape = aBoundShapes[i];
431 if( aShape.IsNull() )
434 QString aBoundName = i<aBoundNames.size() ? aBoundNames[i] : "";
436 Handle( HYDROData_PolylineXY ) aPolyline =
437 Handle( HYDROData_PolylineXY )::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
439 if( aPolyline.IsNull() )
442 aPolyline->SetShape( aShape );
445 QString aName = aBoundName;
446 while( !theDocument->FindObjectByName( aName ).IsNull() )
449 aName = aBoundName + "_" + QString::number( anIndex );
451 aPolyline->SetName( aName );