Salome HOME
4e3d0cca889e942e2651f0e758d54decddf7a32e
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPlugin_SimpleHypothesis_2D.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, 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 <SMESHDS_SubMesh.hxx>
30 #include <SMESH_ControlsDef.hxx>
31 #include <SMESH_Mesh.hxx>
32 #include <SMESH_subMesh.hxx>
33
34 #include <TopExp_Explorer.hxx>
35
36 #include <utilities.h>
37
38 using namespace std;
39
40 //=============================================================================
41 /*!
42  *  
43  */
44 //=============================================================================
45 NETGENPlugin_SimpleHypothesis_2D::NETGENPlugin_SimpleHypothesis_2D (int         hypId,
46                                                                     SMESH_Gen * gen)
47   : SMESH_Hypothesis(hypId, 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)
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 {
81   if ( segmentLength < DBL_MIN )
82     throw SALOME_Exception("segment length must be more than zero");
83   if (segmentLength != _segmentLength)
84   {
85     _segmentLength = segmentLength;
86     if ( _segmentLength > DBL_MIN ) _nbSegments = 0;
87     NotifySubMeshesHypothesisModification();
88   }
89 }
90
91 //=============================================================================
92 /*!
93  *  
94  */
95 //=============================================================================
96 void NETGENPlugin_SimpleHypothesis_2D::LengthFromEdges()
97 {
98   if (_area > DBL_MIN )
99   {
100     _area = 0;
101     NotifySubMeshesHypothesisModification();
102   }
103 }
104
105 //=============================================================================
106 /*!
107  *  
108  */
109 //=============================================================================
110 void NETGENPlugin_SimpleHypothesis_2D::SetMaxElementArea(double area)
111 {
112   if ( area < DBL_MIN )
113     area = 0.;
114   if (_area != area)
115   {
116     _area = area;
117     NotifySubMeshesHypothesisModification();
118   }
119 }
120
121 //=======================================================================
122 //function : SetAllowQuadrangles
123 //purpose  : Enables/disables generation of quadrangular faces
124 //=======================================================================
125
126 void NETGENPlugin_SimpleHypothesis_2D::SetAllowQuadrangles(bool toAllow)
127 {
128   if ( _allowQuad != toAllow )
129   {
130     _allowQuad = toAllow;
131     NotifySubMeshesHypothesisModification();
132   }
133 }
134
135 //=======================================================================
136 //function : GetAllowQuadrangles
137 //purpose  : Returns true if generation of quadrangular faces is enabled
138 //=======================================================================
139
140 bool NETGENPlugin_SimpleHypothesis_2D::GetAllowQuadrangles() const
141 {
142   return _allowQuad;
143 }
144
145 //=============================================================================
146 /*!
147  *  
148  */
149 //=============================================================================
150 ostream & NETGENPlugin_SimpleHypothesis_2D::SaveTo(ostream & save)
151 {
152   save << _nbSegments << " " << _segmentLength << " " << _area << " " << _allowQuad;
153
154   return save;
155 }
156
157 //=============================================================================
158 /*!
159  *  
160  */
161 //=============================================================================
162 istream & NETGENPlugin_SimpleHypothesis_2D::LoadFrom(istream & load)
163 {
164   bool isOK = true;
165   double val;
166
167   isOK = static_cast<bool>(load >> val);
168   if (isOK)
169     _nbSegments = (int) val;
170   else
171     load.clear(ios::badbit | load.rdstate());
172
173   isOK = static_cast<bool>(load >> val);
174   if (isOK)
175     _segmentLength = val;
176   else
177     load.clear(ios::badbit | load.rdstate());
178
179   isOK = static_cast<bool>(load >> val);
180   if (isOK)
181     _area = val;
182   else
183     load.clear(ios::badbit | load.rdstate());
184
185   load >> _allowQuad;
186
187   return load;
188 }
189
190 //================================================================================
191 /*!
192  * \brief Does nothing
193  * \param theMesh - the built mesh
194  * \param theShape - the geometry of interest
195  * \retval bool - always false
196  */
197 //================================================================================
198 bool NETGENPlugin_SimpleHypothesis_2D::SetParametersByMesh(const SMESH_Mesh*   theMesh,
199                                                            const TopoDS_Shape& theShape)
200 {
201   // Find out nb of segments.
202   smIdType nbSeg = 0, nbEdges = 0;
203   TopExp_Explorer exp( theShape, TopAbs_EDGE );
204   for ( ; exp.More(); exp.Next() ) {
205     SMESH_subMesh* sm = theMesh->GetSubMeshContaining( exp.Current() );
206     if ( sm && !sm->IsEmpty() ) {
207       nbSeg += sm->GetSubMeshDS()->NbElements();
208       nbEdges++;
209     }
210   }
211   if ( nbEdges )
212     _nbSegments = nbSeg / nbEdges;
213
214   // Find out max face area
215   _area = 0;
216   SMESH::Controls::Area areaControl;
217   SMESH::Controls::TSequenceOfXYZ nodesCoords;
218   const int nbFacesToCheck = 100;
219   for ( exp.Init( theShape, TopAbs_FACE ); exp.More(); exp.Next() ) {
220     SMESH_subMesh* sm = theMesh->GetSubMeshContaining( exp.Current() );
221     if ( sm && !sm->IsEmpty() ) {
222       SMDS_ElemIteratorPtr fIt = sm->GetSubMeshDS()->GetElements();
223       int nbCheckedFaces = 0;
224       while ( fIt->more() && nbCheckedFaces++ < nbFacesToCheck ) {
225         const SMDS_MeshElement* elem = fIt->next();
226         areaControl.GetPoints( elem, nodesCoords );
227         _area = max( _area, areaControl.GetValue( nodesCoords ));
228       }
229     }
230   }
231   return nbEdges;
232 }
233
234 //================================================================================
235 /*!
236  * \brief Initialize my parameter values by default parameters.
237  *  \retval bool - true if parameter values have been successfully defined
238  */
239 //================================================================================
240
241 bool NETGENPlugin_SimpleHypothesis_2D::SetParametersByDefaults(const TDefaults&    dflts,
242                                                                const SMESH_Mesh* /*theMesh*/)
243 {
244   _nbSegments    = dflts._nbSegments;
245   _segmentLength = dflts._elemLength;
246   return _nbSegments && _segmentLength > 0;
247 }
248