This source file includes following definitions.
- main
- check_usage
- get_vertices
- convert_to_vertex
- level_set_function
1
2
3
4
5
6
7
8
9 #include <iostream>
10 #include <fstream>
11 #include <sstream>
12 #include <vector>
13 #include <valarray>
14
15
16
17
18
19
20
21
22
23 void check_usage(int argc, char** argv);
24
25
26 using Vertex = std::valarray<double>;
27 using Vertices = std::vector<Vertex>;
28 const Vertices get_vertices(const std::string& filename);
29
30
31
32
33
34 double level_set_function(const Vertex& vertex);
35
36 int main(int argc, char** argv) try{
37 check_usage(argc, argv);
38
39 Vertices v_list = get_vertices(argv[1]);
40
41 std::valarray<double> lsf_values (v_list.size());
42 for(size_t it = 0; it < v_list.size(); ++it)
43 lsf_values[it] = level_set_function(v_list[it]);
44
45
46
47
48
49 std::cout << std::scientific;
50 std::cout << "Mean value: " << lsf_values.sum() / v_list.size() << std::endl;
51
52 std::cout << "Median: ";
53 std::sort(std::begin(lsf_values), std::end(lsf_values));
54 if(v_list.size() % 2)
55 std::cout << lsf_values[ v_list.size()/2 ];
56 else
57 std::cout << (lsf_values[ v_list.size()/2 - 1 ] + lsf_values[ v_list.size()/2])/2;
58 std::cout << std::endl;
59 }
60 catch(std::exception& e){
61 std::cerr << e.what() << '\n';
62 return 1;
63 }
64
65
66
67 void check_usage(int argc, char** argv){
68 if(argc != 2){
69 std::ostringstream err_msg;
70 err_msg << "Usage: " << argv[0] << " filename " << '\n'
71 << "Program terminating due to invalid input.";
72 throw std::runtime_error{err_msg.str()};
73 }
74 }
75
76
77 #include <algorithm>
78
79 const Vertex convert_to_vertex(const std::string& str_vertex);
80
81 const Vertices get_vertices(const std::string& filename){
82 std::ifstream dgf_file {filename};
83 if(!dgf_file) throw std::runtime_error {"File \"" + filename + "\" does not exist."};
84
85 std::vector<std::string> lines;
86 for(std::string line; std::getline(dgf_file,line); )
87 lines.push_back(line);
88
89 const std::string begin_vertices = "VERTEX";
90 auto first_it = std::find(std::cbegin(lines), std::cend(lines), begin_vertices);
91 if(first_it == std::cend(lines))
92 throw std::runtime_error
93 {"Could not find " + begin_vertices + " in file \"" + filename + "\"."};
94 ++first_it;
95
96 const std::string end_vertices = "#";
97 const auto second_it = std::find(first_it, std::cend(lines), end_vertices);
98 if(second_it == first_it)
99 throw std::runtime_error
100 {"Empty vertex list detected in file \"" + filename + "\"."};
101
102 Vertices vertex_list;
103 int line_no = 1;
104 for( ; first_it != second_it; ++first_it){
105 try{
106 const auto numerical_vertex = convert_to_vertex(*first_it);
107 vertex_list.push_back(numerical_vertex);
108 }
109 catch(const std::exception& e){
110 std::cerr << e.what() << '\n';
111 const std::string err_msg {"line" + std::to_string(line_no)};
112 throw std::runtime_error {err_msg};
113 }
114 ++line_no;
115 }
116
117 return vertex_list;
118 }
119
120 const Vertex convert_to_vertex(const std::string& str_vertex){
121 std::istringstream iss {str_vertex};
122 double d1, d2, d3;
123 if(!(iss >> d1 >> d2 >> d3) || !(iss >> std::ws).eof() )
124 throw std::runtime_error {"convert_to_vertex() failed."};
125 return {d1, d2, d3};
126 }
127
128
129 #include <numeric>
130 #include <cmath>
131
132 double level_set_function(const Vertex& vertex){
133
134 using std::begin;
135 using std::end;
136 return std::inner_product(begin(vertex), end(vertex), begin(vertex), 0.);
137 }