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_TopoCurve.h>
21 #include <Approx_Curve3d.hxx>
22 #include <BRep_Builder.hxx>
23 #include <BRepAdaptor_Curve.hxx>
24 #include <BRepAdaptor_HCurve.hxx>
25 #include <BRepBuilderAPI_MakeEdge.hxx>
26 #include <Extrema_ExtCC.hxx>
27 #include <Extrema_ExtPC.hxx>
28 #include <GeomAPI_Interpolate.hxx>
29 #include <Geom_BSplineCurve.hxx>
30 #include <Precision.hxx>
31 #include <ShapeAnalysis_TransferParametersProj.hxx>
32 #include <ShapeBuild_Edge.hxx>
33 #include <TColgp_Array1OfVec.hxx>
34 #include <TColgp_HArray1OfPnt.hxx>
35 #include <TColStd_HArray1OfBoolean.hxx>
36 #include <TopExp_Explorer.hxx>
38 #include <TopoDS_Wire.hxx>
39 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
40 #include <TopTools_ListOfShape.hxx>
43 #include "HYDRO_trace.hxx"
44 #include <BRepTools.hxx>
46 //! The type is intended to traverse the container
47 //! either from the begin to the end or vice versa.
48 template<typename ContainerType, typename IteratorType>
52 IteratorType myIterator; //!< The iterator.
53 IteratorType myIteratorLimit; //!< The iterator limit.
55 //! The pointer to the method to traverse the next item.
56 IteratorType& (IteratorType::*myNext)();
61 const ContainerType& theContainer,
62 const bool theIsForward)
66 myIterator = theContainer.begin();
67 myIteratorLimit = theContainer.end();
68 myNext = &IteratorType::operator++;
72 myIterator = --theContainer.end();
73 myIteratorLimit = --theContainer.begin();
74 myNext = &IteratorType::operator--;
78 //! Returna 'true' if the container contains not yet traversed item.
81 return myIterator != myIteratorLimit;
84 //! Traverses to the next item.
85 IteratorType& operator ++()
87 return (myIterator.*myNext)();
90 //! Returns the iterator.
91 IteratorType& operator *() {return myIterator;}
94 // Inserts the value after the position.
95 template<typename ItemType> static void InsertAfter(
96 const typename std::list<ItemType>::iterator& thePosition,
97 const ItemType& theValue,
98 std::list<ItemType>& theList)
100 typename std::list<ItemType>::iterator aEIt2 = thePosition;
101 if (++aEIt2 != theList.end())
103 theList.insert(aEIt2, theValue);
107 theList.push_back(theValue);
111 // Converts the curve to a smooth cubic B-spline using the deflection.
112 static Handle(Geom_BSplineCurve) BSpline(
113 const BRepAdaptor_Curve& theCurve, const double theDeflection)
115 Handle(BRepAdaptor_HCurve) aCurve = new BRepAdaptor_HCurve(theCurve);
116 Approx_Curve3d aConverter(aCurve, theDeflection, GeomAbs_C1, 4, 3);
117 Handle(Geom_BSplineCurve) aBSpline;
118 return aConverter.HasResult() ? aConverter.Curve() : aBSpline;
121 // Replaces the vertex of the edge considering the edge orientation.
122 static TopoDS_Edge ReplaceVertex(
123 const TopoDS_Edge& theEdge, const bool theIsEndVertex)
125 TopoDS_Vertex aVertices[] = {
126 TopExp::FirstVertex(theEdge, Standard_True),
127 TopExp::LastVertex(theEdge, Standard_True)};
128 aVertices[theIsEndVertex ? 1 : 0].EmptyCopy();
129 TopoDS_Edge aNewEdge = TopoDS::Edge(theEdge.Oriented(TopAbs_FORWARD));
131 ShapeBuild_Edge().CopyReplaceVertices(aNewEdge, aVertices[0], aVertices[1]);
132 aNewEdge.Orientation(theEdge.Orientation());
136 // Projects the point to the curve.
137 double ProjectPointToCurve(
138 const gp_XYZ& thePoint,
139 const Adaptor3d_Curve& theCurve,
140 double& theParameter)
142 // Calculate the nearest curve internal extremum.
143 Extrema_ExtPC aAlgo(thePoint, theCurve);
145 double aMinSqDist = DBL_MAX;
148 const int aECount = aAlgo.NbExt();
149 for (int aEN = 1; aEN <= aECount; ++aEN)
151 const gp_XYZ& aP = aAlgo.Point(aEN).Value().XYZ();
152 const double aSqDist = (thePoint - aP).SquareModulus();
153 if (aMinSqDist > aSqDist)
155 aMinSqDist = aSqDist;
161 // Calculate the nearest curve end extremum.
162 const double aParams[] =
163 {theCurve.FirstParameter(), theCurve.LastParameter()};
164 const gp_XYZ aEnds[] =
165 {theCurve.Value(aParams[0]).XYZ(), theCurve.Value(aParams[1]).XYZ()};
166 const double aSqDists[] = {
167 (thePoint - aEnds[0]).SquareModulus(),
168 (thePoint - aEnds[1]).SquareModulus()};
169 for (int aEI = 0; aEI < 2; ++aEI)
171 if (aMinSqDist > aSqDists[aEI])
173 aMinSqDist = aSqDists[aEI];
180 theParameter = aParams[-aMinEN];
184 const Extrema_POnCurv& aPOnC = aAlgo.Point(aMinEN);
185 const gp_XYZ& aP = aPOnC.Value().XYZ();
186 theParameter = aPOnC.Parameter();
187 for (int aEI = 0; aEI < 2; ++aEI)
189 if (Abs(theParameter - aParams[aEI]) < Precision::PConfusion() ||
190 (aP - aEnds[aEI]).SquareModulus() < Precision::SquareConfusion())
192 theParameter = aParams[aEI];
198 // Projects the point to the edge.
199 static double ProjectPointToEdge(
200 const gp_XYZ& thePoint, const TopoDS_Edge& theEdge, double& theParameter)
202 return ProjectPointToCurve(thePoint, BRepAdaptor_Curve(theEdge), theParameter);
205 // Adds the parameter to the curve parameter list.
206 static int AddParameter(
207 const Adaptor3d_Curve& theCurve,
208 const double theParameter,
209 std::list<double>& theParameters)
211 // Check the coincidence.
212 std::list<double> aEndParams;
213 aEndParams.push_back(theCurve.FirstParameter());
214 aEndParams.push_back(theCurve.LastParameter());
215 std::list<double>* aParams[] = {&theParameters, &aEndParams};
216 const gp_XYZ aPoint = theCurve.Value(theParameter).XYZ();
217 for (int aLI = 0; aLI < 2; ++aLI)
219 std::list<double>::iterator aPIt = aParams[aLI]->begin();
220 std::list<double>::iterator aLastPIt = aParams[aLI]->end();
221 for (int aPI = 0; aPIt != aLastPIt; ++aPI, ++aPIt)
223 const double aParam = *aPIt;
224 if (Abs(theParameter - aParam) < Precision::PConfusion() ||
225 (theCurve.Value(aParam).XYZ() - aPoint).SquareModulus() <=
226 Precision::SquareConfusion())
233 theParameters.push_front(aEndParams.front());
237 theParameters.push_back(aEndParams.back());
246 // Calculate the position to insert.
247 std::list<double>::iterator aPIt = theParameters.begin();
248 std::list<double>::iterator aLastPIt = theParameters.end();
249 if (aPIt != aLastPIt && *aPIt < theParameter)
251 for (++aPIt; aPIt != aLastPIt && *aPIt < theParameter; ++aPIt);
252 if (aPIt != aLastPIt)
254 theParameters.insert(aPIt, theParameter);
258 theParameters.push_back(theParameter);
263 theParameters.push_front(theParameter);
268 // Intersects the first curve by the second one and
269 // adds the intersection parameters to the ordered list.
270 static int IntersectCurve(
271 const Adaptor3d_Curve& theCurve1,
272 const Adaptor3d_Curve& theCurve2,
273 std::list<double>& theParameters)
277 const gp_XYZ aEndPs[] = {
278 theCurve2.Value(theCurve2.FirstParameter()).XYZ(),
279 theCurve2.Value(theCurve2.LastParameter()).XYZ()};
280 for (int aPI = 0; aPI < 2; ++aPI)
283 if (ProjectPointToCurve(aEndPs[aPI], theCurve1, aParameter) <=
284 Precision::SquareConfusion())
286 DEBTRACE("aParameter " << aParameter);
287 aIntCount += AddParameter(theCurve1, aParameter, theParameters);
291 // Process the internal extremums.
292 Extrema_ExtCC aAlgo(theCurve1, theCurve2); //, 1.e-6, 1.e-6);
295 const int aECount = aAlgo.NbExt();
296 for (int aEN = 1; aEN <= aECount; ++aEN)
298 Extrema_POnCurv aP1, aP2;
299 aAlgo.Points(aEN, aP1, aP2);
300 DEBTRACE("SquareDistance " << aP1.Value().SquareDistance(aP2.Value()));
301 if (aP1.Value().SquareDistance(aP2.Value()) <=
302 Precision::SquareConfusion())
304 DEBTRACE("aP1.Parameter() " << aP1.Parameter());
305 aIntCount += AddParameter(theCurve1, aP1.Parameter(), theParameters);
312 // Intersects the first edge by the second one and
313 // adds the intersection parameters to the ordered list.
314 static int IntersectEdge(
315 const TopoDS_Edge& theEdge1,
316 const TopoDS_Edge& theEdge2,
317 std::list<double>& theParameters)
319 BRepAdaptor_Curve aCurve1 = BRepAdaptor_Curve(theEdge1);
320 BRepAdaptor_Curve aCurve2 = BRepAdaptor_Curve(theEdge2);
321 return IntersectCurve(aCurve1, aCurve2, theParameters);
324 // Returns the curve tangent in the position: 0 - start, 1 - end.
325 static gp_XYZ Tangent(const Adaptor3d_Curve& theCurve, const int thePosition)
327 const Standard_Real aParam = (thePosition == 0) ?
328 theCurve.FirstParameter() : theCurve.LastParameter();
331 theCurve.D1(aParam, aP, aV);
332 Standard_Real aNorm = aV.Magnitude();
333 aNorm = (aNorm >= Precision::PConfusion()) ? aNorm : 0;
334 return ((1 / aNorm) * aV).XYZ();
337 // Returns the edge tangent in the position: 0 - start, 1 - end.
338 static gp_XYZ Tangent(const TopoDS_Edge& theEdge, const int thePosition)
340 BRepAdaptor_Curve aCurve(theEdge);
341 return Tangent(BRepAdaptor_Curve(theEdge), thePosition);
344 static bool Interpolate(
345 const gp_XYZ thePoint1,
346 const gp_XYZ thePoint2,
347 const gp_XYZ theTangent1,
348 const gp_XYZ theTangent2,
349 Handle(Geom_BSplineCurve)& theBSpline)
351 Handle(TColgp_HArray1OfPnt) aPs = new TColgp_HArray1OfPnt(1, 2);
352 TColgp_Array1OfVec aTs(1, 2);
353 Handle(TColStd_HArray1OfBoolean) aTFs = new TColStd_HArray1OfBoolean(1, 2);
354 aPs->SetValue(1, thePoint1);
355 aPs->SetValue(2, thePoint2);
356 aTs.SetValue(1, theTangent1);
357 aTs.SetValue(2, theTangent2);
358 aTFs->SetValue(1, Standard_True);
359 aTFs->SetValue(2, Standard_True);
360 GeomAPI_Interpolate aInterpolator(aPs, Standard_False, 0);
361 aInterpolator.Load(aTs, aTFs, Standard_False);
362 aInterpolator.Perform();
363 const bool aResult = (aInterpolator.IsDone() == Standard_True);
366 theBSpline = aInterpolator.Curve();
371 bool HYDROData_TopoCurve::Initialize(const TopoDS_Wire& theWire)
373 // Check for non-emptiness.
375 TopTools_IndexedDataMapOfShapeListOfShape aVertexToEdges;
376 TopExp::MapShapesAndAncestors(theWire,
377 TopAbs_VERTEX, TopAbs_EDGE, aVertexToEdges);
378 const int aVCount = aVertexToEdges.Extent();
379 DEBTRACE("initialize VCount= "<< aVCount);
385 // Check for 1 manifoldness.
386 bool isClosed = false;
389 for (int aVN = 1; aVN <= aVCount; ++aVN)
391 const int aEdgeCount = aVertexToEdges.FindFromIndex(aVN).Extent();
401 isClosed = (aEndCount == 0);
402 if (!isClosed && aEndCount != 2)
412 for (; aVertexToEdges.FindFromIndex(aVN).Extent() == 2; ++aVN);
413 if (!aVertexToEdges.FindKey(aVN).IsEqual(TopExp::FirstVertex(
414 TopoDS::Edge(aVertexToEdges.FindFromIndex(aVN).First()), Standard_True)))
416 for (; aVertexToEdges.FindFromIndex(aVN).Extent() == 2; ++aVN);
421 TopTools_ListOfShape& aEdges = aVertexToEdges.ChangeFromIndex(1);
422 if (!aVertexToEdges.FindKey(aVN).IsEqual(
423 TopExp::FirstVertex(TopoDS::Edge(aEdges.First()), Standard_True)))
425 const TopoDS_Shape aEdge = aEdges.First();
426 aEdges.First() = aEdges.Last();
427 aEdges.Last() = aEdge;
431 // Calculate the edge order.
432 TopTools_ListOfShape* aEdges = &aVertexToEdges.ChangeFromIndex(aVN);
433 while (!aEdges->IsEmpty())
435 const TopoDS_Edge aEdge = TopoDS::Edge(aEdges->First());
436 aEdges->RemoveFirst();
437 myEdges.push_back(aEdge);
438 int aVN2 = aVertexToEdges.FindIndex(TopExp::FirstVertex(aEdge));
441 aVN2 = aVertexToEdges.FindIndex(TopExp::LastVertex(aEdge));
445 aEdges = &aVertexToEdges.ChangeFromIndex(aVN2);
446 const TopoDS_Edge aEdge2 = TopoDS::Edge(aEdges->First());
447 if (aEdge2.IsEqual(aEdge))
449 aEdges->RemoveFirst();
454 aEdges->Append(aEdge2);
458 // Check for connectedness and free vertex.
459 return aVCount - myEdges.size() == (isClosed ? 0 : 1);
462 TopoDS_Wire HYDROData_TopoCurve::Wire() const
465 BRep_Builder aBuilder;
466 aBuilder.MakeWire(aWire);
467 std::list<TopoDS_Edge>::const_iterator aEItLast = myEdges.end();
468 std::list<TopoDS_Edge>::const_iterator aEIt = myEdges.begin();
469 for (; aEIt != aEItLast; ++aEIt)
471 aBuilder.Add(aWire, *aEIt);
476 bool HYDROData_TopoCurve::Cut(
477 const std::list<TopoDS_Edge>::iterator& theEdgePosition,
478 const double theParameter,
479 HYDROData_TopoCurve& theCurve)
481 bool aResult = false;
484 std::list<TopoDS_Edge>::iterator aFirstEIt = myEdges.begin();
485 std::list<TopoDS_Edge>::iterator aEIt = aFirstEIt;
486 for (; aEIt != theEdgePosition; ++aEIt);
489 TopoDS_Edge aEdge = *aEIt;
490 BRepAdaptor_Curve aCurve(aEdge);
492 const double aEdgeEndParams[] =
493 {aCurve.FirstParameter(), aCurve.LastParameter()};
494 if (Abs(theParameter - aEdgeEndParams[0]) < Precision::PConfusion())
498 else if (Abs(theParameter - aEdgeEndParams[1]) < Precision::PConfusion())
502 const TopAbs_Orientation aOrient = aEdge.Orientation();
503 if (aOrient == TopAbs_REVERSED)
507 const bool isClosed = IsClosed();
508 DEBTRACE("aParamI: " << aParamI << " isClosed: "<< isClosed);
511 aEdge.Orientation(TopAbs_FORWARD);
512 TopoDS_Vertex aSplitV1, aSplitV2;
513 BRep_Builder().MakeVertex(
514 aSplitV1, aCurve.Value(theParameter), Precision::Confusion());
515 BRep_Builder().MakeVertex(
516 aSplitV2, aCurve.Value(theParameter), Precision::Confusion());
517 TopoDS_Edge aEParts[] = {
518 ShapeBuild_Edge().CopyReplaceVertices(aEdge, TopoDS_Vertex(),
519 TopoDS::Vertex(aSplitV1.Oriented(TopAbs_REVERSED))),
520 ShapeBuild_Edge().CopyReplaceVertices(aEdge, aSplitV2, TopoDS_Vertex())};
521 ShapeBuild_Edge().CopyPCurves(aEParts[0], aEdge);
522 ShapeBuild_Edge().CopyPCurves(aEParts[1], aEdge);
523 BRep_Builder().SameRange(aEParts[0], Standard_False);
524 BRep_Builder().SameRange(aEParts[1], Standard_False);
525 BRep_Builder().SameParameter(aEParts[0], Standard_False);
526 BRep_Builder().SameParameter(aEParts[1], Standard_False);
527 ShapeAnalysis_TransferParametersProj aSATPP(aEdge, TopoDS_Face());
528 aSATPP.SetMaxTolerance(Precision::Confusion());
529 aSATPP.TransferRange(aEParts[0],
530 aEdgeEndParams[0], theParameter, Standard_False);
531 aSATPP.TransferRange(aEParts[1],
532 theParameter, aEdgeEndParams[1], Standard_False);
533 aEParts[0].Orientation(aOrient);
534 aEParts[1].Orientation(aOrient);
536 const int aFirstPI = (aOrient != TopAbs_REVERSED) ? 0 : 1;
537 *aEIt = aEParts[aFirstPI];
538 InsertAfter(aEIt, aEParts[1 - aFirstPI], myEdges);
545 TopoDS_Edge aNewEdge = ReplaceVertex(aEdge, (aParamI == 0) ? false : true);
551 std::list<TopoDS_Edge>::iterator aEdgePosition = theEdgePosition;
552 if (isClosed || ++aEdgePosition != myEdges.end())
559 if (isClosed || theEdgePosition != aFirstEIt)
566 // Calculate the curve parts.
567 std::list<TopoDS_Edge>::iterator aLastEIt = myEdges.end();
568 if (aEIt != aFirstEIt && aEIt != aLastEIt)
570 std::list<TopoDS_Edge>* aEdges = !isClosed ? &theCurve.myEdges : &myEdges;
571 aEdges->splice(aEdges->begin(), myEdges, aEIt, aLastEIt);
577 void HYDROData_TopoCurve::Cut(
578 const std::list<TopoDS_Edge>::const_iterator& theEdgePosition,
579 const double theParameter,
580 HYDROData_TopoCurve& theCurve1,
581 HYDROData_TopoCurve& theCurve2) const
584 std::list<TopoDS_Edge>::const_iterator aEPos = myEdges.begin();
585 std::list<TopoDS_Edge>::iterator aEPos1 = theCurve1.myEdges.begin();
586 for (; aEPos != theEdgePosition; ++aEPos1, ++aEPos);
587 theCurve1.Cut(aEPos1, theParameter, theCurve2);
590 bool HYDROData_TopoCurve::Cut(
591 const std::deque<std::list<double> >& theParameters,
592 std::deque<HYDROData_TopoCurve>& theCurves) const
594 bool aResult = false;
595 HYDROData_TopoCurve aCurves[2];
598 std::list<TopoDS_Edge>::iterator aEIt = aCurves[0].myEdges.begin();
599 std::deque<std::list<double> >::const_iterator aPLIt = theParameters.begin();
600 for (std::deque<std::list<double> >::const_iterator aLastPLIt =
601 theParameters.end(); aPLIt != aLastPLIt; ++aPLIt)
603 TopoDS_Edge aNextEdge;
605 std::list<TopoDS_Edge>::iterator aNextEIt = aEIt;
607 if (aNextEIt != aCurves[aCI].myEdges.end())
609 aNextEdge = *aNextEIt;
613 for (Iterator<std::list<double>, std::list<double>::const_iterator> aPIt(
614 *aPLIt, (aEIt->Orientation() != TopAbs_REVERSED)); aPIt.More(); ++aPIt)
616 const int aCI1 = 1 - aCI;
617 aResult |= aCurves[aCI].Cut(aEIt, **aPIt, aCurves[aCI1]);
618 if (!aCurves[aCI1].IsEmpty())
620 theCurves.push_back(HYDROData_TopoCurve());
621 theCurves.back().append(aCurves[aCI]);
622 aEIt = aCurves[aCI1].myEdges.begin();
627 aEIt = aCurves[aCI].myEdges.begin();
631 if (!aNextEdge.IsNull() && !aEIt->IsEqual(aNextEdge))
636 theCurves.push_back(aCurves[aCI]);
640 double HYDROData_TopoCurve::Project(
641 const gp_XYZ& thePoint,
642 std::list<TopoDS_Edge>::const_iterator& theEdgeIterator,
643 double& theParameter) const
645 double aMinSqDist = DBL_MAX;
646 std::list<TopoDS_Edge>::const_iterator aEIt = myEdges.begin();
647 std::list<TopoDS_Edge>::const_iterator aLastEIt = myEdges.end();
648 for (; aEIt != aLastEIt; ++aEIt)
651 const double aSqDist = ProjectPointToEdge(thePoint, *aEIt, aParam);
652 if (aMinSqDist > aSqDist)
654 aMinSqDist = aSqDist;
655 theEdgeIterator = aEIt;
656 theParameter = aParam;
662 int HYDROData_TopoCurve::Intersect(
663 const TopoDS_Wire& theWire,
664 std::deque<std::list<double> >& theParameters) const
666 std::string brepName = "theWireToIntersect";
668 BRepTools::Write( theWire, brepName.c_str() );
671 theParameters.resize(myEdges.size());
672 DEBTRACE("myEdges.size() " << myEdges.size());
673 std::list<TopoDS_Edge>::const_iterator aEIt = myEdges.begin();
674 std::list<TopoDS_Edge>::const_iterator aLastEIt = myEdges.end();
675 std::deque<std::list<double> >::iterator aPIt = theParameters.begin();
676 for (; aEIt != aLastEIt; ++aPIt, ++aEIt)
679 const TopoDS_Edge& aEdge = *aEIt;
680 std::list<double>& aParams = *aPIt;
681 TopExp_Explorer aEIt2(theWire, TopAbs_EDGE);
682 for (; aEIt2.More(); aEIt2.Next())
684 aIntCount += IntersectEdge(aEdge,TopoDS::Edge(aEIt2.Current()), aParams);
685 DEBTRACE("aParams.size() " << aParams.size());
688 DEBTRACE("aIntCount " << aIntCount);
692 void HYDROData_TopoCurve::CloseCurve()
694 const TopoDS_Vertex aVertex = TopoDS::Vertex(TopExp::LastVertex(
695 myEdges.back(), Standard_True).Oriented(TopAbs_FORWARD));
696 TopoDS_Edge& aEdge = myEdges.front();
697 const TopoDS_Edge aForwardEdge = TopoDS::Edge(aEdge.Oriented(TopAbs_FORWARD));
698 aEdge = TopoDS::Edge(ShapeBuild_Edge().CopyReplaceVertices(
699 aForwardEdge, aVertex, TopoDS_Vertex()).Oriented(aEdge.Orientation()));
702 void HYDROData_TopoCurve::Merge(
703 const int thePosition, HYDROData_TopoCurve& theCurve)
705 if (thePosition == 0)
707 const TopoDS_Vertex aVertex = TopoDS::Vertex(TopExp::LastVertex(
708 theCurve.myEdges.back(), Standard_True).Oriented(TopAbs_FORWARD));
709 TopoDS_Edge& aEdge = myEdges.front();
710 aEdge = TopoDS::Edge(ShapeBuild_Edge().CopyReplaceVertices(
711 TopoDS::Edge(aEdge.Oriented(TopAbs_FORWARD)), aVertex, TopoDS_Vertex()).
712 Oriented(aEdge.Orientation()));
717 const TopoDS_Vertex aVertex = TopoDS::Vertex(TopExp::FirstVertex(
718 theCurve.myEdges.front(), Standard_True).Oriented(TopAbs_REVERSED));
719 TopoDS_Edge& aEdge = myEdges.back();
720 aEdge = TopoDS::Edge(ShapeBuild_Edge().CopyReplaceVertices(
721 TopoDS::Edge(aEdge.Oriented(TopAbs_FORWARD)), TopoDS_Vertex(), aVertex).
722 Oriented(aEdge.Orientation()));
727 void HYDROData_TopoCurve::Merge(
728 const double theTolerance, std::deque<HYDROData_TopoCurve>& theCurves)
730 // Process the curve closeness.
731 const double aSqTol = theTolerance * theTolerance;
732 const gp_XYZ aPs[] = {
733 BRep_Tool::Pnt(TopExp::FirstVertex(myEdges.front(), Standard_True)).XYZ(),
734 BRep_Tool::Pnt(TopExp::LastVertex(myEdges.back(), Standard_True)).XYZ()};
735 bool isClosed = IsClosed();
736 if (!isClosed && (aPs[0] - aPs[1]).SquareModulus() <= aSqTol)
742 // Find the merge places.
743 HYDROData_TopoCurve* aCurves[] = {NULL, NULL};
747 std::deque<HYDROData_TopoCurve>::iterator aCIt = theCurves.begin();
748 std::deque<HYDROData_TopoCurve>::iterator aLastCIt = theCurves.end();
749 for (; aCIt != aLastCIt; ++aCIt)
751 HYDROData_TopoCurve& aCurve = *aCIt;
752 if (aCurve.IsEmpty() || aCurve.IsClosed())
757 const gp_XYZ aP1 = BRep_Tool::Pnt(
758 TopExp::FirstVertex(aCurve.myEdges.front(), Standard_True)).XYZ();
759 if (aCurves[0] == NULL && (aPs[1] - aP1).SquareModulus() <= aSqTol)
761 aCurves[0] = &aCurve;
764 const gp_XYZ aP2 = BRep_Tool::Pnt(
765 TopExp::LastVertex(aCurve.myEdges.back(), Standard_True)).XYZ();
766 if (aCurves[1] == NULL && (aPs[0] - aP2).SquareModulus() <= aSqTol)
768 aCurves[1] = &aCurve;
769 aOrder = (aCurves[0] == NULL) ? 1 : 0;
774 if (aCurves[0] == NULL && aCurves[1] == NULL)
776 theCurves.push_back(HYDROData_TopoCurve());
777 theCurves.back().append(*this);
779 else if (aCurves[1] == NULL)
781 aCurves[0]->Merge(0, *this);
783 else if (aCurves[0] == NULL)
785 aCurves[1]->Merge(1, *this);
789 aCurves[aOrder]->Merge(aOrder, *this);
790 if (aCurves[0] != aCurves[1])
792 aCurves[aOrder]->Merge(aOrder, *aCurves[1 - aOrder]);
796 aCurves[aOrder]->CloseCurve();
801 bool HYDROData_TopoCurve::Connect(
802 const Standard_Real theTolerance, std::deque<HYDROData_TopoCurve>& theCurves)
804 const double aSqTol = theTolerance * theTolerance;
805 const gp_XYZ aPs[] = {
806 BRep_Tool::Pnt(TopExp::FirstVertex(myEdges.front(), Standard_True)).XYZ(),
807 BRep_Tool::Pnt(TopExp::LastVertex(myEdges.back(), Standard_True)).XYZ()};
808 bool isClosed = IsClosed();
809 if (!isClosed && (aPs[0] - aPs[1]).SquareModulus() <= aSqTol)
817 std::deque<HYDROData_TopoCurve>::iterator aCIt = theCurves.begin();
818 std::deque<HYDROData_TopoCurve>::iterator aLastCIt = theCurves.end();
819 for (; aCIt != aLastCIt; ++aCIt)
821 HYDROData_TopoCurve& aCurve2 = *aCIt;
822 if (aCurve2.IsEmpty() || aCurve2.IsClosed())
827 const TopoDS_Edge* aEdges2[] =
828 {&aCurve2.myEdges.front(), &aCurve2.myEdges.back()};
829 const gp_XYZ aPs2[] = {
830 BRep_Tool::Pnt(TopExp::FirstVertex(*aEdges2[0], Standard_True)).XYZ(),
831 BRep_Tool::Pnt(TopExp::LastVertex(*aEdges2[1], Standard_True)).XYZ()};
832 const double aSqDists[] =
833 {(aPs[1] - aPs2[0]).SquareModulus(), (aPs[0] - aPs2[1]).SquareModulus()};
834 const int aOrder = (aSqDists[0] <= aSqDists[1]) ? 0 : 1;
835 if (aSqDists[aOrder] > aSqTol)
837 const TopoDS_Edge& aEdge =
838 (aOrder == 0) ? myEdges.back() : myEdges.front();
839 const gp_XYZ aPs3[] = {aPs[1 - aOrder], aPs2[aOrder]};
841 {Tangent(aEdge, 1 - aOrder), Tangent(*aEdges2[aOrder], aOrder)};
842 Handle(Geom_BSplineCurve) aBSpline;
843 if (!Interpolate(aPs3[aOrder], aPs3[1 - aOrder],
844 aTs[aOrder], aTs[1 - aOrder], aBSpline))
849 HYDROData_TopoCurve aECurve = BRepBuilderAPI_MakeEdge(aBSpline).Edge();
850 aCurve2.Merge(aOrder, aECurve);
852 aCurve2.Merge(aOrder, *this);
853 if (aSqDists[1 - aOrder] <= aSqTol)
855 aCurve2.CloseCurve();
861 theCurves.push_back(HYDROData_TopoCurve());
862 theCurves.back().append(*this);
866 int HYDROData_TopoCurve::BSplinePiecewiseCurve(
867 const double theDeflection, HYDROData_TopoCurve& theCurve) const
870 std::list<TopoDS_Edge>::const_iterator aLastEIt = myEdges.end();
871 std::list<TopoDS_Edge>::const_iterator aEIt = myEdges.begin();
872 TopoDS_Vertex aEndVertex;
873 TopoDS_Edge aPrevEdge;
874 for (; aEIt != aLastEIt; ++aEIt)
876 Handle(Geom_BSplineCurve) aBSpline =
877 ::BSpline(BRepAdaptor_Curve(*aEIt), theDeflection);
878 if (aBSpline.IsNull())
883 if (aEIt->Orientation() == TopAbs_REVERSED)
889 BRep_Builder().MakeEdge(aEdge, aBSpline, Precision::Confusion());
890 TopoDS_Vertex aVertex;
891 BRep_Builder().MakeVertex(aVertex,
892 aBSpline->Value(aBSpline->FirstParameter()), Precision::Confusion());
893 if (!aPrevEdge.IsNull())
895 BRep_Builder().Add(aPrevEdge, aVertex.Oriented(TopAbs_REVERSED));
899 aEndVertex = aVertex;
901 BRep_Builder().Add(aEdge, aVertex);
902 theCurve.myEdges.push_back(aEdge);
903 aPieceCount += aBSpline->NbKnots() - 1;
909 BRepAdaptor_Curve aCurve(aPrevEdge);
910 BRep_Builder().MakeVertex(aEndVertex,
911 aCurve.Value(aCurve.LastParameter()), Precision::Confusion());
913 BRep_Builder().Add(aPrevEdge, aEndVertex.Oriented(TopAbs_REVERSED));
917 bool HYDROData_TopoCurve::ValuesInKnots(std::list<gp_XYZ>& theValues) const
919 std::list<TopoDS_Edge>::const_iterator aLastEIt = myEdges.end();
920 std::list<TopoDS_Edge>::const_iterator aEIt = myEdges.begin();
921 for (; aEIt != aLastEIt; ++aEIt)
923 Handle(Geom_BSplineCurve) aCurve;
925 TopLoc_Location aLoc;
927 aCurve = Handle(Geom_BSplineCurve)::
928 DownCast(BRep_Tool::Curve(*aEIt, aLoc, aParams[0], aParams[1]));
929 if (!aLoc.IsIdentity() || aEIt->Orientation() != TopAbs_FORWARD ||
936 for (int aNbKnots = aCurve->NbKnots(), aKN = 1; aKN < aNbKnots; ++aKN)
938 theValues.push_back(aCurve->Value(aCurve->Knot(aKN)).XYZ());
944 TopLoc_Location aLoc;
946 Handle(Geom_Curve) aCurve =
947 BRep_Tool::Curve(myEdges.back(), aLoc, aParams[0], aParams[1]);
948 theValues.push_back(aCurve->Value(aParams[1]).XYZ());