Salome HOME
Update copyrights
[modules/smesh.git] / src / StdMeshers_I / StdMeshers_CartesianParameters3D_i.cxx
1 // Copyright (C) 2007-2019  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, or (at your option) any later version.
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 //  File   : StdMeshers_CartesianParameters3D_i.cxx
23 //  Module : SMESH
24 //
25 #include "StdMeshers_CartesianParameters3D_i.hxx"
26
27 #include "StdMeshers_CartesianParameters3D.hxx"
28 #include "SMESH_Gen_i.hxx"
29 #include "SMESH_Gen.hxx"
30 #include "SMESH_PythonDump.hxx"
31
32 #include "Utils_CorbaException.hxx"
33 #include "utilities.h"
34
35 #define _vec2array( v, a,conversion )           \
36   {                                             \
37     a->length( v.size() );                      \
38     for ( size_t i = 0; i < v.size(); ++i )     \
39       a[i] = conversion( v[i] );                \
40   }
41 #define _array2vec(a,v,conversion)              \
42   {                                             \
43     v.resize( a.length() );                     \
44     for ( size_t i = 0; i < v.size(); ++i )     \
45       v[i] = conversion ( a[i] );               \
46   }
47 namespace
48 {
49   const char* _string2chars(const std::string& s ) { return s.c_str(); }
50 }
51
52 //=============================================================================
53 /*!
54  *  StdMeshers_CartesianParameters3D_i::StdMeshers_CartesianParameters3D_i
55  *
56  *  Constructor
57  */
58 //=============================================================================
59
60 StdMeshers_CartesianParameters3D_i::
61 StdMeshers_CartesianParameters3D_i( PortableServer::POA_ptr thePOA,
62                                     ::SMESH_Gen*            theGenImpl )
63   : SALOME::GenericObj_i( thePOA ), 
64     SMESH_Hypothesis_i( thePOA )
65 {
66   myBaseImpl = new ::StdMeshers_CartesianParameters3D( theGenImpl->GetANewId(),
67                                                        theGenImpl );
68 }
69
70 //=============================================================================
71 /*!
72  *  StdMeshers_CartesianParameters3D_i::~StdMeshers_CartesianParameters3D_i
73  *
74  *  Destructor
75  */
76 //=============================================================================
77
78 StdMeshers_CartesianParameters3D_i::~StdMeshers_CartesianParameters3D_i()
79 {
80 }
81
82 //=============================================================================
83 /*!
84  * SetGrid
85  */
86 //=============================================================================
87
88 void StdMeshers_CartesianParameters3D_i::SetGrid(const SMESH::double_array& coords,
89                                                  CORBA::Short               axis)
90   throw (SALOME::SALOME_Exception)
91 {
92   std::vector<double> coordVec;//, yCoords, zCoords;
93   _array2vec( coords, coordVec, );
94
95   ASSERT( myBaseImpl );
96   try {
97     this->GetImpl()->SetGrid( coordVec, axis );
98   }
99   catch ( SALOME_Exception& S_ex ) {
100     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
101   }
102
103   // Update Python script
104   SMESH::TPythonDump() << _this() << ".SetGrid( " << coords << ", " << axis << " )";
105 }
106
107 //=============================================================================
108 /*!
109  *  GetGrid
110  */
111 //=============================================================================
112
113 SMESH::double_array* StdMeshers_CartesianParameters3D_i::GetGrid(CORBA::Short axis)
114   throw (SALOME::SALOME_Exception)
115 {
116   std::vector<double> coordVec;
117   ASSERT( myBaseImpl );
118   try {
119     this->GetImpl()->GetGrid(coordVec, axis);
120   }
121   catch ( SALOME_Exception& S_ex ) {
122     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
123   }
124
125   SMESH::double_array_var coords = new SMESH::double_array();
126   _vec2array( coordVec, coords, );
127
128   return coords._retn();
129 }
130
131 //=============================================================================
132 /*!
133  *  SetSizeThreshold
134  */
135 //=============================================================================
136
137 void StdMeshers_CartesianParameters3D_i::SetSizeThreshold(CORBA::Double threshold)
138   throw (SALOME::SALOME_Exception)
139 {
140   ASSERT( myBaseImpl );
141   try {
142     this->GetImpl()->SetSizeThreshold(threshold);
143   }
144   catch ( SALOME_Exception& S_ex ) {
145     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
146   }
147
148   // Update Python script
149   SMESH::TPythonDump() << _this() << ".SetSizeThreshold( " << SMESH::TVar(threshold) << " )";
150 }
151
152 //=============================================================================
153 /*!
154  *  GetSizeThreshold
155  */
156 //=============================================================================
157
158 CORBA::Double StdMeshers_CartesianParameters3D_i::GetSizeThreshold()
159 {
160   return this->GetImpl()->GetSizeThreshold();
161 }
162
163 //=======================================================================
164 //function : SetGridSpacing
165 //\brief Set grid spacing along the three axes
166 // \param spaceFunctions - functions defining spacing values at given point on axis
167 // \param internalPoints - points dividing a grid into parts along each direction
168 // Parameter t of spaceFunction f(t) is a position [0,1] within bounding box of
169 // the shape to mesh or within an interval defined by internal points
170 //=======================================================================
171
172 void StdMeshers_CartesianParameters3D_i::SetGridSpacing(const SMESH::string_array& spaceFunctions,
173                                                         const SMESH::double_array& internalPoints,
174                                                         CORBA::Short               axis)
175   throw (SALOME::SALOME_Exception)
176 {
177   std::vector<std::string> funVec;
178   std::vector<double>      pointVec;
179   _array2vec( spaceFunctions, funVec, (const char*) );
180   _array2vec( internalPoints, pointVec, );
181
182   ASSERT( myBaseImpl );
183   try {
184     this->GetImpl()->SetGridSpacing( funVec, pointVec, axis );
185   }
186   catch ( SALOME_Exception& S_ex ) {
187     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
188   }
189
190   // Update Python script
191   SMESH::TPythonDump() << _this() << ".SetGridSpacing( "
192                        << spaceFunctions << ", "
193                        << internalPoints << ", "
194                        << axis << " )";
195 }
196
197 //=======================================================================
198 //function : GetGridSpacing
199 //=======================================================================
200
201 void StdMeshers_CartesianParameters3D_i::GetGridSpacing(SMESH::string_array_out xSpaceFunctions,
202                                                         SMESH::double_array_out xInternalPoints,
203                                                         CORBA::Short            axis)
204   throw (SALOME::SALOME_Exception)
205 {
206   ASSERT( myBaseImpl );
207   try {
208     std::vector<std::string> funVec;
209     std::vector<double>      pointVec;
210     this->GetImpl()->GetGridSpacing( funVec, pointVec, axis );
211
212     xSpaceFunctions = new SMESH::string_array();
213     xInternalPoints = new SMESH::double_array();
214
215     _vec2array( funVec, xSpaceFunctions, _string2chars );
216     _vec2array( pointVec, xInternalPoints, );
217   }
218   catch ( SALOME_Exception& S_ex ) {
219     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
220   }
221 }
222
223 //=======================================================================
224 //function : SetAxesDirs
225 //purpose  : Set custom direction of axes
226 //=======================================================================
227
228 void StdMeshers_CartesianParameters3D_i::SetAxesDirs(const SMESH::DirStruct& xDir,
229                                                      const SMESH::DirStruct& yDir,
230                                                      const SMESH::DirStruct& zDir)
231   throw (SALOME::SALOME_Exception)
232 {
233   double coords[9];
234   coords[0] = xDir.PS.x;
235   coords[1] = xDir.PS.y;
236   coords[2] = xDir.PS.z;
237   coords[3] = yDir.PS.x;
238   coords[4] = yDir.PS.y;
239   coords[5] = yDir.PS.z;
240   coords[6] = zDir.PS.x;
241   coords[7] = zDir.PS.y;
242   coords[8] = zDir.PS.z;
243   try {
244     this->GetImpl()->SetAxisDirs(coords);
245
246     SMESH::TPythonDump() << _this() << ".SetAxesDirs( "
247                          << xDir << ", "
248                          << yDir << ", "
249                          << zDir << " )";
250   }
251   catch ( SALOME_Exception& S_ex ) {
252     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
253   }
254 }
255
256 //=======================================================================
257 //function : GetAxesDirs
258 //purpose  : Returns direction of axes
259 //=======================================================================
260
261 void StdMeshers_CartesianParameters3D_i::GetAxesDirs(SMESH::DirStruct& xDir,
262                                                      SMESH::DirStruct& yDir,
263                                                      SMESH::DirStruct& zDir)
264 {
265   const double* coords = GetImpl()->GetAxisDirs();
266   xDir.PS.x = coords[0];
267   xDir.PS.y = coords[1];
268   xDir.PS.z = coords[2];
269   yDir.PS.x = coords[3];
270   yDir.PS.y = coords[4];
271   yDir.PS.z = coords[5];
272   zDir.PS.x = coords[6];
273   zDir.PS.y = coords[7];
274   zDir.PS.z = coords[8];
275 }
276
277 //=======================================================================
278 //function : SetFixedPoint
279 //purpose  : * Set/unset a fixed point, at which a node will be created provided that grid
280 //           * is defined by spacing in all directions
281 //=======================================================================
282
283 void StdMeshers_CartesianParameters3D_i::SetFixedPoint(const SMESH::PointStruct& ps,
284                                                        CORBA::Boolean            toUnset)
285 {
286   double p[3] = { ps.x, ps.y, ps.z };
287   GetImpl()->SetFixedPoint( p, toUnset );
288
289   SMESH::TPythonDump() << _this() << ".SetFixedPoint( " << ps << ", " << toUnset << " )";
290 }
291
292 //=======================================================================
293 //function : GetFixedPoint
294 //purpose  : Returns a fixed point
295 //=======================================================================
296
297 CORBA::Boolean StdMeshers_CartesianParameters3D_i::GetFixedPoint(SMESH::PointStruct& ps)
298 {
299   double p[3];
300   if ( GetImpl()->GetFixedPoint( p ) )
301   {
302     ps.x = p[0];
303     ps.y = p[1];
304     ps.z = p[2];
305     return true;
306   }
307   else
308   {
309     ps.x = 0.;
310     ps.y = 0.;
311     ps.z = 0.;
312   }
313   return false;
314 }
315
316 //=======================================================================
317 //function : SetToAddEdges
318 //purpose  : Enables implementation of geometrical edges into the mesh.
319 //=======================================================================
320
321 void StdMeshers_CartesianParameters3D_i::SetToAddEdges(CORBA::Boolean toAdd)
322 {
323   GetImpl()->SetToAddEdges( toAdd );
324   SMESH::TPythonDump() << _this() << ".SetToAddEdges( " << toAdd << " )";
325 }
326
327 //=======================================================================
328 //function : GetToAddEdges
329 //purpose  : Returns true if implementation of geometrical edges into the
330 //           mesh is enabled
331 //=======================================================================
332
333 CORBA::Boolean StdMeshers_CartesianParameters3D_i::GetToAddEdges()
334 {
335   return GetImpl()->GetToAddEdges();
336 }
337
338 //=======================================================================
339 //function : IsGridBySpacing
340 //purpose  : Return true if the grid is defined by spacing functions and
341 //           not by node coordinates
342 //=======================================================================
343
344 CORBA::Boolean StdMeshers_CartesianParameters3D_i::IsGridBySpacing(CORBA::Short axis)
345 {
346   return this->GetImpl()->IsGridBySpacing(axis);
347 }
348
349 //=======================================================================
350 //function : ComputeOptimalAxesDirs
351 //purpose  : Returns axes at which number of hexahedra is maximal
352 //=======================================================================
353
354 void StdMeshers_CartesianParameters3D_i::
355 ComputeOptimalAxesDirs(GEOM::GEOM_Object_ptr go,
356                        CORBA::Boolean        isOrthogonal,
357                        SMESH::DirStruct&     xDir,
358                        SMESH::DirStruct&     yDir,
359                        SMESH::DirStruct&     zDir)
360   throw (SALOME::SALOME_Exception)
361 {
362   TopoDS_Shape shape = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( go );
363   if ( shape.IsNull() )
364     THROW_SALOME_CORBA_EXCEPTION( "Null shape", SALOME::BAD_PARAM );
365
366   double c[9];
367   ::StdMeshers_CartesianParameters3D::ComputeOptimalAxesDirs( shape, isOrthogonal, c );
368
369   xDir.PS.x = c[0];
370   xDir.PS.y = c[1];
371   xDir.PS.z = c[2];
372   yDir.PS.x = c[3];
373   yDir.PS.y = c[4];
374   yDir.PS.z = c[5];
375   zDir.PS.x = c[6];
376   zDir.PS.y = c[7];
377   zDir.PS.z = c[8];
378 }
379
380 //=======================================================================
381 //function : ComputeCoordinates
382 //purpose  : Computes node coordinates by spacing functions
383 //=======================================================================
384
385 SMESH::double_array*
386 StdMeshers_CartesianParameters3D_i::ComputeCoordinates(CORBA::Double              x0,
387                                                        CORBA::Double              x1,
388                                                        const SMESH::string_array& spaceFuns,
389                                                        const SMESH::double_array& points,
390                                                        const char*                axisName )
391   throw (SALOME::SALOME_Exception)
392 {
393   std::vector<std::string> xFuns;
394   std::vector<double>      xPoints, coords;
395   _array2vec( spaceFuns, xFuns, (const char*) );
396   _array2vec( points, xPoints, );
397
398   try {
399     this->GetImpl()->ComputeCoordinates( x0, x1, xFuns, xPoints, coords, axisName );
400   }
401   catch ( SALOME_Exception& S_ex ) {
402     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
403   }
404   SMESH::double_array_var res = new SMESH::double_array;
405   _vec2array( coords, res,  );
406
407   return res._retn();
408 }
409
410 //=============================================================================
411 /*!
412  *  Get implementation
413  */
414 //=============================================================================
415
416 ::StdMeshers_CartesianParameters3D* StdMeshers_CartesianParameters3D_i::GetImpl()
417 {
418   return ( ::StdMeshers_CartesianParameters3D* )myBaseImpl;
419 }
420
421 //================================================================================
422 /*!
423  * \brief Verify whether hypothesis supports given entity type 
424   * \param type - dimension (see SMESH::Dimension enumeration)
425   * \retval CORBA::Boolean - TRUE if dimension is supported, FALSE otherwise
426  * 
427  * Verify whether hypothesis supports given entity type (see SMESH::Dimension enumeration)
428  */
429 //================================================================================  
430
431 CORBA::Boolean StdMeshers_CartesianParameters3D_i::IsDimSupported( SMESH::Dimension type )
432 {
433   return type == SMESH::DIM_3D;
434 }