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, aCurvesList, true );
92 isOK = isOK && isLocalOK;
97 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
98 const Handle( HYDROData_PolylineXY )& thePolyline,
99 const Handle( HYDROData_PolylineXY )& theTool,
101 bool& theIsIntersected) const
103 if (thePolyline.IsNull() || theTool.IsNull())
108 HYDROData_SequenceOfObjects aSeq;
109 aSeq.Append( theTool );
110 return split( theDoc, thePolyline, aSeq, theTolerance, -1, theIsIntersected);
113 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
114 const HYDROData_SequenceOfObjects& thePolylines,
115 double theTolerance )
117 int f = thePolylines.Lower(), l = thePolylines.Upper();
118 for( int i=f; i<=l; i++ )
120 Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
122 if( !split( theDoc, aPolyline, thePolylines, theTolerance, i, isIntersected) )
128 bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theDoc,
129 const QString& theName,
130 const HYDROData_SequenceOfObjects& thePolylines,
131 bool isConnectByNewSegment,
132 double theTolerance )
134 Handle( HYDROData_PolylineXY ) aNewPolyline =
135 Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
138 HYDROData_SequenceOfObjects::Iterator aPIt(thePolylines);
140 for (int ip=1; aPIt.More(); aPIt.Next(), ip++)
142 Handle(HYDROData_PolylineXY) aPolyline =
143 Handle(HYDROData_PolylineXY)::DownCast(aPIt.Value());
144 NCollection_Sequence<TCollection_AsciiString> aSectNames;
145 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
146 NCollection_Sequence<bool> aSectClosures;
147 aPolyline->GetSections( aSectNames, aSectTypes, aSectClosures );
149 for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
151 const TCollection_AsciiString& aSectName = aSectNames.Value( i ) + "_" + ip;
152 const HYDROData_PolylineXY::SectionType& aSectType = aSectTypes.Value( i );
153 bool aSectClosure = aSectClosures.Value( i );
154 aNewPolyline->AddSection(aSectName, aSectType, aSectClosure);
155 HYDROData_PolylineXY::PointsList aPointsList = aPolyline->GetPoints(i-1, false);
156 aNewPolyline->SetPoints(ins++, aPointsList);
159 QString aName = theName;
160 if( aName.isEmpty() )
164 QString aNewName = aName + "_" + QString::number(anIndex);
165 while (!theDoc->FindObjectByName(aNewName).IsNull()) // the object with such a name is not found
168 aNewName = aName + "_" + QString::number(anIndex);
173 aNewPolyline->SetName(aName);
174 aNewPolyline->Update();
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, aResult, true );
252 void HYDROData_PolylineOperator::GetWires(
253 const Handle( HYDROData_PolylineXY )& thePolyline,
254 std::vector<TopoDS_Wire>& theWires)
256 TopoDS_Shape aShape = thePolyline->GetShape();
257 if( aShape.ShapeType()==TopAbs_WIRE )
259 theWires.push_back( TopoDS::Wire( aShape ) );
263 TopExp_Explorer anExp( aShape, TopAbs_WIRE );
264 for( ; anExp.More(); anExp.Next() )
266 theWires.push_back( TopoDS::Wire( anExp.Current() ) );
271 void HYDROData_PolylineOperator::Split(
272 const TopoDS_Wire& theWire,
273 const gp_Pnt2d& thePoint,
275 std::vector<TopoDS_Shape>& theWires)
277 HYDROData_TopoCurve aCurve;
278 if (!aCurve.Initialize(theWire))
280 theWires.push_back(theWire);
284 const gp_XYZ aP(thePoint.X(), thePoint.Y(), 0);
285 std::list<TopoDS_Edge>::const_iterator aEPos;
287 aCurve.Project(aP, aEPos, aParam);
288 HYDROData_TopoCurve aCurve1, aCurve2;
289 aCurve.Cut(aEPos, aParam, aCurve1, aCurve2);
290 theWires.push_back(aCurve1.Wire());
291 if (!aCurve2.IsEmpty())
293 theWires.push_back(aCurve2.Wire());
297 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
298 const Handle( HYDROData_PolylineXY )& theOldPolyline,
299 const std::vector<TopoDS_Shape>& theShapes,
302 if( theDoc.IsNull() )
305 if ( theOldPolyline.IsNull() )
307 const QString& theNamePrefix = theOldPolyline->GetName();
308 const QColor& theColor = theOldPolyline->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 aPolyline->ImportShape(theShapes[i], false, theOldPolyline, false);
324 QString aNewName = theNamePrefix + "_" + QString::number( anIndex );
325 while( !theDoc->FindObjectByName( aNewName ).IsNull() ) // the object with such a name is not found
328 aNewName = theNamePrefix + "_" + QString::number( anIndex );
330 aPolyline->SetName( aNewName );
334 aPolyline->SetName( theNamePrefix );
337 if( theColor.isValid() )
338 aPolyline->SetWireColor( theColor );
343 double HYDROData_PolylineOperator::ReduceDeflection(
344 const double theDeflection,
345 HYDROData_TopoCurve& theCurve,
348 // Construct the approximating B-spline.
349 std::list<gp_XYZ> aPs;
350 if (!theCurve.ValuesInKnots(aPs))
355 Handle(TColgp_HArray1OfPnt) aPs2 = new TColgp_HArray1OfPnt(1, aPs.size());
357 std::list<gp_XYZ>::const_iterator aLastPIt = aPs.end();
358 std::list<gp_XYZ>::const_iterator aPIt = aPs.begin();
359 for (int aPN = 1; aPIt != aLastPIt; ++aPN, ++aPIt)
361 aPs2->SetValue(aPN, *aPIt);
364 Handle(Geom_BSplineCurve) aBSpline2;
365 const bool isClosed = theCurve.IsClosed();
367 if (!CurveCreator_Utils::constructBSpline(aPs2, isClosed, aBSpline2))
373 // Calculate the piece deflections.
374 std::deque<double> aSqDefls;
375 double aMaxSqDefl = 0;
376 std::list<TopoDS_Edge>& aEdges = theCurve.Edges();
377 std::list<TopoDS_Edge>::const_iterator aLastEIt = aEdges.end();
379 std::list<TopoDS_Edge>::const_iterator aEIt = aEdges.begin();
380 for (int aPrevKCount = 0; aEIt != aLastEIt; ++aEIt)
382 TopLoc_Location aLoc;
384 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
385 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
386 const int aKCount = aBSpline->NbKnots();
387 for (int aKN = 1; aKN < aKCount; ++aKN)
389 const double aParam =
390 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5;
391 const double aParam2 = (aBSpline2->Knot(aPrevKCount + aKN) +
392 aBSpline2->Knot(aPrevKCount + aKN + 1)) * 0.5;
393 const double aSqDefl = Abs(aBSpline->Value(aParam).
394 SquareDistance(aBSpline2->Value(aParam2)));
395 aSqDefls.push_back(aSqDefl);
396 if (aMaxSqDefl < aSqDefl)
398 aMaxSqDefl = aSqDefl;
401 aPrevKCount += aKCount - 1;
405 // Check whether the reducing is necessary.
406 const double aMaxDefl = Sqrt(aMaxSqDefl);
407 if (aMaxDefl <= theDeflection)
412 // Reduce the deflections.
413 const double aThresSqDefl =
414 Max(aMaxSqDefl * 0.25, theDeflection * theDeflection);
415 std::list<TopoDS_Edge>::iterator aEIt = aEdges.begin();
416 std::deque<double>::const_iterator aSqDIt = aSqDefls.begin();
418 for (; aEIt != aLastEIt; ++aEIt)
420 TopLoc_Location aLoc;
422 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
423 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
424 Handle(Geom_BSplineCurve) aBSpline2 =
425 Handle(Geom_BSplineCurve)::DownCast(aBSpline->Copy());
426 const int aKCount = aBSpline->NbKnots();
427 for (int aKN = 1; aKN < aKCount; ++aSqDIt, ++aKN)
429 if (*aSqDIt > aThresSqDefl)
431 aBSpline2->InsertKnot(
432 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5);
436 BRep_Builder().MakeEdge(aEdge, aBSpline2, Precision::Confusion());
437 BRep_Builder().Add(aEdge, TopExp::FirstVertex(*aEIt));
438 BRep_Builder().Add(aEdge, TopExp::LastVertex(*aEIt));
439 thePieceCount += aBSpline2->NbKnots() - 1;
445 bool HYDROData_PolylineOperator::Extract( const Handle(HYDROData_Document)& theDocument,
446 const Handle(HYDROData_Object)& theObject )
448 if( theObject.IsNull() || theDocument.IsNull() )
451 QList<TopoDS_Shape> aBoundShapes;
452 QStringList aBoundNames;
454 theObject->GetBoundaries( aBoundShapes, aBoundNames );
456 for( int i=0, n=aBoundShapes.size(); i<n; i++ )
458 TopoDS_Shape aShape = aBoundShapes[i];
459 if( aShape.IsNull() )
462 QString aBoundName = i<aBoundNames.size() ? aBoundNames[i] : "";
464 Handle( HYDROData_PolylineXY ) aPolyline =
465 Handle( HYDROData_PolylineXY )::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
467 if( aPolyline.IsNull() )
470 aPolyline->SetShape( aShape );
473 QString aName = aBoundName;
474 while( !theDocument->FindObjectByName( aName ).IsNull() )
477 aName = aBoundName + "_" + QString::number( anIndex );
479 aPolyline->SetName( aName );