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