0033040: Coding - get rid of unused headers [Storage to TopOpeBRepTool]
[occt.git] / src / TopOpeBRep / TopOpeBRep_ProcessGR.cxx
1 // Created on: 1997-02-24
2 // Created by: Prestataire Xuan PHAM PHU
3 // Copyright (c) 1997-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 <TopOpeBRep_FacesFiller.hxx>
19 #include <TopOpeBRep_FFDumper.hxx>
20 #include <TopOpeBRep_LineInter.hxx>
21 #include <TopOpeBRep_VPointInter.hxx>
22 #include <TopOpeBRepDS_Transition.hxx>
23
24 #ifdef DRAW
25 #include <TopOpeBRep_DRAW.hxx>
26 #endif
27
28 #include <Geom_Curve.hxx>
29 #include <GeomAPI_ProjectPointOnSurf.hxx>
30 //#include <BRepAdaptor_Curve2d.hxx>
31 #include <TopoDS.hxx>
32 #include <TopOpeBRep_FFTransitionTool.hxx>
33 #include <TopOpeBRep_VPointInterIterator.hxx>
34 #include <TopOpeBRep_GeomTool.hxx>
35 #include <Precision.hxx>
36 #include <Standard_CString.hxx>
37 #include <Standard_ProgramError.hxx>
38 #include <TopOpeBRep.hxx>
39 #include <TopOpeBRepTool_EXPORT.hxx>
40 #include <TopOpeBRepTool_SC.hxx>
41
42 #ifdef OCCT_DEBUG
43 #include <TopOpeBRep_FFDumper.hxx>
44 #include <Geom_TrimmedCurve.hxx>
45 #include <Geom_Line.hxx>
46 extern Standard_Boolean TopOpeBRep_GettraceBIPS(); 
47 extern Standard_Boolean TopOpeBRep_GettraceDEGEN();
48 extern Standard_Boolean FUN_debnull(const TopoDS_Shape& s){Standard_Boolean isnull = s.IsNull(); if (isnull) std::cout <<"***"; return isnull;}
49 #endif
50
51 //Standard_EXPORT extern Standard_Real GLOBAL_tolFF;
52 Standard_EXPORTEXTERN Standard_Real GLOBAL_tolFF;
53
54 //=======================================================================
55 //function : StBipVPonF
56 //purpose  : 
57 //=======================================================================
58 TopAbs_State TopOpeBRep_FacesFiller::StBipVPonF
59 (const TopOpeBRep_VPointInter& vpf,const TopOpeBRep_VPointInter& vpl,
60  const TopOpeBRep_LineInter& Lrest,const Standard_Boolean isonedge1) const
61 {
62   
63 #define M_OUT(st) (st == TopAbs_OUT);
64 #define M_IN(st) (st == TopAbs_IN);
65   
66   Standard_Integer sind = isonedge1 ? 2 : 1; 
67   TopAbs_State stf = vpf.State(sind);
68   TopAbs_State stl = vpl.State(sind);
69   Standard_Boolean isout = M_OUT(stf); isout = isout || M_OUT(stl);
70   Standard_Boolean isin = M_IN(stf); isin = isin || M_IN(stl);   
71   if (isout) return TopAbs_OUT;
72   if (isin) return TopAbs_IN;
73
74   Standard_Boolean isperiodic;
75   const TopoDS_Edge& EArc = TopoDS::Edge(Lrest.Arc());
76   BRepAdaptor_Curve BAC(EArc);
77   GeomAbs_CurveType CT = BAC.GetType();
78   isperiodic = (CT == GeomAbs_Circle);
79   isperiodic = isperiodic || (CT == GeomAbs_Ellipse);
80
81   TopOpeBRep_VPointInter vpff = vpf;
82   TopOpeBRep_VPointInter vpll = vpl;
83
84   // xpu200798 : CTS21216, restriction edge 7, f8
85   // purpose : periodic restriction; vpf, vpl describing restriction bounds
86   //      if the Rline is describing the portion on curve (vpl,vpf),
87   //      we have to commutate these bounds.
88   if (isperiodic) { 
89 #ifdef OCCT_DEBUG
90 //    TopOpeBRep_FFDumper FFD(*this); 
91 //    std::cout <<"vpf :"; FFD.DumpVP(vpf,std::cout);
92 //    std::cout <<"vpl :"; FFD.DumpVP(vpl,std::cout);
93 #endif
94
95     Standard_Integer IArc = 0; 
96     if (Lrest.ArcIsEdge(1)) IArc = 1; 
97     if (Lrest.ArcIsEdge(2)) IArc = 2; 
98     if (IArc == 0) {
99 #ifdef OCCT_DEBUG
100       throw Standard_Failure("StBipVPonF");
101 #else
102       return TopAbs_UNKNOWN;
103 #endif
104     }
105     Standard_Integer ISI = (IArc == 1) ? 2 : 1; 
106     Standard_Integer sif = vpf.ShapeIndex();
107     Standard_Integer sil = vpl.ShapeIndex();
108     Standard_Boolean act = ((sif == 3)||(sif == ISI)) && ((sil == 3)||(sil == ISI));
109     if (act) {
110       TopOpeBRepDS_Transition Tf = TopOpeBRep_FFTransitionTool::ProcessLineTransition(vpf,ISI,vpf.Edge(ISI).Orientation());
111       TopOpeBRepDS_Transition Tl = TopOpeBRep_FFTransitionTool::ProcessLineTransition(vpl,ISI,vpl.Edge(ISI).Orientation());
112       Standard_Boolean toreverse = (Tf.Orientation(TopAbs_IN) == TopAbs_REVERSED);
113       toreverse = toreverse && (Tl.Orientation(TopAbs_IN) == TopAbs_FORWARD);
114       if (toreverse) {
115         vpff = vpl;
116         vpll = vpf;
117       }
118     } // act
119   } // isperiodic
120
121   TopoDS_Shape F;
122   if (isonedge1) F = myF2;
123   else           F = myF1;
124   Standard_Real uf = TopOpeBRep_FacesFiller::VPParamOnER (vpff,Lrest);
125   Standard_Real ul = TopOpeBRep_FacesFiller::VPParamOnER (vpll,Lrest);
126
127   // NYI XPU: 16-05-97: INTPATCH -> the parametrization of a point on a 
128   // periodized curve is INSUFFICIENT : parVP on line can be either 0. 
129   // or period. 
130   Standard_Boolean badparametrized = (uf > ul);
131   Standard_Real f,l;  f = BAC.FirstParameter(); l = BAC.LastParameter();
132   if (badparametrized) {
133     if (isperiodic) {
134       if (uf == l) uf = f;
135       if (ul == f) ul = l;
136     }
137   }
138
139   const TopoDS_Edge& arc = TopoDS::Edge(Lrest.Arc());
140   BRepAdaptor_Curve BC( arc );     
141   Standard_Real x = 0.789; Standard_Real parmil = (1-x)*uf + x*ul; //xpu170898
142   gp_Pnt pmil = BC.Value(parmil);
143
144 #ifdef OCCT_DEBUG
145 #ifdef DRAW
146   Standard_Boolean trc = TopOpeBRep_GettraceBIPS();
147   if (trc) {TCollection_AsciiString aa("pmil"); FUN_brep_draw(aa,pmil);}
148 #endif
149 #endif
150   TopAbs_State st = FSC_StatePonFace (pmil,F,*myPShapeClassifier);
151   return st;
152 }
153
154 //=======================================================================
155 //function : StateVPonFace
156 //purpose  :
157 //=======================================================================
158 TopAbs_State TopOpeBRep_FacesFiller::StateVPonFace(const TopOpeBRep_VPointInter& VP) const
159 {
160   Standard_Integer iVP = VP.ShapeIndex();
161   if (iVP == 3) return TopAbs_ON;
162   
163   Standard_Integer iother =  (iVP == 1) ? 2 : 1;
164   TopoDS_Shape F;
165   if (iother == 1) F = myF1;
166   else             F = myF2;
167   Standard_Real u,v;
168   if (iother == 1) VP.ParametersOnS1(u,v);
169   else VP.ParametersOnS2(u,v);
170   
171   myPShapeClassifier->SetReference(TopoDS::Face(F));
172   myPShapeClassifier->StateP2DReference(gp_Pnt2d(u,v));
173   TopAbs_State state = myPShapeClassifier->State();
174
175   return state;
176 }
177
178 // ----------------------------------------------------------------------
179 //           Class methods
180 // ----------------------------------------------------------------------
181
182 //=======================================================================
183 //function : Lminmax
184 //purpose  : Computes <pmin> and <pmax> the upper and lower bounds of <L>
185 //           enclosing all vpoints.
186 //=======================================================================
187 void TopOpeBRep_FacesFiller::Lminmax(const TopOpeBRep_LineInter& L,
188                                      Standard_Real& pmin,Standard_Real& pmax)
189 {
190   pmin = RealLast();
191   pmax = RealFirst();
192   TopOpeBRep_VPointInterIterator VPI;
193   VPI.Init(L,Standard_False);
194   for (; VPI.More(); VPI.Next()) {
195     const TopOpeBRep_VPointInter& VP = VPI.CurrentVP();
196     Standard_Real p = VP.ParameterOnLine();
197     pmin = Min(pmin,p);
198     pmax = Max(pmax,p);
199   }
200   Standard_Real d = Abs(pmin-pmax);
201   Standard_Boolean id = (d <= Precision::PConfusion());
202   Standard_Boolean isper = L.IsPeriodic();
203   Standard_Integer n = L.NbVPoint();
204   if (id && isper && n >= 2) {
205     Standard_Real per = L.Period();
206     pmax = pmin + per;
207   }
208 }
209
210 //=======================================================================
211 //function : LSameDomainERL
212 //purpose  : Returns <True> if GLine shares a same geometric domain with
213 //           at least one of the restriction edges of <ERL>.    
214 //=======================================================================
215 Standard_Boolean TopOpeBRep_FacesFiller::LSameDomainERL(const TopOpeBRep_LineInter& L,
216                                            const TopTools_ListOfShape& ERL)
217 {
218   Standard_Boolean isone = Standard_False;
219   if(L.TypeLineCurve() == TopOpeBRep_WALKING) return isone;
220
221 #ifdef DRAW
222   Standard_Boolean trc = Standard_False;
223   if (trc) {Handle(Geom_Curve) C = L.Curve(); TCollection_AsciiString aa("line"); FUN_brep_draw(aa,C);}
224 #endif
225
226   Standard_Real f,l; TopOpeBRep_FacesFiller::Lminmax(L,f,l);
227   Standard_Real d = Abs(f-l);
228
229   {
230     Standard_Boolean idINL = (L.INL() && (d == 0)); // null length line, made of VPoints only
231     if (idINL) return Standard_False;
232   } // INL
233
234   Standard_Boolean id = (d <= Precision::PConfusion());
235   if (id) return Standard_False;
236   
237   Handle(Geom_Curve) CL; TopOpeBRep_GeomTool::MakeCurve(f,l,L,CL); 
238   Standard_Real t = 0.417789; Standard_Real p = (1-t)*f + t*l;
239   gp_Pnt Pm = CL->Value(p);
240
241   TopTools_ListIteratorOfListOfShape it; it.Initialize(ERL);
242   for(; it.More(); it.Next()) {
243     const TopoDS_Edge& E = TopoDS::Edge(it.Value());
244     Standard_Real tolE = BRep_Tool::Tolerance(E);
245     Standard_Real maxtol = Max(tolE,GLOBAL_tolFF);
246     BRepAdaptor_Curve BAC(E);
247     f = BAC.FirstParameter(); l = BAC.LastParameter();
248     Standard_Boolean pinc = FUN_tool_PinC(Pm,BAC,f,l,maxtol);
249     if (pinc) {isone = Standard_True; break;}
250   }
251   return isone;
252 }
253
254 //=======================================================================
255 //function : IsVPtransLok
256 //purpose  : Computes the transition <T> of the VPoint <iVP> on the edge
257 //           <SI12>. Returns <False> if the status is unknown.
258 //=======================================================================
259 Standard_Boolean TopOpeBRep_FacesFiller::IsVPtransLok(const TopOpeBRep_LineInter& L,
260                                          const Standard_Integer iVP,const Standard_Integer SI12,
261                                          TopOpeBRepDS_Transition& T)
262 {
263   const TopOpeBRep_VPointInter& VP = L.VPoint(iVP);
264   Standard_Boolean is1 = (SI12 == 1);
265   Standard_Boolean VPonEd = (is1 && VP.IsOnDomS1());
266   VPonEd = VPonEd || (!is1 && VP.IsOnDomS2());
267   if (!VPonEd) return Standard_False;
268
269   const TopoDS_Edge& E = TopoDS::Edge(VP.Edge(SI12)); 
270   TopAbs_Orientation O = E.Orientation(); 
271   T = TopOpeBRep_FFTransitionTool::ProcessLineTransition(VP,SI12,O);
272   Standard_Boolean u = T.IsUnknown();
273   return (!u);
274 }
275
276 Standard_Boolean TopOpeBRep_FacesFiller::TransvpOK(const TopOpeBRep_LineInter& L,
277                                       const Standard_Integer ivp,
278                                       const Standard_Integer SI,
279                                       const Standard_Boolean isINOUT)
280 {
281 #define M_INOUT(stf,stl) ((stf == TopAbs_IN) && (stl == TopAbs_OUT))
282 #define M_OUTIN(stf,stl) ((stf == TopAbs_OUT) && (stl == TopAbs_IN))
283
284   TopOpeBRepDS_Transition T;
285   Standard_Boolean ok = TopOpeBRep_FacesFiller::IsVPtransLok(L,ivp,SI,T);
286   if (ok) {
287     TopAbs_State stb = T.Before();
288     TopAbs_State sta = T.After();
289     if (isINOUT) ok = M_INOUT(stb,sta);
290     else ok = M_OUTIN(stb,sta);
291   }
292   return ok;
293 }
294
295 //=======================================================================
296 //function : VPParamOnER
297 //purpose  :
298 //=======================================================================
299 Standard_Real TopOpeBRep_FacesFiller::VPParamOnER(const TopOpeBRep_VPointInter& vp,
300                                         const TopOpeBRep_LineInter& Lrest)
301 {
302   // If vp(index) is an edge boundary returns the point's parameter.
303
304   const TopoDS_Edge& E = TopoDS::Edge(Lrest.Arc());
305   Standard_Boolean isedge1 =  Lrest.ArcIsEdge(1);
306   Standard_Boolean isedge2 =  Lrest.ArcIsEdge(2);
307   if (isedge1 && vp.IsVertexOnS1()) {
308     const TopoDS_Vertex& v1 = TopoDS::Vertex(vp.VertexOnS1());
309     Standard_Real rr = BRep_Tool::Parameter(v1,E);
310     return rr;
311   }
312   if (isedge2 && vp.IsVertexOnS2())  {
313     const TopoDS_Vertex& v2 = TopoDS::Vertex(vp.VertexOnS2());
314     Standard_Real rr = BRep_Tool::Parameter(v2,E);
315     return rr;
316   }
317   // vp is an intersection point,and we get it's parameter.
318   if (isedge1 && vp.IsOnDomS1()) return vp.ParameterOnArc1();
319   if (isedge2 && vp.IsOnDomS2()) return vp.ParameterOnArc2();
320   
321   // Else,we have to project the point on the edge restriction
322   Standard_Real tolee = BRep_Tool::Tolerance(E);
323   tolee = tolee * 1.e2; //xpu290998 : PRO15369
324   Standard_Real param, dist; Standard_Boolean projok = FUN_tool_projPonE(vp.Value(),tolee,E,param,dist);
325   if (!projok) {
326     throw Standard_ProgramError("TopOpeBRep_FacesFiller::VPParamOnER");
327   }
328   return param;
329 }
330
331 //Standard_EXPORT Standard_Boolean FUN_EqualPonR(const TopOpeBRep_LineInter& Lrest,
332 Standard_EXPORT Standard_Boolean FUN_EqualPonR(const TopOpeBRep_LineInter& ,
333                                   const TopOpeBRep_VPointInter& VP1,
334                                   const TopOpeBRep_VPointInter& VP2)
335 {
336   gp_Pnt P1 = VP1.Value(); gp_Pnt P2 = VP2.Value();
337   Standard_Real Ptol1 = VP1.Tolerance(),Ptol2 = VP2.Tolerance();
338   Standard_Real Ptol = (Ptol1 > Ptol2) ? Ptol1 : Ptol2;
339   Standard_Boolean Pequal = P1.IsEqual(P2,Ptol);
340   return Pequal;
341 }
342 Standard_EXPORT Standard_Boolean FUN_EqualponR(const TopOpeBRep_LineInter& Lrest,
343                                   const TopOpeBRep_VPointInter& VP1,
344                                   const TopOpeBRep_VPointInter& VP2)
345 {
346   Standard_Real p1 = TopOpeBRep_FacesFiller::VPParamOnER(VP1,Lrest);
347   Standard_Real p2 = TopOpeBRep_FacesFiller::VPParamOnER(VP2,Lrest);
348   Standard_Boolean pequal = fabs(p1-p2) < Precision::PConfusion();
349   return pequal;
350 }
351
352 //=======================================================================
353 //function : EqualpPOnR
354 //purpose  : 
355 //=======================================================================
356 Standard_Boolean TopOpeBRep_FacesFiller::EqualpPonR(const TopOpeBRep_LineInter& Lrest,
357                                        const TopOpeBRep_VPointInter& VP1,
358                                        const TopOpeBRep_VPointInter& VP2)
359 {
360   Standard_Boolean Pequal = ::FUN_EqualPonR(Lrest,VP1,VP2);
361   Standard_Boolean pequal = ::FUN_EqualponR(Lrest,VP1,VP2);
362   Standard_Boolean pPequal = Pequal && pequal;
363   return pPequal;
364 }