geom/src/BlockFix/BlockFix_SphereSpaceModifier.cxx

284 lines
10 KiB
C++
Raw Normal View History

2014-02-18 12:44:41 +06:00
// Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
2005-12-05 21:23:52 +05:00
//
2012-08-09 13:58:02 +06:00
// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
2005-12-05 21:23:52 +05:00
//
2012-08-09 13:58:02 +06:00
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
2014-02-18 12:44:41 +06:00
// version 2.1 of the License, or (at your option) any later version.
2009-02-13 17:16:39 +05:00
//
2012-08-09 13:58:02 +06:00
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
2009-02-13 17:16:39 +05:00
//
2012-08-09 13:58:02 +06:00
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2009-02-13 17:16:39 +05:00
//
2012-08-09 13:58:02 +06:00
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
2013-04-01 18:25:01 +06:00
//
2012-08-09 13:58:02 +06:00
// File: BlockFix.cxx
// Created: Tue Dec 7 11:59:05 2004
// Author: Pavel DURANDIN
2013-02-12 17:35:16 +06:00
#include <BlockFix_SphereSpaceModifier.hxx>
#include <ShapeAnalysis.hxx>
#include <ShapeFix_Edge.hxx>
#include <TopExp.hxx>
2005-01-20 11:24:17 +05:00
#include <TopLoc_Location.hxx>
2013-02-12 17:35:16 +06:00
#include <TopoDS.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Vertex.hxx>
2005-01-20 11:24:17 +05:00
#include <BRep_Tool.hxx>
2013-02-12 17:35:16 +06:00
#include <BRep_Builder.hxx>
#include <BRepGProp.hxx>
#include <GProp_GProps.hxx>
2005-01-20 11:24:17 +05:00
#include <Geom_SphericalSurface.hxx>
#include <Geom_RectangularTrimmedSurface.hxx>
2013-02-12 17:35:16 +06:00
2005-01-20 11:24:17 +05:00
#include <Geom_Curve.hxx>
2013-02-12 17:35:16 +06:00
#include <Geom_Surface.hxx>
2005-01-20 11:24:17 +05:00
#include <Geom2d_Curve.hxx>
2013-02-12 17:35:16 +06:00
#include <gp_Pnt.hxx>
#include <gp_Sphere.hxx>
IMPLEMENT_STANDARD_HANDLE(BlockFix_SphereSpaceModifier, BRepTools_Modification);
IMPLEMENT_STANDARD_RTTIEXT(BlockFix_SphereSpaceModifier, BRepTools_Modification);
2005-01-20 11:24:17 +05:00
//=======================================================================
//function : BlockFix_SphereSpaceModifier
2012-08-09 13:58:02 +06:00
//purpose :
2005-01-20 11:24:17 +05:00
//=======================================================================
BlockFix_SphereSpaceModifier::BlockFix_SphereSpaceModifier()
{
myMapOfFaces.Clear();
myMapOfSpheres.Clear();
}
//=======================================================================
2013-02-12 17:35:16 +06:00
//function : ~BlockFix_SphereSpaceModifier
2012-08-09 13:58:02 +06:00
//purpose :
2005-01-20 11:24:17 +05:00
//=======================================================================
2013-02-12 17:35:16 +06:00
BlockFix_SphereSpaceModifier::~BlockFix_SphereSpaceModifier() {}
2005-01-20 11:24:17 +05:00
2013-02-12 17:35:16 +06:00
//=======================================================================
//function : SetTolerance
//purpose :
//=======================================================================
2005-01-20 11:24:17 +05:00
void BlockFix_SphereSpaceModifier::SetTolerance(const Standard_Real Tol)
{
myTolerance = Tol;
}
//=======================================================================
//function : NewSurface
2012-08-09 13:58:02 +06:00
//purpose :
2005-01-20 11:24:17 +05:00
//=======================================================================
static Standard_Boolean ModifySurface(const TopoDS_Face& aFace,
const Handle(Geom_Surface)& aSurface,
Handle(Geom_Surface)& aNewSurface)
{
Handle(Geom_Surface) S = aSurface;
if(S->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
2012-08-09 13:58:02 +06:00
Handle(Geom_RectangularTrimmedSurface) RTS =
2005-01-20 11:24:17 +05:00
Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
S = RTS->BasisSurface();
}
2012-08-09 13:58:02 +06:00
2005-01-20 11:24:17 +05:00
if(S->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
Standard_Real Umin, Umax, Vmin, Vmax;
ShapeAnalysis::GetFaceUVBounds(aFace,Umin, Umax, Vmin, Vmax);
2012-08-09 13:58:02 +06:00
Standard_Real PI2 = M_PI/2.;
2005-01-20 11:24:17 +05:00
if(Vmax > PI2 - Precision::PConfusion() || Vmin < -PI2+::Precision::PConfusion()) {
2005-03-15 17:06:31 +05:00
Handle(Geom_SphericalSurface) aSphere = Handle(Geom_SphericalSurface)::DownCast(S);
gp_Sphere sp = aSphere->Sphere();
2012-12-13 14:40:36 +06:00
//modified by jgv, 12.11.2012 for issue 21777//
Standard_Real Radius = sp.Radius();
Standard_Real HalfArea = 2.*M_PI*Radius*Radius;
GProp_GProps Properties;
BRepGProp::SurfaceProperties(aFace, Properties);
Standard_Real anArea = Properties.Mass();
Standard_Real AreaTol = Radius*Radius*1.e-6;
if (anArea > HalfArea - AreaTol) //no chance to avoid singularity
2013-06-17 18:15:50 +06:00
return Standard_False;
2012-12-13 14:40:36 +06:00
///////////////////////////////////////////////
2005-03-15 17:06:31 +05:00
gp_Ax3 ax3 = sp.Position();
2005-01-20 11:24:17 +05:00
if(Abs(Vmax-Vmin) < PI2) {
gp_Ax3 axnew3(ax3.Axis().Location(), ax3.Direction()^ax3.XDirection(),ax3.XDirection());
sp.SetPosition(axnew3);
Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
aNewSurface = aNewSphere;
return Standard_True;
}
2005-03-15 17:06:31 +05:00
else {
gp_Pnt PC = ax3.Location();
Standard_Real Vpar;
if(fabs(PI2-Vmax)>fabs(-PI2-Vmin))
Vpar = (PI2+Vmax)/2.;
else
Vpar = (-PI2+Vmin)/2.;
Standard_Real Upar = (Umin+Umax)/2.;;
gp_Pnt PN,PX;
S->D0(Upar,Vpar,PN);
S->D0(Upar+PI2,0.,PX);
gp_Dir newNorm(gp_Vec(PC,PN));
gp_Dir newDirX(gp_Vec(PC,PX));
gp_Ax3 axnew3(ax3.Axis().Location(), newNorm, newDirX);
sp.SetPosition(axnew3);
Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(sp);
aNewSurface = aNewSphere;
return Standard_True;
}
2005-01-20 11:24:17 +05:00
}
}
return Standard_False;
}
2012-08-09 13:58:02 +06:00
2005-01-20 11:24:17 +05:00
Standard_Boolean BlockFix_SphereSpaceModifier::NewSurface(const TopoDS_Face& F,
Handle(Geom_Surface)& S,
TopLoc_Location& L,Standard_Real& Tol,
Standard_Boolean& RevWires,
2012-08-09 13:58:02 +06:00
Standard_Boolean& RevFace)
2005-01-20 11:24:17 +05:00
{
TopLoc_Location LS;
Handle(Geom_Surface) SIni = BRep_Tool::Surface(F, LS);
2012-08-09 13:58:02 +06:00
2005-01-20 11:24:17 +05:00
//check if pole of the sphere in the parametric space
if(ModifySurface(F, SIni, S)) {
2012-08-09 13:58:02 +06:00
2005-01-20 11:24:17 +05:00
RevWires = Standard_False;
RevFace = Standard_False;
2012-08-09 13:58:02 +06:00
2005-01-20 11:24:17 +05:00
L = LS;
Tol = BRep_Tool::Tolerance(F);
2012-08-09 13:58:02 +06:00
2005-01-20 11:24:17 +05:00
Standard_Integer anIndex = myMapOfSpheres.Add(S);
myMapOfFaces.Bind(F,anIndex);
return Standard_True;
}
2012-08-09 13:58:02 +06:00
2005-01-20 11:24:17 +05:00
return Standard_False;
}
//=======================================================================
//function : NewCurve
2012-08-09 13:58:02 +06:00
//purpose :
2005-01-20 11:24:17 +05:00
//=======================================================================
Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve(const TopoDS_Edge& /*E*/,Handle(Geom_Curve)& /*C*/,
2012-08-09 13:58:02 +06:00
TopLoc_Location& /*L*/,Standard_Real& /*Tol*/)
2005-01-20 11:24:17 +05:00
{
return Standard_False;
}
//=======================================================================
//function : NewPoint
2012-08-09 13:58:02 +06:00
//purpose :
2005-01-20 11:24:17 +05:00
//=======================================================================
Standard_Boolean BlockFix_SphereSpaceModifier::NewPoint(const TopoDS_Vertex& /*V*/,
gp_Pnt& /*P*/,
2012-08-09 13:58:02 +06:00
Standard_Real& /*Tol*/)
2005-01-20 11:24:17 +05:00
{
return Standard_False;
}
//=======================================================================
//function : NewCurve2d
2012-08-09 13:58:02 +06:00
//purpose :
2005-01-20 11:24:17 +05:00
//=======================================================================
Standard_Boolean BlockFix_SphereSpaceModifier::NewCurve2d(const TopoDS_Edge& E,const TopoDS_Face& F,
const TopoDS_Edge& /*NewE*/,const TopoDS_Face& /*NewF*/,
2012-08-09 13:58:02 +06:00
Handle(Geom2d_Curve)& C,Standard_Real& Tol)
2005-01-20 11:24:17 +05:00
{
//check if undelying surface of the face was modified
if(myMapOfFaces.IsBound(F)) {
Standard_Integer anIndex = myMapOfFaces.Find(F);
2012-08-09 13:58:02 +06:00
2005-01-20 11:24:17 +05:00
Handle(Geom_Surface) aNewSphere = Handle(Geom_Surface)::DownCast(myMapOfSpheres.FindKey(anIndex));
2012-08-09 13:58:02 +06:00
2005-01-20 11:24:17 +05:00
Standard_Real f,l;
TopLoc_Location LC, LS;
Handle(Geom_Curve) C3d = BRep_Tool::Curve ( E, LC, f, l );
Handle(Geom_Surface) S = BRep_Tool::Surface(F, LS);
2012-08-09 13:58:02 +06:00
2005-01-20 11:24:17 +05:00
//taking into accound the orientation of the seam
C = BRep_Tool::CurveOnSurface(E,F,f,l);
Tol = BRep_Tool::Tolerance(E);
2012-08-09 13:58:02 +06:00
2005-01-20 11:24:17 +05:00
BRep_Builder B;
TopoDS_Edge TempE;
B.MakeEdge(TempE);
B.Add(TempE, TopExp::FirstVertex(E));
B.Add(TempE, TopExp::LastVertex(E));
2012-08-09 13:58:02 +06:00
if(!C3d.IsNull())
2005-01-20 11:24:17 +05:00
B.UpdateEdge(TempE, Handle(Geom_Curve)::DownCast(C3d->Transformed(LC.Transformation())), Precision::Confusion());
B.Range(TempE, f, l);
2012-08-09 13:58:02 +06:00
2005-01-20 11:24:17 +05:00
Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
Handle(Geom_Surface) STemp = Handle(Geom_Surface)::DownCast(aNewSphere->Transformed(LS.Transformation()));
TopLoc_Location LTemp;
LTemp.Identity();
2012-08-09 13:58:02 +06:00
2005-01-20 11:24:17 +05:00
Standard_Boolean isClosed = BRep_Tool::IsClosed (E, F);
Standard_Real aWorkTol = 2*myTolerance+Tol;
sfe->FixAddPCurve(TempE, STemp, LTemp, isClosed, Max(Precision::Confusion(), aWorkTol));
sfe->FixSameParameter(TempE);
2012-08-09 13:58:02 +06:00
2005-01-20 11:24:17 +05:00
//keep the orientation of original edge
TempE.Orientation(E.Orientation());
C = BRep_Tool::CurveOnSurface(TempE, STemp, LTemp, f, l);
2012-08-09 13:58:02 +06:00
2005-01-20 11:24:17 +05:00
// shifting seam of sphere
if(isClosed && !C.IsNull()) {
Standard_Real f2,l2;
2012-08-09 13:58:02 +06:00
Handle(Geom2d_Curve) c22 =
2005-01-20 11:24:17 +05:00
BRep_Tool::CurveOnSurface(TopoDS::Edge(TempE.Reversed()),STemp, LTemp,f2,l2);
Standard_Real dPreci = Precision::PConfusion()*Precision::PConfusion();
if((C->Value(f).SquareDistance(c22->Value(f2)) < dPreci)
||(C->Value(l).SquareDistance(c22->Value(l2)) < dPreci)) {
gp_Vec2d shift(S->UPeriod(),0.);
C->Translate(shift);
}
}
//sphere was modified
return Standard_True;
}
return Standard_False;
}
//=======================================================================
//function : NewParameter
2012-08-09 13:58:02 +06:00
//purpose :
2005-01-20 11:24:17 +05:00
//=======================================================================
Standard_Boolean BlockFix_SphereSpaceModifier::NewParameter(const TopoDS_Vertex& /*V*/,const TopoDS_Edge& /*E*/,
2012-08-09 13:58:02 +06:00
Standard_Real& /*P*/,Standard_Real& /*Tol*/)
2005-01-20 11:24:17 +05:00
{
return Standard_False;
}
//=======================================================================
//function : Continuity
2012-08-09 13:58:02 +06:00
//purpose :
2005-01-20 11:24:17 +05:00
//=======================================================================
GeomAbs_Shape BlockFix_SphereSpaceModifier::Continuity(const TopoDS_Edge& E,const TopoDS_Face& F1,
const TopoDS_Face& F2,const TopoDS_Edge& /*NewE*/,
2012-08-09 13:58:02 +06:00
const TopoDS_Face& /*NewF1*/,const TopoDS_Face& /*NewF2*/)
2005-01-20 11:24:17 +05:00
{
return BRep_Tool::Continuity(E,F1,F2);
}