Salome HOME
Fix compilation error (conflict of OK name between OCCT Plate_Plate.hxx and GEOM...
[modules/geom.git] / src / BlockFix / BlockFix_SphereSpaceModifier.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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 <ShapeFix_Edge.hxx>
30
31 #include <TopExp.hxx>
32
33 #include <TopLoc_Location.hxx>
34
35 #include <TopoDS.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <TopoDS_Vertex.hxx>
39 #include <TopoDS_Iterator.hxx>
40
41 #include <BRepGProp.hxx>
42 #include <GProp_GProps.hxx>
43
44 #include <BRep_Tool.hxx>
45 #include <BRep_Builder.hxx>
46 #include <BRepTools.hxx>
47 #include <BRepAdaptor_Curve2d.hxx>
48 #include <BRepTopAdaptor_FClass2d.hxx>
49 #include <BRepClass_FaceClassifier.hxx>
50
51 #include <ElSLib.hxx>
52 #include <Geom_Circle.hxx>
53 #include <Geom_TrimmedCurve.hxx>
54 #include <Geom_SphericalSurface.hxx>
55 #include <Geom_RectangularTrimmedSurface.hxx>
56 #include <Geom_Curve.hxx>
57 #include <Geom_Surface.hxx>
58
59 #include <Geom2d_Curve.hxx>
60
61 #include <gp_Pnt.hxx>
62 #include <gp_Sphere.hxx>
63
64 IMPLEMENT_STANDARD_RTTIEXT(BlockFix_SphereSpaceModifier, BRepTools_Modification)
65
66 //=======================================================================
67 //function : BlockFix_SphereSpaceModifier
68 //purpose  :
69 //=======================================================================
70 BlockFix_SphereSpaceModifier::BlockFix_SphereSpaceModifier()
71 : myTolerance(Precision::Confusion()),
72   mySmallRotation(Standard_True)
73 {
74   myMapOfFaces.Clear();
75   myMapOfSpheres.Clear();
76 }
77
78 //=======================================================================
79 //function : ~BlockFix_SphereSpaceModifier
80 //purpose  :
81 //=======================================================================
82 BlockFix_SphereSpaceModifier::~BlockFix_SphereSpaceModifier() {}
83
84 //=======================================================================
85 //function : SetTolerance
86 //purpose  :
87 //=======================================================================
88 void BlockFix_SphereSpaceModifier::SetTolerance(const Standard_Real Tol)
89 {
90   myTolerance = Tol;
91 }
92
93 //=======================================================================
94 //function : SetTrySmallRotation
95 //purpose  :
96 //=======================================================================
97 void BlockFix_SphereSpaceModifier::SetTrySmallRotation(const Standard_Boolean isSmallRotation)
98 {
99   mySmallRotation = isSmallRotation;
100 }
101
102 //=======================================================================
103 //function : NewSurface
104 //purpose  :
105 //=======================================================================
106 static Standard_Boolean ModifySurface(const TopoDS_Face&          theFace,
107                                       const Handle(Geom_Surface)& theSurface,
108                                       Handle(Geom_Surface)&       theNewSurface,
109                                       const Standard_Boolean      theTrySmallRotation)
110 {
111   Handle(Geom_Surface) aSurf = theSurface;
112   if (aSurf->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
113     Handle(Geom_RectangularTrimmedSurface) RTS =
114       Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
115     aSurf = RTS->BasisSurface();
116   }
117
118   if (!aSurf->IsKind(STANDARD_TYPE(Geom_SphericalSurface)))
119     return Standard_False;
120
121   Standard_Real PI2 = M_PI/2.;
122   Handle(Geom_SphericalSurface) aSphere = Handle(Geom_SphericalSurface)::DownCast(aSurf);
123   gp_Sphere sp = aSphere->Sphere();
124   Standard_Real Radius = sp.Radius();
125   Standard_Real Umin, Umax, Vmin, Vmax;
126
127   // try with small rotation (old implementation, giving better result in some cases
128   if (theTrySmallRotation) {
129     BRepTools::UVBounds(theFace, Umin, Umax, Vmin, Vmax);
130     if (Vmax > PI2 - Precision::PConfusion() || Vmin < -PI2 + Precision::PConfusion()) {
131       //modified by jgv, 12.11.2012 for issue 21777//
132       Standard_Real HalfArea = 2.*M_PI*Radius*Radius;
133       GProp_GProps Properties;
134       BRepGProp::SurfaceProperties(theFace, Properties);
135       Standard_Real anArea = Properties.Mass();
136       Standard_Real AreaTol = Radius*Radius*1.e-6;
137       if (anArea < HalfArea - AreaTol) { // a chance to avoid singularity
138         gp_Ax3 ax3 = sp.Position();
139         if (Abs(Vmax-Vmin) < PI2) {
140           gp_Ax3 axnew3 (ax3.Axis().Location(), ax3.Direction()^ax3.XDirection(), ax3.XDirection());
141           if (!ax3.Direct()) {
142             axnew3.YReverse();
143           }
144           sp.SetPosition(axnew3);
145           Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
146           theNewSurface = aNewSphere;
147           return Standard_True;
148         }
149         else {
150           gp_Pnt PC = ax3.Location();
151           Standard_Real Vpar;
152           if (fabs(PI2-Vmax) > fabs(-PI2-Vmin))
153             Vpar = (PI2+Vmax)/2.;
154           else
155             Vpar = (-PI2+Vmin)/2.;
156           Standard_Real Upar = (Umin+Umax)/2.;
157           gp_Pnt PN,PX;
158           aSurf->D0(Upar,Vpar,PN);
159           aSurf->D0(Upar+PI2,0.,PX);
160           gp_Dir newNorm(gp_Vec(PC,PN));
161           gp_Dir newDirX(gp_Vec(PC,PX));
162           gp_Ax3 axnew3(ax3.Axis().Location(), newNorm, newDirX);
163           if (!ax3.Direct()) {
164             axnew3.YReverse();
165           }
166           sp.SetPosition(axnew3);
167
168           // check if both new poles are outside theFace
169           gp_Pnt LP; // lowest pole (opposite to PN)
170           aSurf->D0(Upar + M_PI, -Vpar, LP);
171           BRepClass_FaceClassifier aClsf (theFace, LP, Precision::PConfusion());
172           if (aClsf.State() != TopAbs_IN && aClsf.State() != TopAbs_ON) {
173             Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
174             theNewSurface = aNewSphere;
175             return Standard_True;
176           }
177         }
178       }
179     }
180     else {
181       // no rotation needed
182       return Standard_False;
183     }
184   }
185
186   // try with big rotation (new implementation)
187   TopoDS_Face aFace = theFace;
188   aFace.Orientation (TopAbs_FORWARD);
189   BRepTools::UVBounds(aFace, Umin, Umax, Vmin, Vmax);
190
191   gp_Pnt aCentre = sp.Location();
192
193   TopoDS_Wire aWire = BRepTools::OuterWire (aFace);
194   BRepTopAdaptor_FClass2d aClassifier (aFace, Precision::PConfusion());
195   TopTools_MapOfShape aEmap;
196   const Standard_Real anOffsetValue = 0.01*M_PI;
197   for (Standard_Integer ii = 1; ii <= 2; ii++) {
198     TopoDS_Iterator itw (aWire);
199     for (; itw.More(); itw.Next()) {
200       const TopoDS_Edge& anEdge = TopoDS::Edge (itw.Value());
201       if (aEmap.Contains (anEdge) ||
202           anEdge.Orientation() == TopAbs_INTERNAL ||
203           anEdge.Orientation() == TopAbs_EXTERNAL ||
204           BRep_Tool::Degenerated (anEdge) ||
205           BRepTools::IsReallyClosed (anEdge, aFace))
206         continue;
207
208       BRepAdaptor_Curve2d aBAcurve2d (anEdge, aFace);
209       GeomAbs_CurveType aType = aBAcurve2d.GetType();
210       if (ii == 1 && aType == GeomAbs_Line) //first pass: consider only curvilinear edges
211         continue;
212
213       Standard_Real aMidPar = (aBAcurve2d.FirstParameter() + aBAcurve2d.LastParameter())/2;
214       gp_Pnt2d aMidP2d;
215       gp_Vec2d aTangent;
216       aBAcurve2d.D1 (aMidPar, aMidP2d, aTangent);
217       if (anEdge.Orientation() == TopAbs_REVERSED)
218         aTangent.Reverse();
219
220       aTangent.Normalize();
221       gp_Vec2d aNormal (aTangent.Y(), -aTangent.X());
222       aNormal *= anOffsetValue;
223       gp_Pnt2d anUpperPole = aMidP2d.Translated (aNormal);
224       if (anUpperPole.Y() < -PI2 || anUpperPole.Y() > PI2) {
225         aEmap.Add(anEdge);
226         continue;
227       }
228       if (anUpperPole.X() < 0.)
229         anUpperPole.SetX (anUpperPole.X() + 2.*M_PI);
230       else if (anUpperPole.X() > 2.*M_PI)
231         anUpperPole.SetX (anUpperPole.X() - 2.*M_PI);
232
233       TopAbs_State aStatus = aClassifier.Perform (anUpperPole);
234       if (aStatus != TopAbs_OUT) {
235         aEmap.Add(anEdge);
236         continue;
237       }
238
239       gp_Pnt anUpperPole3d = aSphere->Value (anUpperPole.X(), anUpperPole.Y());
240       gp_Vec aVec (aCentre, anUpperPole3d);
241       aVec.Reverse();
242       gp_Pnt aLowerPole3d = aCentre.Translated (aVec);
243       Standard_Real aU, aV;
244       ElSLib::Parameters (sp, aLowerPole3d, aU, aV);
245       gp_Pnt2d aLowerPole (aU, aV);
246       aStatus = aClassifier.Perform (aLowerPole);
247       if (aStatus != TopAbs_OUT) {
248         aEmap.Add(anEdge);
249         continue;
250       }
251
252       //Build a meridian
253       gp_Vec anUp (aCentre, anUpperPole3d);
254       anUp.Normalize();
255       gp_Pnt aMidPnt = aSphere->Value (aMidP2d.X(), aMidP2d.Y());
256       gp_Vec aMidOnEdge (aCentre, aMidPnt);
257       aMidOnEdge.Normalize();
258       gp_Vec AxisOfCircle = anUp ^ aMidOnEdge;
259       gp_Vec XDirOfCircle = anUp ^ AxisOfCircle;
260       gp_Ax2 anAxis (aCentre, AxisOfCircle, XDirOfCircle);
261       Handle(Geom_Circle) aCircle = new Geom_Circle (anAxis, Radius);
262       Handle(Geom_TrimmedCurve) aMeridian = new Geom_TrimmedCurve (aCircle, -PI2, PI2);
263
264       //Check the meridian
265       Standard_Boolean IsInnerPointFound = Standard_False;
266       Standard_Integer NbSamples = 10;
267       Standard_Real aDelta = M_PI / NbSamples;
268       for (Standard_Integer jj = 1; jj < NbSamples; jj++) {
269         Standard_Real aParam = -PI2 + jj*aDelta;
270         gp_Pnt aPnt = aMeridian->Value (aParam);
271         ElSLib::Parameters (sp, aPnt, aU, aV);
272         gp_Pnt2d aP2d (aU, aV);
273         aStatus = aClassifier.Perform (aP2d);
274         if (aStatus != TopAbs_OUT) {
275           IsInnerPointFound = Standard_True;
276           break;
277         }
278       }
279       if (IsInnerPointFound) {
280         aEmap.Add(anEdge);
281         continue;
282       }
283
284       gp_Ax3 anAxisOfNewSphere (aCentre, anUp, XDirOfCircle);
285       gp_Ax3 ax3 = sp.Position();
286       if (!ax3.Direct()) {
287         anAxisOfNewSphere.YReverse();
288       }
289       theNewSurface = new Geom_SphericalSurface (anAxisOfNewSphere, Radius);
290       break;
291     } //for (; itw.More(); itw.Next()) (iteration on outer wire)
292     if (!theNewSurface.IsNull())
293       break;
294   } //for (Standard_Integer ii = 1; ii <= 2; ii++) (two passes)
295
296   return (!theNewSurface.IsNull());
297 }
298
299 Standard_Boolean BlockFix_SphereSpaceModifier::NewSurface(const TopoDS_Face& F,
300                                                           Handle(Geom_Surface)& S,
301                                                           TopLoc_Location& L,
302                                                           Standard_Real& Tol,
303                                                           Standard_Boolean& RevWires,
304                                                           Standard_Boolean& RevFace)
305 {
306   TopLoc_Location LS;
307   Handle(Geom_Surface) SIni = BRep_Tool::Surface(F, LS);
308
309   //check if pole of the sphere in the parametric space
310   if (ModifySurface(F, SIni, S, mySmallRotation)) {
311
312     RevWires = Standard_False;
313     RevFace = Standard_False;
314
315     L = LS;
316     Tol = BRep_Tool::Tolerance(F);
317
318     Standard_Integer anIndex = myMapOfSpheres.Add(S);
319     myMapOfFaces.Bind(F,anIndex);
320     return Standard_True;
321   }
322
323   return Standard_False;
324 }
325
326 //=======================================================================
327 //function : NewCurve
328 //purpose  :
329 //=======================================================================
330 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve(const TopoDS_Edge& /*E*/,Handle(Geom_Curve)& /*C*/,
331                                                         TopLoc_Location& /*L*/,Standard_Real& /*Tol*/)
332 {
333   return Standard_False;
334 }
335
336 //=======================================================================
337 //function : NewPoint
338 //purpose  :
339 //=======================================================================
340 Standard_Boolean BlockFix_SphereSpaceModifier::NewPoint(const TopoDS_Vertex& /*V*/,
341                                                       gp_Pnt& /*P*/,
342                                                       Standard_Real& /*Tol*/)
343 {
344   return Standard_False;
345 }
346
347 //=======================================================================
348 //function : NewCurve2d
349 //purpose  :
350 //=======================================================================
351 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve2d(const TopoDS_Edge& E,const TopoDS_Face& F,
352                                                         const TopoDS_Edge& /*NewE*/,const TopoDS_Face& /*NewF*/,
353                                                         Handle(Geom2d_Curve)& C,Standard_Real& Tol)
354 {
355   //check if undelying surface of the face was modified
356   if(myMapOfFaces.IsBound(F)) {
357     Standard_Integer anIndex = myMapOfFaces.Find(F);
358
359     Handle(Geom_Surface) aNewSphere = Handle(Geom_Surface)::DownCast(myMapOfSpheres.FindKey(anIndex));
360
361     Standard_Real f,l;
362     TopLoc_Location LC, LS;
363     Handle(Geom_Curve) C3d = BRep_Tool::Curve ( E, LC, f, l );
364     Handle(Geom_Surface) S = BRep_Tool::Surface(F, LS);
365
366     //taking into account the orientation of the seam
367     C = BRep_Tool::CurveOnSurface(E,F,f,l);
368     Tol = BRep_Tool::Tolerance(E);
369
370     BRep_Builder B;
371     TopoDS_Edge TempE;
372     B.MakeEdge(TempE);
373     B.Add(TempE, TopExp::FirstVertex(E));
374     B.Add(TempE, TopExp::LastVertex(E));
375
376     if(!C3d.IsNull())
377       B.UpdateEdge(TempE, Handle(Geom_Curve)::DownCast(C3d->Transformed(LC.Transformation())), Precision::Confusion());
378     B.Range(TempE, f, l);
379
380     Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
381     Handle(Geom_Surface) STemp = Handle(Geom_Surface)::DownCast(aNewSphere->Transformed(LS.Transformation()));
382     TopLoc_Location LTemp;
383     LTemp.Identity();
384
385     Standard_Boolean isClosed = BRep_Tool::IsClosed (E, F);
386     Standard_Real aWorkTol = 2*myTolerance+Tol;
387     sfe->FixAddPCurve(TempE, STemp, LTemp, isClosed, Max(Precision::Confusion(), aWorkTol));
388     sfe->FixSameParameter(TempE);
389
390     //keep the orientation of original edge
391     TempE.Orientation(E.Orientation());
392     C = BRep_Tool::CurveOnSurface(TempE, STemp, LTemp, f, l);
393
394     // shifting seam of sphere
395     if(isClosed  && !C.IsNull()) {
396       Standard_Real f2,l2;
397       Handle(Geom2d_Curve) c22 =
398         BRep_Tool::CurveOnSurface(TopoDS::Edge(TempE.Reversed()),STemp, LTemp,f2,l2);
399       Standard_Real dPreci = Precision::PConfusion()*Precision::PConfusion();
400       if((C->Value(f).SquareDistance(c22->Value(f2)) < dPreci)
401          ||(C->Value(l).SquareDistance(c22->Value(l2)) < dPreci)) {
402         gp_Vec2d shift(S->UPeriod(),0.);
403         C->Translate(shift);
404       }
405     }
406     //sphere was modified
407     return Standard_True;
408   }
409
410   return Standard_False;
411 }
412
413 //=======================================================================
414 //function : NewParameter
415 //purpose  :
416 //=======================================================================
417 Standard_Boolean BlockFix_SphereSpaceModifier::NewParameter(const TopoDS_Vertex& /*V*/,const TopoDS_Edge& /*E*/,
418                                                             Standard_Real& /*P*/,Standard_Real& /*Tol*/)
419 {
420   return Standard_False;
421 }
422
423 //=======================================================================
424 //function : Continuity
425 //purpose  :
426 //=======================================================================
427 GeomAbs_Shape BlockFix_SphereSpaceModifier::Continuity(const TopoDS_Edge& E,const TopoDS_Face& F1,
428                                                      const TopoDS_Face& F2,const TopoDS_Edge& /*NewE*/,
429                                                      const TopoDS_Face& /*NewF1*/,const TopoDS_Face& /*NewF2*/)
430 {
431   return BRep_Tool::Continuity(E,F1,F2);
432 }