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