-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfishtank.cpp
212 lines (165 loc) · 6.8 KB
/
fishtank.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <cmath>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
#include "eigen-dir/Eigen/Dense"
using namespace std;
int NUM_FRAMES = 600;
class Boid {
public:
Boid(Eigen::Vector3d position_arg, Eigen::Vector3d velocity_arg) {
position = position_arg;
velocity = velocity_arg;
}
Eigen::Vector3d position;
Eigen::Vector3d velocity;
};
vector<Boid*> boids;
void write_to_file();
void change_positions_of_boids(int num_neighbors, double neighbor_radius);
Eigen::Vector3d collision_avoidance(Boid* b);
Eigen::Vector3d velocity_matching(Boid* b, int num_neighbors, double neighbor_radius);
Eigen::Vector3d flock_centering(Boid* b, int num_neighbors, double neighbor_radius);
int main(int argc, char** argv) {
srand(time(NULL));
string input_file_name = argv[1];
string output_file_name = argv[2];
fstream input_file(input_file_name);
ofstream output_file(output_file_name);
double size, neighbor_radius, mass, collision, centering, velocity, hunger, damping, dt, length;
int num_neighbors, num_boids;
input_file >> size >> neighbor_radius >> num_neighbors >> mass >> collision >> centering >> velocity >> hunger >> damping >> dt >> length;
input_file >> num_boids;
cout << num_boids << endl;
input_file.ignore(); //ignore newline character
string pos_x_string;
string pos_y_string;
string pos_z_string;
string vel_x_string;
string vel_y_string;
string vel_z_string;
double pos_x;
double pos_y;
double pos_z;
double vel_x;
double vel_y;
double vel_z;
for (int i=0; i < num_boids; i++) {
input_file.ignore();
getline(input_file, pos_x_string, ',');
getline(input_file, pos_y_string, ',');
getline(input_file, pos_z_string, ']');
getline(input_file, vel_x_string, ',');
getline(input_file, vel_y_string, ',');
getline(input_file, vel_z_string, ']');
pos_x = stod(pos_x_string.substr(1, pos_x_string.length() - 1));
pos_y = stod(pos_y_string);
pos_z = stod(pos_z_string);
vel_x = stod(vel_x_string.substr(2, vel_x_string.length() - 1));
vel_y = stod(vel_y_string);
vel_z = stod(vel_z_string);
Eigen::Vector3d position(pos_x, pos_y, pos_z);
Eigen::Vector3d velocity(vel_x, vel_y, vel_z);
Boid* new_boid = new Boid(position, velocity);
boids.push_back(new_boid);
}
//write NUM_FRAMES to sample.out
output_file << NUM_FRAMES << "\n";
for (int i=0; i < NUM_FRAMES; i++) {
//write new position and velocity for each boid to sample.out
output_file << num_boids << "\n";
for (int i=0; i < num_boids; i++) {
output_file << "[" << boids[i]->position.x() << ',' << boids[i]->position.y() << ',' << boids[i]->position.z() << ']' << ' ';
output_file << "[" << boids[i]->velocity.x() << ',' << boids[i]->velocity.y() << ',' << boids[i]->velocity.z() << ']' << ' ' << '\n';
}
output_file << 0 << "\n";
change_positions_of_boids(num_neighbors, neighbor_radius);
}
}
void change_positions_of_boids(int num_neighbors, double neighbor_radius) {
Eigen::Vector3d v1, v2, v3;
//Boid* b;
//traverse boids and apply rules to each boid
for (int i=0; i < (int) boids.size(); i++) {
v1 = collision_avoidance(boids[i]);
v2 = velocity_matching(boids[i], num_neighbors, neighbor_radius);
v3 = flock_centering(boids[i], num_neighbors, neighbor_radius);
boids[i]->velocity = boids[i]->velocity + v1 + v2 + v3;
boids[i]->velocity = boids[i]->velocity * .999;
double new_position_x = (double) (boids[i]->position.x() + boids[i]->velocity.x());
double new_position_y = (double) (boids[i]->position.y() + boids[i]->velocity.y());
double new_position_z = (double) (boids[i]->position.z() + boids[i]->velocity.z());
if (new_position_x >= .5 || new_position_x < -.5)
boids[i]->velocity.x() = boids[i]->velocity.x() * -1;
if (new_position_y >= .5 || new_position_y < -.5)
boids[i]->velocity.y() = boids[i]->velocity.y() * -1;
if (new_position_z >= .5 || new_position_z < -.5)
boids[i]->velocity.z() = boids[i]->velocity.z() * -1;
boids[i]->position = boids[i]->position + boids[i]->velocity;
}
}
//rule for collision avoidance
Eigen::Vector3d collision_avoidance(Boid* b) {
Eigen::Vector3d delta(0,0,0);
Boid* curr_boid;
int neighbors_seen = 0;
for (int i=0; i < (int) boids.size(); i++) {
curr_boid = boids[i];
if (curr_boid != b) {
Eigen::Vector3d distance;
distance.x() = curr_boid->position.x() - b->position.x();
distance.y() = curr_boid->position.y() - b->position.y();
distance.z() = curr_boid->position.z() - b->position.z();
// modify delta if distance between curr_boid and b is less than 0.001
if (sqrt(distance.x() * distance.x() + distance.y() * distance.y() + distance.z() * distance.z()) < .001) {
delta = delta - (curr_boid->position - b-> position);
}
}
}
//do collision avoidance on edges too?
return delta;
}
//rule for velocity matching
Eigen::Vector3d velocity_matching(Boid* b, int num_neighbors, double neighbor_radius) {
Eigen::Vector3d perceived_velocity(0,0,0);
Boid* curr_boid;
for (int i=0; i < (int) boids.size(); i++) {
curr_boid = boids[i];
Eigen::Vector3d distance;
distance.x() = curr_boid->position.x() - b->position.x();
distance.y() = curr_boid->position.y() - b->position.y();
distance.z() = curr_boid->position.z() - b->position.z();
double real_radius = (double) sqrt(distance.x() * distance.x() + distance.y() * distance.y() + distance.z() * distance.z());
int neighbors_count = 0;
if (curr_boid != b && sqrt(distance.x() * distance.x() + distance.y() * distance.y() + distance.z() * distance.z()) < neighbor_radius && neighbors_count < num_neighbors) {
perceived_velocity = perceived_velocity + curr_boid->velocity;
neighbors_count++;
}
}
perceived_velocity = perceived_velocity / (boids.size() - 1);
return (perceived_velocity - b->velocity) / 8;
}
//rule for flock centering
Eigen::Vector3d flock_centering(Boid* b, int num_neighbors, double neighbor_radius) {
Eigen::Vector3d perceived_center(0,0,0);
Boid* curr_boid;
int neighbors_count = 0;
for (int i=0; i < (int) boids.size(); i++) {
curr_boid = boids[i];
Eigen::Vector3d distance;
distance.x() = curr_boid->position.x() - b->position.x();
distance.y() = curr_boid->position.y() - b->position.y();
distance.z() = curr_boid->position.z() - b->position.z();
double real_radius = (double) sqrt(distance.x() * distance.x() + distance.y() * distance.y() + distance.z() * distance.z());
if (b != curr_boid && real_radius < neighbor_radius && neighbors_count < num_neighbors) {
perceived_center += curr_boid->position;
neighbors_count++;
}
}
perceived_center /= ((int) boids.size() - 1);
return perceived_center / 100;
}