0029694: Geom2dGcc_Circ2dTanCenGeo crash
[occt.git] / src / Geom2dGcc / Geom2dGcc_Circ2dTanCenGeo.cxx
1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15
16 #include <Extrema_ExtPC2d.hxx>
17 #include <Extrema_POnCurv2d.hxx>
18 #include <GccEnt_BadQualifier.hxx>
19 #include <Geom2dGcc_Circ2dTanCenGeo.hxx>
20 #include <Geom2dGcc_CurveTool.hxx>
21 #include <Geom2dGcc_QCurve.hxx>
22 #include <gp.hxx>
23 #include <gp_Ax2d.hxx>
24 #include <gp_Circ2d.hxx>
25 #include <gp_Pnt2d.hxx>
26 #include <gp_Vec2d.hxx>
27 #include <Standard_Failure.hxx>
28 #include <Standard_OutOfRange.hxx>
29 #include <StdFail_NotDone.hxx>
30 #include <TColStd_Array1OfInteger.hxx>
31 #include <TColStd_Array1OfReal.hxx>
32
33 //=========================================================================
34 //   Creation d un cercle tangent a une courbe centre en un point.        +
35 //=========================================================================
36 Geom2dGcc_Circ2dTanCenGeo::
37 Geom2dGcc_Circ2dTanCenGeo (const Geom2dGcc_QCurve&  Qualified1,
38                            const gp_Pnt2d&          Pcenter   ,
39                            const Standard_Real      Tolerance ):
40
41 //========================================================================
42 //   Initialisation des champs.                                          +
43 //========================================================================
44   WellDone(Standard_False),
45   NbrSol(0),
46   cirsol(1, 2),
47   qualifier1(1, 2),
48   pnttg1sol(1, 2),
49   par1sol(1, 2),
50   pararg1(1, 2)
51 {
52   Standard_Real Tol = Abs(Tolerance);
53   TColgp_Array1OfPnt2d pTan(1,2);
54   TColStd_Array1OfInteger Index(1,2);
55   TColStd_Array1OfReal theDist2(1,2);
56   TColStd_Array1OfReal theParam(1,2);
57   theDist2(1) = RealLast();
58   theDist2(2) = 0.;
59   Standard_Integer i = 1;
60   Standard_Integer nbsol = 0;
61   gp_Dir2d dirx(1.0,0.0);
62   Standard_Real thePar;
63   Geom2dAdaptor_Curve curve = Qualified1.Qualified();
64   Extrema_ExtPC2d distmin(Pcenter, curve, Geom2dGcc_CurveTool::FirstParameter(curve),
65                           Geom2dGcc_CurveTool::LastParameter(curve), Tol);
66   if (!distmin.IsDone() ) { throw Standard_Failure(); }
67   Standard_Integer nbext = distmin.NbExt();
68   if(nbext==0) { throw Standard_Failure(); }
69   while (i<=nbext) {
70     thePar = distmin.Point(i).Parameter();
71     if (distmin.SquareDistance(i)<theDist2(1)) {
72         theDist2(1) = distmin.SquareDistance(i);
73         theParam(1) = thePar;
74         pTan(1) = distmin.Point(i).Value();
75     }
76     if (distmin.SquareDistance(i)>theDist2(2)) {
77         theDist2(2) = distmin.SquareDistance(i);
78         theParam(2) = thePar;
79         pTan(2) = distmin.Point(i).Value();
80     }
81     i++;
82   }
83   if (Index(1) == Index(2)) { nbsol = 1; }
84   else { nbsol = 2; }
85   for (i = 1 ; i <= nbsol; i++) {
86     gp_Pnt2d point1;
87     gp_Vec2d Tan1;
88     Geom2dGcc_CurveTool::D1(curve,theParam(i),point1,Tan1);
89     Standard_Real normetan1 = Tan1.Magnitude();
90     gp_Vec2d Vec1(point1,Pcenter);
91     Standard_Real normevec1 = Vec1.Magnitude();
92     Standard_Real dot1;
93     if (normevec1 >= gp::Resolution() && normetan1 >= gp::Resolution()) {
94       dot1 = Vec1.Dot(Tan1)/(normevec1*normetan1);
95     }
96     else { dot1 = 0.; }
97     Tol = 1.e-12;
98     if (dot1 <= Tol) {
99       Standard_Real Angle1 = Vec1.Angle(Tan1);
100       if (Qualified1.IsUnqualified()||
101         (Qualified1.IsEnclosing()&&Angle1<=0.)||
102         (Qualified1.IsOutside() && Angle1 >= 0.) ||
103         (Qualified1.IsEnclosed() && Angle1 <= 0.)) {
104           NbrSol++;
105           cirsol(NbrSol) = gp_Circ2d(gp_Ax2d(Pcenter,dirx),sqrt (theDist2(i)));
106           qualifier1(NbrSol) = Qualified1.Qualifier();
107           pararg1(NbrSol) = theParam(i);
108           par1sol(NbrSol) = 0.;
109           pnttg1sol(NbrSol) = pTan(i);
110           WellDone = Standard_True;
111       }
112     }
113   }
114 }
115
116
117
118 //=========================================================================
119
120
121 Standard_Boolean Geom2dGcc_Circ2dTanCenGeo::
122 IsDone () const { return WellDone; }
123
124 Standard_Integer Geom2dGcc_Circ2dTanCenGeo::
125 NbSolutions () const { return NbrSol; }
126
127 gp_Circ2d Geom2dGcc_Circ2dTanCenGeo::
128 ThisSolution (const Standard_Integer Index) const 
129 {
130   if (Index > NbrSol || Index <= 0) throw Standard_OutOfRange();
131
132   return cirsol(Index);
133 }
134
135 void Geom2dGcc_Circ2dTanCenGeo::
136 WhichQualifier(const Standard_Integer Index   ,
137                GccEnt_Position& Qualif1 ) const
138 {
139   if (!WellDone) { throw StdFail_NotDone(); }
140   else if (Index <= 0 ||Index > NbrSol) { throw Standard_OutOfRange(); }
141   else {
142     Qualif1 = qualifier1(Index);
143   }
144 }
145
146 void Geom2dGcc_Circ2dTanCenGeo::
147 Tangency1 (const Standard_Integer Index,
148            Standard_Real&   ParSol,
149            Standard_Real&   ParArg,
150            gp_Pnt2d& PntSol) const{
151              if (!WellDone) {
152                throw StdFail_NotDone();
153              }
154              else if (Index <= 0 ||Index > NbrSol) {
155                throw Standard_OutOfRange();
156              }
157              else {
158                PntSol = gp_Pnt2d(pnttg1sol(Index));
159                ParSol = par1sol(Index);
160                ParArg = pararg1(Index);
161              }
162 }
163