Salome HOME
fa86224076f79c00fd4ddeb40a0e1fc0e1e87d8b
[modules/geom.git] / src / BlockFix / BlockFix_SphereSpaceModifier.cxx
1 // Copyright (C) 2007-2021  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.cxx
24 // Created:     Tue Dec  7 11:59:05 2004
25 // Author:      Pavel DURANDIN
26
27 #include <BlockFix_SphereSpaceModifier.hxx>
28
29 #include <ShapeAnalysis.hxx>
30
31 #include <ShapeFix_Edge.hxx>
32
33 #include <TopExp.hxx>
34
35 #include <TopLoc_Location.hxx>
36
37 #include <TopoDS.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Face.hxx>
40 #include <TopoDS_Vertex.hxx>
41
42 #include <BRep_Tool.hxx>
43 #include <BRep_Builder.hxx>
44
45 #include <BRepGProp.hxx>
46 #include <GProp_GProps.hxx>
47
48 #include <Geom_SphericalSurface.hxx>
49 #include <Geom_RectangularTrimmedSurface.hxx>
50
51 #include <Geom_Curve.hxx>
52 #include <Geom_Surface.hxx>
53
54 #include <Geom2d_Curve.hxx>
55
56 #include <gp_Pnt.hxx>
57 #include <gp_Sphere.hxx>
58
59 IMPLEMENT_STANDARD_RTTIEXT(BlockFix_SphereSpaceModifier, BRepTools_Modification)
60
61 //=======================================================================
62 //function : BlockFix_SphereSpaceModifier
63 //purpose  :
64 //=======================================================================
65 BlockFix_SphereSpaceModifier::BlockFix_SphereSpaceModifier()
66 {
67   myMapOfFaces.Clear();
68   myMapOfSpheres.Clear();
69 }
70
71 //=======================================================================
72 //function : ~BlockFix_SphereSpaceModifier
73 //purpose  :
74 //=======================================================================
75 BlockFix_SphereSpaceModifier::~BlockFix_SphereSpaceModifier() {}
76
77 //=======================================================================
78 //function : SetTolerance
79 //purpose  :
80 //=======================================================================
81 void BlockFix_SphereSpaceModifier::SetTolerance(const Standard_Real Tol)
82 {
83   myTolerance = Tol;
84 }
85
86 //=======================================================================
87 //function : NewSurface
88 //purpose  :
89 //=======================================================================
90 static Standard_Boolean ModifySurface(const TopoDS_Face& aFace,
91                                       const Handle(Geom_Surface)& aSurface,
92                                       Handle(Geom_Surface)& aNewSurface)
93 {
94   Handle(Geom_Surface) S = aSurface;
95   if(S->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
96     Handle(Geom_RectangularTrimmedSurface) RTS =
97       Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
98     S = RTS->BasisSurface();
99   }
100
101   if(S->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
102     Standard_Real Umin, Umax, Vmin, Vmax;
103     ShapeAnalysis::GetFaceUVBounds(aFace,Umin, Umax, Vmin, Vmax);
104     Standard_Real PI2 = M_PI/2.;
105     if(Vmax > PI2 - Precision::PConfusion() || Vmin < -PI2+::Precision::PConfusion()) {
106       Handle(Geom_SphericalSurface) aSphere = Handle(Geom_SphericalSurface)::DownCast(S);
107       gp_Sphere sp = aSphere->Sphere();
108       //modified by jgv, 12.11.2012 for issue 21777//
109       Standard_Real Radius = sp.Radius();
110       Standard_Real HalfArea = 2.*M_PI*Radius*Radius;
111       GProp_GProps Properties;
112       BRepGProp::SurfaceProperties(aFace, Properties);
113       Standard_Real anArea = Properties.Mass();
114       Standard_Real AreaTol = Radius*Radius*1.e-6;
115       if (anArea > HalfArea - AreaTol) //no chance to avoid singularity
116         return Standard_False;
117       ///////////////////////////////////////////////
118       gp_Ax3 ax3 = sp.Position();
119       if(Abs(Vmax-Vmin) < PI2) {
120         gp_Ax3 axnew3(ax3.Axis().Location(), ax3.Direction()^ax3.XDirection(),ax3.XDirection());
121         sp.SetPosition(axnew3);
122         Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
123         aNewSurface = aNewSphere;
124         return Standard_True;
125       }
126       else {
127         gp_Pnt PC = ax3.Location();
128         Standard_Real Vpar;
129         if(fabs(PI2-Vmax)>fabs(-PI2-Vmin))
130           Vpar = (PI2+Vmax)/2.;
131         else
132           Vpar = (-PI2+Vmin)/2.;
133         Standard_Real Upar = (Umin+Umax)/2.;;
134         gp_Pnt PN,PX;
135         S->D0(Upar,Vpar,PN);
136         S->D0(Upar+PI2,0.,PX);
137         gp_Dir newNorm(gp_Vec(PC,PN));
138         gp_Dir newDirX(gp_Vec(PC,PX));
139         gp_Ax3 axnew3(ax3.Axis().Location(), newNorm, newDirX);
140         sp.SetPosition(axnew3);
141         Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
142         aNewSurface = aNewSphere;
143         return Standard_True;
144       }
145     }
146   }
147   return Standard_False;
148 }
149
150 Standard_Boolean BlockFix_SphereSpaceModifier::NewSurface(const TopoDS_Face& F,
151                                                         Handle(Geom_Surface)& S,
152                                                         TopLoc_Location& L,Standard_Real& Tol,
153                                                         Standard_Boolean& RevWires,
154                                                         Standard_Boolean& RevFace)
155 {
156   TopLoc_Location LS;
157   Handle(Geom_Surface) SIni = BRep_Tool::Surface(F, LS);
158
159   //check if pole of the sphere in the parametric space
160   if(ModifySurface(F, SIni, S)) {
161
162     RevWires = Standard_False;
163     RevFace = Standard_False;
164
165     L = LS;
166     Tol = BRep_Tool::Tolerance(F);
167
168     Standard_Integer anIndex = myMapOfSpheres.Add(S);
169     myMapOfFaces.Bind(F,anIndex);
170     return Standard_True;
171   }
172
173   return Standard_False;
174 }
175
176 //=======================================================================
177 //function : NewCurve
178 //purpose  :
179 //=======================================================================
180 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve(const TopoDS_Edge& /*E*/,Handle(Geom_Curve)& /*C*/,
181                                                         TopLoc_Location& /*L*/,Standard_Real& /*Tol*/)
182 {
183   return Standard_False;
184 }
185
186 //=======================================================================
187 //function : NewPoint
188 //purpose  :
189 //=======================================================================
190 Standard_Boolean BlockFix_SphereSpaceModifier::NewPoint(const TopoDS_Vertex& /*V*/,
191                                                       gp_Pnt& /*P*/,
192                                                       Standard_Real& /*Tol*/)
193 {
194   return Standard_False;
195 }
196
197 //=======================================================================
198 //function : NewCurve2d
199 //purpose  :
200 //=======================================================================
201 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve2d(const TopoDS_Edge& E,const TopoDS_Face& F,
202                                                         const TopoDS_Edge& /*NewE*/,const TopoDS_Face& /*NewF*/,
203                                                         Handle(Geom2d_Curve)& C,Standard_Real& Tol)
204 {
205   //check if undelying surface of the face was modified
206   if(myMapOfFaces.IsBound(F)) {
207     Standard_Integer anIndex = myMapOfFaces.Find(F);
208
209     Handle(Geom_Surface) aNewSphere = Handle(Geom_Surface)::DownCast(myMapOfSpheres.FindKey(anIndex));
210
211     Standard_Real f,l;
212     TopLoc_Location LC, LS;
213     Handle(Geom_Curve) C3d = BRep_Tool::Curve ( E, LC, f, l );
214     Handle(Geom_Surface) S = BRep_Tool::Surface(F, LS);
215
216     //taking into account the orientation of the seam
217     C = BRep_Tool::CurveOnSurface(E,F,f,l);
218     Tol = BRep_Tool::Tolerance(E);
219
220     BRep_Builder B;
221     TopoDS_Edge TempE;
222     B.MakeEdge(TempE);
223     B.Add(TempE, TopExp::FirstVertex(E));
224     B.Add(TempE, TopExp::LastVertex(E));
225
226     if(!C3d.IsNull())
227       B.UpdateEdge(TempE, Handle(Geom_Curve)::DownCast(C3d->Transformed(LC.Transformation())), Precision::Confusion());
228     B.Range(TempE, f, l);
229
230     Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
231     Handle(Geom_Surface) STemp = Handle(Geom_Surface)::DownCast(aNewSphere->Transformed(LS.Transformation()));
232     TopLoc_Location LTemp;
233     LTemp.Identity();
234
235     Standard_Boolean isClosed = BRep_Tool::IsClosed (E, F);
236     Standard_Real aWorkTol = 2*myTolerance+Tol;
237     sfe->FixAddPCurve(TempE, STemp, LTemp, isClosed, Max(Precision::Confusion(), aWorkTol));
238     sfe->FixSameParameter(TempE);
239
240     //keep the orientation of original edge
241     TempE.Orientation(E.Orientation());
242     C = BRep_Tool::CurveOnSurface(TempE, STemp, LTemp, f, l);
243
244     // shifting seam of sphere
245     if(isClosed  && !C.IsNull()) {
246       Standard_Real f2,l2;
247       Handle(Geom2d_Curve) c22 =
248         BRep_Tool::CurveOnSurface(TopoDS::Edge(TempE.Reversed()),STemp, LTemp,f2,l2);
249       Standard_Real dPreci = Precision::PConfusion()*Precision::PConfusion();
250       if((C->Value(f).SquareDistance(c22->Value(f2)) < dPreci)
251          ||(C->Value(l).SquareDistance(c22->Value(l2)) < dPreci)) {
252         gp_Vec2d shift(S->UPeriod(),0.);
253         C->Translate(shift);
254       }
255     }
256     //sphere was modified
257     return Standard_True;
258   }
259
260   return Standard_False;
261 }
262
263 //=======================================================================
264 //function : NewParameter
265 //purpose  :
266 //=======================================================================
267 Standard_Boolean BlockFix_SphereSpaceModifier::NewParameter(const TopoDS_Vertex& /*V*/,const TopoDS_Edge& /*E*/,
268                                                             Standard_Real& /*P*/,Standard_Real& /*Tol*/)
269 {
270   return Standard_False;
271 }
272
273 //=======================================================================
274 //function : Continuity
275 //purpose  :
276 //=======================================================================
277 GeomAbs_Shape BlockFix_SphereSpaceModifier::Continuity(const TopoDS_Edge& E,const TopoDS_Face& F1,
278                                                      const TopoDS_Face& F2,const TopoDS_Edge& /*NewE*/,
279                                                      const TopoDS_Face& /*NewF1*/,const TopoDS_Face& /*NewF2*/)
280 {
281   return BRep_Tool::Continuity(E,F1,F2);
282 }