1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, 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 <ShapeAnalysis.hxx>
31 #include <ShapeFix_Edge.hxx>
35 #include <TopLoc_Location.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Face.hxx>
40 #include <TopoDS_Vertex.hxx>
42 #include <BRep_Tool.hxx>
43 #include <BRep_Builder.hxx>
45 #include <BRepGProp.hxx>
46 #include <GProp_GProps.hxx>
48 #include <Geom_SphericalSurface.hxx>
49 #include <Geom_RectangularTrimmedSurface.hxx>
51 #include <Geom_Curve.hxx>
52 #include <Geom_Surface.hxx>
54 #include <Geom2d_Curve.hxx>
57 #include <gp_Sphere.hxx>
59 IMPLEMENT_STANDARD_RTTIEXT(BlockFix_SphereSpaceModifier, BRepTools_Modification);
61 //=======================================================================
62 //function : BlockFix_SphereSpaceModifier
64 //=======================================================================
65 BlockFix_SphereSpaceModifier::BlockFix_SphereSpaceModifier()
68 myMapOfSpheres.Clear();
71 //=======================================================================
72 //function : ~BlockFix_SphereSpaceModifier
74 //=======================================================================
75 BlockFix_SphereSpaceModifier::~BlockFix_SphereSpaceModifier() {}
77 //=======================================================================
78 //function : SetTolerance
80 //=======================================================================
81 void BlockFix_SphereSpaceModifier::SetTolerance(const Standard_Real Tol)
86 //=======================================================================
87 //function : NewSurface
89 //=======================================================================
90 static Standard_Boolean ModifySurface(const TopoDS_Face& aFace,
91 const Handle(Geom_Surface)& aSurface,
92 Handle(Geom_Surface)& aNewSurface)
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();
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;
127 gp_Pnt PC = ax3.Location();
129 if(fabs(PI2-Vmax)>fabs(-PI2-Vmin))
130 Vpar = (PI2+Vmax)/2.;
132 Vpar = (-PI2+Vmin)/2.;
133 Standard_Real Upar = (Umin+Umax)/2.;;
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;
147 return Standard_False;
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)
157 Handle(Geom_Surface) SIni = BRep_Tool::Surface(F, LS);
159 //check if pole of the sphere in the parametric space
160 if(ModifySurface(F, SIni, S)) {
162 RevWires = Standard_False;
163 RevFace = Standard_False;
166 Tol = BRep_Tool::Tolerance(F);
168 Standard_Integer anIndex = myMapOfSpheres.Add(S);
169 myMapOfFaces.Bind(F,anIndex);
170 return Standard_True;
173 return Standard_False;
176 //=======================================================================
177 //function : NewCurve
179 //=======================================================================
180 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve(const TopoDS_Edge& /*E*/,Handle(Geom_Curve)& /*C*/,
181 TopLoc_Location& /*L*/,Standard_Real& /*Tol*/)
183 return Standard_False;
186 //=======================================================================
187 //function : NewPoint
189 //=======================================================================
190 Standard_Boolean BlockFix_SphereSpaceModifier::NewPoint(const TopoDS_Vertex& /*V*/,
192 Standard_Real& /*Tol*/)
194 return Standard_False;
197 //=======================================================================
198 //function : NewCurve2d
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)
205 //check if undelying surface of the face was modified
206 if(myMapOfFaces.IsBound(F)) {
207 Standard_Integer anIndex = myMapOfFaces.Find(F);
209 Handle(Geom_Surface) aNewSphere = Handle(Geom_Surface)::DownCast(myMapOfSpheres.FindKey(anIndex));
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);
216 //taking into accound the orientation of the seam
217 C = BRep_Tool::CurveOnSurface(E,F,f,l);
218 Tol = BRep_Tool::Tolerance(E);
223 B.Add(TempE, TopExp::FirstVertex(E));
224 B.Add(TempE, TopExp::LastVertex(E));
227 B.UpdateEdge(TempE, Handle(Geom_Curve)::DownCast(C3d->Transformed(LC.Transformation())), Precision::Confusion());
228 B.Range(TempE, f, l);
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;
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);
240 //keep the orientation of original edge
241 TempE.Orientation(E.Orientation());
242 C = BRep_Tool::CurveOnSurface(TempE, STemp, LTemp, f, l);
244 // shifting seam of sphere
245 if(isClosed && !C.IsNull()) {
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.);
256 //sphere was modified
257 return Standard_True;
260 return Standard_False;
263 //=======================================================================
264 //function : NewParameter
266 //=======================================================================
267 Standard_Boolean BlockFix_SphereSpaceModifier::NewParameter(const TopoDS_Vertex& /*V*/,const TopoDS_Edge& /*E*/,
268 Standard_Real& /*P*/,Standard_Real& /*Tol*/)
270 return Standard_False;
273 //=======================================================================
274 //function : Continuity
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*/)
281 return BRep_Tool::Continuity(E,F1,F2);