This source file includes following definitions.
- load_file
- write_file
- improve_sphere
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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;
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
114