Skip to content

Commit acaaaae

Browse files
authored
Expose more properties for VolumeParticleEmitter in Python API (#221)
1 parent 1d8b746 commit acaaaae

5 files changed

Lines changed: 151 additions & 9 deletions

File tree

include/jet/volume_particle_emitter3.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class VolumeParticleEmitter3 final : public ParticleEmitter3 {
8484
//!
8585
void setIsOneShot(bool newValue);
8686

87-
//! Returns trhe if particles can be overlapped.
87+
//! Returns true if particles can be overlapped.
8888
bool allowOverlapping() const;
8989

9090
//!

src/examples/python_examples/apic_example01.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def main():
3434
solver.collider = collider
3535

3636
# Visualization
37-
fig, ax = plt.subplots()
38-
ax.set_aspect('equal')
37+
fig = plt.figure(figsize=(3, 6))
38+
ax = fig.add_axes([0, 0, 1, 1], frameon=False)
3939
ax.set_xlim(0, 1), ax.set_xticks([])
4040
ax.set_ylim(0, 2), ax.set_yticks([])
4141

src/examples/python_examples/apic_example02.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ def main():
3838
solver.collider = collider
3939

4040
# Visualization
41-
fig, ax = plt.subplots()
42-
ax.set_aspect('equal')
41+
fig = plt.figure(figsize=(3, 6))
42+
ax = fig.add_axes([0, 0, 1, 1], frameon=False)
4343
ax.set_xlim(0, 1), ax.set_xticks([])
4444
ax.set_ylim(0, 2), ax.set_yticks([])
4545

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Copyright (c) 2018 Doyub Kim
5+
6+
I am making my contributions/submissions to this project solely in my personal
7+
capacity and am not conveying any rights to any intellectual property of any
8+
third parties.
9+
"""
10+
11+
from pyjet import *
12+
import math
13+
import numpy as np
14+
import matplotlib.pyplot as plt
15+
import matplotlib.animation as animation
16+
17+
ANIM_NUM_FRAMES = 360
18+
ANIM_FPS = 60
19+
20+
21+
def main():
22+
"""
23+
This example demonstrates how to animate emitter as well as collider properties.
24+
"""
25+
26+
# Create APIC solver
27+
resX = 24
28+
solver = ApicSolver3(resolution=(resX, 2 * resX, resX), domainSizeX=1.0)
29+
solver.useCompressedLinearSystem = True
30+
31+
# Setup emitter
32+
sphere = Sphere3(center=(0.5, 1.0, 0.5), radius=0.15)
33+
emitter = VolumeParticleEmitter3(
34+
implicitSurface=sphere, spacing=1.0 / (2 * resX), isOneShot=False, initialVelocity=(0, 0, 0))
35+
solver.particleEmitter = emitter
36+
37+
# Setup collider
38+
anotherSphere = Sphere3(center=(0.5, 0.5, 0.5), radius=0.15)
39+
collider = RigidBodyCollider3(surface=anotherSphere)
40+
solver.collider = collider
41+
42+
# Visualization
43+
fig = plt.figure(figsize=(3, 6))
44+
ax = fig.add_axes([0, 0, 1, 1], frameon=False)
45+
ax.set_xlim(0, 1), ax.set_xticks([])
46+
ax.set_ylim(0, 2), ax.set_yticks([])
47+
48+
# Make first frame
49+
frame = Frame(0, 1.0 / ANIM_FPS)
50+
solver.update(frame)
51+
frame.advance()
52+
53+
# Visualization
54+
pos = np.array(solver.particleSystemData.positions, copy=False)
55+
scat = ax.scatter(pos[:, 0], pos[:, 1])
56+
57+
# Animation
58+
def updatefig(*args):
59+
# Change emitter velocity after frame 50
60+
if frame.index == 50:
61+
emitter.initialVelocity = (0, 3, 0)
62+
# Stop emitter after frame 100
63+
if frame.index == 100:
64+
emitter.isOneShot = True
65+
# Animate collider's position (and thus the velocity which is its derivative)
66+
collider.surface.transform = Transform3(translation=(0.2 * math.sin(10 * frame.timeInSeconds()), 0, 0))
67+
collider.linearVelocity = (2.0 * math.cos(10 * frame.timeInSeconds()), 0, 0)
68+
69+
solver.update(frame)
70+
frame.advance()
71+
pos = np.array(solver.particleSystemData.positions, copy=False)
72+
scat.set_offsets(np.vstack((pos[:, 0], pos[:, 1])).transpose())
73+
return scat,
74+
75+
anim = animation.FuncAnimation(fig, updatefig, frames=ANIM_NUM_FRAMES,
76+
interval=1, blit=True)
77+
plt.show()
78+
79+
80+
if __name__ == '__main__':
81+
Logging.mute()
82+
main()

src/python/volume_particle_emitter.cpp

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ void addVolumeParticleEmitter2(py::module& m) {
8585
if (kwargs.contains("spacing")) {
8686
spacing = kwargs["spacing"].cast<double>();
8787
}
88-
if (kwargs.contains("initialVel")) {
89-
initialVel = objectToVector2D(kwargs["initialVel"]);
88+
if (kwargs.contains("initialVelocity")) {
89+
initialVel = objectToVector2D(kwargs["initialVelocity"]);
9090
}
9191
if (kwargs.contains("maxNumberOfParticles")) {
9292
maxNumberOfParticles =
@@ -119,6 +119,36 @@ void addVolumeParticleEmitter2(py::module& m) {
119119
(optional), whether it's one shot or not (optional), whether it
120120
should allow overlapping or not (optional), and random seed
121121
(optional).
122+
)pbdoc")
123+
.def_property("jitter", &VolumeParticleEmitter2::jitter,
124+
&VolumeParticleEmitter2::setJitter, R"pbdoc(
125+
Jitter amount between 0 and 1.
126+
)pbdoc")
127+
.def_property("isOneShot", &VolumeParticleEmitter2::isOneShot,
128+
&VolumeParticleEmitter2::setIsOneShot, R"pbdoc(
129+
True if particles should be emitted just once.
130+
)pbdoc")
131+
.def_property("allowOverlapping",
132+
&VolumeParticleEmitter2::allowOverlapping,
133+
&VolumeParticleEmitter2::setAllowOverlapping, R"pbdoc(
134+
True if particles can be overlapped.
135+
)pbdoc")
136+
.def_property(
137+
"allowOverlapping", &VolumeParticleEmitter2::maxNumberOfParticles,
138+
&VolumeParticleEmitter2::setMaxNumberOfParticles, R"pbdoc(
139+
Max number of particles to be emitted.
140+
)pbdoc")
141+
.def_property("spacing", &VolumeParticleEmitter2::spacing,
142+
&VolumeParticleEmitter2::setSpacing, R"pbdoc(
143+
The spacing between particles.
144+
)pbdoc")
145+
.def_property(
146+
"initialVelocity", &VolumeParticleEmitter2::initialVelocity,
147+
[](VolumeParticleEmitter2& instance, py::object newInitialVel) {
148+
instance.setInitialVelocity(objectToVector2D(newInitialVel));
149+
},
150+
R"pbdoc(
151+
The initial velocity of the particles.
122152
)pbdoc");
123153
}
124154

@@ -192,8 +222,8 @@ void addVolumeParticleEmitter3(py::module& m) {
192222
if (kwargs.contains("spacing")) {
193223
spacing = kwargs["spacing"].cast<double>();
194224
}
195-
if (kwargs.contains("initialVel")) {
196-
initialVel = objectToVector3D(kwargs["initialVel"]);
225+
if (kwargs.contains("initialVelocity")) {
226+
initialVel = objectToVector3D(kwargs["initialVelocity"]);
197227
}
198228
if (kwargs.contains("maxNumberOfParticles")) {
199229
maxNumberOfParticles =
@@ -226,5 +256,35 @@ void addVolumeParticleEmitter3(py::module& m) {
226256
(optional), whether it's one shot or not (optional), whether it
227257
should allow overlapping or not (optional), and random seed
228258
(optional).
259+
)pbdoc")
260+
.def_property("jitter", &VolumeParticleEmitter3::jitter,
261+
&VolumeParticleEmitter3::setJitter, R"pbdoc(
262+
Jitter amount between 0 and 1.
263+
)pbdoc")
264+
.def_property("isOneShot", &VolumeParticleEmitter3::isOneShot,
265+
&VolumeParticleEmitter3::setIsOneShot, R"pbdoc(
266+
True if particles should be emitted just once.
267+
)pbdoc")
268+
.def_property("allowOverlapping",
269+
&VolumeParticleEmitter3::allowOverlapping,
270+
&VolumeParticleEmitter3::setAllowOverlapping, R"pbdoc(
271+
True if particles can be overlapped.
272+
)pbdoc")
273+
.def_property(
274+
"allowOverlapping", &VolumeParticleEmitter3::maxNumberOfParticles,
275+
&VolumeParticleEmitter3::setMaxNumberOfParticles, R"pbdoc(
276+
Max number of particles to be emitted.
277+
)pbdoc")
278+
.def_property("spacing", &VolumeParticleEmitter3::spacing,
279+
&VolumeParticleEmitter3::setSpacing, R"pbdoc(
280+
The spacing between particles.
281+
)pbdoc")
282+
.def_property(
283+
"initialVelocity", &VolumeParticleEmitter3::initialVelocity,
284+
[](VolumeParticleEmitter3& instance, py::object newInitialVel) {
285+
instance.setInitialVelocity(objectToVector3D(newInitialVel));
286+
},
287+
R"pbdoc(
288+
The initial velocity of the particles.
229289
)pbdoc");
230290
}

0 commit comments

Comments
 (0)