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 CreatePolylines( theDoc, theName, aPolylines, false, QColor() );
170 bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theDoc,
171 const Handle( HYDROData_PolylineXY )& thePolyline,
172 const HYDROData_SequenceOfObjects& theTools,
175 bool& theIsIntersected) const
177 theIsIntersected = false;
179 if (thePolyline.IsNull())
184 std::vector<TopoDS_Wire> aCurves;
185 GetWires(thePolyline, aCurves);
186 std::vector<TopoDS_Wire> aToolCurves;
187 for( int i=theTools.Lower(), n=theTools.Upper(); i<=n; i++ )
188 if( i!=theIgnoreIndex )
190 Handle( HYDROData_PolylineXY ) aToolPolyline =
191 Handle( HYDROData_PolylineXY )::DownCast( theTools.Value( i ) );
192 if (!aToolPolyline.IsNull())
194 std::vector<TopoDS_Wire> aTCurves;
195 GetWires(aToolPolyline, aTCurves);
196 append( aToolCurves, aTCurves);
200 if (aToolCurves.empty())
205 const int aPSCount = aCurves.size();
206 const int aTSCount = aToolCurves.size();
207 std::vector<TopoDS_Shape> aResult;
208 for (int aPSI = 0; aPSI < aPSCount; ++aPSI)
210 HYDROData_TopoCurve aCurve;
211 if (!aCurve.Initialize(aCurves[aPSI]))
216 std::deque<std::list<double> > aParams;
217 for (int aTSI = 0; aTSI < aTSCount; ++aTSI)
219 aCurve.Intersect(aToolCurves[aTSI], aParams);
222 std::deque<HYDROData_TopoCurve> aSplittedCurves;
223 theIsIntersected |= aCurve.Cut(aParams, aSplittedCurves);
224 std::deque<HYDROData_TopoCurve>::const_iterator aCIt =
225 aSplittedCurves.begin();
226 std::deque<HYDROData_TopoCurve>::const_iterator aLastCIt =
227 aSplittedCurves.end();
228 for (; aCIt != aLastCIt; ++aCIt)
230 aResult.push_back(aCIt->Wire());
234 CreatePolylines( theDoc, thePolyline->GetName(), aResult, true, thePolyline->GetWireColor() );
238 void HYDROData_PolylineOperator::GetWires(
239 const Handle( HYDROData_PolylineXY )& thePolyline,
240 std::vector<TopoDS_Wire>& theWires)
242 TopoDS_Shape aShape = thePolyline->GetShape();
243 if( aShape.ShapeType()==TopAbs_WIRE )
245 theWires.push_back( TopoDS::Wire( aShape ) );
249 TopExp_Explorer anExp( aShape, TopAbs_WIRE );
250 for( ; anExp.More(); anExp.Next() )
252 theWires.push_back( TopoDS::Wire( anExp.Current() ) );
257 void HYDROData_PolylineOperator::Split(
258 const TopoDS_Wire& theWire,
259 const gp_Pnt2d& thePoint,
261 std::vector<TopoDS_Shape>& theWires)
263 HYDROData_TopoCurve aCurve;
264 if (!aCurve.Initialize(theWire))
266 theWires.push_back(theWire);
270 const gp_XYZ aP(thePoint.X(), thePoint.Y(), 0);
271 std::list<TopoDS_Edge>::const_iterator aEPos;
273 aCurve.Project(aP, aEPos, aParam);
274 HYDROData_TopoCurve aCurve1, aCurve2;
275 aCurve.Cut(aEPos, aParam, aCurve1, aCurve2);
276 theWires.push_back(aCurve1.Wire());
277 if (!aCurve2.IsEmpty())
279 theWires.push_back(aCurve2.Wire());
283 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
284 const QString& theNamePrefix,
285 const std::vector<TopoDS_Shape>& theShapes,
287 const QColor& theColor )
289 if( theDoc.IsNull() )
292 int n = theShapes.size();
294 for( int i=0; i<n; i++ )
296 Handle( HYDROData_PolylineXY ) aPolyline =
297 Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
298 if( aPolyline.IsNull() )
301 aPolyline->SetShape( theShapes[i] );
305 QString aNewName = theNamePrefix + "_" + QString::number( anIndex );
306 if( theDoc->FindObjectByName( aNewName ).IsNull() ) // the object with such a name is not found
307 aPolyline->SetName( aNewName );
312 aPolyline->SetName( theNamePrefix );
315 if( theColor.isValid() )
316 aPolyline->SetWireColor( theColor );
321 double HYDROData_PolylineOperator::ReduceDeflection(
322 const double theDeflection,
323 HYDROData_TopoCurve& theCurve,
326 // Construct the approximating B-spline.
327 std::list<gp_XYZ> aPs;
328 if (!theCurve.ValuesInKnots(aPs))
333 Handle(TColgp_HArray1OfPnt) aPs2 = new TColgp_HArray1OfPnt(1, aPs.size());
335 std::list<gp_XYZ>::const_iterator aLastPIt = aPs.end();
336 std::list<gp_XYZ>::const_iterator aPIt = aPs.begin();
337 for (int aPN = 1; aPIt != aLastPIt; ++aPN, ++aPIt)
339 aPs2->SetValue(aPN, *aPIt);
342 Handle(Geom_BSplineCurve) aBSpline2;
343 const bool isClosed = theCurve.IsClosed();
345 if (!CurveCreator_Utils::constructBSpline(aPs2, isClosed, aBSpline2))
351 // Calculate the piece deflections.
352 std::deque<double> aSqDefls;
353 double aMaxSqDefl = 0;
354 std::list<TopoDS_Edge>& aEdges = theCurve.Edges();
355 std::list<TopoDS_Edge>::const_iterator aLastEIt = aEdges.end();
357 std::list<TopoDS_Edge>::const_iterator aEIt = aEdges.begin();
358 for (int aPrevKCount = 0; aEIt != aLastEIt; ++aEIt)
360 TopLoc_Location aLoc;
362 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
363 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
364 const int aKCount = aBSpline->NbKnots();
365 for (int aKN = 1; aKN < aKCount; ++aKN)
367 const double aParam =
368 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5;
369 const double aParam2 = (aBSpline2->Knot(aPrevKCount + aKN) +
370 aBSpline2->Knot(aPrevKCount + aKN + 1)) * 0.5;
371 const double aSqDefl = Abs(aBSpline->Value(aParam).
372 SquareDistance(aBSpline2->Value(aParam2)));
373 aSqDefls.push_back(aSqDefl);
374 if (aMaxSqDefl < aSqDefl)
376 aMaxSqDefl = aSqDefl;
379 aPrevKCount += aKCount - 1;
383 // Check whether the reducing is necessary.
384 const double aMaxDefl = Sqrt(aMaxSqDefl);
385 if (aMaxDefl <= theDeflection)
390 // Reduce the deflections.
391 const double aThresSqDefl =
392 Max(aMaxSqDefl * 0.25, theDeflection * theDeflection);
393 std::list<TopoDS_Edge>::iterator aEIt = aEdges.begin();
394 std::deque<double>::const_iterator aSqDIt = aSqDefls.begin();
396 for (; aEIt != aLastEIt; ++aEIt)
398 TopLoc_Location aLoc;
400 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
401 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
402 Handle(Geom_BSplineCurve) aBSpline2 =
403 Handle(Geom_BSplineCurve)::DownCast(aBSpline->Copy());
404 const int aKCount = aBSpline->NbKnots();
405 for (int aKN = 1; aKN < aKCount; ++aSqDIt, ++aKN)
407 if (*aSqDIt > aThresSqDefl)
409 aBSpline2->InsertKnot(
410 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5);
414 BRep_Builder().MakeEdge(aEdge, aBSpline2, Precision::Confusion());
415 BRep_Builder().Add(aEdge, TopExp::FirstVertex(*aEIt));
416 BRep_Builder().Add(aEdge, TopExp::LastVertex(*aEIt));
417 thePieceCount += aBSpline2->NbKnots() - 1;
423 bool HYDROData_PolylineOperator::Extract( const Handle(HYDROData_Document)& theDocument,
424 const Handle(HYDROData_Object)& theObject )
426 if( theObject.IsNull() || theDocument.IsNull() )
429 QList<TopoDS_Shape> aBoundShapes;
430 QStringList aBoundNames;
432 theObject->GetBoundaries( aBoundShapes, aBoundNames );
434 for( int i=0, n=aBoundShapes.size(); i<n; i++ )
436 TopoDS_Shape aShape = aBoundShapes[i];
437 if( aShape.IsNull() )
440 QString aBoundName = i<aBoundNames.size() ? aBoundNames[i] : "";
442 Handle( HYDROData_PolylineXY ) aPolyline =
443 Handle( HYDROData_PolylineXY )::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
445 if( aPolyline.IsNull() )
448 aPolyline->SetShape( aShape );
451 QString aName = aBoundName;
452 while( !theDocument->FindObjectByName( aName ).IsNull() )
455 aName = aBoundName + "_" + QString::number( anIndex );
457 aPolyline->SetName( aName );