root/data/sphere/2014paperALE_sphere/improve_sphere.cpp

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. load_file
  2. write_file
  3. improve_sphere
  4. main

   1 /*! \file improve_sphere.cpp
   2 
   3     \brief <Program Name>
   4 
   5      Revision history:
   6 
   7           Revised by Christian Power dd.mm.yyyy
   8           Originally written by Christian Power
   9                (power22c@gmail.com) 15.06.2015
  10 
  11      Read a dgf file and improve the nodes. This codes only works for the unit sphere.
  12          With a minor modification we can count vertices.
  13 
  14      Created by Christian Power on 15.06.2015
  15      Copyright (c) 2015 Christian Power. All rights reserved.
  16  */
  17 
  18 #include <string>
  19 #include <sstream>
  20 #include <iostream>
  21 #include <fstream>
  22 #include <vector>
  23 #include <array>
  24 #include <algorithm>
  25 #include <cmath>
  26 
  27 using namespace std;
  28 
  29 vector<string> load_file(const string ifname){
  30         ifstream ifs(ifname);
  31         if (!ifs) throw runtime_error("load_file()");
  32         vector<string> lines;
  33         for(string line; getline(ifs,line); )
  34                 lines.push_back(line);
  35         ifs.close();
  36         return lines;
  37 }
  38 
  39 typedef vector<string>::iterator Line_pt;
  40 
  41 void write_file(const string ofname, const vector<string>& content){
  42         ofstream ofs(ofname);
  43         for(const auto& line : content)
  44                 ofs << line << endl;
  45         ofs.close();
  46 }
  47 
  48 void improve_sphere(Line_pt first, Line_pt last){
  49         while (first != last){
  50                 istringstream line {*first};
  51                 double d1,d2,d3;
  52                 if( !(line >> d1 >> d2 >> d3) || !(line >> ws).eof() )
  53                         throw runtime_error("improve_sphere()");
  54 
  55                 // improving nodes
  56                 const double node_norm = sqrt(d1*d1 + d2*d2 + d3*d3);
  57                 d1 /= node_norm;
  58                 d2 /= node_norm;
  59                 d3 /= node_norm;
  60 
  61                 ostringstream improved_line;
  62                 improved_line << scientific
  63                                           << d1 << ' ' << d2 << ' ' << d3;
  64                 *first = improved_line.str();
  65                 ++first;
  66         }
  67 }
  68 
  69 int main() try{
  70         // improving sphere mesh code
  71         // const string filename = "Sphere_6.dgf";
  72         // vector<string> lines = load_file(filename);
  73         // 
  74         // Line_pt first_vertex = find(lines.begin(), lines.end(), "VERTEX");
  75         // if (first_vertex == lines.end()) throw runtime_error("main()");
  76         // ++first_vertex;      // prev. *first_vertex == VERTEX, now it is the first vertex 
  77         // Line_pt last_vertex = find(first_vertex, lines.end(), "#");
  78         // if (last_vertex == lines.end()) throw runtime_error("main()");
  79         //
  80         // improve_sphere(first_vertex, last_vertex);
  81         //
  82         // write_file(filename, lines);
  83 
  84         // counting vertices code
  85         ofstream notepad {"nr_of_vertices.txt"};
  86 
  87         const string basename = "Sphere_";
  88         const string suffix = ".dgf";
  89 
  90         for(int i = 0; i<6; ++i){
  91                 const string dgfname = basename + to_string(i+1) + suffix;
  92                 vector<string> lines = load_file(dgfname);
  93 
  94                 Line_pt first_vertex = find(lines.begin(), lines.end(), "VERTEX");
  95                 if (first_vertex == lines.end()) throw runtime_error("main()");
  96                 ++first_vertex; // prev. *first_vertex == VERTEX, now it is the first vertex 
  97                 Line_pt last_vertex = find(first_vertex, lines.end(), "#");
  98                 if (last_vertex == lines.end()) throw runtime_error("main()");          
  99                 
 100                 notepad << dgfname << ": " << distance(first_vertex,last_vertex) << endl;
 101         }
 102         notepad.close();
 103 }
 104 catch(exception& e){
 105         cerr << e.what() << '\n';
 106         return 1;
 107 }
 108 catch(...){
 109         cerr << "An error occured!\n";
 110         return 2;
 111 }
 112 
 113 /*! Log:
 114  */

/* [<][>][^][v][top][bottom][index][help] */