1 // Copyright (C) 2007-2008 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.
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
23 // Created: Tue Dec 7 11:59:05 2004
24 // Author: Pavel DURANDIN
26 #include <BlockFix_SphereSpaceModifier.ixx>
28 #include <TopLoc_Location.hxx>
29 #include <BRep_Tool.hxx>
30 #include <Geom_SphericalSurface.hxx>
31 #include <Geom_RectangularTrimmedSurface.hxx>
32 #include <ShapeAnalysis.hxx>
33 #include <gp_Sphere.hxx>
34 #include <BRep_Builder.hxx>
36 #include <TopoDS_Vertex.hxx>
37 #include <TopoDS_Edge.hxx>
39 #include <ShapeFix_Edge.hxx>
40 #include <Geom_Curve.hxx>
41 #include <Geom2d_Curve.hxx>
44 //=======================================================================
45 //function : BlockFix_SphereSpaceModifier
47 //=======================================================================
49 BlockFix_SphereSpaceModifier::BlockFix_SphereSpaceModifier()
52 myMapOfSpheres.Clear();
55 //=======================================================================
56 //function : SetTolerance
58 //=======================================================================
60 void BlockFix_SphereSpaceModifier::SetTolerance(const Standard_Real Tol)
66 //=======================================================================
67 //function : NewSurface
69 //=======================================================================
72 static Standard_Boolean ModifySurface(const TopoDS_Face& aFace,
73 const Handle(Geom_Surface)& aSurface,
74 Handle(Geom_Surface)& aNewSurface)
76 Handle(Geom_Surface) S = aSurface;
77 if(S->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
78 Handle(Geom_RectangularTrimmedSurface) RTS =
79 Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
80 S = RTS->BasisSurface();
83 if(S->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
84 Standard_Real Umin, Umax, Vmin, Vmax;
85 ShapeAnalysis::GetFaceUVBounds(aFace,Umin, Umax, Vmin, Vmax);
86 Standard_Real PI2 = PI/2.;
87 if(Vmax > PI2 - Precision::PConfusion() || Vmin < -PI2+::Precision::PConfusion()) {
88 Handle(Geom_SphericalSurface) aSphere = Handle(Geom_SphericalSurface)::DownCast(S);
89 gp_Sphere sp = aSphere->Sphere();
90 gp_Ax3 ax3 = sp.Position();
91 if(Abs(Vmax-Vmin) < PI2) {
92 gp_Ax3 axnew3(ax3.Axis().Location(), ax3.Direction()^ax3.XDirection(),ax3.XDirection());
93 sp.SetPosition(axnew3);
94 Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
95 aNewSurface = aNewSphere;
99 gp_Pnt PC = ax3.Location();
101 if(fabs(PI2-Vmax)>fabs(-PI2-Vmin))
102 Vpar = (PI2+Vmax)/2.;
104 Vpar = (-PI2+Vmin)/2.;
105 Standard_Real Upar = (Umin+Umax)/2.;;
108 S->D0(Upar+PI2,0.,PX);
109 gp_Dir newNorm(gp_Vec(PC,PN));
110 gp_Dir newDirX(gp_Vec(PC,PX));
111 gp_Ax3 axnew3(ax3.Axis().Location(), newNorm, newDirX);
112 sp.SetPosition(axnew3);
113 Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
114 aNewSurface = aNewSphere;
115 return Standard_True;
119 return Standard_False;
123 Standard_Boolean BlockFix_SphereSpaceModifier::NewSurface(const TopoDS_Face& F,
124 Handle(Geom_Surface)& S,
125 TopLoc_Location& L,Standard_Real& Tol,
126 Standard_Boolean& RevWires,
127 Standard_Boolean& RevFace)
130 Handle(Geom_Surface) SIni = BRep_Tool::Surface(F, LS);
132 //check if pole of the sphere in the parametric space
133 if(ModifySurface(F, SIni, S)) {
135 RevWires = Standard_False;
136 RevFace = Standard_False;
139 Tol = BRep_Tool::Tolerance(F);
141 Standard_Integer anIndex = myMapOfSpheres.Add(S);
142 myMapOfFaces.Bind(F,anIndex);
143 return Standard_True;
146 return Standard_False;
149 //=======================================================================
150 //function : NewCurve
152 //=======================================================================
154 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve(const TopoDS_Edge& /*E*/,Handle(Geom_Curve)& /*C*/,
155 TopLoc_Location& /*L*/,Standard_Real& /*Tol*/)
157 return Standard_False;
160 //=======================================================================
161 //function : NewPoint
163 //=======================================================================
165 Standard_Boolean BlockFix_SphereSpaceModifier::NewPoint(const TopoDS_Vertex& /*V*/,
167 Standard_Real& /*Tol*/)
169 return Standard_False;
172 //=======================================================================
173 //function : NewCurve2d
175 //=======================================================================
177 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve2d(const TopoDS_Edge& E,const TopoDS_Face& F,
178 const TopoDS_Edge& /*NewE*/,const TopoDS_Face& /*NewF*/,
179 Handle(Geom2d_Curve)& C,Standard_Real& Tol)
181 //check if undelying surface of the face was modified
182 if(myMapOfFaces.IsBound(F)) {
183 Standard_Integer anIndex = myMapOfFaces.Find(F);
185 Handle(Geom_Surface) aNewSphere = Handle(Geom_Surface)::DownCast(myMapOfSpheres.FindKey(anIndex));
188 TopLoc_Location LC, LS;
189 Handle(Geom_Curve) C3d = BRep_Tool::Curve ( E, LC, f, l );
190 Handle(Geom_Surface) S = BRep_Tool::Surface(F, LS);
192 //taking into accound the orientation of the seam
193 C = BRep_Tool::CurveOnSurface(E,F,f,l);
194 Tol = BRep_Tool::Tolerance(E);
199 B.Add(TempE, TopExp::FirstVertex(E));
200 B.Add(TempE, TopExp::LastVertex(E));
203 B.UpdateEdge(TempE, Handle(Geom_Curve)::DownCast(C3d->Transformed(LC.Transformation())), Precision::Confusion());
204 B.Range(TempE, f, l);
206 Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
207 Handle(Geom_Surface) STemp = Handle(Geom_Surface)::DownCast(aNewSphere->Transformed(LS.Transformation()));
208 TopLoc_Location LTemp;
211 Standard_Boolean isClosed = BRep_Tool::IsClosed (E, F);
212 Standard_Real aWorkTol = 2*myTolerance+Tol;
213 sfe->FixAddPCurve(TempE, STemp, LTemp, isClosed, Max(Precision::Confusion(), aWorkTol));
214 sfe->FixSameParameter(TempE);
216 //keep the orientation of original edge
217 TempE.Orientation(E.Orientation());
218 C = BRep_Tool::CurveOnSurface(TempE, STemp, LTemp, f, l);
220 // shifting seam of sphere
221 if(isClosed && !C.IsNull()) {
223 Handle(Geom2d_Curve) c22 =
224 BRep_Tool::CurveOnSurface(TopoDS::Edge(TempE.Reversed()),STemp, LTemp,f2,l2);
225 Standard_Real dPreci = Precision::PConfusion()*Precision::PConfusion();
226 if((C->Value(f).SquareDistance(c22->Value(f2)) < dPreci)
227 ||(C->Value(l).SquareDistance(c22->Value(l2)) < dPreci)) {
228 gp_Vec2d shift(S->UPeriod(),0.);
232 //sphere was modified
233 return Standard_True;
236 return Standard_False;
240 //=======================================================================
241 //function : NewParameter
243 //=======================================================================
245 Standard_Boolean BlockFix_SphereSpaceModifier::NewParameter(const TopoDS_Vertex& /*V*/,const TopoDS_Edge& /*E*/,
246 Standard_Real& /*P*/,Standard_Real& /*Tol*/)
248 return Standard_False;
252 //=======================================================================
253 //function : Continuity
255 //=======================================================================
257 GeomAbs_Shape BlockFix_SphereSpaceModifier::Continuity(const TopoDS_Edge& E,const TopoDS_Face& F1,
258 const TopoDS_Face& F2,const TopoDS_Edge& /*NewE*/,
259 const TopoDS_Face& /*NewF1*/,const TopoDS_Face& /*NewF2*/)
261 return BRep_Tool::Continuity(E,F1,F2);