-
Notifications
You must be signed in to change notification settings - Fork 5
/
mass_spring.cpp
97 lines (78 loc) · 2.96 KB
/
mass_spring.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
/**
* @file mass_spring.cpp
* Implementation of mass-spring system using Graph
*
* @brief Reads in two files specified on the command line.
* First file: 3D Points (one per line) defined by three doubles
* Second file: Tetrahedra (one per line) defined by 4 indices into the point
* list
*/
#include "CME212/SFML_Viewer.hpp"
#include "mass_spring.hpp"
int main(int argc, char** argv)
{
// Check arguments
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " NODES_FILE TETS_FILE\n";
exit(1);
}
// Construct an empty graph
GraphType graph;
// Create a nodes_file from the first input argument
std::ifstream nodes_file(argv[1]);
// Interpret each line of the nodes_file as a 3D Point and add to the Graph
Point p;
std::vector<typename GraphType::node_type> nodes;
while (CME212::getline_parsed(nodes_file, p))
nodes.push_back(graph.add_node(p));
// Create a tets_file from the second input argument
std::ifstream tets_file(argv[2]);
// Interpret each line of the tets_file as four ints which refer to nodes
std::array<int,4> t;
while (CME212::getline_parsed(tets_file, t)) {
graph.add_edge(nodes[t[0]], nodes[t[1]]);
graph.add_edge(nodes[t[0]], nodes[t[2]]);
#if 0
// Diagonal edges: include as of HW2 #2
graph.add_edge(nodes[t[0]], nodes[t[3]]);
graph.add_edge(nodes[t[1]], nodes[t[2]]);
#endif
graph.add_edge(nodes[t[1]], nodes[t[3]]);
graph.add_edge(nodes[t[2]], nodes[t[3]]);
}
// HW2 #1 YOUR CODE HERE
// Set initial conditions for your nodes, if necessary.
// Print out the stats
std::cout << graph.num_nodes() << " " << graph.num_edges() << std::endl;
// Launch the Viewer
CME212::SFML_Viewer viewer;
auto node_map = viewer.empty_node_map(graph);
viewer.add_nodes(graph.node_begin(), graph.node_end(), node_map);
viewer.add_edges(graph.edge_begin(), graph.edge_end(), node_map);
viewer.center_view();
// We want viewer interaction and the simulation at the same time
// Viewer is thread-safe, so launch the simulation in a child thread
bool interrupt_sim_thread = false;
auto sim_thread = std::thread([&](){
// Begin the mass-spring simulation
double dt = 0.001;
double t_start = 0;
double t_end = 5.0;
for (double t = t_start; t < t_end && !interrupt_sim_thread; t += dt) {
//std::cout << "t = " << t << std::endl;
symp_euler_step(graph, t, dt, Problem1Force());
// Update viewer with nodes' new positions
viewer.add_nodes(graph.node_begin(), graph.node_end(), node_map);
viewer.set_label(t);
// These lines slow down the animation for small graphs, like grid0_*.
// Feel free to remove them or tweak the constants.
if (graph.size() < 100)
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}); // simulation thread
viewer.event_loop();
// If we return from the event loop, we've killed the window.
interrupt_sim_thread = true;
sim_thread.join();
return 0;
}