Back to index

salome-geom  6.5.0
BlockFix_SphereSpaceModifier.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File:        BlockFix.cxx
00024 // Created:     Tue Dec  7 11:59:05 2004
00025 // Author:      Pavel DURANDIN
00026 
00027 #include <BlockFix_SphereSpaceModifier.ixx>
00028 
00029 #include <TopLoc_Location.hxx>
00030 #include <BRep_Tool.hxx>
00031 #include <Geom_SphericalSurface.hxx>
00032 #include <Geom_RectangularTrimmedSurface.hxx>
00033 #include <ShapeAnalysis.hxx>
00034 #include <gp_Sphere.hxx>
00035 #include <BRep_Builder.hxx>
00036 #include <TopoDS.hxx>
00037 #include <TopoDS_Vertex.hxx>
00038 #include <TopoDS_Edge.hxx>
00039 #include <TopExp.hxx>
00040 #include <ShapeFix_Edge.hxx>
00041 #include <Geom_Curve.hxx>
00042 #include <Geom2d_Curve.hxx>
00043 
00044 
00045 //=======================================================================
00046 //function : BlockFix_SphereSpaceModifier
00047 //purpose  :
00048 //=======================================================================
00049 
00050 BlockFix_SphereSpaceModifier::BlockFix_SphereSpaceModifier()
00051 {
00052   myMapOfFaces.Clear();
00053   myMapOfSpheres.Clear();
00054 }
00055 
00056 //=======================================================================
00057 //function : SetTolerance
00058 //purpose  :
00059 //=======================================================================
00060 
00061 void BlockFix_SphereSpaceModifier::SetTolerance(const Standard_Real Tol)
00062 {
00063   myTolerance = Tol;
00064 }
00065 
00066 
00067 //=======================================================================
00068 //function : NewSurface
00069 //purpose  :
00070 //=======================================================================
00071 
00072 
00073 static Standard_Boolean ModifySurface(const TopoDS_Face& aFace,
00074                                       const Handle(Geom_Surface)& aSurface,
00075                                       Handle(Geom_Surface)& aNewSurface)
00076 {
00077   Handle(Geom_Surface) S = aSurface;
00078   if(S->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
00079     Handle(Geom_RectangularTrimmedSurface) RTS =
00080       Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
00081     S = RTS->BasisSurface();
00082   }
00083 
00084   if(S->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
00085     Standard_Real Umin, Umax, Vmin, Vmax;
00086     ShapeAnalysis::GetFaceUVBounds(aFace,Umin, Umax, Vmin, Vmax);
00087     Standard_Real PI2 = M_PI/2.;
00088     if(Vmax > PI2 - Precision::PConfusion() || Vmin < -PI2+::Precision::PConfusion()) {
00089       Handle(Geom_SphericalSurface) aSphere = Handle(Geom_SphericalSurface)::DownCast(S);
00090       gp_Sphere sp = aSphere->Sphere();
00091       gp_Ax3 ax3 = sp.Position();
00092       if(Abs(Vmax-Vmin) < PI2) {
00093         gp_Ax3 axnew3(ax3.Axis().Location(), ax3.Direction()^ax3.XDirection(),ax3.XDirection());
00094         sp.SetPosition(axnew3);
00095         Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
00096         aNewSurface = aNewSphere;
00097         return Standard_True;
00098       }
00099       else {
00100         gp_Pnt PC = ax3.Location();
00101         Standard_Real Vpar;
00102         if(fabs(PI2-Vmax)>fabs(-PI2-Vmin))
00103           Vpar = (PI2+Vmax)/2.;
00104         else
00105           Vpar = (-PI2+Vmin)/2.;
00106         Standard_Real Upar = (Umin+Umax)/2.;;
00107         gp_Pnt PN,PX;
00108         S->D0(Upar,Vpar,PN);
00109         S->D0(Upar+PI2,0.,PX);
00110         gp_Dir newNorm(gp_Vec(PC,PN));
00111         gp_Dir newDirX(gp_Vec(PC,PX));
00112         gp_Ax3 axnew3(ax3.Axis().Location(), newNorm, newDirX);
00113         sp.SetPosition(axnew3);
00114         Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
00115         aNewSurface = aNewSphere;
00116         return Standard_True;
00117       }
00118     }
00119   }
00120   return Standard_False;
00121 }
00122 
00123 
00124 Standard_Boolean BlockFix_SphereSpaceModifier::NewSurface(const TopoDS_Face& F,
00125                                                         Handle(Geom_Surface)& S,
00126                                                         TopLoc_Location& L,Standard_Real& Tol,
00127                                                         Standard_Boolean& RevWires,
00128                                                         Standard_Boolean& RevFace)
00129 {
00130   TopLoc_Location LS;
00131   Handle(Geom_Surface) SIni = BRep_Tool::Surface(F, LS);
00132 
00133   //check if pole of the sphere in the parametric space
00134   if(ModifySurface(F, SIni, S)) {
00135 
00136     RevWires = Standard_False;
00137     RevFace = Standard_False;
00138 
00139     L = LS;
00140     Tol = BRep_Tool::Tolerance(F);
00141 
00142     Standard_Integer anIndex = myMapOfSpheres.Add(S);
00143     myMapOfFaces.Bind(F,anIndex);
00144     return Standard_True;
00145   }
00146 
00147   return Standard_False;
00148 }
00149 
00150 //=======================================================================
00151 //function : NewCurve
00152 //purpose  :
00153 //=======================================================================
00154 
00155 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve(const TopoDS_Edge& /*E*/,Handle(Geom_Curve)& /*C*/,
00156                                                         TopLoc_Location& /*L*/,Standard_Real& /*Tol*/)
00157 {
00158   return Standard_False;
00159 }
00160 
00161 //=======================================================================
00162 //function : NewPoint
00163 //purpose  :
00164 //=======================================================================
00165 
00166 Standard_Boolean BlockFix_SphereSpaceModifier::NewPoint(const TopoDS_Vertex& /*V*/,
00167                                                       gp_Pnt& /*P*/,
00168                                                       Standard_Real& /*Tol*/)
00169 {
00170   return Standard_False;
00171 }
00172 
00173 //=======================================================================
00174 //function : NewCurve2d
00175 //purpose  :
00176 //=======================================================================
00177 
00178 Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve2d(const TopoDS_Edge& E,const TopoDS_Face& F,
00179                                                         const TopoDS_Edge& /*NewE*/,const TopoDS_Face& /*NewF*/,
00180                                                         Handle(Geom2d_Curve)& C,Standard_Real& Tol)
00181 {
00182   //check if undelying surface of the face was modified
00183   if(myMapOfFaces.IsBound(F)) {
00184     Standard_Integer anIndex = myMapOfFaces.Find(F);
00185 
00186     Handle(Geom_Surface) aNewSphere = Handle(Geom_Surface)::DownCast(myMapOfSpheres.FindKey(anIndex));
00187 
00188     Standard_Real f,l;
00189     TopLoc_Location LC, LS;
00190     Handle(Geom_Curve) C3d = BRep_Tool::Curve ( E, LC, f, l );
00191     Handle(Geom_Surface) S = BRep_Tool::Surface(F, LS);
00192 
00193     //taking into accound the orientation of the seam
00194     C = BRep_Tool::CurveOnSurface(E,F,f,l);
00195     Tol = BRep_Tool::Tolerance(E);
00196 
00197     BRep_Builder B;
00198     TopoDS_Edge TempE;
00199     B.MakeEdge(TempE);
00200     B.Add(TempE, TopExp::FirstVertex(E));
00201     B.Add(TempE, TopExp::LastVertex(E));
00202 
00203     if(!C3d.IsNull())
00204       B.UpdateEdge(TempE, Handle(Geom_Curve)::DownCast(C3d->Transformed(LC.Transformation())), Precision::Confusion());
00205     B.Range(TempE, f, l);
00206 
00207     Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
00208     Handle(Geom_Surface) STemp = Handle(Geom_Surface)::DownCast(aNewSphere->Transformed(LS.Transformation()));
00209     TopLoc_Location LTemp;
00210     LTemp.Identity();
00211 
00212     Standard_Boolean isClosed = BRep_Tool::IsClosed (E, F);
00213     Standard_Real aWorkTol = 2*myTolerance+Tol;
00214     sfe->FixAddPCurve(TempE, STemp, LTemp, isClosed, Max(Precision::Confusion(), aWorkTol));
00215     sfe->FixSameParameter(TempE);
00216 
00217     //keep the orientation of original edge
00218     TempE.Orientation(E.Orientation());
00219     C = BRep_Tool::CurveOnSurface(TempE, STemp, LTemp, f, l);
00220 
00221     // shifting seam of sphere
00222     if(isClosed  && !C.IsNull()) {
00223       Standard_Real f2,l2;
00224       Handle(Geom2d_Curve) c22 =
00225         BRep_Tool::CurveOnSurface(TopoDS::Edge(TempE.Reversed()),STemp, LTemp,f2,l2);
00226       Standard_Real dPreci = Precision::PConfusion()*Precision::PConfusion();
00227       if((C->Value(f).SquareDistance(c22->Value(f2)) < dPreci)
00228          ||(C->Value(l).SquareDistance(c22->Value(l2)) < dPreci)) {
00229         gp_Vec2d shift(S->UPeriod(),0.);
00230         C->Translate(shift);
00231       }
00232     }
00233     //sphere was modified
00234     return Standard_True;
00235   }
00236 
00237   return Standard_False;
00238 }
00239 
00240 
00241 //=======================================================================
00242 //function : NewParameter
00243 //purpose  :
00244 //=======================================================================
00245 
00246 Standard_Boolean BlockFix_SphereSpaceModifier::NewParameter(const TopoDS_Vertex& /*V*/,const TopoDS_Edge& /*E*/,
00247                                                             Standard_Real& /*P*/,Standard_Real& /*Tol*/)
00248 {
00249   return Standard_False;
00250 }
00251 
00252 
00253 //=======================================================================
00254 //function : Continuity
00255 //purpose  :
00256 //=======================================================================
00257 
00258 GeomAbs_Shape BlockFix_SphereSpaceModifier::Continuity(const TopoDS_Edge& E,const TopoDS_Face& F1,
00259                                                      const TopoDS_Face& F2,const TopoDS_Edge& /*NewE*/,
00260                                                      const TopoDS_Face& /*NewF1*/,const TopoDS_Face& /*NewF2*/)
00261 {
00262   return BRep_Tool::Continuity(E,F1,F2);
00263 }