1 // Copyright (C) 2007-2010 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
24 // Created: Tue Dec 7 11:59:05 2004
25 // Author: Pavel DURANDIN
27 #include <BlockFix_SphereSpaceModifier.ixx>
29 #include <TopLoc_Location.hxx>
30 #include <BRep_Tool.hxx>
31 #include <Geom_SphericalSurface.hxx>
32 #include <Geom_RectangularTrimmedSurface.hxx>
33 #include <ShapeAnalysis.hxx>
34 #include <gp_Sphere.hxx>
35 #include <BRep_Builder.hxx>
37 #include <TopoDS_Vertex.hxx>
38 #include <TopoDS_Edge.hxx>
40 #include <ShapeFix_Edge.hxx>
41 #include <Geom_Curve.hxx>
42 #include <Geom2d_Curve.hxx>
45 //=======================================================================
46 //function : BlockFix_SphereSpaceModifier
48 //=======================================================================
50 BlockFix_SphereSpaceModifier::BlockFix_SphereSpaceModifier()
53 myMapOfSpheres.Clear();
56 //=======================================================================
57 //function : SetTolerance
59 //=======================================================================
61 void BlockFix_SphereSpaceModifier::SetTolerance(const Standard_Real Tol)
67 //=======================================================================
68 //function : NewSurface
70 //=======================================================================
73 static Standard_Boolean ModifySurface(const TopoDS_Face& aFace,
74 const Handle(Geom_Surface)& aSurface,
75 Handle(Geom_Surface)& aNewSurface)
77 Handle(Geom_Surface) S = aSurface;
78 if(S->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
79 Handle(Geom_RectangularTrimmedSurface) RTS =
80 Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
81 S = RTS->BasisSurface();
84 if(S->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
85 Standard_Real Umin, Umax, Vmin, Vmax;
86 ShapeAnalysis::GetFaceUVBounds(aFace,Umin, Umax, Vmin, Vmax);
87 Standard_Real PI2 = PI/2.;
88 if(Vmax > PI2 - Precision::PConfusion() || Vmin < -PI2+::Precision::PConfusion()) {
89 Handle(Geom_SphericalSurface) aSphere = Handle(Geom_SphericalSurface)::DownCast(S);
90 gp_Sphere sp = aSphere->Sphere();
91 gp_Ax3 ax3 = sp.Position();
92 if(Abs(Vmax-Vmin) < PI2) {
93 gp_Ax3 axnew3(ax3.Axis().Location(), ax3.Direction()^ax3.XDirection(),ax3.XDirection());
94 sp.SetPosition(axnew3);
95 Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
96 aNewSurface = aNewSphere;
100 gp_Pnt PC = ax3.Location();
102 if(fabs(PI2-Vmax)>fabs(-PI2-Vmin))
103 Vpar = (PI2+Vmax)/2.;
105 Vpar = (-PI2+Vmin)/2.;
106 Standard_Real Upar = (Umin+Umax)/2.;;
109 S->D0(Upar+PI2,0.,PX);
110 gp_Dir newNorm(gp_Vec(PC,PN));
111 gp_Dir newDirX(gp_Vec(PC,PX));
112 gp_Ax3 axnew3(ax3.Axis().Location(), newNorm, newDirX);
113 sp.SetPosition(axnew3);
114 Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
115 aNewSurface = aNewSphere;
116 return Standard_True;
120 return Standard_False;
124 Standard_Boolean BlockFix_SphereSpaceModifier::NewSurface(const TopoDS_Face& F,
125 Handle(Geom_Surface)& S,
126 TopLoc_Location& L,Standard_Real& Tol,
127 Standard_Boolean& RevWires,
128 Standard_Boolean& RevFace)
131 Handle(Geom_Surface) SIni = BRep_Tool::Surface(F, LS);
133 //check if pole of the sphere in the parametric space
134 if(ModifySurface(F, SIni, S)) {
136 RevWires = Standard_False;
137 RevFace = Standard_False;
140 Tol = BRep_Tool::Tolerance(F);
142 Standard_Integer anIndex = myMapOfSpheres.Add(S);
143 myMapOfFaces.Bind(F,anIndex);
144 return Standard_True;
147 return Standard_False;
150 //=======================================================================
151 //function : NewCurve
153 //=======================================================================
155 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve(const TopoDS_Edge& /*E*/,Handle(Geom_Curve)& /*C*/,
156 TopLoc_Location& /*L*/,Standard_Real& /*Tol*/)
158 return Standard_False;
161 //=======================================================================
162 //function : NewPoint
164 //=======================================================================
166 Standard_Boolean BlockFix_SphereSpaceModifier::NewPoint(const TopoDS_Vertex& /*V*/,
168 Standard_Real& /*Tol*/)
170 return Standard_False;
173 //=======================================================================
174 //function : NewCurve2d
176 //=======================================================================
178 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve2d(const TopoDS_Edge& E,const TopoDS_Face& F,
179 const TopoDS_Edge& /*NewE*/,const TopoDS_Face& /*NewF*/,
180 Handle(Geom2d_Curve)& C,Standard_Real& Tol)
182 //check if undelying surface of the face was modified
183 if(myMapOfFaces.IsBound(F)) {
184 Standard_Integer anIndex = myMapOfFaces.Find(F);
186 Handle(Geom_Surface) aNewSphere = Handle(Geom_Surface)::DownCast(myMapOfSpheres.FindKey(anIndex));
189 TopLoc_Location LC, LS;
190 Handle(Geom_Curve) C3d = BRep_Tool::Curve ( E, LC, f, l );
191 Handle(Geom_Surface) S = BRep_Tool::Surface(F, LS);
193 //taking into accound the orientation of the seam
194 C = BRep_Tool::CurveOnSurface(E,F,f,l);
195 Tol = BRep_Tool::Tolerance(E);
200 B.Add(TempE, TopExp::FirstVertex(E));
201 B.Add(TempE, TopExp::LastVertex(E));
204 B.UpdateEdge(TempE, Handle(Geom_Curve)::DownCast(C3d->Transformed(LC.Transformation())), Precision::Confusion());
205 B.Range(TempE, f, l);
207 Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
208 Handle(Geom_Surface) STemp = Handle(Geom_Surface)::DownCast(aNewSphere->Transformed(LS.Transformation()));
209 TopLoc_Location LTemp;
212 Standard_Boolean isClosed = BRep_Tool::IsClosed (E, F);
213 Standard_Real aWorkTol = 2*myTolerance+Tol;
214 sfe->FixAddPCurve(TempE, STemp, LTemp, isClosed, Max(Precision::Confusion(), aWorkTol));
215 sfe->FixSameParameter(TempE);
217 //keep the orientation of original edge
218 TempE.Orientation(E.Orientation());
219 C = BRep_Tool::CurveOnSurface(TempE, STemp, LTemp, f, l);
221 // shifting seam of sphere
222 if(isClosed && !C.IsNull()) {
224 Handle(Geom2d_Curve) c22 =
225 BRep_Tool::CurveOnSurface(TopoDS::Edge(TempE.Reversed()),STemp, LTemp,f2,l2);
226 Standard_Real dPreci = Precision::PConfusion()*Precision::PConfusion();
227 if((C->Value(f).SquareDistance(c22->Value(f2)) < dPreci)
228 ||(C->Value(l).SquareDistance(c22->Value(l2)) < dPreci)) {
229 gp_Vec2d shift(S->UPeriod(),0.);
233 //sphere was modified
234 return Standard_True;
237 return Standard_False;
241 //=======================================================================
242 //function : NewParameter
244 //=======================================================================
246 Standard_Boolean BlockFix_SphereSpaceModifier::NewParameter(const TopoDS_Vertex& /*V*/,const TopoDS_Edge& /*E*/,
247 Standard_Real& /*P*/,Standard_Real& /*Tol*/)
249 return Standard_False;
253 //=======================================================================
254 //function : Continuity
256 //=======================================================================
258 GeomAbs_Shape BlockFix_SphereSpaceModifier::Continuity(const TopoDS_Edge& E,const TopoDS_Face& F1,
259 const TopoDS_Face& F2,const TopoDS_Edge& /*NewE*/,
260 const TopoDS_Face& /*NewF1*/,const TopoDS_Face& /*NewF2*/)
262 return BRep_Tool::Continuity(E,F1,F2);