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