Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/geom.git] / src / GEOMAlgo / BlockFix_SphereSpaceModifier.cxx
1 //  Copyright (C) 2007-2008  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 // File:        BlockFix.cxx
23 // Created:     Tue Dec  7 11:59:05 2004
24 // Author:      Pavel DURANDIN
25 //
26 #include <BlockFix_SphereSpaceModifier.ixx>
27
28 #include <TopLoc_Location.hxx>
29 #include <BRep_Tool.hxx>
30 #include <Geom_SphericalSurface.hxx>
31 #include <Geom_RectangularTrimmedSurface.hxx>
32 #include <ShapeAnalysis.hxx>
33 #include <gp_Sphere.hxx>
34 #include <BRep_Builder.hxx>
35 #include <TopoDS.hxx>
36 #include <TopoDS_Vertex.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopExp.hxx>
39 #include <ShapeFix_Edge.hxx>
40 #include <Geom_Curve.hxx>
41 #include <Geom2d_Curve.hxx>
42
43
44 //=======================================================================
45 //function : BlockFix_SphereSpaceModifier
46 //purpose  : 
47 //=======================================================================
48
49 BlockFix_SphereSpaceModifier::BlockFix_SphereSpaceModifier()
50 {
51   myMapOfFaces.Clear();
52   myMapOfSpheres.Clear();
53 }
54
55 //=======================================================================
56 //function : SetTolerance
57 //purpose  : 
58 //=======================================================================
59
60 void BlockFix_SphereSpaceModifier::SetTolerance(const Standard_Real Tol)
61 {
62   myTolerance = Tol;
63 }
64
65
66 //=======================================================================
67 //function : NewSurface
68 //purpose  : 
69 //=======================================================================
70
71
72 static Standard_Boolean ModifySurface(const TopoDS_Face& aFace,
73                                       const Handle(Geom_Surface)& aSurface,
74                                       Handle(Geom_Surface)& aNewSurface)
75 {
76   Handle(Geom_Surface) S = aSurface;
77   if(S->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
78     Handle(Geom_RectangularTrimmedSurface) RTS = 
79       Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
80     S = RTS->BasisSurface();
81   }
82   
83   if(S->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
84     Standard_Real Umin, Umax, Vmin, Vmax;
85     ShapeAnalysis::GetFaceUVBounds(aFace,Umin, Umax, Vmin, Vmax);
86     Standard_Real PI2 = PI/2.;
87     if(Vmax > PI2 - Precision::PConfusion() || Vmin < -PI2+::Precision::PConfusion()) {
88       Handle(Geom_SphericalSurface) aSphere = Handle(Geom_SphericalSurface)::DownCast(S);
89       gp_Sphere sp = aSphere->Sphere();
90       gp_Ax3 ax3 = sp.Position();
91       if(Abs(Vmax-Vmin) < PI2) {
92         gp_Ax3 axnew3(ax3.Axis().Location(), ax3.Direction()^ax3.XDirection(),ax3.XDirection());
93         sp.SetPosition(axnew3);
94         Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
95         aNewSurface = aNewSphere;
96         return Standard_True;
97       }
98       else {
99         gp_Pnt PC = ax3.Location();
100         Standard_Real Vpar;
101         if(fabs(PI2-Vmax)>fabs(-PI2-Vmin))
102           Vpar = (PI2+Vmax)/2.;
103         else
104           Vpar = (-PI2+Vmin)/2.;
105         Standard_Real Upar = (Umin+Umax)/2.;;
106         gp_Pnt PN,PX;
107         S->D0(Upar,Vpar,PN);
108         S->D0(Upar+PI2,0.,PX);
109         gp_Dir newNorm(gp_Vec(PC,PN));
110         gp_Dir newDirX(gp_Vec(PC,PX));
111         gp_Ax3 axnew3(ax3.Axis().Location(), newNorm, newDirX);
112         sp.SetPosition(axnew3);
113         Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
114         aNewSurface = aNewSphere;
115         return Standard_True;
116       }
117     }
118   }
119   return Standard_False;
120 }
121                                       
122
123 Standard_Boolean BlockFix_SphereSpaceModifier::NewSurface(const TopoDS_Face& F,
124                                                         Handle(Geom_Surface)& S,
125                                                         TopLoc_Location& L,Standard_Real& Tol,
126                                                         Standard_Boolean& RevWires,
127                                                         Standard_Boolean& RevFace) 
128 {
129   TopLoc_Location LS;
130   Handle(Geom_Surface) SIni = BRep_Tool::Surface(F, LS);
131   
132   //check if pole of the sphere in the parametric space
133   if(ModifySurface(F, SIni, S)) {
134   
135     RevWires = Standard_False;
136     RevFace = Standard_False;
137     
138     L = LS;
139     Tol = BRep_Tool::Tolerance(F);
140      
141     Standard_Integer anIndex = myMapOfSpheres.Add(S);
142     myMapOfFaces.Bind(F,anIndex);
143     return Standard_True;
144   }
145   
146   return Standard_False;
147 }
148
149 //=======================================================================
150 //function : NewCurve
151 //purpose  : 
152 //=======================================================================
153
154 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve(const TopoDS_Edge& /*E*/,Handle(Geom_Curve)& /*C*/,
155                                                         TopLoc_Location& /*L*/,Standard_Real& /*Tol*/) 
156 {
157   return Standard_False;
158 }
159
160 //=======================================================================
161 //function : NewPoint
162 //purpose  : 
163 //=======================================================================
164
165 Standard_Boolean BlockFix_SphereSpaceModifier::NewPoint(const TopoDS_Vertex& /*V*/,
166                                                       gp_Pnt& /*P*/,
167                                                       Standard_Real& /*Tol*/) 
168 {
169   return Standard_False;
170 }
171
172 //=======================================================================
173 //function : NewCurve2d
174 //purpose  : 
175 //=======================================================================
176
177 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve2d(const TopoDS_Edge& E,const TopoDS_Face& F,
178                                                         const TopoDS_Edge& /*NewE*/,const TopoDS_Face& /*NewF*/,
179                                                         Handle(Geom2d_Curve)& C,Standard_Real& Tol) 
180 {
181   //check if undelying surface of the face was modified
182   if(myMapOfFaces.IsBound(F)) {
183     Standard_Integer anIndex = myMapOfFaces.Find(F);
184     
185     Handle(Geom_Surface) aNewSphere = Handle(Geom_Surface)::DownCast(myMapOfSpheres.FindKey(anIndex));
186     
187     Standard_Real f,l;
188     TopLoc_Location LC, LS;
189     Handle(Geom_Curve) C3d = BRep_Tool::Curve ( E, LC, f, l );
190     Handle(Geom_Surface) S = BRep_Tool::Surface(F, LS);
191   
192     //taking into accound the orientation of the seam
193     C = BRep_Tool::CurveOnSurface(E,F,f,l);
194     Tol = BRep_Tool::Tolerance(E);
195      
196     BRep_Builder B;
197     TopoDS_Edge TempE;
198     B.MakeEdge(TempE);
199     B.Add(TempE, TopExp::FirstVertex(E));
200     B.Add(TempE, TopExp::LastVertex(E));
201
202     if(!C3d.IsNull()) 
203       B.UpdateEdge(TempE, Handle(Geom_Curve)::DownCast(C3d->Transformed(LC.Transformation())), Precision::Confusion());
204     B.Range(TempE, f, l);
205     
206     Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
207     Handle(Geom_Surface) STemp = Handle(Geom_Surface)::DownCast(aNewSphere->Transformed(LS.Transformation()));
208     TopLoc_Location LTemp;
209     LTemp.Identity();
210     
211     Standard_Boolean isClosed = BRep_Tool::IsClosed (E, F);
212     Standard_Real aWorkTol = 2*myTolerance+Tol;
213     sfe->FixAddPCurve(TempE, STemp, LTemp, isClosed, Max(Precision::Confusion(), aWorkTol));
214     sfe->FixSameParameter(TempE);
215       
216     //keep the orientation of original edge
217     TempE.Orientation(E.Orientation());
218     C = BRep_Tool::CurveOnSurface(TempE, STemp, LTemp, f, l);
219     
220     // shifting seam of sphere
221     if(isClosed  && !C.IsNull()) {
222       Standard_Real f2,l2;
223       Handle(Geom2d_Curve) c22 = 
224         BRep_Tool::CurveOnSurface(TopoDS::Edge(TempE.Reversed()),STemp, LTemp,f2,l2);
225       Standard_Real dPreci = Precision::PConfusion()*Precision::PConfusion();
226       if((C->Value(f).SquareDistance(c22->Value(f2)) < dPreci)
227          ||(C->Value(l).SquareDistance(c22->Value(l2)) < dPreci)) {
228         gp_Vec2d shift(S->UPeriod(),0.);
229         C->Translate(shift);
230       }
231     }
232     //sphere was modified
233     return Standard_True;
234   }
235
236   return Standard_False;
237 }
238
239
240 //=======================================================================
241 //function : NewParameter
242 //purpose  : 
243 //=======================================================================
244
245 Standard_Boolean BlockFix_SphereSpaceModifier::NewParameter(const TopoDS_Vertex& /*V*/,const TopoDS_Edge& /*E*/,
246                                                             Standard_Real& /*P*/,Standard_Real& /*Tol*/) 
247 {
248   return Standard_False;
249 }
250
251
252 //=======================================================================
253 //function : Continuity
254 //purpose  : 
255 //=======================================================================
256
257 GeomAbs_Shape BlockFix_SphereSpaceModifier::Continuity(const TopoDS_Edge& E,const TopoDS_Face& F1,
258                                                      const TopoDS_Face& F2,const TopoDS_Edge& /*NewE*/,
259                                                      const TopoDS_Face& /*NewF1*/,const TopoDS_Face& /*NewF2*/) 
260 {
261   return BRep_Tool::Continuity(E,F1,F2);
262 }