Salome HOME
Update copyright information
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPlugin_SimpleHypothesis_2D.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  NETGENPlugin : C++ implementation
23 // File      : NETGENPlugin_SimpleHypothesis_2D.cxx
24 // Author    : Edward AGAPOV
25 // Project   : SALOME
26 //=============================================================================
27 //
28 #include "NETGENPlugin_SimpleHypothesis_2D.hxx"
29 #include "NETGENPlugin_Hypothesis.hxx"
30
31 #include <SMESH_Mesh.hxx>
32 #include <SMESH_subMesh.hxx>
33 #include <SMESH_ControlsDef.hxx>
34
35 #include <TopExp_Explorer.hxx>
36
37 #include <utilities.h>
38
39 using namespace std;
40
41 //=============================================================================
42 /*!
43  *  
44  */
45 //=============================================================================
46 NETGENPlugin_SimpleHypothesis_2D::NETGENPlugin_SimpleHypothesis_2D (int         hypId,
47                                                                     int         studyId,
48                                                                     SMESH_Gen * gen)
49   : SMESH_Hypothesis(hypId, studyId, gen),
50     _nbSegments ((int)NETGENPlugin_Hypothesis::GetDefaultNbSegPerEdge()),
51     _segmentLength(0),
52     _area       (0.)
53 {
54   _name = "NETGEN_SimpleParameters_2D";
55   _param_algo_dim = 2;
56 }
57
58 //=============================================================================
59 /*!
60  *  
61  */
62 //=============================================================================
63 void NETGENPlugin_SimpleHypothesis_2D::SetNumberOfSegments(int nb) throw (SALOME_Exception)
64 {
65   if ( nb < 1 )
66     throw SALOME_Exception("Number of segments must be positive");
67   if (nb != _nbSegments)
68   {
69     _nbSegments = nb;
70     if ( _nbSegments ) _segmentLength = 0.;
71     NotifySubMeshesHypothesisModification();
72   }
73 }
74
75 //=============================================================================
76 /*!
77  *  
78  */
79 //=============================================================================
80 void NETGENPlugin_SimpleHypothesis_2D::SetLocalLength(double segmentLength)
81   throw (SALOME_Exception)
82 {
83   if ( segmentLength < DBL_MIN )
84     throw SALOME_Exception("segment length must be more than zero");
85   if (segmentLength != _segmentLength)
86   {
87     _segmentLength = segmentLength;
88     if ( _segmentLength > DBL_MIN ) _nbSegments = 0;
89     NotifySubMeshesHypothesisModification();
90   }
91 }
92
93 //=============================================================================
94 /*!
95  *  
96  */
97 //=============================================================================
98 void NETGENPlugin_SimpleHypothesis_2D::LengthFromEdges()
99 {
100   if (_area > DBL_MIN )
101   {
102     _area = 0;
103     NotifySubMeshesHypothesisModification();
104   }
105 }
106
107 //=============================================================================
108 /*!
109  *  
110  */
111 //=============================================================================
112 void NETGENPlugin_SimpleHypothesis_2D::SetMaxElementArea(double area)
113 {
114   if ( area < DBL_MIN )
115     area = 0.;
116   if (_area != area)
117   {
118     _area = area;
119     NotifySubMeshesHypothesisModification();
120   }
121 }
122
123 //=============================================================================
124 /*!
125  *  
126  */
127 //=============================================================================
128 ostream & NETGENPlugin_SimpleHypothesis_2D::SaveTo(ostream & save)
129 {
130   save << _nbSegments << " " << _segmentLength << " " << _area;
131
132   return save;
133 }
134
135 //=============================================================================
136 /*!
137  *  
138  */
139 //=============================================================================
140 istream & NETGENPlugin_SimpleHypothesis_2D::LoadFrom(istream & load)
141 {
142   bool isOK = true;
143   double val;
144
145   isOK = (load >> val);
146   if (isOK)
147     _nbSegments = (int) val;
148   else
149     load.clear(ios::badbit | load.rdstate());
150
151   isOK = (load >> val);
152   if (isOK)
153     _segmentLength = val;
154   else
155     load.clear(ios::badbit | load.rdstate());
156
157   isOK = (load >> val);
158   if (isOK)
159     _area = val;
160   else
161     load.clear(ios::badbit | load.rdstate());
162
163   return load;
164 }
165
166 //================================================================================
167 /*!
168  * \brief Does nothing
169  * \param theMesh - the built mesh
170  * \param theShape - the geometry of interest
171  * \retval bool - always false
172  */
173 //================================================================================
174 bool NETGENPlugin_SimpleHypothesis_2D::SetParametersByMesh(const SMESH_Mesh*   theMesh,
175                                                            const TopoDS_Shape& theShape)
176 {
177   // Find out nb of segments.
178   int nbSeg = 0, nbEdges = 0;
179   TopExp_Explorer exp( theShape, TopAbs_EDGE );
180   for ( ; exp.More(); exp.Next() ) {
181     SMESH_subMesh* sm = theMesh->GetSubMeshContaining( exp.Current() );
182     if ( sm && !sm->IsEmpty() ) {
183       nbSeg += sm->GetSubMeshDS()->NbElements();
184       nbEdges++;
185     }
186   }
187   if ( nbEdges )
188     _nbSegments = nbSeg / nbEdges;
189
190   // Find out max face area
191   _area = 0;
192   SMESH::Controls::Area areaControl;
193   SMESH::Controls::TSequenceOfXYZ nodesCoords;
194   const int nbFacesToCheck = 100;
195   for ( exp.Init( theShape, TopAbs_FACE ); exp.More(); exp.Next() ) {
196     SMESH_subMesh* sm = theMesh->GetSubMeshContaining( exp.Current() );
197     if ( sm && !sm->IsEmpty() ) {
198       SMDS_ElemIteratorPtr fIt = sm->GetSubMeshDS()->GetElements();
199       int nbCheckedFaces = 0;
200       while ( fIt->more() && nbCheckedFaces++ < nbFacesToCheck ) {
201         const SMDS_MeshElement* elem = fIt->next();
202         areaControl.GetPoints( elem, nodesCoords );
203         _area = max( _area, areaControl.GetValue( nodesCoords ));
204       }
205     }
206   }
207   return nbEdges;
208 }