Back to index

salome-smesh  6.5.0
StdMeshers_Arithmetic1D.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_Arithmetic1D.cxx
00025 //  Author : Damien COQUERET, OCC
00026 //  Module : SMESH
00027 //
00028 #include "StdMeshers_Arithmetic1D.hxx"
00029 
00030 #include "SMESH_Algo.hxx"
00031 #include "SMESH_Mesh.hxx"
00032 
00033 #include <BRep_Tool.hxx>
00034 #include <GCPnts_AbscissaPoint.hxx>
00035 #include <GeomAdaptor_Curve.hxx>
00036 #include <Geom_Curve.hxx>
00037 #include <TopExp.hxx>
00038 #include <TopLoc_Location.hxx>
00039 #include <TopTools_IndexedMapOfShape.hxx>
00040 #include <TopoDS.hxx>
00041 #include <TopoDS_Edge.hxx>
00042 
00043 using namespace std;
00044 
00045 //=============================================================================
00049 //=============================================================================
00050 
00051 StdMeshers_Arithmetic1D::StdMeshers_Arithmetic1D(int hypId, int studyId, SMESH_Gen * gen)
00052   :SMESH_Hypothesis(hypId, studyId, gen)
00053 {
00054   _begLength = 1.;
00055   _endLength = 10.;
00056   _name = "Arithmetic1D";
00057   _param_algo_dim = 1; 
00058 }
00059 
00060 //=============================================================================
00064 //=============================================================================
00065 
00066 StdMeshers_Arithmetic1D::~StdMeshers_Arithmetic1D()
00067 {
00068 }
00069 
00070 //=============================================================================
00074 //=============================================================================
00075 
00076 void StdMeshers_Arithmetic1D::SetLength(double length, bool isStartLength)
00077      throw(SALOME_Exception)
00078 {
00079   if ( (isStartLength ? _begLength : _endLength) != length ) {
00080     if (length <= 0)
00081       throw SALOME_Exception(LOCALIZED("length must be positive"));
00082     if ( isStartLength )
00083       _begLength = length;
00084     else
00085       _endLength = length;
00086 
00087     NotifySubMeshesHypothesisModification();
00088   }
00089 }
00090 
00091 //=============================================================================
00095 //=============================================================================
00096 
00097 double StdMeshers_Arithmetic1D::GetLength(bool isStartLength) const
00098 {
00099   return isStartLength ? _begLength : _endLength;
00100 }
00101 
00102 //=============================================================================
00106 //=============================================================================
00107 
00108 void StdMeshers_Arithmetic1D::SetReversedEdges( std::vector<int>& ids )
00109 {
00110   if ( ids != _edgeIDs ) {
00111     _edgeIDs = ids;
00112 
00113     NotifySubMeshesHypothesisModification();
00114   }
00115 }
00116 
00117 //=============================================================================
00121 //=============================================================================
00122 
00123 ostream & StdMeshers_Arithmetic1D::SaveTo(ostream & save)
00124 {
00125   int listSize = _edgeIDs.size();
00126   save << _begLength << " " << _endLength << " " << listSize;
00127 
00128   if ( listSize > 0 ) {
00129     for ( int i = 0; i < listSize; i++)
00130       save << " " << _edgeIDs[i];
00131     save << " " << _objEntry;
00132   }
00133 
00134   return save;
00135 }
00136 
00137 //=============================================================================
00141 //=============================================================================
00142 
00143 istream & StdMeshers_Arithmetic1D::LoadFrom(istream & load)
00144 {
00145   bool isOK = true;
00146   int intVal;
00147   isOK = (load >> _begLength);
00148   if (!isOK)
00149     load.clear(ios::badbit | load.rdstate());
00150   isOK = (load >> _endLength);
00151 
00152   if (!isOK)
00153     load.clear(ios::badbit | load.rdstate());
00154 
00155   isOK = (load >> intVal);
00156   if (isOK && intVal > 0) {
00157     _edgeIDs.reserve( intVal );
00158     for (int i = 0; i < _edgeIDs.capacity() && isOK; i++) {
00159       isOK = (load >> intVal);
00160       if ( isOK ) _edgeIDs.push_back( intVal );
00161     }
00162     isOK = (load >> _objEntry);
00163   }
00164 
00165   return load;
00166 }
00167 
00168 //=============================================================================
00172 //=============================================================================
00173 
00174 ostream & operator <<(ostream & save, StdMeshers_Arithmetic1D & hyp)
00175 {
00176   return hyp.SaveTo( save );
00177 }
00178 
00179 //=============================================================================
00183 //=============================================================================
00184 
00185 istream & operator >>(istream & load, StdMeshers_Arithmetic1D & hyp)
00186 {
00187   return hyp.LoadFrom( load );
00188 }
00189 
00190 //================================================================================
00197 //================================================================================
00198 
00199 bool StdMeshers_Arithmetic1D::SetParametersByMesh(const SMESH_Mesh*   theMesh,
00200                                                   const TopoDS_Shape& theShape)
00201 {
00202   if ( !theMesh || theShape.IsNull() )
00203     return false;
00204 
00205   _begLength = _endLength = 0.;
00206 
00207   Standard_Real UMin, UMax;
00208   TopLoc_Location L;
00209 
00210   int nbEdges = 0;
00211   TopTools_IndexedMapOfShape edgeMap;
00212   TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
00213   for ( int i = 1; i <= edgeMap.Extent(); ++i )
00214   {
00215     const TopoDS_Edge& edge = TopoDS::Edge( edgeMap( i ));
00216     Handle(Geom_Curve) C = BRep_Tool::Curve(edge, L, UMin, UMax);
00217     GeomAdaptor_Curve AdaptCurve(C, UMin, UMax);
00218 
00219     vector< double > params;
00220     SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
00221     if ( SMESH_Algo::GetNodeParamOnEdge( aMeshDS, edge, params ))
00222     {
00223       nbEdges++;
00224       _begLength += GCPnts_AbscissaPoint::Length( AdaptCurve, params[0], params[1]);
00225       int nb = params.size();
00226       _endLength += GCPnts_AbscissaPoint::Length( AdaptCurve, params[nb-2], params[nb-1]);
00227     }
00228   }
00229   if ( nbEdges ) {
00230     _begLength /= nbEdges;
00231     _endLength /= nbEdges;
00232   }
00233   return nbEdges;
00234 }
00235 
00236 //================================================================================
00241 //================================================================================
00242 
00243 bool StdMeshers_Arithmetic1D::SetParametersByDefaults(const TDefaults&  dflts,
00244                                                       const SMESH_Mesh* /*mesh*/)
00245 {
00246   return ( _begLength = _endLength = dflts._elemLength );
00247 }
00248