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