0025720: Incorrect code of math classes can lead to unpredicted behavior of algorithms
[occt.git] / src / Geom2dGcc / Geom2dGcc_Lin2d2TanIter.cxx
1 // Created on: 1991-12-20
2 // Created by: Remi GILET
3 // Copyright (c) 1991-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 //  CREATION D UNE LIGNE TANGENTE A DEUX COURBES.                        +
19 //========================================================================
20
21 #include <Geom2dGcc_Lin2d2TanIter.ixx>
22
23 #include <StdFail_NotDone.hxx>
24 #include <GccEnt_BadQualifier.hxx>
25 #include <gp_XY.hxx>
26 #include <gp_Dir2d.hxx>
27 #include <gp_Vec2d.hxx>
28 #include <gp_Circ2d.hxx>
29 #include <math_Vector.hxx>
30 #include <math_Matrix.hxx>
31 #include <math_FunctionSetRoot.hxx>
32 #include <math_FunctionRoot.hxx>
33
34 #include <Geom2dGcc_CurveTool.hxx>
35
36 #include <Geom2dGcc_FunctionTanCuCu.hxx>
37 #include <Geom2dGcc_FunctionTanCuPnt.hxx>
38 #include <Geom2dGcc_FunctionTanCirCu.hxx>
39
40 Geom2dGcc_Lin2d2TanIter::
41 Geom2dGcc_Lin2d2TanIter (const GccEnt_QualifiedCirc& Qualified1 ,
42                          const Geom2dGcc_QCurve&     Qualified2 ,
43                          const Standard_Real         Param2     ,
44                          const Standard_Real         Tolang     ) {
45
46                            par1sol = 0.;
47                            pararg1 = 0.;
48
49                            //Standard_Real Tol = Abs(Tolang);
50
51                            WellDone = Standard_False;
52                            if (Qualified1.IsEnclosed()) { GccEnt_BadQualifier::Raise(); }
53                            gp_Circ2d C1 = Qualified1.Qualified();
54                            Geom2dAdaptor_Curve Cu2 = Qualified2.Qualified();
55                            Standard_Real U1 = Geom2dGcc_CurveTool::FirstParameter(Cu2);
56                            Standard_Real U2 = Geom2dGcc_CurveTool::LastParameter(Cu2);
57                            Geom2dGcc_FunctionTanCirCu func(C1,Cu2);
58                            math_FunctionRoot sol(func,Param2,Geom2dGcc_CurveTool::EpsX(Cu2,Abs(Tolang)),U1,U2,100);
59                            if (sol.IsDone()) {
60                              Standard_Real Usol = sol.Root();
61                              //     gp_Pnt2d Origine,Pt;
62                              //  Modified by Sergey KHROMOV - Thu Apr  5 17:39:47 2001 Begin
63                              Standard_Real Norm;
64                              func.Value(Usol, Norm);
65                              if (Abs(Norm) < Tolang) {
66                                //  Modified by Sergey KHROMOV - Thu Apr  5 17:39:48 2001 End
67                                gp_Pnt2d Origine;
68                                gp_Vec2d Vect1;
69                                gp_Vec2d Vect2;
70                                Geom2dGcc_CurveTool::D2(Cu2,Usol,Origine,Vect1,Vect2);
71                                gp_Vec2d Vdir(C1.Location().XY() - Origine.XY());
72                                Standard_Real sign1 = Vect1.Dot(Vdir);
73                                if (sign1 <= 0. ) { Vect1.Reverse(); }
74                                Standard_Real sign2 = Vect2.Crossed(Vect1);
75                                if (Qualified2.IsUnqualified() || 
76                                  (Qualified2.IsEnclosing() && sign2<=0.) ||
77                                  (Qualified2.IsOutside() && sign1 <= 0. && sign2 >= 0.) ||
78                                  (Qualified2.IsEnclosed() && sign1 >= 0. && sign2 >= 0.)) {
79                                    if (Qualified1.IsUnqualified() ||
80                                      (Qualified1.IsOutside() && Vect1.Angle(Vdir) <= 0.) ||
81                                      (Qualified1.IsEnclosing() && Vect1.Angle(Vdir) >= 0.)) {
82                                        gp_Dir2d direc(Vect1);
83                                        Standard_Real R1 = C1.Radius();
84                                        gp_XY normal(-R1*direc.Y(),R1*direc.X());
85                                        sign1 = Vect1.Crossed(Vdir);
86                                        if (Qualified1.IsEnclosing()) {
87                                          pnttg1sol = gp_Pnt2d(C1.Location().XY()-normal);
88                                        }
89                                        else if (Qualified1.IsOutside()) {
90                                          pnttg1sol = gp_Pnt2d(C1.Location().XY()+normal);
91                                        }
92                                        else {
93                                          if (sign1 >= 0.) {
94                                            pnttg1sol = gp_Pnt2d(C1.Location().XY()-normal);
95                                          }
96                                          else {
97                                            pnttg1sol = gp_Pnt2d(C1.Location().XY()+normal);
98                                          }
99                                        }
100                                        //        if (gp_Vec2d(direc.XY()).Angle(gp_Vec2d(pnttg1sol,Origine)) <= Tol) {
101                                        pnttg2sol = Origine;
102                                        linsol = gp_Lin2d(pnttg1sol,direc);
103                                        WellDone = Standard_True;
104                                        qualifier1 = Qualified1.Qualifier();
105                                        qualifier2 = Qualified2.Qualifier();
106                                        pararg2 = Usol;
107                                        par1sol = 0.;
108                                        par2sol = pnttg2sol.Distance(pnttg1sol);
109                                        pararg1 = 0.;
110                                    }
111                                }
112                              }
113                            }
114 }
115
116 Geom2dGcc_Lin2d2TanIter::
117 Geom2dGcc_Lin2d2TanIter (const Geom2dGcc_QCurve& Qualified1 ,
118                          const Geom2dGcc_QCurve& Qualified2 ,
119                          const Standard_Real      Param1     ,
120                          const Standard_Real      Param2     ,
121                          const Standard_Real      Tolang     ) {
122                            par1sol = 0.;
123                            pararg1 = 0.;
124                            WellDone = Standard_False;
125                            if (!(Qualified1.IsEnclosed() || Qualified1.IsEnclosing() || 
126                              Qualified1.IsOutside() || Qualified1.IsUnqualified()) ||
127                              !(Qualified2.IsEnclosed() || Qualified2.IsEnclosing() || 
128                              Qualified2.IsOutside() || Qualified2.IsUnqualified())) {
129                                GccEnt_BadQualifier::Raise();
130                                return;
131                            }
132                            Geom2dAdaptor_Curve Cu1 = Qualified1.Qualified();
133                            Geom2dAdaptor_Curve Cu2 = Qualified2.Qualified();
134                            Geom2dGcc_FunctionTanCuCu Func(Cu1,Cu2);
135                            math_Vector Umin(1,2);
136                            math_Vector Umax(1,2);
137                            math_Vector Ufirst(1,2);
138                            math_Vector tol(1,2);
139                            Umin(1) = Geom2dGcc_CurveTool::FirstParameter(Cu1);
140                            Umin(2) = Geom2dGcc_CurveTool::FirstParameter(Cu2);
141                            Umax(1) = Geom2dGcc_CurveTool::LastParameter(Cu1);
142                            Umax(2) = Geom2dGcc_CurveTool::LastParameter(Cu2);
143                            Ufirst(1) = Param1;
144                            Ufirst(2) = Param2;
145                            tol(1) = Geom2dGcc_CurveTool::EpsX(Cu1,Abs(Tolang));
146                            tol(2) = Geom2dGcc_CurveTool::EpsX(Cu2,Abs(Tolang));
147                            math_FunctionSetRoot Root(Func, tol);
148                            Root.Perform(Func, Ufirst, Umin, Umax);
149                            if (Root.IsDone()) {
150                              Root.Root(Ufirst);
151                              //  Modified by Sergey KHROMOV - Thu Apr  5 17:45:00 2001 Begin
152                              math_Vector Norm(1,2);
153                              Func.Value(Ufirst, Norm);
154                              if (Abs(Norm(1)) < Tolang && Abs(Norm(2)) < Tolang) {
155                                //  Modified by Sergey KHROMOV - Thu Apr  5 17:45:01 2001 End
156                                gp_Pnt2d point1,point2;
157                                gp_Vec2d Vect11,Vect12,Vect21,Vect22;
158                                Geom2dGcc_CurveTool::D2(Cu1,Ufirst(1),point1,Vect11,Vect12);
159                                Geom2dGcc_CurveTool::D2(Cu2,Ufirst(2),point2,Vect21,Vect22);
160                                gp_Vec2d Vec(point1.XY(),point2.XY());
161                                Standard_Real Angle1 = Vec.Angle(Vect12);
162                                Standard_Real sign1 = Vect11.Dot(Vec);
163                                if (Qualified1.IsUnqualified() || 
164                                  (Qualified1.IsEnclosing() && Angle1 >= 0.) ||
165                                  (Qualified1.IsOutside() && Angle1 <= 0. && sign1 <= 0.) ||
166                                  (Qualified1.IsEnclosed() && Angle1 <= 0. && sign1 >= 0.)) {
167                                    Angle1 = Vec.Angle(Vect22);
168                                    sign1 = Vect21.Dot(Vec);
169                                    if (Qualified2.IsUnqualified() || 
170                                      (Qualified2.IsEnclosing() && Angle1 >= 0.) ||
171                                      (Qualified2.IsOutside() && Angle1 <= 0. && sign1 <= 0.) ||
172                                      (Qualified2.IsEnclosed() && Angle1 <= 0. && sign1 >= 0.)) {
173                                        qualifier1 = Qualified1.Qualifier();
174                                        qualifier2 = Qualified2.Qualifier();
175                                        pararg1 = Ufirst(1);
176                                        par1sol = 0.;
177                                        pnttg1sol = point1;
178                                        pararg2 = Ufirst(2);
179                                        pnttg2sol = point2;
180                                        par2sol = pnttg2sol.Distance(pnttg1sol);
181                                        gp_Dir2d dir(pnttg2sol.X()-pnttg1sol.X(),pnttg2sol.Y()-pnttg1sol.Y());
182                                        linsol = gp_Lin2d(pnttg1sol,dir);
183                                        WellDone = Standard_True;
184                                    }
185                                }
186                              }
187                            }
188 }
189
190 Geom2dGcc_Lin2d2TanIter::
191 Geom2dGcc_Lin2d2TanIter (const Geom2dGcc_QCurve& Qualified1 ,
192                          const gp_Pnt2d&          ThePoint   ,
193                          const Standard_Real      Param1     ,
194                          const Standard_Real      Tolang     ) {
195
196                            par1sol = 0.;
197                            pararg1 = 0.;
198                            WellDone = Standard_False;
199                            if (!(Qualified1.IsEnclosed() || Qualified1.IsEnclosing() || 
200                              Qualified1.IsOutside() || Qualified1.IsUnqualified())) {
201                                GccEnt_BadQualifier::Raise();
202                                return;
203                            }
204                            Geom2dAdaptor_Curve Cu1 = Qualified1.Qualified();
205                            Standard_Real U1 = Geom2dGcc_CurveTool::FirstParameter(Cu1);
206                            Standard_Real U2 = Geom2dGcc_CurveTool::LastParameter(Cu1);
207                            Geom2dGcc_FunctionTanCuPnt func(Cu1,ThePoint);
208                            math_FunctionRoot sol(func,Param1,Geom2dGcc_CurveTool::EpsX(Cu1,Abs(Tolang)),U1,U2,100);
209                            if (sol.IsDone()) {
210                              Standard_Real Usol = sol.Root();
211                              //  Modified by Sergey KHROMOV - Thu Apr  5 17:45:17 2001 Begin
212                              Standard_Real Norm;
213                              func.Value(Usol, Norm);
214                              if (Abs(Norm) < Tolang) {
215                                //  Modified by Sergey KHROMOV - Thu Apr  5 17:45:19 2001 End
216                                gp_Pnt2d Origine;
217                                gp_Vec2d Vect1;
218                                gp_Vec2d Vect2;
219                                Geom2dGcc_CurveTool::D2(Cu1,Usol,Origine,Vect1,Vect2);
220                                gp_Vec2d Vdir(ThePoint.XY()-Origine.XY());
221                                Standard_Real sign1 = Vect1.Dot(Vdir);
222                                Standard_Real sign2 = Vect2.Crossed(Vdir);
223                                if (Qualified1.IsUnqualified() || 
224                                  (Qualified1.IsEnclosing() && 
225                                  ((sign1 >= 0. && sign2 <= 0.) || (sign1 <= 0. && sign2 <= 0.))) ||
226                                  (Qualified1.IsOutside() && sign1 <= 0. && sign2 >= 0.) ||
227                                  (Qualified1.IsEnclosed() && sign1 >= 0. && sign2 >= 0.)) {
228                                    WellDone = Standard_True;
229                                    linsol = gp_Lin2d(Origine,gp_Dir2d(Vdir));
230                                    qualifier1 = Qualified1.Qualifier();
231                                    qualifier2 = GccEnt_noqualifier;
232                                    pnttg1sol = Origine;
233                                    pnttg2sol = ThePoint;
234                                    pararg1 = Usol;
235                                    par1sol = 0.;
236                                    pararg2 = ThePoint.Distance(Origine);
237                                    par2sol = 0.;
238                                }
239                              }
240                            }
241 }
242
243 Standard_Boolean Geom2dGcc_Lin2d2TanIter::
244 IsDone () const { return WellDone; }
245
246 gp_Lin2d Geom2dGcc_Lin2d2TanIter::
247 ThisSolution () const 
248 {
249   if (!WellDone) StdFail_NotDone::Raise();
250   return linsol;
251 }
252
253 void Geom2dGcc_Lin2d2TanIter:: 
254 WhichQualifier (GccEnt_Position& Qualif1  ,
255                 GccEnt_Position& Qualif2  ) const
256 {
257   if (!WellDone) { StdFail_NotDone::Raise(); }
258   else {
259     Qualif1 = qualifier1;
260     Qualif2 = qualifier2;
261   }
262 }
263
264 void Geom2dGcc_Lin2d2TanIter::
265 Tangency1 (Standard_Real& ParSol ,
266            Standard_Real& ParArg ,
267            gp_Pnt2d& Pnt) const {
268              if (!WellDone) { StdFail_NotDone::Raise(); }
269              else {
270                ParSol = par1sol;
271                ParArg = pararg1;
272                Pnt    = pnttg1sol;
273              }
274 }
275
276 void Geom2dGcc_Lin2d2TanIter::
277 Tangency2 (Standard_Real& ParSol ,
278            Standard_Real& ParArg ,
279            gp_Pnt2d& Pnt) const {
280              if (!WellDone) { StdFail_NotDone::Raise(); }
281              else {
282                ParSol = par2sol;
283                ParArg = pararg2;
284                Pnt    = pnttg2sol;
285              }
286 }
287