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> aSplittedCurves;
226 theIsIntersected |= aCurve.Cut(aParams, aSplittedCurves);
227 std::deque<HYDROData_TopoCurve>::const_iterator aCIt =
228 aSplittedCurves.begin();
229 std::deque<HYDROData_TopoCurve>::const_iterator aLastCIt =
230 aSplittedCurves.end();
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 if( theDoc->FindObjectByName( aNewName ).IsNull() ) // the object with such a name is not found
310 aPolyline->SetName( aNewName );
315 aPolyline->SetName( theNamePrefix );
318 if( theColor.isValid() )
319 aPolyline->SetWireColor( theColor );
324 double HYDROData_PolylineOperator::ReduceDeflection(
325 const double theDeflection,
326 HYDROData_TopoCurve& theCurve,
329 // Construct the approximating B-spline.
330 std::list<gp_XYZ> aPs;
331 if (!theCurve.ValuesInKnots(aPs))
336 Handle(TColgp_HArray1OfPnt) aPs2 = new TColgp_HArray1OfPnt(1, aPs.size());
338 std::list<gp_XYZ>::const_iterator aLastPIt = aPs.end();
339 std::list<gp_XYZ>::const_iterator aPIt = aPs.begin();
340 for (int aPN = 1; aPIt != aLastPIt; ++aPN, ++aPIt)
342 aPs2->SetValue(aPN, *aPIt);
345 Handle(Geom_BSplineCurve) aBSpline2;
346 const bool isClosed = theCurve.IsClosed();
348 if (!CurveCreator_Utils::constructBSpline(aPs2, isClosed, aBSpline2))
354 // Calculate the piece deflections.
355 std::deque<double> aSqDefls;
356 double aMaxSqDefl = 0;
357 std::list<TopoDS_Edge>& aEdges = theCurve.Edges();
358 std::list<TopoDS_Edge>::const_iterator aLastEIt = aEdges.end();
360 std::list<TopoDS_Edge>::const_iterator aEIt = aEdges.begin();
361 for (int aPrevKCount = 0; aEIt != aLastEIt; ++aEIt)
363 TopLoc_Location aLoc;
365 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
366 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
367 const int aKCount = aBSpline->NbKnots();
368 for (int aKN = 1; aKN < aKCount; ++aKN)
370 const double aParam =
371 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5;
372 const double aParam2 = (aBSpline2->Knot(aPrevKCount + aKN) +
373 aBSpline2->Knot(aPrevKCount + aKN + 1)) * 0.5;
374 const double aSqDefl = Abs(aBSpline->Value(aParam).
375 SquareDistance(aBSpline2->Value(aParam2)));
376 aSqDefls.push_back(aSqDefl);
377 if (aMaxSqDefl < aSqDefl)
379 aMaxSqDefl = aSqDefl;
382 aPrevKCount += aKCount - 1;
386 // Check whether the reducing is necessary.
387 const double aMaxDefl = Sqrt(aMaxSqDefl);
388 if (aMaxDefl <= theDeflection)
393 // Reduce the deflections.
394 const double aThresSqDefl =
395 Max(aMaxSqDefl * 0.25, theDeflection * theDeflection);
396 std::list<TopoDS_Edge>::iterator aEIt = aEdges.begin();
397 std::deque<double>::const_iterator aSqDIt = aSqDefls.begin();
399 for (; aEIt != aLastEIt; ++aEIt)
401 TopLoc_Location aLoc;
403 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
404 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
405 Handle(Geom_BSplineCurve) aBSpline2 =
406 Handle(Geom_BSplineCurve)::DownCast(aBSpline->Copy());
407 const int aKCount = aBSpline->NbKnots();
408 for (int aKN = 1; aKN < aKCount; ++aSqDIt, ++aKN)
410 if (*aSqDIt > aThresSqDefl)
412 aBSpline2->InsertKnot(
413 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5);
417 BRep_Builder().MakeEdge(aEdge, aBSpline2, Precision::Confusion());
418 BRep_Builder().Add(aEdge, TopExp::FirstVertex(*aEIt));
419 BRep_Builder().Add(aEdge, TopExp::LastVertex(*aEIt));
420 thePieceCount += aBSpline2->NbKnots() - 1;
426 bool HYDROData_PolylineOperator::Extract( const Handle(HYDROData_Document)& theDocument,
427 const Handle(HYDROData_Object)& theObject )
429 if( theObject.IsNull() || theDocument.IsNull() )
432 QList<TopoDS_Shape> aBoundShapes;
433 QStringList aBoundNames;
435 theObject->GetBoundaries( aBoundShapes, aBoundNames );
437 for( int i=0, n=aBoundShapes.size(); i<n; i++ )
439 TopoDS_Shape aShape = aBoundShapes[i];
440 if( aShape.IsNull() )
443 QString aBoundName = i<aBoundNames.size() ? aBoundNames[i] : "";
445 Handle( HYDROData_PolylineXY ) aPolyline =
446 Handle( HYDROData_PolylineXY )::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
448 if( aPolyline.IsNull() )
451 aPolyline->SetShape( aShape );
454 QString aName = aBoundName;
455 while( !theDocument->FindObjectByName( aName ).IsNull() )
458 aName = aBoundName + "_" + QString::number( anIndex );
460 aPolyline->SetName( aName );