-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.cpp
179 lines (150 loc) · 5.45 KB
/
driver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/******************************************************
* Driver file for the test nu-spline interpolation *
* algorithm, based on the work of G. Nielson. *
* *
* First edition of the code: April 2015 *
******************************************************/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/multi/geometries/multi_point.hpp>
#include "ComputeNuSpline.cpp"
using namespace std;
void load_options(char* filename, map<string, string> * options);
void load_points(string filename, mp_type *pts);
void load_tensions(string filename, vector<double> *tensions);
/*
* The program expects exactly 1 parameter indicating the file containing IO configuration.
*/
int main(int argc, char** argv) {
/*
* Check for enough input arguments.
*/
if (argc < 2) {
cout << "Input data needs to be provided from external files!" << endl;
cout << "Please use as indicated: " << argv[0] << " config_file" << endl;
return -1;
}
cout << "Reading data..." << endl;
map<string,string> options;
load_options(argv[1], &options);
cout << "Points file: " << options["points_file"] << endl;
cout << "Tensions file: " << options["tensions_file"] << endl;
cout << "Tolerance: " << options["tolerance"] << endl;
cout << "Iterations: " << options["iterations"] << endl;
ofstream ofile;
mp_type points;
vector<double> tensions;
load_points(options["points_file"], &points);
load_tensions(options["tensions_file"], &tensions);
/*
* Display data (for debugging purposes). In the loop below the points read in spherical cartesian form
* are written in cartesian form into the file "points" so they can be later plotted by gnuplot.
*/
cout << "Data read successfully!" << endl << endl;
cout << "Points: " << endl;
ofile.open(options["plot_points_file"].c_str());
for (mp_type::iterator it = points.begin(); it != points.end(); it++) {
cartesian_point p;
transform(*it, p);
cout << " " << dsv(*it) << endl;
ofile << p.get<0>() << " " << p.get<1>() << " " << p.get<2>() << endl;
}
ofile.close();
/*
* Display tension values.
*/
cout << endl << "Tensions:" << endl;
for (vector<double>::iterator it = tensions.begin(); it != tensions.end(); it++) {
cout << *it << " ";
}
cout << endl << endl;
/*
* Create an instance here (initialization allows to retrieve knot values)
*/
ComputeNuSpline cmp = ComputeNuSpline(points, tensions, atof(options["tolerance"].c_str()), atoi(options["iterations"].c_str()), atoi(options["uniform"].c_str()) == 1);
/*
* Display knot values
*/
vector<double> t = cmp.GetTvals();
cout << endl << "Knots:" << endl;
for (vector<double>::iterator it = t.begin(); it != t.end(); it++) {
cout << *it << " ";
}
cout << endl << endl;
cout << "Approximation in course..." << endl;
/*
* Execute the main method of the class.
* The value returned indicates whether the approximation has converged in the steps allowed.
*/
if( cmp.Execute() ) {
cout << endl << "Approximation converged with requested accuracy!" << endl;
} else {
cout << endl << "Approximation reached maximum number of iterations!" << endl;
cout << "Last error is " << cmp.GetLastError() << endl;
}
/*
* Output the control nodes. The output is also written in the file "approx"
* in order to allow plotting by gnuplot.
*/
cout << endl << "The approximated nodes (in cartesian coordinates) are:" << endl;
ofile.open(options["plot_control_pts_file"].c_str());
mp_type d = cmp.GetDvals();
for (mp_type::iterator it = d.begin(); it != d.end(); it++) {
cartesian_point p;
transform(*it, p);
cout << dsv(p) << endl;
ofile << p.get<0>() << " " << p.get<1>() << " " << p.get<2>() << endl;
}
ofile.close();
mp_type sp = cmp.GetNuSpline(atoi(options["spline_points"].c_str()));
ofile.open(options["plot_spline_file"].c_str());
for (mp_type::iterator it = sp.begin(); it != sp.end(); it++) {
// cout << dsv(*it) << endl;
cartesian_point p;
transform(*it, p);
ofile << p.get<0>() << " " << p.get<1>() << " " << p.get<2>() << endl;
}
ofile.close();
cout << endl << "Plotting data... ";
string knots = ( atoi(options["plot_knots"].c_str()) == 1 ? "; knots='" + options["plot_control_pts_file"] + "'" : "");
string args = "sphere='" + options["plot_sphere_file"] + "'; points='" + options["plot_points_file"] +
"'; spline='" + options["plot_spline_file"] + "'" + knots;
string comm = "gnuplot -persist -e \"" + args + "\" " + (atoi(options["plot_knots"].c_str()) == 1 ? "plot/plotall_knots.gnu" : "plot/plotall.gnu");
system(comm.c_str());
cout << endl << " DONE! Exiting." << endl;
return 0;
}
void load_options(char* filename, map<string, string> * options) {
ifstream f;
string key, value;
f.open(filename);
while (f >> key >> value) {
(*options)[key] = value;
}
f.close();
return;
}
void load_points(string filename, mp_type *pts) {
ifstream f;
f.open(filename.c_str());
double phi, theta;
while (f >> phi >> theta) {
append(*pts, spherical_point(phi, theta));
}
f.close();
return;
}
void load_tensions(string filename, vector<double> *tensions) {
ifstream f;
f.open(filename.c_str());
double t;
while(f >> t) {
tensions->push_back(t);
}
f.close();
return;
}