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>
53 #include <TopTools_IndexedMapOfShape.hxx>
54 #include <BRepLib_MakeWire.hxx>
57 #include "HYDRO_trace.hxx"
58 #include <BRepTools.hxx>
61 template<class T> void append( std::vector<T>& theList, const std::vector<T>& theList2 )
63 int aSize = theList.size();
64 int aNewSize = aSize + theList2.size();
69 theList.resize( aNewSize );
70 for( int i=aSize, j=0; i<aNewSize; i++, j++ )
71 theList[i] = theList2[j];
74 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
75 const Handle( HYDROData_PolylineXY )& thePolyline,
76 const gp_Pnt2d& thePoint,
77 double theTolerance ) const
79 if (thePolyline.IsNull())
84 std::vector<gp_Pnt2d> aPointsList( 1 );
85 aPointsList[0] = thePoint;
86 std::vector<TopoDS_Wire> aCurves;
87 GetWires(thePolyline, aCurves);
90 std::vector<QColor> aSectColors;
91 int nbSec = thePolyline->NbSections();
92 for (int i = 0; i < nbSec; i++)
95 thePolyline->GetSectionColor(i, aColor);
96 aSectColors.push_back(aColor);
99 bool bsetColor = aCurves.size() == aSectColors.size();
100 for( int i=0, n=aCurves.size(); i<n; i++ )
102 std::vector<TopoDS_Shape> aCurvesList;
103 Split( aCurves[i], thePoint, theTolerance, aCurvesList );
104 NCollection_IndexedDataMap<Handle(HYDROData_PolylineXY), TopoDS_Shape> outPoly2Sh;
105 bool isLocalOK = CreatePolylines( theDoc, thePolyline, aCurvesList, true, outPoly2Sh );
106 isOK = isOK && isLocalOK;
109 QColor aColor = aSectColors[i];
110 if( aColor.isValid() )
112 for (int j=1;j<=outPoly2Sh.Extent();j++)
114 const Handle(HYDROData_PolylineXY)& Poly = outPoly2Sh.FindKey(j);
115 int nbSec = Poly->NbSections();
116 for (int k = 0; k < nbSec; k++)
117 Poly->SetSectionColor(k, aColor);
126 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
127 const Handle( HYDROData_PolylineXY )& thePolyline,
128 const Handle( HYDROData_PolylineXY )& theTool,
130 bool& theIsIntersected) const
132 if (thePolyline.IsNull() || theTool.IsNull())
137 HYDROData_SequenceOfObjects aSeq;
138 aSeq.Append( theTool );
139 return split( theDoc, thePolyline, aSeq, theTolerance, -1, theIsIntersected);
142 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
143 const HYDROData_SequenceOfObjects& thePolylines,
144 double theTolerance )
146 int f = thePolylines.Lower(), l = thePolylines.Upper();
147 for( int i=f; i<=l; i++ )
149 Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
151 if( !split( theDoc, aPolyline, thePolylines, theTolerance, i, isIntersected) )
157 bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theDoc,
158 const QString& theName,
159 const HYDROData_SequenceOfObjects& thePolylines,
160 bool isConnectByNewSegment,
161 double theTolerance )
163 Handle( HYDROData_PolylineXY ) aNewPolyline =
164 Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
167 HYDROData_SequenceOfObjects::Iterator aPIt(thePolylines);
169 for (int ip=1; aPIt.More(); aPIt.Next(), ip++)
171 Handle(HYDROData_PolylineXY) aPolyline =
172 Handle(HYDROData_PolylineXY)::DownCast(aPIt.Value());
173 NCollection_Sequence<TCollection_AsciiString> aSectNames;
174 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
175 NCollection_Sequence<bool> aSectClosures;
176 aPolyline->GetSections( aSectNames, aSectTypes, aSectClosures );
178 for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
180 const TCollection_AsciiString& aSectName = aSectNames.Value( i ) + "_" + ip;
181 const HYDROData_PolylineXY::SectionType& aSectType = aSectTypes.Value( i );
182 bool aSectClosure = aSectClosures.Value( i );
183 aNewPolyline->AddSection(aSectName, aSectType, aSectClosure);
184 HYDROData_PolylineXY::PointsList aPointsList = aPolyline->GetPoints(i-1, false);
185 aNewPolyline->SetPoints(ins++, aPointsList);
188 QString aName = theName;
189 if( aName.isEmpty() )
193 QString aNewName = aName + "_" + QString::number(anIndex);
194 while (!theDoc->FindObjectByName(aNewName).IsNull()) // the object with such a name is not found
197 aNewName = aName + "_" + QString::number(anIndex);
202 aNewPolyline->SetName(aName);
203 aNewPolyline->Update();
207 bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theDoc,
208 const Handle( HYDROData_PolylineXY )& thePolyline,
209 const HYDROData_SequenceOfObjects& theTools,
212 bool& theIsIntersected) const
214 theIsIntersected = false;
216 if (thePolyline.IsNull())
221 std::vector<TopoDS_Wire> aCurves;
222 GetWires(thePolyline, aCurves);
223 std::vector<TopoDS_Wire> aToolCurves;
224 for( int i=theTools.Lower(), n=theTools.Upper(); i<=n; i++ )
225 if( i!=theIgnoreIndex )
227 Handle( HYDROData_PolylineXY ) aToolPolyline =
228 Handle( HYDROData_PolylineXY )::DownCast( theTools.Value( i ) );
229 if (!aToolPolyline.IsNull())
231 std::vector<TopoDS_Wire> aTCurves;
232 GetWires(aToolPolyline, aTCurves);
233 append( aToolCurves, aTCurves);
237 if (aToolCurves.empty())
242 std::vector<QColor> aSectColors;
243 int nbSec = thePolyline->NbSections();
247 const int aPSCount = aCurves.size();
248 const int aTSCount = aToolCurves.size();
249 std::vector<TopoDS_Shape> aResult;
251 bool bSetColor = aPSCount == nbSec;
254 for (int i = 0; i < nbSec; i++)
257 thePolyline->GetSectionColor(i, aColor);
258 aSectColors.push_back(aColor);
261 NCollection_DataMap <TopoDS_Shape, QColor, TopTools_ShapeMapHasher> W2Color;
263 for (int aPSI = 0; aPSI < aPSCount; ++aPSI)
265 HYDROData_TopoCurve aCurve;
266 DEBTRACE("Initialize curve " << aPSI);
267 if (!aCurve.Initialize(aCurves[aPSI]))
272 std::deque<std::list<double> > aParams;
273 for (int aTSI = 0; aTSI < aTSCount; ++aTSI)
275 aCurve.Intersect(aToolCurves[aTSI], aParams);
278 std::deque<HYDROData_TopoCurve> aSplitCurves;
279 theIsIntersected |= aCurve.Cut(aParams, aSplitCurves);
280 std::deque<HYDROData_TopoCurve>::const_iterator aCIt =
281 aSplitCurves.begin();
282 std::deque<HYDROData_TopoCurve>::const_iterator aLastCIt =
284 for (int iw=0; aCIt != aLastCIt; ++aCIt, iw++)
286 //std::stringstream brepName;
287 //brepName << "theSplitWire_";
289 //brepName << ".brep";
290 //BRepTools::Write(aCIt->Wire() , brepName.str().c_str() );
291 const TopoDS_Wire& aW = aCIt->Wire();
293 W2Color.Bind(aW, aSectColors[aPSI]);
294 aResult.push_back(aW);
298 NCollection_IndexedDataMap<Handle(HYDROData_PolylineXY), TopoDS_Shape> outPoly2Sh;
299 CreatePolylines( theDoc, thePolyline, aResult, true, outPoly2Sh );
301 for (int j=1;j<=outPoly2Sh.Extent();j++)
303 const Handle(HYDROData_PolylineXY)& Poly = outPoly2Sh.FindKey(j);
304 const TopoDS_Shape& Sh = outPoly2Sh.FindFromIndex(j);
306 int nbSec = Poly->NbSections();
307 for (int k = 0; k < nbSec; k++)
309 const QColor* color = W2Color.Seek(Sh);
311 Poly->SetSectionColor(k, *color);
318 void HYDROData_PolylineOperator::GetWires(
319 const Handle( HYDROData_PolylineXY )& thePolyline,
320 std::vector<TopoDS_Wire>& theWires)
322 TopoDS_Shape aShape = thePolyline->GetShape();
325 if( aShape.ShapeType()==TopAbs_WIRE )
327 theWires.push_back( TopoDS::Wire( aShape ) );
331 TopExp_Explorer anExp( aShape, TopAbs_WIRE );
332 for( ; anExp.More(); anExp.Next() )
334 theWires.push_back( TopoDS::Wire( anExp.Current() ) );
339 void HYDROData_PolylineOperator::Split(
340 const TopoDS_Wire& theWire,
341 const gp_Pnt2d& thePoint,
343 std::vector<TopoDS_Shape>& theWires)
345 HYDROData_TopoCurve aCurve;
346 if (!aCurve.Initialize(theWire))
348 theWires.push_back(theWire);
352 const gp_XYZ aP(thePoint.X(), thePoint.Y(), 0);
353 std::list<TopoDS_Edge>::const_iterator aEPos;
355 aCurve.Project(aP, aEPos, aParam);
356 HYDROData_TopoCurve aCurve1, aCurve2;
357 aCurve.Cut(aEPos, aParam, aCurve1, aCurve2);
358 theWires.push_back(aCurve1.Wire());
359 if (!aCurve2.IsEmpty())
361 theWires.push_back(aCurve2.Wire());
365 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
366 const Handle( HYDROData_PolylineXY )& theOldPolyline,
367 const std::vector<TopoDS_Shape>& theShapes,
369 NCollection_IndexedDataMap<Handle(HYDROData_PolylineXY), TopoDS_Shape>& outPoly2Sh)
371 if( theDoc.IsNull() )
374 if ( theOldPolyline.IsNull() )
376 const QString& theNamePrefix = theOldPolyline->GetName();
378 int n = theShapes.size();
379 DEBTRACE("theShapes.size() "<< n);
381 for( int i=0; i<n; i++ )
383 Handle( HYDROData_PolylineXY ) aPolyline =
384 Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
385 if( aPolyline.IsNull() )
388 aPolyline->ImportShape(theShapes[i], false, theOldPolyline, false);
392 QString aNewName = theNamePrefix + "_" + QString::number( anIndex );
393 while( !theDoc->FindObjectByName( aNewName ).IsNull() ) // the object with such a name is not found
396 aNewName = theNamePrefix + "_" + QString::number( anIndex );
398 aPolyline->SetName( aNewName );
402 aPolyline->SetName( theNamePrefix );
405 outPoly2Sh.Add(aPolyline, theShapes[i]);
410 double HYDROData_PolylineOperator::ReduceDeflection(
411 const double theDeflection,
412 HYDROData_TopoCurve& theCurve,
415 // Construct the approximating B-spline.
416 std::list<gp_XYZ> aPs;
417 if (!theCurve.ValuesInKnots(aPs))
422 Handle(TColgp_HArray1OfPnt) aPs2 = new TColgp_HArray1OfPnt(1, aPs.size());
424 std::list<gp_XYZ>::const_iterator aLastPIt = aPs.end();
425 std::list<gp_XYZ>::const_iterator aPIt = aPs.begin();
426 for (int aPN = 1; aPIt != aLastPIt; ++aPN, ++aPIt)
428 aPs2->SetValue(aPN, *aPIt);
431 Handle(Geom_BSplineCurve) aBSpline2;
432 const bool isClosed = theCurve.IsClosed();
434 if (!CurveCreator_Utils::constructBSpline(aPs2, isClosed, aBSpline2))
440 // Calculate the piece deflections.
441 std::deque<double> aSqDefls;
442 double aMaxSqDefl = 0;
443 std::list<TopoDS_Edge>& aEdges = theCurve.Edges();
444 std::list<TopoDS_Edge>::const_iterator aLastEIt = aEdges.end();
446 std::list<TopoDS_Edge>::const_iterator aEIt = aEdges.begin();
447 for (int aPrevKCount = 0; aEIt != aLastEIt; ++aEIt)
449 TopLoc_Location aLoc;
451 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
452 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
453 const int aKCount = aBSpline->NbKnots();
454 for (int aKN = 1; aKN < aKCount; ++aKN)
456 const double aParam =
457 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5;
458 const double aParam2 = (aBSpline2->Knot(aPrevKCount + aKN) +
459 aBSpline2->Knot(aPrevKCount + aKN + 1)) * 0.5;
460 const double aSqDefl = Abs(aBSpline->Value(aParam).
461 SquareDistance(aBSpline2->Value(aParam2)));
462 aSqDefls.push_back(aSqDefl);
463 if (aMaxSqDefl < aSqDefl)
465 aMaxSqDefl = aSqDefl;
468 aPrevKCount += aKCount - 1;
472 // Check whether the reducing is necessary.
473 const double aMaxDefl = Sqrt(aMaxSqDefl);
474 if (aMaxDefl <= theDeflection)
479 // Reduce the deflections.
480 const double aThresSqDefl =
481 Max(aMaxSqDefl * 0.25, theDeflection * theDeflection);
482 std::list<TopoDS_Edge>::iterator aEIt = aEdges.begin();
483 std::deque<double>::const_iterator aSqDIt = aSqDefls.begin();
485 for (; aEIt != aLastEIt; ++aEIt)
487 TopLoc_Location aLoc;
489 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
490 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
491 Handle(Geom_BSplineCurve) aBSpline2 =
492 Handle(Geom_BSplineCurve)::DownCast(aBSpline->Copy());
493 const int aKCount = aBSpline->NbKnots();
494 for (int aKN = 1; aKN < aKCount; ++aSqDIt, ++aKN)
496 if (*aSqDIt > aThresSqDefl)
498 aBSpline2->InsertKnot(
499 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5);
503 BRep_Builder().MakeEdge(aEdge, aBSpline2, Precision::Confusion());
504 BRep_Builder().Add(aEdge, TopExp::FirstVertex(*aEIt));
505 BRep_Builder().Add(aEdge, TopExp::LastVertex(*aEIt));
506 thePieceCount += aBSpline2->NbKnots() - 1;
512 bool HYDROData_PolylineOperator::Extract( const Handle(HYDROData_Document)& theDocument,
513 const Handle(HYDROData_Object)& theObject )
515 if( theObject.IsNull() || theDocument.IsNull() )
518 QList<TopoDS_Shape> aBoundShapes;
519 QStringList aBoundNames;
520 QMap<QString, TopTools_IndexedMapOfShape> aNameToShMap;
522 theObject->GetBoundaries( aBoundShapes, aBoundNames );
524 for( int i=0, n=aBoundShapes.size(); i<n; i++ )
526 TopoDS_Shape aShape = aBoundShapes[i];
527 if( aShape.IsNull() )
530 QString aBoundName = i<aBoundNames.size() ? aBoundNames[i] : "";
532 if (!aNameToShMap.contains(aBoundName))
534 TopTools_IndexedMapOfShape IM;
536 aNameToShMap[aBoundName] = IM;
539 aNameToShMap[aBoundName].Add(aShape);
543 foreach( QString K, aNameToShMap.keys() )
545 const TopTools_IndexedMapOfShape& IM = aNameToShMap.value(K);
546 TopTools_ListOfShape LSE;
547 for (int i = 1; i <= IM.Extent(); i++)
549 const TopoDS_Edge& E = TopoDS::Edge(IM(i));
556 TopoDS_Shape aShapeOut;
557 if (LSE.Extent() == 1)
559 aShapeOut = LSE.First();
561 else if (LSE.Extent() > 1)
566 aShapeOut = WM.Wire();
572 Handle( HYDROData_PolylineXY ) aPolyline =
573 Handle( HYDROData_PolylineXY )::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
575 if( aPolyline.IsNull() )
578 aPolyline->SetShape( aShapeOut );
582 while( !theDocument->FindObjectByName( aName ).IsNull() )
585 aName = K + "_" + QString::number( anIndex );
587 aPolyline->SetName( aName );