1 // Copyright (C) 2007-2024 CEA, EDF, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // Created: Tue Dec 7 11:59:05 2004
25 // Author: Pavel DURANDIN
27 #include <BlockFix_SphereSpaceModifier.hxx>
29 #include <ShapeFix_Edge.hxx>
33 #include <TopLoc_Location.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <TopoDS_Vertex.hxx>
39 #include <TopoDS_Iterator.hxx>
41 #include <BRepGProp.hxx>
42 #include <GProp_GProps.hxx>
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>
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>
59 #include <Geom2d_Curve.hxx>
62 #include <gp_Sphere.hxx>
64 IMPLEMENT_STANDARD_RTTIEXT(BlockFix_SphereSpaceModifier, BRepTools_Modification)
66 //=======================================================================
67 //function : BlockFix_SphereSpaceModifier
69 //=======================================================================
70 BlockFix_SphereSpaceModifier::BlockFix_SphereSpaceModifier()
71 : myTolerance(Precision::Confusion()),
72 mySmallRotation(Standard_True)
75 myMapOfSpheres.Clear();
78 //=======================================================================
79 //function : ~BlockFix_SphereSpaceModifier
81 //=======================================================================
82 BlockFix_SphereSpaceModifier::~BlockFix_SphereSpaceModifier() {}
84 //=======================================================================
85 //function : SetTolerance
87 //=======================================================================
88 void BlockFix_SphereSpaceModifier::SetTolerance(const Standard_Real Tol)
93 //=======================================================================
94 //function : SetTrySmallRotation
96 //=======================================================================
97 void BlockFix_SphereSpaceModifier::SetTrySmallRotation(const Standard_Boolean isSmallRotation)
99 mySmallRotation = isSmallRotation;
102 //=======================================================================
103 //function : NewSurface
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)
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();
118 if (!aSurf->IsKind(STANDARD_TYPE(Geom_SphericalSurface)))
119 return Standard_False;
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;
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());
144 sp.SetPosition(axnew3);
145 Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
146 theNewSurface = aNewSphere;
147 return Standard_True;
150 gp_Pnt PC = ax3.Location();
152 if (fabs(PI2-Vmax) > fabs(-PI2-Vmin))
153 Vpar = (PI2+Vmax)/2.;
155 Vpar = (-PI2+Vmin)/2.;
156 Standard_Real Upar = (Umin+Umax)/2.;
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);
166 sp.SetPosition(axnew3);
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;
181 // no rotation needed
182 return Standard_False;
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);
191 gp_Pnt aCentre = sp.Location();
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))
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
213 Standard_Real aMidPar = (aBAcurve2d.FirstParameter() + aBAcurve2d.LastParameter())/2;
216 aBAcurve2d.D1 (aMidPar, aMidP2d, aTangent);
217 if (anEdge.Orientation() == TopAbs_REVERSED)
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) {
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);
233 TopAbs_State aStatus = aClassifier.Perform (anUpperPole);
234 if (aStatus != TopAbs_OUT) {
239 gp_Pnt anUpperPole3d = aSphere->Value (anUpperPole.X(), anUpperPole.Y());
240 gp_Vec aVec (aCentre, anUpperPole3d);
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) {
253 gp_Vec anUp (aCentre, anUpperPole3d);
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);
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;
279 if (IsInnerPointFound) {
284 gp_Ax3 anAxisOfNewSphere (aCentre, anUp, XDirOfCircle);
285 gp_Ax3 ax3 = sp.Position();
287 anAxisOfNewSphere.YReverse();
289 theNewSurface = new Geom_SphericalSurface (anAxisOfNewSphere, Radius);
291 } //for (; itw.More(); itw.Next()) (iteration on outer wire)
292 if (!theNewSurface.IsNull())
294 } //for (Standard_Integer ii = 1; ii <= 2; ii++) (two passes)
296 return (!theNewSurface.IsNull());
299 Standard_Boolean BlockFix_SphereSpaceModifier::NewSurface(const TopoDS_Face& F,
300 Handle(Geom_Surface)& S,
303 Standard_Boolean& RevWires,
304 Standard_Boolean& RevFace)
307 Handle(Geom_Surface) SIni = BRep_Tool::Surface(F, LS);
309 //check if pole of the sphere in the parametric space
310 if (ModifySurface(F, SIni, S, mySmallRotation)) {
312 RevWires = Standard_False;
313 RevFace = Standard_False;
316 Tol = BRep_Tool::Tolerance(F);
318 Standard_Integer anIndex = myMapOfSpheres.Add(S);
319 myMapOfFaces.Bind(F,anIndex);
320 return Standard_True;
323 return Standard_False;
326 //=======================================================================
327 //function : NewCurve
329 //=======================================================================
330 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve(const TopoDS_Edge& /*E*/,Handle(Geom_Curve)& /*C*/,
331 TopLoc_Location& /*L*/,Standard_Real& /*Tol*/)
333 return Standard_False;
336 //=======================================================================
337 //function : NewPoint
339 //=======================================================================
340 Standard_Boolean BlockFix_SphereSpaceModifier::NewPoint(const TopoDS_Vertex& /*V*/,
342 Standard_Real& /*Tol*/)
344 return Standard_False;
347 //=======================================================================
348 //function : NewCurve2d
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)
355 //check if undelying surface of the face was modified
356 if(myMapOfFaces.IsBound(F)) {
357 Standard_Integer anIndex = myMapOfFaces.Find(F);
359 Handle(Geom_Surface) aNewSphere = Handle(Geom_Surface)::DownCast(myMapOfSpheres.FindKey(anIndex));
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);
366 //taking into account the orientation of the seam
367 C = BRep_Tool::CurveOnSurface(E,F,f,l);
368 Tol = BRep_Tool::Tolerance(E);
373 B.Add(TempE, TopExp::FirstVertex(E));
374 B.Add(TempE, TopExp::LastVertex(E));
377 B.UpdateEdge(TempE, Handle(Geom_Curve)::DownCast(C3d->Transformed(LC.Transformation())), Precision::Confusion());
378 B.Range(TempE, f, l);
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;
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);
390 //keep the orientation of original edge
391 TempE.Orientation(E.Orientation());
392 C = BRep_Tool::CurveOnSurface(TempE, STemp, LTemp, f, l);
394 // shifting seam of sphere
395 if(isClosed && !C.IsNull()) {
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.);
406 //sphere was modified
407 return Standard_True;
410 return Standard_False;
413 //=======================================================================
414 //function : NewParameter
416 //=======================================================================
417 Standard_Boolean BlockFix_SphereSpaceModifier::NewParameter(const TopoDS_Vertex& /*V*/,const TopoDS_Edge& /*E*/,
418 Standard_Real& /*P*/,Standard_Real& /*Tol*/)
420 return Standard_False;
423 //=======================================================================
424 //function : Continuity
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*/)
431 return BRep_Tool::Continuity(E,F1,F2);