1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 // init. de MinRad et MaxRad (PRO15604), JCT 09/10/98
18 #include <GccAna_Circ2d3Tan.hxx>
19 #include <GccAna_CircLin2dBisec.hxx>
20 #include <GccAna_LinPnt2dBisec.hxx>
21 #include <GccEnt_BadQualifier.hxx>
22 #include <GccEnt_QualifiedCirc.hxx>
23 #include <GccEnt_QualifiedLin.hxx>
24 #include <GccInt_BLine.hxx>
25 #include <GccInt_BParab.hxx>
26 #include <GccInt_IType.hxx>
27 #include <gp_Circ2d.hxx>
28 #include <gp_Dir2d.hxx>
29 #include <gp_Lin2d.hxx>
30 #include <gp_Pnt2d.hxx>
31 #include <IntAna2d_AnaIntersection.hxx>
32 #include <IntAna2d_Conic.hxx>
33 #include <IntAna2d_IntPoint.hxx>
34 #include <Standard_OutOfRange.hxx>
35 #include <StdFail_NotDone.hxx>
36 #include <TColStd_Array1OfReal.hxx>
38 //===========================================================================
39 // Creation of a circle tangent to a circle, a straight line and a point. +
40 //===========================================================================
42 GccAna_Circ2d3Tan (const GccEnt_QualifiedCirc& Qualified1 ,
43 const GccEnt_QualifiedLin& Qualified2 ,
44 const gp_Pnt2d& Point3 ,
45 const Standard_Real Tolerance ):
47 //=========================================================================
48 // Initialization of fields. +
49 //=========================================================================
69 gp_Dir2d dirx(1.0,0.0);
70 Standard_Real Tol = Abs(Tolerance);
71 Standard_Real MaxRad = 1e10, MinRad = 1e-6;
72 WellDone = Standard_False;
74 if (!(Qualified1.IsEnclosed() || Qualified1.IsEnclosing() ||
75 Qualified1.IsOutside() || Qualified1.IsUnqualified()) ||
76 !(Qualified2.IsEnclosed() ||
77 Qualified2.IsOutside() || Qualified2.IsUnqualified())) {
78 throw GccEnt_BadQualifier();
82 //=========================================================================
84 //=========================================================================
86 gp_Circ2d C1(Qualified1.Qualified());
87 gp_Lin2d L2(Qualified2.Qualified());
88 Standard_Real R1 = C1.Radius();
89 gp_Pnt2d center1(C1.Location());
90 gp_Pnt2d origin2(L2.Location());
91 gp_Dir2d dir2(L2.Direction());
92 gp_Dir2d normL2(-dir2.Y(),dir2.X());
94 TColStd_Array1OfReal Radius(1,2);
95 GccAna_CircLin2dBisec Bis1(C1,L2);
96 GccAna_LinPnt2dBisec Bis2(L2,Point3);
97 if (Bis1.IsDone() && Bis2.IsDone()) {
98 Standard_Integer nbsolution1 = Bis1.NbSolutions();
99 for (Standard_Integer i = 1 ; i <= nbsolution1; i++) {
100 Handle(GccInt_Bisec) Sol1 = Bis1.ThisSolution(i);
101 Handle(GccInt_Bisec) Sol2 = Bis2.ThisSolution();
102 GccInt_IType typ1 = Sol1->ArcType();
103 GccInt_IType typ2 = Sol2->ArcType();
104 IntAna2d_AnaIntersection Intp;
105 if (typ1 == GccInt_Lin) {
106 if (typ2 == GccInt_Lin) {
107 Intp.Perform(Sol1->Line(),Sol2->Line());
109 else if (typ2 == GccInt_Par) {
110 Intp.Perform(Sol1->Line(),IntAna2d_Conic(Sol2->Parabola()));
113 else if (typ1 == GccInt_Par) {
114 if (typ2 == GccInt_Lin) {
115 Intp.Perform(Sol2->Line(),IntAna2d_Conic(Sol1->Parabola()));
117 else if (typ2 == GccInt_Par) {
118 Intp.Perform(Sol1->Parabola(),IntAna2d_Conic(Sol2->Parabola()));
122 if (!Intp.IsEmpty()) {
123 for (Standard_Integer j = 1 ; j <= Intp.NbPoints() ; j++) {
124 gp_Pnt2d Center(Intp.Point(j).Value());
125 Standard_Real dist1 = Center.Distance(C1.Location());
126 Standard_Real dist2 = L2.Distance(Center);
127 Standard_Real dist3 = Center.Distance(Point3);
128 Standard_Integer nbsol1 = 0;
129 Standard_Integer nbsol3 = 0;
130 Standard_Boolean ok = Standard_False;
131 if (Qualified1.IsEnclosed()) {
132 if (dist1-R1 < Tolerance) {
133 Radius(1) = Abs(R1-dist1);
138 else if (Qualified1.IsOutside()) {
139 if (R1-dist1 < Tolerance) {
140 Radius(1) = Abs(R1-dist1);
145 else if (Qualified1.IsEnclosing()) {
148 Radius(1) = Abs(R1-dist1);
150 else if (Qualified1.IsUnqualified()) {
153 Radius(1) = Abs(R1-dist1);
154 Radius(2) = R1+dist1;
156 if (Qualified2.IsEnclosed() && ok) {
157 if ((((L2.Location().X()-Center.X())*(-L2.Direction().Y()))+
158 ((L2.Location().Y()-Center.Y())*(L2.Direction().X())))<=0){
159 for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
160 if (Abs(dist2-Radius(ii)) < Tol) {
162 Radius(1) = Radius(ii);
167 else if (Qualified2.IsOutside() && ok) {
168 if ((((L2.Location().X()-Center.X())*(-L2.Direction().Y()))+
169 ((L2.Location().Y()-Center.Y())*(L2.Direction().X())))>=0){
170 for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
171 if (Abs(dist2-Radius(ii)) < Tol) {
173 Radius(1) = Radius(ii);
178 else if (Qualified2.IsUnqualified() && ok) {
179 for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
180 if (Abs(dist2-Radius(ii)) < Tol) {
182 Radius(1) = Radius(ii);
186 if (Abs(dist3-Radius(1)) <= Tol && ok) {
191 for (Standard_Integer k = 1 ; k <= nbsol3 ; k++) {
194 // pop : if the radius is too great - no creation
195 if (Radius(k) > MaxRad) break;
196 if (Abs(Radius(k)) < MinRad) break;
199 cirsol(NbrSol) = gp_Circ2d(gp_Ax2d(Center,dirx),Radius(k));
200 // ==========================================================
201 Standard_Real distcc1 = Center.Distance(center1);
202 if (!Qualified1.IsUnqualified()) {
203 qualifier1(NbrSol) = Qualified1.Qualifier();
205 else if (Abs(distcc1+Radius(k)-R1) < Tol) {
206 qualifier1(NbrSol) = GccEnt_enclosed;
208 else if (Abs(distcc1-R1-Radius(k)) < Tol) {
209 qualifier1(NbrSol) = GccEnt_outside;
211 else { qualifier1(NbrSol) = GccEnt_enclosing; }
212 gp_Dir2d dc2(origin2.XY()-Center.XY());
213 if (!Qualified2.IsUnqualified()) {
214 qualifier2(NbrSol) = Qualified2.Qualifier();
216 else if (dc2.Dot(normL2) > 0.0) {
217 qualifier2(NbrSol) = GccEnt_outside;
219 else { qualifier2(NbrSol) = GccEnt_enclosed; }
220 qualifier3(NbrSol) = GccEnt_noqualifier;
221 if (Center.Distance(C1.Location()) <= Tolerance &&
222 Abs(Radius(k)-R1) <= Tolerance) {
223 TheSame1(NbrSol) = 1;
226 TheSame1(NbrSol) = 0;
227 // modified by NIZHNY-EAP Mon Nov 1 13:48:21 1999 ___BEGIN___
228 // gp_Dir2d dc(C1.Location().XY()-Center.XY());
229 gp_Dir2d dc(Center.XY()-C1.Location().XY());
230 // modified by NIZHNY-EAP Mon Nov 1 13:48:55 1999 ___END___
231 pnttg1sol(NbrSol)=gp_Pnt2d(Center.XY()+Radius(k)*dc.XY());
232 par1sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
234 pararg1(NbrSol)=ElCLib::Parameter(C1,pnttg1sol(NbrSol));
236 TheSame2(NbrSol) = 0;
237 TheSame3(NbrSol) = 0;
238 gp_Dir2d dc(L2.Location().XY()-Center.XY());
239 Standard_Real sign = dc.Dot(gp_Dir2d(-L2.Direction().Y(),
240 L2.Direction().X()));
241 dc = gp_Dir2d(sign*gp_XY(-L2.Direction().Y(),
242 L2.Direction().X()));
243 pnttg2sol(NbrSol) = gp_Pnt2d(Center.XY()+Radius(k)*dc.XY());
244 par2sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
246 pararg2(NbrSol)=ElCLib::Parameter(L2,pnttg2sol(NbrSol));
247 pnttg3sol(NbrSol) = Point3;
248 par3sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
250 pararg3(NbrSol) = 0.;
255 WellDone = Standard_True;