a1bff019ae867b02da87df0b55e1c4c525ed8946
[occt.git] / src / HLRBRep / HLRBRep_Surface.cxx
1 // Created on: 1992-03-13
2 // Created by: Christophe MARION
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 <BRepAdaptor_Surface.hxx>
19 #include <BRepClass_FaceClassifier.hxx>
20 #include <Geom_BezierSurface.hxx>
21 #include <Geom_BSplineSurface.hxx>
22 #include <gp_Pln.hxx>
23 #include <gp_Pnt.hxx>
24 #include <gp_Vec.hxx>
25 #include <GProp_PEquation.hxx>
26 #include <HLRAlgo_Projector.hxx>
27 #include <HLRBRep_BSurfaceTool.hxx>
28 #include <HLRBRep_Curve.hxx>
29 #include <HLRBRep_Surface.hxx>
30 #include <Standard_DomainError.hxx>
31 #include <Standard_NoSuchObject.hxx>
32 #include <Standard_OutOfRange.hxx>
33 #include <TColgp_Array1OfPnt.hxx>
34 #include <TColStd_Array2OfReal.hxx>
35 #include <TopoDS_Face.hxx>
36
37 //=======================================================================
38 //function : HLRBRep_Surface
39 //purpose  : 
40 //=======================================================================
41 HLRBRep_Surface::HLRBRep_Surface ()
42 {
43 }
44
45 //=======================================================================
46 //function : Surface
47 //purpose  : 
48 //=======================================================================
49
50 void HLRBRep_Surface::Surface (const TopoDS_Face& F)
51 {
52   mySurf.Initialize(F,Standard_False);
53   GeomAbs_SurfaceType typ = HLRBRep_BSurfaceTool::GetType(mySurf);
54   switch (typ) {
55
56   case GeomAbs_Plane :
57   case GeomAbs_Cylinder :
58   case GeomAbs_Cone :
59   case GeomAbs_Sphere :
60   case GeomAbs_Torus :
61     // unchanged type
62     myType = typ;
63     break;
64     
65   case GeomAbs_BezierSurface :
66     if (HLRBRep_BSurfaceTool::UDegree(mySurf) == 1 &&
67         HLRBRep_BSurfaceTool::VDegree(mySurf) == 1) {
68       myType = GeomAbs_Plane;
69     }
70     else
71       myType = typ;
72     break;
73     
74     default :
75     myType = GeomAbs_OtherSurface;
76     break;
77   }
78 }
79
80 //=======================================================================
81 //function : SideRowsOfPoles
82 //purpose  : 
83 //=======================================================================
84
85 Standard_Boolean 
86 HLRBRep_Surface::SideRowsOfPoles (const Standard_Real tol,
87                                   const Standard_Integer nbuPoles,
88                                   const Standard_Integer nbvPoles,
89                                   TColgp_Array2OfPnt& Pnt) const
90 {
91   Standard_Integer iu,iv;
92   Standard_Real x0,y0,x,y,z;
93   Standard_Boolean result;
94   Standard_Real tole = (Standard_Real)tol;
95   const gp_Trsf& T = ((HLRAlgo_Projector*) myProj)->Transformation();
96
97   for (iu = 1; iu <= nbuPoles; iu++) {
98     
99     for (iv = 1; iv <= nbvPoles; iv++)
100       Pnt(iu,iv).Transform(T);
101   }
102   result = Standard_True;
103
104   for (iu = 1; iu <= nbuPoles && result; iu++) {         // Side iso u ?
105     Pnt(iu,1).Coord(x0,y0,z);
106     
107     for (iv = 2; iv <= nbvPoles && result; iv++) {
108       Pnt(iu,iv).Coord(x,y,z);
109       result = Abs(x-x0) < tole && Abs(y-y0) < tole;
110     }
111   }
112   if (result) return result;
113   result = Standard_True;
114   
115   for (iv = 1; iv <= nbvPoles && result; iv++) {         // Side iso v ?
116     Pnt(1,iv).Coord(x0,y0,z);
117     
118     for (iu = 2; iu <= nbuPoles && result; iu++) {
119       Pnt(iu,iv).Coord(x,y,z);
120       result = Abs(x-x0) < tole && Abs(y-y0) < tole;
121     }
122   }
123   if (result) return result;
124
125   // Are the Poles in a Side Plane ?
126   TColgp_Array1OfPnt p(1,nbuPoles*nbvPoles);
127   Standard_Integer i = 0;
128
129   for (iu = 1; iu <= nbuPoles; iu++) {
130     
131     for (iv = 1; iv <= nbvPoles; iv++) { 
132       i++;
133       p(i) = Pnt(iu,iv);
134     }
135   }
136
137   GProp_PEquation Pl(p,(Standard_Real)tol);
138   if (Pl.IsPlanar())
139     result = Abs(Pl.Plane().Axis().Direction().Z()) < 0.0001;
140
141   return result;
142 }
143
144 //=======================================================================
145 //function : IsSide
146 //purpose  : 
147 //=======================================================================
148
149 Standard_Boolean 
150 HLRBRep_Surface::IsSide (const Standard_Real tolF,
151                          const Standard_Real toler) const
152 {
153   gp_Pnt Pt;
154   gp_Vec D;
155   Standard_Real r;
156
157   if (myType == GeomAbs_Plane) {
158     gp_Pln Pl = Plane();
159     gp_Ax1 A  = Pl.Axis();
160     Pt = A.Location();
161     D  = A.Direction();
162     Pt.Transform(((HLRAlgo_Projector*) myProj)->Transformation());
163     D .Transform(((HLRAlgo_Projector*) myProj)->Transformation());
164     if (((HLRAlgo_Projector*) myProj)->Perspective()) {
165       r = D.Z() * ((HLRAlgo_Projector*) myProj)->Focus() - 
166         ( D.X() * Pt.X() + D.Y() * Pt.Y() + D.Z() * Pt.Z() );
167     }
168     else r= D.Z();
169     return Abs(r) < toler;
170   }
171   else if (myType == GeomAbs_Cylinder) {
172     if (((HLRAlgo_Projector*) myProj)->Perspective()) return Standard_False;
173     gp_Cylinder Cyl = HLRBRep_BSurfaceTool::Cylinder(mySurf);
174     gp_Ax1 A = Cyl.Axis();
175     D  = A.Direction();
176     D .Transform(((HLRAlgo_Projector*) myProj)->Transformation());
177     r = Sqrt(D.X() * D.X() + D.Y() * D.Y());
178     return r < toler;
179   }
180   else if (myType == GeomAbs_Cone) {
181     if (!((HLRAlgo_Projector*) myProj)->Perspective()) return Standard_False;
182     gp_Cone Con = HLRBRep_BSurfaceTool::Cone(mySurf);
183     Pt = Con.Apex();
184     Pt.Transform(((HLRAlgo_Projector*) myProj)->Transformation());
185     Standard_Real tol = 0.001;
186     return Pt.IsEqual(gp_Pnt(0,0,((HLRAlgo_Projector*) myProj)->Focus()),tol);
187   }
188   else if (myType == GeomAbs_BezierSurface) {
189     if (((HLRAlgo_Projector*) myProj)->Perspective()) return Standard_False;
190     Standard_Integer nu = HLRBRep_BSurfaceTool::NbUPoles(mySurf);
191     Standard_Integer nv = HLRBRep_BSurfaceTool::NbVPoles(mySurf);
192     TColgp_Array2OfPnt Pnt(1,nu,1,nv);
193     HLRBRep_BSurfaceTool::Bezier(mySurf)->Poles(Pnt);
194     return SideRowsOfPoles (tolF,nu,nv,Pnt);
195   }
196   else if (myType == GeomAbs_BSplineSurface) {
197     if (((HLRAlgo_Projector*) myProj)->Perspective()) return Standard_False;
198     Standard_Integer nu = HLRBRep_BSurfaceTool::NbUPoles(mySurf);
199     Standard_Integer nv = HLRBRep_BSurfaceTool::NbVPoles(mySurf);
200     TColgp_Array2OfPnt Pnt(1,nu,1,nv);
201     TColStd_Array2OfReal W(1,nu,1,nv);
202     HLRBRep_BSurfaceTool::BSpline(mySurf)->Poles(Pnt);
203     HLRBRep_BSurfaceTool::BSpline(mySurf)->Weights(W);
204     return SideRowsOfPoles (tolF,nu,nv,Pnt);
205   }
206   else return Standard_False;
207 }
208
209 //=======================================================================
210 //function : IsAbove
211 //purpose  : 
212 //=======================================================================
213
214 Standard_Boolean  
215 HLRBRep_Surface::IsAbove (const Standard_Boolean back,
216                           const Standard_Address A,
217                           const Standard_Real tol) const
218
219   Standard_Boolean planar = (myType == GeomAbs_Plane);
220   if (planar) {
221     gp_Pln Pl = Plane();
222     Standard_Real a,b,c,d;
223     Pl.Coefficients(a,b,c,d);
224     Standard_Real u,u1,u2,dd,x,y,z;
225     gp_Pnt P;
226     u1 = ((HLRBRep_Curve*)A)->Parameter3d
227       (((HLRBRep_Curve*)A)->FirstParameter());
228     u2 = ((HLRBRep_Curve*)A)->Parameter3d
229       (((HLRBRep_Curve*)A)->LastParameter());
230     u=u1;
231     ((HLRBRep_Curve*)A)->D0(u,P);
232     P.Coord(x,y,z);
233     dd = a*x + b*y + c*z + d;
234     if (back) dd = -dd;
235     if (dd < -tol) return Standard_False;
236     if (((HLRBRep_Curve*)A)->GetType() != GeomAbs_Line) {
237       Standard_Integer nbPnt = 30;
238       Standard_Real step = (u2-u1)/(nbPnt+1);
239       for (Standard_Integer i = 1; i <= nbPnt; i++) {
240         u += step;
241         ((HLRBRep_Curve*)A)->D0(u,P);
242         P.Coord(x,y,z);
243         dd = a*x + b*y + c*z + d;
244         if (back) dd = -dd;
245         if (dd < -tol) return Standard_False;
246       }
247     }
248     u = u2;
249     ((HLRBRep_Curve*)A)->D0(u,P);
250     P.Coord(x,y,z);
251     dd = a*x + b*y + c*z + d;
252     if (back) dd = -dd;
253     if (dd < -tol) return Standard_False;
254     return Standard_True;
255   }
256   else return Standard_False; 
257 }
258
259 //=======================================================================
260 //function : Value
261 //purpose  : 
262 //=======================================================================
263
264 gp_Pnt HLRBRep_Surface::Value (const Standard_Real U,
265                                const Standard_Real V) const
266
267   gp_Pnt P;
268   D0(U,V,P);
269   return P;
270 }
271
272 //=======================================================================
273 //function : Plane
274 //purpose  : 
275 //=======================================================================
276
277 gp_Pln  HLRBRep_Surface::Plane () const 
278 {
279   GeomAbs_SurfaceType typ = HLRBRep_BSurfaceTool::GetType(mySurf);
280   switch (typ) {
281   case GeomAbs_BezierSurface :
282     {
283       gp_Pnt P;
284       gp_Vec D1U;
285       gp_Vec D1V;
286       D1(0.5,0.5,P,D1U,D1V);
287       return gp_Pln(P,gp_Dir(D1U.Crossed(D1V)));
288     }
289     
290     default :
291     return HLRBRep_BSurfaceTool::Plane(mySurf);
292   }
293 }