2 // Created: Tue Dec 7 11:59:05 2004
3 // Author: Pavel DURANDIN
4 // Copyright: Open CASCADE SA 2004
6 #include <BlockFix_SphereSpaceModifier.ixx>
8 #include <TopLoc_Location.hxx>
9 #include <BRep_Tool.hxx>
10 #include <Geom_SphericalSurface.hxx>
11 #include <Geom_RectangularTrimmedSurface.hxx>
12 #include <ShapeAnalysis.hxx>
13 #include <gp_Sphere.hxx>
14 #include <BRep_Builder.hxx>
16 #include <TopoDS_Vertex.hxx>
17 #include <TopoDS_Edge.hxx>
19 #include <ShapeFix_Edge.hxx>
20 #include <Geom_Curve.hxx>
21 #include <Geom2d_Curve.hxx>
24 //=======================================================================
25 //function : BlockFix_SphereSpaceModifier
27 //=======================================================================
29 BlockFix_SphereSpaceModifier::BlockFix_SphereSpaceModifier()
32 myMapOfSpheres.Clear();
35 //=======================================================================
36 //function : SetTolerance
38 //=======================================================================
40 void BlockFix_SphereSpaceModifier::SetTolerance(const Standard_Real Tol)
46 //=======================================================================
47 //function : NewSurface
49 //=======================================================================
52 static Standard_Boolean ModifySurface(const TopoDS_Face& aFace,
53 const Handle(Geom_Surface)& aSurface,
54 Handle(Geom_Surface)& aNewSurface)
56 Handle(Geom_Surface) S = aSurface;
57 if(S->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
58 Handle(Geom_RectangularTrimmedSurface) RTS =
59 Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
60 S = RTS->BasisSurface();
63 if(S->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
64 Standard_Real Umin, Umax, Vmin, Vmax;
65 ShapeAnalysis::GetFaceUVBounds(aFace,Umin, Umax, Vmin, Vmax);
66 Standard_Real PI2 = PI/2.;
67 if(Vmax > PI2 - Precision::PConfusion() || Vmin < -PI2+::Precision::PConfusion()) {
68 if(Abs(Vmax-Vmin) < PI2) {
69 Handle(Geom_SphericalSurface) aSphere = Handle(Geom_SphericalSurface)::DownCast(S);
70 gp_Sphere sp = aSphere->Sphere();
71 gp_Ax3 ax3 = sp.Position();
72 gp_Ax3 axnew3(ax3.Axis().Location(), ax3.Direction()^ax3.XDirection(),ax3.XDirection());
73 sp.SetPosition(axnew3);
74 Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
75 aNewSurface = aNewSphere;
80 return Standard_False;
84 Standard_Boolean BlockFix_SphereSpaceModifier::NewSurface(const TopoDS_Face& F,
85 Handle(Geom_Surface)& S,
86 TopLoc_Location& L,Standard_Real& Tol,
87 Standard_Boolean& RevWires,
88 Standard_Boolean& RevFace)
91 Handle(Geom_Surface) SIni = BRep_Tool::Surface(F, LS);
93 //check if pole of the sphere in the parametric space
94 if(ModifySurface(F, SIni, S)) {
96 RevWires = Standard_False;
97 RevFace = Standard_False;
100 Tol = BRep_Tool::Tolerance(F);
102 Standard_Integer anIndex = myMapOfSpheres.Add(S);
103 myMapOfFaces.Bind(F,anIndex);
104 return Standard_True;
107 return Standard_False;
110 //=======================================================================
111 //function : NewCurve
113 //=======================================================================
115 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve(const TopoDS_Edge& /*E*/,Handle(Geom_Curve)& /*C*/,
116 TopLoc_Location& /*L*/,Standard_Real& /*Tol*/)
118 return Standard_False;
121 //=======================================================================
122 //function : NewPoint
124 //=======================================================================
126 Standard_Boolean BlockFix_SphereSpaceModifier::NewPoint(const TopoDS_Vertex& /*V*/,
128 Standard_Real& /*Tol*/)
130 return Standard_False;
133 //=======================================================================
134 //function : NewCurve2d
136 //=======================================================================
138 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve2d(const TopoDS_Edge& E,const TopoDS_Face& F,
139 const TopoDS_Edge& /*NewE*/,const TopoDS_Face& /*NewF*/,
140 Handle(Geom2d_Curve)& C,Standard_Real& Tol)
142 //check if undelying surface of the face was modified
143 if(myMapOfFaces.IsBound(F)) {
144 Standard_Integer anIndex = myMapOfFaces.Find(F);
146 Handle(Geom_Surface) aNewSphere = Handle(Geom_Surface)::DownCast(myMapOfSpheres.FindKey(anIndex));
149 TopLoc_Location LC, LS;
150 Handle(Geom_Curve) C3d = BRep_Tool::Curve ( E, LC, f, l );
151 Handle(Geom_Surface) S = BRep_Tool::Surface(F, LS);
153 //taking into accound the orientation of the seam
154 C = BRep_Tool::CurveOnSurface(E,F,f,l);
155 Tol = BRep_Tool::Tolerance(E);
160 B.Add(TempE, TopExp::FirstVertex(E));
161 B.Add(TempE, TopExp::LastVertex(E));
164 B.UpdateEdge(TempE, Handle(Geom_Curve)::DownCast(C3d->Transformed(LC.Transformation())), Precision::Confusion());
165 B.Range(TempE, f, l);
167 Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
168 Handle(Geom_Surface) STemp = Handle(Geom_Surface)::DownCast(aNewSphere->Transformed(LS.Transformation()));
169 TopLoc_Location LTemp;
172 Standard_Boolean isClosed = BRep_Tool::IsClosed (E, F);
173 Standard_Real aWorkTol = 2*myTolerance+Tol;
174 sfe->FixAddPCurve(TempE, STemp, LTemp, isClosed, Max(Precision::Confusion(), aWorkTol));
175 sfe->FixSameParameter(TempE);
177 //keep the orientation of original edge
178 TempE.Orientation(E.Orientation());
179 C = BRep_Tool::CurveOnSurface(TempE, STemp, LTemp, f, l);
181 // shifting seam of sphere
182 if(isClosed && !C.IsNull()) {
184 Handle(Geom2d_Curve) c22 =
185 BRep_Tool::CurveOnSurface(TopoDS::Edge(TempE.Reversed()),STemp, LTemp,f2,l2);
186 Standard_Real dPreci = Precision::PConfusion()*Precision::PConfusion();
187 if((C->Value(f).SquareDistance(c22->Value(f2)) < dPreci)
188 ||(C->Value(l).SquareDistance(c22->Value(l2)) < dPreci)) {
189 gp_Vec2d shift(S->UPeriod(),0.);
193 //sphere was modified
194 return Standard_True;
197 return Standard_False;
201 //=======================================================================
202 //function : NewParameter
204 //=======================================================================
206 Standard_Boolean BlockFix_SphereSpaceModifier::NewParameter(const TopoDS_Vertex& /*V*/,const TopoDS_Edge& /*E*/,
207 Standard_Real& /*P*/,Standard_Real& /*Tol*/)
209 return Standard_False;
213 //=======================================================================
214 //function : Continuity
216 //=======================================================================
218 GeomAbs_Shape BlockFix_SphereSpaceModifier::Continuity(const TopoDS_Edge& E,const TopoDS_Face& F1,
219 const TopoDS_Face& F2,const TopoDS_Edge& /*NewE*/,
220 const TopoDS_Face& /*NewF1*/,const TopoDS_Face& /*NewF2*/)
222 return BRep_Tool::Continuity(E,F1,F2);