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