Commit 355a96b4 authored by Tomáš Jakubec's avatar Tomáš Jakubec
Browse files

The prototype of MeshDataInput is done.

parent ab87c8a7
Loading
Loading
Loading
Loading
+129 −0
Original line number Diff line number Diff line
#ifndef VTKMESHDATAREADER_H
#define VTKMESHDATAREADER_H
#include "../Traits.h"
#include "../MeshDataContainer.h"
#include <istream>
#include <map>
#include <sstream>

template <unsigned int MeshDimension, typename IndexType>
class VTKMeshDataReader {

    /**
     * @brief readColumn
     * reads a single column of traited data
     */
    static void readColumn(std::istream& ist [[maybe_unused]],...){
        DBGMSG("capture");
        throw std::runtime_error("capture of read column must not be called.");
    }

    template<typename T, unsigned int Index, unsigned int Position>
    static auto readColumn(std::istream& ist, DataContainer<T, Position, MeshDimension> &data,std::map<std::string, std::istream::pos_type>& dataPositions)
    -> typename std::enable_if<
        Detail::is_indexable<typename Traits<T>::ttype::template type<Index>>::value
       >::type
    {

        ist.seekg(dataPositions[Traits<T>::ttype::template getName<Index>()]);

        typename Traits<T>::ttype::template type<Index> value;

        for (IndexType i = 0; i < data.size(); i++) {
            for (unsigned int j = 0; j < Traits<T>::ttype::template getValue<Index>(data.at(i)).size(); j++){
                ist >> value[j];
            }
            Traits<T>::ttype::template setValue<Index>(data.at(i), value);
        }

    }


    template<typename T, unsigned int Index, unsigned int Position>
    static auto readColumn(std::istream& ist, DataContainer<T, Position, MeshDimension> &data,std::map<std::string, std::istream::pos_type>& dataPositions)
    -> typename std::enable_if<
        !Detail::is_indexable<typename Traits<T>::ttype::template type<Index>>::value
    >::type
    {


        ist.seekg(dataPositions[Traits<T>::ttype::template getName<Index>()]);

        typename Traits<T>::ttype::template type<Index> value;

        for (IndexType i = 0; i < data.size(); i++){
            ist >> value;
            Traits<T>::ttype::template setValue<Index>(data.at(i), value);
        }

    }
private:

    template<typename T,unsigned int Index = 0, typename VOID = void>
    struct readCellData{};

    template<typename T,unsigned int Index, typename... Types>
    struct readCellData <Traits<T, Types...>, Index, std::enable_if_t<Index < Traits<T, Types...>::size() - 1>>{

        template<unsigned int Position>

        static void read(std::istream& ist, DataContainer<T, Position, MeshDimension> &data, std::map<std::string, std::istream::pos_type>& dataPositions){
            DBGVAR(Detail::is_indexable<typename Traits<T>::ttype::template type<Index>>::value);
            readColumn<T, Index, Position>(ist, data, dataPositions);
            readCellData<Traits<T, Types...>, Index + 1>::read(ist, data, dataPositions);

        }
    };

    template<typename T,unsigned int Index, typename ... Types>
    struct readCellData <Traits<T, Types...>, Index, std::enable_if_t<Index == Traits<T, Types...>::size() - 1>>{
        template<unsigned int Position>
        static void read(std::istream& ist, DataContainer<T, Position, MeshDimension> &data, std::map<std::string, std::istream::pos_type>& dataPositions){

            readColumn<T, Index, Position>(ist, data, dataPositions);

        }
    };



public:

    static std::map<std::string, std::istream::pos_type> indexData(std::istream& ist) {

        std::map<std::string, std::istream::pos_type> dataPositions;

        std::string line;
        ist.seekg(ist.beg);
        while(getline(ist, line)) {
            int flag = (line.find("SCALARS")!= line.npos ? 1
                             : line.find("VECTORS") != line.npos ? 2
                                   : 0 );
            if (flag != 0){
                std::string dataName;
                std::stringstream sstream(line);
                sstream.ignore(9, ' ');
                sstream >> dataName;

                if (flag == 1) { // scalar quantity found
                    ist.ignore(500, '\n');
                }

                dataPositions.insert(std::pair(dataName, ist.tellg()));
            }
        }
        ist.clear();
        return dataPositions;
    }

    template<typename T, unsigned int Position>
    static void readData(std::istream& ist, DataContainer<T, Position, MeshDimension>& data) {

        std::map<std::string, std::istream::pos_type> dataPositions = indexData(ist);

        readCellData<typename Traits<T>::ttype>::read(ist, data, dataPositions);
    }
};


#endif // VTKMESHDATAREADER_H
+2 −2
Original line number Diff line number Diff line
@@ -76,12 +76,12 @@ public:
    }

    template<unsigned int Index>
    static void setValue(const Class* c, const type<Index>& val){
    static void setValue(Class* c, const type<Index>& val){
        getReference<Index>()->setValue(c, val);
    }

    template<unsigned int Index>
    static void setValue(const Class& c, const type<Index>& val){
    static void setValue(Class& c, const type<Index>& val){
        getReference<Index>()->setValue(c, val);
    }

+1 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ HEADERS += \
    InlineArrayOperations.h \
    UnstructuredMesh/MeshDataContainer/MemberApproach.h \
    UnstructuredMesh/MeshDataContainer/MeshDataContainer.h \
    UnstructuredMesh/MeshDataContainer/MeshDataIO/VTKMeshDataReader.h \
    UnstructuredMesh/MeshDataContainer/MeshDataIO/VTKMeshDataWriter.h \
    UnstructuredMesh/MeshDataContainer/Singleton.h \
    UnstructuredMesh/MeshDataContainer/Traits.h \
+1 −1
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.10.0, 2019-11-08T10:23:23. -->
<!-- Written by QtCreator 4.10.0, 2019-11-09T23:11:25. -->
<qtcreator>
 <data>
  <variable>EnvironmentId</variable>
+6 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
#include "UnstructuredMesh/MeshIO/MeshReader/VTKMeshReader.h"
#include "UnstructuredMesh/MeshIO/MeshWriter/VTKMeshWriter.h"
#include "UnstructuredMesh/MeshDataContainer/MeshDataIO/VTKMeshDataWriter.h"
#include "UnstructuredMesh/MeshDataContainer/MeshDataIO/VTKMeshDataReader.h"
#include "UnstructuredMesh/MeshIO/MeshReader/FPMAMeshReader.h"
#include "UnstructuredMesh/MeshIO/MeshWriter/FPMAMeshWriter.h"

@@ -654,6 +655,11 @@ void testMeshRefine() {
    in3D.open("mesh_refine_1.vtk");
    VTKMeshReader<3> reader;
    reader.loadFromStream(in3D, mesh);

    MeshDataContainer<colourData, 3> cd1(mesh);
    VTKMeshDataReader<3, size_t>::readData(in3D, cd1.getDataByPos<0>());

    DBGVAR(cd1.getDataByDim<3>());
    in3D.close();

    mesh.initializeCenters();