Skip to content

Commit 6e2623a

Browse files
doyubkimutilForever
authored andcommitted
Animation support for emitters and collider sets in Python (#229)
* [WIP] Animation support for emitters * Adding collider set and tests * More tests and fixes
1 parent 92294da commit 6e2623a

18 files changed

Lines changed: 797 additions & 140 deletions

include/jet/volume_particle_emitter2.h

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <jet/implicit_surface2.h>
1313
#include <jet/particle_emitter2.h>
1414
#include <jet/point_generator2.h>
15+
1516
#include <limits>
1617
#include <memory>
1718
#include <random>
@@ -33,9 +34,11 @@ class VolumeParticleEmitter2 final : public ParticleEmitter2 {
3334
//! the particle generation region.
3435
//!
3536
//! \param[in] implicitSurface The implicit surface.
36-
//! \param[in] bounds The max region.
37+
//! \param[in] maxRegion The max region.
3738
//! \param[in] spacing The spacing between particles.
38-
//! \param[in] initialVel The initial velocity.
39+
//! \param[in] initialVel The initial velocity of new particles.
40+
//! \param[in] linearVel The linear velocity of the emitter.
41+
//! \param[in] angularVel The angular velocity of the emitter.
3942
//! \param[in] maxNumberOfParticles The max number of particles to be
4043
//! emitted.
4144
//! \param[in] jitter The jitter amount between 0 and 1.
@@ -46,9 +49,11 @@ class VolumeParticleEmitter2 final : public ParticleEmitter2 {
4649
//!
4750
VolumeParticleEmitter2(
4851
const ImplicitSurface2Ptr& implicitSurface,
49-
const BoundingBox2D& bounds,
52+
const BoundingBox2D& maxRegion,
5053
double spacing,
5154
const Vector2D& initialVel = Vector2D(),
55+
const Vector2D& linearVel = Vector2D(),
56+
double angularVel = 0.0,
5257
size_t maxNumberOfParticles = kMaxSize,
5358
double jitter = 0.0,
5459
bool isOneShot = true,
@@ -65,6 +70,18 @@ class VolumeParticleEmitter2 final : public ParticleEmitter2 {
6570
//!
6671
void setPointGenerator(const PointGenerator2Ptr& newPointsGen);
6772

73+
//! Returns source surface.
74+
const ImplicitSurface2Ptr& surface() const;
75+
76+
//! Sets the source surface.
77+
void setSurface(const ImplicitSurface2Ptr& newSurface);
78+
79+
//! Returns max particle gen region.
80+
const BoundingBox2D& maxRegion() const;
81+
82+
//! Sets the max particle gen region.
83+
void setMaxRegion(const BoundingBox2D& newBox);
84+
6885
//! Returns jitter amount.
6986
double jitter() const;
7087

@@ -117,6 +134,18 @@ class VolumeParticleEmitter2 final : public ParticleEmitter2 {
117134
//! Returns the initial velocity of the particles.
118135
void setInitialVelocity(const Vector2D& newInitialVel);
119136

137+
//! Returns the linear velocity of the emitter.
138+
Vector2D linearVelocity() const;
139+
140+
//! Sets the linear velocity of the emitter.
141+
void setLinearVelocity(const Vector2D& newLinearVel);
142+
143+
//! Returns the angular velocity of the emitter.
144+
double angularVelocity() const;
145+
146+
//! Sets the linear velocity of the emitter.
147+
void setAngularVelocity(double newAngularVel);
148+
120149
//! Returns builder fox VolumeParticleEmitter2.
121150
static Builder builder();
122151

@@ -127,6 +156,8 @@ class VolumeParticleEmitter2 final : public ParticleEmitter2 {
127156
BoundingBox2D _bounds;
128157
double _spacing;
129158
Vector2D _initialVel;
159+
Vector2D _linearVel;
160+
double _angularVel = 0.0;
130161
PointGenerator2Ptr _pointsGen;
131162

132163
size_t _maxNumberOfParticles = kMaxSize;
@@ -152,6 +183,8 @@ class VolumeParticleEmitter2 final : public ParticleEmitter2 {
152183
Array1<Vector2D>* newVelocities);
153184

154185
double random();
186+
187+
Vector2D velocityAt(const Vector2D& point) const;
155188
};
156189

157190
//! Shared pointer for the VolumeParticleEmitter2 type.
@@ -178,6 +211,12 @@ class VolumeParticleEmitter2::Builder final {
178211
//! Returns builder with initial velocity.
179212
Builder& withInitialVelocity(const Vector2D& initialVel);
180213

214+
//! Returns builder with linear velocity.
215+
Builder& withLinearVelocity(const Vector2D& linearVel);
216+
217+
//! Returns builder with angular velocity.
218+
Builder& withAngularVelocity(double angularVel);
219+
181220
//! Returns builder with max number of particles.
182221
Builder& withMaxNumberOfParticles(size_t maxNumberOfParticles);
183222

@@ -204,7 +243,9 @@ class VolumeParticleEmitter2::Builder final {
204243
bool _isBoundSet = false;
205244
BoundingBox2D _bounds;
206245
double _spacing = 0.1;
207-
Vector2D _initialVel{0, 0};
246+
Vector2D _initialVel;
247+
Vector2D _linearVel;
248+
double _angularVel = 0.0;
208249
size_t _maxNumberOfParticles = kMaxSize;
209250
double _jitter = 0.0;
210251
bool _isOneShot = true;

include/jet/volume_particle_emitter3.h

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <jet/implicit_surface3.h>
1212
#include <jet/particle_emitter3.h>
1313
#include <jet/point_generator3.h>
14+
1415
#include <limits>
1516
#include <memory>
1617
#include <random>
@@ -32,9 +33,11 @@ class VolumeParticleEmitter3 final : public ParticleEmitter3 {
3233
//! the particle generation region.
3334
//!
3435
//! \param[in] implicitSurface The implicit surface.
35-
//! \param[in] bounds The max region.
36+
//! \param[in] maxRegion The max region.
3637
//! \param[in] spacing The spacing between particles.
3738
//! \param[in] initialVel The initial velocity.
39+
//! \param[in] linearVel The linear velocity of the emitter.
40+
//! \param[in] angularVel The angular velocity of the emitter.
3841
//! \param[in] maxNumberOfParticles The max number of particles to be
3942
//! emitted.
4043
//! \param[in] jitter The jitter amount between 0 and 1.
@@ -45,9 +48,11 @@ class VolumeParticleEmitter3 final : public ParticleEmitter3 {
4548
//!
4649
VolumeParticleEmitter3(
4750
const ImplicitSurface3Ptr& implicitSurface,
48-
const BoundingBox3D& bounds,
51+
const BoundingBox3D& maxRegion,
4952
double spacing,
5053
const Vector3D& initialVel = Vector3D(),
54+
const Vector3D& linearVel = Vector3D(),
55+
const Vector3D& angularVel = Vector3D(),
5156
size_t maxNumberOfParticles = kMaxSize,
5257
double jitter = 0.0,
5358
bool isOneShot = true,
@@ -64,6 +69,18 @@ class VolumeParticleEmitter3 final : public ParticleEmitter3 {
6469
//!
6570
void setPointGenerator(const PointGenerator3Ptr& newPointsGen);
6671

72+
//! Returns source surface.
73+
const ImplicitSurface3Ptr& surface() const;
74+
75+
//! Sets the source surface.
76+
void setSurface(const ImplicitSurface3Ptr& newSurface);
77+
78+
//! Returns max particle gen region.
79+
const BoundingBox3D& maxRegion() const;
80+
81+
//! Sets the max particle gen region.
82+
void setMaxRegion(const BoundingBox3D& newBox);
83+
6784
//! Returns jitter amount.
6885
double jitter() const;
6986

@@ -116,6 +133,18 @@ class VolumeParticleEmitter3 final : public ParticleEmitter3 {
116133
//! Returns the initial velocity of the particles.
117134
void setInitialVelocity(const Vector3D& newInitialVel);
118135

136+
//! Returns the linear velocity of the emitter.
137+
Vector3D linearVelocity() const;
138+
139+
//! Sets the linear velocity of the emitter.
140+
void setLinearVelocity(const Vector3D& newLinearVel);
141+
142+
//! Returns the angular velocity of the emitter.
143+
Vector3D angularVelocity() const;
144+
145+
//! Sets the linear velocity of the emitter.
146+
void setAngularVelocity(const Vector3D& newAngularVel);
147+
119148
//! Returns builder fox VolumeParticleEmitter3.
120149
static Builder builder();
121150

@@ -126,6 +155,8 @@ class VolumeParticleEmitter3 final : public ParticleEmitter3 {
126155
BoundingBox3D _bounds;
127156
double _spacing;
128157
Vector3D _initialVel;
158+
Vector3D _linearVel;
159+
Vector3D _angularVel;
129160
PointGenerator3Ptr _pointsGen;
130161

131162
size_t _maxNumberOfParticles = kMaxSize;
@@ -151,6 +182,8 @@ class VolumeParticleEmitter3 final : public ParticleEmitter3 {
151182
Array1<Vector3D>* newVelocities);
152183

153184
double random();
185+
186+
Vector3D velocityAt(const Vector3D& point) const;
154187
};
155188

156189
//! Shared pointer for the VolumeParticleEmitter3 type.
@@ -177,6 +210,12 @@ class VolumeParticleEmitter3::Builder final {
177210
//! Returns builder with initial velocity.
178211
Builder& withInitialVelocity(const Vector3D& initialVel);
179212

213+
//! Returns builder with linear velocity.
214+
Builder& withLinearVelocity(const Vector3D& linearVel);
215+
216+
//! Returns builder with angular velocity.
217+
Builder& withAngularVelocity(const Vector3D& angularVel);
218+
180219
//! Returns builder with max number of particles.
181220
Builder& withMaxNumberOfParticles(size_t maxNumberOfParticles);
182221

@@ -203,7 +242,9 @@ class VolumeParticleEmitter3::Builder final {
203242
bool _isBoundSet = false;
204243
BoundingBox3D _bounds;
205244
double _spacing = 0.1;
206-
Vector3D _initialVel{0, 0, 0};
245+
Vector3D _initialVel;
246+
Vector3D _linearVel;
247+
Vector3D _angularVel;
207248
size_t _maxNumberOfParticles = kMaxSize;
208249
double _jitter = 0.0;
209250
bool _isOneShot = true;

src/examples/python_examples/collider_emitter_anim_example.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ def main():
3131
# Setup emitter
3232
sphere = Sphere3(center=(0.5, 1.0, 0.5), radius=0.15)
3333
emitter = VolumeParticleEmitter3(
34-
implicitSurface=sphere, spacing=1.0 / (2 * resX), isOneShot=False, initialVelocity=(0, 0, 0))
34+
implicitSurface=sphere,
35+
maxRegion=solver.gridSystemData.boundingBox,
36+
spacing=1.0 / (2 * resX),
37+
isOneShot=False,
38+
initialVelocity=(0, 0, 0))
3539
solver.particleEmitter = emitter
3640

3741
# Setup collider
@@ -62,6 +66,10 @@ def updatefig(*args):
6266
# Stop emitter after frame 100
6367
if frame.index == 100:
6468
emitter.isOneShot = True
69+
# Animate emitter's position (and thus the velocity which is its derivative)
70+
emitter.surface.transform = Transform3(translation=(0.1 * math.sin(5 * frame.timeInSeconds()), 0, 0))
71+
emitter.linearVelocity = (0.5 * math.cos(5 * frame.timeInSeconds()), 0, 0)
72+
6573
# Animate collider's position (and thus the velocity which is its derivative)
6674
collider.surface.transform = Transform3(translation=(0.2 * math.sin(10 * frame.timeInSeconds()), 0, 0))
6775
collider.linearVelocity = (2.0 * math.cos(10 * frame.timeInSeconds()), 0, 0)

0 commit comments

Comments
 (0)