Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDMEM / MEDMEM_Meshing.cxx
index c9aa2932385c7d02b243f7b6608fc915b3d1de73..abb6b231bf0a48357d9becd98baa3d21e8b2a37c 100644 (file)
@@ -1,27 +1,27 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License.
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 /*
   File MEDMEM_Meshing.cxx
-  $Header$
 */
 
 #include <string>
@@ -86,37 +86,10 @@ MESHING::~MESHING()
   MESSAGE_MED("Deleting MESHING !!");
 }
 
-/*!  
-\addtogroup MESHING_general
- @{  */
-
-/*! Sets the dimension of the space. */
-void MESHING::setSpaceDimension(int SpaceDimension)
-{
-  _spaceDimension = SpaceDimension ;
-}
-
-/*! Sets the dimension of the mesh. */
-void MESHING::setMeshDimension(const int MeshDimension)
-{
-   _meshDimension = MeshDimension ;
-   if (_connectivity)
-     _connectivity->setEntityDimension(MeshDimension);
-}
-/*!  @}  */
-
 /*!  
 \addtogroup MESHING_nodes
  @{  */
 
-/*! Sets the number of nodes used in the mesh. */
-void MESHING::setNumberOfNodes(const int NumberOfNodes)
-{
-  _numberOfNodes = NumberOfNodes ;
-  if (_connectivity)
-    _connectivity->setNumberOfNodes(NumberOfNodes);
-}
-
 /*! 
   Sets the whole coordinates array in a given system and interlacing mode.
   The system coordinates are :
@@ -140,36 +113,30 @@ myMeshing.setCoordinates(SpaceDimension,NumberOfNodes,Coordinates,System,Mode);
 
 */
 void MESHING::setCoordinates(const int SpaceDimension,
-                                                                                                                const int NumberOfNodes,
-                                                                                                                const double * Coordinates,
-                                                                                                                const string System,
-                                                                                                                const MED_EN::medModeSwitch Mode)
+                             const int NumberOfNodes,
+                             const double * Coordinates,
+                             const string System,
+                             const MED_EN::medModeSwitch Mode)
 {
-  setSpaceDimension(SpaceDimension);
-  setNumberOfNodes(NumberOfNodes);
+  _spaceDimension = SpaceDimension;
+  _numberOfNodes  = NumberOfNodes;
+  if (_connectivity)
+    _connectivity->setNumberOfNodes(NumberOfNodes);
+
+  if ( !NumberOfNodes && !Coordinates )
+    return; // enable using this method for setting SpaceDimension only
 
   SCRUTE_MED(_coordinate);
   SCRUTE_MED(_connectivity);
-  //if (NULL != _coordinate) delete _coordinate;
 
-  _coordinate = new COORDINATE(SpaceDimension,
-                              NumberOfNodes,
-                              Mode);
+  if ( _coordinate )
+    delete _coordinate, _coordinate = 0;
+  _coordinate = new COORDINATE(SpaceDimension,NumberOfNodes,Mode);
   _coordinate->setCoordinates(Mode,Coordinates);
   _coordinate->setCoordinatesSystem(System);
 }
 /*!  @}  */
 
-
-/*! Sets the system in which coordinates are given (MED_CART,MED_CYL,MED_SPHER). */
-void MESHING::setCoordinatesSystem(const string& System)
-  throw (MEDEXCEPTION)
-{
-  if (NULL == _coordinate)
-    throw MEDEXCEPTION(LOCALIZED("MESHING::setCoordinatesSystem : no coordinates defined"));
-  _coordinate->setCoordinatesSystem(System);
-}
-
 /*! 
  \addtogroup MESHING_general
  @{  */
@@ -234,7 +201,7 @@ void MESHING::setCoordinateUnit(const string unit, const int i)
 
 /*! 
  \addtogroup MESHING_connectivity
- @{ 
+ @{
 */
 
 /*!
@@ -249,7 +216,7 @@ void MESHING::setCoordinateUnit(const string unit, const int i)
   MED_FACE connectivity before MED_CELL).
 */
 void MESHING::setNumberOfTypes(const int NumberOfTypes,
-                                                                                                                        const MED_EN::medEntityMesh Entity) 
+                               const MED_EN::medEntityMesh Entity) 
   throw (MEDEXCEPTION)
 {
   const char * LOC = "MESHING::setNumberOfTypes( medEntityMesh ) : ";
@@ -260,10 +227,9 @@ void MESHING::setNumberOfTypes(const int NumberOfTypes,
 
   if (MED_CELL == Entity) {
     SCRUTE_MED(_connectivity);
-//     if (_connectivity != (CONNECTIVITY *) NULL)
-//       delete _connectivity ;
+    if (_connectivity )
+      delete _connectivity ;
     _connectivity = new CONNECTIVITY(NumberOfTypes,Entity) ;
-
     _connectivity->setNumberOfNodes( _numberOfNodes );
 
   } else {
@@ -273,19 +239,22 @@ void MESHING::setNumberOfTypes(const int NumberOfTypes,
 
     if (MED_FACE == Entity)
       if (3 != getSpaceDimension())
-       throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"No connectivity on MED_FACE could be defined in non 3D space !"));
-    
+        throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"No connectivity on MED_FACE could be defined in non 3D space !"));
+
     if (MED_EDGE == Entity)
-      if (3 == getSpaceDimension()) {
-       if (!_connectivity->existConnectivity(MED_NODAL,MED_FACE))
-         throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"No connectivity on MED_FACE defined !"));
-      } else {
-       if (2 != getSpaceDimension())
-         throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"Could not set connectivity on MED_EDGE !"));
+      {
+        if (3 == getSpaceDimension()) {
+          if (3 == getMeshDimension() && !_connectivity->existConnectivity(MED_NODAL,MED_FACE))
+            throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"No connectivity on MED_FACE defined !"));
+          if (2 > getMeshDimension())
+            throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"Could not set connectivity on MED_EDGE !"));
+        } else {
+          if (2 != getSpaceDimension())
+            throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"Could not set connectivity on MED_EDGE !"));
+        }
       }
     // all right, we could create connectivity !
     CONNECTIVITY * myConnectivity = new CONNECTIVITY(NumberOfTypes,Entity) ;
-    myConnectivity->setEntityDimension(_connectivity->getEntityDimension()-1);
     myConnectivity->setNumberOfNodes(_connectivity->getNumberOfNodes());
     _connectivity->setConstituent(myConnectivity);
   }
@@ -304,7 +273,7 @@ If \a entity is not defined, the method will throw an exception.
 */
 
 void MESHING::setTypes(const MED_EN::medGeometryElement * Types,
-                                                                                       const MED_EN::medEntityMesh entity)
+                       const MED_EN::medEntityMesh        entity)
   throw (MEDEXCEPTION)
 {
   if (entity == MED_NODE)
@@ -313,7 +282,7 @@ void MESHING::setTypes(const MED_EN::medGeometryElement * Types,
   if (_connectivity == NULL)
     throw MEDEXCEPTION(LOCALIZED("MESHING::setTypes : No connectivity defined !"));
 
-  _connectivity->setGeometricTypes(Types,entity) ;
+  _connectivity->setGeometricTypes(Types,entity);
 }
 
 /*!
@@ -324,7 +293,7 @@ void MESHING::setTypes(const MED_EN::medGeometryElement * Types,
   this sets 12 triangles and 23 quadrangles.
 */
 void MESHING::setNumberOfElements(const int * NumberOfElements,
-                                 const MED_EN::medEntityMesh Entity)
+                                  const MED_EN::medEntityMesh Entity)
   throw (MEDEXCEPTION)
 {
   const char * LOC = "MESHING::setNumberOfElements(const int *, medEntityMesh) : " ;
@@ -348,26 +317,32 @@ void MESHING::setNumberOfElements(const int * NumberOfElements,
   Sets the nodal connectivity for geometric type \a Type of  entity \a Entity.
   The nodal connectivity must be defined one element type at a time :
   \a MED_ALL_ELEMENTS is not a valid \a Type argument.
+  To define connectivity of \a MED_POLYGON and \a MED_POLYHEDRA, \a PolyConnectivityIndex
+  is also necessary, which defines index of the first node of each element.
+  Connectiviy of polyhedron must contain -1 as a separator of faces. For example,
+  a tetrahedron with connectivity {1,2,3,4} can be represented as a polyhedron by the following arrays:<br>
+Connectivity : {1,2,3,-1,2,3,4,-1,3,4,1,-1,4,1,2}<br>
+Connectivity_index : {1,16}
 
   Example :
 
 \verbatim
 MESHING myMeshing ;
 myMeshing.setCoordinates(SpaceDimension,NumberOfNodes,Coordinates,System,Mode);
-
 myMeshing.setNumberOfTypes(2,MED_CELL);
 myMeshing.setTypes({MED_TRIA3,MED_QUAD4},MED_CELL);
 myMeshing.setNumberOfElements({3,2},MED_CELL); // 3 MED_TRIA3 and 2 MED_QUAD4
-myMeshing.setConnectivity({1,2,3,6,8,9,4,5,6},MED_CELL,MED_TRIA3);
-myMeshing.setConnectivity({1,3,4,5,4,5,7,8},MED_CELL,MED_QUAD4);
+myMeshing.setConnectivity(MED_CELL,MED_TRIA3,{1,2,3,6,8,9,4,5,6});
+myMeshing.setConnectivity(MED_CELL,MED_QUAD4,{1,3,4,5,4,5,7,8});
 \endverbatim
 
-  Example : setConnectivity({1,2,3,1,4,2},MED_FACE,MED_TRIA3)
+  Example : setConnectivity(MED_FACE,MED_TRIA3,{1,2,3,1,4,2})
   Define 2 triangles face defined with nodes 1,2,3 and 1,4,2.
 */
-void MESHING::setConnectivity(const int * Connectivity,
-                             const MED_EN::medEntityMesh Entity,
-                             const MED_EN::medGeometryElement Type)
+void MESHING::setConnectivity(const MED_EN::medEntityMesh Entity,
+                              const MED_EN::medGeometryElement Type,
+                              const int * Connectivity,
+                              const int * PolyConnectivityIndex)
   throw (MEDEXCEPTION)
 {
   const char * LOC = "MESHING::setConnectivity : " ;
@@ -379,119 +354,11 @@ void MESHING::setConnectivity(const int * Connectivity,
     throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"No connectivity defined !"));
 
   _connectivity->setNumberOfNodes(_numberOfNodes);
-  _connectivity->setNodal(Connectivity,Entity,Type) ;
-}
-/*!  @}  */
-
-/*!  
-\addtogroup MESHING_poly
- @{
-  */
-
-/*! Method setting the connectivity for MED_POLYGON
-elements
-\param ConnectivityIndex polygon connectivity index
-\param Connectivity polygon connectivity
-\param nbOfPolygons number of polygons defined
-\param Entity entity of the polygon
-
-The \a Entity parameter specifies whether the polygon is the highest order
- element (MED_CELL) or it is a 2D element in a 3D mesh (MED_FACE).
-
-This method is fully compatible with the MESHING::setConnectivity method.
-The following code excerpt creates two polygons with 5 and 4 nodes respectively.
-\verbatim
-MESHING myMeshing ;
-myMeshing.setCoordinates(SpaceDimension,NumberOfNodes,Coordinates,System,Mode);
-
-int conn_index[3]={1,6,10};
-int conn[9]={1,2,3,4,5,5,4,6,7};
-int nb_poly=2;
-meshing.setPolygonsConnectivity(conn_index, conn, nb_poly, MED_CELL)
-\endverbatim
-
-*/
-
-void MESHING::setPolygonsConnectivity     (const int * ConnectivityIndex,
-                                          const int * Connectivity,
-                                          int nbOfPolygons,
-                                          const MED_EN::medEntityMesh Entity)
-  throw (MEDEXCEPTION)
-{
-  if (_connectivity == (CONNECTIVITY*)NULL)
-    throw MEDEXCEPTION("No connectivity defined !");
-  
-  _connectivity->setPolygonsConnectivity(MED_NODAL, Entity, Connectivity, ConnectivityIndex,ConnectivityIndex[nbOfPolygons]-1,nbOfPolygons) ;
+  _connectivity->setNodal(Connectivity,Entity,Type,PolyConnectivityIndex) ;
 }
 
-
-/*! Method setting the connectivity for MED_POLYHEDRON
-elements
-\param PolyhedronIndex polyhedra connectivity index
-\param FacesIndex polyhedra face connectivity index
-\param Nodes polyhedra connectivity
-\param nbOfPolyhedra number of polyhedra defined
-\param Entity deprecated parameter 
-*/
-void MESHING::setPolyhedraConnectivity     (const int * PolyhedronIndex,
-                                           const int * FacesIndex,
-                                           const int * Nodes,
-                                           int nbOfPolyhedra,
-                                           const MED_EN::medEntityMesh Entity)
-  throw (MEDEXCEPTION)
-{
-  if (_connectivity == (CONNECTIVITY*)NULL)
-    throw MEDEXCEPTION("No connectivity defined !");
-  if(_connectivity->getPolyTypeRelativeTo()==MED_EN::MED_POLYHEDRA)
-    {
-      int nbOfFacesOfAllPolyhedra=PolyhedronIndex[nbOfPolyhedra]-1;
-      _connectivity->setPolyhedronConnectivity(MED_NODAL, Nodes, PolyhedronIndex, FacesIndex[nbOfFacesOfAllPolyhedra]-1 , nbOfPolyhedra, FacesIndex, nbOfFacesOfAllPolyhedra) ;
-    }
-  else
-    throw MEDEXCEPTION("Invalid connectivity for polyhedra !!!");
-}
 /*!  @}  */
 
-/*!
-  NOT YET IMPLEMENTED !! WARNING
-*/
-void MESHING::setConnectivities (const int * ConnectivityIndex,
-                                const int * ConnectivityValue,
-                                const MED_EN::medConnectivity ConnectivityType,
-                                const MED_EN::medEntityMesh Entity)
-  throw (MEDEXCEPTION)
-{
-  const char * LOC = "MESHING::setConnectivities : " ;
-  SCRUTE_MED(Entity);
-  SCRUTE_MED(ConnectivityType);
-  SCRUTE_MED(ConnectivityValue);
-  SCRUTE_MED(ConnectivityIndex);
-
-  throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"Not Yet Implemented :: Warning !"));
-}
-
-/*!
-*/
-
-// void MESHING::setGroup(const string name,
-//                    const string description,
-//                    const int NumberOfElements,
-//                    const int * ElementsNumbers,
-//                    const medEntityMesh Entity)
-// {
-//   GROUP * myGroup = new GROUP() ;
-//   myGroup->setMesh(*this) ;
-//   myGroup->setName(name) ;
-//   myGroup->setDescription(description) ;
-//   myGroup->setEntity(Entity) ;
-//   // medEntityMesh and medGeometryElement ???
-//   myGroup->setNumberOfGeometricType(NumberOfType) ;
-//   myGroup->setGeometricType(Type) ;
-//   myGroup->setNumberOfGaussPoint(NumberOfGaussPoint) ;
-//   myGroup->setNumberOfElements(NumberOfElements) ;
-//   myGroup->setNumber(Number) ;
-// }
-
 /*!  
 \addtogroup MESHING_group
  @{  */
@@ -509,6 +376,10 @@ void MESHING::addGroup(const GROUP & Group)
   const char * LOC = "MESHING::addGroup : " ;
 
   GROUP * myGroup = new GROUP(Group) ;
+  if(myGroup->getMesh()==this)
+    {
+      removeReference();
+    }
   switch(Group.getEntity()){
   case MED_CELL : {
     _groupCell.push_back(myGroup);
@@ -531,5 +402,5 @@ void MESHING::addGroup(const GROUP & Group)
   }
 }
 
-};
+}
 /*!  @}  */