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