5cf4a415240949ed78b28ee391396890614f3c78
[occt.git] / src / IntImp / IntImp_ComputeTangence.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 #include <IntImp_ComputeTangence.hxx>
16 #include <IntImp_ConstIsoparametric.hxx>
17
18 static const IntImp_ConstIsoparametric staticChoixRef [4] = {
19 IntImp_UIsoparametricOnCaro1,
20 IntImp_VIsoparametricOnCaro1,
21 IntImp_UIsoparametricOnCaro2,
22 IntImp_VIsoparametricOnCaro2,
23 };
24
25 Standard_EXPORT const IntImp_ConstIsoparametric *ChoixRef = staticChoixRef ;
26
27 //=======================================================================
28 //function : IntImp_ComputeTangence
29 //purpose  : 
30 //=======================================================================
31 Standard_Boolean IntImp_ComputeTangence(const gp_Vec DPuv[],
32                                         const Standard_Real EpsUV[],
33                                         Standard_Real Tgduv[],
34                                         IntImp_ConstIsoparametric TabIso[]) 
35 //                 arguments d entree:
36 // DPuv [0] =derivee en u sur caro 1    
37 // DPuv [1] =derivee en v sur caro 1
38 // DPuv [2] =derivee en u sur caro 2
39 // DPuv [3] =derivee en v sur caro 2
40 // EpsUV[0] tolerance en u sur caro1
41 // EpsUV[1] tolerance en v sur caro1
42 // EpsUV[2] tolerance en u sur caro2
43 // EpsUV[3] tolerance en v sur caro2
44 //                 arguments de sortie:
45 // Tgduv[0] composante sup dp/du de caro1 de la tangente a l intersection
46 // Tgduv[1] composante sup dp/dv de caro1 de la tangente a l intersection
47 // Tgduv[2] composante sup dp/du de caro2 de la tangente a l intersection
48 // Tgduv[3] composante sup dp/dv de caro2 de la tangente a l intersection
49 // TabIso[0...3] meilleure iso range par ordre decroissant candidate 
50 //               a l intersection
51 //         algorithme
52 // calculer la tangente a l 'intersection ;en utilisant la propriete suivante
53 // du produit scalaire a^(b^c)=b(ac)-c(ab) on obtient les composantes de la
54 // tangente a l intersection dans les 2 plans tangents (t=n1^n2 ou n1 normale
55 // au premier carreau n2 au 2ieme)
56 // on s assurera que les plans tangents des 2 carreaux ne sont pas //
57 // les composantes de l intersection dans les plans tangents permettent de 
58 //determiner l  angle entre les isoparametriques d un carreau avec le carreau
59 //reciproque 
60 //on triera par ordre croissant les cosinus :le plus petit cosinus determine le
61 // meilleure angle donc la meilleure iso a choisir pour trouver
62 // l intersection 
63
64 {
65    Standard_Real NormDuv[4], aM2, aTol2;
66    Standard_Integer i;
67    //
68    aTol2=1.e-32;
69    //
70    for (i=0; i<4; ++i) {
71      NormDuv[i] = DPuv[i].SquareMagnitude();    
72      if(NormDuv[i]<=aTol2) { 
73        return Standard_True;
74      } 
75    }
76    //
77    //-------------------------------------------------
78    gp_Vec N1 = DPuv[0];
79    N1.Cross(DPuv[1]);
80    //
81    //modified by NIZNHY-PKV Tue Nov 01 08:37:32 2011f
82    aM2=N1.SquareMagnitude();
83    if (aM2<aTol2) {
84      return Standard_True;
85    }
86    //modified by NIZNHY-PKV Tue Nov 01 08:37:34 2011t
87    N1.Normalize();
88    //-------------------------------------------------
89    gp_Vec N2 = DPuv[2];
90    N2.Cross(DPuv[3]);
91    //modified by NIZNHY-PKV Tue Nov 01 08:37:32 2011f
92    aM2=N2.SquareMagnitude();
93    if (aM2<aTol2) {
94      return Standard_True;
95    }
96    //modified by NIZNHY-PKV Tue Nov 01 08:37:34 2011t
97    N2.Normalize();
98    //
99    //modified by NIZNHY-PKV Tue Nov 01 08:31:25 2011f
100    for (i=0; i<4; ++i) {
101      NormDuv[i]=sqrt(NormDuv[i]);
102    }  
103    //modified by NIZNHY-PKV Tue Nov 01 08:31:29 2011t
104    Tgduv[0] = -DPuv[1].Dot(N2);
105    Tgduv[1] =  DPuv[0].Dot(N2);
106    Tgduv[2] =  DPuv[3].Dot(N1); 
107    Tgduv[3] = -DPuv[2].Dot(N1);
108
109
110    Standard_Boolean tangent = (Abs(Tgduv[0]) <= EpsUV[0]*NormDuv[1] &&
111                                Abs(Tgduv[1]) <= EpsUV[1]*NormDuv[0] &&
112                                Abs(Tgduv[2]) <= EpsUV[2]*NormDuv[3] &&
113                                Abs(Tgduv[3]) <= EpsUV[3]*NormDuv[2]  );
114    if(!tangent) { 
115      Standard_Real t=N1.Dot(N2);
116      if(t<0.0) t=-t;
117      if(t>0.999999999) { 
118        tangent=Standard_True; 
119      }
120    }
121
122    if (!tangent) {
123      NormDuv[0] = Abs(Tgduv[1]) /NormDuv[0]; //iso u sur caro1
124      NormDuv[1] = Abs(Tgduv[0]) /NormDuv[1]; //iso v sur caro1
125      NormDuv[2] = Abs(Tgduv[3]) /NormDuv[2]; // iso u sur caro2
126      NormDuv[3] = Abs(Tgduv[2]) /NormDuv[3]; //iso v sur caro2
127
128
129
130      //-- Tri sur NormDuv  ( en para. avec ChoixRef ) 
131      Standard_Boolean triOk = Standard_False;
132      Standard_Real t;
133      IntImp_ConstIsoparametric ti;
134      for ( i=0;i<=3;i++)  TabIso[i] = ChoixRef[i];
135      do { 
136        triOk = Standard_True;
137        for(i=1;i<=3;i++) { 
138          if(NormDuv[i-1]>NormDuv[i]) { 
139            triOk=Standard_False;
140            t=NormDuv[i]; 
141            NormDuv[i]=NormDuv[i-1];
142            NormDuv[i-1]=t;
143            
144            ti = TabIso[i];
145            TabIso[i] = TabIso[i-1];
146            TabIso[i-1] = ti;
147          }
148        }
149      }
150      while(!triOk);
151
152
153 #if 0     
154      // trier par ordre croissant le tableau NormDuv
155      Standard_Integer II;
156      for (j =0;j<=3;j++) Irang[j]=j;
157      for (j =0;j<=3;j++) {
158        Tampon = NormDuv[j];
159        II=j;
160        for (i =j+1;i<=3;i++) {
161          if (NormDuv[i] < Tampon) {
162            Tampon = NormDuv[i];
163            II = i;
164          }
165        }
166        Irang[j] = Irang[II];
167        Irang[II] = j;
168        NormDuv[II] = NormDuv[j];
169        NormDuv[j] = Tampon;
170      }
171      for (j=0; j<=3;j++) TabIso[j]=ChoixRef[Irang[j]];     
172 #endif
173    }
174    return tangent;
175  }
176
177