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 Handle(Geom_SphericalSurface) aSphere = Handle(Geom_SphericalSurface)::DownCast(S);
69 gp_Sphere sp = aSphere->Sphere();
70 gp_Ax3 ax3 = sp.Position();
71 if(Abs(Vmax-Vmin) < PI2) {
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;
79 gp_Pnt PC = ax3.Location();
81 if(fabs(PI2-Vmax)>fabs(-PI2-Vmin))
84 Vpar = (-PI2+Vmin)/2.;
85 Standard_Real Upar = (Umin+Umax)/2.;;
88 S->D0(Upar+PI2,0.,PX);
89 gp_Dir newNorm(gp_Vec(PC,PN));
90 gp_Dir newDirX(gp_Vec(PC,PX));
91 gp_Ax3 axnew3(ax3.Axis().Location(), newNorm, newDirX);
92 sp.SetPosition(axnew3);
93 Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
94 aNewSurface = aNewSphere;
99 return Standard_False;
103 Standard_Boolean BlockFix_SphereSpaceModifier::NewSurface(const TopoDS_Face& F,
104 Handle(Geom_Surface)& S,
105 TopLoc_Location& L,Standard_Real& Tol,
106 Standard_Boolean& RevWires,
107 Standard_Boolean& RevFace)
110 Handle(Geom_Surface) SIni = BRep_Tool::Surface(F, LS);
112 //check if pole of the sphere in the parametric space
113 if(ModifySurface(F, SIni, S)) {
115 RevWires = Standard_False;
116 RevFace = Standard_False;
119 Tol = BRep_Tool::Tolerance(F);
121 Standard_Integer anIndex = myMapOfSpheres.Add(S);
122 myMapOfFaces.Bind(F,anIndex);
123 return Standard_True;
126 return Standard_False;
129 //=======================================================================
130 //function : NewCurve
132 //=======================================================================
134 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve(const TopoDS_Edge& /*E*/,Handle(Geom_Curve)& /*C*/,
135 TopLoc_Location& /*L*/,Standard_Real& /*Tol*/)
137 return Standard_False;
140 //=======================================================================
141 //function : NewPoint
143 //=======================================================================
145 Standard_Boolean BlockFix_SphereSpaceModifier::NewPoint(const TopoDS_Vertex& /*V*/,
147 Standard_Real& /*Tol*/)
149 return Standard_False;
152 //=======================================================================
153 //function : NewCurve2d
155 //=======================================================================
157 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve2d(const TopoDS_Edge& E,const TopoDS_Face& F,
158 const TopoDS_Edge& /*NewE*/,const TopoDS_Face& /*NewF*/,
159 Handle(Geom2d_Curve)& C,Standard_Real& Tol)
161 //check if undelying surface of the face was modified
162 if(myMapOfFaces.IsBound(F)) {
163 Standard_Integer anIndex = myMapOfFaces.Find(F);
165 Handle(Geom_Surface) aNewSphere = Handle(Geom_Surface)::DownCast(myMapOfSpheres.FindKey(anIndex));
168 TopLoc_Location LC, LS;
169 Handle(Geom_Curve) C3d = BRep_Tool::Curve ( E, LC, f, l );
170 Handle(Geom_Surface) S = BRep_Tool::Surface(F, LS);
172 //taking into accound the orientation of the seam
173 C = BRep_Tool::CurveOnSurface(E,F,f,l);
174 Tol = BRep_Tool::Tolerance(E);
179 B.Add(TempE, TopExp::FirstVertex(E));
180 B.Add(TempE, TopExp::LastVertex(E));
183 B.UpdateEdge(TempE, Handle(Geom_Curve)::DownCast(C3d->Transformed(LC.Transformation())), Precision::Confusion());
184 B.Range(TempE, f, l);
186 Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
187 Handle(Geom_Surface) STemp = Handle(Geom_Surface)::DownCast(aNewSphere->Transformed(LS.Transformation()));
188 TopLoc_Location LTemp;
191 Standard_Boolean isClosed = BRep_Tool::IsClosed (E, F);
192 Standard_Real aWorkTol = 2*myTolerance+Tol;
193 sfe->FixAddPCurve(TempE, STemp, LTemp, isClosed, Max(Precision::Confusion(), aWorkTol));
194 sfe->FixSameParameter(TempE);
196 //keep the orientation of original edge
197 TempE.Orientation(E.Orientation());
198 C = BRep_Tool::CurveOnSurface(TempE, STemp, LTemp, f, l);
200 // shifting seam of sphere
201 if(isClosed && !C.IsNull()) {
203 Handle(Geom2d_Curve) c22 =
204 BRep_Tool::CurveOnSurface(TopoDS::Edge(TempE.Reversed()),STemp, LTemp,f2,l2);
205 Standard_Real dPreci = Precision::PConfusion()*Precision::PConfusion();
206 if((C->Value(f).SquareDistance(c22->Value(f2)) < dPreci)
207 ||(C->Value(l).SquareDistance(c22->Value(l2)) < dPreci)) {
208 gp_Vec2d shift(S->UPeriod(),0.);
212 //sphere was modified
213 return Standard_True;
216 return Standard_False;
220 //=======================================================================
221 //function : NewParameter
223 //=======================================================================
225 Standard_Boolean BlockFix_SphereSpaceModifier::NewParameter(const TopoDS_Vertex& /*V*/,const TopoDS_Edge& /*E*/,
226 Standard_Real& /*P*/,Standard_Real& /*Tol*/)
228 return Standard_False;
232 //=======================================================================
233 //function : Continuity
235 //=======================================================================
237 GeomAbs_Shape BlockFix_SphereSpaceModifier::Continuity(const TopoDS_Edge& E,const TopoDS_Face& F1,
238 const TopoDS_Face& F2,const TopoDS_Edge& /*NewE*/,
239 const TopoDS_Face& /*NewF1*/,const TopoDS_Face& /*NewF2*/)
241 return BRep_Tool::Continuity(E,F1,F2);