-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-wasm.cpp
77 lines (62 loc) · 1.65 KB
/
main-wasm.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
#include <iostream>
#include <emscripten.h>
#include <memory>
#include <vector>
#include "body.h"
#include "quad.h"
#include "bhtree.h"
std::vector<std::shared_ptr<Body>> bodies;
double R;
void _init(int n_bodies, double *x_pos, double *y_pos, double *x_vel, double *y_vel, double *mass, double radius) {
bodies.clear();
R = radius;
for (int i = 0; i < n_bodies; i++) {
bodies.push_back(std::make_shared<Body>(x_pos[i], y_pos[i], x_vel[i], y_vel[i], mass[i]));
}
}
void _step(double dt) {
std::shared_ptr<Quad> quad = std::make_shared<Quad>(0, 0, R);
std::shared_ptr<BHTree> tree = std::make_shared<BHTree>(quad);
for (const auto &body : bodies) {
if (body->in(quad)) {
tree->insert(body);
}
}
for (const auto &body : bodies) {
body->resetForce();
tree->updateForce(body);
body->update(dt);
}
}
double *_getXPos() {
auto xPos = (double *) malloc(bodies.size() * sizeof(double));
for (int i = 0; i < bodies.size(); i++) {
xPos[i] = bodies[i]->x;
}
return xPos;
}
double *_getYPos() {
auto yPos = (double *) malloc(bodies.size() * sizeof(double));
for (int i = 0; i < bodies.size(); i++) {
yPos[i] = bodies[i]->y;
}
return yPos;
}
extern "C" {
void init(int n_bodies, double *x_pos, double *y_pos, double *x_vel, double *y_vel, double *mass, double radius) {
_init(n_bodies, x_pos, y_pos, x_vel, y_vel, mass, radius);
}
void step(double dt) {
_step(dt);
}
double *getXPos() {
return _getXPos();
}
double *getYPos() {
return _getYPos();
}
}
int main() {
emscripten_exit_with_live_runtime();
return 0;
}