1 // File: HYDROData_Pipes.cxx
2 // Created: 25.12.13 16:39:40
4 // Copyright: Open CASCADE 2013
6 #include <HYDROData_Pipes.h>
9 #include <TopoDS_Edge.hxx>
10 #include <TopoDS_Face.hxx>
11 #include <TopoDS_Iterator.hxx>
13 #include <BRepOffsetAPI_NormalProjection.hxx>
14 #include <BRepLib_MakeVertex.hxx>
15 #include <BRepLib_MakeEdge.hxx>
16 #include <BRepLib_MakeFace.hxx>
17 #include <BRepLib_MakeWire.hxx>
18 #include <BRepExtrema_ExtCC.hxx>
19 #include <BRepBuilderAPI_Transform.hxx>
20 #include <BRepOffsetAPI_MakePipeShell.hxx>
21 #include <BRep_Tool.hxx>
22 #include <Geom_Curve.hxx>
23 #include <Geom2d_Curve.hxx>
24 #include <Geom_Plane.hxx>
25 #include <TColStd_SequenceOfInteger.hxx>
26 #include <TColStd_ListOfReal.hxx>
27 #include <BRepAdaptor_Curve.hxx>
28 #include <GeomProjLib.hxx>
29 #include <Geom2dAPI_InterCurveCurve.hxx>
30 #include <Geom2dAPI_InterCurveCurve.hxx>
31 #include <Geom2d_Line.hxx>
32 #include <BRepLib_FindSurface.hxx>
33 #include <GProp_GProps.hxx>
34 #include <BRepGProp.hxx>
35 #include <BRepTools_WireExplorer.hxx>
36 #include <Geom2dAPI_ProjectPointOnCurve.hxx>
37 #include <gp_Circ.hxx>
38 #include <gp_Elips.hxx>
41 #include <TColStd_ListIteratorOfListOfReal.hxx>
42 #include <Geom_Circle.hxx>
43 #include <GeomAPI_ProjectPointOnCurve.hxx>
44 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
45 #include <TopTools_ListIteratorOfListOfShape.hxx>
46 #include <TopExp_Explorer.hxx>
47 #include <ShapeUpgrade_UnifySameDomain.hxx>
50 * GEOMImpl_Fillet1dPoint is an internal class for 1D fillet algorithm
51 * to store and compare computed solutions on edges
54 class GEOMImpl_Fillet1dPoint
60 Standard_EXPORT GEOMImpl_Fillet1dPoint(Standard_Real theParam)
63 //! Make copy of point
64 //!WARNING: Copies only field values: myParam, myV, myD, myValid
65 Standard_EXPORT GEOMImpl_Fillet1dPoint* Copy(); // warning: this is not the full copy!
68 Standard_EXPORT inline void SetParam(Standard_Real theParam)
70 Standard_EXPORT inline Standard_Real GetParam() const
72 Standard_EXPORT inline void SetParam2(const Standard_Real theParam2)
73 {myParam2 = theParam2;}
74 Standard_EXPORT inline Standard_Real GetParam2()
78 Standard_EXPORT inline Standard_Boolean IsValid(int theIndex)
79 {return (Standard_Boolean)myValid.Value(theIndex);}
82 Standard_EXPORT inline Standard_Integer GetNBValues() {return myV.Length();}
83 Standard_EXPORT inline Standard_Real GetValue(Standard_Integer theIndex)
84 {return myV.Value(theIndex);}
85 Standard_EXPORT inline Standard_Real GetDiff(Standard_Integer theIndex)
86 {return myD.Value(theIndex);}
87 Standard_EXPORT inline Standard_Integer GetNear(Standard_Integer theIndex)
88 {return myNear.Value(theIndex);}
90 //! Set/Get center point
91 Standard_EXPORT inline void SetCenter(const gp_Pnt2d thePoint)
92 {myCenter = thePoint;}
93 Standard_EXPORT inline const gp_Pnt2d GetCenter()
96 Standard_EXPORT void AddValue(Standard_Real theValue, Standard_Boolean theIsValid);
98 //! compute difference between this and given point
99 Standard_EXPORT Standard_Boolean ComputeDifference(GEOMImpl_Fillet1dPoint*);
100 Standard_EXPORT void FilterPoints(GEOMImpl_Fillet1dPoint*);
102 //! Checks if point contains solution and returns the index of it if any
103 Standard_EXPORT Standard_Integer HasSolution(Standard_Real theRadius);
104 //! Remove solution by index
105 void RemoveSolution(Standard_Integer theIndex);
110 Standard_Real myParam, myParam2;
111 TColStd_SequenceOfReal myV;
112 TColStd_SequenceOfReal myD;
113 TColStd_SequenceOfInteger myValid;
114 TColStd_SequenceOfInteger myNear;
117 class GEOMImpl_Fillet1d
121 //! The fillet 1D algorithm is initialised by two edges and plane
122 Standard_EXPORT GEOMImpl_Fillet1d(const TopoDS_Edge& theEdge1,
123 const TopoDS_Edge& theEdge2,
124 const gp_Pln& thePlane);
125 //! Makes fillet with given radius
126 //! @returns Standard_True, if at least one result computed
127 Standard_EXPORT Standard_Boolean Perform(const Standard_Real theRadius);
129 //! Returns result fillet edge and modified edges as out parameters
130 Standard_EXPORT TopoDS_Edge Result(const gp_Pnt& thePoint, TopoDS_Edge& theEdge1, TopoDS_Edge& theEdge2);
134 void fillPoint(GEOMImpl_Fillet1dPoint*);
135 void fillDiff(GEOMImpl_Fillet1dPoint*, Standard_Real, Standard_Boolean);
136 void performNewton(GEOMImpl_Fillet1dPoint*, GEOMImpl_Fillet1dPoint*);
137 Standard_Boolean processPoint(GEOMImpl_Fillet1dPoint*, GEOMImpl_Fillet1dPoint*, Standard_Real);
141 TopoDS_Edge myEdge1, myEdge2;
142 Handle(Geom_Plane) myPlane;
143 Handle(Geom2d_Curve) myCurve1, myCurve2;
144 Standard_Real myStart1, myEnd1, myStart2, myEnd2, myRadius;
145 TColStd_ListOfReal myResultParams;
146 TColStd_SequenceOfInteger myResultOrientation;
147 Standard_Boolean myStartSide, myEdgesExchnged;
148 Standard_Integer myDegreeOfRecursion;
151 //from GEOMImpl_Fillet1d.cxx
152 GEOMImpl_Fillet1d::GEOMImpl_Fillet1d(const TopoDS_Edge& theEdge1,
153 const TopoDS_Edge& theEdge2,
154 const gp_Pln& thePlane)
155 : myEdgesExchnged( Standard_False )
157 myPlane = new Geom_Plane(thePlane);
159 BRepAdaptor_Curve aBAC1(theEdge1);
160 BRepAdaptor_Curve aBAC2(theEdge2);
161 if (aBAC1.GetType() < aBAC2.GetType())
162 { // first curve must be more complicated
165 myEdgesExchnged = Standard_True;
173 Handle(Geom_Curve) aCurve1 = BRep_Tool::Curve(myEdge1, myStart1, myEnd1);
174 Handle(Geom_Curve) aCurve2 = BRep_Tool::Curve(myEdge2, myStart2, myEnd2);
176 myCurve1 = GeomProjLib::Curve2d(aCurve1, myStart1, myEnd1, myPlane);
177 myCurve2 = GeomProjLib::Curve2d(aCurve2, myStart2, myEnd2, myPlane);
179 while (myCurve1->IsPeriodic() && myStart1 >= myEnd1)
180 myEnd1 += myCurve1->Period();
181 while (myCurve2->IsPeriodic() && myStart2 >= myEnd2)
182 myEnd2 += myCurve2->Period();
184 if (aBAC1.GetType() == aBAC2.GetType())
186 if (myEnd2 - myStart2 < myEnd1 - myStart1)
187 { // first curve must be parametrically shorter
188 TopoDS_Edge anEdge = myEdge1;
191 Handle(Geom2d_Curve) aCurve = myCurve1;
194 Standard_Real a = myStart1;
200 myEdgesExchnged = Standard_True;
205 //=======================================================================
206 //function : isRadiusIntersected
207 //purpose : local function
208 //=======================================================================
209 static Standard_Boolean isRadiusIntersected(const Handle(Geom2d_Curve)& theCurve,
210 const gp_Pnt2d theStart,
211 const gp_Pnt2d theEnd,
212 const Standard_Boolean theStartConnected)
214 const Standard_Real aTol = Precision::Confusion();
215 const Standard_Real anAngTol = Precision::Angular();
216 Geom2dAPI_InterCurveCurve anInter(theCurve, new Geom2d_Line(theStart,
217 gp_Dir2d(gp_Vec2d(theStart, theEnd))), aTol);
220 for(a = anInter.NbPoints(); a > 0; a--)
222 aPoint = anInter.Point(a);
223 if ( aPoint.Distance(theStart) < aTol && !theStartConnected )
224 return Standard_True;
225 if (aPoint.Distance(theEnd) < aTol * 200)
226 return Standard_True;
227 if (gp_Vec2d(aPoint, theStart).IsOpposite(gp_Vec2d(aPoint, theEnd), anAngTol))
228 return Standard_True;
230 Handle(Geom2d_Curve) aCurve;
231 for(a = anInter.NbSegments(); a > 0; a--)
233 anInter.Segment(a, aCurve);
234 aPoint = aCurve->Value(aCurve->FirstParameter());
235 if (aPoint.Distance(theStart) < aTol)
236 if (!theStartConnected)
237 return Standard_True;
238 if (aPoint.Distance(theEnd) < aTol)
239 return Standard_True;
240 if (gp_Vec2d(aPoint, theStart).IsOpposite(gp_Vec2d(aPoint, theEnd), anAngTol))
241 return Standard_True;
242 aPoint = aCurve->Value(aCurve->LastParameter());
243 if (aPoint.Distance(theStart) < aTol)
244 if (!theStartConnected)
245 return Standard_True;
246 if (aPoint.Distance(theEnd) < aTol)
247 return Standard_True;
248 if (gp_Vec2d(aPoint, theStart).IsOpposite(gp_Vec2d(aPoint, theEnd), anAngTol))
249 return Standard_True;
251 return Standard_False;
254 //=======================================================================
255 //function : PlaneOfWire
256 //purpose : local function
257 //=======================================================================
258 static Standard_Boolean PlaneOfWire (const TopoDS_Wire& W, gp_Pln& P)
260 Standard_Boolean isplane = Standard_True;
261 BRepLib_FindSurface findPlanarSurf;
262 Handle(Geom_Surface) S;
266 BRepGProp::LinearProperties(W,GP);
267 gp_Pnt Bary = GP.CentreOfMass();
269 // shielding for particular cases : only one edge circle or ellipse
270 // on a closed wire !
271 Standard_Integer nbEdges = 0;
272 BRepTools_WireExplorer anExp;
274 Standard_Boolean wClosed = W.Closed();
276 // it is checked if the vertices are the same.
277 TopoDS_Vertex V1, V2;
278 TopExp::Vertices(W,V1,V2);
279 if ( V1.IsSame(V2)) wClosed = Standard_True;
281 TopoDS_Edge Edge = TopoDS::Edge(anExp.Current());
282 Standard_Real first, last;
284 Handle(Geom_Curve) curv;
285 curv = BRep_Tool::Curve(Edge, loc, first, last);
287 Handle(Geom_Curve)::DownCast(curv->Transformed(loc.Transformation()));
289 GeomAdaptor_Curve AdC;
291 for(; anExp.More(); anExp.Next()) {
294 if ( nbEdges==1 && AdC.GetType() == GeomAbs_Circle ) {
295 Bary = AdC.Circle().Location();
297 if ( nbEdges==1 && AdC.GetType() == GeomAbs_Ellipse ) {
298 Bary = AdC.Ellipse().Location();
302 findPlanarSurf.Init(W, -1, Standard_True);
303 if ( findPlanarSurf.Found()) {
304 S = findPlanarSurf.Surface();
305 L = findPlanarSurf.Location();
306 if (!L.IsIdentity()) S = Handle(Geom_Surface)::
307 DownCast(S->Transformed(L.Transformation()));
308 P = (Handle(Geom_Plane)::DownCast(S))->Pln();
313 isplane = Standard_False;
319 //=======================================================================
320 //function : fillPoint
322 //=======================================================================
323 void GEOMImpl_Fillet1d::fillPoint(GEOMImpl_Fillet1dPoint* thePoint)
327 const Standard_Real aTol = Precision::Confusion();
328 myCurve1->D1(thePoint->GetParam(), aPoint, aVec);
329 if (aVec.SquareMagnitude() < aTol)
332 gp_Vec2d aPerp(((myStartSide)?-1:1) * aVec.Y(), ((myStartSide)?1:-1) * aVec.X());
334 aPerp.Multiply(myRadius);
335 gp_Pnt2d aCenter = aPoint.Translated(aPerp);
336 thePoint->SetCenter(aCenter);
338 // on the intersection point
339 Standard_Boolean aValid = Standard_True;
340 Geom2dAPI_ProjectPointOnCurve aProjInt(aPoint, myCurve2);
341 if (aProjInt.NbPoints() && aPoint.Distance(aProjInt.NearestPoint()) < aTol)
342 aValid = Standard_False;
344 aValid = !isRadiusIntersected(myCurve2, aPoint, aCenter, Standard_True);
346 Geom2dAPI_ProjectPointOnCurve aProj(aCenter, myCurve2);
347 Standard_Integer a, aNB = aProj.NbPoints();
348 for(a = aNB; a > 0; a--)
350 if (aPoint.Distance(aProj.Point(a)) < aTol)
353 Standard_Boolean aValid2 = aValid;
355 aValid2 = !isRadiusIntersected(myCurve1, aCenter, aProj.Point(a), Standard_False);
357 // checking the right parameter
358 Standard_Real aParam = aProj.Parameter(a);
359 while(myCurve2->IsPeriodic() && aParam < myStart2)
360 aParam += myCurve2->Period();
362 thePoint->AddValue(aProj.Distance(a) * aProj.Distance(a) - myRadius * myRadius,
363 (aParam >= myStart2 && aParam <= myEnd2 && aValid2));
364 if (fabs(fabs(aProj.Distance(a)) - myRadius) < aTol)
365 thePoint->SetParam2(aParam);
369 //=======================================================================
370 //function : fillDiff
372 //=======================================================================
373 void GEOMImpl_Fillet1d::fillDiff(GEOMImpl_Fillet1dPoint* thePoint, Standard_Real theDiffStep, Standard_Boolean theFront)
375 GEOMImpl_Fillet1dPoint* aDiff =
376 new GEOMImpl_Fillet1dPoint(thePoint->GetParam() + (theFront?(theDiffStep):(-theDiffStep)));
378 if (!thePoint->ComputeDifference(aDiff))
380 aDiff->SetParam(thePoint->GetParam() + (theFront?(-theDiffStep):(theDiffStep)));
382 thePoint->ComputeDifference(aDiff);
387 //=======================================================================
390 //=======================================================================
391 Standard_Boolean GEOMImpl_Fillet1d::Perform(const Standard_Real theRadius)
393 myDegreeOfRecursion = 0;
394 myResultParams.Clear();
395 myResultOrientation.Clear();
397 Standard_Real aNBSteps = 100;
398 Geom2dAdaptor_Curve aGAC(myCurve1);
399 switch (aGAC.GetType())
407 case GeomAbs_Ellipse:
410 case GeomAbs_BezierCurve:
411 case GeomAbs_BSplineCurve:
412 aNBSteps = 2 + aGAC.Degree() * aGAC.NbPoles();
414 default: // unknown: maximum
418 myRadius = theRadius;
419 Standard_Real aParam, aStep, aDStep;
420 aStep = (myEnd1 - myStart1) / aNBSteps;
421 aDStep = aStep/1000.;
423 Standard_Integer aCycle;
424 for(aCycle = 2, myStartSide = Standard_False; aCycle; myStartSide = !myStartSide, aCycle--)
426 GEOMImpl_Fillet1dPoint *aLeft = NULL, *aRight = NULL;
428 for(aParam = myStart1 + aStep; aParam < myEnd1 || fabs(myEnd1 - aParam) < Precision::Confusion(); aParam += aStep)
432 aLeft = new GEOMImpl_Fillet1dPoint(aParam - aStep);
434 fillDiff(aLeft, aDStep, Standard_True);
437 aRight = new GEOMImpl_Fillet1dPoint(aParam);
439 fillDiff(aRight, aDStep, Standard_False);
441 aLeft->FilterPoints(aRight);
442 performNewton(aLeft, aRight);
450 if (myResultParams.Extent())
451 return Standard_True;
453 return Standard_False;
456 //=======================================================================
457 //function : processPoint
459 //=======================================================================
460 Standard_Boolean GEOMImpl_Fillet1d::processPoint(GEOMImpl_Fillet1dPoint* theLeft,
461 GEOMImpl_Fillet1dPoint* theRight,
462 Standard_Real theParameter)
464 if (theParameter >= theLeft->GetParam() && theParameter < theRight->GetParam())
466 Standard_Real aDX = theRight->GetParam() - theLeft->GetParam();
467 if (theParameter - theLeft->GetParam() < aDX / 100.)
469 theParameter = theLeft->GetParam() + aDX / 100.;
471 if (theRight->GetParam() - theParameter < aDX / 100.)
473 theParameter = theRight->GetParam() - aDX / 100.;
476 // Protection on infinite loop.
477 myDegreeOfRecursion++;
478 Standard_Real diffx = 0.001 * aDX;
479 if (myDegreeOfRecursion > 1000)
482 if (myDegreeOfRecursion > 10000)
485 if (myDegreeOfRecursion > 100000)
487 return Standard_True;
492 GEOMImpl_Fillet1dPoint* aPoint1 = theLeft->Copy();
493 GEOMImpl_Fillet1dPoint* aPoint2 = new GEOMImpl_Fillet1dPoint(theParameter);
495 fillDiff(aPoint2, diffx, Standard_True);
497 aPoint1->FilterPoints(aPoint2);
498 performNewton(aPoint1, aPoint2);
499 aPoint2->FilterPoints(theRight);
500 performNewton(aPoint2, theRight);
504 return Standard_True;
507 return Standard_False;
510 //=======================================================================
511 //function : performNewton
513 //=======================================================================
514 void GEOMImpl_Fillet1d::performNewton(GEOMImpl_Fillet1dPoint* theLeft,
515 GEOMImpl_Fillet1dPoint* theRight)
518 // check the left: if this is solution store it and remove it from the list of researching points of theLeft
519 a = theLeft->HasSolution(myRadius);
522 if (theLeft->IsValid(a))
524 myResultParams.Append(theLeft->GetParam());
525 myResultOrientation.Append(myStartSide);
530 Standard_Real aDX = theRight->GetParam() - theLeft->GetParam();
531 if ( aDX < Precision::Confusion() / 1000000.)
533 a = theRight->HasSolution(myRadius);
535 if (theRight->IsValid(a))
537 myResultParams.Append(theRight->GetParam());
538 myResultOrientation.Append(myStartSide);
543 for(a = 1; a <= theLeft->GetNBValues(); a++)
545 Standard_Integer aNear = theLeft->GetNear(a);
547 Standard_Real aA = (theRight->GetDiff(aNear) - theLeft->GetDiff(a)) / aDX;
548 Standard_Real aB = theLeft->GetDiff(a) - aA * theLeft->GetParam();
549 Standard_Real aC = theLeft->GetValue(a) - theLeft->GetDiff(a) * theLeft->GetParam() +
550 aA * theLeft->GetParam() * theLeft->GetParam() / 2.0;
551 Standard_Real aDet = aB * aB - 2.0 * aA * aC;
553 if ( fabs(aDet) < gp::Resolution() )
556 if (fabs(aA) < Precision::Confusion())
558 if (fabs(aB) > 10e-20)
560 Standard_Real aX0 = - aC / aB; // use extremum
561 if (aX0 > theLeft->GetParam() && aX0 < theRight->GetParam())
562 processPoint(theLeft, theRight, aX0);
566 processPoint(theLeft, theRight, theLeft->GetParam() + aDX / 2.0); // linear division otherwise
571 if (fabs(aB) > fabs(aDet * 1000000.))
572 { // possible floating point operations accuracy errors
573 processPoint(theLeft, theRight, theLeft->GetParam() + aDX / 2.0); // linear division otherwise
580 Standard_Boolean aRes = processPoint(theLeft, theRight, (- aB + aDet) / aA);
582 aRes = processPoint(theLeft, theRight, (- aB - aDet) / aA);
584 processPoint(theLeft, theRight, theLeft->GetParam() + aDX / 2.0); // linear division otherwise
588 Standard_Real aX0 = - aB / aA; // use extremum
589 if (aX0 > theLeft->GetParam() && aX0 < theRight->GetParam())
590 processPoint(theLeft, theRight, aX0);
592 processPoint(theLeft, theRight, theLeft->GetParam() + aDX / 2.0); // linear division otherwise
599 //=======================================================================
602 //=======================================================================
603 TopoDS_Edge GEOMImpl_Fillet1d::Result(const gp_Pnt& thePoint,
604 TopoDS_Edge& theEdge1,
605 TopoDS_Edge& theEdge2)
608 gp_Pnt2d aTargetPoint2d;
609 Standard_Real aX, aY;
610 ElSLib::PlaneParameters(myPlane->Pln().Position(), thePoint, aX, aY);
611 aTargetPoint2d.SetCoord(aX, aY);
613 // choose the nearest circle
614 Standard_Real aDistance = RealLast(), aP;
615 GEOMImpl_Fillet1dPoint *aNearest;
617 TColStd_ListIteratorOfListOfReal anIter(myResultParams);
618 for(aNearest = NULL, a = 1; anIter.More(); anIter.Next(), a++)
620 myStartSide = (myResultOrientation.Value(a)) ? Standard_True : Standard_False;
621 GEOMImpl_Fillet1dPoint *aPoint = new GEOMImpl_Fillet1dPoint(anIter.Value());
623 if (!aPoint->HasSolution(myRadius))
625 aP = fabs(aPoint->GetCenter().Distance(aTargetPoint2d) - myRadius);
626 if (!aNearest || aP < aDistance)
640 // create circle edge
641 gp_Pnt aCenter = ElSLib::PlaneValue(aNearest->GetCenter().X(),
642 aNearest->GetCenter().Y(),
643 myPlane->Pln().Position());
644 Handle(Geom_Circle) aCircle =
645 new Geom_Circle(gp_Ax2(aCenter, myPlane->Pln().Axis().Direction()), myRadius);
646 gp_Pnt2d aPoint2d1, aPoint2d2;
647 myCurve1->D0(aNearest->GetParam(), aPoint2d1);
648 myCurve2->D0(aNearest->GetParam2(), aPoint2d2);
649 gp_Pnt aPoint1 = ElSLib::PlaneValue(aPoint2d1.X(), aPoint2d1.Y(), myPlane->Pln().Position());
650 gp_Pnt aPoint2 = ElSLib::PlaneValue(aPoint2d2.X(), aPoint2d2.Y(), myPlane->Pln().Position());
652 GeomAPI_ProjectPointOnCurve aProj(thePoint, aCircle);
653 Standard_Real aTarGetParam = aProj.LowerDistanceParameter();
654 gp_Pnt aPointOnCircle = aProj.NearestPoint();
656 // Check extrema point manually, because there is a bug in Open CASCADE
657 // in calculation of nearest point to a circle near the parameter 0.0
658 gp_Pnt p0 = ElCLib::Value(0.0, aCircle->Circ());
659 if (p0.Distance(thePoint) < aPointOnCircle.Distance(thePoint))
665 aProj.Perform(aPoint1);
666 Standard_Real aParam1 = aProj.LowerDistanceParameter();
667 aProj.Perform(aPoint2);
668 Standard_Real aParam2 = aProj.LowerDistanceParameter();
669 Standard_Boolean aIsOut = ((aParam1 < aTarGetParam && aParam2 < aTarGetParam) ||
670 (aParam1 > aTarGetParam && aParam2 > aTarGetParam));
671 if (aParam1 > aParam2)
673 BRepLib_MakeEdge aBuilder(aCircle->Circ(),
674 aIsOut ? aParam2 : aParam1,
675 aIsOut? aParam1 : aParam2);
676 aResult = aBuilder.Edge();
679 Standard_Real aStart, anEnd;
680 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(myEdge1, aStart, anEnd);
682 aCurve->D1(aNearest->GetParam(), aPoint1, aDir);
685 aCircle->D1(aParam1, aPoint1, aCircleDir);
686 if ((aCircleDir.Angle(aDir) > M_PI / 2.0) ^ aIsOut)
687 aStart = aNearest->GetParam();
689 anEnd = aNearest->GetParam();
691 if (fabs(aStart - anEnd) > Precision::Confusion())
694 BRepLib_MakeEdge aDivider1(aCurve, aStart, anEnd);
696 theEdge2 = aDivider1.Edge();
698 theEdge1 = aDivider1.Edge();
701 aCurve = BRep_Tool::Curve(myEdge2, aStart, anEnd);
702 aCurve->D1(aNearest->GetParam2(), aPoint2, aDir);
704 aCircle->D1(aParam2, aPoint2, aCircleDir);
705 if ((aCircleDir.Angle(aDir) > M_PI / 2.0) ^ (!aIsOut))
706 aStart = aNearest->GetParam2();
708 anEnd = aNearest->GetParam2();
710 if (fabs(aStart - anEnd) > Precision::Confusion())
712 BRepLib_MakeEdge aDivider2(aCurve, aStart, anEnd);
714 theEdge1 = aDivider2.Edge();
716 theEdge2 = aDivider2.Edge();
723 //=======================================================================
724 //function : AddValue
726 //=======================================================================
727 void GEOMImpl_Fillet1dPoint::AddValue(Standard_Real theValue, Standard_Boolean theValid)
730 for(a = 1; a <= myV.Length(); a++)
732 if (theValue < myV.Value(a))
734 myV.InsertBefore(a, theValue);
735 myValid.InsertBefore(a, (Standard_Integer)theValid);
739 myV.Append(theValue);
740 myValid.Append((Standard_Integer)theValid);
743 //=======================================================================
744 //function : ComputeDifference
746 //=======================================================================
747 Standard_Boolean GEOMImpl_Fillet1dPoint::ComputeDifference(GEOMImpl_Fillet1dPoint* thePoint)
750 Standard_Boolean aDiffsSet = (myD.Length() != 0);
751 Standard_Real aDX = thePoint->GetParam() - myParam, aDY = RealLast(); //???
752 if (thePoint->myV.Length() == myV.Length())
753 { // absolutely the same points
754 for(a = 1; a <= myV.Length(); a++)
756 aDY = thePoint->myV.Value(a) - myV.Value(a);
758 myD.SetValue(a, fabs(aDX) > gp::Resolution() ? (aDY/aDX) : 0);
760 myD.Append( fabs(aDX) > gp::Resolution() ? (aDY/aDX) : 0);
762 return Standard_True;
764 // between the diffeerent points searching for nearest analogs
766 for(a = 1; a <= myV.Length(); a++)
768 for(b = 1; b <= thePoint->myV.Length(); b++)
770 if (b == 1 || fabs(thePoint->myV.Value(b) - myV.Value(a)) < fabs(aDY))
771 aDY = thePoint->myV.Value(b) - myV.Value(a);
775 if ( fabs(aDX) > gp::Resolution() && fabs(aDY / aDX) < fabs(myD.Value(a)))
776 myD.SetValue(a, aDY / aDX);
782 myD.Append( fabs(aDX) > gp::Resolution() ? aDY/aDX : 0);
786 return Standard_False;
789 //=======================================================================
790 //function : FilterPoints
792 //=======================================================================
793 void GEOMImpl_Fillet1dPoint::FilterPoints(GEOMImpl_Fillet1dPoint* thePoint)
795 Standard_Integer a, b;
796 TColStd_SequenceOfReal aDiffs;
797 Standard_Real aY, aY2, aDX = thePoint->GetParam() - myParam;
798 for(a = 1; a <= myV.Length(); a++)
800 // searching for near point from thePoint
801 Standard_Integer aNear = 0;
802 Standard_Real aDiff = aDX * 10000.;
803 aY = myV.Value(a) + myD.Value(a) * aDX;
804 for(b = 1; b <= thePoint->myV.Length(); b++)
806 // calculate hypothesis value of the Y2 with the constant first and second derivative
807 aY2 = aY + aDX * (thePoint->myD.Value(b) - myD.Value(a)) / 2.0;
808 if (aNear == 0 || fabs(aY2 - thePoint->myV.Value(b)) < fabs(aDiff))
811 aDiff = aY2 - thePoint->myV.Value(b);
817 if (myV.Value(a) * thePoint->myV.Value(aNear) > 0)
818 {// the same sign at the same sides of the interval
819 if (myV.Value(a) * myD.Value(a) > 0)
821 if (fabs(myD.Value(a)) > Precision::Confusion())
826 if (fabs(myV.Value(a)) > fabs(thePoint->myV.Value(aNear)))
827 if (thePoint->myV.Value(aNear) * thePoint->myD.Value(aNear) < 0 &&
828 fabs(thePoint->myD.Value(aNear)) > Precision::Confusion())
838 if (myV.Value(a) * thePoint->myV.Value(aNear) > 0)
840 if ((myV.Value(a) + myD.Value(a) * aDX) * myV.Value(a) > Precision::Confusion() &&
841 (thePoint->myV.Value(aNear) + thePoint->myD.Value(aNear) * aDX) * thePoint->myV.Value(aNear) > Precision::Confusion())
850 if ( fabs(aDX) < gp::Resolution() || fabs(aDiff / aDX) > 1.e+7)
857 { // there is no near: remove it from the list
865 Standard_Boolean aFound = Standard_False;
866 for(b = 1; b <= myNear.Length(); b++)
868 if (myNear.Value(b) == aNear)
870 if (fabs(aDiffs.Value(b)) < fabs(aDiff))
871 { // return this 'near'
872 aFound = Standard_True;
880 { // remove the old 'near'
893 myNear.Append(aNear);
894 aDiffs.Append(aDiff);
900 //=======================================================================
903 //=======================================================================
904 GEOMImpl_Fillet1dPoint* GEOMImpl_Fillet1dPoint::Copy()
906 GEOMImpl_Fillet1dPoint* aCopy = new GEOMImpl_Fillet1dPoint(myParam);
908 for(a = 1; a <= myV.Length(); a++)
910 aCopy->myV.Append(myV.Value(a));
911 aCopy->myD.Append(myD.Value(a));
912 aCopy->myValid.Append(myValid.Value(a));
917 //=======================================================================
918 //function : HasSolution
920 //=======================================================================
921 Standard_Integer GEOMImpl_Fillet1dPoint::HasSolution(const Standard_Real theRadius)
924 for(a = 1; a <= myV.Length(); a++)
926 if (fabs(sqrt(fabs(fabs(myV.Value(a)) + theRadius * theRadius)) - theRadius) < Precision::Confusion() / 10.)
932 //=======================================================================
933 //function : RemoveSolution
935 //=======================================================================
936 void GEOMImpl_Fillet1dPoint::RemoveSolution(Standard_Integer theIndex)
938 myV.Remove(theIndex);
939 myD.Remove(theIndex);
940 myValid.Remove(theIndex);
941 myNear.Remove(theIndex);
947 //=======================================================================
948 //function : addEdgeRelation
949 //purpose : local function to remember relation between initial and modified edge
950 //=======================================================================
951 static void addEdgeRelation(TopTools_DataMapOfShapeShape& theMap,
952 const TopoDS_Edge& theInitE,
953 const TopoDS_Edge& theResE)
955 if ( theMap.IsBound( theInitE ) )
956 theMap.ChangeFind( theInitE ) = theResE;
958 theMap.Bind( theInitE, theResE );
961 HYDROData_Canal3dAnd2d::HYDROData_Canal3dAnd2d(const TopoDS_Wire& Profile,
962 const TopoDS_Wire& Guideline)
963 : myProfile(Profile), myOriginalGuideline(Guideline)
965 TopExp::Vertices(myProfile, myLeftVertex, myRightVertex);
966 gp_Pnt LeftPoint = BRep_Tool::Pnt(myLeftVertex);
967 gp_Pnt RightPoint = BRep_Tool::Pnt(myRightVertex);
968 myFilletRadius = (LeftPoint.Distance(RightPoint))/2. * 1.5;
971 MakeSharpVertexList();
972 if (!mySharpVertexList.IsEmpty())
975 ProjectWireOntoXOY(myGuideline, myProjectedGuideline);
981 void HYDROData_Canal3dAnd2d::MakeSharpVertexList()
983 TopTools_IndexedDataMapOfShapeListOfShape VEmap;
984 TopExp::MapShapesAndAncestors(myOriginalGuideline, TopAbs_VERTEX, TopAbs_EDGE, VEmap);
986 for (Standard_Integer i = 1; i <= VEmap.Extent(); i++)
988 const TopTools_ListOfShape& Elist = VEmap(i);
989 if (Elist.Extent() < 2)
992 const TopoDS_Vertex& CommonVertex = TopoDS::Vertex(VEmap.FindKey(i));
993 TopoDS_Edge E1 = TopoDS::Edge(Elist.First());
994 TopoDS_Edge E2 = TopoDS::Edge(Elist.Last());
995 TopoDS_Edge Edge1 = E1, Edge2 = E2;
996 TopoDS_Vertex V1, V2;
997 TopExp::Vertices(E1, V1, V2);
998 if (!V2.IsSame(CommonVertex))
999 { Edge1 = E2; Edge2 = E1; }
1000 BRepAdaptor_Curve BAC1(Edge1);
1001 BRepAdaptor_Curve BAC2(Edge2);
1004 BAC1.D1( BAC1.LastParameter(), aPoint, Vec1 );
1005 BAC2.D1( BAC2.FirstParameter(), aPoint, Vec2 );
1006 Standard_Real theAngle = Vec1.Angle(Vec2);
1007 if (theAngle > myTolAngular)
1008 mySharpVertexList.Append(CommonVertex);
1012 Standard_Boolean HYDROData_Canal3dAnd2d::MakeFillet()
1014 //Standard_Boolean isAllStepsOk = true;
1016 TopTools_DataMapOfShapeShape anEdgeToEdgeMap;
1018 //iterates on vertices, and make fillet on each couple of edges
1019 //collect result fillet edges in list
1020 TopTools_ListOfShape aListOfNewEdge;
1021 // remember relation between initial and modified map
1022 TopTools_IndexedDataMapOfShapeListOfShape aMapVToEdges;
1023 TopExp::MapShapesAndAncestors( myOriginalGuideline, TopAbs_VERTEX, TopAbs_EDGE, aMapVToEdges );
1024 TopTools_ListIteratorOfListOfShape anIt( mySharpVertexList );
1025 for ( ; anIt.More(); anIt.Next() ) {
1026 TopoDS_Vertex aV = TopoDS::Vertex( anIt.Value() );
1027 const TopTools_ListOfShape& aVertexEdges = aMapVToEdges.FindFromKey( aV );
1028 if ( aVertexEdges.Extent() != 2 )
1029 continue; // no input data to make fillet
1030 TopoDS_Edge anEdge1 = TopoDS::Edge( aVertexEdges.First() );
1031 TopoDS_Edge anEdge2 = TopoDS::Edge( aVertexEdges.Last() );
1032 // check if initial edges already modified in previous fillet operation
1033 if ( anEdgeToEdgeMap.IsBound( anEdge1 ) ) anEdge1 = TopoDS::Edge(anEdgeToEdgeMap.Find( anEdge1 ));
1034 if ( anEdgeToEdgeMap.IsBound( anEdge2 ) ) anEdge2 = TopoDS::Edge(anEdgeToEdgeMap.Find( anEdge2 ));
1035 if ( anEdge1.IsNull() || anEdge2.IsNull() || anEdge1.IsSame( anEdge2 ) )
1036 continue; //no input data to make fillet
1038 // create plane on 2 edges
1040 TopoDS_Wire LocalWire = BRepLib_MakeWire(anEdge1, anEdge2);
1041 //if ( !takePlane(anEdge1, anEdge2, aV, aPlane) )
1042 if (!PlaneOfWire(LocalWire, aPlane))
1043 return Standard_False; // seems edges does not belong to same plane or parallel (fillet can not be build)
1045 GEOMImpl_Fillet1d aFilletAlgo (anEdge1, anEdge2, aPlane);
1046 if (!aFilletAlgo.Perform(myFilletRadius)) {
1047 return Standard_False; //can not create fillet at this vertex with given radius
1050 // take fillet result in given vertex
1051 TopoDS_Edge aModifE1, aModifE2;
1052 TopoDS_Edge aNewE = aFilletAlgo.Result(BRep_Tool::Pnt(aV), aModifE1, aModifE2);
1053 if (aNewE.IsNull()) {
1054 return Standard_False; //fillet failed at this vertex
1057 // add new created edges and take modified edges
1058 aListOfNewEdge.Append(aNewE);
1060 // check if wire edges modified,
1061 // if yes, then map to original edges (from vertex-edges list), because edges can be modified before
1062 if (aModifE1.IsNull() || !anEdge1.IsSame( aModifE1 ))
1063 addEdgeRelation( anEdgeToEdgeMap, TopoDS::Edge(aVertexEdges.First()), aModifE1 );
1064 if (aModifE2.IsNull() || !anEdge2.IsSame( aModifE2 ))
1065 addEdgeRelation( anEdgeToEdgeMap, TopoDS::Edge(aVertexEdges.Last()), aModifE2 );
1068 if (anEdgeToEdgeMap.IsEmpty() && aListOfNewEdge.IsEmpty()) {
1069 return Standard_False; //fillet can't be computed on the given shape with the given radius
1072 // create new wire instead of original
1073 BRepLib_MakeWire MW;
1074 TopExp_Explorer anExp(myOriginalGuideline, TopAbs_EDGE);
1075 TopoDS_Edge aFirstEdge = TopoDS::Edge(anExp.Current());
1076 if (anEdgeToEdgeMap.IsBound(aFirstEdge))
1077 aFirstEdge = TopoDS::Edge(anEdgeToEdgeMap(aFirstEdge));
1080 for (anExp.Next(); anExp.More(); anExp.Next()) {
1081 TopoDS_Shape anEdge = anExp.Current();
1082 if (!anEdgeToEdgeMap.IsBound(anEdge))
1083 aListOfNewEdge.Append(anEdge);
1084 else if (!anEdgeToEdgeMap.Find(anEdge).IsNull())
1085 aListOfNewEdge.Append(anEdgeToEdgeMap.Find(anEdge));
1088 MW.Add(aListOfNewEdge);
1090 myGuideline = MW.Wire();
1092 return Standard_True;
1095 Standard_Boolean HYDROData_Canal3dAnd2d::ProjectWireOntoXOY(const TopoDS_Wire& aWire,
1096 TopoDS_Wire& ProjectedWire)
1098 gp_Pln XOY; //default plane
1099 TopoDS_Face theXOYface = BRepLib_MakeFace(XOY);
1101 BRepOffsetAPI_NormalProjection OrtProj(theXOYface);
1103 //OrtProj.SetParams(...); ???
1105 TopTools_ListOfShape Wires;
1106 OrtProj.BuildWire(Wires);
1108 if (Wires.Extent() != 1)
1109 return Standard_False;
1111 ProjectedWire = TopoDS::Wire(Wires.First());
1112 return Standard_True;
1115 TopoDS_Vertex HYDROData_Canal3dAnd2d::ProjectVertexOntoXOY(const TopoDS_Vertex& aVertex)
1117 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
1118 gp_Pnt aProjPoint(aPoint.X(), aPoint.Y(), 0.);
1119 TopoDS_Vertex aProjVertex = BRepLib_MakeVertex(aProjPoint);
1123 void HYDROData_Canal3dAnd2d::Make2dProfile()
1125 TopoDS_Vertex ProjectedLeftVertex = ProjectVertexOntoXOY(myLeftVertex);
1126 TopoDS_Vertex ProjectedRightVertex = ProjectVertexOntoXOY(myRightVertex);
1127 TopoDS_Edge anEdge = BRepLib_MakeEdge(ProjectedLeftVertex, ProjectedRightVertex);
1128 myProjectedProfile = BRepLib_MakeWire(anEdge);
1131 void HYDROData_Canal3dAnd2d::SetMiddlePoint2d()
1133 TopoDS_Vertex V1, V2;
1134 TopExp::Vertices(myProjectedProfile, V1, V2);
1135 gp_Pnt Pnt1 = BRep_Tool::Pnt(V1);
1136 gp_Pnt Pnt2 = BRep_Tool::Pnt(V2);
1137 myMiddlePoint2d.SetXYZ(0.5*(Pnt1.XYZ() + Pnt2.XYZ()));
1140 void HYDROData_Canal3dAnd2d::SetMiddlePoint3d()
1142 gp_Lin MidLin(myMiddlePoint2d, gp::DZ());
1143 TopoDS_Edge MidEdge = BRepLib_MakeEdge(MidLin);
1144 TopoDS_Iterator itw(myProfile);
1145 for (; itw.More(); itw.Next())
1147 const TopoDS_Edge& anEdge = TopoDS::Edge(itw.Value());
1148 BRepExtrema_ExtCC ExtremaEE(MidEdge, anEdge);
1149 if (ExtremaEE.IsDone() && ExtremaEE.NbExt() != 0)
1151 for (Standard_Integer i = 1; i <= ExtremaEE.NbExt(); i++)
1153 if (ExtremaEE.SquareDistance(i) <= Precision::Confusion())
1155 myMiddlePoint3d = ExtremaEE.PointOnE1(i);
1163 TopoDS_Wire HYDROData_Canal3dAnd2d::SetTransformedProfile(const TopoDS_Wire& aProfile,
1164 const TopoDS_Wire& aGuideline,
1165 const gp_Pnt& aMiddlePoint)
1167 TopoDS_Wire aTransformedProfile;
1169 TopoDS_Iterator itw(myProjectedGuideline);
1170 TopoDS_Edge FirstEdge = TopoDS::Edge(itw.Value());
1171 Standard_Real fpar, lpar;
1172 Handle(Geom_Curve) FirstCurve = BRep_Tool::Curve(FirstEdge, fpar, lpar);
1175 FirstCurve->D1(fpar, StartPnt, StartVec);
1177 TopoDS_Vertex FirstOnGuide, LastOnGuide;
1178 TopExp::Vertices(aGuideline, FirstOnGuide, LastOnGuide);
1179 gp_Pnt StartPointOnGuide = BRep_Tool::Pnt(FirstOnGuide);
1181 //gp_Vec Offset(myMiddlePoint3d, StartPointOnGuide);
1182 gp_Trsf Translation, Rotation;
1183 Translation.SetTranslation(aMiddlePoint, StartPointOnGuide);
1184 aTransformedProfile = TopoDS::Wire(BRepBuilderAPI_Transform(aProfile, Translation, Standard_True)); //copy
1186 gp_Vec Vertical(0.,0.,1.);
1187 TopoDS_Vertex LeftVertex, RightVertex;
1188 TopExp::Vertices(aTransformedProfile, LeftVertex, RightVertex);
1189 gp_Pnt LeftPoint = BRep_Tool::Pnt(LeftVertex);
1190 gp_Pnt RightPoint = BRep_Tool::Pnt(RightVertex);
1191 gp_Vec LeftToRight(LeftPoint, RightPoint);
1192 gp_Vec NormalToProfile = Vertical ^ LeftToRight;
1194 gp_Vec AxisOfRotation = NormalToProfile ^ StartVec;
1195 if (AxisOfRotation.Magnitude() <= gp::Resolution())
1197 if (Vertical * LeftToRight < 0.)
1199 gp_Ax1 theVertical(StartPointOnGuide, gp::DZ());
1200 Rotation.SetRotation(theVertical, M_PI);
1205 gp_Ax1 theAxis(StartPointOnGuide, AxisOfRotation);
1206 Standard_Real theAngle = NormalToProfile.AngleWithRef(StartVec, AxisOfRotation);
1207 Rotation.SetRotation(theAxis, theAngle);
1210 aTransformedProfile = TopoDS::Wire(BRepBuilderAPI_Transform(aTransformedProfile, Rotation, Standard_True)); //copy
1211 return aTransformedProfile;
1214 Standard_Boolean HYDROData_Canal3dAnd2d::Create3dPresentation()
1216 myTransformedProfile3d = SetTransformedProfile(myProfile, myGuideline, myMiddlePoint3d);
1218 mySweep3d = new BRepOffsetAPI_MakePipeShell(myGuideline);
1219 mySweep3d->SetMode(gp::DZ()); //optional
1220 mySweep3d->Add(myTransformedProfile3d);
1221 //mySweep3d->SetTransitionMode(BRepBuilderAPI_RightCorner); //optional
1223 if (!mySweep3d->IsDone())
1224 return Standard_False;
1226 myPipe3d = mySweep3d->Shape();
1227 return Standard_True;
1230 Standard_Boolean HYDROData_Canal3dAnd2d::Create2dPresentation()
1232 myTransformedProfile2d = SetTransformedProfile(myProjectedProfile, myProjectedGuideline, myMiddlePoint2d);
1234 mySweep2d = new BRepOffsetAPI_MakePipeShell(myProjectedGuideline);
1235 mySweep2d->SetMode(gp::DZ()); //optional
1236 mySweep2d->Add(myTransformedProfile2d);
1237 //mySweep2d->SetTransitionMode(BRepBuilderAPI_RightCorner); //optional
1239 if (!mySweep2d->IsDone())
1240 return Standard_False;
1242 myPipe2d = mySweep2d->Shape();
1243 myUnifier.Initialize(myPipe2d);
1245 myUnifiedPipe2d = myUnifier.Shape();
1247 TopoDS_Wire OriginalInlet = TopoDS::Wire(mySweep2d->FirstShape());
1248 TopoDS_Wire OriginalOutlet = TopoDS::Wire(mySweep2d->LastShape());
1249 myInlet = CreateWireOnUnifiedPipe2d(OriginalInlet);
1250 myOutlet = CreateWireOnUnifiedPipe2d(OriginalOutlet);
1251 TopoDS_Vertex V1, V2, V3, V4;
1252 TopExp::Vertices(myTransformedProfile2d, V1, V2);
1253 TopExp::Vertices(OriginalInlet, V3, V4);
1254 gp_Pnt P1 = BRep_Tool::Pnt(V1);
1255 gp_Pnt P3 = BRep_Tool::Pnt(V3);
1256 if (P1.IsEqual(P3, Precision::Confusion()))
1257 { myLeftVertex2d = V3; myRightVertex2d = V4; }
1259 { myLeftVertex2d = V4; myRightVertex2d = V3; }
1261 return Standard_True;
1264 TopoDS_Wire HYDROData_Canal3dAnd2d::GetBank(const TopoDS_Vertex& aFreeVertex)
1268 TopTools_ListOfShape GeneratedShapes;
1269 GeneratedShapes = mySweep2d->Generated(aFreeVertex);
1270 BRepLib_MakeWire MW;
1271 MW.Add(GeneratedShapes);
1274 TopoDS_Wire aBankOnUnifiedPipe2d = CreateWireOnUnifiedPipe2d(aBank);
1276 return aBankOnUnifiedPipe2d;
1279 TopoDS_Wire Canal3dAnd2d::CreateWireOnUnifiedPipe2d(const TopoDS_Wire& aWireOnPipe2d)
1281 BRepLib_MakeWire MW;
1282 BRepTools_WireExplorer wexp(aWireOnPipe2d);
1283 for (; wexp.More(); wexp.Next())
1285 TopoDS_Shape anEdge = wexp.Current();
1286 TopoDS_Shape NewEdge = myUnifier.Generated(anEdge);
1287 if (!NewEdge.IsNull())
1288 MW.Add(TopoDS::Edge(NewEdge));
1293 TopoDS_Shape HYDROData_Canal3dAnd2d::Get3dPresentation()
1298 TopoDS_Face HYDROData_Canal3dAnd2d::Get2dPresentation()
1300 TopoDS_Iterator iter(myUnifiedPipe2d);
1301 return TopoDS::Face(iter.Value());
1304 TopoDS_Wire HYDROData_Canal3dAnd2d::GetInlet()
1309 TopoDS_Wire HYDROData_Canal3dAnd2d::GetOutlet()
1314 TopoDS_Wire HYDROData_Canal3dAnd2d::GetLeftBank()
1316 return GetBank(myLeftVertex2d);
1319 TopoDS_Wire HYDROData_Canal3dAnd2d::GetRightBank()
1321 return GetBank(myRightVertex2d);
1324 /*TopoDS_Wire HYDROData_Canal3dAnd2d::GetRoundedGuideline()
1329 TopoDS_Wire HYDROData_Canal3dAnd2d::GetProjectedRoundedGuideline()
1331 return myProjectedGuideline;