LCOV - code coverage report
Current view: top level - ugbase/lib_grid/file_io - file_io_txt.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 62 0
Test Date: 2025-09-21 23:31:46 Functions: 0.0 % 2 0

            Line data    Source code
       1              : /*
       2              :  * Copyright (c) 2009-2015:  G-CSC, Goethe University Frankfurt
       3              :  * Author: Sebastian Reiter
       4              :  * 
       5              :  * This file is part of UG4.
       6              :  * 
       7              :  * UG4 is free software: you can redistribute it and/or modify it under the
       8              :  * terms of the GNU Lesser General Public License version 3 (as published by the
       9              :  * Free Software Foundation) with the following additional attribution
      10              :  * requirements (according to LGPL/GPL v3 §7):
      11              :  * 
      12              :  * (1) The following notice must be displayed in the Appropriate Legal Notices
      13              :  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
      14              :  * 
      15              :  * (2) The following notice must be displayed at a prominent place in the
      16              :  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
      17              :  * 
      18              :  * (3) The following bibliography is recommended for citation and must be
      19              :  * preserved in all covered files:
      20              :  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
      21              :  *   parallel geometric multigrid solver on hierarchically distributed grids.
      22              :  *   Computing and visualization in science 16, 4 (2013), 151-164"
      23              :  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
      24              :  *   flexible software system for simulating pde based models on high performance
      25              :  *   computers. Computing and visualization in science 16, 4 (2013), 165-179"
      26              :  * 
      27              :  * This program is distributed in the hope that it will be useful,
      28              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      29              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      30              :  * GNU Lesser General Public License for more details.
      31              :  */
      32              : #include <fstream>
      33              : #include <sstream>
      34              : #include <string>
      35              : #include <vector>
      36              : 
      37              : #include "../lg_base.h"
      38              : #include "common/util/string_util.h"
      39              : 
      40              : #include "file_io_txt.h"
      41              : 
      42              : using namespace std;
      43              : 
      44              : namespace ug
      45              : {
      46              : 
      47            0 : bool LoadGridFromTXT(Grid& grid, const char* filename, AVector3& aPos)
      48              : {
      49            0 :         ifstream in(filename);
      50              : 
      51            0 :         if(!in)
      52              :                 return false;
      53              : 
      54              :         //grid.clear();
      55              : 
      56              :         int numVrts, numElems;
      57              : 
      58            0 :         in >> numVrts;
      59            0 :         in >> numElems;
      60              : 
      61              : //      create points
      62              : //      store pointers to the vertices on the fly in a vector.
      63              :         vector<Vertex*>   vVrts;
      64              :         vector<int> vVrtIds;
      65            0 :         vVrts.resize(numVrts); vVrtIds.resize(numVrts);
      66              : 
      67            0 :         for(int i = 0; i < numVrts; ++i)
      68            0 :                 vVrts[i] = *grid.create<RegularVertex>();
      69              : 
      70            0 :         if(!grid.has_vertex_attachment(aPos))
      71            0 :                 grid.attach_to_vertices(aPos);
      72              :         Grid::VertexAttachmentAccessor<APosition> aaPos(grid, aPos);
      73              : 
      74              : //      read the points
      75              :         {
      76              :                 int i = 0;
      77            0 :                 for(VertexIterator iter = grid.vertices_begin(); iter != grid.vertices_end(); ++iter, ++i)
      78              :                 {
      79            0 :                         in >> vVrtIds[i];
      80              :                         in >> aaPos[*iter].x();
      81              :                         in >> aaPos[*iter].y();
      82              :                         in >> aaPos[*iter].z();
      83              :                 }
      84            0 :                 if (in.fail())
      85            0 :                         UG_THROW ("LoadGridFromTXT: Failed to read vertices from '" << filename << "'");
      86              :         }
      87              : 
      88              : //      read the triangles and the quadrilaterals
      89              :         {
      90              :                 string e_line;
      91              :                 
      92            0 :                 for(int i = 0; i < numElems; ++i)
      93              :                 {
      94              :                         int Index, vrt_id_1, vrt_id_2, vrt_id_3, vrt_id_4;
      95              :                         vector<int>::iterator i1, i2, i3, i4;
      96              :                         
      97              :                         do
      98              :                         {
      99            0 :                                 getline(in, e_line);
     100            0 :                                 e_line = TrimString(e_line);
     101              :                         }
     102            0 :                         while(e_line.empty());
     103            0 :                         stringstream ss(e_line, ios_base::in);
     104              :                         
     105            0 :                         ss >> Index;
     106            0 :                         ss >> vrt_id_1;
     107            0 :                         ss >> vrt_id_2;
     108            0 :                         ss >> vrt_id_3;
     109              :                         
     110            0 :                         if (ss.fail())
     111            0 :                                 UG_THROW ("LoadGridFromTXT: Failed to read grid elements from '" << filename << "'");
     112              :                         
     113            0 :                         i1 = find (vVrtIds.begin(), vVrtIds.end(), vrt_id_1);
     114            0 :                         i2 = find (vVrtIds.begin(), vVrtIds.end(), vrt_id_2);
     115            0 :                         i3 = find (vVrtIds.begin(), vVrtIds.end(), vrt_id_3);
     116              :                         
     117            0 :                         if (i1 == vVrtIds.end() || i2 == vVrtIds.end() || i3 == vVrtIds.end())
     118            0 :                                 UG_THROW ("LoadGridFromTXT: Wrong index of a vertex of element " << Index << "in '" << filename << "'");
     119              :                         
     120            0 :                         ss >> vrt_id_4;
     121            0 :                         if (ss.fail ())
     122              :                                 grid.create<Triangle>
     123            0 :                                         (TriangleDescriptor
     124            0 :                                                 (vVrts[i1 - vVrtIds.begin()], vVrts[i2 - vVrtIds.begin()], vVrts[i3 - vVrtIds.begin()]));
     125              :                         else
     126              :                         {
     127            0 :                                 i4 = find (vVrtIds.begin(), vVrtIds.end(), vrt_id_4);
     128            0 :                                 if (i4 == vVrtIds.end())
     129            0 :                                         UG_THROW ("LoadGridFromTXT: Wrong index of the last vertex of quadrilateral " << Index << "in '" << filename << "'");
     130              :                                 grid.create<Quadrilateral>
     131            0 :                                         (QuadrilateralDescriptor
     132            0 :                                                 (vVrts[i1 - vVrtIds.begin()], vVrts[i2 - vVrtIds.begin()], vVrts[i3 - vVrtIds.begin()], vVrts[i4 - vVrtIds.begin()]));
     133              :                         }
     134            0 :                 }
     135              :         }
     136              : 
     137            0 :         in.close();
     138              :         return true;
     139            0 : }
     140              : 
     141            0 : bool SaveGridToTXT(Grid& grid, const char* filename, AVector3& aPos)
     142              : {
     143            0 :         if(!grid.has_vertex_attachment(aPos))
     144              :                 return false;
     145              : 
     146            0 :         ofstream out(filename);
     147              : 
     148            0 :         if(!out)
     149              :                 return false;
     150              : 
     151              : //      write the header
     152            0 :         out << grid.num_vertices() << " " << grid.num<Triangle>() + grid.num<Quadrilateral>() << endl;
     153              : 
     154              : //      write the vertices
     155              : //      store in each vertex at which position it has been written to the file.
     156              :         AInt aInt;
     157              :         grid.attach_to_vertices(aInt);
     158              :         Grid::VertexAttachmentAccessor<AVector3> aaPos(grid, aPos);
     159              :         Grid::VertexAttachmentAccessor<AInt> aaInt(grid, aInt);
     160              : 
     161              :         {
     162              :                 int counter = 0;
     163              : 
     164            0 :                 for(VertexIterator iter = grid.vertices_begin(); iter != grid.vertices_end(); iter++)
     165              :                 {
     166            0 :                         out << counter << " " <<    aaPos[*iter].x() << " " <<
     167            0 :                                                                                 aaPos[*iter].y() << " " <<
     168            0 :                                                                                 aaPos[*iter].z() << endl;
     169              : 
     170            0 :                         aaInt[*iter] = counter++;
     171              :                 }
     172              :         }
     173              : 
     174              : //      write the faces.
     175              :         {
     176              :                 int counter = 0;
     177              : 
     178            0 :                 for(TriangleIterator iter = grid.begin<Triangle>(); iter != grid.end<Triangle>(); ++iter, ++counter)
     179              :                 {
     180              :                         Triangle* t = *iter;
     181            0 :                         out << counter << " "     << aaInt[t->vertex(0)] << " "
     182            0 :                                                                         << aaInt[t->vertex(1)] << " "
     183            0 :                                                                         << aaInt[t->vertex(2)] << endl;
     184              :                 }
     185              : 
     186            0 :                 for(QuadrilateralIterator iter = grid.begin<Quadrilateral>(); iter != grid.end<Quadrilateral>(); ++iter, ++counter)
     187              :                 {
     188              :                         Quadrilateral* t = *iter;
     189            0 :                         out << counter << " "     << aaInt[t->vertex(0)] << " "
     190            0 :                                                                         << aaInt[t->vertex(1)] << " "
     191            0 :                                                                         << aaInt[t->vertex(2)] << " "
     192            0 :                                                                         << aaInt[t->vertex(3)] << endl;
     193              :                 }
     194              :         }
     195              : 
     196              :         grid.detach_from_vertices(aInt);
     197              :         return true;
     198            0 : }
     199              : 
     200              : }//     end of namespace
        

Generated by: LCOV version 2.0-1