Salome HOME
c5f774d82de9dcd1ea4ad732878f54e03006da56
[modules/geom.git] / src / GEOMAlgo / BlockFix_SphereSpaceModifier.cxx
1 // File:        BlockFix.cxx
2 // Created:     Tue Dec  7 11:59:05 2004
3 // Author:      Pavel DURANDIN
4 // Copyright:   Open CASCADE SA 2004
5
6 #include <BlockFix_SphereSpaceModifier.ixx>
7
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>
15 #include <TopoDS.hxx>
16 #include <TopoDS_Vertex.hxx>
17 #include <TopoDS_Edge.hxx>
18 #include <TopExp.hxx>
19 #include <ShapeFix_Edge.hxx>
20 #include <Geom_Curve.hxx>
21 #include <Geom2d_Curve.hxx>
22
23
24 //=======================================================================
25 //function : BlockFix_SphereSpaceModifier
26 //purpose  : 
27 //=======================================================================
28
29 BlockFix_SphereSpaceModifier::BlockFix_SphereSpaceModifier()
30 {
31   myMapOfFaces.Clear();
32   myMapOfSpheres.Clear();
33 }
34
35 //=======================================================================
36 //function : SetTolerance
37 //purpose  : 
38 //=======================================================================
39
40 void BlockFix_SphereSpaceModifier::SetTolerance(const Standard_Real Tol)
41 {
42   myTolerance = Tol;
43 }
44
45
46 //=======================================================================
47 //function : NewSurface
48 //purpose  : 
49 //=======================================================================
50
51
52 static Standard_Boolean ModifySurface(const TopoDS_Face& aFace,
53                                       const Handle(Geom_Surface)& aSurface,
54                                       Handle(Geom_Surface)& aNewSurface)
55 {
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();
61   }
62   
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;
76         return Standard_True;
77       }
78     }
79   }
80   return Standard_False;
81 }
82                                       
83
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) 
89 {
90   TopLoc_Location LS;
91   Handle(Geom_Surface) SIni = BRep_Tool::Surface(F, LS);
92   
93   //check if pole of the sphere in the parametric space
94   if(ModifySurface(F, SIni, S)) {
95   
96     RevWires = Standard_False;
97     RevFace = Standard_False;
98     
99     L = LS;
100     Tol = BRep_Tool::Tolerance(F);
101      
102     Standard_Integer anIndex = myMapOfSpheres.Add(S);
103     myMapOfFaces.Bind(F,anIndex);
104     return Standard_True;
105   }
106   
107   return Standard_False;
108 }
109
110 //=======================================================================
111 //function : NewCurve
112 //purpose  : 
113 //=======================================================================
114
115 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve(const TopoDS_Edge& /*E*/,Handle(Geom_Curve)& /*C*/,
116                                                         TopLoc_Location& /*L*/,Standard_Real& /*Tol*/) 
117 {
118   return Standard_False;
119 }
120
121 //=======================================================================
122 //function : NewPoint
123 //purpose  : 
124 //=======================================================================
125
126 Standard_Boolean BlockFix_SphereSpaceModifier::NewPoint(const TopoDS_Vertex& /*V*/,
127                                                       gp_Pnt& /*P*/,
128                                                       Standard_Real& /*Tol*/) 
129 {
130   return Standard_False;
131 }
132
133 //=======================================================================
134 //function : NewCurve2d
135 //purpose  : 
136 //=======================================================================
137
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) 
141 {
142   //check if undelying surface of the face was modified
143   if(myMapOfFaces.IsBound(F)) {
144     Standard_Integer anIndex = myMapOfFaces.Find(F);
145     
146     Handle(Geom_Surface) aNewSphere = Handle(Geom_Surface)::DownCast(myMapOfSpheres.FindKey(anIndex));
147     
148     Standard_Real f,l;
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);
152   
153     //taking into accound the orientation of the seam
154     C = BRep_Tool::CurveOnSurface(E,F,f,l);
155     Tol = BRep_Tool::Tolerance(E);
156      
157     BRep_Builder B;
158     TopoDS_Edge TempE;
159     B.MakeEdge(TempE);
160     B.Add(TempE, TopExp::FirstVertex(E));
161     B.Add(TempE, TopExp::LastVertex(E));
162
163     if(!C3d.IsNull()) 
164       B.UpdateEdge(TempE, Handle(Geom_Curve)::DownCast(C3d->Transformed(LC.Transformation())), Precision::Confusion());
165     B.Range(TempE, f, l);
166     
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;
170     LTemp.Identity();
171     
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);
176       
177     //keep the orientation of original edge
178     TempE.Orientation(E.Orientation());
179     C = BRep_Tool::CurveOnSurface(TempE, STemp, LTemp, f, l);
180     
181     // shifting seam of sphere
182     if(isClosed  && !C.IsNull()) {
183       Standard_Real f2,l2;
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.);
190         C->Translate(shift);
191       }
192     }
193     //sphere was modified
194     return Standard_True;
195   }
196
197   return Standard_False;
198 }
199
200
201 //=======================================================================
202 //function : NewParameter
203 //purpose  : 
204 //=======================================================================
205
206 Standard_Boolean BlockFix_SphereSpaceModifier::NewParameter(const TopoDS_Vertex& /*V*/,const TopoDS_Edge& /*E*/,
207                                                             Standard_Real& /*P*/,Standard_Real& /*Tol*/) 
208 {
209   return Standard_False;
210 }
211
212
213 //=======================================================================
214 //function : Continuity
215 //purpose  : 
216 //=======================================================================
217
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*/) 
221 {
222   return BRep_Tool::Continuity(E,F1,F2);
223 }
224