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