motion-content 0.2.0

Backend-independent images, text, vectors, particles and artwork
Documentation
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! Deterministic flow sampling and stable-order population forces.

use super::*;
use motion_core::{Point, Rect};

/// Spatial attenuation with no singular denominator at a force center.
#[derive(Clone)]
pub enum ForceFalloff {
    /// Constant strength inside the authored radius.
    Constant,
    /// Linear falloff to zero at the radius.
    Linear,
    /// Softened inverse-square distance.
    InverseSquare,
    /// Explicit distance-to-strength curve.
    Curve(ParticleCurve),
}

/// Radius-bounded attraction/repulsion using an explicit attenuation policy.
#[derive(Clone)]
pub struct RadialForce {
    /// Force center.
    pub center: Point,
    /// Signed acceleration scale.
    pub strength: f32,
    /// Finite positive influence radius.
    pub radius: f32,
    /// Spatial attenuation.
    pub falloff: ForceFalloff,
}

impl ParticleModifier for RadialForce {
    fn apply(
        &self,
        particle: &mut Particle,
        step: f32,
        _: Time,
        _: &mut Random,
    ) -> Result<(), ValidationError> {
        if !self.radius.is_finite()
            || self.radius <= 0.0
            || !self.strength.is_finite()
            || !self.center.x.is_finite()
            || !self.center.y.is_finite()
        {
            return Err(invalid(
                "force",
                "Require finite force center/strength and positive radius",
            ));
        }
        let delta = [
            self.center.x as f32 - particle.position[0],
            self.center.y as f32 - particle.position[1],
        ];
        let distance = libm::hypotf(delta[0], delta[1]);
        if distance >= self.radius || distance == 0.0 {
            return Ok(());
        }
        let amount = match &self.falloff {
            ForceFalloff::Constant => 1.0,
            ForceFalloff::Linear => 1.0 - distance / self.radius,
            ForceFalloff::InverseSquare => 1.0 / distance.max(1.0).powi(2),
            ForceFalloff::Curve(curve) => curve.sample(distance / self.radius)?,
        };
        for (velocity, delta) in particle.velocity.iter_mut().zip(delta) {
            *velocity += delta / distance * self.strength * amount * step;
        }
        particle.validate()
    }
}

/// Divergence-free curl of the shared seeded Perlin potential, advected over time.
#[derive(Clone, Copy, Debug)]
pub struct CurlNoise {
    /// Positive spatial frequency.
    pub frequency: f64,
    /// Signed acceleration multiplier.
    pub strength: f64,
    /// Explicit potential seed.
    pub seed: u32,
    /// Potential advection rate per second.
    pub phase_speed: f64,
}

fn shared_curl(
    position: [f32; 2],
    frequency: f64,
    phase: f64,
    seed: u32,
) -> Result<[f64; 2], ValidationError> {
    if !frequency.is_finite() || frequency <= 0.0 || !phase.is_finite() {
        return Err(invalid(
            "curl",
            "Require positive finite frequency and finite phase",
        ));
    }
    let point = position.map(|value| f64::from(value) * frequency + phase);
    if !motion_core::noise::lattice_supported(point) {
        return Err(invalid(
            "curl",
            "Curl coordinates exceed signed 32-bit lattice support",
        ));
    }
    Ok(motion_core::noise::curl2(point, seed))
}

impl ParticleModifier for CurlNoise {
    fn apply(
        &self,
        particle: &mut Particle,
        step: f32,
        time: Time,
        _: &mut Random,
    ) -> Result<(), ValidationError> {
        if !self.strength.is_finite()
            || !self.phase_speed.is_finite()
            || !step.is_finite()
            || step < 0.0
        {
            return Err(invalid(
                "curl",
                "Require finite strength, phase speed and nonnegative step",
            ));
        }
        let direction = shared_curl(
            particle.position,
            self.frequency,
            time.as_seconds() * self.phase_speed,
            self.seed,
        )?;
        let velocity: [f32; 2] = std::array::from_fn(|axis| {
            (f64::from(particle.velocity[axis]) + direction[axis] * self.strength * f64::from(step))
                as f32
        });
        if velocity.into_iter().any(|value| !value.is_finite()) {
            return Err(invalid(
                "curl",
                "Curl velocity exceeds finite particle support",
            ));
        }
        particle.velocity = velocity;
        particle.validate()
    }

    fn work(&self) -> u64 {
        96
    }
}

/// Immutable source for an evolving velocity field.
#[derive(Clone)]
pub enum FlowSource {
    /// Two-dimensional curl of the shared analytic Perlin potential.
    Curl {
        /// Explicit stable seed.
        seed: u32,
        /// Positive spatial frequency.
        frequency: f64,
    },
    /// Red/green channels interpreted as a normalized vector map.
    Image {
        /// Immutable premultiplied material whose straight RG encodes vectors.
        image: Arc<crate::ImageAsset>,
        /// Local domain mapped onto the image.
        bounds: Rect,
    },
}

/// Time-evolving noise or image flow, with no mutable frame cursor.
#[derive(Clone)]
pub struct FlowField {
    /// Field material.
    pub source: FlowSource,
    /// Acceleration scale.
    pub strength: f32,
    /// Phase rate per second.
    pub phase_speed: f64,
}

impl FlowField {
    /// Constructs an explicit shared two-dimensional curl source.
    pub fn curl(seed: u32, frequency: f64, strength: f32) -> Self {
        Self {
            source: FlowSource::Curl { seed, frequency },
            strength,
            phase_speed: 1.0,
        }
    }
}

impl ParticleModifier for FlowField {
    fn apply(
        &self,
        particle: &mut Particle,
        step: f32,
        time: Time,
        _: &mut Random,
    ) -> Result<(), ValidationError> {
        if !self.strength.is_finite() || !self.phase_speed.is_finite() {
            return Err(invalid("flow", "Require finite strength and phase speed"));
        }
        let phase = time.as_seconds() * self.phase_speed;
        let direction = match &self.source {
            FlowSource::Curl { seed, frequency } => {
                shared_curl(particle.position, *frequency, phase, *seed)?.map(|value| value as f32)
            }
            FlowSource::Image { image, bounds } => {
                motion_core::drawing::validate_rect(*bounds, "particles.flow", "bounds")?;
                if bounds.width() <= 0.0 || bounds.height() <= 0.0 {
                    return Err(invalid("flow", "Require positive flow image bounds"));
                }
                let horizontal = ((f64::from(particle.position[0]) - bounds.left) / bounds.width()
                    + phase)
                    .rem_euclid(1.0);
                let vertical = ((f64::from(particle.position[1]) - bounds.top) / bounds.height())
                    .clamp(0.0, 1.0);
                let sample = image.levels()[0]
                    .pixel(
                        (horizontal * f64::from(image.width())) as u32,
                        ((vertical * f64::from(image.height())) as u32).min(image.height() - 1),
                    )
                    .ok_or_else(|| invalid("flow", "Missing image vector"))?;
                if sample[3] == 0.0 {
                    [0.0; 2]
                } else {
                    [
                        sample[0] / sample[3] * 2.0 - 1.0,
                        sample[1] / sample[3] * 2.0 - 1.0,
                    ]
                }
            }
        };
        for (velocity, direction) in particle.velocity.iter_mut().zip(direction) {
            *velocity += direction * self.strength * step;
        }
        particle.validate()
    }

    fn work(&self) -> u64 {
        if matches!(self.source, FlowSource::Curl { .. }) {
            96
        } else {
            1
        }
    }
}

/// Spatially hashed flocking evaluated from one immutable population snapshot.
#[derive(Clone, Copy, Debug)]
pub struct Boids {
    /// Positive neighbor radius.
    pub radius: f64,
    /// Separation strength.
    pub separation: f32,
    /// Alignment strength.
    pub alignment: f32,
    /// Cohesion strength.
    pub cohesion: f32,
    /// Maximum population admitted by this operator.
    pub max_particles: usize,
}

impl Default for Boids {
    fn default() -> Self {
        Self {
            radius: 32.0,
            separation: 1.0,
            alignment: 0.5,
            cohesion: 0.1,
            max_particles: 4096,
        }
    }
}

impl ParticleGroupModifier for Boids {
    fn apply(
        &self,
        particles: &mut [Particle],
        step: f32,
        _: Time,
        max_work: u64,
    ) -> Result<u64, ValidationError> {
        if self.max_particles == 0
            || particles.len() > self.max_particles
            || [self.separation, self.alignment, self.cohesion]
                .iter()
                .any(|value| !value.is_finite() || *value < 0.0)
        {
            return Err(limit("Invalid flock parameters or population allowance"));
        }
        let pairs = super::neighbors::pairs(particles, self.radius, max_work / 16)?;
        let work = particles.len() as u64 * 4 + pairs.len() as u64 * 32;
        if work > max_work {
            return Err(limit("Flock work exhausted"));
        }
        let mut forces = vec![[0.0_f32; 6]; particles.len()];
        let mut counts = vec![0u32; particles.len()];
        for (first, second) in pairs {
            for (owner, other) in [(first, second), (second, first)] {
                let difference = [
                    particles[owner].position[0] - particles[other].position[0],
                    particles[owner].position[1] - particles[other].position[1],
                ];
                let square =
                    (difference[0] * difference[0] + difference[1] * difference[1]).max(1.0);
                for axis in 0..2 {
                    forces[owner][axis] += difference[axis] / square;
                    forces[owner][axis + 2] += particles[other].velocity[axis];
                    forces[owner][axis + 4] += particles[other].position[axis];
                }
                counts[owner] += 1;
            }
        }
        for (index, particle) in particles.iter_mut().enumerate() {
            if counts[index] == 0 {
                continue;
            }
            let count = counts[index] as f32;
            for axis in 0..2 {
                particle.velocity[axis] += step
                    * (forces[index][axis] * self.separation
                        + (forces[index][axis + 2] / count - particle.velocity[axis])
                            * self.alignment
                        + (forces[index][axis + 4] / count - particle.position[axis])
                            * self.cohesion);
            }
            particle.validate()?;
        }
        Ok(work)
    }
}

/// Consecutive stable IDs coupled by Hooke springs, optionally forming a closed chain.
#[derive(Clone, Copy, Debug)]
pub struct Spring {
    /// Rest distance between consecutive records.
    pub rest_length: f32,
    /// Nonnegative spring acceleration coefficient.
    pub stiffness: f32,
    /// Nonnegative damping coefficient along the connection.
    pub damping: f32,
    /// Connect the last record back to the first.
    pub closed: bool,
}

impl ParticleGroupModifier for Spring {
    fn apply(
        &self,
        particles: &mut [Particle],
        step: f32,
        _: Time,
        max_work: u64,
    ) -> Result<u64, ValidationError> {
        if [self.rest_length, self.stiffness, self.damping]
            .iter()
            .any(|value| !value.is_finite() || *value < 0.0)
        {
            return Err(invalid(
                "spring",
                "Require finite nonnegative spring parameters",
            ));
        }
        let work = particles.len() as u64 * 32;
        if work > max_work {
            return Err(limit("Spring work exhausted"));
        }
        let mut order = (0..particles.len()).collect::<Vec<_>>();
        order.sort_by_key(|index| particles[*index].index);
        let mut forces = vec![[0.0_f32; 2]; particles.len()];
        let count = order.len().saturating_sub(1) + usize::from(self.closed && order.len() > 2);
        for index in 0..count {
            let first = order[index];
            let second = order[(index + 1) % order.len()];
            let delta = [
                particles[second].position[0] - particles[first].position[0],
                particles[second].position[1] - particles[first].position[1],
            ];
            let distance = libm::hypotf(delta[0], delta[1]);
            if distance == 0.0 {
                continue;
            }
            let normal = delta.map(|value| value / distance);
            let speed = (particles[second].velocity[0] - particles[first].velocity[0]) * normal[0]
                + (particles[second].velocity[1] - particles[first].velocity[1]) * normal[1];
            let amount = (distance - self.rest_length) * self.stiffness + speed * self.damping;
            for axis in 0..2 {
                forces[first][axis] += normal[axis] * amount;
                forces[second][axis] -= normal[axis] * amount;
            }
        }
        for (particle, force) in particles.iter_mut().zip(forces) {
            for (velocity, force) in particle.velocity.iter_mut().zip(force) {
                *velocity += force * step;
            }
            particle.validate()?;
        }
        Ok(work)
    }
}

/// Optional equal-mass sphere contacts using stable spatial-hash pair ordering.
#[derive(Clone, Copy, Debug)]
pub struct ParticleCollisions {
    /// Contact restitution/friction; kill_on_hit is supported.
    pub response: CollisionResponse,
}

impl ParticleGroupModifier for ParticleCollisions {
    fn apply(
        &self,
        particles: &mut [Particle],
        _: f32,
        _: Time,
        max_work: u64,
    ) -> Result<u64, ValidationError> {
        self.response.validate()?;
        let radius = particles
            .iter()
            .fold(0.0_f32, |value, particle| value.max(particle.size))
            * 2.0;
        if radius == 0.0 {
            return Ok(particles.len() as u64);
        }
        let pairs = super::neighbors::pairs(particles, f64::from(radius), max_work / 16)?;
        let work = particles.len() as u64 + pairs.len() as u64 * 32;
        if work > max_work {
            return Err(limit("Particle contact work exhausted"));
        }
        for (first, second) in pairs {
            let delta = [
                particles[second].position[0] - particles[first].position[0],
                particles[second].position[1] - particles[first].position[1],
            ];
            let distance = libm::hypotf(delta[0], delta[1]);
            let minimum = particles[first].size + particles[second].size;
            if distance >= minimum {
                continue;
            }
            let normal = if distance == 0.0 {
                [1.0, 0.0]
            } else {
                delta.map(|value| value / distance)
            };
            let relative = [
                particles[second].velocity[0] - particles[first].velocity[0],
                particles[second].velocity[1] - particles[first].velocity[1],
            ];
            let speed = relative[0] * normal[0] + relative[1] * normal[1];
            for axis in 0..2 {
                let correction = normal[axis] * (minimum - distance + 1e-5) * 0.5;
                particles[first].position[axis] -= correction;
                particles[second].position[axis] += correction;
                if speed < 0.0 {
                    let impulse = normal[axis] * speed * (1.0 + self.response.restitution) * 0.5;
                    let friction =
                        (relative[axis] - speed * normal[axis]) * self.response.friction * 0.5;
                    particles[first].velocity[axis] += impulse + friction;
                    particles[second].velocity[axis] -= impulse + friction;
                }
            }
            if self.response.kill_on_hit {
                for index in [first, second] {
                    particles[index].lifetime = particles[index].age.max(f32::MIN_POSITIVE);
                }
            }
        }
        for particle in particles {
            particle.validate()?;
        }
        Ok(work)
    }

    fn collisions(&self) -> bool {
        true
    }
}