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
7 // under the terms of the GNU Lesser General Public 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
17 #include <GccAna_Circ2d3Tan.jxx>
20 #include <IntAna2d_AnaIntersection.hxx>
21 #include <IntAna2d_IntPoint.hxx>
22 #include <gp_Lin2d.hxx>
23 #include <gp_Circ2d.hxx>
24 #include <gp_Dir2d.hxx>
25 #include <TColStd_Array1OfReal.hxx>
26 #include <GccAna_CircLin2dBisec.hxx>
27 #include <GccAna_LinPnt2dBisec.hxx>
28 #include <GccInt_IType.hxx>
29 #include <GccInt_BLine.hxx>
30 #include <GccInt_BParab.hxx>
31 #include <IntAna2d_Conic.hxx>
32 #include <GccEnt_BadQualifier.hxx>
34 //===========================================================================
35 // Creation of a circle tangent to a circle, a straight line and a point. +
36 //===========================================================================
39 GccAna_Circ2d3Tan (const GccEnt_QualifiedCirc& Qualified1 ,
40 const GccEnt_QualifiedLin& Qualified2 ,
41 const gp_Pnt2d& Point3 ,
42 const Standard_Real Tolerance ):
44 //=========================================================================
45 // Initialization of fields. +
46 //=========================================================================
66 gp_Dir2d dirx(1.0,0.0);
67 Standard_Real Tol = Abs(Tolerance);
68 Standard_Real MaxRad = 1e10, MinRad = 1e-6;
69 WellDone = Standard_False;
71 if (!(Qualified1.IsEnclosed() || Qualified1.IsEnclosing() ||
72 Qualified1.IsOutside() || Qualified1.IsUnqualified()) ||
73 !(Qualified2.IsEnclosed() ||
74 Qualified2.IsOutside() || Qualified2.IsUnqualified())) {
75 GccEnt_BadQualifier::Raise();
79 //=========================================================================
81 //=========================================================================
83 gp_Circ2d C1(Qualified1.Qualified());
84 gp_Lin2d L2(Qualified2.Qualified());
85 Standard_Real R1 = C1.Radius();
86 gp_Pnt2d center1(C1.Location());
87 gp_Pnt2d origin2(L2.Location());
88 gp_Dir2d dir2(L2.Direction());
89 gp_Dir2d normL2(-dir2.Y(),dir2.X());
91 TColStd_Array1OfReal Radius(1,2);
92 GccAna_CircLin2dBisec Bis1(C1,L2);
93 GccAna_LinPnt2dBisec Bis2(L2,Point3);
94 if (Bis1.IsDone() && Bis2.IsDone()) {
95 Standard_Integer nbsolution1 = Bis1.NbSolutions();
96 for (Standard_Integer i = 1 ; i <= nbsolution1; i++) {
97 Handle(GccInt_Bisec) Sol1 = Bis1.ThisSolution(i);
98 Handle(GccInt_Bisec) Sol2 = Bis2.ThisSolution();
99 GccInt_IType typ1 = Sol1->ArcType();
100 GccInt_IType typ2 = Sol2->ArcType();
101 IntAna2d_AnaIntersection Intp;
102 if (typ1 == GccInt_Lin) {
103 if (typ2 == GccInt_Lin) {
104 Intp.Perform(Sol1->Line(),Sol2->Line());
106 else if (typ2 == GccInt_Par) {
107 Intp.Perform(Sol1->Line(),IntAna2d_Conic(Sol2->Parabola()));
110 else if (typ1 == GccInt_Par) {
111 if (typ2 == GccInt_Lin) {
112 Intp.Perform(Sol2->Line(),IntAna2d_Conic(Sol1->Parabola()));
114 else if (typ2 == GccInt_Par) {
115 Intp.Perform(Sol1->Parabola(),IntAna2d_Conic(Sol2->Parabola()));
119 if (!Intp.IsEmpty()) {
120 for (Standard_Integer j = 1 ; j <= Intp.NbPoints() ; j++) {
121 gp_Pnt2d Center(Intp.Point(j).Value());
122 Standard_Real dist1 = Center.Distance(C1.Location());
123 Standard_Real dist2 = L2.Distance(Center);
124 Standard_Real dist3 = Center.Distance(Point3);
125 Standard_Integer nbsol1 = 0;
126 Standard_Integer nbsol3 = 0;
127 Standard_Boolean ok = Standard_False;
128 if (Qualified1.IsEnclosed()) {
129 if (dist1-R1 < Tolerance) {
130 Radius(1) = Abs(R1-dist1);
135 else if (Qualified1.IsOutside()) {
136 if (R1-dist1 < Tolerance) {
137 Radius(1) = Abs(R1-dist1);
142 else if (Qualified1.IsEnclosing()) {
145 Radius(1) = Abs(R1-dist1);
147 else if (Qualified1.IsUnqualified()) {
150 Radius(1) = Abs(R1-dist1);
151 Radius(2) = R1+dist1;
153 if (Qualified2.IsEnclosed() && ok) {
154 if ((((L2.Location().X()-Center.X())*(-L2.Direction().Y()))+
155 ((L2.Location().Y()-Center.Y())*(L2.Direction().X())))<=0){
156 for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
157 if (Abs(dist2-Radius(ii)) < Tol) {
159 Radius(1) = Radius(ii);
164 else if (Qualified2.IsOutside() && ok) {
165 if ((((L2.Location().X()-Center.X())*(-L2.Direction().Y()))+
166 ((L2.Location().Y()-Center.Y())*(L2.Direction().X())))>=0){
167 for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
168 if (Abs(dist2-Radius(ii)) < Tol) {
170 Radius(1) = Radius(ii);
175 else if (Qualified2.IsUnqualified() && ok) {
176 for (Standard_Integer ii = 1 ; ii <= nbsol1 ; ii++) {
177 if (Abs(dist2-Radius(ii)) < Tol) {
179 Radius(1) = Radius(ii);
183 if (Abs(dist3-Radius(1)) <= Tol && ok) {
188 for (Standard_Integer k = 1 ; k <= nbsol3 ; k++) {
191 // pop : if the radius is too great - no creation
192 if (Radius(k) > MaxRad) break;
193 if (Abs(Radius(k)) < MinRad) break;
196 cirsol(NbrSol) = gp_Circ2d(gp_Ax2d(Center,dirx),Radius(k));
197 // ==========================================================
198 Standard_Real distcc1 = Center.Distance(center1);
199 if (!Qualified1.IsUnqualified()) {
200 qualifier1(NbrSol) = Qualified1.Qualifier();
202 else if (Abs(distcc1+Radius(k)-R1) < Tol) {
203 qualifier1(NbrSol) = GccEnt_enclosed;
205 else if (Abs(distcc1-R1-Radius(k)) < Tol) {
206 qualifier1(NbrSol) = GccEnt_outside;
208 else { qualifier1(NbrSol) = GccEnt_enclosing; }
209 gp_Dir2d dc2(origin2.XY()-Center.XY());
210 if (!Qualified2.IsUnqualified()) {
211 qualifier2(NbrSol) = Qualified2.Qualifier();
213 else if (dc2.Dot(normL2) > 0.0) {
214 qualifier2(NbrSol) = GccEnt_outside;
216 else { qualifier2(NbrSol) = GccEnt_enclosed; }
217 qualifier3(NbrSol) = GccEnt_noqualifier;
218 if (Center.Distance(C1.Location()) <= Tolerance &&
219 Abs(Radius(k)-R1) <= Tolerance) {
220 TheSame1(NbrSol) = 1;
223 TheSame1(NbrSol) = 0;
224 // modified by NIZHNY-EAP Mon Nov 1 13:48:21 1999 ___BEGIN___
225 // gp_Dir2d dc(C1.Location().XY()-Center.XY());
226 gp_Dir2d dc(Center.XY()-C1.Location().XY());
227 // modified by NIZHNY-EAP Mon Nov 1 13:48:55 1999 ___END___
228 pnttg1sol(NbrSol)=gp_Pnt2d(Center.XY()+Radius(k)*dc.XY());
229 par1sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
231 pararg1(NbrSol)=ElCLib::Parameter(C1,pnttg1sol(NbrSol));
233 TheSame2(NbrSol) = 0;
234 TheSame3(NbrSol) = 0;
235 gp_Dir2d dc(L2.Location().XY()-Center.XY());
236 Standard_Real sign = dc.Dot(gp_Dir2d(-L2.Direction().Y(),
237 L2.Direction().X()));
238 dc = gp_Dir2d(sign*gp_XY(-L2.Direction().Y(),
239 L2.Direction().X()));
240 pnttg2sol(NbrSol) = gp_Pnt2d(Center.XY()+Radius(k)*dc.XY());
241 par2sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
243 pararg2(NbrSol)=ElCLib::Parameter(L2,pnttg2sol(NbrSol));
244 pnttg3sol(NbrSol) = Point3;
245 par3sol(NbrSol)=ElCLib::Parameter(cirsol(NbrSol),
247 pararg3(NbrSol) = 0.;
252 WellDone = Standard_True;