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>
49 * GEOMImpl_Fillet1dPoint is an internal class for 1D fillet algorithm
50 * to store and compare computed solutions on edges
53 class GEOMImpl_Fillet1dPoint
59 Standard_EXPORT GEOMImpl_Fillet1dPoint(Standard_Real theParam)
62 //! Make copy of point
63 //!WARNING: Copies only field values: myParam, myV, myD, myValid
64 Standard_EXPORT GEOMImpl_Fillet1dPoint* Copy(); // warning: this is not the full copy!
67 Standard_EXPORT inline void SetParam(Standard_Real theParam)
69 Standard_EXPORT inline Standard_Real GetParam() const
71 Standard_EXPORT inline void SetParam2(const Standard_Real theParam2)
72 {myParam2 = theParam2;}
73 Standard_EXPORT inline Standard_Real GetParam2()
77 Standard_EXPORT inline Standard_Boolean IsValid(int theIndex)
78 {return (Standard_Boolean)myValid.Value(theIndex);}
81 Standard_EXPORT inline Standard_Integer GetNBValues() {return myV.Length();}
82 Standard_EXPORT inline Standard_Real GetValue(Standard_Integer theIndex)
83 {return myV.Value(theIndex);}
84 Standard_EXPORT inline Standard_Real GetDiff(Standard_Integer theIndex)
85 {return myD.Value(theIndex);}
86 Standard_EXPORT inline Standard_Integer GetNear(Standard_Integer theIndex)
87 {return myNear.Value(theIndex);}
89 //! Set/Get center point
90 Standard_EXPORT inline void SetCenter(const gp_Pnt2d thePoint)
91 {myCenter = thePoint;}
92 Standard_EXPORT inline const gp_Pnt2d GetCenter()
95 Standard_EXPORT void AddValue(Standard_Real theValue, Standard_Boolean theIsValid);
97 //! compute difference between this and given point
98 Standard_EXPORT Standard_Boolean ComputeDifference(GEOMImpl_Fillet1dPoint*);
99 Standard_EXPORT void FilterPoints(GEOMImpl_Fillet1dPoint*);
101 //! Checks if point contains solution and returns the index of it if any
102 Standard_EXPORT Standard_Integer HasSolution(Standard_Real theRadius);
103 //! Remove solution by index
104 void RemoveSolution(Standard_Integer theIndex);
109 Standard_Real myParam, myParam2;
110 TColStd_SequenceOfReal myV;
111 TColStd_SequenceOfReal myD;
112 TColStd_SequenceOfInteger myValid;
113 TColStd_SequenceOfInteger myNear;
116 class GEOMImpl_Fillet1d
120 //! The fillet 1D algorithm is initialised by two edges and plane
121 Standard_EXPORT GEOMImpl_Fillet1d(const TopoDS_Edge& theEdge1,
122 const TopoDS_Edge& theEdge2,
123 const gp_Pln& thePlane);
124 //! Makes fillet with given radius
125 //! @returns Standard_True, if at least one result computed
126 Standard_EXPORT Standard_Boolean Perform(const Standard_Real theRadius);
128 //! Returns result fillet edge and modified edges as out parameters
129 Standard_EXPORT TopoDS_Edge Result(const gp_Pnt& thePoint, TopoDS_Edge& theEdge1, TopoDS_Edge& theEdge2);
133 void fillPoint(GEOMImpl_Fillet1dPoint*);
134 void fillDiff(GEOMImpl_Fillet1dPoint*, Standard_Real, Standard_Boolean);
135 void performNewton(GEOMImpl_Fillet1dPoint*, GEOMImpl_Fillet1dPoint*);
136 Standard_Boolean processPoint(GEOMImpl_Fillet1dPoint*, GEOMImpl_Fillet1dPoint*, Standard_Real);
140 TopoDS_Edge myEdge1, myEdge2;
141 Handle(Geom_Plane) myPlane;
142 Handle(Geom2d_Curve) myCurve1, myCurve2;
143 Standard_Real myStart1, myEnd1, myStart2, myEnd2, myRadius;
144 TColStd_ListOfReal myResultParams;
145 TColStd_SequenceOfInteger myResultOrientation;
146 Standard_Boolean myStartSide, myEdgesExchnged;
147 Standard_Integer myDegreeOfRecursion;
150 //from GEOMImpl_Fillet1d.cxx
151 GEOMImpl_Fillet1d::GEOMImpl_Fillet1d(const TopoDS_Edge& theEdge1,
152 const TopoDS_Edge& theEdge2,
153 const gp_Pln& thePlane)
154 : myEdgesExchnged( Standard_False )
156 myPlane = new Geom_Plane(thePlane);
158 BRepAdaptor_Curve aBAC1(theEdge1);
159 BRepAdaptor_Curve aBAC2(theEdge2);
160 if (aBAC1.GetType() < aBAC2.GetType())
161 { // first curve must be more complicated
164 myEdgesExchnged = Standard_True;
172 Handle(Geom_Curve) aCurve1 = BRep_Tool::Curve(myEdge1, myStart1, myEnd1);
173 Handle(Geom_Curve) aCurve2 = BRep_Tool::Curve(myEdge2, myStart2, myEnd2);
175 myCurve1 = GeomProjLib::Curve2d(aCurve1, myStart1, myEnd1, myPlane);
176 myCurve2 = GeomProjLib::Curve2d(aCurve2, myStart2, myEnd2, myPlane);
178 while (myCurve1->IsPeriodic() && myStart1 >= myEnd1)
179 myEnd1 += myCurve1->Period();
180 while (myCurve2->IsPeriodic() && myStart2 >= myEnd2)
181 myEnd2 += myCurve2->Period();
183 if (aBAC1.GetType() == aBAC2.GetType())
185 if (myEnd2 - myStart2 < myEnd1 - myStart1)
186 { // first curve must be parametrically shorter
187 TopoDS_Edge anEdge = myEdge1;
190 Handle(Geom2d_Curve) aCurve = myCurve1;
193 Standard_Real a = myStart1;
199 myEdgesExchnged = Standard_True;
204 //=======================================================================
205 //function : isRadiusIntersected
206 //purpose : local function
207 //=======================================================================
208 static Standard_Boolean isRadiusIntersected(const Handle(Geom2d_Curve)& theCurve,
209 const gp_Pnt2d theStart,
210 const gp_Pnt2d theEnd,
211 const Standard_Boolean theStartConnected)
213 const Standard_Real aTol = Precision::Confusion();
214 const Standard_Real anAngTol = Precision::Angular();
215 Geom2dAPI_InterCurveCurve anInter(theCurve, new Geom2d_Line(theStart,
216 gp_Dir2d(gp_Vec2d(theStart, theEnd))), aTol);
219 for(a = anInter.NbPoints(); a > 0; a--)
221 aPoint = anInter.Point(a);
222 if ( aPoint.Distance(theStart) < aTol && !theStartConnected )
223 return Standard_True;
224 if (aPoint.Distance(theEnd) < aTol * 200)
225 return Standard_True;
226 if (gp_Vec2d(aPoint, theStart).IsOpposite(gp_Vec2d(aPoint, theEnd), anAngTol))
227 return Standard_True;
229 Handle(Geom2d_Curve) aCurve;
230 for(a = anInter.NbSegments(); a > 0; a--)
232 anInter.Segment(a, aCurve);
233 aPoint = aCurve->Value(aCurve->FirstParameter());
234 if (aPoint.Distance(theStart) < aTol)
235 if (!theStartConnected)
236 return Standard_True;
237 if (aPoint.Distance(theEnd) < aTol)
238 return Standard_True;
239 if (gp_Vec2d(aPoint, theStart).IsOpposite(gp_Vec2d(aPoint, theEnd), anAngTol))
240 return Standard_True;
241 aPoint = aCurve->Value(aCurve->LastParameter());
242 if (aPoint.Distance(theStart) < aTol)
243 if (!theStartConnected)
244 return Standard_True;
245 if (aPoint.Distance(theEnd) < aTol)
246 return Standard_True;
247 if (gp_Vec2d(aPoint, theStart).IsOpposite(gp_Vec2d(aPoint, theEnd), anAngTol))
248 return Standard_True;
250 return Standard_False;
253 //=======================================================================
254 //function : PlaneOfWire
255 //purpose : local function
256 //=======================================================================
257 static Standard_Boolean PlaneOfWire (const TopoDS_Wire& W, gp_Pln& P)
259 Standard_Boolean isplane = Standard_True;
260 BRepLib_FindSurface findPlanarSurf;
261 Handle(Geom_Surface) S;
265 BRepGProp::LinearProperties(W,GP);
266 gp_Pnt Bary = GP.CentreOfMass();
268 // shielding for particular cases : only one edge circle or ellipse
269 // on a closed wire !
270 Standard_Integer nbEdges = 0;
271 BRepTools_WireExplorer anExp;
273 Standard_Boolean wClosed = W.Closed();
275 // it is checked if the vertices are the same.
276 TopoDS_Vertex V1, V2;
277 TopExp::Vertices(W,V1,V2);
278 if ( V1.IsSame(V2)) wClosed = Standard_True;
280 TopoDS_Edge Edge = TopoDS::Edge(anExp.Current());
281 Standard_Real first, last;
283 Handle(Geom_Curve) curv;
284 curv = BRep_Tool::Curve(Edge, loc, first, last);
286 Handle(Geom_Curve)::DownCast(curv->Transformed(loc.Transformation()));
288 GeomAdaptor_Curve AdC;
290 for(; anExp.More(); anExp.Next()) {
293 if ( nbEdges==1 && AdC.GetType() == GeomAbs_Circle ) {
294 Bary = AdC.Circle().Location();
296 if ( nbEdges==1 && AdC.GetType() == GeomAbs_Ellipse ) {
297 Bary = AdC.Ellipse().Location();
301 findPlanarSurf.Init(W, -1, Standard_True);
302 if ( findPlanarSurf.Found()) {
303 S = findPlanarSurf.Surface();
304 L = findPlanarSurf.Location();
305 if (!L.IsIdentity()) S = Handle(Geom_Surface)::
306 DownCast(S->Transformed(L.Transformation()));
307 P = (Handle(Geom_Plane)::DownCast(S))->Pln();
312 isplane = Standard_False;
318 //=======================================================================
319 //function : fillPoint
321 //=======================================================================
322 void GEOMImpl_Fillet1d::fillPoint(GEOMImpl_Fillet1dPoint* thePoint)
326 const Standard_Real aTol = Precision::Confusion();
327 myCurve1->D1(thePoint->GetParam(), aPoint, aVec);
328 if (aVec.SquareMagnitude() < aTol)
331 gp_Vec2d aPerp(((myStartSide)?-1:1) * aVec.Y(), ((myStartSide)?1:-1) * aVec.X());
333 aPerp.Multiply(myRadius);
334 gp_Pnt2d aCenter = aPoint.Translated(aPerp);
335 thePoint->SetCenter(aCenter);
337 // on the intersection point
338 Standard_Boolean aValid = Standard_True;
339 Geom2dAPI_ProjectPointOnCurve aProjInt(aPoint, myCurve2);
340 if (aProjInt.NbPoints() && aPoint.Distance(aProjInt.NearestPoint()) < aTol)
341 aValid = Standard_False;
343 aValid = !isRadiusIntersected(myCurve2, aPoint, aCenter, Standard_True);
345 Geom2dAPI_ProjectPointOnCurve aProj(aCenter, myCurve2);
346 Standard_Integer a, aNB = aProj.NbPoints();
347 for(a = aNB; a > 0; a--)
349 if (aPoint.Distance(aProj.Point(a)) < aTol)
352 Standard_Boolean aValid2 = aValid;
354 aValid2 = !isRadiusIntersected(myCurve1, aCenter, aProj.Point(a), Standard_False);
356 // checking the right parameter
357 Standard_Real aParam = aProj.Parameter(a);
358 while(myCurve2->IsPeriodic() && aParam < myStart2)
359 aParam += myCurve2->Period();
361 thePoint->AddValue(aProj.Distance(a) * aProj.Distance(a) - myRadius * myRadius,
362 (aParam >= myStart2 && aParam <= myEnd2 && aValid2));
363 if (fabs(fabs(aProj.Distance(a)) - myRadius) < aTol)
364 thePoint->SetParam2(aParam);
368 //=======================================================================
369 //function : fillDiff
371 //=======================================================================
372 void GEOMImpl_Fillet1d::fillDiff(GEOMImpl_Fillet1dPoint* thePoint, Standard_Real theDiffStep, Standard_Boolean theFront)
374 GEOMImpl_Fillet1dPoint* aDiff =
375 new GEOMImpl_Fillet1dPoint(thePoint->GetParam() + (theFront?(theDiffStep):(-theDiffStep)));
377 if (!thePoint->ComputeDifference(aDiff))
379 aDiff->SetParam(thePoint->GetParam() + (theFront?(-theDiffStep):(theDiffStep)));
381 thePoint->ComputeDifference(aDiff);
386 //=======================================================================
389 //=======================================================================
390 Standard_Boolean GEOMImpl_Fillet1d::Perform(const Standard_Real theRadius)
392 myDegreeOfRecursion = 0;
393 myResultParams.Clear();
394 myResultOrientation.Clear();
396 Standard_Real aNBSteps = 100;
397 Geom2dAdaptor_Curve aGAC(myCurve1);
398 switch (aGAC.GetType())
406 case GeomAbs_Ellipse:
409 case GeomAbs_BezierCurve:
410 case GeomAbs_BSplineCurve:
411 aNBSteps = 2 + aGAC.Degree() * aGAC.NbPoles();
413 default: // unknown: maximum
417 myRadius = theRadius;
418 Standard_Real aParam, aStep, aDStep;
419 aStep = (myEnd1 - myStart1) / aNBSteps;
420 aDStep = aStep/1000.;
422 Standard_Integer aCycle;
423 for(aCycle = 2, myStartSide = Standard_False; aCycle; myStartSide = !myStartSide, aCycle--)
425 GEOMImpl_Fillet1dPoint *aLeft = NULL, *aRight = NULL;
427 for(aParam = myStart1 + aStep; aParam < myEnd1 || fabs(myEnd1 - aParam) < Precision::Confusion(); aParam += aStep)
431 aLeft = new GEOMImpl_Fillet1dPoint(aParam - aStep);
433 fillDiff(aLeft, aDStep, Standard_True);
436 aRight = new GEOMImpl_Fillet1dPoint(aParam);
438 fillDiff(aRight, aDStep, Standard_False);
440 aLeft->FilterPoints(aRight);
441 performNewton(aLeft, aRight);
449 if (myResultParams.Extent())
450 return Standard_True;
452 return Standard_False;
455 //=======================================================================
456 //function : processPoint
458 //=======================================================================
459 Standard_Boolean GEOMImpl_Fillet1d::processPoint(GEOMImpl_Fillet1dPoint* theLeft,
460 GEOMImpl_Fillet1dPoint* theRight,
461 Standard_Real theParameter)
463 if (theParameter >= theLeft->GetParam() && theParameter < theRight->GetParam())
465 Standard_Real aDX = theRight->GetParam() - theLeft->GetParam();
466 if (theParameter - theLeft->GetParam() < aDX / 100.)
468 theParameter = theLeft->GetParam() + aDX / 100.;
470 if (theRight->GetParam() - theParameter < aDX / 100.)
472 theParameter = theRight->GetParam() - aDX / 100.;
475 // Protection on infinite loop.
476 myDegreeOfRecursion++;
477 Standard_Real diffx = 0.001 * aDX;
478 if (myDegreeOfRecursion > 1000)
481 if (myDegreeOfRecursion > 10000)
484 if (myDegreeOfRecursion > 100000)
486 return Standard_True;
491 GEOMImpl_Fillet1dPoint* aPoint1 = theLeft->Copy();
492 GEOMImpl_Fillet1dPoint* aPoint2 = new GEOMImpl_Fillet1dPoint(theParameter);
494 fillDiff(aPoint2, diffx, Standard_True);
496 aPoint1->FilterPoints(aPoint2);
497 performNewton(aPoint1, aPoint2);
498 aPoint2->FilterPoints(theRight);
499 performNewton(aPoint2, theRight);
503 return Standard_True;
506 return Standard_False;
509 //=======================================================================
510 //function : performNewton
512 //=======================================================================
513 void GEOMImpl_Fillet1d::performNewton(GEOMImpl_Fillet1dPoint* theLeft,
514 GEOMImpl_Fillet1dPoint* theRight)
517 // check the left: if this is solution store it and remove it from the list of researching points of theLeft
518 a = theLeft->HasSolution(myRadius);
521 if (theLeft->IsValid(a))
523 myResultParams.Append(theLeft->GetParam());
524 myResultOrientation.Append(myStartSide);
529 Standard_Real aDX = theRight->GetParam() - theLeft->GetParam();
530 if ( aDX < Precision::Confusion() / 1000000.)
532 a = theRight->HasSolution(myRadius);
534 if (theRight->IsValid(a))
536 myResultParams.Append(theRight->GetParam());
537 myResultOrientation.Append(myStartSide);
542 for(a = 1; a <= theLeft->GetNBValues(); a++)
544 Standard_Integer aNear = theLeft->GetNear(a);
546 Standard_Real aA = (theRight->GetDiff(aNear) - theLeft->GetDiff(a)) / aDX;
547 Standard_Real aB = theLeft->GetDiff(a) - aA * theLeft->GetParam();
548 Standard_Real aC = theLeft->GetValue(a) - theLeft->GetDiff(a) * theLeft->GetParam() +
549 aA * theLeft->GetParam() * theLeft->GetParam() / 2.0;
550 Standard_Real aDet = aB * aB - 2.0 * aA * aC;
552 if ( fabs(aDet) < gp::Resolution() )
555 if (fabs(aA) < Precision::Confusion())
557 if (fabs(aB) > 10e-20)
559 Standard_Real aX0 = - aC / aB; // use extremum
560 if (aX0 > theLeft->GetParam() && aX0 < theRight->GetParam())
561 processPoint(theLeft, theRight, aX0);
565 processPoint(theLeft, theRight, theLeft->GetParam() + aDX / 2.0); // linear division otherwise
570 if (fabs(aB) > fabs(aDet * 1000000.))
571 { // possible floating point operations accuracy errors
572 processPoint(theLeft, theRight, theLeft->GetParam() + aDX / 2.0); // linear division otherwise
579 Standard_Boolean aRes = processPoint(theLeft, theRight, (- aB + aDet) / aA);
581 aRes = processPoint(theLeft, theRight, (- aB - aDet) / aA);
583 processPoint(theLeft, theRight, theLeft->GetParam() + aDX / 2.0); // linear division otherwise
587 Standard_Real aX0 = - aB / aA; // use extremum
588 if (aX0 > theLeft->GetParam() && aX0 < theRight->GetParam())
589 processPoint(theLeft, theRight, aX0);
591 processPoint(theLeft, theRight, theLeft->GetParam() + aDX / 2.0); // linear division otherwise
598 //=======================================================================
601 //=======================================================================
602 TopoDS_Edge GEOMImpl_Fillet1d::Result(const gp_Pnt& thePoint,
603 TopoDS_Edge& theEdge1,
604 TopoDS_Edge& theEdge2)
607 gp_Pnt2d aTargetPoint2d;
608 Standard_Real aX, aY;
609 ElSLib::PlaneParameters(myPlane->Pln().Position(), thePoint, aX, aY);
610 aTargetPoint2d.SetCoord(aX, aY);
612 // choose the nearest circle
613 Standard_Real aDistance = RealLast(), aP;
614 GEOMImpl_Fillet1dPoint *aNearest;
616 TColStd_ListIteratorOfListOfReal anIter(myResultParams);
617 for(aNearest = NULL, a = 1; anIter.More(); anIter.Next(), a++)
619 myStartSide = (myResultOrientation.Value(a)) ? Standard_True : Standard_False;
620 GEOMImpl_Fillet1dPoint *aPoint = new GEOMImpl_Fillet1dPoint(anIter.Value());
622 if (!aPoint->HasSolution(myRadius))
624 aP = fabs(aPoint->GetCenter().Distance(aTargetPoint2d) - myRadius);
625 if (!aNearest || aP < aDistance)
639 // create circle edge
640 gp_Pnt aCenter = ElSLib::PlaneValue(aNearest->GetCenter().X(),
641 aNearest->GetCenter().Y(),
642 myPlane->Pln().Position());
643 Handle(Geom_Circle) aCircle =
644 new Geom_Circle(gp_Ax2(aCenter, myPlane->Pln().Axis().Direction()), myRadius);
645 gp_Pnt2d aPoint2d1, aPoint2d2;
646 myCurve1->D0(aNearest->GetParam(), aPoint2d1);
647 myCurve2->D0(aNearest->GetParam2(), aPoint2d2);
648 gp_Pnt aPoint1 = ElSLib::PlaneValue(aPoint2d1.X(), aPoint2d1.Y(), myPlane->Pln().Position());
649 gp_Pnt aPoint2 = ElSLib::PlaneValue(aPoint2d2.X(), aPoint2d2.Y(), myPlane->Pln().Position());
651 GeomAPI_ProjectPointOnCurve aProj(thePoint, aCircle);
652 Standard_Real aTarGetParam = aProj.LowerDistanceParameter();
653 gp_Pnt aPointOnCircle = aProj.NearestPoint();
655 // Check extrema point manually, because there is a bug in Open CASCADE
656 // in calculation of nearest point to a circle near the parameter 0.0
657 gp_Pnt p0 = ElCLib::Value(0.0, aCircle->Circ());
658 if (p0.Distance(thePoint) < aPointOnCircle.Distance(thePoint))
664 aProj.Perform(aPoint1);
665 Standard_Real aParam1 = aProj.LowerDistanceParameter();
666 aProj.Perform(aPoint2);
667 Standard_Real aParam2 = aProj.LowerDistanceParameter();
668 Standard_Boolean aIsOut = ((aParam1 < aTarGetParam && aParam2 < aTarGetParam) ||
669 (aParam1 > aTarGetParam && aParam2 > aTarGetParam));
670 if (aParam1 > aParam2)
672 BRepLib_MakeEdge aBuilder(aCircle->Circ(),
673 aIsOut ? aParam2 : aParam1,
674 aIsOut? aParam1 : aParam2);
675 aResult = aBuilder.Edge();
678 Standard_Real aStart, anEnd;
679 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(myEdge1, aStart, anEnd);
681 aCurve->D1(aNearest->GetParam(), aPoint1, aDir);
684 aCircle->D1(aParam1, aPoint1, aCircleDir);
685 if ((aCircleDir.Angle(aDir) > M_PI / 2.0) ^ aIsOut)
686 aStart = aNearest->GetParam();
688 anEnd = aNearest->GetParam();
690 if (fabs(aStart - anEnd) > Precision::Confusion())
693 BRepLib_MakeEdge aDivider1(aCurve, aStart, anEnd);
695 theEdge2 = aDivider1.Edge();
697 theEdge1 = aDivider1.Edge();
700 aCurve = BRep_Tool::Curve(myEdge2, aStart, anEnd);
701 aCurve->D1(aNearest->GetParam2(), aPoint2, aDir);
703 aCircle->D1(aParam2, aPoint2, aCircleDir);
704 if ((aCircleDir.Angle(aDir) > M_PI / 2.0) ^ (!aIsOut))
705 aStart = aNearest->GetParam2();
707 anEnd = aNearest->GetParam2();
709 if (fabs(aStart - anEnd) > Precision::Confusion())
711 BRepLib_MakeEdge aDivider2(aCurve, aStart, anEnd);
713 theEdge1 = aDivider2.Edge();
715 theEdge2 = aDivider2.Edge();
722 //=======================================================================
723 //function : AddValue
725 //=======================================================================
726 void GEOMImpl_Fillet1dPoint::AddValue(Standard_Real theValue, Standard_Boolean theValid)
729 for(a = 1; a <= myV.Length(); a++)
731 if (theValue < myV.Value(a))
733 myV.InsertBefore(a, theValue);
734 myValid.InsertBefore(a, (Standard_Integer)theValid);
738 myV.Append(theValue);
739 myValid.Append((Standard_Integer)theValid);
742 //=======================================================================
743 //function : ComputeDifference
745 //=======================================================================
746 Standard_Boolean GEOMImpl_Fillet1dPoint::ComputeDifference(GEOMImpl_Fillet1dPoint* thePoint)
749 Standard_Boolean aDiffsSet = (myD.Length() != 0);
750 Standard_Real aDX = thePoint->GetParam() - myParam, aDY = RealLast(); //???
751 if (thePoint->myV.Length() == myV.Length())
752 { // absolutely the same points
753 for(a = 1; a <= myV.Length(); a++)
755 aDY = thePoint->myV.Value(a) - myV.Value(a);
757 myD.SetValue(a, fabs(aDX) > gp::Resolution() ? (aDY/aDX) : 0);
759 myD.Append( fabs(aDX) > gp::Resolution() ? (aDY/aDX) : 0);
761 return Standard_True;
763 // between the diffeerent points searching for nearest analogs
765 for(a = 1; a <= myV.Length(); a++)
767 for(b = 1; b <= thePoint->myV.Length(); b++)
769 if (b == 1 || fabs(thePoint->myV.Value(b) - myV.Value(a)) < fabs(aDY))
770 aDY = thePoint->myV.Value(b) - myV.Value(a);
774 if ( fabs(aDX) > gp::Resolution() && fabs(aDY / aDX) < fabs(myD.Value(a)))
775 myD.SetValue(a, aDY / aDX);
781 myD.Append( fabs(aDX) > gp::Resolution() ? aDY/aDX : 0);
785 return Standard_False;
788 //=======================================================================
789 //function : FilterPoints
791 //=======================================================================
792 void GEOMImpl_Fillet1dPoint::FilterPoints(GEOMImpl_Fillet1dPoint* thePoint)
794 Standard_Integer a, b;
795 TColStd_SequenceOfReal aDiffs;
796 Standard_Real aY, aY2, aDX = thePoint->GetParam() - myParam;
797 for(a = 1; a <= myV.Length(); a++)
799 // searching for near point from thePoint
800 Standard_Integer aNear = 0;
801 Standard_Real aDiff = aDX * 10000.;
802 aY = myV.Value(a) + myD.Value(a) * aDX;
803 for(b = 1; b <= thePoint->myV.Length(); b++)
805 // calculate hypothesis value of the Y2 with the constant first and second derivative
806 aY2 = aY + aDX * (thePoint->myD.Value(b) - myD.Value(a)) / 2.0;
807 if (aNear == 0 || fabs(aY2 - thePoint->myV.Value(b)) < fabs(aDiff))
810 aDiff = aY2 - thePoint->myV.Value(b);
816 if (myV.Value(a) * thePoint->myV.Value(aNear) > 0)
817 {// the same sign at the same sides of the interval
818 if (myV.Value(a) * myD.Value(a) > 0)
820 if (fabs(myD.Value(a)) > Precision::Confusion())
825 if (fabs(myV.Value(a)) > fabs(thePoint->myV.Value(aNear)))
826 if (thePoint->myV.Value(aNear) * thePoint->myD.Value(aNear) < 0 &&
827 fabs(thePoint->myD.Value(aNear)) > Precision::Confusion())
837 if (myV.Value(a) * thePoint->myV.Value(aNear) > 0)
839 if ((myV.Value(a) + myD.Value(a) * aDX) * myV.Value(a) > Precision::Confusion() &&
840 (thePoint->myV.Value(aNear) + thePoint->myD.Value(aNear) * aDX) * thePoint->myV.Value(aNear) > Precision::Confusion())
849 if ( fabs(aDX) < gp::Resolution() || fabs(aDiff / aDX) > 1.e+7)
856 { // there is no near: remove it from the list
864 Standard_Boolean aFound = Standard_False;
865 for(b = 1; b <= myNear.Length(); b++)
867 if (myNear.Value(b) == aNear)
869 if (fabs(aDiffs.Value(b)) < fabs(aDiff))
870 { // return this 'near'
871 aFound = Standard_True;
879 { // remove the old 'near'
892 myNear.Append(aNear);
893 aDiffs.Append(aDiff);
899 //=======================================================================
902 //=======================================================================
903 GEOMImpl_Fillet1dPoint* GEOMImpl_Fillet1dPoint::Copy()
905 GEOMImpl_Fillet1dPoint* aCopy = new GEOMImpl_Fillet1dPoint(myParam);
907 for(a = 1; a <= myV.Length(); a++)
909 aCopy->myV.Append(myV.Value(a));
910 aCopy->myD.Append(myD.Value(a));
911 aCopy->myValid.Append(myValid.Value(a));
916 //=======================================================================
917 //function : HasSolution
919 //=======================================================================
920 Standard_Integer GEOMImpl_Fillet1dPoint::HasSolution(const Standard_Real theRadius)
923 for(a = 1; a <= myV.Length(); a++)
925 if (fabs(sqrt(fabs(fabs(myV.Value(a)) + theRadius * theRadius)) - theRadius) < Precision::Confusion() / 10.)
931 //=======================================================================
932 //function : RemoveSolution
934 //=======================================================================
935 void GEOMImpl_Fillet1dPoint::RemoveSolution(Standard_Integer theIndex)
937 myV.Remove(theIndex);
938 myD.Remove(theIndex);
939 myValid.Remove(theIndex);
940 myNear.Remove(theIndex);
946 //=======================================================================
947 //function : addEdgeRelation
948 //purpose : local function to remember relation between initial and modified edge
949 //=======================================================================
950 static void addEdgeRelation(TopTools_DataMapOfShapeShape& theMap,
951 const TopoDS_Edge& theInitE,
952 const TopoDS_Edge& theResE)
954 if ( theMap.IsBound( theInitE ) )
955 theMap.ChangeFind( theInitE ) = theResE;
957 theMap.Bind( theInitE, theResE );
960 HYDROData_Canal3dAnd2d::HYDROData_Canal3dAnd2d(const TopoDS_Wire& Profile,
961 const TopoDS_Wire& Guideline)
962 : myProfile(Profile), myOriginalGuideline(Guideline)
964 TopExp::Vertices(myProfile, myLeftVertex, myRightVertex);
965 gp_Pnt LeftPoint = BRep_Tool::Pnt(myLeftVertex);
966 gp_Pnt RightPoint = BRep_Tool::Pnt(myRightVertex);
967 myFilletRadius = (LeftPoint.Distance(RightPoint))/2.;
970 MakeSharpVertexList();
971 if (!mySharpVertexList.IsEmpty())
974 ProjectWireOntoXOY(myGuideline, myProjectedGuideline);
980 void HYDROData_Canal3dAnd2d::MakeSharpVertexList()
982 TopTools_IndexedDataMapOfShapeListOfShape VEmap;
983 TopExp::MapShapesAndAncestors(myOriginalGuideline, TopAbs_VERTEX, TopAbs_EDGE, VEmap);
985 for (Standard_Integer i = 1; i <= VEmap.Extent(); i++)
987 const TopTools_ListOfShape& Elist = VEmap(i);
988 if (Elist.Extent() < 2)
991 const TopoDS_Vertex& CommonVertex = TopoDS::Vertex(VEmap.FindKey(i));
992 TopoDS_Edge E1 = TopoDS::Edge(Elist.First());
993 TopoDS_Edge E2 = TopoDS::Edge(Elist.Last());
994 TopoDS_Edge Edge1 = E1, Edge2 = E2;
995 TopoDS_Vertex V1, V2;
996 TopExp::Vertices(E1, V1, V2);
997 if (!V2.IsSame(CommonVertex))
998 { Edge1 = E2; Edge2 = E1; }
999 BRepAdaptor_Curve BAC1(Edge1);
1000 BRepAdaptor_Curve BAC2(Edge2);
1003 BAC1.D1( BAC1.LastParameter(), aPoint, Vec1 );
1004 BAC2.D1( BAC2.FirstParameter(), aPoint, Vec2 );
1005 Standard_Real theAngle = Vec1.Angle(Vec2);
1006 if (theAngle > myTolAngular)
1007 mySharpVertexList.Append(CommonVertex);
1011 Standard_Boolean HYDROData_Canal3dAnd2d::MakeFillet()
1013 //Standard_Boolean isAllStepsOk = true;
1015 TopTools_DataMapOfShapeShape anEdgeToEdgeMap;
1017 //iterates on vertices, and make fillet on each couple of edges
1018 //collect result fillet edges in list
1019 TopTools_ListOfShape aListOfNewEdge;
1020 // remember relation between initial and modified map
1021 TopTools_IndexedDataMapOfShapeListOfShape aMapVToEdges;
1022 TopExp::MapShapesAndAncestors( myOriginalGuideline, TopAbs_VERTEX, TopAbs_EDGE, aMapVToEdges );
1023 TopTools_ListIteratorOfListOfShape anIt( mySharpVertexList );
1024 for ( ; anIt.More(); anIt.Next() ) {
1025 TopoDS_Vertex aV = TopoDS::Vertex( anIt.Value() );
1026 const TopTools_ListOfShape& aVertexEdges = aMapVToEdges.FindFromKey( aV );
1027 if ( aVertexEdges.Extent() != 2 )
1028 continue; // no input data to make fillet
1029 TopoDS_Edge anEdge1 = TopoDS::Edge( aVertexEdges.First() );
1030 TopoDS_Edge anEdge2 = TopoDS::Edge( aVertexEdges.Last() );
1031 // check if initial edges already modified in previous fillet operation
1032 if ( anEdgeToEdgeMap.IsBound( anEdge1 ) ) anEdge1 = TopoDS::Edge(anEdgeToEdgeMap.Find( anEdge1 ));
1033 if ( anEdgeToEdgeMap.IsBound( anEdge2 ) ) anEdge2 = TopoDS::Edge(anEdgeToEdgeMap.Find( anEdge2 ));
1034 if ( anEdge1.IsNull() || anEdge2.IsNull() || anEdge1.IsSame( anEdge2 ) )
1035 continue; //no input data to make fillet
1037 // create plane on 2 edges
1039 TopoDS_Wire LocalWire = BRepLib_MakeWire(anEdge1, anEdge2);
1040 //if ( !takePlane(anEdge1, anEdge2, aV, aPlane) )
1041 if (!PlaneOfWire(LocalWire, aPlane))
1042 return Standard_False; // seems edges does not belong to same plane or parallel (fillet can not be build)
1044 GEOMImpl_Fillet1d aFilletAlgo (anEdge1, anEdge2, aPlane);
1045 if (!aFilletAlgo.Perform(myFilletRadius)) {
1046 return Standard_False; //can not create fillet at this vertex with given radius
1049 // take fillet result in given vertex
1050 TopoDS_Edge aModifE1, aModifE2;
1051 TopoDS_Edge aNewE = aFilletAlgo.Result(BRep_Tool::Pnt(aV), aModifE1, aModifE2);
1052 if (aNewE.IsNull()) {
1053 return Standard_False; //fillet failed at this vertex
1056 // add new created edges and take modified edges
1057 aListOfNewEdge.Append(aNewE);
1059 // check if wire edges modified,
1060 // if yes, then map to original edges (from vertex-edges list), because edges can be modified before
1061 if (aModifE1.IsNull() || !anEdge1.IsSame( aModifE1 ))
1062 addEdgeRelation( anEdgeToEdgeMap, TopoDS::Edge(aVertexEdges.First()), aModifE1 );
1063 if (aModifE2.IsNull() || !anEdge2.IsSame( aModifE2 ))
1064 addEdgeRelation( anEdgeToEdgeMap, TopoDS::Edge(aVertexEdges.Last()), aModifE2 );
1067 if (anEdgeToEdgeMap.IsEmpty() && aListOfNewEdge.IsEmpty()) {
1068 return Standard_False; //fillet can't be computed on the given shape with the given radius
1071 // create new wire instead of original
1072 BRepLib_MakeWire MW;
1073 TopExp_Explorer anExp(myOriginalGuideline, TopAbs_EDGE);
1074 TopoDS_Edge aFirstEdge = TopoDS::Edge(anExp.Current());
1075 if (anEdgeToEdgeMap.IsBound(aFirstEdge))
1076 aFirstEdge = TopoDS::Edge(anEdgeToEdgeMap(aFirstEdge));
1079 for (anExp.Next(); anExp.More(); anExp.Next()) {
1080 TopoDS_Shape anEdge = anExp.Current();
1081 if (!anEdgeToEdgeMap.IsBound(anEdge))
1082 aListOfNewEdge.Append(anEdge);
1083 else if (!anEdgeToEdgeMap.Find(anEdge).IsNull())
1084 aListOfNewEdge.Append(anEdgeToEdgeMap.Find(anEdge));
1087 MW.Add(aListOfNewEdge);
1089 myGuideline = MW.Wire();
1091 return Standard_True;
1094 Standard_Boolean HYDROData_Canal3dAnd2d::ProjectWireOntoXOY(const TopoDS_Wire& aWire,
1095 TopoDS_Wire& ProjectedWire)
1097 gp_Pln XOY; //default plane
1098 TopoDS_Face theXOYface = BRepLib_MakeFace(XOY);
1100 BRepOffsetAPI_NormalProjection OrtProj(theXOYface);
1102 //OrtProj.SetParams(...); ???
1104 TopTools_ListOfShape Wires;
1105 OrtProj.BuildWire(Wires);
1107 if (Wires.Extent() != 1)
1108 return Standard_False;
1110 ProjectedWire = TopoDS::Wire(Wires.First());
1111 return Standard_True;
1114 TopoDS_Vertex HYDROData_Canal3dAnd2d::ProjectVertexOntoXOY(const TopoDS_Vertex& aVertex)
1116 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
1117 gp_Pnt aProjPoint(aPoint.X(), aPoint.Y(), 0.);
1118 TopoDS_Vertex aProjVertex = BRepLib_MakeVertex(aProjPoint);
1122 void HYDROData_Canal3dAnd2d::Make2dProfile()
1124 TopoDS_Vertex ProjectedLeftVertex = ProjectVertexOntoXOY(myLeftVertex);
1125 TopoDS_Vertex ProjectedRightVertex = ProjectVertexOntoXOY(myRightVertex);
1126 TopoDS_Edge anEdge = BRepLib_MakeEdge(ProjectedLeftVertex, ProjectedRightVertex);
1127 myProjectedProfile = BRepLib_MakeWire(anEdge);
1130 void HYDROData_Canal3dAnd2d::SetMiddlePoint2d()
1132 TopoDS_Vertex V1, V2;
1133 TopExp::Vertices(myProjectedProfile, V1, V2);
1134 gp_Pnt Pnt1 = BRep_Tool::Pnt(V1);
1135 gp_Pnt Pnt2 = BRep_Tool::Pnt(V2);
1136 myMiddlePoint2d.SetXYZ(0.5*(Pnt1.XYZ() + Pnt2.XYZ()));
1139 void HYDROData_Canal3dAnd2d::SetMiddlePoint3d()
1141 gp_Lin MidLin(myMiddlePoint2d, gp::DZ());
1142 TopoDS_Edge MidEdge = BRepLib_MakeEdge(MidLin);
1143 TopoDS_Iterator itw(myProfile);
1144 for (; itw.More(); itw.Next())
1146 const TopoDS_Edge& anEdge = TopoDS::Edge(itw.Value());
1147 BRepExtrema_ExtCC ExtremaEE(MidEdge, anEdge);
1148 if (ExtremaEE.IsDone() && ExtremaEE.NbExt() != 0)
1150 for (Standard_Integer i = 1; i <= ExtremaEE.NbExt(); i++)
1152 if (ExtremaEE.SquareDistance(i) <= Precision::Confusion())
1154 myMiddlePoint3d = ExtremaEE.PointOnE1(i);
1162 TopoDS_Wire HYDROData_Canal3dAnd2d::SetTransformedProfile(const TopoDS_Wire& aProfile,
1163 const TopoDS_Wire& aGuideline,
1164 const gp_Pnt& aMiddlePoint)
1166 TopoDS_Wire aTransformedProfile;
1168 TopoDS_Iterator itw(myProjectedGuideline);
1169 TopoDS_Edge FirstEdge = TopoDS::Edge(itw.Value());
1170 Standard_Real fpar, lpar;
1171 Handle(Geom_Curve) FirstCurve = BRep_Tool::Curve(FirstEdge, fpar, lpar);
1174 FirstCurve->D1(fpar, StartPnt, StartVec);
1176 TopoDS_Vertex FirstOnGuide, LastOnGuide;
1177 TopExp::Vertices(aGuideline, FirstOnGuide, LastOnGuide);
1178 gp_Pnt StartPointOnGuide = BRep_Tool::Pnt(FirstOnGuide);
1180 //gp_Vec Offset(myMiddlePoint3d, StartPointOnGuide);
1181 gp_Trsf Translation, Rotation;
1182 Translation.SetTranslation(aMiddlePoint, StartPointOnGuide);
1183 aTransformedProfile = TopoDS::Wire(BRepBuilderAPI_Transform(aProfile, Translation, Standard_True)); //copy
1185 gp_Vec Vertical(0.,0.,1.);
1186 TopoDS_Vertex LeftVertex, RightVertex;
1187 TopExp::Vertices(aTransformedProfile, LeftVertex, RightVertex);
1188 gp_Pnt LeftPoint = BRep_Tool::Pnt(LeftVertex);
1189 gp_Pnt RightPoint = BRep_Tool::Pnt(RightVertex);
1190 gp_Vec LeftToRight(LeftPoint, RightPoint);
1191 gp_Vec NormalToProfile = Vertical ^ LeftToRight;
1193 gp_Vec AxisOfRotation = NormalToProfile ^ StartVec;
1194 if (AxisOfRotation.Magnitude() <= gp::Resolution())
1196 if (Vertical * LeftToRight < 0.)
1198 gp_Ax1 theVertical(StartPointOnGuide, gp::DZ());
1199 Rotation.SetRotation(theVertical, M_PI);
1204 gp_Ax1 theAxis(StartPointOnGuide, AxisOfRotation);
1205 Standard_Real theAngle = NormalToProfile.AngleWithRef(StartVec, AxisOfRotation);
1206 Rotation.SetRotation(theAxis, theAngle);
1209 aTransformedProfile = TopoDS::Wire(BRepBuilderAPI_Transform(aTransformedProfile, Rotation, Standard_True)); //copy
1210 return aTransformedProfile;
1213 Standard_Boolean HYDROData_Canal3dAnd2d::Create3dPresentation()
1215 myTransformedProfile3d = SetTransformedProfile(myProfile, myGuideline, myMiddlePoint3d);
1217 mySweep3d = new BRepOffsetAPI_MakePipeShell(myGuideline);
1218 mySweep3d->SetMode(gp::DZ()); //optional
1219 mySweep3d->Add(myTransformedProfile3d);
1220 //mySweep3d->SetTransitionMode(BRepBuilderAPI_RightCorner); //optional
1222 if (!mySweep3d->IsDone())
1223 return Standard_False;
1225 myPipe3d = mySweep3d->Shape();
1226 return Standard_True;
1229 Standard_Boolean HYDROData_Canal3dAnd2d::Create2dPresentation()
1231 myTransformedProfile2d = SetTransformedProfile(myProjectedProfile, myProjectedGuideline, myMiddlePoint2d);
1233 mySweep2d = new BRepOffsetAPI_MakePipeShell(myProjectedGuideline);
1234 mySweep2d->SetMode(gp::DZ()); //optional
1235 mySweep2d->Add(myTransformedProfile2d);
1236 //mySweep2d->SetTransitionMode(BRepBuilderAPI_RightCorner); //optional
1238 if (!mySweep2d->IsDone())
1239 return Standard_False;
1241 myPipe2d = mySweep2d->Shape();
1242 myInlet = TopoDS::Wire(mySweep2d->FirstShape());
1243 myOutlet = TopoDS::Wire(mySweep2d->LastShape());
1244 TopoDS_Vertex V1, V2, V3, V4;
1245 TopExp::Vertices(myTransformedProfile2d, V1, V2);
1246 TopExp::Vertices(myInlet, V3, V4);
1247 gp_Pnt P1 = BRep_Tool::Pnt(V1);
1248 gp_Pnt P3 = BRep_Tool::Pnt(V3);
1249 if (P1.IsEqual(P3, Precision::Confusion()))
1250 { myLeftVertex2d = V3; myRightVertex2d = V4; }
1252 { myLeftVertex2d = V4; myRightVertex2d = V3; }
1254 return Standard_True;
1257 TopoDS_Wire HYDROData_Canal3dAnd2d::GetBank(const TopoDS_Vertex& aFreeVertex)
1261 TopTools_ListOfShape GeneratedShapes;
1262 GeneratedShapes = mySweep2d->Generated(aFreeVertex);
1263 BRepLib_MakeWire MW;
1264 MW.Add(GeneratedShapes);
1270 TopoDS_Shape HYDROData_Canal3dAnd2d::Get3dPresentation()
1275 TopoDS_Shape HYDROData_Canal3dAnd2d::Get2dPresentation()
1280 TopoDS_Wire HYDROData_Canal3dAnd2d::GetInlet()
1285 TopoDS_Wire HYDROData_Canal3dAnd2d::GetOutlet()
1290 TopoDS_Wire HYDROData_Canal3dAnd2d::GetLeftBank()
1292 return GetBank(myLeftVertex2d);
1295 TopoDS_Wire HYDROData_Canal3dAnd2d::GetRightBank()
1297 return GetBank(myRightVertex2d);
1300 /*TopoDS_Wire HYDROData_Canal3dAnd2d::GetRoundedGuideline()
1305 TopoDS_Wire HYDROData_Canal3dAnd2d::GetProjectedRoundedGuideline()
1307 return myProjectedGuideline;