Commit 800a8b65 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Refactoring PointData and CellData sections in VTK writers

parent b65b6429
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@
#include <TNL/Functions/MeshFunctionEvaluator.h>
#include <TNL/Functions/MeshFunctionNormGetter.h>
#include <TNL/Functions/MeshFunctionGnuplotWriter.h>
#include <TNL/Functions/MeshFunctionVTKWriter.h>
#include <TNL/Meshes/Writers/VTKWriter.h>

#pragma once

@@ -406,8 +406,12 @@ write( const String& fileName,
      return false;
   }
   if( format == "vtk" ) {
      MeshFunctionVTKWriter< MeshFunction > writer( file );
      writer.write( *this );
      Meshes::Writers::VTKWriter< Mesh > writer( file );
      writer.template writeEntities< getEntitiesDimension() >( *meshPointer );
      if( MeshFunction::getEntitiesDimension() == 0 )
         writer.writePointData( getData(), "cellFunctionValues", 1 );
      else
         writer.writeCellData( getData(), "pointFunctionValues", 1 );
   }
   else if( format == "gnuplot" )
      return MeshFunctionGnuplotWriter< MeshFunction >::write( *this, file );
+0 −54
Original line number Diff line number Diff line
/***************************************************************************
                          MeshFunctionVTKWriter.h  -  description
                             -------------------
    begin                : Jan 28, 2016
    copyright            : (C) 2016 by Tomas Oberhuber et al.
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

/* See Copyright Notice in tnl/Copyright */

#pragma once

#include <TNL/Meshes/Writers/VTKWriter.h>

namespace TNL {
namespace Functions {

template< typename MeshFunction >
class MeshFunctionVTKWriter
: protected Meshes::Writers::VTKWriter< typename MeshFunction::MeshType >
{
   using MeshType = typename MeshFunction::MeshType;
   using EntityType = typename MeshType::template EntityType< MeshFunction::getEntitiesDimension() >;
   using GlobalIndex = typename MeshType::GlobalIndexType;

public:
   MeshFunctionVTKWriter( std::ostream& str,
                          Meshes::VTK::FileFormat format = Meshes::VTK::FileFormat::ascii )
   : Meshes::Writers::VTKWriter< MeshType >( str, format )
   {}

   void write( const MeshFunction& function,
               const String& functionName = "cellFunctionValues" )
   {
      const MeshType& mesh = function.getMesh();
      this->template writeEntities< MeshFunction::getEntitiesDimension() >( mesh );
      appendFunction( function, functionName );
   }

   // VTK supports writing multiple functions into the same file.
   // You can call this after 'write', which initializes the mesh entities,
   // with different function name.
   void appendFunction( const MeshFunction& function,
                        const String& functionName )
   {
      if( MeshFunction::getEntitiesDimension() == 0 )
         this->writeDataArray( function.getData(), functionName, 1, Meshes::VTK::DataType::PointData );
      else
         this->writeDataArray( function.getData(), functionName, 1, Meshes::VTK::DataType::CellData );
   }
};

} // namespace Functions
} // namespace TNL
+9 −3
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@
#include <TNL/Functions/MeshFunctionEvaluator.h>
#include <TNL/Functions/MeshFunctionNormGetter.h>
#include <TNL/Functions/MeshFunctionGnuplotWriter.h>
#include <TNL/Functions/MeshFunctionVTKWriter.h>
#include <TNL/Meshes/Writers/VTKWriter.h>

#pragma once

@@ -490,8 +490,14 @@ write( const String& fileName,
      std::cerr << "Unable to open a file " << fileName << "." << std::endl;
      return false;
   }
   if( format == "vtk" )
      return MeshFunctionVTKWriter< MeshFunctionView >::write( *this, file );
   if( format == "vtk" ) {
      Meshes::Writers::VTKWriter< Mesh > writer( file );
      writer.template writeEntities< getEntitiesDimension() >( *meshPointer );
      if( MeshFunctionView::getEntitiesDimension() == 0 )
         writer.writePointData( getData(), "cellFunctionValues", 1 );
      else
         writer.writeCellData( getData(), "pointFunctionValues", 1 );
   }
   else if( format == "gnuplot" )
      return MeshFunctionGnuplotWriter< MeshFunctionView >::write( *this, file );
   else {
+43 −5
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@
#include <TNL/Functions/MeshFunction.h>
#include <TNL/Functions/MeshFunctionView.h>
#include <TNL/Functions/VectorFieldGnuplotWriter.h>
#include <TNL/Functions/VectorFieldVTKWriter.h>
#include <TNL/Meshes/Writers/VTKWriter.h>

namespace TNL {
namespace Functions {
@@ -259,8 +259,28 @@ class VectorField< Size, MeshFunction< Mesh, MeshEntityDimension, Real > >
            std::cerr << "Unable to open a file " << fileName << "." << std::endl;
            return false;
         }
         if( format == "vtk" )
            return VectorFieldVTKWriter< VectorField >::write( *this, file );
         if( format == "vtk" ) {
            Meshes::Writers::VTKWriter< Mesh > writer( file );
            writer.template writeEntities< getEntitiesDimension() >( *getMeshPointer() );

            // copy all values from the vector field into a contiguous array
            using BufferType = Containers::Array< typename VectorField::RealType, Devices::Host, IndexType >;
            const IndexType entitiesCount = getMeshPointer()->template getEntitiesCount< getEntitiesDimension() >();
            BufferType buffer( 3 * entitiesCount );
            IndexType k = 0;
            for( IndexType i = 0; i < entitiesCount; i++ ) {
               const VectorType vector = getElement( i );
               static_assert( getVectorDimension() <= 3, "The VTK format supports only up to 3D vector fields." );
               for( int j = 0; j < 3; j++ )
                  buffer[ k++ ] = ( j < vector.getSize() ? vector[ j ] : 0 );
            }

            // write the buffer
            if( getEntitiesDimension() == 0 )
               writer.writePointData( buffer, "cellVectorFieldValues", 3 );
            else
               writer.writeCellData( buffer, "pointVectorFieldValues", 3 );
         }
         else if( format == "gnuplot" )
            return VectorFieldGnuplotWriter< VectorField >::write( *this, file );
         else {
@@ -505,8 +525,26 @@ class VectorField< Size, MeshFunctionView< Mesh, MeshEntityDimension, Real > >
            return false;
         }
         if( format == "vtk" ) {
            VectorFieldVTKWriter< VectorField > writer( file );
            writer.write( *this );
            Meshes::Writers::VTKWriter< Mesh > writer( file );
            writer.template writeEntities< getEntitiesDimension() >( *getMeshPointer() );

            // copy all values from the vector field into a contiguous array
            using BufferType = Containers::Array< typename VectorField::RealType, Devices::Host, IndexType >;
            const IndexType entitiesCount = getMeshPointer()->template getEntitiesCount< getEntitiesDimension() >();
            BufferType buffer( 3 * entitiesCount );
            IndexType k = 0;
            for( IndexType i = 0; i < entitiesCount; i++ ) {
               const VectorType vector = getElement( i );
               static_assert( getVectorDimension() <= 3, "The VTK format supports only up to 3D vector fields." );
               for( int j = 0; j < 3; j++ )
                  buffer[ k++ ] = ( j < vector.getSize() ? vector[ j ] : 0 );
            }

            // write the buffer
            if( getEntitiesDimension() == 0 )
               writer.writePointData( buffer, "cellVectorFieldValues", 3 );
            else
               writer.writeCellData( buffer, "pointVectorFieldValues", 3 );
         }
         else if( format == "gnuplot" )
            return VectorFieldGnuplotWriter< VectorField >::write( *this, file );
+0 −70
Original line number Diff line number Diff line
/***************************************************************************
                          VectorFieldVTKWriter.h  -  description
                             -------------------
    begin                : Jan 10, 2018
    copyright            : (C) 2018 by Tomas Oberhuber et al.
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

/* See Copyright Notice in tnl/Copyright */

#pragma once

#include <TNL/Meshes/Writers/VTKWriter.h>

namespace TNL {
namespace Functions {

template< typename VectorField >
class VectorFieldVTKWriter
: protected Meshes::Writers::VTKWriter< typename VectorField::MeshType >
{
   using MeshType = typename VectorField::MeshType;
   using MeshWriter = Meshes::Writers::VTKWriter< MeshType >;
   using EntityType = typename MeshType::template EntityType< VectorField::getEntitiesDimension() >;
   using GlobalIndex = typename MeshType::GlobalIndexType;

public:
   VectorFieldVTKWriter( std::ostream& str,
                         Meshes::VTK::FileFormat format = Meshes::VTK::FileFormat::ascii )
   : Meshes::Writers::VTKWriter< MeshType >( str, format )
   {}

   void write( const VectorField& field,
               const String& fieldName = "cellVectorFieldValues" )
   {
      const MeshType& mesh = field.getMesh();
      this->template writeEntities< VectorField::getEntitiesDimension() >( mesh );
      appendField( field, fieldName );
   }

   // VTK supports writing multiple fields into the same file.
   // You can call this after 'write', which initializes the mesh entities,
   // with different field name.
   void appendField( const VectorField& field,
                     const String& fieldName )
   {
      const MeshType& mesh = field.getMesh();
      const GlobalIndex entitiesCount = mesh.template getEntitiesCount< EntityType >();

      // copy all values from the vector field into a contiguous array
      using BufferType = Containers::Array< typename VectorField::RealType, Devices::Host, GlobalIndex >;
      BufferType buffer( 3 * entitiesCount );
      GlobalIndex j = 0;
      for( GlobalIndex i = 0; i < entitiesCount; i++ ) {
         const typename VectorField::VectorType vector = field.getElement( i );
         static_assert( VectorField::getVectorDimension() <= 3, "The VTK format supports only up to 3D vector fields." );
         for( int i = 0; i < 3; i++ )
            buffer[ j++ ] = ( i < vector.getSize() ? vector[ i ] : 0 );
      }

      // write the buffer
      if( VectorField::getEntitiesDimension() == 0 )
         this->writeDataArray( buffer, fieldName, 3, Meshes::VTK::DataType::PointData );
      else
         this->writeDataArray( buffer, fieldName, 3, Meshes::VTK::DataType::CellData );
   }
};

} // namespace Functions
} // namespace TNL
Loading