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>
23 #include <CurveCreator_Utils.hxx>
25 #include <BRepAdaptor_Curve.hxx>
26 #include <BRep_Builder.hxx>
27 #include <BRep_Tool.hxx>
28 #include <BRepBuilderAPI_MakeEdge2d.hxx>
29 #include <BRepBuilderAPI_MakeEdge.hxx>
30 #include <BRepBuilderAPI_MakeWire.hxx>
31 #include <Extrema_ExtCC.hxx>
32 #include <Extrema_ExtPC.hxx>
33 #include <GeomAPI_Interpolate.hxx>
34 #include <NCollection_Vector.hxx>
35 #include <Precision.hxx>
36 #include <ShapeAnalysis_TransferParametersProj.hxx>
37 #include <ShapeBuild_Edge.hxx>
38 #include <TColgp_Array1OfVec.hxx>
39 #include <TColgp_HArray1OfPnt.hxx>
40 #include <TColStd_HArray1OfBoolean.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <TopoDS_Wire.hxx>
45 #include <TopExp_Explorer.hxx>
46 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
49 template<class T> void append( std::vector<T>& theList, const std::vector<T>& theList2 )
51 int aSize = theList.size();
52 int aNewSize = aSize + theList2.size();
57 theList.resize( aNewSize );
58 for( int i=aSize, j=0; i<aNewSize; i++, j++ )
59 theList[i] = theList2[j];
62 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
63 const Handle( HYDROData_PolylineXY )& thePolyline,
64 const gp_Pnt2d& thePoint,
65 double theTolerance ) const
67 std::vector<gp_Pnt2d> aPointsList( 1 );
68 aPointsList[0] = thePoint;
69 std::vector<TopoDS_Wire> aCurves;
70 GetWires(thePolyline, aCurves);
72 for( int i=0, n=aCurves.size(); i<n; i++ )
74 std::vector<TopoDS_Shape> aCurvesList;
75 Split(aCurves[i], thePoint, theTolerance, aCurvesList);
76 bool isLocalOK = CreatePolylines( theDoc, thePolyline->GetName(), aCurvesList, true );
77 isOK = isOK && isLocalOK;
82 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
83 const Handle( HYDROData_PolylineXY )& thePolyline,
84 const Handle( HYDROData_PolylineXY )& theTool,
85 double theTolerance ) const
87 HYDROData_SequenceOfObjects aSeq;
88 aSeq.Append( theTool );
89 return split( theDoc, thePolyline, aSeq, theTolerance, -1 );
92 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
93 const HYDROData_SequenceOfObjects& thePolylines,
96 int f = thePolylines.Lower(), l = thePolylines.Upper();
97 for( int i=f; i<=l; i++ )
99 Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
100 if( !split( theDoc, aPolyline, thePolylines, theTolerance, i ) )
106 bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theDoc,
107 const QString& theName,
108 const HYDROData_SequenceOfObjects& thePolylines,
109 bool isConnectByNewSegment,
110 double theTolerance )
112 std::deque<HYDROData_TopoCurve> aMergedCurves;
113 HYDROData_SequenceOfObjects::Iterator aPIt(thePolylines);
114 for (; aPIt.More(); aPIt.Next())
116 Handle(HYDROData_PolylineXY) aPolyline =
117 Handle(HYDROData_PolylineXY)::DownCast(aPIt.Value());
118 std::vector<TopoDS_Wire> aWires;
119 GetWires(aPolyline, aWires);
120 for (std::vector<TopoDS_Wire>::const_iterator aWIt = aWires.begin(),
121 aLastWIt = aWires.end(); aWIt != aLastWIt; ++aWIt)
123 const Standard_Boolean aResult = !isConnectByNewSegment ?
124 HYDROData_TopoCurve::Merge(theTolerance, *aWIt, aMergedCurves) :
125 HYDROData_TopoCurve::Connect(theTolerance, *aWIt, aMergedCurves);
133 TopoDS_Compound aWireSet;
134 BRep_Builder aBuilder;
135 aBuilder.MakeCompound(aWireSet);
136 std::deque<HYDROData_TopoCurve>::iterator aCIt = aMergedCurves.begin();
137 std::deque<HYDROData_TopoCurve>::iterator aLastCIt = aMergedCurves.end();
138 for (; aCIt != aLastCIt; ++aCIt)
140 if (!aCIt->IsEmpty())
142 aBuilder.Add(aWireSet, aCIt->Wire());
146 std::vector<TopoDS_Shape> aPolylines(1);
147 aPolylines[0] = aWireSet;
148 CreatePolylines(theDoc, theName, aPolylines, false);
152 bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theDoc,
153 const Handle( HYDROData_PolylineXY )& thePolyline,
154 const HYDROData_SequenceOfObjects& theTools,
156 int theIgnoreIndex ) const
158 std::vector<TopoDS_Wire> aCurves;
159 GetWires(thePolyline, aCurves);
160 std::vector<TopoDS_Wire> aToolCurves;
161 for( int i=theTools.Lower(), n=theTools.Upper(); i<=n; i++ )
162 if( i!=theIgnoreIndex )
164 Handle( HYDROData_PolylineXY ) aToolPolyline =
165 Handle( HYDROData_PolylineXY )::DownCast( theTools.Value( i ) );
166 std::vector<TopoDS_Wire> aTCurves;
167 GetWires(aToolPolyline, aTCurves);
168 append( aToolCurves, aTCurves);
171 const int aPSCount = aCurves.size();
172 const int aTSCount = aToolCurves.size();
173 std::vector<TopoDS_Shape> aResult;
174 for (int aPSI = 0; aPSI < aPSCount; ++aPSI)
176 HYDROData_TopoCurve aCurve;
177 if (!aCurve.Initialize(aCurves[aPSI]))
182 std::deque<std::list<double> > aParams;
183 for (int aTSI = 0; aTSI < aTSCount; ++aTSI)
185 aCurve.Intersect(aToolCurves[aTSI], aParams);
188 std::deque<HYDROData_TopoCurve> aSplittedCurves;
189 aCurve.Cut(aParams, aSplittedCurves);
190 std::deque<HYDROData_TopoCurve>::const_iterator aCIt =
191 aSplittedCurves.begin();
192 std::deque<HYDROData_TopoCurve>::const_iterator aLastCIt =
193 aSplittedCurves.end();
194 for (; aCIt != aLastCIt; ++aCIt)
196 aResult.push_back(aCIt->Wire());
200 CreatePolylines(theDoc, thePolyline->GetName(), aResult, true);
204 void HYDROData_PolylineOperator::GetWires(
205 const Handle( HYDROData_PolylineXY )& thePolyline,
206 std::vector<TopoDS_Wire>& theWires)
208 TopoDS_Shape aShape = thePolyline->GetShape();
209 if( aShape.ShapeType()==TopAbs_WIRE )
211 theWires.push_back( TopoDS::Wire( aShape ) );
215 TopExp_Explorer anExp( aShape, TopAbs_WIRE );
216 for( ; anExp.More(); anExp.Next() )
218 theWires.push_back( TopoDS::Wire( anExp.Current() ) );
223 void HYDROData_PolylineOperator::Split(
224 const TopoDS_Wire& theWire,
225 const gp_Pnt2d& thePoint,
227 std::vector<TopoDS_Shape>& theWires)
229 HYDROData_TopoCurve aCurve;
230 if (!aCurve.Initialize(theWire))
232 theWires.push_back(theWire);
236 const gp_XYZ aP(thePoint.X(), thePoint.Y(), 0);
237 std::list<TopoDS_Edge>::const_iterator aEPos;
239 aCurve.Project(aP, aEPos, aParam);
240 HYDROData_TopoCurve aCurve1, aCurve2;
241 aCurve.Cut(aEPos, aParam, aCurve1, aCurve2);
242 theWires.push_back(aCurve1.Wire());
243 if (!aCurve2.IsEmpty())
245 theWires.push_back(aCurve2.Wire());
249 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
250 const QString& theNamePrefix,
251 const std::vector<TopoDS_Shape>& theShapes,
254 if( theDoc.IsNull() )
257 int n = theShapes.size();
259 for( int i=0; i<n; i++ )
261 Handle( HYDROData_PolylineXY ) aPolyline =
262 Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
263 if( aPolyline.IsNull() )
266 aPolyline->SetShape( theShapes[i] );
270 QString aNewName = theNamePrefix + "_" + QString::number( anIndex );
271 if( theDoc->FindObjectByName( aNewName ).IsNull() ) // the object with such a name is not found
272 aPolyline->SetName( aNewName );
277 aPolyline->SetName( theNamePrefix );
283 double HYDROData_PolylineOperator::ReduceDeflection(
284 const double theDeflection,
285 HYDROData_TopoCurve& theCurve,
288 // Construct the approximating B-spline.
289 std::list<gp_XYZ> aPs;
290 if (!theCurve.ValuesInKnots(aPs))
295 Handle(TColgp_HArray1OfPnt) aPs2 = new TColgp_HArray1OfPnt(1, aPs.size());
297 std::list<gp_XYZ>::const_iterator aLastPIt = aPs.end();
298 std::list<gp_XYZ>::const_iterator aPIt = aPs.begin();
299 for (int aPN = 1; aPIt != aLastPIt; ++aPN, ++aPIt)
301 aPs2->SetValue(aPN, *aPIt);
304 Handle(Geom_BSplineCurve) aBSpline2;
305 const bool isClosed = theCurve.IsClosed();
306 if (!CurveCreator_Utils::constructBSpline(aPs2, isClosed, aBSpline2))
311 // Calculate the piece deflections.
312 std::deque<double> aSqDefls;
313 double aMaxSqDefl = 0;
314 std::list<TopoDS_Edge>& aEdges = theCurve.Edges();
315 std::list<TopoDS_Edge>::const_iterator aLastEIt = aEdges.end();
317 std::list<TopoDS_Edge>::const_iterator aEIt = aEdges.begin();
318 for (int aPrevKCount = 0; aEIt != aLastEIt; ++aEIt)
320 TopLoc_Location aLoc;
322 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
323 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
324 const int aKCount = aBSpline->NbKnots();
325 for (int aKN = 1; aKN < aKCount; ++aKN)
327 const double aParam =
328 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5;
329 const double aParam2 = (aBSpline2->Knot(aPrevKCount + aKN) +
330 aBSpline2->Knot(aPrevKCount + aKN + 1)) * 0.5;
331 const double aSqDefl = Abs(aBSpline->Value(aParam).
332 SquareDistance(aBSpline2->Value(aParam2)));
333 aSqDefls.push_back(aSqDefl);
334 if (aMaxSqDefl < aSqDefl)
336 aMaxSqDefl = aSqDefl;
339 aPrevKCount += aKCount - 1;
343 // Check whether the reducing is necessary.
344 const double aMaxDefl = Sqrt(aMaxSqDefl);
345 if (aMaxDefl <= theDeflection)
350 // Reduce the deflections.
351 const double aThresSqDefl =
352 Max(aMaxSqDefl * 0.25, theDeflection * theDeflection);
353 std::list<TopoDS_Edge>::iterator aEIt = aEdges.begin();
354 std::deque<double>::const_iterator aSqDIt = aSqDefls.begin();
356 for (; aEIt != aLastEIt; ++aEIt)
358 TopLoc_Location aLoc;
360 Handle(Geom_BSplineCurve) aBSpline = Handle(Geom_BSplineCurve)::DownCast(
361 BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
362 Handle(Geom_BSplineCurve) aBSpline2 =
363 Handle(Geom_BSplineCurve)::DownCast(aBSpline->Copy());
364 const int aKCount = aBSpline->NbKnots();
365 for (int aKN = 1; aKN < aKCount; ++aSqDIt, ++aKN)
367 if (*aSqDIt > aThresSqDefl)
369 aBSpline2->InsertKnot(
370 (aBSpline->Knot(aKN) + aBSpline->Knot(aKN + 1)) * 0.5);
374 BRep_Builder().MakeEdge(aEdge, aBSpline2, Precision::Confusion());
375 BRep_Builder().Add(aEdge, TopExp::FirstVertex(*aEIt));
376 BRep_Builder().Add(aEdge, TopExp::LastVertex(*aEIt));
377 thePieceCount += aBSpline2->NbKnots() - 1;