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>
55 #include "HYDRO_trace.hxx"
56 #include <BRepTools.hxx>
59 template<class T> void append( std::vector<T>& theList, const std::vector<T>& theList2 )
61 int aSize = theList.size();
62 int aNewSize = aSize + theList2.size();
67 theList.resize( aNewSize );
68 for( int i=aSize, j=0; i<aNewSize; i++, j++ )
69 theList[i] = theList2[j];
72 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
73 const Handle( HYDROData_PolylineXY )& thePolyline,
74 const gp_Pnt2d& thePoint,
75 double theTolerance ) const
77 if (thePolyline.IsNull())
82 std::vector<gp_Pnt2d> aPointsList( 1 );
83 aPointsList[0] = thePoint;
84 std::vector<TopoDS_Wire> aCurves;
85 GetWires(thePolyline, aCurves);
87 for( int i=0, n=aCurves.size(); i<n; i++ )
89 std::vector<TopoDS_Shape> aCurvesList;
90 Split( aCurves[i], thePoint, theTolerance, aCurvesList );
91 bool isLocalOK = CreatePolylines( theDoc, thePolyline->GetName(),
92 aCurvesList, true, thePolyline->GetWireColor() );
93 isOK = isOK && isLocalOK;
98 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
99 const Handle( HYDROData_PolylineXY )& thePolyline,
100 const Handle( HYDROData_PolylineXY )& theTool,
102 bool& theIsIntersected) const
104 if (thePolyline.IsNull() || theTool.IsNull())
109 HYDROData_SequenceOfObjects aSeq;
110 aSeq.Append( theTool );
111 return split( theDoc, thePolyline, aSeq, theTolerance, -1, theIsIntersected);
114 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
115 const HYDROData_SequenceOfObjects& thePolylines,
116 double theTolerance )
118 int f = thePolylines.Lower(), l = thePolylines.Upper();
119 for( int i=f; i<=l; i++ )
121 Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
123 if( !split( theDoc, aPolyline, thePolylines, theTolerance, i, isIntersected) )
129 bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theDoc,
130 const QString& theName,
131 const HYDROData_SequenceOfObjects& thePolylines,
132 bool isConnectByNewSegment,
133 double theTolerance )
135 std::deque<HYDROData_TopoCurve> aMergedCurves;
136 HYDROData_SequenceOfObjects::Iterator aPIt(thePolylines);
137 for (; aPIt.More(); aPIt.Next())
139 Handle(HYDROData_PolylineXY) aPolyline =
140 Handle(HYDROData_PolylineXY)::DownCast(aPIt.Value());
141 std::vector<TopoDS_Wire> aWires;
142 GetWires(aPolyline, aWires);
143 for (std::vector<TopoDS_Wire>::const_iterator aWIt = aWires.begin(),
144 aLastWIt = aWires.end(); aWIt != aLastWIt; ++aWIt)
146 const Standard_Boolean aResult = !isConnectByNewSegment ?
147 HYDROData_TopoCurve::Merge(theTolerance, *aWIt, aMergedCurves) :
148 HYDROData_TopoCurve::Connect(theTolerance, *aWIt, aMergedCurves);
156 TopoDS_Compound aWireSet;
157 BRep_Builder aBuilder;
158 aBuilder.MakeCompound(aWireSet);
159 std::deque<HYDROData_TopoCurve>::iterator aCIt = aMergedCurves.begin();
160 std::deque<HYDROData_TopoCurve>::iterator aLastCIt = aMergedCurves.end();
161 for (; aCIt != aLastCIt; ++aCIt)
163 if (!aCIt->IsEmpty())
165 aBuilder.Add(aWireSet, aCIt->Wire());
169 std::vector<TopoDS_Shape> aPolylines(1);
170 aPolylines[0] = aWireSet;
171 QString aName = theName;
172 if( aName.isEmpty() )
174 CreatePolylines( theDoc, aName, aPolylines, true, QColor() );
178 bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theDoc,
179 const Handle( HYDROData_PolylineXY )& thePolyline,
180 const HYDROData_SequenceOfObjects& theTools,
183 bool& theIsIntersected) const
185 theIsIntersected = false;
187 if (thePolyline.IsNull())
192 std::vector<TopoDS_Wire> aCurves;
193 GetWires(thePolyline, aCurves);
194 std::vector<TopoDS_Wire> aToolCurves;
195 for( int i=theTools.Lower(), n=theTools.Upper(); i<=n; i++ )
196 if( i!=theIgnoreIndex )
198 Handle( HYDROData_PolylineXY ) aToolPolyline =
199 Handle( HYDROData_PolylineXY )::DownCast( theTools.Value( i ) );
200 if (!aToolPolyline.IsNull())
202 std::vector<TopoDS_Wire> aTCurves;
203 GetWires(aToolPolyline, aTCurves);
204 append( aToolCurves, aTCurves);
208 if (aToolCurves.empty())
213 const int aPSCount = aCurves.size();
214 const int aTSCount = aToolCurves.size();
215 std::vector<TopoDS_Shape> aResult;
216 for (int aPSI = 0; aPSI < aPSCount; ++aPSI)
218 HYDROData_TopoCurve aCurve;
219 DEBTRACE("Initialize curve " << aPSI);
220 if (!aCurve.Initialize(aCurves[aPSI]))
225 std::deque<std::list<double> > aParams;
226 for (int aTSI = 0; aTSI < aTSCount; ++aTSI)
228 aCurve.Intersect(aToolCurves[aTSI], aParams);
231 std::deque<HYDROData_TopoCurve> aSplitCurves;
232 theIsIntersected |= aCurve.Cut(aParams, aSplitCurves);
233 std::deque<HYDROData_TopoCurve>::const_iterator aCIt =
234 aSplitCurves.begin();
235 std::deque<HYDROData_TopoCurve>::const_iterator aLastCIt =
237 for (int iw=0; aCIt != aLastCIt; ++aCIt, iw++)
239 std::stringstream brepName;
240 brepName << "theSplitWire_";
243 BRepTools::Write(aCIt->Wire() , brepName.str().c_str() );
244 aResult.push_back(aCIt->Wire());
248 CreatePolylines( theDoc, thePolyline->GetName(), aResult, true, thePolyline->GetWireColor() );
249 //CreatePolylinesSplit( theDoc, thePolyline, aResult, true );
253 void HYDROData_PolylineOperator::GetWires(
254 const Handle( HYDROData_PolylineXY )& thePolyline,
255 std::vector<TopoDS_Wire>& theWires)
257 TopoDS_Shape aShape = thePolyline->GetShape();
258 if( aShape.ShapeType()==TopAbs_WIRE )
260 theWires.push_back( TopoDS::Wire( aShape ) );
264 TopExp_Explorer anExp( aShape, TopAbs_WIRE );
265 for( ; anExp.More(); anExp.Next() )
267 theWires.push_back( TopoDS::Wire( anExp.Current() ) );
272 void HYDROData_PolylineOperator::Split(
273 const TopoDS_Wire& theWire,
274 const gp_Pnt2d& thePoint,
276 std::vector<TopoDS_Shape>& theWires)
278 HYDROData_TopoCurve aCurve;
279 if (!aCurve.Initialize(theWire))
281 theWires.push_back(theWire);
285 const gp_XYZ aP(thePoint.X(), thePoint.Y(), 0);
286 std::list<TopoDS_Edge>::const_iterator aEPos;
288 aCurve.Project(aP, aEPos, aParam);
289 HYDROData_TopoCurve aCurve1, aCurve2;
290 aCurve.Cut(aEPos, aParam, aCurve1, aCurve2);
291 theWires.push_back(aCurve1.Wire());
292 if (!aCurve2.IsEmpty())
294 theWires.push_back(aCurve2.Wire());
298 //bool HYDROData_PolylineOperator::CreatePolylinesSplit(const Handle( HYDROData_Document )& theDoc,
299 // const Handle( HYDROData_PolylineXY )& thePolyline,
300 // const std::vector<TopoDS_Shape>& theShapes, bool isUseIndices)
303 // if (theDoc.IsNull())
306 // int nbSections = thePolyline->NbSections();
307 // const QString& theNamePrefix = thePolyline->GetName();
308 // const QColor& theColor = thePolyline->GetWireColor();
310 // int n = theShapes.size();
311 // DEBTRACE("theShapes.size() "<< n);
313 // for (int i = 0; i < n; i++)
315 // Handle( HYDROData_PolylineXY ) aPolyline =
316 // Handle( HYDROData_PolylineXY )::DownCast(theDoc->CreateObject(KIND_POLYLINEXY));
317 // if (aPolyline.IsNull())
320 // for (int isec = 0; isec < nbSections; isec++)
322 // gp_XY p = gp_XY(0, 0);
323 // //double d = thePolyline->GetMinDistance(isec, p);
328 // QString aNewName = theNamePrefix + "_" + QString::number(anIndex);
329 // while (!theDoc->FindObjectByName(aNewName).IsNull()) // the object with such a name is not found
332 // aNewName = theNamePrefix + "_" + QString::number(anIndex);
334 // aPolyline->SetName(aNewName);
338 // aPolyline->SetName(theNamePrefix);
341 // if (theColor.isValid())
342 // aPolyline->SetWireColor(theColor);
348 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
349 const QString& theNamePrefix,
350 const std::vector<TopoDS_Shape>& theShapes,
352 const QColor& theColor )
354 if( theDoc.IsNull() )
357 int n = theShapes.size();
358 DEBTRACE("theShapes.size() "<< n);
360 for( int i=0; i<n; i++ )
362 Handle( HYDROData_PolylineXY ) aPolyline =
363 Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
364 if( aPolyline.IsNull() )
367 aPolyline->ImportShape(theShapes[i], false);
368 //aPolyline->SetShape( theShapes[i] );
373 QString aNewName = theNamePrefix + "_" + QString::number( anIndex );
374 while( !theDoc->FindObjectByName( aNewName ).IsNull() ) // the object with such a name is not found
377 aNewName = theNamePrefix + "_" + QString::number( anIndex );
379 aPolyline->SetName( aNewName );
383 aPolyline->SetName( theNamePrefix );
386 if( theColor.isValid() )
387 aPolyline->SetWireColor( theColor );
392 double HYDROData_PolylineOperator::ReduceDeflection(
393 const double theDeflection,
394 HYDROData_TopoCurve& theCurve,
397 // Construct the approximating B-spline.
398 std::list<gp_XYZ> aPs;
399 if (!theCurve.ValuesInKnots(aPs))
404 Handle(TColgp_HArray1OfPnt) aPs2 = new TColgp_HArray1OfPnt(1, aPs.size());
406 std::list<gp_XYZ>::const_iterator aLastPIt = aPs.end();
407 std::list<gp_XYZ>::const_iterator aPIt = aPs.begin();
408 for (int aPN = 1; aPIt != aLastPIt; ++aPN, ++aPIt)
410 aPs2->SetValue(aPN, *aPIt);
413 Handle(Geom_BSplineCurve) aBSpline2;
414 const bool isClosed = theCurve.IsClosed();
416 if (!CurveCreator_Utils::constructBSpline(aPs2, isClosed, aBSpline2))
422 // Calculate the piece deflections.
423 std::deque<double> aSqDefls;
424 double aMaxSqDefl = 0;
425 std::list<TopoDS_Edge>& aEdges = theCurve.Edges();
426 std::list<TopoDS_Edge>::const_iterator aLastEIt = aEdges.end();
428 std::list<TopoDS_Edge>::const_iterator aEIt = aEdges.begin();
429 for (int aPrevKCount = 0; aEIt != aLastEIt; ++aEIt)
431 TopLoc_Location aLoc;
433 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
434 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
435 const int aKCount = aBSpline->NbKnots();
436 for (int aKN = 1; aKN < aKCount; ++aKN)
438 const double aParam =
439 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5;
440 const double aParam2 = (aBSpline2->Knot(aPrevKCount + aKN) +
441 aBSpline2->Knot(aPrevKCount + aKN + 1)) * 0.5;
442 const double aSqDefl = Abs(aBSpline->Value(aParam).
443 SquareDistance(aBSpline2->Value(aParam2)));
444 aSqDefls.push_back(aSqDefl);
445 if (aMaxSqDefl < aSqDefl)
447 aMaxSqDefl = aSqDefl;
450 aPrevKCount += aKCount - 1;
454 // Check whether the reducing is necessary.
455 const double aMaxDefl = Sqrt(aMaxSqDefl);
456 if (aMaxDefl <= theDeflection)
461 // Reduce the deflections.
462 const double aThresSqDefl =
463 Max(aMaxSqDefl * 0.25, theDeflection * theDeflection);
464 std::list<TopoDS_Edge>::iterator aEIt = aEdges.begin();
465 std::deque<double>::const_iterator aSqDIt = aSqDefls.begin();
467 for (; aEIt != aLastEIt; ++aEIt)
469 TopLoc_Location aLoc;
471 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
472 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
473 Handle(Geom_BSplineCurve) aBSpline2 =
474 Handle(Geom_BSplineCurve)::DownCast(aBSpline->Copy());
475 const int aKCount = aBSpline->NbKnots();
476 for (int aKN = 1; aKN < aKCount; ++aSqDIt, ++aKN)
478 if (*aSqDIt > aThresSqDefl)
480 aBSpline2->InsertKnot(
481 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5);
485 BRep_Builder().MakeEdge(aEdge, aBSpline2, Precision::Confusion());
486 BRep_Builder().Add(aEdge, TopExp::FirstVertex(*aEIt));
487 BRep_Builder().Add(aEdge, TopExp::LastVertex(*aEIt));
488 thePieceCount += aBSpline2->NbKnots() - 1;
494 bool HYDROData_PolylineOperator::Extract( const Handle(HYDROData_Document)& theDocument,
495 const Handle(HYDROData_Object)& theObject )
497 if( theObject.IsNull() || theDocument.IsNull() )
500 QList<TopoDS_Shape> aBoundShapes;
501 QStringList aBoundNames;
503 theObject->GetBoundaries( aBoundShapes, aBoundNames );
505 for( int i=0, n=aBoundShapes.size(); i<n; i++ )
507 TopoDS_Shape aShape = aBoundShapes[i];
508 if( aShape.IsNull() )
511 QString aBoundName = i<aBoundNames.size() ? aBoundNames[i] : "";
513 Handle( HYDROData_PolylineXY ) aPolyline =
514 Handle( HYDROData_PolylineXY )::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
516 if( aPolyline.IsNull() )
519 aPolyline->SetShape( aShape );
522 QString aName = aBoundName;
523 while( !theDocument->FindObjectByName( aName ).IsNull() )
526 aName = aBoundName + "_" + QString::number( anIndex );
528 aPolyline->SetName( aName );