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