Back to index

salome-smesh  6.5.0
StdMeshers_LocalLength.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 SMESH : implementaion of SMESH idl descriptions
00024 //  File   : StdMeshers_LocalLength.cxx
00025 //           Moved here from SMESH_LocalLength.cxx
00026 //  Author : Paul RASCLE, EDF
00027 //  Module : SMESH
00028 //
00029 #include "StdMeshers_LocalLength.hxx"
00030 
00031 #include "SMESH_Mesh.hxx"
00032 #include "SMESH_Algo.hxx"
00033 
00034 #include "utilities.h"
00035 
00036 #include <BRep_Tool.hxx>
00037 #include <GCPnts_AbscissaPoint.hxx>
00038 #include <GeomAdaptor_Curve.hxx>
00039 #include <Geom_Curve.hxx>
00040 #include <TopExp.hxx>
00041 #include <TopLoc_Location.hxx>
00042 #include <TopTools_IndexedMapOfShape.hxx>
00043 #include <TopoDS.hxx>
00044 #include <TopoDS_Edge.hxx>
00045 #include <Precision.hxx>
00046 
00047 using namespace std;
00048 
00049 //=============================================================================
00053 //=============================================================================
00054 
00055 StdMeshers_LocalLength::StdMeshers_LocalLength(int hypId, int studyId, SMESH_Gen * gen)
00056   :SMESH_Hypothesis(hypId, studyId, gen)
00057 {
00058   _length = 1.;
00059   _precision = Precision::Confusion();
00060   _name = "LocalLength";
00061   _param_algo_dim = 1; // is used by SMESH_Regular_1D
00062 }
00063 
00064 //=============================================================================
00068 //=============================================================================
00069 
00070 StdMeshers_LocalLength::~StdMeshers_LocalLength()
00071 {
00072 }
00073 
00074 //=============================================================================
00078 //=============================================================================
00079 
00080 void StdMeshers_LocalLength::SetLength(double length) throw(SALOME_Exception)
00081 {
00082   double oldLength = _length;
00083   if (length <= 0)
00084     throw SALOME_Exception(LOCALIZED("length must be positive"));
00085   _length = length;
00086   const double precision = 1e-7;
00087   if (fabs(oldLength - _length) > precision)
00088     NotifySubMeshesHypothesisModification();
00089 }
00090 
00091 //=============================================================================
00095 //=============================================================================
00096 
00097 double StdMeshers_LocalLength::GetLength() const
00098 {
00099   return _length;
00100 }
00101 
00102 //=============================================================================
00106 //=============================================================================
00107 void StdMeshers_LocalLength::SetPrecision (double thePrecision) throw(SALOME_Exception)
00108 {
00109   double oldPrecision = _precision;
00110   if (_precision < 0)
00111     throw SALOME_Exception(LOCALIZED("precision cannot be negative"));
00112   _precision = thePrecision;
00113   const double precision = 1e-8;
00114   if (fabs(oldPrecision - _precision) > precision)
00115     NotifySubMeshesHypothesisModification();
00116 }
00117 
00118 //=============================================================================
00122 //=============================================================================
00123 double StdMeshers_LocalLength::GetPrecision() const
00124 {
00125   return _precision;
00126 }
00127 
00128 //=============================================================================
00132 //=============================================================================
00133 
00134 ostream & StdMeshers_LocalLength::SaveTo(ostream & save)
00135 {
00136   save << this->_length << " " << this->_precision;
00137   return save;
00138 }
00139 
00140 //=============================================================================
00144 //=============================================================================
00145 
00146 istream & StdMeshers_LocalLength::LoadFrom(istream & load)
00147 {
00148   bool isOK = true;
00149   double a;
00150 
00151   isOK = (load >> a);
00152   if (isOK)
00153     this->_length = a;
00154   else
00155     load.clear(ios::badbit | load.rdstate());
00156 
00157   isOK = (load >> a);
00158   if (isOK)
00159     this->_precision = a;
00160   else
00161   {
00162     load.clear(ios::badbit | load.rdstate());
00163     // old format, without precision
00164     _precision = 0.;
00165   }
00166 
00167   return load;
00168 }
00169 
00170 //=============================================================================
00174 //=============================================================================
00175 
00176 ostream & operator <<(ostream & save, StdMeshers_LocalLength & hyp)
00177 {
00178   return hyp.SaveTo( save );
00179 }
00180 
00181 //=============================================================================
00185 //=============================================================================
00186 
00187 istream & operator >>(istream & load, StdMeshers_LocalLength & hyp)
00188 {
00189   return hyp.LoadFrom( load );
00190 }
00191 
00192 //================================================================================
00199 //================================================================================
00200 
00201 bool StdMeshers_LocalLength::SetParametersByMesh(const SMESH_Mesh*   theMesh,
00202                                                  const TopoDS_Shape& theShape)
00203 {
00204   if ( !theMesh || theShape.IsNull() )
00205     return false;
00206 
00207   _length = 0.;
00208 
00209   Standard_Real UMin, UMax;
00210   TopLoc_Location L;
00211 
00212   int nbEdges = 0;
00213   TopTools_IndexedMapOfShape edgeMap;
00214   TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
00215   for ( int iE = 1; iE <= edgeMap.Extent(); ++iE )
00216   {
00217     const TopoDS_Edge& edge = TopoDS::Edge( edgeMap( iE ));
00218     Handle(Geom_Curve) C = BRep_Tool::Curve( edge, L, UMin, UMax );
00219     GeomAdaptor_Curve AdaptCurve(C, UMin, UMax);
00220 
00221     vector< double > params;
00222     SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
00223     if ( SMESH_Algo::GetNodeParamOnEdge( aMeshDS, edge, params ))
00224     {
00225       for ( int i = 1; i < params.size(); ++i )
00226         _length += GCPnts_AbscissaPoint::Length( AdaptCurve, params[ i-1 ], params[ i ]);
00227       nbEdges += params.size() - 1;
00228     }
00229   }
00230   if ( nbEdges )
00231     _length /= nbEdges;
00232 
00233   _precision = Precision::Confusion();
00234 
00235   return nbEdges;
00236 }
00237 //================================================================================
00242 //================================================================================
00243 
00244 bool StdMeshers_LocalLength::SetParametersByDefaults(const TDefaults&  dflts,
00245                                                      const SMESH_Mesh* /*theMesh*/)
00246 {
00247   return ( _length = dflts._elemLength );
00248 }
00249