-
Notifications
You must be signed in to change notification settings - Fork 2
/
PGroup.cpp
executable file
·163 lines (135 loc) · 3.65 KB
/
PGroup.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
#include "PGroup.h"
#include <iostream>
#include <algorithm>
#include "Thread.h"
void PGroup::render(void)
{
// Create a buffer of just the particle data we want and render the array
// in one go. It may seem slower to make this extra copy, but it's
// actually way faster due to the way gfx cards work
std::vector<particleData> buf(particles_.size());
for (size_t i = 0; i < particles_.size(); i++)
{
buf[i].pos = particles_[i]->loc;
buf[i].color = particles_[i]->color;
}
renderParticles(buf);
}
PGroup::PGroup()
{
}
PGroup::~PGroup()
{
reset();
}
Particle* PGroup::newParticle()
{
// Add the particle to the "read" buffer so it gets updated on the
// next frame
Particle *ret = new Particle();
particles_.push_back(ret);
return ret;
}
int PGroup::numParticles(void)
{
return particles_.size();
}
void PGroup::addAction(PActionF* pa)
{
actions_.push_back(pa);
}
void PGroup::startUpdate(float dt)
{
update_dt_ = dt;
}
// Struct used for thread communication
struct threadarg
{
std::list<PActionF*> *actions;
float dt;
std::vector<Particle*> *particles;
int start, end;
std::vector<int> *deadPartInds;
Mutex *dpilock;
};
void *threadfunc(void *arg)
{
threadarg *targ = (threadarg *) arg;
std::vector<Particle*> &particles = *(targ->particles);
std::list<PActionF*> &actions = *(targ->actions);
std::vector<int> &deadPartInds = *(targ->deadPartInds);
Mutex &dpilock = *(targ->dpilock);
float dt = targ->dt;
int start = targ->start;
int end = targ->end;
// Update/integrate each particle
for (int i = start; i < end; i++)
{
Particle *part = particles[i];
if (part->t > 0)
{
// Update each particle first for all actions
std::list<PActionF*>::iterator pfit;
for (pfit = actions.begin(); pfit != actions.end(); pfit++)
(**pfit)(part, dt);
// Integrate
part->update(dt);
}
else
{
// The particle is dead, add it to the list, be safe though
dpilock.lock();
deadPartInds.push_back(i);
dpilock.unlock();
}
}
// Clean up and finish
delete targ;
return 0;
}
void PGroup::update()
{
// Use the dt from startUpdate
float dt = update_dt_;
// Don't do anything if there are no particles
if (particles_.empty())
return;
// index of dead particles
std::vector<int> deadPartInds;
int num_threads = 4;
std::vector<Thread> threads_(num_threads);
Mutex dpilock;
for (int i = 0; i < num_threads; i++)
{
threadarg *arg = new threadarg();
arg->actions = &actions_;
arg->dt = dt;
arg->particles = &particles_;
arg->start = i * (particles_.size() / num_threads);
arg->end = (i+1) * (particles_.size() / num_threads);
arg->deadPartInds = &deadPartInds;
arg->dpilock = &dpilock;
threads_[i].run(threadfunc, arg);
}
for (size_t i = 0; i < threads_.size(); i++)
{
threads_[i].join();
}
// sort in descending order
std::sort(deadPartInds.rbegin(), deadPartInds.rend());
// Remove dead particles
for (size_t i = 0; i < deadPartInds.size(); i++)
{
int pind = deadPartInds[i];
delete particles_[pind];
// Swap trick to quickly remove particles from a vector
std::swap(particles_[pind], particles_.back());
particles_.pop_back();
}
}
void PGroup::reset(void)
{
for (size_t i = 0; i < particles_.size(); i++)
delete particles_[i];
particles_.clear();
}