From a516513bee0aea7d898c7f70d2f19dd80eeefd1d Mon Sep 17 00:00:00 2001
From: Tomas Oberhuber <tomas.oberhuber@fjfi.cvut.cz>
Date: Thu, 20 Mar 2014 21:30:07 +0100
Subject: [PATCH] Implementing the mesh integrity checker.

---
 src/mesh/tnlMeshInitializer.h      | 10 ++++++++++
 src/mesh/tnlMeshIntegrityChecker.h | 22 ++++++++++++++++++++++
 tools/src/tnl-mesh-convert.h       | 13 ++++++++++---
 3 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/src/mesh/tnlMeshInitializer.h b/src/mesh/tnlMeshInitializer.h
index 9ae0812abd..0fc913f4e1 100644
--- a/src/mesh/tnlMeshInitializer.h
+++ b/src/mesh/tnlMeshInitializer.h
@@ -108,17 +108,27 @@ class tnlMeshInitializerLayer< ConfigTag,
    bool checkCells()
    {
       typedef typename tnlMeshEntity< ConfigTag, EntityTag >::template SubentitiesTraits< 0 >::LocalIndexType LocalIndexType;
+      const GlobalIndexType numberOfVertices( this->getMesh().getNumberOfVertices() );
       for( GlobalIndexType cell = 0;
            cell < this->getMesh().getNumberOfCells();
            cell++ )
          for( LocalIndexType i = 0;
               i < this->getMesh().getCell( cell ).getNumberOfVertices();
               i++ )
+         {
             if( this->getMesh().getCell( cell ).getVerticesIndices()[ i ] == - 1 )
             {
                cerr << "The cell number " << cell << " does not have properly set vertex index number " << i << "." << endl;
                return false;
             }
+            if( this->getMesh().getCell( cell ).getVerticesIndices()[ i ] >= numberOfVertices )
+            {
+               cerr << "The cell number " << cell << " does not have properly set vertex index number " << i
+                    << ". The index is higher than the number of all vertices ( " << numberOfVertices
+                    << " )." << endl;
+               return false;
+            }
+         }
       return true;
    }
 
diff --git a/src/mesh/tnlMeshIntegrityChecker.h b/src/mesh/tnlMeshIntegrityChecker.h
index 5b215ee5e1..4be1c207b5 100644
--- a/src/mesh/tnlMeshIntegrityChecker.h
+++ b/src/mesh/tnlMeshIntegrityChecker.h
@@ -23,7 +23,29 @@
 template< typename MeshType >
 class tnlMeshIntegrityChecker
 {
+   public:
 
+   typedef typename MeshType::Config                       ConfigTag;
+   typedef typename ConfigTag::CellTag                     CellTag;
+   typedef tnlDimensionsTraits< CellTag::dimensions >      CellDimensionsTraits;
+   typedef tnlMeshEntitiesTraits< ConfigTag,
+                                  CellDimensionsTraits >   CellTraits;
+   typedef typename CellTraits::SharedContainerType        CellsSharedContainerType;
+   typedef tnlDimensionsTraits< 0 >                        VertexDimensionsTraits;
+   typedef tnlMeshEntitiesTraits< ConfigTag,
+                                  VertexDimensionsTraits > VertexTraits;
+   typedef typename VertexTraits::SharedContainerType      VertexSharedConatinerType;
+
+   static bool checkMesh( const MeshType& mesh )
+   {
+      for( CellsGlobalIndexType cell = 0;
+           cell < mesh.getNumberOfCells();
+           cell++ )
+      {
+         cout << "Checking cell number " << cell << endl;
+      }
+      return true;
+   }
 };
 
 
diff --git a/tools/src/tnl-mesh-convert.h b/tools/src/tnl-mesh-convert.h
index 9b4d5556eb..82e507a369 100644
--- a/tools/src/tnl-mesh-convert.h
+++ b/tools/src/tnl-mesh-convert.h
@@ -25,6 +25,7 @@
 #include <mesh/topologies/tnlMeshTetrahedronTag.h>
 #include <mesh/tnlMesh.h>
 #include <mesh/tnlMeshInitializer.h>
+#include <mesh/tnlMeshIntegrityChecker.h>
 #include <core/mfilename.h>
 
 template< int Dimensions >
@@ -38,8 +39,9 @@ bool readMeshWithDimensions( const tnlParameterContainer& parameters )
       struct MeshConfig : public tnlMeshConfigBase< 2 >
       {
          typedef tnlMeshTriangleTag CellTag;
-      };      
-      tnlMesh< MeshConfig > mesh;
+      };            
+      typedef tnlMesh< MeshConfig >  MeshType;
+      MeshType mesh;
       if( fileExt == "ng" &&
           ! tnlMeshReaderNetgen::readMesh<>( inputFileName, mesh, true ) )
          return false;
@@ -47,6 +49,8 @@ bool readMeshWithDimensions( const tnlParameterContainer& parameters )
       meshInitializer.setVerbose( true );
       if( ! meshInitializer.initMesh( mesh ) )
          return false;
+      if( ! tnlMeshIntegrityChecker< MeshType >::checkMesh( mesh ) )
+         return false;
       tnlString outputFile;
       if( parameters.GetParameter< tnlString >( "output-file", outputFile ) )
       {
@@ -64,7 +68,8 @@ bool readMeshWithDimensions( const tnlParameterContainer& parameters )
       {
          typedef tnlMeshTetrahedronTag CellTag;
       };
-      tnlMesh< MeshConfig > mesh;
+      typedef tnlMesh< MeshConfig > MeshType;
+      MeshType mesh;
       if( fileExt == "ng" &&
           ! tnlMeshReaderNetgen::readMesh<>( inputFileName, mesh, true ) )
          return false;
@@ -72,6 +77,8 @@ bool readMeshWithDimensions( const tnlParameterContainer& parameters )
       meshInitializer.setVerbose( true );
       if( ! meshInitializer.initMesh( mesh ) )
          return false;
+      if( ! tnlMeshIntegrityChecker< MeshType >::checkMesh( mesh ) )
+         return false;
       tnlString outputFile;
       if( parameters.GetParameter< tnlString >( "output-file", outputFile ) )
       {
-- 
GitLab