+namespace
+{
+ // Methods of splitting volumes into tetra
+
+ const int theHexTo5[5*4] =
+ {
+ 0, 1, 5, 2,
+ 0, 4, 5, 7,
+ 0, 3, 7, 2,
+ 5, 6, 7, 2,
+ 0, 2, 5, 7
+ };
+ const int theHexTo6[6*4] =
+ {
+ 0, 1, 5, 2,
+ 0, 4, 5, 7,
+ 0, 3, 7, 2,
+ 5, 6, 7, 2,
+ 0, 2, 5, 7
+ };
+ const int thePyraTo2[2*4] =
+ {
+ 0, 1, 2, 4,
+ 0, 2, 3, 4
+ };
+
+ const int thePentaTo8[8*4] =
+ {
+ 0, 1, 2, 6,
+ 3, 5, 4, 6,
+ 0, 3, 4, 6,
+ 0, 4, 1, 6,
+ 1, 4, 5, 6,
+ 1, 5, 2, 6,
+ 2, 5, 3, 6,
+ 2, 3, 0, 6
+ };
+
+ struct TSplitMethod
+ {
+ int _nbTetra;
+ const int* _connectivity;
+ bool _addNode; // additional node is to be created
+ TSplitMethod( int nbTet=0, const int* conn=0, bool addNode=false)
+ : _nbTetra(nbTet), _connectivity(conn), _addNode(addNode) {}
+ };
+
+ /*!
+ * \brief return TSplitMethod for the given element
+ */
+ TSplitMethod getSplitMethod( const SMDS_MeshElement* vol, const int theMethodFlags)
+ {
+ TSplitMethod method;
+ if ( vol->GetType() == SMDSAbs_Volume && !vol->IsPoly())
+ switch ( vol->NbNodes() )
+ {
+ case 8:
+ case 20:
+ if ( theMethodFlags & SMESH_MeshEditor::HEXA_TO_5 )
+ method = TSplitMethod( 5, theHexTo5 );
+ else
+ method = TSplitMethod( 6, theHexTo6 );
+ break;
+ case 5:
+ case 13:
+ method = TSplitMethod( 2, thePyraTo2 );
+ break;
+ case 6:
+ case 15:
+ method = TSplitMethod( 8, thePentaTo8, /*addNode=*/true );
+ break;
+ default:;
+ }
+ return method;
+ }
+}
+
+//=======================================================================
+//function : SplitVolumesIntoTetra
+//purpose : Split volumic elements into tetrahedra.
+//=======================================================================
+
+void SMESH_MeshEditor::SplitVolumesIntoTetra (const TIDSortedElemSet & theElems,
+ const int theMethodFlags)
+{
+ // sdt-like iterator on coordinates of nodes of mesh element
+ typedef SMDS_StdIterator< TNodeXYZ, SMDS_ElemIteratorPtr > NXyzIterator;
+ NXyzIterator xyzEnd;
+
+ SMESH_MesherHelper helper( *GetMesh());
+
+ TIDSortedElemSet::const_iterator elem = theElems.begin();
+ for ( ; elem != theElems.end(); ++elem )
+ {
+ SMDSAbs_EntityType geomType = (*elem)->GetEntityType();
+ if ( geomType <= SMDSEntity_Quad_Tetra )
+ continue; // tetra or face or edge
+
+ if ( (*elem)->IsQuadratic() )
+ {
+ // add quadratic links to the helper
+ SMDS_VolumeTool vol( *elem );
+ for ( int iF = 0; iF < vol.NbFaces(); ++iF )
+ {
+ const SMDS_MeshNode** fNodes = vol.GetFaceNodes( iF );
+ for ( int iN = 0; iN < vol.NbFaceNodes( iF ); iN += 2)
+ helper.AddTLinkNode( fNodes[iF], fNodes[iF+2], fNodes[iF+1] );
+ }
+ helper.SetIsQuadratic( true );
+ }
+ else
+ {
+ helper.SetIsQuadratic( false );
+ }
+
+ vector<const SMDS_MeshElement* > tetras; // splits of a volume
+
+ if ( geomType == SMDSEntity_Polyhedra )
+ {
+ // Each face of a polyhedron is split into triangles and
+ // each of triangles and a cell barycenter form a tetrahedron.
+
+ SMDS_VolumeTool vol( *elem );
+
+ // make a node at barycenter
+ gp_XYZ gc = std::accumulate( NXyzIterator((*elem)->nodesIterator()), xyzEnd,gp_XYZ(0,0,0));
+ gc /= vol.NbNodes();
+ SMDS_MeshNode* gcNode = GetMeshDS()->AddNode( gc.X(), gc.Y(), gc.Z() );
+
+ for ( int iF = 0; iF < vol.NbFaces(); ++iF )
+ {
+ const SMDS_MeshNode** fNodes = vol.GetFaceNodes( iF );
+ int nbFNodes = vol.NbFaceNodes( iF );
+ int nbTria = nbFNodes - 2;
+ bool extFace = vol.IsFaceExternal( iF );
+ SMDS_MeshElement* tet;
+ for ( int i = 0; i < nbTria; ++i )
+ {
+ if ( extFace )
+ tet = helper.AddVolume( fNodes[0], fNodes[i+1], fNodes[i+2], gcNode );
+ else
+ tet = helper.AddVolume( fNodes[0], fNodes[i+2], fNodes[i+1], gcNode );
+ tetras.push_back( tet );
+ }
+ }
+
+ }
+ else
+ {
+
+ TSplitMethod splitMethod = getSplitMethod( *elem, theMethodFlags );
+ if ( splitMethod._nbTetra < 1 ) continue;
+
+ vector<const SMDS_MeshNode*> volNodes( (*elem)->begin_nodes(), (*elem)->end_nodes());
+ }
+ }
+}
+