1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 // Created: Tue Dec 7 11:59:05 2004
22 // Author: Pavel DURANDIN
23 // Copyright: Open CASCADE SA 2004
25 #include <BlockFix_SphereSpaceModifier.ixx>
27 #include <TopLoc_Location.hxx>
28 #include <BRep_Tool.hxx>
29 #include <Geom_SphericalSurface.hxx>
30 #include <Geom_RectangularTrimmedSurface.hxx>
31 #include <ShapeAnalysis.hxx>
32 #include <gp_Sphere.hxx>
33 #include <BRep_Builder.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <TopoDS_Edge.hxx>
38 #include <ShapeFix_Edge.hxx>
39 #include <Geom_Curve.hxx>
40 #include <Geom2d_Curve.hxx>
43 //=======================================================================
44 //function : BlockFix_SphereSpaceModifier
46 //=======================================================================
48 BlockFix_SphereSpaceModifier::BlockFix_SphereSpaceModifier()
51 myMapOfSpheres.Clear();
54 //=======================================================================
55 //function : SetTolerance
57 //=======================================================================
59 void BlockFix_SphereSpaceModifier::SetTolerance(const Standard_Real Tol)
65 //=======================================================================
66 //function : NewSurface
68 //=======================================================================
71 static Standard_Boolean ModifySurface(const TopoDS_Face& aFace,
72 const Handle(Geom_Surface)& aSurface,
73 Handle(Geom_Surface)& aNewSurface)
75 Handle(Geom_Surface) S = aSurface;
76 if(S->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
77 Handle(Geom_RectangularTrimmedSurface) RTS =
78 Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
79 S = RTS->BasisSurface();
82 if(S->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
83 Standard_Real Umin, Umax, Vmin, Vmax;
84 ShapeAnalysis::GetFaceUVBounds(aFace,Umin, Umax, Vmin, Vmax);
85 Standard_Real PI2 = PI/2.;
86 if(Vmax > PI2 - Precision::PConfusion() || Vmin < -PI2+::Precision::PConfusion()) {
87 Handle(Geom_SphericalSurface) aSphere = Handle(Geom_SphericalSurface)::DownCast(S);
88 gp_Sphere sp = aSphere->Sphere();
89 gp_Ax3 ax3 = sp.Position();
90 if(Abs(Vmax-Vmin) < PI2) {
91 gp_Ax3 axnew3(ax3.Axis().Location(), ax3.Direction()^ax3.XDirection(),ax3.XDirection());
92 sp.SetPosition(axnew3);
93 Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
94 aNewSurface = aNewSphere;
98 gp_Pnt PC = ax3.Location();
100 if(fabs(PI2-Vmax)>fabs(-PI2-Vmin))
101 Vpar = (PI2+Vmax)/2.;
103 Vpar = (-PI2+Vmin)/2.;
104 Standard_Real Upar = (Umin+Umax)/2.;;
107 S->D0(Upar+PI2,0.,PX);
108 gp_Dir newNorm(gp_Vec(PC,PN));
109 gp_Dir newDirX(gp_Vec(PC,PX));
110 gp_Ax3 axnew3(ax3.Axis().Location(), newNorm, newDirX);
111 sp.SetPosition(axnew3);
112 Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
113 aNewSurface = aNewSphere;
114 return Standard_True;
118 return Standard_False;
122 Standard_Boolean BlockFix_SphereSpaceModifier::NewSurface(const TopoDS_Face& F,
123 Handle(Geom_Surface)& S,
124 TopLoc_Location& L,Standard_Real& Tol,
125 Standard_Boolean& RevWires,
126 Standard_Boolean& RevFace)
129 Handle(Geom_Surface) SIni = BRep_Tool::Surface(F, LS);
131 //check if pole of the sphere in the parametric space
132 if(ModifySurface(F, SIni, S)) {
134 RevWires = Standard_False;
135 RevFace = Standard_False;
138 Tol = BRep_Tool::Tolerance(F);
140 Standard_Integer anIndex = myMapOfSpheres.Add(S);
141 myMapOfFaces.Bind(F,anIndex);
142 return Standard_True;
145 return Standard_False;
148 //=======================================================================
149 //function : NewCurve
151 //=======================================================================
153 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve(const TopoDS_Edge& /*E*/,Handle(Geom_Curve)& /*C*/,
154 TopLoc_Location& /*L*/,Standard_Real& /*Tol*/)
156 return Standard_False;
159 //=======================================================================
160 //function : NewPoint
162 //=======================================================================
164 Standard_Boolean BlockFix_SphereSpaceModifier::NewPoint(const TopoDS_Vertex& /*V*/,
166 Standard_Real& /*Tol*/)
168 return Standard_False;
171 //=======================================================================
172 //function : NewCurve2d
174 //=======================================================================
176 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve2d(const TopoDS_Edge& E,const TopoDS_Face& F,
177 const TopoDS_Edge& /*NewE*/,const TopoDS_Face& /*NewF*/,
178 Handle(Geom2d_Curve)& C,Standard_Real& Tol)
180 //check if undelying surface of the face was modified
181 if(myMapOfFaces.IsBound(F)) {
182 Standard_Integer anIndex = myMapOfFaces.Find(F);
184 Handle(Geom_Surface) aNewSphere = Handle(Geom_Surface)::DownCast(myMapOfSpheres.FindKey(anIndex));
187 TopLoc_Location LC, LS;
188 Handle(Geom_Curve) C3d = BRep_Tool::Curve ( E, LC, f, l );
189 Handle(Geom_Surface) S = BRep_Tool::Surface(F, LS);
191 //taking into accound the orientation of the seam
192 C = BRep_Tool::CurveOnSurface(E,F,f,l);
193 Tol = BRep_Tool::Tolerance(E);
198 B.Add(TempE, TopExp::FirstVertex(E));
199 B.Add(TempE, TopExp::LastVertex(E));
202 B.UpdateEdge(TempE, Handle(Geom_Curve)::DownCast(C3d->Transformed(LC.Transformation())), Precision::Confusion());
203 B.Range(TempE, f, l);
205 Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
206 Handle(Geom_Surface) STemp = Handle(Geom_Surface)::DownCast(aNewSphere->Transformed(LS.Transformation()));
207 TopLoc_Location LTemp;
210 Standard_Boolean isClosed = BRep_Tool::IsClosed (E, F);
211 Standard_Real aWorkTol = 2*myTolerance+Tol;
212 sfe->FixAddPCurve(TempE, STemp, LTemp, isClosed, Max(Precision::Confusion(), aWorkTol));
213 sfe->FixSameParameter(TempE);
215 //keep the orientation of original edge
216 TempE.Orientation(E.Orientation());
217 C = BRep_Tool::CurveOnSurface(TempE, STemp, LTemp, f, l);
219 // shifting seam of sphere
220 if(isClosed && !C.IsNull()) {
222 Handle(Geom2d_Curve) c22 =
223 BRep_Tool::CurveOnSurface(TopoDS::Edge(TempE.Reversed()),STemp, LTemp,f2,l2);
224 Standard_Real dPreci = Precision::PConfusion()*Precision::PConfusion();
225 if((C->Value(f).SquareDistance(c22->Value(f2)) < dPreci)
226 ||(C->Value(l).SquareDistance(c22->Value(l2)) < dPreci)) {
227 gp_Vec2d shift(S->UPeriod(),0.);
231 //sphere was modified
232 return Standard_True;
235 return Standard_False;
239 //=======================================================================
240 //function : NewParameter
242 //=======================================================================
244 Standard_Boolean BlockFix_SphereSpaceModifier::NewParameter(const TopoDS_Vertex& /*V*/,const TopoDS_Edge& /*E*/,
245 Standard_Real& /*P*/,Standard_Real& /*Tol*/)
247 return Standard_False;
251 //=======================================================================
252 //function : Continuity
254 //=======================================================================
256 GeomAbs_Shape BlockFix_SphereSpaceModifier::Continuity(const TopoDS_Edge& E,const TopoDS_Face& F1,
257 const TopoDS_Face& F2,const TopoDS_Edge& /*NewE*/,
258 const TopoDS_Face& /*NewF1*/,const TopoDS_Face& /*NewF2*/)
260 return BRep_Tool::Continuity(E,F1,F2);