Salome HOME
Merge from BR_V5_DEV 17Feb09
[modules/geom.git] / src / GEOMAlgo / BlockFix_PeriodicSurfaceModifier.cxx
1 //  Copyright (C) 2007-2008  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 // File:      BlockFix_PeriodicSurfaceModifier.cxx
23 // Created:   15.12.04 10:08:50
24 // Author:    Sergey KUUL
25 //
26 #include <BlockFix_PeriodicSurfaceModifier.ixx>
27
28 #include <BRep_Builder.hxx>
29 #include <BRep_Tool.hxx>
30 #include <BRepTools.hxx>
31 #include <Geom_CylindricalSurface.hxx>
32 #include <Geom_SphericalSurface.hxx>
33 #include <ShapeFix_Edge.hxx>
34 #include <TopExp.hxx>
35
36
37 //=======================================================================
38 //function : BlockFix_PeriodicSurfaceModifier()
39 //purpose  : Constructor
40 //=======================================================================
41
42 BlockFix_PeriodicSurfaceModifier::BlockFix_PeriodicSurfaceModifier (  )
43 {
44   myMapOfFaces.Clear();
45   myMapOfSurfaces.Clear();
46 }
47
48
49 //=======================================================================
50 //function : SetTolerance
51 //purpose  : 
52 //=======================================================================
53
54 void BlockFix_PeriodicSurfaceModifier::SetTolerance(const Standard_Real Tol)
55 {
56   myTolerance = Tol;
57 }
58
59
60 //=======================================================================
61 //function : ModifySurface
62 //purpose  : auxilary
63 //=======================================================================
64
65 static Standard_Boolean ModifySurface(const TopoDS_Face& aFace,
66                                       const Handle(Geom_Surface)& aSurface,
67                                       Handle(Geom_Surface)& aNewSurface)
68 {
69   Handle(Geom_Surface) S = aSurface;
70
71   if(S->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
72     Handle(Geom_CylindricalSurface) aCyl = 
73       Handle(Geom_CylindricalSurface)::DownCast(S);
74     Standard_Real Umin, Umax, Vmin, Vmax;
75     BRepTools::UVBounds(aFace, Umin, Umax, Vmin, Vmax);
76     if( Umin<-Precision::PConfusion() || Umax>2*PI+Precision::PConfusion() ) {
77       gp_Ax3 ax3 = aCyl->Position();
78       gp_Ax1 NDir = ax3.Axis();
79       gp_Ax3 newax3 = ax3.Rotated(NDir,Umin-Precision::PConfusion());
80       Handle(Geom_CylindricalSurface) aNewCyl =
81         new Geom_CylindricalSurface(newax3,aCyl->Radius());
82       aNewSurface = aNewCyl;
83       return Standard_True;
84     }
85   }
86   
87   if(S->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
88     Handle(Geom_SphericalSurface) aSphere = Handle(Geom_SphericalSurface)::DownCast(S);
89     Standard_Real Umin, Umax, Vmin, Vmax;
90     BRepTools::UVBounds(aFace, Umin, Umax, Vmin, Vmax);
91     if( Umin<-Precision::PConfusion() || Umax>2*PI+Precision::PConfusion() ) {
92       gp_Ax3 ax3 = aSphere->Position();
93       gp_Ax1 NDir = ax3.Axis();
94       gp_Ax3 newax3 = ax3.Rotated(NDir,Umin-Precision::PConfusion());
95       Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(newax3,aSphere->Radius());
96       aNewSurface = aNewSphere;
97       return Standard_True;
98     }
99   }
100
101   return Standard_False;
102 }
103                                       
104
105 //=======================================================================
106 //function : NewSurface
107 //purpose  : 
108 //=======================================================================
109
110 Standard_Boolean BlockFix_PeriodicSurfaceModifier::NewSurface(const TopoDS_Face& F,
111                                                               Handle(Geom_Surface)& S,
112                                                               TopLoc_Location& L,Standard_Real& Tol,
113                                                               Standard_Boolean& RevWires,
114                                                               Standard_Boolean& RevFace) 
115 {
116   TopLoc_Location LS;
117   Handle(Geom_Surface) SIni = BRep_Tool::Surface(F, LS);
118   
119   if(ModifySurface(F, SIni, S)) {
120   
121     RevWires = Standard_False;
122     RevFace = Standard_False;
123     
124     L = LS;
125     Tol = BRep_Tool::Tolerance(F);
126      
127     Standard_Integer anIndex = myMapOfSurfaces.Add(S);
128     myMapOfFaces.Bind(F,anIndex);
129     return Standard_True;
130   }
131
132   return Standard_False;
133 }
134
135
136 //=======================================================================
137 //function : NewCurve
138 //purpose  : 
139 //=======================================================================
140
141 Standard_Boolean BlockFix_PeriodicSurfaceModifier::NewCurve(const TopoDS_Edge& /*E*/,
142                                                             Handle(Geom_Curve)& /*C*/,
143                                                             TopLoc_Location& /*L*/,
144                                                             Standard_Real& /*Tol*/) 
145 {
146   return Standard_False;
147 }
148
149
150 //=======================================================================
151 //function : NewPoint
152 //purpose  : 
153 //=======================================================================
154
155 Standard_Boolean BlockFix_PeriodicSurfaceModifier::NewPoint(const TopoDS_Vertex& /*V*/,
156                                                             gp_Pnt& /*P*/,
157                                                             Standard_Real& /*Tol*/) 
158 {
159   return Standard_False;
160 }
161
162
163 //=======================================================================
164 //function : NewCurve2d
165 //purpose  : 
166 //=======================================================================
167
168 Standard_Boolean BlockFix_PeriodicSurfaceModifier::NewCurve2d(const TopoDS_Edge& E,
169                                                               const TopoDS_Face& F,
170                                                               const TopoDS_Edge& /*NewE*/,
171                                                               const TopoDS_Face& /*NewF*/,
172                                                               Handle(Geom2d_Curve)& C,
173                                                               Standard_Real& Tol) 
174 {
175   //check if undelying surface of the face was modified
176   if(myMapOfFaces.IsBound(F)) {
177     Standard_Integer anIndex = myMapOfFaces.Find(F);
178     
179     Handle(Geom_Surface) aNewSurf = Handle(Geom_Surface)::DownCast(myMapOfSurfaces.FindKey(anIndex));
180     
181     Standard_Real f,l;
182     TopLoc_Location LC, LS;
183     Handle(Geom_Curve) C3d = BRep_Tool::Curve ( E, LC, f, l );
184     Handle(Geom_Surface) S = BRep_Tool::Surface(F, LS);
185   
186     //taking into accound the orientation of the seam
187     C = BRep_Tool::CurveOnSurface(E,F,f,l);
188     Tol = BRep_Tool::Tolerance(E);
189      
190     BRep_Builder B;
191     TopoDS_Edge TempE;
192     B.MakeEdge(TempE);
193     B.Add(TempE, TopExp::FirstVertex(E));
194     B.Add(TempE, TopExp::LastVertex(E));
195
196     if(!C3d.IsNull()) 
197       B.UpdateEdge(TempE, Handle(Geom_Curve)::DownCast(C3d->Transformed(LC.Transformation())), Precision::Confusion());
198     B.Range(TempE, f, l);
199     
200     Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
201     Handle(Geom_Surface) STemp = Handle(Geom_Surface)::DownCast(aNewSurf->Transformed(LS.Transformation()));
202     TopLoc_Location LTemp;
203     LTemp.Identity();
204     
205     Standard_Boolean isClosed = BRep_Tool::IsClosed (E, F);
206     Standard_Real aWorkTol = 2*myTolerance+Tol;
207     sfe->FixAddPCurve(TempE, STemp, LTemp, isClosed, Max(Precision::Confusion(), aWorkTol));
208     sfe->FixSameParameter(TempE);
209       
210     //keep the orientation of original edge
211     TempE.Orientation(E.Orientation());
212     C = BRep_Tool::CurveOnSurface(TempE, STemp, LTemp, f, l);
213     
214     //surface was modified
215     return Standard_True;
216   }
217
218   return Standard_False;
219 }
220
221
222 //=======================================================================
223 //function : NewParameter
224 //purpose  : 
225 //=======================================================================
226
227 Standard_Boolean BlockFix_PeriodicSurfaceModifier::NewParameter(const TopoDS_Vertex& /*V*/,
228                                                                 const TopoDS_Edge& /*E*/,
229                                                                 Standard_Real& /*P*/,
230                                                                 Standard_Real& /*Tol*/) 
231 {
232   return Standard_False;
233 }
234
235
236 //=======================================================================
237 //function : Continuity
238 //purpose  : 
239 //=======================================================================
240
241 GeomAbs_Shape BlockFix_PeriodicSurfaceModifier::Continuity(const TopoDS_Edge& E,
242                                                            const TopoDS_Face& F1,
243                                                            const TopoDS_Face& F2,
244                                                            const TopoDS_Edge& /*NewE*/,
245                                                            const TopoDS_Face& /*NewF1*/,
246                                                            const TopoDS_Face& /*NewF2*/) 
247 {
248   return BRep_Tool::Continuity(E,F1,F2);
249 }
250