0031035: Coding - uninitialized class fields reported by Visual Studio Code Analysis
[occt.git] / src / ShapeUpgrade / ShapeUpgrade_FaceDivide.cxx
1 // Created on: 1999-04-26
2 // Created by: Andrey BETENEV
3 // Copyright (c) 1999-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 //    gka  01.06.99 S4205: changing order of splitting surface/curves for converting to bezier
18
19 #include <Bnd_Box2d.hxx>
20 #include <BRep_Builder.hxx>
21 #include <BRep_Tool.hxx>
22 #include <BRepTools.hxx>
23 #include <Precision.hxx>
24 #include <ShapeAnalysis.hxx>
25 #include <ShapeAnalysis_Edge.hxx>
26 #include <ShapeBuild_Edge.hxx>
27 #include <ShapeBuild_ReShape.hxx>
28 #include <ShapeExtend.hxx>
29 #include <ShapeExtend_CompositeSurface.hxx>
30 #include <ShapeFix_ComposeShell.hxx>
31 #include <ShapeUpgrade_FaceDivide.hxx>
32 #include <ShapeUpgrade_SplitSurface.hxx>
33 #include <ShapeUpgrade_WireDivide.hxx>
34 #include <Standard_Type.hxx>
35 #include <TopExp_Explorer.hxx>
36 #include <TopoDS.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS_Face.hxx>
39 #include <TopoDS_Iterator.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Vertex.hxx>
42
43 IMPLEMENT_STANDARD_RTTIEXT(ShapeUpgrade_FaceDivide,ShapeUpgrade_Tool)
44
45 //=======================================================================
46 //function : ShapeUpgrade_FaceDivide
47 //purpose  : 
48 //=======================================================================
49 ShapeUpgrade_FaceDivide::ShapeUpgrade_FaceDivide():
50        ShapeUpgrade_Tool(), myStatus(0)
51 {
52   mySegmentMode = Standard_True;
53   mySplitSurfaceTool = new ShapeUpgrade_SplitSurface;
54   myWireDivideTool = new ShapeUpgrade_WireDivide;
55 }
56
57 //=======================================================================
58 //function : ShapeUpgrade_FaceDivide
59 //purpose  : 
60 //=======================================================================
61
62 ShapeUpgrade_FaceDivide::ShapeUpgrade_FaceDivide (const TopoDS_Face &F):
63        ShapeUpgrade_Tool(), myStatus(0)
64 {
65   mySegmentMode = Standard_True;
66   mySplitSurfaceTool = new ShapeUpgrade_SplitSurface;
67   myWireDivideTool = new ShapeUpgrade_WireDivide;
68   Init ( F );
69 }
70
71 //=======================================================================
72 //function : Init
73 //purpose  : 
74 //=======================================================================
75
76 void ShapeUpgrade_FaceDivide::Init (const TopoDS_Face &F)
77 {
78   myResult = myFace = F;
79 }
80   
81
82 //=======================================================================
83 //function : SetSurfaceSegmentMode
84 //purpose  : 
85 //=======================================================================
86
87 void ShapeUpgrade_FaceDivide::SetSurfaceSegmentMode(const Standard_Boolean Segment)
88 {
89   mySegmentMode = Segment;
90 }
91
92
93 //=======================================================================
94 //function : Perform
95 //purpose  : 
96 //=======================================================================
97
98 Standard_Boolean ShapeUpgrade_FaceDivide::Perform ()
99 {
100   myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
101   if ( myFace.IsNull() ) return Standard_False;
102   myResult = myFace;
103   SplitSurface();
104   SplitCurves();
105   return Status ( ShapeExtend_DONE );
106  } 
107
108 //=======================================================================
109 //function : SplitSurface
110 //purpose  : 
111 //=======================================================================
112
113 Standard_Boolean ShapeUpgrade_FaceDivide::SplitSurface ()  
114 {  
115   Handle(ShapeUpgrade_SplitSurface) SplitSurf = GetSplitSurfaceTool();
116   if ( SplitSurf.IsNull() ) return Standard_False;
117
118   // myResult should be face; else return with FAIL
119   if ( myResult.IsNull() || myResult.ShapeType() != TopAbs_FACE ) {
120     myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
121     return Standard_False;
122   }
123   TopoDS_Face face = TopoDS::Face ( myResult );
124   
125   TopLoc_Location L;
126   Handle(Geom_Surface) surf;
127   surf = BRep_Tool::Surface ( face, L );
128   
129   Standard_Real Uf,Ul,Vf,Vl;
130 //  BRepTools::UVBounds(myFace,Uf,Ul,Vf,Vl);
131   ShapeAnalysis::GetFaceUVBounds ( face, Uf, Ul, Vf, Vl );
132   if(Precision::IsInfinite(Uf) || Precision::IsInfinite(Ul) ||
133      Precision::IsInfinite(Vf) || Precision::IsInfinite(Vl))
134     return Standard_False;
135
136   // make little extension to ensure all pcurves fit inside new surface bounds
137   Standard_Real aSUf, aSUl, aSVf, aSVl;
138   surf->Bounds(aSUf, aSUl, aSVf, aSVl);
139   if (!surf->IsUPeriodic()) {
140     Standard_Real dU = (Ul - Uf) * 0.01;
141     if (Uf > aSUf) Uf -= Min(dU, Uf - aSUf);
142     if (Ul < aSUl) Ul += Min(dU, aSUl - Ul);
143   }
144   if (!surf->IsVPeriodic()) {
145     Standard_Real dV = (Vl - Vf) * 0.01;
146     if (Vf > aSVf) Vf -= Min(dV, Vf - aSVf);
147     if (Vl < aSVl) Vl += Min(dV, aSVl - Vl);
148   }
149
150   SplitSurf->Init ( surf, Uf, Ul, Vf, Vl );
151   SplitSurf->Perform(mySegmentMode);
152
153   // If surface was neither splitted nor modified, do nothing
154   if ( ! SplitSurf->Status ( ShapeExtend_DONE ) ) return Standard_False;
155     
156   // if surface was modified, force copying all vertices (and edges as consequence)
157   // to protect original shape from increasing tolerance after SameParameter
158   if ( SplitSurf->Status ( ShapeExtend_DONE3 ) )
159     for (TopExp_Explorer exp(face,TopAbs_VERTEX); exp.More(); exp.Next() ) {
160       if ( Context()->IsRecorded ( exp.Current() ) ) continue;
161 //smh#8
162       TopoDS_Shape emptyCopied = exp.Current().EmptyCopied();
163       TopoDS_Vertex V = TopoDS::Vertex ( emptyCopied );
164       Context()->Replace ( exp.Current(), V );
165     }
166     
167   Handle(ShapeExtend_CompositeSurface) Grid = SplitSurf->ResSurfaces();
168   
169   ShapeFix_ComposeShell CompShell;
170   CompShell.Init( Grid, L, face, Precision() );
171   CompShell.SetContext(Context());
172   CompShell.SetMaxTolerance(MaxTolerance());
173   Handle(ShapeUpgrade_WireDivide) SplitWire = GetWireDivideTool();
174   if ( ! SplitWire.IsNull() )
175        CompShell.SetTransferParamTool(GetWireDivideTool()->GetTransferParamTool());
176   CompShell.Perform();
177   if ( CompShell.Status ( ShapeExtend_FAIL ) || 
178        ! CompShell.Status ( ShapeExtend_DONE ) ) 
179     myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
180
181   myResult = CompShell.Result();
182   myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
183
184   return Standard_True;
185 }
186  
187 //=======================================================================
188 //function : SplitCurves
189 //purpose  : 
190 //=======================================================================
191
192 Standard_Boolean ShapeUpgrade_FaceDivide::SplitCurves ()
193 {
194   Handle(ShapeUpgrade_WireDivide) SplitWire = GetWireDivideTool();
195   if ( SplitWire.IsNull() ) return Standard_False;
196   
197   SplitWire->SetMaxTolerance(MaxTolerance());
198   for ( TopExp_Explorer explf(myResult,TopAbs_FACE); explf.More(); explf.Next()) {
199     TopoDS_Shape S = Context()->Apply ( explf.Current(), TopAbs_SHAPE);
200     
201     // S should be face; else return with FAIL
202     if ( S.IsNull() || S.ShapeType() != TopAbs_FACE ) {
203       myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
204       return Standard_False;
205     }
206     TopoDS_Face F = TopoDS::Face ( S );
207     
208     SplitWire->SetFace ( F );
209     for ( TopoDS_Iterator wi(F,Standard_False); wi.More(); wi.Next() ) {
210       //TopoDS_Wire wire = TopoDS::Wire ( wi.Value() );
211       // modifications already defined in context are to be applied inside SplitWire
212       if(wi.Value().ShapeType() !=TopAbs_WIRE)
213         continue;
214       TopoDS_Wire wire = TopoDS::Wire(wi.Value());
215       SplitWire->Load ( wire );
216       SplitWire->SetContext(Context());
217       SplitWire->Perform ();
218       if ( SplitWire->Status ( ShapeExtend_FAIL ) )
219         myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
220       if ( SplitWire->Status ( ShapeExtend_DONE ) ) {
221         myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
222         Context()->Replace ( wire, SplitWire->Wire() );
223       }
224     }
225   }
226   myResult = Context()->Apply ( myResult );
227   return Status ( ShapeExtend_DONE );
228 }
229
230 //=======================================================================
231 //function : Shell
232 //purpose  : 
233 //=======================================================================
234
235 TopoDS_Shape ShapeUpgrade_FaceDivide::Result () const
236 {
237   return myResult;
238 }
239
240 //=======================================================================
241 //function : Status
242 //purpose  : 
243 //=======================================================================
244
245 Standard_Boolean ShapeUpgrade_FaceDivide::Status (const ShapeExtend_Status status) const
246 {
247   return ShapeExtend::DecodeStatus ( myStatus, status );
248 }
249     
250 //=======================================================================
251 //function : SetSplitSurfaceTool
252 //purpose  : 
253 //=======================================================================
254
255 void ShapeUpgrade_FaceDivide::SetSplitSurfaceTool(const Handle(ShapeUpgrade_SplitSurface)& splitSurfaceTool)
256 {
257   mySplitSurfaceTool = splitSurfaceTool;
258 }
259
260 //=======================================================================
261 //function : SetWireDivideTool
262 //purpose  : 
263 //=======================================================================
264
265 void ShapeUpgrade_FaceDivide::SetWireDivideTool(const Handle(ShapeUpgrade_WireDivide)& wireDivideTool)
266 {
267   myWireDivideTool = wireDivideTool;
268 }
269
270 //=======================================================================
271 //function : GetSplitSurfaceTool
272 //purpose  : 
273 //=======================================================================
274
275 Handle(ShapeUpgrade_SplitSurface) ShapeUpgrade_FaceDivide::GetSplitSurfaceTool () const
276 {
277   return mySplitSurfaceTool;
278 }
279         
280 //=======================================================================
281 //function : GetWireDivideTool
282 //purpose  : 
283 //=======================================================================
284
285 Handle(ShapeUpgrade_WireDivide) ShapeUpgrade_FaceDivide::GetWireDivideTool () const
286 {
287   return myWireDivideTool;
288 }