Salome HOME
Update copyright information
[modules/geom.git] / src / GEOMAlgo / BlockFix.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:        BlockFix.cxx
24 // Created:     Tue Dec  7 11:59:05 2004
25 // Author:      Pavel DURANDIN
26 //
27 #include <BlockFix.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <TopTools_DataMapOfShapeShape.hxx>
30 #include <ShapeCustom.hxx>
31 #include <BRepTools.hxx>
32 #include <ShapeBuild_ReShape.hxx>
33 #include <TopoDS_Face.hxx>
34 #include <TopExp_Explorer.hxx>
35 #include <TopoDS.hxx>
36 #include <TopLoc_Location.hxx>
37 #include <Geom_Surface.hxx>
38 #include <Geom_CylindricalSurface.hxx>
39 #include <Geom_ConicalSurface.hxx>
40 #include <ShapeFix_Wire.hxx>
41 #include <TopoDS_Wire.hxx>
42 #include <BRepTools_Modifier.hxx>
43 #include <Geom_SphericalSurface.hxx>
44 #include <Geom_ToroidalSurface.hxx>
45 #include <BRep_Tool.hxx>
46 #include <TopoDS_Edge.hxx>
47 #include <Geom2d_Curve.hxx>
48 #include <BRep_Builder.hxx>
49 #include <ShapeAnalysis_Edge.hxx>
50 #include <ShapeFix_Edge.hxx>
51 #include <ShapeFix.hxx>
52 #include <ShapeFix_Face.hxx>
53 #include <ShapeAnalysis.hxx>
54
55 #include <TColgp_SequenceOfPnt2d.hxx>
56 #include <ShapeAnalysis_Curve.hxx>
57 #include <TopoDS_Vertex.hxx>
58 #include <ShapeBuild_Edge.hxx>
59
60 #include <BlockFix_SphereSpaceModifier.hxx>
61 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
62 #include <TopTools_MapOfShape.hxx>
63 #include <BlockFix_PeriodicSurfaceModifier.hxx>
64
65 #include <TopoDS_Solid.hxx>
66
67
68 //=======================================================================
69 //function : FixResult
70 //purpose  : auxilary
71 //=======================================================================
72 static void FixResult(const TopoDS_Shape& result,
73                       Handle(ShapeBuild_ReShape)& Context,
74                       const Standard_Real Tol)
75 {
76   for (TopExp_Explorer ex_f(result,TopAbs_FACE); ex_f.More(); ex_f.Next()) {
77     TopoDS_Shape aShape = Context->Apply(ex_f.Current().Oriented(TopAbs_FORWARD));
78     // face coud not be dropped or splitted on this step
79     TopoDS_Face aFace = TopoDS::Face(aShape);
80     TopLoc_Location L;
81     Handle(Geom_Surface) Surf = BRep_Tool::Surface(aFace,L);
82     
83     if( Surf->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ||
84         Surf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ) {
85     
86       Standard_Integer nbWires = 0;
87       for (TopExp_Explorer ex_w(aFace,TopAbs_WIRE); ex_w.More(); ex_w.Next()) {
88         nbWires++;
89         Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(TopoDS::Wire(ex_w.Current()), 
90                                                       aFace, 
91                                                       Precision::Confusion());
92         sfw->FixReorder();
93         if(sfw->StatusReorder ( ShapeExtend_FAIL ))
94           continue;
95         
96         sfw->SetPrecision(2.*Tol);
97         sfw->FixShifted();
98         
99         Standard_Boolean isDone = sfw->LastFixStatus ( ShapeExtend_DONE );
100         isDone |= sfw->FixDegenerated();
101         
102         // remove degenerated edges from not degenerated points
103         ShapeAnalysis_Edge sae;
104         Handle(ShapeExtend_WireData) sewd = sfw->WireData();
105         Standard_Integer i;
106         for( i = 1; i<=sewd->NbEdges();i++) {
107           TopoDS_Edge E = sewd->Edge(i);
108           if(BRep_Tool::Degenerated(E)&&!sae.HasPCurve(E,aFace)) {
109             sewd->Remove(i);
110             isDone = Standard_True;
111             i--;
112           }
113         }
114         
115         //isDone |= sfw->FixLacking(); // commented by skl 22.03.2005 (PAL8395)
116         
117         // remove neighbour seam edges 
118         if(isDone) {
119           for( i = 1; i<sewd->NbEdges();i++) {
120             if(sewd->IsSeam(i) && sewd->IsSeam(i+1)) {
121               isDone = Standard_True;
122               sewd->Remove(i);
123               sewd->Remove(i);
124               i--;
125             }
126           }
127           if(sewd->IsSeam(1) && sewd->IsSeam(sewd->NbEdges())) {
128             sewd->Remove(1);
129             sewd->Remove(sewd->NbEdges());
130           }
131         }
132         
133                
134         if(isDone) {
135           TopoDS_Wire ResWire = sfw->Wire();
136           Context->Replace(ex_w.Current(), ResWire);
137         };
138       }
139       // Implement fix orientation in case of several wires
140       if(nbWires > 1) {
141         TopoDS_Face aFixedFace = TopoDS::Face(Context->Apply(aFace));
142         Handle(ShapeFix_Face) sff = new ShapeFix_Face(aFixedFace);
143         if(sff->FixOrientation())
144           Context->Replace(aFixedFace,sff->Face());
145       }
146       
147     }
148   }
149 }
150
151
152
153
154
155 //=======================================================================
156 //function : ConvertToAnalytical
157 //purpose  : 
158 //=======================================================================
159
160 TopoDS_Shape BlockFix::RotateSphereSpace (const TopoDS_Shape& S,
161                                           const Standard_Real Tol) 
162 {
163
164   // Create a modification description
165   Handle(BlockFix_SphereSpaceModifier) SR = new BlockFix_SphereSpaceModifier;
166   SR->SetTolerance(Tol);
167
168   TopTools_DataMapOfShapeShape context;
169   BRepTools_Modifier MD;
170   TopoDS_Shape result = ShapeCustom::ApplyModifier ( S, SR, context,MD );
171   
172   Handle(ShapeBuild_ReShape) RS = new ShapeBuild_ReShape;
173   FixResult(result,RS,Tol);
174   result = RS->Apply(result);
175   
176   ShapeFix_Edge sfe;
177   for(TopExp_Explorer exp(result,TopAbs_EDGE); exp.More(); exp.Next()) {
178     TopoDS_Edge E = TopoDS::Edge(exp.Current());
179     sfe.FixVertexTolerance (E);
180   }
181   
182   ShapeFix::SameParameter(result,Standard_False);
183   return result;
184 }
185
186
187 //=======================================================================
188 //function : FixRanges
189 //purpose  : 
190 //=======================================================================
191
192 TopoDS_Shape BlockFix::FixRanges (const TopoDS_Shape& S,
193                                   const Standard_Real Tol) 
194 {
195   // Create a modification description
196   Handle(BlockFix_PeriodicSurfaceModifier) SR = new BlockFix_PeriodicSurfaceModifier;
197   SR->SetTolerance(Tol);
198
199   TopTools_DataMapOfShapeShape context;
200   BRepTools_Modifier MD;
201   TopoDS_Shape result = ShapeCustom::ApplyModifier ( S, SR, context,MD );
202   
203   Handle(ShapeBuild_ReShape) RS = new ShapeBuild_ReShape;
204   FixResult(result,RS,Tol);
205   result = RS->Apply(result);
206   
207   ShapeFix_Edge sfe;
208   for(TopExp_Explorer exp(result,TopAbs_EDGE); exp.More(); exp.Next()) {
209     TopoDS_Edge E = TopoDS::Edge(exp.Current());
210     sfe.FixVertexTolerance (E);
211   }
212   
213   ShapeFix::SameParameter(result,Standard_False);
214
215   return result;
216 }