Salome HOME
fb9e3f27fa4ade1656bbf7010f2b1f0676231834
[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       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;
76         return Standard_True;
77       }
78       else {
79         gp_Pnt PC = ax3.Location();
80         Standard_Real Vpar;
81         if(fabs(PI2-Vmax)>fabs(-PI2-Vmin))
82           Vpar = (PI2+Vmax)/2.;
83         else
84           Vpar = (-PI2+Vmin)/2.;
85         Standard_Real Upar = (Umin+Umax)/2.;;
86         gp_Pnt PN,PX;
87         S->D0(Upar,Vpar,PN);
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;
95         return Standard_True;
96       }
97     }
98   }
99   return Standard_False;
100 }
101                                       
102
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) 
108 {
109   TopLoc_Location LS;
110   Handle(Geom_Surface) SIni = BRep_Tool::Surface(F, LS);
111   
112   //check if pole of the sphere in the parametric space
113   if(ModifySurface(F, SIni, S)) {
114   
115     RevWires = Standard_False;
116     RevFace = Standard_False;
117     
118     L = LS;
119     Tol = BRep_Tool::Tolerance(F);
120      
121     Standard_Integer anIndex = myMapOfSpheres.Add(S);
122     myMapOfFaces.Bind(F,anIndex);
123     return Standard_True;
124   }
125   
126   return Standard_False;
127 }
128
129 //=======================================================================
130 //function : NewCurve
131 //purpose  : 
132 //=======================================================================
133
134 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve(const TopoDS_Edge& /*E*/,Handle(Geom_Curve)& /*C*/,
135                                                         TopLoc_Location& /*L*/,Standard_Real& /*Tol*/) 
136 {
137   return Standard_False;
138 }
139
140 //=======================================================================
141 //function : NewPoint
142 //purpose  : 
143 //=======================================================================
144
145 Standard_Boolean BlockFix_SphereSpaceModifier::NewPoint(const TopoDS_Vertex& /*V*/,
146                                                       gp_Pnt& /*P*/,
147                                                       Standard_Real& /*Tol*/) 
148 {
149   return Standard_False;
150 }
151
152 //=======================================================================
153 //function : NewCurve2d
154 //purpose  : 
155 //=======================================================================
156
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) 
160 {
161   //check if undelying surface of the face was modified
162   if(myMapOfFaces.IsBound(F)) {
163     Standard_Integer anIndex = myMapOfFaces.Find(F);
164     
165     Handle(Geom_Surface) aNewSphere = Handle(Geom_Surface)::DownCast(myMapOfSpheres.FindKey(anIndex));
166     
167     Standard_Real f,l;
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);
171   
172     //taking into accound the orientation of the seam
173     C = BRep_Tool::CurveOnSurface(E,F,f,l);
174     Tol = BRep_Tool::Tolerance(E);
175      
176     BRep_Builder B;
177     TopoDS_Edge TempE;
178     B.MakeEdge(TempE);
179     B.Add(TempE, TopExp::FirstVertex(E));
180     B.Add(TempE, TopExp::LastVertex(E));
181
182     if(!C3d.IsNull()) 
183       B.UpdateEdge(TempE, Handle(Geom_Curve)::DownCast(C3d->Transformed(LC.Transformation())), Precision::Confusion());
184     B.Range(TempE, f, l);
185     
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;
189     LTemp.Identity();
190     
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);
195       
196     //keep the orientation of original edge
197     TempE.Orientation(E.Orientation());
198     C = BRep_Tool::CurveOnSurface(TempE, STemp, LTemp, f, l);
199     
200     // shifting seam of sphere
201     if(isClosed  && !C.IsNull()) {
202       Standard_Real f2,l2;
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.);
209         C->Translate(shift);
210       }
211     }
212     //sphere was modified
213     return Standard_True;
214   }
215
216   return Standard_False;
217 }
218
219
220 //=======================================================================
221 //function : NewParameter
222 //purpose  : 
223 //=======================================================================
224
225 Standard_Boolean BlockFix_SphereSpaceModifier::NewParameter(const TopoDS_Vertex& /*V*/,const TopoDS_Edge& /*E*/,
226                                                             Standard_Real& /*P*/,Standard_Real& /*Tol*/) 
227 {
228   return Standard_False;
229 }
230
231
232 //=======================================================================
233 //function : Continuity
234 //purpose  : 
235 //=======================================================================
236
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*/) 
240 {
241   return BRep_Tool::Continuity(E,F1,F2);
242 }