Salome HOME
Copyright update 2022
[modules/smesh.git] / src / StdMeshers_I / StdMeshers_CartesianParameters3D_i.cxx
1 // Copyright (C) 2007-2022  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   
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   
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   
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   
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   
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   
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
244   const double* oldCoords = GetImpl()->GetAxisDirs();
245   bool isSame = true;
246   for ( int i = 0; i < 9 &&  isSame; ++i )
247     isSame = ( oldCoords[i] == coords[i] );
248   if ( isSame )
249     return;
250
251   try {
252     this->GetImpl()->SetAxisDirs(coords);
253
254     SMESH::TPythonDump() << _this() << ".SetAxesDirs( "
255                          << xDir << ", "
256                          << yDir << ", "
257                          << zDir << " )";
258   }
259   catch ( SALOME_Exception& S_ex ) {
260     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
261   }
262 }
263
264 //=======================================================================
265 //function : GetAxesDirs
266 //purpose  : Returns direction of axes
267 //=======================================================================
268
269 void StdMeshers_CartesianParameters3D_i::GetAxesDirs(SMESH::DirStruct& xDir,
270                                                      SMESH::DirStruct& yDir,
271                                                      SMESH::DirStruct& zDir)
272 {
273   const double* coords = GetImpl()->GetAxisDirs();
274   xDir.PS.x = coords[0];
275   xDir.PS.y = coords[1];
276   xDir.PS.z = coords[2];
277   yDir.PS.x = coords[3];
278   yDir.PS.y = coords[4];
279   yDir.PS.z = coords[5];
280   zDir.PS.x = coords[6];
281   zDir.PS.y = coords[7];
282   zDir.PS.z = coords[8];
283 }
284
285 //=======================================================================
286 //function : SetFixedPoint
287 //purpose  : * Set/unset a fixed point, at which a node will be created provided that grid
288 //           * is defined by spacing in all directions
289 //=======================================================================
290
291 void StdMeshers_CartesianParameters3D_i::SetFixedPoint(const SMESH::PointStruct& ps,
292                                                        CORBA::Boolean            toUnset)
293 {
294   SMESH::PointStruct oldPS;
295   GetFixedPoint( oldPS );
296   if ( oldPS.x == ps.x && oldPS.y == ps.y && oldPS.z == ps.z )
297     return;
298
299   double p[3] = { ps.x, ps.y, ps.z };
300   GetImpl()->SetFixedPoint( p, toUnset );
301
302   SMESH::TPythonDump() << _this() << ".SetFixedPoint( " << ps << ", " << toUnset << " )";
303 }
304
305 //=======================================================================
306 //function : GetFixedPoint
307 //purpose  : Returns a fixed point
308 //=======================================================================
309
310 CORBA::Boolean StdMeshers_CartesianParameters3D_i::GetFixedPoint(SMESH::PointStruct& ps)
311 {
312   double p[3];
313   if ( GetImpl()->GetFixedPoint( p ) )
314   {
315     ps.x = p[0];
316     ps.y = p[1];
317     ps.z = p[2];
318     return true;
319   }
320   else
321   {
322     ps.x = 0.;
323     ps.y = 0.;
324     ps.z = 0.;
325   }
326   return false;
327 }
328
329 //=======================================================================
330 //function : SetToAddEdges
331 //purpose  : Enables implementation of geometrical edges into the mesh.
332 //=======================================================================
333
334 void StdMeshers_CartesianParameters3D_i::SetToAddEdges(CORBA::Boolean toAdd)
335 {
336   GetImpl()->SetToAddEdges( toAdd );
337   SMESH::TPythonDump() << _this() << ".SetToAddEdges( " << toAdd << " )";
338 }
339
340 //=======================================================================
341 //function : GetToAddEdges
342 //purpose  : Returns true if implementation of geometrical edges into the
343 //           mesh is enabled
344 //=======================================================================
345
346 CORBA::Boolean StdMeshers_CartesianParameters3D_i::GetToAddEdges()
347 {
348   return GetImpl()->GetToAddEdges();
349 }
350
351 //=======================================================================
352 //function : SetToConsiderInternalFaces
353 //purpose  : Enables treatment of geom faces, either shared by solids or internal.
354 //=======================================================================
355
356 void  StdMeshers_CartesianParameters3D_i::SetToConsiderInternalFaces(CORBA::Boolean toTreat)
357 {
358   if ( GetToConsiderInternalFaces() == toTreat )
359     return;
360   GetImpl()->SetToConsiderInternalFaces( toTreat );
361   SMESH::TPythonDump() << _this() << ".SetToConsiderInternalFaces( " << toTreat << " )";
362 }
363
364 //=======================================================================
365 //function : GetToConsiderInternalFaces
366 //purpose  : Return true if treatment of internal geom faces is enabled
367 //=======================================================================
368
369 CORBA::Boolean  StdMeshers_CartesianParameters3D_i::GetToConsiderInternalFaces()
370 {
371   return GetImpl()->GetToConsiderInternalFaces();
372 }
373
374 //=======================================================================
375 //function : SetToUseThresholdForInternalFaces
376 //purpose  : Enables applying size threshold to grid cells cut by internal geom faces.
377 //=======================================================================
378
379 void  StdMeshers_CartesianParameters3D_i::SetToUseThresholdForInternalFaces(CORBA::Boolean toUse)
380 {
381   if ( GetToUseThresholdForInternalFaces() == toUse )
382     return;
383   GetImpl()->SetToUseThresholdForInternalFaces( toUse );
384   SMESH::TPythonDump() << _this() << ".SetToUseThresholdForInternalFaces( " << toUse << " )";
385 }
386
387 //=======================================================================
388 //function : GetToUseThresholdForInternalFaces
389 //purpose  : Return true if applying size threshold to grid cells cut by
390 //           internal geom faces is enabled
391 //=======================================================================
392
393 CORBA::Boolean StdMeshers_CartesianParameters3D_i::GetToUseThresholdForInternalFaces()
394 {
395   return GetImpl()->GetToUseThresholdForInternalFaces();
396 }
397
398 //=======================================================================
399 //function : SetToCreateFaces
400 //purpose  : Enables creation of mesh faces.
401 //=======================================================================
402
403 void  StdMeshers_CartesianParameters3D_i::SetToCreateFaces(CORBA::Boolean toCreate)
404 {
405   if ( GetToCreateFaces() == toCreate )
406     return;
407   GetImpl()->SetToCreateFaces( toCreate );
408   SMESH::TPythonDump() << _this() << ".SetToCreateFaces( " << toCreate << " )";
409 }
410
411 //=======================================================================
412 //function : GetToCreateFaces
413 //purpose  : Check if creation of mesh faces enabled
414 //=======================================================================
415
416 CORBA::Boolean StdMeshers_CartesianParameters3D_i::GetToCreateFaces()
417 {
418   return GetImpl()->GetToCreateFaces();
419 }
420
421 //=======================================================================
422 //function : IsGridBySpacing
423 //purpose  : Return true if the grid is defined by spacing functions and
424 //           not by node coordinates
425 //=======================================================================
426
427 CORBA::Boolean StdMeshers_CartesianParameters3D_i::IsGridBySpacing(CORBA::Short axis)
428 {
429   return this->GetImpl()->IsGridBySpacing(axis);
430 }
431
432 //=======================================================================
433 //function : ComputeOptimalAxesDirs
434 //purpose  : Returns axes at which number of hexahedra is maximal
435 //=======================================================================
436
437 void StdMeshers_CartesianParameters3D_i::
438 ComputeOptimalAxesDirs(GEOM::GEOM_Object_ptr go,
439                        CORBA::Boolean        isOrthogonal,
440                        SMESH::DirStruct&     xDir,
441                        SMESH::DirStruct&     yDir,
442                        SMESH::DirStruct&     zDir)
443   
444 {
445   TopoDS_Shape shape = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( go );
446   if ( shape.IsNull() )
447     THROW_SALOME_CORBA_EXCEPTION( "Null shape", SALOME::BAD_PARAM );
448
449   double c[9];
450   ::StdMeshers_CartesianParameters3D::ComputeOptimalAxesDirs( shape, isOrthogonal, c );
451
452   xDir.PS.x = c[0];
453   xDir.PS.y = c[1];
454   xDir.PS.z = c[2];
455   yDir.PS.x = c[3];
456   yDir.PS.y = c[4];
457   yDir.PS.z = c[5];
458   zDir.PS.x = c[6];
459   zDir.PS.y = c[7];
460   zDir.PS.z = c[8];
461 }
462
463 //=======================================================================
464 //function : ComputeCoordinates
465 //purpose  : Computes node coordinates by spacing functions
466 //=======================================================================
467
468 SMESH::double_array*
469 StdMeshers_CartesianParameters3D_i::ComputeCoordinates(CORBA::Double              x0,
470                                                        CORBA::Double              x1,
471                                                        const SMESH::string_array& spaceFuns,
472                                                        const SMESH::double_array& points,
473                                                        const char*                axisName )
474   
475 {
476   std::vector<std::string> xFuns;
477   std::vector<double>      xPoints, coords;
478   _array2vec( spaceFuns, xFuns, (const char*) );
479   _array2vec( points, xPoints, );
480
481   try {
482     this->GetImpl()->ComputeCoordinates( x0, x1, xFuns, xPoints, coords, axisName );
483   }
484   catch ( SALOME_Exception& S_ex ) {
485     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
486   }
487   SMESH::double_array_var res = new SMESH::double_array;
488   _vec2array( coords, res,  );
489
490   return res._retn();
491 }
492
493 //=============================================================================
494 /*!
495  *  Get implementation
496  */
497 //=============================================================================
498
499 ::StdMeshers_CartesianParameters3D* StdMeshers_CartesianParameters3D_i::GetImpl()
500 {
501   return ( ::StdMeshers_CartesianParameters3D* )myBaseImpl;
502 }
503
504 //================================================================================
505 /*!
506  * \brief Verify whether hypothesis supports given entity type 
507   * \param type - dimension (see SMESH::Dimension enumeration)
508   * \retval CORBA::Boolean - TRUE if dimension is supported, FALSE otherwise
509  * 
510  * Verify whether hypothesis supports given entity type (see SMESH::Dimension enumeration)
511  */
512 //================================================================================  
513
514 CORBA::Boolean StdMeshers_CartesianParameters3D_i::IsDimSupported( SMESH::Dimension type )
515 {
516   return type == SMESH::DIM_3D;
517 }