0022048: Visualization, AIS_InteractiveContext - single object selection should alway...
[occt.git] / src / GeomFill / GeomFill_BoundWithSurf.cxx
1 // Created on: 1995-10-19
2 // Created by: Laurent BOURESCHE
3 // Copyright (c) 1995-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 <Adaptor2d_HCurve2d.hxx>
19 #include <Adaptor3d_CurveOnSurface.hxx>
20 #include <Adaptor3d_HSurface.hxx>
21 #include <GeomFill_BoundWithSurf.hxx>
22 #include <gp_Pnt.hxx>
23 #include <gp_Pnt2d.hxx>
24 #include <gp_Vec.hxx>
25 #include <gp_Vec2d.hxx>
26 #include <Law.hxx>
27 #include <Law_BSpFunc.hxx>
28 #include <Law_Function.hxx>
29 #include <Standard_Type.hxx>
30
31 IMPLEMENT_STANDARD_RTTIEXT(GeomFill_BoundWithSurf,GeomFill_Boundary)
32
33 //=======================================================================
34 //function : GeomFill_BoundWithSurf
35 //purpose  : 
36 //=======================================================================
37 GeomFill_BoundWithSurf::GeomFill_BoundWithSurf
38 (const Adaptor3d_CurveOnSurface& CurveOnSurf,
39  const Standard_Real           Tol3d,
40  const Standard_Real           Tolang) :
41  GeomFill_Boundary(Tol3d,Tolang), myConS(CurveOnSurf)
42 {
43 }
44
45
46 //=======================================================================
47 //function : Value
48 //purpose  : 
49 //=======================================================================
50
51 gp_Pnt GeomFill_BoundWithSurf::Value(const Standard_Real U) const 
52 {
53   Standard_Real x = U;
54   if(!myPar.IsNull()) x = myPar->Value(U);
55   return myConS.Value(x);
56 }
57
58
59 //=======================================================================
60 //function : D1
61 //purpose  : 
62 //=======================================================================
63
64 void GeomFill_BoundWithSurf::D1(const Standard_Real U, 
65                                 gp_Pnt& P, 
66                                 gp_Vec& V) const 
67 {
68   Standard_Real x = U, dx = 1.;
69   if(!myPar.IsNull()) myPar->D1(U,x,dx);
70   myConS.D1(x, P, V);
71   V.Multiply(dx);
72 }
73
74
75 //=======================================================================
76 //function : HasNormals
77 //purpose  : 
78 //=======================================================================
79
80 Standard_Boolean GeomFill_BoundWithSurf::HasNormals() const 
81 {
82   return Standard_True;
83 }
84
85
86 //=======================================================================
87 //function : Norm
88 //purpose  : 
89 //=======================================================================
90
91 gp_Vec GeomFill_BoundWithSurf::Norm(const Standard_Real U) const 
92 {
93   // voir s il ne faudrait pas utiliser LProp ou autre.
94   if (!HasNormals()) 
95     throw Standard_Failure("BoundWithSurf Norm : pas de contrainte");
96
97 //  Handle(Adaptor3d_HSurface)& S = myConS.GetSurface();
98 //  Handle(Adaptor2d_HCurve2d)& C2d = myConS.GetCurve();
99   Standard_Real x,y;
100   Standard_Real w = U;
101   if(!myPar.IsNull()) w = myPar->Value(U);
102   myConS.GetCurve()->Value(w).Coord(x,y);
103   gp_Pnt P;
104   gp_Vec Su, Sv;
105   myConS.GetSurface()->D1(x,y,P,Su,Sv);
106   Su.Cross(Sv);
107   Su.Normalize();
108   return Su;
109 }
110
111
112 //=======================================================================
113 //function : D1Norm
114 //purpose  : 
115 //=======================================================================
116
117 void GeomFill_BoundWithSurf::D1Norm(const Standard_Real U,
118                                     gp_Vec&             N,
119                                     gp_Vec&             DN) const 
120 {
121   if (!HasNormals()) 
122     throw Standard_Failure("BoundWithSurf Norm : pas de contrainte");
123 //  Handle(Adaptor3d_HSurface)& S = myConS.GetSurface();
124 //  Handle(Adaptor2d_HCurve2d)& C2d = myConS.GetCurve();
125   gp_Pnt2d P2d;
126   gp_Vec2d V2d;
127   Standard_Real x,y,dx,dy;
128   Standard_Real w = U, dw = 1.;
129   if(!myPar.IsNull()) myPar->D1(U,w,dw);
130   myConS.GetCurve()->D1(w,P2d,V2d);
131   P2d.Coord(x,y);
132   V2d.Multiply(dw);
133   V2d.Coord(dx,dy);
134   gp_Pnt P;
135   gp_Vec Su, Sv, Suu, Suv, Svv;
136   myConS.GetSurface()->D2(x,y,P,Su,Sv, Suu, Svv, Suv);
137   N = Su.Crossed(Sv);
138   N.Normalize();
139   Standard_Real nsuu = N.Dot(Suu), nsuv = N.Dot(Suv), nsvv = N.Dot(Svv);
140   Standard_Real susu = Su.Dot(Su), susv = Su.Dot(Sv), svsv = Sv.Dot(Sv);
141   Standard_Real deno = (susu*svsv) - (susv*susv);
142   if(Abs(deno) < 1.e-16) {
143     // on embraye sur un calcul approche, c est mieux que rien!?!
144     gp_Vec temp = Norm(U + 1.e-12);
145     DN = N.Multiplied(-1.);
146     DN.Add(temp);
147     DN.Multiply(1.e-12);
148   }
149   else{
150     Standard_Real a = (-nsuu*svsv + nsuv*susv)/deno;
151     Standard_Real b = ( nsuu*susv - nsuv*susu)/deno;
152     Standard_Real c = (-nsuv*svsv + nsvv*susv)/deno;
153     Standard_Real d = ( nsuv*susv - nsvv*susu)/deno;
154
155     gp_Vec temp1 = Su.Multiplied(a);
156     gp_Vec temp2 = Sv.Multiplied(b);
157     temp1.Add(temp2);
158     temp2        = Su.Multiplied(c);
159     gp_Vec temp3 = Sv.Multiplied(d);
160     temp2.Add(temp3);
161     temp1.Multiply(dx);
162     temp2.Multiply(dy);
163     DN = temp1.Added(temp2);
164   }
165 }
166
167
168 //=======================================================================
169 //function : Reparametrize
170 //purpose  : 
171 //=======================================================================
172
173 void GeomFill_BoundWithSurf::Reparametrize(const Standard_Real First, 
174                                            const Standard_Real Last,
175                                            const Standard_Boolean HasDF, 
176                                            const Standard_Boolean HasDL, 
177                                            const Standard_Real DF, 
178                                            const Standard_Real DL,
179                                            const Standard_Boolean Rev)
180 {
181   Handle(Law_BSpline) curve = Law::Reparametrize(myConS,
182                                                  First,Last,
183                                                  HasDF,HasDL,DF,DL,
184                                                  Rev,30);
185   myPar = new Law_BSpFunc();
186   Handle(Law_BSpFunc)::DownCast (myPar)->SetCurve(curve);
187 }
188
189 //=======================================================================
190 //function : Bounds
191 //purpose  : 
192 //=======================================================================
193
194 void GeomFill_BoundWithSurf::Bounds(Standard_Real& First, 
195                                     Standard_Real& Last) const 
196 {
197   if(!myPar.IsNull()) myPar->Bounds(First,Last);
198   else{
199     First = myConS.FirstParameter();
200     Last  = myConS.LastParameter();
201   }
202 }
203
204
205 //=======================================================================
206 //function : IsDegenerated
207 //purpose  : 
208 //=======================================================================
209
210 Standard_Boolean GeomFill_BoundWithSurf::IsDegenerated() const 
211 {
212   return Standard_False;
213 }