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