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>
24 #include <CurveCreator_Utils.hxx>
27 #include <BRepAdaptor_Curve.hxx>
28 #include <BRep_Builder.hxx>
29 #include <BRep_Tool.hxx>
30 #include <BRepBuilderAPI_MakeEdge2d.hxx>
31 #include <BRepBuilderAPI_MakeEdge.hxx>
32 #include <BRepBuilderAPI_MakeWire.hxx>
33 #include <Extrema_ExtCC.hxx>
34 #include <Extrema_ExtPC.hxx>
35 #include <GeomAPI_Interpolate.hxx>
36 #include <NCollection_Vector.hxx>
37 #include <Precision.hxx>
38 #include <ShapeAnalysis_TransferParametersProj.hxx>
39 #include <ShapeBuild_Edge.hxx>
40 #include <TColgp_Array1OfVec.hxx>
41 #include <TColgp_HArray1OfPnt.hxx>
42 #include <TColStd_HArray1OfBoolean.hxx>
44 #include <TopoDS_Edge.hxx>
45 #include <TopoDS_Wire.hxx>
47 #include <TopExp_Explorer.hxx>
48 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
50 #include <Geom_BSplineCurve.hxx>
52 template<class T> void append( std::vector<T>& theList, const std::vector<T>& theList2 )
54 int aSize = theList.size();
55 int aNewSize = aSize + theList2.size();
60 theList.resize( aNewSize );
61 for( int i=aSize, j=0; i<aNewSize; i++, j++ )
62 theList[i] = theList2[j];
65 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
66 const Handle( HYDROData_PolylineXY )& thePolyline,
67 const gp_Pnt2d& thePoint,
68 double theTolerance ) const
70 if (thePolyline.IsNull())
75 std::vector<gp_Pnt2d> aPointsList( 1 );
76 aPointsList[0] = thePoint;
77 std::vector<TopoDS_Wire> aCurves;
78 GetWires(thePolyline, aCurves);
80 for( int i=0, n=aCurves.size(); i<n; i++ )
82 std::vector<TopoDS_Shape> aCurvesList;
83 Split(aCurves[i], thePoint, theTolerance, aCurvesList);
84 bool isLocalOK = CreatePolylines( theDoc, thePolyline->GetName(), aCurvesList, true );
85 isOK = isOK && isLocalOK;
90 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
91 const Handle( HYDROData_PolylineXY )& thePolyline,
92 const Handle( HYDROData_PolylineXY )& theTool,
94 bool& theIsIntersected) const
96 if (thePolyline.IsNull() || theTool.IsNull())
101 HYDROData_SequenceOfObjects aSeq;
102 aSeq.Append( theTool );
103 return split( theDoc, thePolyline, aSeq, theTolerance, -1, theIsIntersected);
106 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
107 const HYDROData_SequenceOfObjects& thePolylines,
108 double theTolerance )
110 int f = thePolylines.Lower(), l = thePolylines.Upper();
111 for( int i=f; i<=l; i++ )
113 Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
115 if( !split( theDoc, aPolyline, thePolylines, theTolerance, i, isIntersected) )
121 bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theDoc,
122 const QString& theName,
123 const HYDROData_SequenceOfObjects& thePolylines,
124 bool isConnectByNewSegment,
125 double theTolerance )
127 std::deque<HYDROData_TopoCurve> aMergedCurves;
128 HYDROData_SequenceOfObjects::Iterator aPIt(thePolylines);
129 for (; aPIt.More(); aPIt.Next())
131 Handle(HYDROData_PolylineXY) aPolyline =
132 Handle(HYDROData_PolylineXY)::DownCast(aPIt.Value());
133 std::vector<TopoDS_Wire> aWires;
134 GetWires(aPolyline, aWires);
135 for (std::vector<TopoDS_Wire>::const_iterator aWIt = aWires.begin(),
136 aLastWIt = aWires.end(); aWIt != aLastWIt; ++aWIt)
138 const Standard_Boolean aResult = !isConnectByNewSegment ?
139 HYDROData_TopoCurve::Merge(theTolerance, *aWIt, aMergedCurves) :
140 HYDROData_TopoCurve::Connect(theTolerance, *aWIt, aMergedCurves);
148 TopoDS_Compound aWireSet;
149 BRep_Builder aBuilder;
150 aBuilder.MakeCompound(aWireSet);
151 std::deque<HYDROData_TopoCurve>::iterator aCIt = aMergedCurves.begin();
152 std::deque<HYDROData_TopoCurve>::iterator aLastCIt = aMergedCurves.end();
153 for (; aCIt != aLastCIt; ++aCIt)
155 if (!aCIt->IsEmpty())
157 aBuilder.Add(aWireSet, aCIt->Wire());
161 std::vector<TopoDS_Shape> aPolylines(1);
162 aPolylines[0] = aWireSet;
163 CreatePolylines(theDoc, theName, aPolylines, false);
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 if (!aCurve.Initialize(aCurves[aPSI]))
213 std::deque<std::list<double> > aParams;
214 for (int aTSI = 0; aTSI < aTSCount; ++aTSI)
216 aCurve.Intersect(aToolCurves[aTSI], aParams);
219 std::deque<HYDROData_TopoCurve> aSplittedCurves;
220 theIsIntersected |= aCurve.Cut(aParams, aSplittedCurves);
221 std::deque<HYDROData_TopoCurve>::const_iterator aCIt =
222 aSplittedCurves.begin();
223 std::deque<HYDROData_TopoCurve>::const_iterator aLastCIt =
224 aSplittedCurves.end();
225 for (; aCIt != aLastCIt; ++aCIt)
227 aResult.push_back(aCIt->Wire());
231 CreatePolylines(theDoc, thePolyline->GetName(), aResult, true);
235 void HYDROData_PolylineOperator::GetWires(
236 const Handle( HYDROData_PolylineXY )& thePolyline,
237 std::vector<TopoDS_Wire>& theWires)
239 TopoDS_Shape aShape = thePolyline->GetShape();
240 if( aShape.ShapeType()==TopAbs_WIRE )
242 theWires.push_back( TopoDS::Wire( aShape ) );
246 TopExp_Explorer anExp( aShape, TopAbs_WIRE );
247 for( ; anExp.More(); anExp.Next() )
249 theWires.push_back( TopoDS::Wire( anExp.Current() ) );
254 void HYDROData_PolylineOperator::Split(
255 const TopoDS_Wire& theWire,
256 const gp_Pnt2d& thePoint,
258 std::vector<TopoDS_Shape>& theWires)
260 HYDROData_TopoCurve aCurve;
261 if (!aCurve.Initialize(theWire))
263 theWires.push_back(theWire);
267 const gp_XYZ aP(thePoint.X(), thePoint.Y(), 0);
268 std::list<TopoDS_Edge>::const_iterator aEPos;
270 aCurve.Project(aP, aEPos, aParam);
271 HYDROData_TopoCurve aCurve1, aCurve2;
272 aCurve.Cut(aEPos, aParam, aCurve1, aCurve2);
273 theWires.push_back(aCurve1.Wire());
274 if (!aCurve2.IsEmpty())
276 theWires.push_back(aCurve2.Wire());
280 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
281 const QString& theNamePrefix,
282 const std::vector<TopoDS_Shape>& theShapes,
285 if( theDoc.IsNull() )
288 int n = theShapes.size();
290 for( int i=0; i<n; i++ )
292 Handle( HYDROData_PolylineXY ) aPolyline =
293 Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
294 if( aPolyline.IsNull() )
297 aPolyline->SetShape( theShapes[i] );
301 QString aNewName = theNamePrefix + "_" + QString::number( anIndex );
302 if( theDoc->FindObjectByName( aNewName ).IsNull() ) // the object with such a name is not found
303 aPolyline->SetName( aNewName );
308 aPolyline->SetName( theNamePrefix );
314 double HYDROData_PolylineOperator::ReduceDeflection(
315 const double theDeflection,
316 HYDROData_TopoCurve& theCurve,
319 // Construct the approximating B-spline.
320 std::list<gp_XYZ> aPs;
321 if (!theCurve.ValuesInKnots(aPs))
326 Handle(TColgp_HArray1OfPnt) aPs2 = new TColgp_HArray1OfPnt(1, aPs.size());
328 std::list<gp_XYZ>::const_iterator aLastPIt = aPs.end();
329 std::list<gp_XYZ>::const_iterator aPIt = aPs.begin();
330 for (int aPN = 1; aPIt != aLastPIt; ++aPN, ++aPIt)
332 aPs2->SetValue(aPN, *aPIt);
335 Handle(Geom_BSplineCurve) aBSpline2;
336 const bool isClosed = theCurve.IsClosed();
338 if (!CurveCreator_Utils::constructBSpline(aPs2, isClosed, aBSpline2))
344 // Calculate the piece deflections.
345 std::deque<double> aSqDefls;
346 double aMaxSqDefl = 0;
347 std::list<TopoDS_Edge>& aEdges = theCurve.Edges();
348 std::list<TopoDS_Edge>::const_iterator aLastEIt = aEdges.end();
350 std::list<TopoDS_Edge>::const_iterator aEIt = aEdges.begin();
351 for (int aPrevKCount = 0; aEIt != aLastEIt; ++aEIt)
353 TopLoc_Location aLoc;
355 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
356 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
357 const int aKCount = aBSpline->NbKnots();
358 for (int aKN = 1; aKN < aKCount; ++aKN)
360 const double aParam =
361 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5;
362 const double aParam2 = (aBSpline2->Knot(aPrevKCount + aKN) +
363 aBSpline2->Knot(aPrevKCount + aKN + 1)) * 0.5;
364 const double aSqDefl = Abs(aBSpline->Value(aParam).
365 SquareDistance(aBSpline2->Value(aParam2)));
366 aSqDefls.push_back(aSqDefl);
367 if (aMaxSqDefl < aSqDefl)
369 aMaxSqDefl = aSqDefl;
372 aPrevKCount += aKCount - 1;
376 // Check whether the reducing is necessary.
377 const double aMaxDefl = Sqrt(aMaxSqDefl);
378 if (aMaxDefl <= theDeflection)
383 // Reduce the deflections.
384 const double aThresSqDefl =
385 Max(aMaxSqDefl * 0.25, theDeflection * theDeflection);
386 std::list<TopoDS_Edge>::iterator aEIt = aEdges.begin();
387 std::deque<double>::const_iterator aSqDIt = aSqDefls.begin();
389 for (; aEIt != aLastEIt; ++aEIt)
391 TopLoc_Location aLoc;
393 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
394 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
395 Handle(Geom_BSplineCurve) aBSpline2 =
396 Handle(Geom_BSplineCurve)::DownCast(aBSpline->Copy());
397 const int aKCount = aBSpline->NbKnots();
398 for (int aKN = 1; aKN < aKCount; ++aSqDIt, ++aKN)
400 if (*aSqDIt > aThresSqDefl)
402 aBSpline2->InsertKnot(
403 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5);
407 BRep_Builder().MakeEdge(aEdge, aBSpline2, Precision::Confusion());
408 BRep_Builder().Add(aEdge, TopExp::FirstVertex(*aEIt));
409 BRep_Builder().Add(aEdge, TopExp::LastVertex(*aEIt));
410 thePieceCount += aBSpline2->NbKnots() - 1;