Back to index

salome-smesh  6.5.0
StdMeshers_Deflection1D.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 //  SMESH StdMeshers_Deflection1D : implementaion of SMESH idl descriptions
00024 //  File   : StdMeshers_Deflection1D.cxx
00025 //  Module : SMESH
00026 //
00027 #include "StdMeshers_Deflection1D.hxx"
00028 #include "utilities.h"
00029 
00030 #include "SMESH_Mesh.hxx"
00031 #include "SMESH_Algo.hxx"
00032 
00033 #include <BRep_Tool.hxx>
00034 #include <GeomAdaptor_Curve.hxx>
00035 #include <Geom_Curve.hxx>
00036 #include <TopExp.hxx>
00037 #include <TopLoc_Location.hxx>
00038 #include <TopTools_IndexedMapOfShape.hxx>
00039 #include <TopoDS.hxx>
00040 #include <TopoDS_Edge.hxx>
00041 #include <gp_Lin.hxx>
00042 #include <gp_Pnt.hxx>
00043 
00044 using namespace std;
00045 
00046 //=============================================================================
00050 //=============================================================================
00051 
00052 StdMeshers_Deflection1D::StdMeshers_Deflection1D(int         hypId,
00053                                                  int         studyId,
00054                                                  SMESH_Gen * gen)
00055      :SMESH_Hypothesis(hypId, studyId, gen)
00056 {
00057   _value = 1.;
00058   _name = "Deflection1D";
00059   _param_algo_dim = 1; // is used by SMESH_Regular_1D
00060 }
00061 
00062 //=============================================================================
00066 //=============================================================================
00067 
00068 StdMeshers_Deflection1D::~StdMeshers_Deflection1D()
00069 {
00070 }
00071 
00072 //=============================================================================
00076 //=============================================================================
00077 
00078 void StdMeshers_Deflection1D::SetDeflection(double value)
00079      throw(SALOME_Exception)
00080 {
00081   if (_value != value) {
00082     if (value <= 0.)
00083       throw SALOME_Exception(LOCALIZED("Value must be positive"));
00084 
00085     NotifySubMeshesHypothesisModification();
00086 
00087     _value = value;
00088   }
00089 }
00090 
00091 //=============================================================================
00095 //=============================================================================
00096 
00097 double StdMeshers_Deflection1D::GetDeflection() const
00098 {
00099   return _value;
00100 }
00101 
00102 //=============================================================================
00106 //=============================================================================
00107 
00108 ostream & StdMeshers_Deflection1D::SaveTo(ostream & save)
00109 {
00110   save << _value;
00111   return save;
00112 }
00113 
00114 //=============================================================================
00118 //=============================================================================
00119 
00120 istream & StdMeshers_Deflection1D::LoadFrom(istream & load)
00121 {
00122   bool isOK = (load >> _value);
00123   if (!isOK)
00124     load.clear(ios::badbit | load.rdstate());
00125   return load;
00126 }
00127 
00128 //=============================================================================
00132 //=============================================================================
00133 
00134 ostream & operator <<(ostream & save, StdMeshers_Deflection1D & hyp)
00135 {
00136   return hyp.SaveTo( save );
00137 }
00138 
00139 //=============================================================================
00143 //=============================================================================
00144 
00145 istream & operator >>(istream & load, StdMeshers_Deflection1D & hyp)
00146 {
00147   return hyp.LoadFrom( load );
00148 }
00149 //================================================================================
00157 //================================================================================
00158 
00159 static double deflection(const GeomAdaptor_Curve & theCurve,
00160                          double                    theU1,
00161                          double                    theU2)
00162 {
00163   if ( theCurve.GetType() == GeomAbs_Line )
00164     return 0;
00165   // line between theU1 and theU2
00166   gp_Pnt p1 = theCurve.Value( theU1 ), p2 = theCurve.Value( theU2 );
00167   gp_Lin segment( p1, gp_Vec( p1, p2 ));
00168 
00169   // evaluate square distance of theCurve from the segment
00170   Standard_Real dist2 = 0;
00171   const int nbPnt = 7;
00172   const double step = ( theU2 - theU1 ) / nbPnt;
00173   while (( theU1 += step ) < theU2 )
00174     dist2 = Max( dist2, segment.SquareDistance( theCurve.Value( theU1 )));
00175 
00176   return sqrt( dist2 );
00177 }
00178 
00179 //================================================================================
00186 //================================================================================
00187 
00188 bool StdMeshers_Deflection1D::SetParametersByMesh(const SMESH_Mesh*   theMesh,
00189                                                   const TopoDS_Shape& theShape)
00190 {
00191   if ( !theMesh || theShape.IsNull() )
00192     return false;
00193 
00194   _value = 0.;
00195 
00196   Standard_Real UMin, UMax;
00197   TopLoc_Location L;
00198 
00199   int nbEdges = 0;
00200   TopTools_IndexedMapOfShape edgeMap;
00201   TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
00202 
00203   for ( int iE = 1; iE <= edgeMap.Extent(); ++iE )
00204   {
00205     const TopoDS_Edge& edge = TopoDS::Edge( edgeMap( iE ));
00206     Handle(Geom_Curve) C = BRep_Tool::Curve( edge, L, UMin, UMax );
00207     GeomAdaptor_Curve AdaptCurve(C, UMin, UMax);
00208     if ( AdaptCurve.GetType() != GeomAbs_Line )
00209     {
00210       vector< double > params;
00211       SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
00212       if ( SMESH_Algo::GetNodeParamOnEdge( aMeshDS, edge, params ))
00213       {
00214         nbEdges++;
00215         for ( int i = 1; i < params.size(); ++i )
00216           _value = Max( _value, deflection( AdaptCurve, params[ i-1 ], params[ i ]));
00217       }
00218     }
00219     else
00220       nbEdges++;
00221   }
00222   return nbEdges;
00223 }
00224 
00225 //================================================================================
00230 //================================================================================
00231 
00232 bool StdMeshers_Deflection1D::SetParametersByDefaults(const TDefaults&  /*dflts*/,
00233                                                       const SMESH_Mesh* /*theMesh*/)
00234 {
00235   return false;
00236 }