Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-26 09:02:52

0001 // Created on: 2013-05-20
0002 // Created by: Mikhail PONIKAROV
0003 // Copyright (c) 2003-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef ChFi2d_FilletAlgo_HeaderFile
0017 #define ChFi2d_FilletAlgo_HeaderFile
0018 
0019 #include <TopoDS_Edge.hxx>
0020 #include <TopoDS_Wire.hxx>
0021 #include <Geom2d_Curve.hxx>
0022 #include <Geom_Plane.hxx>
0023 #include <NCollection_List.hxx>
0024 #include <NCollection_Sequence.hxx>
0025 #include <Standard_Integer.hxx>
0026 
0027 class FilletPoint;
0028 
0029 //! Algorithm that creates fillet edge: arc tangent to two edges in the start
0030 //! and in the end vertices. Initial edges must be located on the plane and
0031 //! must be connected by the end or start points (shared vertices are not
0032 //! obligatory). Created fillet arc is created with the given radius, that is
0033 //! useful in sketcher applications.
0034 //!
0035 //! The algorithm is iterative that allows to create fillet on any curves
0036 //! of initial edges, that supports projection of point and C2 continuous.
0037 //! Principles of algorithm can de reduced to the Newton method:
0038 //! 1. Splitting initial edge into N segments where probably only 1 root can be
0039 //!    found. N depends on the complexity of the underlying curve.
0040 //! 2. On each segment compute value and derivative of the function:
0041 //!    - argument of the function is the parameter on the curve
0042 //!    - take point on the curve by the parameter: point of tangency
0043 //!    - make center of fillet: perpendicular vector from the point of tagency
0044 //!    - make projection from the center to the second curve
0045 //!    - length of the projection minus radius of the fillet is result of the
0046 //!      function
0047 //!    - derivative of this function in the point is computed by value in
0048 //!      point with small shift
0049 //! 3. Using Newton search method take the point on the segment where function
0050 //!    value is most close to zero. If it is not enough close, step 2 and 3 are
0051 //!    repeated taking as start or end point the found point.
0052 //! 4. If solution is found, result is created on point on root of the function (as a start point),
0053 //!    point of the projection onto second curve (as an end point) and center of arc in found
0054 //!    center. Initial edges are cut by the start and end point of tangency.
0055 class ChFi2d_FilletAlgo
0056 {
0057 public:
0058   //! An empty constructor of the fillet algorithm.
0059   //! Call a method Init() to initialize the algorithm
0060   //! before calling of a Perform() method.
0061   Standard_EXPORT ChFi2d_FilletAlgo();
0062 
0063   //! A constructor of a fillet algorithm: accepts a wire consisting of two edges in a plane.
0064   Standard_EXPORT ChFi2d_FilletAlgo(const TopoDS_Wire& theWire, const gp_Pln& thePlane);
0065 
0066   //! A constructor of a fillet algorithm: accepts two edges in a plane.
0067   Standard_EXPORT ChFi2d_FilletAlgo(const TopoDS_Edge& theEdge1,
0068                                     const TopoDS_Edge& theEdge2,
0069                                     const gp_Pln&      thePlane);
0070 
0071   //! Initializes a fillet algorithm: accepts a wire consisting of two edges in a plane.
0072   Standard_EXPORT void Init(const TopoDS_Wire& theWire, const gp_Pln& thePlane);
0073 
0074   //! Initializes a fillet algorithm: accepts two edges in a plane.
0075   Standard_EXPORT void Init(const TopoDS_Edge& theEdge1,
0076                             const TopoDS_Edge& theEdge2,
0077                             const gp_Pln&      thePlane);
0078 
0079   //! Constructs a fillet edge.
0080   //! Returns true, if at least one result was found
0081   Standard_EXPORT bool Perform(const double theRadius);
0082 
0083   //! Returns number of possible solutions.
0084   //! <thePoint> chooses a particular fillet in case of several fillets
0085   //! may be constructed (for example, a circle intersecting a segment in 2 points).
0086   //! Put the intersecting (or common) point of the edges.
0087   Standard_EXPORT int NbResults(const gp_Pnt& thePoint);
0088 
0089   //! Returns result (fillet edge, modified edge1, modified edge2),
0090   //! nearest to the given point <thePoint> if iSolution == -1.
0091   //! <thePoint> chooses a particular fillet in case of several fillets
0092   //! may be constructed (for example, a circle intersecting a segment in 2 points).
0093   //! Put the intersecting (or common) point of the edges.
0094   Standard_EXPORT TopoDS_Edge Result(const gp_Pnt& thePoint,
0095                                      TopoDS_Edge&  theEdge1,
0096                                      TopoDS_Edge&  theEdge2,
0097                                      const int     iSolution = -1);
0098 
0099 private:
0100   //! Computes the value the function in the current point.
0101   //! <theLimit> is end parameter of the segment
0102   void FillPoint(FilletPoint*, const double theLimit);
0103   //! Computes the derivative value of the function in the current point.
0104   //! <theDiffStep> is small step for approximate derivative computation
0105   //! <theFront> is direction of the step: from or reversed
0106   void FillDiff(FilletPoint*, double theDiffStep, bool theFront);
0107   //! Using Newton methods computes optimal point, that can be root of the
0108   //! function taking into account two input points, functions value and derivatives.
0109   //! Performs iteration until root is found or failed to find root.
0110   //! Stores roots in myResultParams.
0111   void PerformNewton(FilletPoint*, FilletPoint*);
0112   //! Splits segment by the parameter and calls Newton method for both segments.
0113   //! It supplies recursive iterations of the Newton methods calls
0114   //! (PerformNewton calls this function and this calls Netwton two times).
0115   bool ProcessPoint(FilletPoint*, FilletPoint*, double);
0116 
0117   //! Initial edges where the fillet must be computed.
0118   TopoDS_Edge myEdge1, myEdge2;
0119   //! Plane where fillet arc must be created.
0120   occ::handle<Geom_Plane> myPlane;
0121   //! Underlying curves of the initial edges
0122   occ::handle<Geom2d_Curve> myCurve1, myCurve2;
0123   //! Start and end parameters of curves of initial edges.
0124   double myStart1, myEnd1, myStart2, myEnd2, myRadius;
0125   //! List of params where roots were found.
0126   NCollection_List<double> myResultParams;
0127   //! sequence of 0 or 1: position of the fillet relatively to the first curve
0128   NCollection_Sequence<int> myResultOrientation;
0129   //! position of the fillet relatively to the first curve
0130   bool myStartSide;
0131   //! are initial edges where exchanged in the beginning: to make first edge
0132   //! more simple and minimize number of iterations
0133   bool myEdgesExchnged;
0134   //! Number to avoid infinity recursion: indicates how deep the recursion is performed.
0135   int myDegreeOfRecursion;
0136 };
0137 
0138 //! Private class. Corresponds to the point on the first curve, computed
0139 //! fillet function and derivative on it.
0140 class FilletPoint
0141 {
0142 public:
0143   //! Creates a point on a first curve by parameter on this curve.
0144   FilletPoint(const double theParam);
0145 
0146   //! Changes the point position by changing point parameter on the first curve.
0147   void setParam(double theParam) { myParam = theParam; }
0148 
0149   //! Returns the point parameter on the first curve.
0150   double getParam() const { return myParam; }
0151 
0152   //! Returns number of found values of function in this point.
0153   int getNBValues() { return myV.Length(); }
0154 
0155   //! Returns value of function in this point.
0156   double getValue(int theIndex) { return myV.Value(theIndex); }
0157 
0158   //! Returns derivatives of function in this point.
0159   double getDiff(int theIndex) { return myD.Value(theIndex); }
0160 
0161   //! Returns true if function is valid (rediuses vectors of fillet do not intersect any curve).
0162   bool isValid(int theIndex) { return myValid.Value(theIndex); }
0163 
0164   //! Returns the index of the nearest value
0165   int getNear(int theIndex) { return myNear.Value(theIndex); }
0166 
0167   //! Defines the parameter of the projected point on the second curve.
0168   void setParam2(const double theParam2) { myParam2 = theParam2; }
0169 
0170   //! Returns the parameter of the projected point on the second curve.
0171   double getParam2() { return myParam2; }
0172 
0173   //! Center of the fillet.
0174   void setCenter(const gp_Pnt2d thePoint) { myCenter = thePoint; }
0175 
0176   //! Center of the fillet.
0177   const gp_Pnt2d getCenter() { return myCenter; }
0178 
0179   //! Appends value of the function.
0180   void appendValue(double theValue, bool theValid);
0181 
0182   //! Computes difference between this point and the given. Stores difference in myD.
0183   bool calculateDiff(FilletPoint*);
0184 
0185   //! Filters out the values and leaves the most optimal one.
0186   void FilterPoints(FilletPoint*);
0187 
0188   //! Returns a pointer to created copy of the point
0189   //! warning: this is not the full copy! Copies only: myParam, myV, myD, myValid
0190   FilletPoint* Copy();
0191 
0192   //! Returns the index of the solution or zero if there is no solution
0193   int hasSolution(double theRadius);
0194 
0195   //! For debug only
0196   double LowerValue()
0197   {
0198     int    a, aResultIndex = 0;
0199     double aValue;
0200     for (a = myV.Length(); a > 0; a--)
0201     {
0202       if (aResultIndex == 0 || std::abs(aValue) > std::abs(myV.Value(a)))
0203       {
0204         aResultIndex = a;
0205         aValue       = myV.Value(a);
0206       }
0207     }
0208     return aValue;
0209   }
0210 
0211   //! Removes the found value by the given index.
0212   void remove(int theIndex);
0213 
0214 private:
0215   //! Parameter on the first curve (start fillet point).
0216   double myParam;
0217   //! Parameter on the second curve (end fillet point).
0218   double myParam2;
0219   //! Values and derivative values of the fillet function.
0220   //! May be several if there are many projections on the second curve.
0221   NCollection_Sequence<double> myV, myD;
0222   //! Center of the fillet arc.
0223   gp_Pnt2d myCenter;
0224   //! Flags for storage the validity of solutions. Indexes corresponds to indexes
0225   //! in sequences myV, myD.
0226   NCollection_Sequence<bool> myValid;
0227   NCollection_Sequence<int>  myNear;
0228 };
0229 
0230 #endif // _FILLETALGO_H_