0025266: Debug statements in the source are getting flushed on to the console
[occt.git] / src / TopOpeBRepTool / TopOpeBRepTool_GEOMETRY.cxx
1 // Created on: 1998-10-06
2 // Created by: Jean Yves LEBEY
3 // Copyright (c) 1998-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 #include <TopOpeBRepTool_GEOMETRY.hxx>
18
19 #include <Geom2d_Curve.hxx>
20 #include <Geom2d_Line.hxx>
21 #include <Geom_Curve.hxx>
22 #include <Geom_Surface.hxx>
23 #include <Geom2dAdaptor_Curve.hxx>
24 #include <GeomAdaptor_Curve.hxx>
25 #include <GeomAdaptor_Surface.hxx>
26 #include <gp_Vec.hxx>
27 #include <gp_Cone.hxx>
28 #include <gp_Sphere.hxx>
29 #include <Geom2d_OffsetCurve.hxx>
30 #include <Geom2d_TrimmedCurve.hxx>
31 #include <TopOpeBRepTool_ShapeTool.hxx>
32 #include <Precision.hxx>
33
34 // ----------------------------------------------------------------------
35 Standard_EXPORT Handle(Geom2d_Curve) BASISCURVE2D(const Handle(Geom2d_Curve)& C)
36 {
37   Handle(Standard_Type) T = C->DynamicType();
38   if      ( T == STANDARD_TYPE(Geom2d_OffsetCurve) ) 
39     return ::BASISCURVE2D(Handle(Geom2d_OffsetCurve)::DownCast(C)->BasisCurve());
40   else if ( T == STANDARD_TYPE(Geom2d_TrimmedCurve) )
41     return ::BASISCURVE2D(Handle(Geom2d_TrimmedCurve)::DownCast(C)->BasisCurve());
42   else return C;
43 }
44
45 /*// ----------------------------------------------------------------------
46 Standard_EXPORT Standard_Boolean FUN_tool_IsUViso(const Handle(Geom2d_Curve)& PC,
47                                      Standard_Boolean& isoU,Standard_Boolean& isoV,
48                                      gp_Dir2d& d2d,gp_Pnt2d& o2d)
49 {
50   isoU = isoV = Standard_False;
51   if (PC.IsNull()) return Standard_False;
52   Handle(Geom2d_Curve) LLL = BASISCURVE2D(PC);
53   Handle(Standard_Type) T2 = LLL->DynamicType();
54   Standard_Boolean isline2d = (T2 == STANDARD_TYPE(Geom2d_Line));
55   if (!isline2d) return Standard_False;
56
57   Handle(Geom2d_Line) L = Handle(Geom2d_Line)::DownCast(LLL);
58   d2d = L->Direction();
59   isoU = (Abs(d2d.X()) < Precision::Parametric(Precision::Confusion()));
60   isoV = (Abs(d2d.Y()) < Precision::Parametric(Precision::Confusion()));
61   Standard_Boolean isoUV = isoU || isoV;
62   if (!isoUV) return Standard_False;
63
64   o2d = L->Location();
65   return Standard_True;
66 }*/
67
68 // ----------------------------------------------------------------------
69 Standard_EXPORT gp_Dir FUN_tool_dirC(const Standard_Real par,const Handle(Geom_Curve)& C)
70 {
71   gp_Pnt p; gp_Vec tgE; C->D1(par,p,tgE); 
72   gp_Dir dirC(tgE);
73   return dirC;
74 }
75
76 // ----------------------------------------------------------------------
77 Standard_EXPORT Standard_Boolean FUN_tool_onapex(const gp_Pnt2d& p2d,const Handle(Geom_Surface)& S)
78 {  
79   Standard_Boolean isapex = Standard_False;
80   GeomAdaptor_Surface GS(S);
81   Standard_Real tol = Precision::Confusion();
82   GeomAbs_SurfaceType ST = GS.GetType();
83   Standard_Real toluv = 1.e-8;
84   if (ST == GeomAbs_Cone) {
85     gp_Cone co = GS.Cone();
86     gp_Pnt apex = co.Apex();
87     gp_Pnt pnt = GS.Value(p2d.X(),p2d.Y());
88     Standard_Real dist = pnt.Distance(apex);
89     isapex = (dist < tol);
90   }
91   if (ST == GeomAbs_Sphere) {
92     Standard_Real pisur2 = M_PI*.5;
93     Standard_Real v = p2d.Y();
94     Standard_Boolean vpisur2 = (Abs(v-pisur2) < toluv);
95     Standard_Boolean vmoinspisur2 = (Abs(v+pisur2) < toluv);
96     isapex = vpisur2 || vmoinspisur2;
97   }
98   return isapex;
99 }
100
101 // ----------------------------------------------------------------------
102 Standard_EXPORT gp_Dir FUN_tool_ngS(const gp_Pnt2d& p2d,const Handle(Geom_Surface)& S)
103 {
104   // ###############################
105   // nyi : all geometries are direct
106   // ###############################
107   gp_Pnt p; gp_Vec d1u,d1v;
108   S->D1(p2d.X(),p2d.Y(),p,d1u,d1v);  
109
110   Standard_Real du = d1u.Magnitude();
111   Standard_Real dv = d1v.Magnitude();
112   Standard_Real tol = Precision::Confusion();
113   Standard_Boolean kpart = (du < tol) || (dv < tol);
114   if (kpart) { 
115     GeomAdaptor_Surface GS(S);
116     GeomAbs_SurfaceType ST = GS.GetType();
117     Standard_Real toluv = 1.e-8;
118     if (ST == GeomAbs_Cone) {
119       Standard_Boolean nullx = (Abs(p2d.X()) < toluv);
120       Standard_Boolean apex = nullx && (Abs(p2d.Y()) < toluv);
121       if (apex) {
122         const gp_Dir& axis = GS.Cone().Axis().Direction();
123         gp_Vec ng(axis); ng.Reverse();
124         return ng;
125       }
126       else if (du < tol) {
127         Standard_Real vf = GS.FirstVParameter();
128         Standard_Boolean onvf = Abs(p2d.Y()-vf)<toluv;
129
130         Standard_Real x = p2d.X(); Standard_Real y = p2d.Y();
131         //NYIXPU : devrait plutot etre fait sur les faces & TopOpeBRepTool_TOOL::minDUV...
132         if (onvf) y += 1.;
133         else      y -= 1.;
134         S->D1(x,y,p,d1u,d1v);   
135         gp_Vec ng = d1u^d1v;
136         return ng;
137       }
138     }
139     if (ST == GeomAbs_Sphere) {
140 //      Standard_Real deuxpi = 2*M_PI;
141       Standard_Real pisur2 = M_PI*.5;
142       Standard_Real u = p2d.X(),v = p2d.Y();
143 //      Standard_Boolean u0  =(Abs(u) < toluv);
144 //      Standard_Boolean u2pi=(Abs(u-deuxpi) < toluv);
145 //      Standard_Boolean apex = u0 || u2pi;
146       Standard_Boolean vpisur2 = (Abs(v-pisur2) < toluv);
147       Standard_Boolean vmoinspisur2 = (Abs(v+pisur2) < toluv);
148       Standard_Boolean apex = vpisur2 || vmoinspisur2;
149       if (apex) {
150         gp_Pnt center = GS.Sphere().Location();
151         gp_Pnt value  = GS.Value(u,v); 
152         gp_Vec ng(center,value); 
153 //      ng.Reverse();
154         return ng;
155       }
156     }
157 #ifdef TOPOPEBREPTOOL_DEB
158     cout<<"FUN_tool_nggeomF NYI"<<endl;
159 #endif
160     return gp_Dir(0,0,1);
161   }
162
163   gp_Dir udir(d1u);
164   gp_Dir vdir(d1v);
165   gp_Dir ngS(udir^vdir);
166   return ngS;
167 }
168
169 // ----------------------------------------------------------------------
170 Standard_EXPORT Standard_Boolean FUN_tool_line(const Handle(Geom_Curve)& C3d)
171 {
172   Handle(Geom_Curve) C = TopOpeBRepTool_ShapeTool::BASISCURVE(C3d);
173   GeomAdaptor_Curve GC(C);
174   Standard_Boolean line = (GC.GetType() == GeomAbs_Line);
175   return line;
176 }
177
178 // ----------------------------------------------------------------------
179 Standard_EXPORT Standard_Boolean FUN_quadCT(const GeomAbs_CurveType& CT)
180 {
181   Standard_Boolean isquad = Standard_False;
182   if (CT == GeomAbs_Line) isquad = Standard_True;
183   if (CT == GeomAbs_Circle) isquad = Standard_True;
184   if (CT == GeomAbs_Ellipse) isquad = Standard_True;
185   if (CT == GeomAbs_Hyperbola) isquad = Standard_True;
186   if (CT == GeomAbs_Parabola) isquad = Standard_True;
187   return isquad;
188 }
189
190 // ----------------------------------------------------------------------
191 Standard_EXPORT Standard_Boolean FUN_tool_quad(const Handle(Geom_Curve)& C3d)
192 {
193   Handle(Geom_Curve) C = TopOpeBRepTool_ShapeTool::BASISCURVE(C3d);
194   if (C.IsNull()) return Standard_False;
195   GeomAdaptor_Curve GC(C);
196   GeomAbs_CurveType CT = GC.GetType();
197   Standard_Boolean quad = FUN_quadCT(CT);
198   return quad;
199 }
200
201 // ----------------------------------------------------------------------
202 Standard_EXPORT Standard_Boolean FUN_tool_quad(const Handle(Geom2d_Curve)& pc)
203 {
204   Handle(Geom2d_Curve) pcb = BASISCURVE2D(pc); // NYI TopOpeBRepTool_ShapeTool
205   if (pcb.IsNull()) return Standard_False;
206   Geom2dAdaptor_Curve GC2d(pcb);
207   GeomAbs_CurveType typ = GC2d.GetType();
208   Standard_Boolean isquad = Standard_False;
209   if (typ == GeomAbs_Line) isquad = Standard_True;
210   if (typ == GeomAbs_Circle) isquad = Standard_True;
211   if (typ == GeomAbs_Ellipse) isquad = Standard_True;
212   if (typ == GeomAbs_Hyperbola) isquad = Standard_True;
213   if (typ == GeomAbs_Parabola) isquad = Standard_True;
214   return isquad;
215 }
216 // ----------------------------------------------------------------------
217 Standard_EXPORT Standard_Boolean FUN_tool_line(const Handle(Geom2d_Curve)& pc)
218 {
219   Handle(Geom2d_Curve) pcb = BASISCURVE2D(pc); // NYI TopOpeBRepTool_ShapeTool
220   if (pcb.IsNull()) return Standard_False;
221   Geom2dAdaptor_Curve GC2d(pcb);
222   GeomAbs_CurveType typ = GC2d.GetType();
223
224   if (typ == GeomAbs_Line) return Standard_True;
225
226   return Standard_False ;
227
228 }
229
230 // ----------------------------------------------------------------------
231 Standard_EXPORT Standard_Boolean FUN_tool_quad(const Handle(Geom_Surface)& S)
232 {
233   if (S.IsNull()) return Standard_False;   
234   GeomAdaptor_Surface GAS(S);  
235   GeomAbs_SurfaceType typ = GAS.GetType();  
236   Standard_Boolean isquad = Standard_False;
237   if (typ == GeomAbs_Plane) isquad = Standard_True;
238   if (typ == GeomAbs_Cylinder) isquad = Standard_True;
239   if (typ == GeomAbs_Cone) isquad = Standard_True;
240   if (typ == GeomAbs_Sphere) isquad = Standard_True;
241   if (typ == GeomAbs_Torus) isquad = Standard_True;
242   return isquad;
243 }
244
245
246 // ----------------------------------------------------------------------
247 Standard_EXPORT Standard_Boolean FUN_tool_closed(const Handle(Geom_Surface)& S,
248                                     Standard_Boolean& uclosed,Standard_Real& uperiod,
249                                     Standard_Boolean& vclosed,Standard_Real& vperiod)
250 {
251   uperiod = vperiod = 0.;
252   if (S.IsNull()) return Standard_False;  
253   uclosed = S->IsUClosed(); if (uclosed) uclosed = S->IsUPeriodic(); //xpu261098 (BUC60382)
254   if (uclosed) uperiod = S->UPeriod();
255   vclosed = S->IsVClosed(); if (vclosed) vclosed = S->IsVPeriodic(); 
256   if (vclosed) vperiod = S->VPeriod();
257   Standard_Boolean closed = uclosed || vclosed;
258   return closed;
259 }
260
261 // ----------------------------------------------------------------------
262 Standard_EXPORT void FUN_tool_UpdateBnd2d(Bnd_Box2d& B2d,const Bnd_Box2d& newB2d)
263 {
264 //  B2d.SetVoid(); -> DOESN'T EMPTY THE  BOX
265   B2d = newB2d;
266 }