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