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>
52 #include <Geom_BSplineCurve.hxx>
54 template<class T> void append( std::vector<T>& theList, const std::vector<T>& theList2 )
56 int aSize = theList.size();
57 int aNewSize = aSize + theList2.size();
62 theList.resize( aNewSize );
63 for( int i=aSize, j=0; i<aNewSize; i++, j++ )
64 theList[i] = theList2[j];
67 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
68 const Handle( HYDROData_PolylineXY )& thePolyline,
69 const gp_Pnt2d& thePoint,
70 double theTolerance ) const
72 if (thePolyline.IsNull())
77 std::vector<gp_Pnt2d> aPointsList( 1 );
78 aPointsList[0] = thePoint;
79 std::vector<TopoDS_Wire> aCurves;
80 GetWires(thePolyline, aCurves);
82 for( int i=0, n=aCurves.size(); i<n; i++ )
84 std::vector<TopoDS_Shape> aCurvesList;
85 Split( aCurves[i], thePoint, theTolerance, aCurvesList );
86 bool isLocalOK = CreatePolylines( theDoc, thePolyline->GetName(),
87 aCurvesList, true, thePolyline->GetWireColor() );
88 isOK = isOK && isLocalOK;
93 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
94 const Handle( HYDROData_PolylineXY )& thePolyline,
95 const Handle( HYDROData_PolylineXY )& theTool,
97 bool& theIsIntersected) const
99 if (thePolyline.IsNull() || theTool.IsNull())
104 HYDROData_SequenceOfObjects aSeq;
105 aSeq.Append( theTool );
106 return split( theDoc, thePolyline, aSeq, theTolerance, -1, theIsIntersected);
109 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
110 const HYDROData_SequenceOfObjects& thePolylines,
111 double theTolerance )
113 int f = thePolylines.Lower(), l = thePolylines.Upper();
114 for( int i=f; i<=l; i++ )
116 Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
118 if( !split( theDoc, aPolyline, thePolylines, theTolerance, i, isIntersected) )
124 bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theDoc,
125 const QString& theName,
126 const HYDROData_SequenceOfObjects& thePolylines,
127 bool isConnectByNewSegment,
128 double theTolerance )
130 std::deque<HYDROData_TopoCurve> aMergedCurves;
131 HYDROData_SequenceOfObjects::Iterator aPIt(thePolylines);
132 for (; aPIt.More(); aPIt.Next())
134 Handle(HYDROData_PolylineXY) aPolyline =
135 Handle(HYDROData_PolylineXY)::DownCast(aPIt.Value());
136 std::vector<TopoDS_Wire> aWires;
137 GetWires(aPolyline, aWires);
138 for (std::vector<TopoDS_Wire>::const_iterator aWIt = aWires.begin(),
139 aLastWIt = aWires.end(); aWIt != aLastWIt; ++aWIt)
141 const Standard_Boolean aResult = !isConnectByNewSegment ?
142 HYDROData_TopoCurve::Merge(theTolerance, *aWIt, aMergedCurves) :
143 HYDROData_TopoCurve::Connect(theTolerance, *aWIt, aMergedCurves);
151 TopoDS_Compound aWireSet;
152 BRep_Builder aBuilder;
153 aBuilder.MakeCompound(aWireSet);
154 std::deque<HYDROData_TopoCurve>::iterator aCIt = aMergedCurves.begin();
155 std::deque<HYDROData_TopoCurve>::iterator aLastCIt = aMergedCurves.end();
156 for (; aCIt != aLastCIt; ++aCIt)
158 if (!aCIt->IsEmpty())
160 aBuilder.Add(aWireSet, aCIt->Wire());
164 std::vector<TopoDS_Shape> aPolylines(1);
165 aPolylines[0] = aWireSet;
166 QString aName = theName;
167 if( aName.isEmpty() )
169 CreatePolylines( theDoc, aName, aPolylines, true, QColor() );
173 bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theDoc,
174 const Handle( HYDROData_PolylineXY )& thePolyline,
175 const HYDROData_SequenceOfObjects& theTools,
178 bool& theIsIntersected) const
180 theIsIntersected = false;
182 if (thePolyline.IsNull())
187 std::vector<TopoDS_Wire> aCurves;
188 GetWires(thePolyline, aCurves);
189 std::vector<TopoDS_Wire> aToolCurves;
190 for( int i=theTools.Lower(), n=theTools.Upper(); i<=n; i++ )
191 if( i!=theIgnoreIndex )
193 Handle( HYDROData_PolylineXY ) aToolPolyline =
194 Handle( HYDROData_PolylineXY )::DownCast( theTools.Value( i ) );
195 if (!aToolPolyline.IsNull())
197 std::vector<TopoDS_Wire> aTCurves;
198 GetWires(aToolPolyline, aTCurves);
199 append( aToolCurves, aTCurves);
203 if (aToolCurves.empty())
208 const int aPSCount = aCurves.size();
209 const int aTSCount = aToolCurves.size();
210 std::vector<TopoDS_Shape> aResult;
211 for (int aPSI = 0; aPSI < aPSCount; ++aPSI)
213 HYDROData_TopoCurve aCurve;
214 if (!aCurve.Initialize(aCurves[aPSI]))
219 std::deque<std::list<double> > aParams;
220 for (int aTSI = 0; aTSI < aTSCount; ++aTSI)
222 aCurve.Intersect(aToolCurves[aTSI], aParams);
225 std::deque<HYDROData_TopoCurve> aSplitCurves;
226 theIsIntersected |= aCurve.Cut(aParams, aSplitCurves);
227 std::deque<HYDROData_TopoCurve>::const_iterator aCIt =
228 aSplitCurves.begin();
229 std::deque<HYDROData_TopoCurve>::const_iterator aLastCIt =
231 for (; aCIt != aLastCIt; ++aCIt)
233 aResult.push_back(aCIt->Wire());
237 CreatePolylines( theDoc, thePolyline->GetName(), aResult, true, thePolyline->GetWireColor() );
241 void HYDROData_PolylineOperator::GetWires(
242 const Handle( HYDROData_PolylineXY )& thePolyline,
243 std::vector<TopoDS_Wire>& theWires)
245 TopoDS_Shape aShape = thePolyline->GetShape();
246 if( aShape.ShapeType()==TopAbs_WIRE )
248 theWires.push_back( TopoDS::Wire( aShape ) );
252 TopExp_Explorer anExp( aShape, TopAbs_WIRE );
253 for( ; anExp.More(); anExp.Next() )
255 theWires.push_back( TopoDS::Wire( anExp.Current() ) );
260 void HYDROData_PolylineOperator::Split(
261 const TopoDS_Wire& theWire,
262 const gp_Pnt2d& thePoint,
264 std::vector<TopoDS_Shape>& theWires)
266 HYDROData_TopoCurve aCurve;
267 if (!aCurve.Initialize(theWire))
269 theWires.push_back(theWire);
273 const gp_XYZ aP(thePoint.X(), thePoint.Y(), 0);
274 std::list<TopoDS_Edge>::const_iterator aEPos;
276 aCurve.Project(aP, aEPos, aParam);
277 HYDROData_TopoCurve aCurve1, aCurve2;
278 aCurve.Cut(aEPos, aParam, aCurve1, aCurve2);
279 theWires.push_back(aCurve1.Wire());
280 if (!aCurve2.IsEmpty())
282 theWires.push_back(aCurve2.Wire());
286 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
287 const QString& theNamePrefix,
288 const std::vector<TopoDS_Shape>& theShapes,
290 const QColor& theColor )
292 if( theDoc.IsNull() )
295 int n = theShapes.size();
297 for( int i=0; i<n; i++ )
299 Handle( HYDROData_PolylineXY ) aPolyline =
300 Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
301 if( aPolyline.IsNull() )
304 aPolyline->SetShape( theShapes[i] );
308 QString aNewName = theNamePrefix + "_" + QString::number( anIndex );
309 while( !theDoc->FindObjectByName( aNewName ).IsNull() ) // the object with such a name is not found
312 aNewName = theNamePrefix + "_" + QString::number( anIndex );
314 aPolyline->SetName( aNewName );
318 aPolyline->SetName( theNamePrefix );
321 if( theColor.isValid() )
322 aPolyline->SetWireColor( theColor );
327 double HYDROData_PolylineOperator::ReduceDeflection(
328 const double theDeflection,
329 HYDROData_TopoCurve& theCurve,
332 // Construct the approximating B-spline.
333 std::list<gp_XYZ> aPs;
334 if (!theCurve.ValuesInKnots(aPs))
339 Handle(TColgp_HArray1OfPnt) aPs2 = new TColgp_HArray1OfPnt(1, aPs.size());
341 std::list<gp_XYZ>::const_iterator aLastPIt = aPs.end();
342 std::list<gp_XYZ>::const_iterator aPIt = aPs.begin();
343 for (int aPN = 1; aPIt != aLastPIt; ++aPN, ++aPIt)
345 aPs2->SetValue(aPN, *aPIt);
348 Handle(Geom_BSplineCurve) aBSpline2;
349 const bool isClosed = theCurve.IsClosed();
351 if (!CurveCreator_Utils::constructBSpline(aPs2, isClosed, aBSpline2))
357 // Calculate the piece deflections.
358 std::deque<double> aSqDefls;
359 double aMaxSqDefl = 0;
360 std::list<TopoDS_Edge>& aEdges = theCurve.Edges();
361 std::list<TopoDS_Edge>::const_iterator aLastEIt = aEdges.end();
363 std::list<TopoDS_Edge>::const_iterator aEIt = aEdges.begin();
364 for (int aPrevKCount = 0; aEIt != aLastEIt; ++aEIt)
366 TopLoc_Location aLoc;
368 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
369 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
370 const int aKCount = aBSpline->NbKnots();
371 for (int aKN = 1; aKN < aKCount; ++aKN)
373 const double aParam =
374 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5;
375 const double aParam2 = (aBSpline2->Knot(aPrevKCount + aKN) +
376 aBSpline2->Knot(aPrevKCount + aKN + 1)) * 0.5;
377 const double aSqDefl = Abs(aBSpline->Value(aParam).
378 SquareDistance(aBSpline2->Value(aParam2)));
379 aSqDefls.push_back(aSqDefl);
380 if (aMaxSqDefl < aSqDefl)
382 aMaxSqDefl = aSqDefl;
385 aPrevKCount += aKCount - 1;
389 // Check whether the reducing is necessary.
390 const double aMaxDefl = Sqrt(aMaxSqDefl);
391 if (aMaxDefl <= theDeflection)
396 // Reduce the deflections.
397 const double aThresSqDefl =
398 Max(aMaxSqDefl * 0.25, theDeflection * theDeflection);
399 std::list<TopoDS_Edge>::iterator aEIt = aEdges.begin();
400 std::deque<double>::const_iterator aSqDIt = aSqDefls.begin();
402 for (; aEIt != aLastEIt; ++aEIt)
404 TopLoc_Location aLoc;
406 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
407 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
408 Handle(Geom_BSplineCurve) aBSpline2 =
409 Handle(Geom_BSplineCurve)::DownCast(aBSpline->Copy());
410 const int aKCount = aBSpline->NbKnots();
411 for (int aKN = 1; aKN < aKCount; ++aSqDIt, ++aKN)
413 if (*aSqDIt > aThresSqDefl)
415 aBSpline2->InsertKnot(
416 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5);
420 BRep_Builder().MakeEdge(aEdge, aBSpline2, Precision::Confusion());
421 BRep_Builder().Add(aEdge, TopExp::FirstVertex(*aEIt));
422 BRep_Builder().Add(aEdge, TopExp::LastVertex(*aEIt));
423 thePieceCount += aBSpline2->NbKnots() - 1;
429 bool HYDROData_PolylineOperator::Extract( const Handle(HYDROData_Document)& theDocument,
430 const Handle(HYDROData_Object)& theObject )
432 if( theObject.IsNull() || theDocument.IsNull() )
435 QList<TopoDS_Shape> aBoundShapes;
436 QStringList aBoundNames;
438 theObject->GetBoundaries( aBoundShapes, aBoundNames );
440 for( int i=0, n=aBoundShapes.size(); i<n; i++ )
442 TopoDS_Shape aShape = aBoundShapes[i];
443 if( aShape.IsNull() )
446 QString aBoundName = i<aBoundNames.size() ? aBoundNames[i] : "";
448 Handle( HYDROData_PolylineXY ) aPolyline =
449 Handle( HYDROData_PolylineXY )::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
451 if( aPolyline.IsNull() )
454 aPolyline->SetShape( aShape );
457 QString aName = aBoundName;
458 while( !theDocument->FindObjectByName( aName ).IsNull() )
461 aName = aBoundName + "_" + QString::number( anIndex );
463 aPolyline->SetName( aName );