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 Handle( HYDROData_PolylineXY ) aNewPolyline =
136 Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
139 HYDROData_SequenceOfObjects::Iterator aPIt(thePolylines);
141 for (int ip=1; aPIt.More(); aPIt.Next(), ip++)
143 Handle(HYDROData_PolylineXY) aPolyline =
144 Handle(HYDROData_PolylineXY)::DownCast(aPIt.Value());
145 NCollection_Sequence<TCollection_AsciiString> aSectNames;
146 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
147 NCollection_Sequence<bool> aSectClosures;
148 aPolyline->GetSections( aSectNames, aSectTypes, aSectClosures );
150 for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
152 const TCollection_AsciiString& aSectName = aSectNames.Value( i ) + "_" + ip;
153 const HYDROData_PolylineXY::SectionType& aSectType = aSectTypes.Value( i );
154 bool aSectClosure = aSectClosures.Value( i );
155 aNewPolyline->AddSection(aSectName, aSectType, aSectClosure);
156 HYDROData_PolylineXY::PointsList aPointsList = aPolyline->GetPoints(i-1, false);
157 aNewPolyline->SetPoints(ins++, aPointsList);
160 QString aName = theName;
161 if( aName.isEmpty() )
163 aNewPolyline->SetName(aName);
167 bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theDoc,
168 const Handle( HYDROData_PolylineXY )& thePolyline,
169 const HYDROData_SequenceOfObjects& theTools,
172 bool& theIsIntersected) const
174 theIsIntersected = false;
176 if (thePolyline.IsNull())
181 std::vector<TopoDS_Wire> aCurves;
182 GetWires(thePolyline, aCurves);
183 std::vector<TopoDS_Wire> aToolCurves;
184 for( int i=theTools.Lower(), n=theTools.Upper(); i<=n; i++ )
185 if( i!=theIgnoreIndex )
187 Handle( HYDROData_PolylineXY ) aToolPolyline =
188 Handle( HYDROData_PolylineXY )::DownCast( theTools.Value( i ) );
189 if (!aToolPolyline.IsNull())
191 std::vector<TopoDS_Wire> aTCurves;
192 GetWires(aToolPolyline, aTCurves);
193 append( aToolCurves, aTCurves);
197 if (aToolCurves.empty())
202 const int aPSCount = aCurves.size();
203 const int aTSCount = aToolCurves.size();
204 std::vector<TopoDS_Shape> aResult;
205 for (int aPSI = 0; aPSI < aPSCount; ++aPSI)
207 HYDROData_TopoCurve aCurve;
208 DEBTRACE("Initialize curve " << aPSI);
209 if (!aCurve.Initialize(aCurves[aPSI]))
214 std::deque<std::list<double> > aParams;
215 for (int aTSI = 0; aTSI < aTSCount; ++aTSI)
217 aCurve.Intersect(aToolCurves[aTSI], aParams);
220 std::deque<HYDROData_TopoCurve> aSplitCurves;
221 theIsIntersected |= aCurve.Cut(aParams, aSplitCurves);
222 std::deque<HYDROData_TopoCurve>::const_iterator aCIt =
223 aSplitCurves.begin();
224 std::deque<HYDROData_TopoCurve>::const_iterator aLastCIt =
226 for (int iw=0; aCIt != aLastCIt; ++aCIt, iw++)
228 std::stringstream brepName;
229 brepName << "theSplitWire_";
232 BRepTools::Write(aCIt->Wire() , brepName.str().c_str() );
233 aResult.push_back(aCIt->Wire());
237 CreatePolylines( theDoc, thePolyline->GetName(), aResult, true, thePolyline->GetWireColor() );
238 //CreatePolylinesSplit( theDoc, thePolyline, aResult, true );
242 void HYDROData_PolylineOperator::GetWires(
243 const Handle( HYDROData_PolylineXY )& thePolyline,
244 std::vector<TopoDS_Wire>& theWires)
246 TopoDS_Shape aShape = thePolyline->GetShape();
247 if( aShape.ShapeType()==TopAbs_WIRE )
249 theWires.push_back( TopoDS::Wire( aShape ) );
253 TopExp_Explorer anExp( aShape, TopAbs_WIRE );
254 for( ; anExp.More(); anExp.Next() )
256 theWires.push_back( TopoDS::Wire( anExp.Current() ) );
261 void HYDROData_PolylineOperator::Split(
262 const TopoDS_Wire& theWire,
263 const gp_Pnt2d& thePoint,
265 std::vector<TopoDS_Shape>& theWires)
267 HYDROData_TopoCurve aCurve;
268 if (!aCurve.Initialize(theWire))
270 theWires.push_back(theWire);
274 const gp_XYZ aP(thePoint.X(), thePoint.Y(), 0);
275 std::list<TopoDS_Edge>::const_iterator aEPos;
277 aCurve.Project(aP, aEPos, aParam);
278 HYDROData_TopoCurve aCurve1, aCurve2;
279 aCurve.Cut(aEPos, aParam, aCurve1, aCurve2);
280 theWires.push_back(aCurve1.Wire());
281 if (!aCurve2.IsEmpty())
283 theWires.push_back(aCurve2.Wire());
287 //bool HYDROData_PolylineOperator::CreatePolylinesSplit(const Handle( HYDROData_Document )& theDoc,
288 // const Handle( HYDROData_PolylineXY )& thePolyline,
289 // const std::vector<TopoDS_Shape>& theShapes, bool isUseIndices)
292 // if (theDoc.IsNull())
295 // int nbSections = thePolyline->NbSections();
296 // const QString& theNamePrefix = thePolyline->GetName();
297 // const QColor& theColor = thePolyline->GetWireColor();
299 // int n = theShapes.size();
300 // DEBTRACE("theShapes.size() "<< n);
302 // for (int i = 0; i < n; i++)
304 // Handle( HYDROData_PolylineXY ) aPolyline =
305 // Handle( HYDROData_PolylineXY )::DownCast(theDoc->CreateObject(KIND_POLYLINEXY));
306 // if (aPolyline.IsNull())
309 // for (int isec = 0; isec < nbSections; isec++)
311 // gp_XY p = gp_XY(0, 0);
312 // //double d = thePolyline->GetMinDistance(isec, p);
317 // QString aNewName = theNamePrefix + "_" + QString::number(anIndex);
318 // while (!theDoc->FindObjectByName(aNewName).IsNull()) // the object with such a name is not found
321 // aNewName = theNamePrefix + "_" + QString::number(anIndex);
323 // aPolyline->SetName(aNewName);
327 // aPolyline->SetName(theNamePrefix);
330 // if (theColor.isValid())
331 // aPolyline->SetWireColor(theColor);
337 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
338 const QString& theNamePrefix,
339 const std::vector<TopoDS_Shape>& theShapes,
341 const QColor& theColor )
343 if( theDoc.IsNull() )
346 int n = theShapes.size();
347 DEBTRACE("theShapes.size() "<< n);
349 for( int i=0; i<n; i++ )
351 Handle( HYDROData_PolylineXY ) aPolyline =
352 Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
353 if( aPolyline.IsNull() )
356 aPolyline->ImportShape(theShapes[i], false);
357 //aPolyline->SetShape( theShapes[i] );
362 QString aNewName = theNamePrefix + "_" + QString::number( anIndex );
363 while( !theDoc->FindObjectByName( aNewName ).IsNull() ) // the object with such a name is not found
366 aNewName = theNamePrefix + "_" + QString::number( anIndex );
368 aPolyline->SetName( aNewName );
372 aPolyline->SetName( theNamePrefix );
375 if( theColor.isValid() )
376 aPolyline->SetWireColor( theColor );
381 double HYDROData_PolylineOperator::ReduceDeflection(
382 const double theDeflection,
383 HYDROData_TopoCurve& theCurve,
386 // Construct the approximating B-spline.
387 std::list<gp_XYZ> aPs;
388 if (!theCurve.ValuesInKnots(aPs))
393 Handle(TColgp_HArray1OfPnt) aPs2 = new TColgp_HArray1OfPnt(1, aPs.size());
395 std::list<gp_XYZ>::const_iterator aLastPIt = aPs.end();
396 std::list<gp_XYZ>::const_iterator aPIt = aPs.begin();
397 for (int aPN = 1; aPIt != aLastPIt; ++aPN, ++aPIt)
399 aPs2->SetValue(aPN, *aPIt);
402 Handle(Geom_BSplineCurve) aBSpline2;
403 const bool isClosed = theCurve.IsClosed();
405 if (!CurveCreator_Utils::constructBSpline(aPs2, isClosed, aBSpline2))
411 // Calculate the piece deflections.
412 std::deque<double> aSqDefls;
413 double aMaxSqDefl = 0;
414 std::list<TopoDS_Edge>& aEdges = theCurve.Edges();
415 std::list<TopoDS_Edge>::const_iterator aLastEIt = aEdges.end();
417 std::list<TopoDS_Edge>::const_iterator aEIt = aEdges.begin();
418 for (int aPrevKCount = 0; 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 const int aKCount = aBSpline->NbKnots();
425 for (int aKN = 1; aKN < aKCount; ++aKN)
427 const double aParam =
428 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5;
429 const double aParam2 = (aBSpline2->Knot(aPrevKCount + aKN) +
430 aBSpline2->Knot(aPrevKCount + aKN + 1)) * 0.5;
431 const double aSqDefl = Abs(aBSpline->Value(aParam).
432 SquareDistance(aBSpline2->Value(aParam2)));
433 aSqDefls.push_back(aSqDefl);
434 if (aMaxSqDefl < aSqDefl)
436 aMaxSqDefl = aSqDefl;
439 aPrevKCount += aKCount - 1;
443 // Check whether the reducing is necessary.
444 const double aMaxDefl = Sqrt(aMaxSqDefl);
445 if (aMaxDefl <= theDeflection)
450 // Reduce the deflections.
451 const double aThresSqDefl =
452 Max(aMaxSqDefl * 0.25, theDeflection * theDeflection);
453 std::list<TopoDS_Edge>::iterator aEIt = aEdges.begin();
454 std::deque<double>::const_iterator aSqDIt = aSqDefls.begin();
456 for (; aEIt != aLastEIt; ++aEIt)
458 TopLoc_Location aLoc;
460 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
461 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
462 Handle(Geom_BSplineCurve) aBSpline2 =
463 Handle(Geom_BSplineCurve)::DownCast(aBSpline->Copy());
464 const int aKCount = aBSpline->NbKnots();
465 for (int aKN = 1; aKN < aKCount; ++aSqDIt, ++aKN)
467 if (*aSqDIt > aThresSqDefl)
469 aBSpline2->InsertKnot(
470 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5);
474 BRep_Builder().MakeEdge(aEdge, aBSpline2, Precision::Confusion());
475 BRep_Builder().Add(aEdge, TopExp::FirstVertex(*aEIt));
476 BRep_Builder().Add(aEdge, TopExp::LastVertex(*aEIt));
477 thePieceCount += aBSpline2->NbKnots() - 1;
483 bool HYDROData_PolylineOperator::Extract( const Handle(HYDROData_Document)& theDocument,
484 const Handle(HYDROData_Object)& theObject )
486 if( theObject.IsNull() || theDocument.IsNull() )
489 QList<TopoDS_Shape> aBoundShapes;
490 QStringList aBoundNames;
492 theObject->GetBoundaries( aBoundShapes, aBoundNames );
494 for( int i=0, n=aBoundShapes.size(); i<n; i++ )
496 TopoDS_Shape aShape = aBoundShapes[i];
497 if( aShape.IsNull() )
500 QString aBoundName = i<aBoundNames.size() ? aBoundNames[i] : "";
502 Handle( HYDROData_PolylineXY ) aPolyline =
503 Handle( HYDROData_PolylineXY )::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
505 if( aPolyline.IsNull() )
508 aPolyline->SetShape( aShape );
511 QString aName = aBoundName;
512 while( !theDocument->FindObjectByName( aName ).IsNull() )
515 aName = aBoundName + "_" + QString::number( anIndex );
517 aPolyline->SetName( aName );