Salome HOME
Update copyright
[modules/geom.git] / src / GEOMAlgo / BlockFix_SphereSpaceModifier.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:        BlockFix.cxx
24 // Created:     Tue Dec  7 11:59:05 2004
25 // Author:      Pavel DURANDIN
26 //
27 #include <BlockFix_SphereSpaceModifier.ixx>
28
29 #include <TopLoc_Location.hxx>
30 #include <BRep_Tool.hxx>
31 #include <Geom_SphericalSurface.hxx>
32 #include <Geom_RectangularTrimmedSurface.hxx>
33 #include <ShapeAnalysis.hxx>
34 #include <gp_Sphere.hxx>
35 #include <BRep_Builder.hxx>
36 #include <TopoDS.hxx>
37 #include <TopoDS_Vertex.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopExp.hxx>
40 #include <ShapeFix_Edge.hxx>
41 #include <Geom_Curve.hxx>
42 #include <Geom2d_Curve.hxx>
43
44
45 //=======================================================================
46 //function : BlockFix_SphereSpaceModifier
47 //purpose  : 
48 //=======================================================================
49
50 BlockFix_SphereSpaceModifier::BlockFix_SphereSpaceModifier()
51 {
52   myMapOfFaces.Clear();
53   myMapOfSpheres.Clear();
54 }
55
56 //=======================================================================
57 //function : SetTolerance
58 //purpose  : 
59 //=======================================================================
60
61 void BlockFix_SphereSpaceModifier::SetTolerance(const Standard_Real Tol)
62 {
63   myTolerance = Tol;
64 }
65
66
67 //=======================================================================
68 //function : NewSurface
69 //purpose  : 
70 //=======================================================================
71
72
73 static Standard_Boolean ModifySurface(const TopoDS_Face& aFace,
74                                       const Handle(Geom_Surface)& aSurface,
75                                       Handle(Geom_Surface)& aNewSurface)
76 {
77   Handle(Geom_Surface) S = aSurface;
78   if(S->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
79     Handle(Geom_RectangularTrimmedSurface) RTS = 
80       Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
81     S = RTS->BasisSurface();
82   }
83   
84   if(S->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
85     Standard_Real Umin, Umax, Vmin, Vmax;
86     ShapeAnalysis::GetFaceUVBounds(aFace,Umin, Umax, Vmin, Vmax);
87     Standard_Real PI2 = PI/2.;
88     if(Vmax > PI2 - Precision::PConfusion() || Vmin < -PI2+::Precision::PConfusion()) {
89       Handle(Geom_SphericalSurface) aSphere = Handle(Geom_SphericalSurface)::DownCast(S);
90       gp_Sphere sp = aSphere->Sphere();
91       gp_Ax3 ax3 = sp.Position();
92       if(Abs(Vmax-Vmin) < PI2) {
93         gp_Ax3 axnew3(ax3.Axis().Location(), ax3.Direction()^ax3.XDirection(),ax3.XDirection());
94         sp.SetPosition(axnew3);
95         Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
96         aNewSurface = aNewSphere;
97         return Standard_True;
98       }
99       else {
100         gp_Pnt PC = ax3.Location();
101         Standard_Real Vpar;
102         if(fabs(PI2-Vmax)>fabs(-PI2-Vmin))
103           Vpar = (PI2+Vmax)/2.;
104         else
105           Vpar = (-PI2+Vmin)/2.;
106         Standard_Real Upar = (Umin+Umax)/2.;;
107         gp_Pnt PN,PX;
108         S->D0(Upar,Vpar,PN);
109         S->D0(Upar+PI2,0.,PX);
110         gp_Dir newNorm(gp_Vec(PC,PN));
111         gp_Dir newDirX(gp_Vec(PC,PX));
112         gp_Ax3 axnew3(ax3.Axis().Location(), newNorm, newDirX);
113         sp.SetPosition(axnew3);
114         Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
115         aNewSurface = aNewSphere;
116         return Standard_True;
117       }
118     }
119   }
120   return Standard_False;
121 }
122                                       
123
124 Standard_Boolean BlockFix_SphereSpaceModifier::NewSurface(const TopoDS_Face& F,
125                                                         Handle(Geom_Surface)& S,
126                                                         TopLoc_Location& L,Standard_Real& Tol,
127                                                         Standard_Boolean& RevWires,
128                                                         Standard_Boolean& RevFace) 
129 {
130   TopLoc_Location LS;
131   Handle(Geom_Surface) SIni = BRep_Tool::Surface(F, LS);
132   
133   //check if pole of the sphere in the parametric space
134   if(ModifySurface(F, SIni, S)) {
135   
136     RevWires = Standard_False;
137     RevFace = Standard_False;
138     
139     L = LS;
140     Tol = BRep_Tool::Tolerance(F);
141      
142     Standard_Integer anIndex = myMapOfSpheres.Add(S);
143     myMapOfFaces.Bind(F,anIndex);
144     return Standard_True;
145   }
146   
147   return Standard_False;
148 }
149
150 //=======================================================================
151 //function : NewCurve
152 //purpose  : 
153 //=======================================================================
154
155 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve(const TopoDS_Edge& /*E*/,Handle(Geom_Curve)& /*C*/,
156                                                         TopLoc_Location& /*L*/,Standard_Real& /*Tol*/) 
157 {
158   return Standard_False;
159 }
160
161 //=======================================================================
162 //function : NewPoint
163 //purpose  : 
164 //=======================================================================
165
166 Standard_Boolean BlockFix_SphereSpaceModifier::NewPoint(const TopoDS_Vertex& /*V*/,
167                                                       gp_Pnt& /*P*/,
168                                                       Standard_Real& /*Tol*/) 
169 {
170   return Standard_False;
171 }
172
173 //=======================================================================
174 //function : NewCurve2d
175 //purpose  : 
176 //=======================================================================
177
178 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve2d(const TopoDS_Edge& E,const TopoDS_Face& F,
179                                                         const TopoDS_Edge& /*NewE*/,const TopoDS_Face& /*NewF*/,
180                                                         Handle(Geom2d_Curve)& C,Standard_Real& Tol) 
181 {
182   //check if undelying surface of the face was modified
183   if(myMapOfFaces.IsBound(F)) {
184     Standard_Integer anIndex = myMapOfFaces.Find(F);
185     
186     Handle(Geom_Surface) aNewSphere = Handle(Geom_Surface)::DownCast(myMapOfSpheres.FindKey(anIndex));
187     
188     Standard_Real f,l;
189     TopLoc_Location LC, LS;
190     Handle(Geom_Curve) C3d = BRep_Tool::Curve ( E, LC, f, l );
191     Handle(Geom_Surface) S = BRep_Tool::Surface(F, LS);
192   
193     //taking into accound the orientation of the seam
194     C = BRep_Tool::CurveOnSurface(E,F,f,l);
195     Tol = BRep_Tool::Tolerance(E);
196      
197     BRep_Builder B;
198     TopoDS_Edge TempE;
199     B.MakeEdge(TempE);
200     B.Add(TempE, TopExp::FirstVertex(E));
201     B.Add(TempE, TopExp::LastVertex(E));
202
203     if(!C3d.IsNull()) 
204       B.UpdateEdge(TempE, Handle(Geom_Curve)::DownCast(C3d->Transformed(LC.Transformation())), Precision::Confusion());
205     B.Range(TempE, f, l);
206     
207     Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
208     Handle(Geom_Surface) STemp = Handle(Geom_Surface)::DownCast(aNewSphere->Transformed(LS.Transformation()));
209     TopLoc_Location LTemp;
210     LTemp.Identity();
211     
212     Standard_Boolean isClosed = BRep_Tool::IsClosed (E, F);
213     Standard_Real aWorkTol = 2*myTolerance+Tol;
214     sfe->FixAddPCurve(TempE, STemp, LTemp, isClosed, Max(Precision::Confusion(), aWorkTol));
215     sfe->FixSameParameter(TempE);
216       
217     //keep the orientation of original edge
218     TempE.Orientation(E.Orientation());
219     C = BRep_Tool::CurveOnSurface(TempE, STemp, LTemp, f, l);
220     
221     // shifting seam of sphere
222     if(isClosed  && !C.IsNull()) {
223       Standard_Real f2,l2;
224       Handle(Geom2d_Curve) c22 = 
225         BRep_Tool::CurveOnSurface(TopoDS::Edge(TempE.Reversed()),STemp, LTemp,f2,l2);
226       Standard_Real dPreci = Precision::PConfusion()*Precision::PConfusion();
227       if((C->Value(f).SquareDistance(c22->Value(f2)) < dPreci)
228          ||(C->Value(l).SquareDistance(c22->Value(l2)) < dPreci)) {
229         gp_Vec2d shift(S->UPeriod(),0.);
230         C->Translate(shift);
231       }
232     }
233     //sphere was modified
234     return Standard_True;
235   }
236
237   return Standard_False;
238 }
239
240
241 //=======================================================================
242 //function : NewParameter
243 //purpose  : 
244 //=======================================================================
245
246 Standard_Boolean BlockFix_SphereSpaceModifier::NewParameter(const TopoDS_Vertex& /*V*/,const TopoDS_Edge& /*E*/,
247                                                             Standard_Real& /*P*/,Standard_Real& /*Tol*/) 
248 {
249   return Standard_False;
250 }
251
252
253 //=======================================================================
254 //function : Continuity
255 //purpose  : 
256 //=======================================================================
257
258 GeomAbs_Shape BlockFix_SphereSpaceModifier::Continuity(const TopoDS_Edge& E,const TopoDS_Face& F1,
259                                                      const TopoDS_Face& F2,const TopoDS_Edge& /*NewE*/,
260                                                      const TopoDS_Face& /*NewF1*/,const TopoDS_Face& /*NewF2*/) 
261 {
262   return BRep_Tool::Continuity(E,F1,F2);
263 }