aab019cf02c400d7a51ed99e9134a9d2d324ef61
[occt.git] / src / Geom2dGcc / Geom2dGcc_Lin2dTanObl.cxx
1 // Created on: 1992-10-21
2 // Created by: Remi GILET
3 // Copyright (c) 1992-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 <GccAna_Lin2dTanObl.hxx>
19 #include <GccEnt_BadQualifier.hxx>
20 #include <GccEnt_QualifiedCirc.hxx>
21 #include <Geom2d_Circle.hxx>
22 #include <Geom2dAdaptor_Curve.hxx>
23 #include <Geom2dGcc_CurveTool.hxx>
24 #include <Geom2dGcc_IsParallel.hxx>
25 #include <Geom2dGcc_Lin2dTanObl.hxx>
26 #include <Geom2dGcc_Lin2dTanOblIter.hxx>
27 #include <Geom2dGcc_QCurve.hxx>
28 #include <Geom2dGcc_QualifiedCurve.hxx>
29 #include <gp_Lin2d.hxx>
30 #include <gp_Pnt2d.hxx>
31 #include <Standard_NegativeValue.hxx>
32 #include <Standard_OutOfRange.hxx>
33 #include <StdFail_NotDone.hxx>
34
35 Geom2dGcc_Lin2dTanObl::
36    Geom2dGcc_Lin2dTanObl (const Geom2dGcc_QualifiedCurve& Qualified1 ,
37                           const gp_Lin2d&                 TheLine    ,
38                           const Standard_Real             TolAng     ,
39                           const Standard_Real             Angle      ):
40   Paral2(Standard_False),
41   linsol(1,2)    ,
42   qualifier1(1,2),
43   pnttg1sol(1,2) ,
44   pntint2sol(1,2),
45   par1sol(1,2)   ,
46   par2sol(1,2)   ,
47   pararg1(1,2)   ,
48   pararg2(1,2)
49 {
50   Geom2dAdaptor_Curve C1 = Qualified1.Qualified();
51   Handle(Geom2d_Curve) CC1 = C1.Curve();
52   GeomAbs_CurveType Type1 = C1.GetType();
53
54 //=============================================================================
55 //                            Appel a GccAna.                                 +
56 //=============================================================================
57
58   WellDone = Standard_False;
59   NbrSol = 0;
60   if (Type1 == GeomAbs_Circle ) {
61     Handle(Geom2d_Circle) CCC1 = Handle(Geom2d_Circle)::DownCast(CC1);
62     gp_Circ2d c1(CCC1->Circ2d());
63     GccEnt_QualifiedCirc Qc1=GccEnt_QualifiedCirc(c1,Qualified1.Qualifier());
64     GccAna_Lin2dTanObl Lin(Qc1,TheLine,Angle);
65     WellDone = Lin.IsDone();
66     if(WellDone) { 
67       NbrSol = Lin.NbSolutions();
68       for (Standard_Integer i = 1 ; i <= NbrSol ; i++) {
69         linsol(i)    = Lin.ThisSolution(i);
70         Lin.Tangency1(i,par1sol(i),pararg1(i),pnttg1sol(i));
71         Lin.Intersection2(i,par2sol(i),pararg2(i),pntint2sol(i));
72         Lin.WhichQualifier(i,qualifier1(i));
73       }
74     }
75   }
76   else {
77     Geom2dGcc_QCurve Qc1(C1,Qualified1.Qualifier());
78     Standard_Real      aFirstPar  = Geom2dGcc_CurveTool::FirstParameter(C1);
79     Standard_Real      aLastPar   = Geom2dGcc_CurveTool::LastParameter(C1);
80     Standard_Integer   aNbSamples = Geom2dGcc_CurveTool::NbSamples(C1);
81     Standard_Real      aStep      = (aLastPar - aFirstPar)/aNbSamples;
82     Standard_Real      Param1     = aFirstPar;
83     Standard_Integer   i;
84     
85     for (i = 0; i <= aNbSamples && NbrSol < 2; i++) {
86       Geom2dGcc_Lin2dTanOblIter Lin(Qc1,TheLine,Param1,TolAng,Angle);
87
88       if (Lin.IsDone()) {
89         if (Add(NbrSol + 1, Lin, TolAng, C1))
90           NbrSol++;
91       }
92
93       Param1 += aStep;
94     }
95
96     WellDone = (NbrSol > 0);
97   }
98 }
99
100 Geom2dGcc_Lin2dTanObl::
101    Geom2dGcc_Lin2dTanObl (const Geom2dGcc_QualifiedCurve& Qualified1 ,
102                           const gp_Lin2d&                 TheLine    ,
103                           const Standard_Real             TolAng     ,
104                           const Standard_Real             Param1     ,
105                           const Standard_Real             Angle      ):
106   Paral2(Standard_False),
107   linsol(1,2)    ,
108   qualifier1(1,2),
109   pnttg1sol(1,2) ,
110   pntint2sol(1,2),
111   par1sol(1,2)   ,
112   par2sol(1,2)   ,
113   pararg1(1,2)   ,
114   pararg2(1,2)
115 {
116   Geom2dAdaptor_Curve C1 = Qualified1.Qualified();
117   Handle(Geom2d_Curve) CC1 = C1.Curve();
118   GeomAbs_CurveType Type1 = C1.GetType();
119
120 //=============================================================================
121 //                            Appel a GccAna.                                 +
122 //=============================================================================
123
124   WellDone = Standard_False;
125   NbrSol = 0;
126   if (Type1 == GeomAbs_Circle ) {
127     Handle(Geom2d_Circle) CCC1 = Handle(Geom2d_Circle)::DownCast(CC1);
128     gp_Circ2d c1(CCC1->Circ2d());
129     GccEnt_QualifiedCirc Qc1=GccEnt_QualifiedCirc(c1,Qualified1.Qualifier());
130     GccAna_Lin2dTanObl Lin(Qc1,TheLine,Angle);
131     WellDone = Lin.IsDone();
132     if(WellDone) { 
133       NbrSol = Lin.NbSolutions();
134       for (Standard_Integer i = 1 ; i <= NbrSol ; i++) {
135         linsol(i)    = Lin.ThisSolution(i);
136         Lin.Tangency1(i,par1sol(i),pararg1(i),pnttg1sol(i));
137         Lin.Intersection2(i,par2sol(i),pararg2(i),pntint2sol(i));
138         Lin.WhichQualifier(i,qualifier1(i));
139       }
140     }
141   }
142   else {
143     Geom2dGcc_QCurve Qc1(C1,Qualified1.Qualifier());
144     Geom2dGcc_Lin2dTanOblIter Lin(Qc1,TheLine,TolAng,Param1,Angle);
145     WellDone = Lin.IsDone();
146     if(WellDone) { 
147       linsol(1)    = Lin.ThisSolution();
148       Lin.Tangency1(par1sol(1),pararg1(1),pnttg1sol(1));
149       Lin.Intersection2(par2sol(1),pararg2(1),pntint2sol(1));
150       Lin.WhichQualifier(qualifier1(1));
151     }
152   }
153 }
154
155 Standard_Boolean Geom2dGcc_Lin2dTanObl::
156    IsDone () const { return WellDone; }
157
158 Standard_Integer Geom2dGcc_Lin2dTanObl::
159    NbSolutions () const { return NbrSol; }
160
161 gp_Lin2d Geom2dGcc_Lin2dTanObl::
162    ThisSolution (const Standard_Integer Index) const {
163
164    if (Index > NbrSol || Index <= 0) { throw Standard_OutOfRange(); }
165    return linsol(Index);
166  }
167
168 void Geom2dGcc_Lin2dTanObl::
169    WhichQualifier (const Standard_Integer Index,
170                          GccEnt_Position& Qualif1) const
171 {
172   if (!WellDone) { throw StdFail_NotDone(); }
173   else if (Index <= 0 ||Index > NbrSol) { throw Standard_OutOfRange(); }
174   else { Qualif1 = qualifier1(Index);   }
175 }
176
177 void Geom2dGcc_Lin2dTanObl::
178    Tangency1 (const Standard_Integer Index,
179               Standard_Real& ParSol,
180               Standard_Real& ParArg,
181               gp_Pnt2d& PntSol) const {
182    if (!WellDone) { throw StdFail_NotDone(); }
183    else if (Index <= 0 ||Index > NbrSol) { throw Standard_OutOfRange(); }
184    else {
185      ParSol = par1sol(Index);
186      ParArg = pararg1(Index);
187      PntSol = pnttg1sol(Index);
188    }
189  }
190
191 void Geom2dGcc_Lin2dTanObl::
192    Intersection2 (const Standard_Integer   Index  ,
193                     Standard_Real&     ParSol ,
194                     Standard_Real&     ParArg ,
195                     gp_Pnt2d& PntSol ) const {
196    if (!WellDone) { throw StdFail_NotDone(); }
197    else if (Index <= 0 ||Index > NbrSol) { throw Standard_OutOfRange(); }
198    else {
199      ParSol = par2sol(Index);
200      ParArg = pararg2(Index);
201      PntSol = pntint2sol(Index);
202    }
203  }
204
205 Standard_Boolean Geom2dGcc_Lin2dTanObl::Add
206                            (const Standard_Integer       theIndex,
207                             const Geom2dGcc_Lin2dTanOblIter &theLin,
208                             const Standard_Real          theTol,
209                             const Geom2dAdaptor_Curve   &theC1)
210 {
211   Standard_Integer i;
212   Standard_Real    aPar1sol;
213   Standard_Real    aPar2sol;
214   Standard_Real    aPar1arg;
215   Standard_Real    aPar2arg;
216   gp_Pnt2d         aPnt1Sol;
217   gp_Pnt2d         aPnt2Sol;
218   gp_Lin2d         aLin   = theLin.ThisSolution();
219
220   theLin.Tangency1(aPar1sol, aPar1arg, aPnt1Sol);
221   theLin.Intersection2(aPar2sol, aPar2arg, aPnt2Sol);
222
223   for(i = 1; i < theIndex; i++) {
224     if (Abs(aPar1arg - pararg1(i)) <= theTol &&
225         Abs(aPar2arg - pararg2(i)) <= theTol)
226       return Standard_False;
227   }
228
229   gp_Dir2d aLinDir = aLin.Direction();
230   gp_Vec2d aVTan;
231   gp_Pnt2d aPoint;
232
233   Geom2dGcc_CurveTool::D1(theC1, aPar1arg, aPoint, aVTan);
234
235   if (Abs(aLinDir.Crossed(gp_Dir2d(aVTan))) > theTol)
236     return Standard_False;
237
238   linsol(theIndex)     = aLin;
239   par1sol(theIndex)    = aPar1sol;
240   pararg1(theIndex)    = aPar1arg;
241   pnttg1sol(theIndex)  = aPnt1Sol;
242   par2sol(theIndex)    = aPar2sol;
243   pararg2(theIndex)    = aPar2arg;
244   pntint2sol(theIndex) = aPnt2Sol;
245
246   theLin.WhichQualifier(qualifier1(theIndex));
247
248   return Standard_True;
249 }