014af8c906c355fc3ec3cb21ce302f4f78d1ba6b
[occt.git] / src / GCPnts / GCPnts_TangentialDeflection.cxx
1 // Created on: 1996-11-08
2 // Created by: Jean Claude VAUTHIER
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <Adaptor2d_Curve2d.hxx>
19 #include <Adaptor3d_Curve.hxx>
20 #include <GCPnts_TangentialDeflection.hxx>
21 #include <gp_Pnt.hxx>
22 #include <gp_Pnt2d.hxx>
23 #include <gp_Vec.hxx>
24 #include <gp_Vec2d.hxx>
25 #include <gp_XYZ.hxx>
26 #include <Precision.hxx>
27 #include <Standard_ConstructionError.hxx>
28 #include <Standard_OutOfRange.hxx>
29 #include <TColStd_Array1OfReal.hxx>
30
31 inline static void D0 (const Adaptor3d_Curve& C, const Standard_Real U, gp_Pnt& P)
32 {
33   C.D0 (U, P);
34 }
35
36 inline static void D2 (const Adaptor3d_Curve& C, const Standard_Real U, 
37                        gp_Pnt& P, gp_Vec& V1, gp_Vec& V2)
38 {
39   C.D2 (U, P, V1, V2);
40 }
41
42
43 static void D0 (const Adaptor2d_Curve2d& C, const Standard_Real U, gp_Pnt& PP)
44 {
45   Standard_Real X, Y;
46   gp_Pnt2d P;
47   C.D0 (U, P);
48   P.Coord (X, Y);
49   PP.SetCoord (X, Y, 0.0);
50 }
51
52 static void D2 (const Adaptor2d_Curve2d& C, const Standard_Real U,
53                 gp_Pnt& PP, gp_Vec& VV1, gp_Vec& VV2)
54 {
55   Standard_Real X, Y;
56   gp_Pnt2d P;
57   gp_Vec2d V1,V2;
58   C.D2 (U, P, V1, V2);
59   P.Coord (X, Y);
60   PP.SetCoord  (X, Y, 0.0);
61   V1.Coord (X, Y);
62   VV1.SetCoord (X, Y, 0.0);
63   V2.Coord (X, Y);
64   VV2.SetCoord (X, Y, 0.0);
65 }
66
67 // Return number of interval of continuity on which theParam is located.
68 // Last parameter is used to increase search speed.
69 static Standard_Integer getIntervalIdx(const Standard_Real theParam, 
70                                        TColStd_Array1OfReal& theIntervs,
71                                        const Standard_Integer thePreviousIdx)
72 {
73   Standard_Integer anIdx;
74   for(anIdx = thePreviousIdx; anIdx < theIntervs.Upper(); anIdx++)
75   {
76     if (theParam >= theIntervs(anIdx) && 
77         theParam <= theIntervs(anIdx + 1)) // Inside of anIdx interval.
78     {
79       break;
80     }
81   }
82   return anIdx;
83 }
84
85 //=======================================================================
86 //function : CPnts_TangentialDeflection
87 //purpose  : 
88 //=======================================================================
89
90 GCPnts_TangentialDeflection::GCPnts_TangentialDeflection () { }
91
92 //=======================================================================
93 //function : AddPoint
94 //purpose  : 
95 //=======================================================================
96
97 Standard_Integer GCPnts_TangentialDeflection::AddPoint
98  (const gp_Pnt& thePnt,
99   const Standard_Real theParam,
100   const Standard_Boolean theIsReplace)
101 {
102   const Standard_Real tol = Precision::PConfusion();
103   Standard_Integer index = -1;
104   const Standard_Integer nb = parameters.Length();
105   for ( Standard_Integer i = 1; index == -1 && i <= nb; i++ )
106   {
107     Standard_Real dist = parameters.Value( i ) - theParam;
108     if ( fabs( dist ) <= tol )
109     {
110       index = i;
111       if ( theIsReplace )
112       {
113         points.ChangeValue(i) = thePnt;
114         parameters.ChangeValue(i) = theParam;
115       }
116     }
117     else if ( dist > tol )
118     {
119       points.InsertBefore( i, thePnt );
120       parameters.InsertBefore( i, theParam );
121       index = i;
122     }
123   }
124   if ( index == -1 )
125   {
126     points.Append( thePnt );
127     parameters.Append( theParam );
128     index = parameters.Length();
129   }
130   return index;
131 }
132
133 //=======================================================================
134 //function : ArcAngularStep
135 //purpose  : 
136 //=======================================================================
137 Standard_Real GCPnts_TangentialDeflection::ArcAngularStep(
138   const Standard_Real theRadius,
139   const Standard_Real theLinearDeflection,
140   const Standard_Real theAngularDeflection,
141   const Standard_Real theMinLength)
142 {
143   Standard_ConstructionError_Raise_if(theRadius < 0.0, "Negative radius");
144
145   const Standard_Real aPrecision = Precision::Confusion();
146
147   Standard_Real Du = 0.0, aMinSizeAng = 0.0;
148   if (theRadius > aPrecision)
149   {
150     Du = Max(1.0 - (theLinearDeflection / theRadius), 0.0);
151
152     // It is not suitable to consider min size greater than 1/4 arc len.
153     if (theMinLength > aPrecision)
154       aMinSizeAng = Min(theMinLength / theRadius, M_PI_2);
155   }
156   Du = 2.0 * ACos(Du);
157   Du = Max(Min(Du, theAngularDeflection), aMinSizeAng);
158   return Du;
159 }
160
161 #include <Geom_BezierCurve.hxx>
162 #include <Geom_BSplineCurve.hxx>
163 #include <gp_Circ.hxx>
164 #define TheCurve Adaptor3d_Curve
165 #define Handle_TheBezierCurve   Handle(Geom_BezierCurve)
166 #define Handle_TheBSplineCurve  Handle(Geom_BSplineCurve)
167 #include <GCPnts_TangentialDeflection.gxx>
168 #undef Handle_TheBezierCurve
169 #undef Handle_TheBSplineCurve
170 #undef TheCurve
171
172
173 #include <Geom2d_BezierCurve.hxx>
174 #include <Geom2d_BSplineCurve.hxx>
175 #include <gp_Circ2d.hxx>
176 #define TheCurve Adaptor2d_Curve2d
177 #define Handle_TheBezierCurve   Handle(Geom2d_BezierCurve)
178 #define Handle_TheBSplineCurve  Handle(Geom2d_BSplineCurve)
179 #include <GCPnts_TangentialDeflection.gxx>
180 #undef Handle_TheBezierCurve
181 #undef Handle_TheBSplineCurve
182 #undef TheCurve