Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/geom.git] / src / GEOMAlgo / BlockFix_PeriodicSurfaceModifier.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 // File:      BlockFix_PeriodicSurfaceModifier.cxx
21 // Created:   15.12.04 10:08:50
22 // Author:    Sergey KUUL
23 // Copyright: Open CASCADE SA 2004
24
25 #include <BlockFix_PeriodicSurfaceModifier.ixx>
26
27 #include <BRep_Builder.hxx>
28 #include <BRep_Tool.hxx>
29 #include <BRepTools.hxx>
30 #include <Geom_CylindricalSurface.hxx>
31 #include <Geom_SphericalSurface.hxx>
32 #include <ShapeFix_Edge.hxx>
33 #include <TopExp.hxx>
34
35
36 //=======================================================================
37 //function : BlockFix_PeriodicSurfaceModifier()
38 //purpose  : Constructor
39 //=======================================================================
40
41 BlockFix_PeriodicSurfaceModifier::BlockFix_PeriodicSurfaceModifier (  )
42 {
43   myMapOfFaces.Clear();
44   myMapOfSurfaces.Clear();
45 }
46
47
48 //=======================================================================
49 //function : SetTolerance
50 //purpose  : 
51 //=======================================================================
52
53 void BlockFix_PeriodicSurfaceModifier::SetTolerance(const Standard_Real Tol)
54 {
55   myTolerance = Tol;
56 }
57
58
59 //=======================================================================
60 //function : ModifySurface
61 //purpose  : auxilary
62 //=======================================================================
63
64 static Standard_Boolean ModifySurface(const TopoDS_Face& aFace,
65                                       const Handle(Geom_Surface)& aSurface,
66                                       Handle(Geom_Surface)& aNewSurface)
67 {
68   Handle(Geom_Surface) S = aSurface;
69
70   if(S->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
71     Handle(Geom_CylindricalSurface) aCyl = 
72       Handle(Geom_CylindricalSurface)::DownCast(S);
73     Standard_Real Umin, Umax, Vmin, Vmax;
74     BRepTools::UVBounds(aFace, Umin, Umax, Vmin, Vmax);
75     if( Umin<-Precision::PConfusion() || Umax>2*PI+Precision::PConfusion() ) {
76       gp_Ax3 ax3 = aCyl->Position();
77       gp_Ax1 NDir = ax3.Axis();
78       gp_Ax3 newax3 = ax3.Rotated(NDir,Umin-Precision::PConfusion());
79       Handle(Geom_CylindricalSurface) aNewCyl =
80         new Geom_CylindricalSurface(newax3,aCyl->Radius());
81       aNewSurface = aNewCyl;
82       return Standard_True;
83     }
84   }
85   
86   if(S->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
87     Handle(Geom_SphericalSurface) aSphere = Handle(Geom_SphericalSurface)::DownCast(S);
88     Standard_Real Umin, Umax, Vmin, Vmax;
89     BRepTools::UVBounds(aFace, Umin, Umax, Vmin, Vmax);
90     if( Umin<-Precision::PConfusion() || Umax>2*PI+Precision::PConfusion() ) {
91       gp_Ax3 ax3 = aSphere->Position();
92       gp_Ax1 NDir = ax3.Axis();
93       gp_Ax3 newax3 = ax3.Rotated(NDir,Umin-Precision::PConfusion());
94       Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(newax3,aSphere->Radius());
95       aNewSurface = aNewSphere;
96       return Standard_True;
97     }
98   }
99
100   return Standard_False;
101 }
102                                       
103
104 //=======================================================================
105 //function : NewSurface
106 //purpose  : 
107 //=======================================================================
108
109 Standard_Boolean BlockFix_PeriodicSurfaceModifier::NewSurface(const TopoDS_Face& F,
110                                                               Handle(Geom_Surface)& S,
111                                                               TopLoc_Location& L,Standard_Real& Tol,
112                                                               Standard_Boolean& RevWires,
113                                                               Standard_Boolean& RevFace) 
114 {
115   TopLoc_Location LS;
116   Handle(Geom_Surface) SIni = BRep_Tool::Surface(F, LS);
117   
118   if(ModifySurface(F, SIni, S)) {
119   
120     RevWires = Standard_False;
121     RevFace = Standard_False;
122     
123     L = LS;
124     Tol = BRep_Tool::Tolerance(F);
125      
126     Standard_Integer anIndex = myMapOfSurfaces.Add(S);
127     myMapOfFaces.Bind(F,anIndex);
128     return Standard_True;
129   }
130
131   return Standard_False;
132 }
133
134
135 //=======================================================================
136 //function : NewCurve
137 //purpose  : 
138 //=======================================================================
139
140 Standard_Boolean BlockFix_PeriodicSurfaceModifier::NewCurve(const TopoDS_Edge& /*E*/,
141                                                             Handle(Geom_Curve)& /*C*/,
142                                                             TopLoc_Location& /*L*/,
143                                                             Standard_Real& /*Tol*/) 
144 {
145   return Standard_False;
146 }
147
148
149 //=======================================================================
150 //function : NewPoint
151 //purpose  : 
152 //=======================================================================
153
154 Standard_Boolean BlockFix_PeriodicSurfaceModifier::NewPoint(const TopoDS_Vertex& /*V*/,
155                                                             gp_Pnt& /*P*/,
156                                                             Standard_Real& /*Tol*/) 
157 {
158   return Standard_False;
159 }
160
161
162 //=======================================================================
163 //function : NewCurve2d
164 //purpose  : 
165 //=======================================================================
166
167 Standard_Boolean BlockFix_PeriodicSurfaceModifier::NewCurve2d(const TopoDS_Edge& E,
168                                                               const TopoDS_Face& F,
169                                                               const TopoDS_Edge& /*NewE*/,
170                                                               const TopoDS_Face& /*NewF*/,
171                                                               Handle(Geom2d_Curve)& C,
172                                                               Standard_Real& Tol) 
173 {
174   //check if undelying surface of the face was modified
175   if(myMapOfFaces.IsBound(F)) {
176     Standard_Integer anIndex = myMapOfFaces.Find(F);
177     
178     Handle(Geom_Surface) aNewSurf = Handle(Geom_Surface)::DownCast(myMapOfSurfaces.FindKey(anIndex));
179     
180     Standard_Real f,l;
181     TopLoc_Location LC, LS;
182     Handle(Geom_Curve) C3d = BRep_Tool::Curve ( E, LC, f, l );
183     Handle(Geom_Surface) S = BRep_Tool::Surface(F, LS);
184   
185     //taking into accound the orientation of the seam
186     C = BRep_Tool::CurveOnSurface(E,F,f,l);
187     Tol = BRep_Tool::Tolerance(E);
188      
189     BRep_Builder B;
190     TopoDS_Edge TempE;
191     B.MakeEdge(TempE);
192     B.Add(TempE, TopExp::FirstVertex(E));
193     B.Add(TempE, TopExp::LastVertex(E));
194
195     if(!C3d.IsNull()) 
196       B.UpdateEdge(TempE, Handle(Geom_Curve)::DownCast(C3d->Transformed(LC.Transformation())), Precision::Confusion());
197     B.Range(TempE, f, l);
198     
199     Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
200     Handle(Geom_Surface) STemp = Handle(Geom_Surface)::DownCast(aNewSurf->Transformed(LS.Transformation()));
201     TopLoc_Location LTemp;
202     LTemp.Identity();
203     
204     Standard_Boolean isClosed = BRep_Tool::IsClosed (E, F);
205     Standard_Real aWorkTol = 2*myTolerance+Tol;
206     sfe->FixAddPCurve(TempE, STemp, LTemp, isClosed, Max(Precision::Confusion(), aWorkTol));
207     sfe->FixSameParameter(TempE);
208       
209     //keep the orientation of original edge
210     TempE.Orientation(E.Orientation());
211     C = BRep_Tool::CurveOnSurface(TempE, STemp, LTemp, f, l);
212     
213     //surface was modified
214     return Standard_True;
215   }
216
217   return Standard_False;
218 }
219
220
221 //=======================================================================
222 //function : NewParameter
223 //purpose  : 
224 //=======================================================================
225
226 Standard_Boolean BlockFix_PeriodicSurfaceModifier::NewParameter(const TopoDS_Vertex& /*V*/,
227                                                                 const TopoDS_Edge& /*E*/,
228                                                                 Standard_Real& /*P*/,
229                                                                 Standard_Real& /*Tol*/) 
230 {
231   return Standard_False;
232 }
233
234
235 //=======================================================================
236 //function : Continuity
237 //purpose  : 
238 //=======================================================================
239
240 GeomAbs_Shape BlockFix_PeriodicSurfaceModifier::Continuity(const TopoDS_Edge& E,
241                                                            const TopoDS_Face& F1,
242                                                            const TopoDS_Face& F2,
243                                                            const TopoDS_Edge& /*NewE*/,
244                                                            const TopoDS_Face& /*NewF1*/,
245                                                            const TopoDS_Face& /*NewF2*/) 
246 {
247   return BRep_Tool::Continuity(E,F1,F2);
248 }
249