1//! Evaluator & per-particle executors for `pCustom` and `pCustomForce`.
2
3use std::collections::HashSet;
4
5use model::{Comp, ExprEngine, Node};
6
7use super::custom::{Ast, BinOp, Func, Var, compile_expr};
8use super::forces::ModifierFilter;
9use super::sim::value_noise_3d;
10use super::{Particle, SplitMix64, SpriteImage, VELOCITY_SCALE};
11
12struct EvalCtx<'a> {
13 p: &'a Particle,
14 time: f64,
15 seed: i64,
16 n: [f64; 8],
17 pt: [[f64; 3]; 4],
18 s: [f64; 4],
19 i: [f64; 8],
20 img: Option<&'a SpriteImage>,
21}
22
23fn sample_sprite(img: Option<&SpriteImage>, x: f64, y: f64, ch: u8, edge: u8) -> f64 {
24 let Some(sp) = img else {
25 return 0.0;
26 };
27 let w = sp.size[0] as isize;
28 let h = sp.size[1] as isize;
29 if w <= 0 || h <= 0 || sp.rgba.len() < (w as usize) * (h as usize) * 4 {
30 return 0.0;
31 }
32 // Fusion normalized image coords: (0,0) bottom-left, (1,1) top-right.
33 let mut u = x;
34 let mut v = 1.0 - y;
35 match edge {
36 1 => {
37 u = u.rem_euclid(1.0);
38 v = v.rem_euclid(1.0);
39 }
40 2 => {
41 u = u.clamp(0.0, 1.0);
42 v = v.clamp(0.0, 1.0);
43 }
44 _ => {
45 if !(0.0..=1.0).contains(&u) || !(0.0..=1.0).contains(&v) {
46 return 0.0;
47 }
48 }
49 }
50 let ix = ((u * w as f64).floor() as isize).clamp(0, w - 1) as usize;
51 let iy = ((v * h as f64).floor() as isize).clamp(0, h - 1) as usize;
52 let off = (iy * w as usize + ix) * 4;
53 let px = &sp.rgba[off..off + 4];
54 match ch {
55 0 => px[0] as f64 / 255.0,
56 1 => px[1] as f64 / 255.0,
57 2 => px[2] as f64 / 255.0,
58 3 => px[3] as f64 / 255.0,
59 _ => (0.2126 * px[0] as f64 + 0.7152 * px[1] as f64 + 0.0722 * px[2] as f64) / 255.0,
60 }
61}
62
63fn eval_ast(ast: &Ast, ctx: &EvalCtx<'_>) -> f64 {
64 let v = match ast {
65 Ast::Const(c) => *c,
66 Ast::Var(var) => match *var {
67 Var::Px => ctx.p.pos[0],
68 Var::Py => ctx.p.pos[1],
69 Var::Pz => ctx.p.pos[2],
70 Var::Vx => ctx.p.vel[0],
71 Var::Vy => ctx.p.vel[1],
72 Var::Vz => ctx.p.vel[2],
73 Var::Rx => ctx.p.rot_xy[0],
74 Var::Ry => ctx.p.rot_xy[1],
75 Var::Rz => ctx.p.rot_z,
76 Var::Sx => ctx.p.spin_xy[0],
77 Var::Sy => ctx.p.spin_xy[1],
78 Var::Sz => ctx.p.spin_z,
79 Var::R => ctx.p.color[0] as f64,
80 Var::G => ctx.p.color[1] as f64,
81 Var::B => ctx.p.color[2] as f64,
82 Var::A => ctx.p.color[3] as f64,
83 Var::Size => ctx.p.size,
84 Var::Age => ctx.p.age / ctx.p.lifespan.max(1.0),
85 Var::Lifespan => ctx.p.lifespan,
86 Var::Id => ctx.p.id as f64,
87 Var::Mass => ctx.p.mass,
88 Var::User => ctx.p.user,
89 Var::Time => ctx.time,
90 Var::Pi => std::f64::consts::PI,
91 Var::E => std::f64::consts::E,
92 Var::Num(idx) => ctx.n[idx as usize],
93 Var::Pt(idx, axis) => ctx.pt[idx as usize][axis as usize],
94 Var::Setup(idx) => ctx.s[idx as usize],
95 Var::Inter(idx) => ctx.i[idx as usize],
96 },
97 Ast::Neg(x) => -eval_ast(x, ctx),
98 Ast::Not(x) => if eval_ast(x, ctx) == 0.0 { 1.0 } else { 0.0 },
99 Ast::Bin(op, a, b) => {
100 let x = eval_ast(a, ctx);
101 let y = eval_ast(b, ctx);
102 match op {
103 BinOp::Add => x + y,
104 BinOp::Sub => x - y,
105 BinOp::Mul => x * y,
106 BinOp::Div => if y.abs() < 1e-12 { 0.0 } else { x / y },
107 BinOp::Mod => if y.abs() < 1e-12 { 0.0 } else { x % y },
108 BinOp::Pow => x.powf(y),
109 BinOp::Lt => if x < y { 1.0 } else { 0.0 },
110 BinOp::Le => if x <= y { 1.0 } else { 0.0 },
111 BinOp::Gt => if x > y { 1.0 } else { 0.0 },
112 BinOp::Ge => if x >= y { 1.0 } else { 0.0 },
113 BinOp::Eq => if (x - y).abs() < 1e-12 { 1.0 } else { 0.0 },
114 BinOp::Ne => if (x - y).abs() >= 1e-12 { 1.0 } else { 0.0 },
115 BinOp::And => if x != 0.0 && y != 0.0 { 1.0 } else { 0.0 },
116 BinOp::Or => if x != 0.0 || y != 0.0 { 1.0 } else { 0.0 },
117 }
118 }
119 Ast::Call(func, args) => match *func {
120 Func::Sin => eval_ast(&args[0], ctx).to_radians().sin(),
121 Func::Cos => eval_ast(&args[0], ctx).to_radians().cos(),
122 Func::Tan => eval_ast(&args[0], ctx).to_radians().tan(),
123 Func::Asin => eval_ast(&args[0], ctx).clamp(-1.0, 1.0).asin().to_degrees(),
124 Func::Acos => eval_ast(&args[0], ctx).clamp(-1.0, 1.0).acos().to_degrees(),
125 Func::Atan => eval_ast(&args[0], ctx).atan().to_degrees(),
126 Func::Atan2 => eval_ast(&args[0], ctx).atan2(eval_ast(&args[1], ctx)).to_degrees(),
127 Func::Sinr => eval_ast(&args[0], ctx).sin(),
128 Func::Cosr => eval_ast(&args[0], ctx).cos(),
129 Func::Tanr => eval_ast(&args[0], ctx).tan(),
130 Func::Asinr => eval_ast(&args[0], ctx).clamp(-1.0, 1.0).asin(),
131 Func::Acosr => eval_ast(&args[0], ctx).clamp(-1.0, 1.0).acos(),
132 Func::Atanr => eval_ast(&args[0], ctx).atan(),
133 Func::Atan2r => eval_ast(&args[0], ctx).atan2(eval_ast(&args[1], ctx)),
134 Func::Sinh => eval_ast(&args[0], ctx).sinh(),
135 Func::Cosh => eval_ast(&args[0], ctx).cosh(),
136 Func::Tanh => eval_ast(&args[0], ctx).tanh(),
137 Func::Deg => eval_ast(&args[0], ctx).to_degrees(),
138 Func::Rad => eval_ast(&args[0], ctx).to_radians(),
139 Func::Abs => eval_ast(&args[0], ctx).abs(),
140 Func::Sqrt => eval_ast(&args[0], ctx).max(0.0).sqrt(),
141 Func::Pow => eval_ast(&args[0], ctx).powf(eval_ast(&args[1], ctx)),
142 Func::Exp => eval_ast(&args[0], ctx).exp(),
143 Func::Log => eval_ast(&args[0], ctx).max(1e-12).ln(),
144 Func::Log10 => eval_ast(&args[0], ctx).max(1e-12).log10(),
145 Func::Log2 => eval_ast(&args[0], ctx).max(1e-12).log2(),
146 Func::Floor => eval_ast(&args[0], ctx).floor(),
147 Func::Ceil => eval_ast(&args[0], ctx).ceil(),
148 Func::Round => eval_ast(&args[0], ctx).round(),
149 Func::Trunc => eval_ast(&args[0], ctx).trunc(),
150 Func::Fract => eval_ast(&args[0], ctx).fract(),
151 Func::Sign => {
152 let x = eval_ast(&args[0], ctx);
153 if x > 0.0 { 1.0 } else if x < 0.0 { -1.0 } else { 0.0 }
154 }
155 Func::Min => args.iter().map(|a| eval_ast(a, ctx)).fold(f64::INFINITY, f64::min),
156 Func::Max => args.iter().map(|a| eval_ast(a, ctx)).fold(f64::NEG_INFINITY, f64::max),
157 Func::Clamp => {
158 let x = eval_ast(&args[0], ctx);
159 let lo = eval_ast(&args[1], ctx);
160 let hi = eval_ast(&args[2], ctx);
161 if lo <= hi { x.clamp(lo, hi) } else { x.clamp(hi, lo) }
162 }
163 Func::Mod => {
164 let x = eval_ast(&args[0], ctx);
165 let y = eval_ast(&args[1], ctx);
166 if y.abs() < 1e-12 { 0.0 } else { x % y }
167 }
168 Func::If => {
169 if eval_ast(&args[0], ctx) != 0.0 {
170 eval_ast(&args[1], ctx)
171 } else {
172 eval_ast(&args[2], ctx)
173 }
174 }
175 Func::Dist => {
176 let x1 = eval_ast(&args[0], ctx);
177 let y1 = eval_ast(&args[1], ctx);
178 let x2 = eval_ast(&args[2], ctx);
179 let y2 = eval_ast(&args[3], ctx);
180 (x2 - x1).hypot(y2 - y1)
181 }
182 Func::Hypot => eval_ast(&args[0], ctx).hypot(eval_ast(&args[1], ctx)),
183 Func::Mix => {
184 let a = eval_ast(&args[0], ctx);
185 let b = eval_ast(&args[1], ctx);
186 let u = eval_ast(&args[2], ctx);
187 a + (b - a) * u
188 }
189 Func::Step => {
190 if eval_ast(&args[1], ctx) < eval_ast(&args[0], ctx) { 0.0 } else { 1.0 }
191 }
192 Func::Smoothstep => {
193 let e0 = eval_ast(&args[0], ctx);
194 let e1 = eval_ast(&args[1], ctx);
195 let x = eval_ast(&args[2], ctx);
196 let u = if (e1 - e0).abs() < 1e-12 { 0.0 } else { ((x - e0) / (e1 - e0)).clamp(0.0, 1.0) };
197 u * u * (3.0 - 2.0 * u)
198 }
199 Func::Rand => {
200 let lo = eval_ast(&args[0], ctx);
201 let hi = eval_ast(&args[1], ctx);
202 let mut rng = SplitMix64::from_parts(ctx.seed ^ i64::from(ctx.p.id), ctx.time.round() as i64, "pCustomRand");
203 rng.range(lo, hi)
204 }
205 Func::Rands => {
206 let lo = eval_ast(&args[0], ctx);
207 let hi = eval_ast(&args[1], ctx);
208 let s = eval_ast(&args[2], ctx).to_bits() as i64;
209 let mut rng = SplitMix64::from_parts(ctx.seed ^ s ^ i64::from(ctx.p.id), 0, "pCustomRands");
210 rng.range(lo, hi)
211 }
212 Func::Noise1 => value_noise_3d(eval_ast(&args[0], ctx), 0.0, 0.0, ctx.seed as u64),
213 Func::Noise2 => value_noise_3d(eval_ast(&args[0], ctx), eval_ast(&args[1], ctx), 0.0, ctx.seed as u64),
214 Func::Noise3 => value_noise_3d(
215 eval_ast(&args[0], ctx),
216 eval_ast(&args[1], ctx),
217 eval_ast(&args[2], ctx),
218 ctx.seed as u64,
219 ),
220 Func::SampleImg(ch, edge) => {
221 let x = eval_ast(&args[0], ctx);
222 let y = eval_ast(&args[1], ctx);
223 sample_sprite(ctx.img, x, y, ch, edge)
224 }
225 },
226 };
227 if v.is_finite() { v } else { 0.0 }
228}
229
230fn read_common_inputs(comp: &Comp, node: &Node, t: f64, expr: &ExprEngine) -> ([f64; 8], [[f64; 3]; 4], [Option<Ast>; 4], [Option<Ast>; 8]) {
231 let n = std::array::from_fn(|idx| {
232 let key = format!("NumberIn{}", idx + 1);
233 comp.eval_number(node, &key, t, 0.0, expr)
234 });
235 let pt = std::array::from_fn(|idx| {
236 let i = idx + 1;
237 let pin_key = format!("PointIn{i}");
238 let def_pt = if node.inputs.contains_key(&pin_key) {
239 comp.eval_point(node, &pin_key, t, [0.0, 0.0], expr)
240 } else {
241 [0.0, 0.0]
242 };
243 [
244 comp.eval_number(node, &format!("Position{i}.X"), t, def_pt[0], expr),
245 comp.eval_number(node, &format!("Position{i}.Y"), t, def_pt[1], expr),
246 comp.eval_number(node, &format!("Position{i}.Z"), t, 0.0, expr),
247 ]
248 });
249 let setups = std::array::from_fn(|idx| {
250 let key = format!("Setup{}", idx + 1);
251 compile_expr(&comp.eval_text(node, &key, t, "", expr))
252 });
253 let inters = std::array::from_fn(|idx| {
254 let key = format!("Intermediate{}", idx + 1);
255 let alias = format!("Inter{}", idx + 1);
256 let txt = if node.inputs.contains_key(&key) {
257 comp.eval_text(node, &key, t, "", expr)
258 } else if node.inputs.contains_key(&alias) {
259 comp.eval_text(node, &alias, t, "", expr)
260 } else {
261 String::new()
262 };
263 compile_expr(&txt)
264 });
265 (n, pt, setups, inters)
266}
267
268fn compile_channel_expr(
269 comp: &Comp,
270 node: &Node,
271 keys: &[&str],
272 identity_var: &str,
273 t: f64,
274 expr: &ExprEngine,
275) -> Option<Ast> {
276 for &k in keys {
277 if node.inputs.contains_key(k) {
278 let txt = comp.eval_text(node, k, t, identity_var, expr);
279 let trimmed = txt.trim();
280 if trimmed.is_empty() || trimmed == identity_var {
281 return None;
282 }
283 return compile_expr(trimmed);
284 }
285 }
286 None
287}
288
289/// Applies `pCustom` per-particle expression overrides to matching live particles.
290pub(super) fn apply_custom(
291 particles: &mut [Particle],
292 comp: &Comp,
293 node: &Node,
294 scope: &HashSet<u64>,
295 img: Option<&SpriteImage>,
296 t: f64,
297 expr: &ExprEngine,
298) {
299 let px_ast = compile_channel_expr(comp, node, &["PositionXExpression"], "px", t, expr);
300 let py_ast = compile_channel_expr(comp, node, &["PositionYExpression"], "py", t, expr);
301 let pz_ast = compile_channel_expr(comp, node, &["PositionZExpression"], "pz", t, expr);
302 let vx_ast = compile_channel_expr(comp, node, &["VelocityXExpression"], "vx", t, expr);
303 let vy_ast = compile_channel_expr(comp, node, &["VelocityYExpression"], "vy", t, expr);
304 let vz_ast = compile_channel_expr(comp, node, &["VelocityZExpression"], "vz", t, expr);
305 let rx_ast = compile_channel_expr(comp, node, &["RotationXExpression"], "rx", t, expr);
306 let ry_ast = compile_channel_expr(comp, node, &["RotationYExpression"], "ry", t, expr);
307 let rz_ast = compile_channel_expr(comp, node, &["RotationZExpression"], "rz", t, expr);
308 let sx_ast = compile_channel_expr(comp, node, &["SpinXExpression"], "sx", t, expr);
309 let sy_ast = compile_channel_expr(comp, node, &["SpinYExpression"], "sy", t, expr);
310 let sz_ast = compile_channel_expr(comp, node, &["SpinZExpression"], "sz", t, expr);
311 let r_ast = compile_channel_expr(comp, node, &["RedExpression"], "r", t, expr);
312 let g_ast = compile_channel_expr(comp, node, &["GreenExpression"], "g", t, expr);
313 let b_ast = compile_channel_expr(comp, node, &["BlueExpression"], "b", t, expr);
314 let a_ast = compile_channel_expr(comp, node, &["AlphaExpression"], "a", t, expr);
315 let size_ast = compile_channel_expr(comp, node, &["SizeExpression"], "size", t, expr);
316 let life_ast = compile_channel_expr(comp, node, &["LifeSpanExpression", "LifespanExpression"], "lifespan", t, expr);
317 let mass_ast = compile_channel_expr(comp, node, &["MassExpression"], "mass", t, expr);
318 let user_ast = compile_channel_expr(comp, node, &["UserExpression"], "user", t, expr);
319 let kill_ast = compile_channel_expr(comp, node, &["KillExpressionKillIf10", "KillExpression"], "0", t, expr);
320
321 if [
322 &px_ast, &py_ast, &pz_ast, &vx_ast, &vy_ast, &vz_ast, &rx_ast, &ry_ast, &rz_ast,
323 &sx_ast, &sy_ast, &sz_ast, &r_ast, &g_ast, &b_ast, &a_ast, &size_ast, &life_ast,
324 &mass_ast, &user_ast, &kill_ast,
325 ]
326 .iter()
327 .all(|o| o.is_none())
328 {
329 return;
330 }
331
332 let (n, pt, setups, inters) = read_common_inputs(comp, node, t, expr);
333 let filter = ModifierFilter::from_node(comp, node, t, expr);
334 let seed = comp.eval_number(node, "RandomSeed", t, 0.0, expr).round() as i64;
335 let mut rng = SplitMix64::from_parts(seed, t.round() as i64, &node.name);
336
337 for p in particles.iter_mut() {
338 if !filter.matches(p, scope, &mut rng) {
339 continue;
340 }
341 let mut ctx = EvalCtx {
342 p,
343 time: t,
344 seed,
345 n,
346 pt,
347 s: [0.0; 4],
348 i: [0.0; 8],
349 img,
350 };
351 for idx in 0..4 {
352 if let Some(ref ast) = setups[idx] {
353 ctx.s[idx] = eval_ast(ast, &ctx);
354 }
355 }
356 for idx in 0..8 {
357 if let Some(ref ast) = inters[idx] {
358 ctx.i[idx] = eval_ast(ast, &ctx);
359 }
360 }
361 let new_px = px_ast.as_ref().map_or(p.pos[0], |e| eval_ast(e, &ctx));
362 let new_py = py_ast.as_ref().map_or(p.pos[1], |e| eval_ast(e, &ctx));
363 let new_pz = pz_ast.as_ref().map_or(p.pos[2], |e| eval_ast(e, &ctx));
364 let new_vx = vx_ast.as_ref().map_or(p.vel[0], |e| eval_ast(e, &ctx));
365 let new_vy = vy_ast.as_ref().map_or(p.vel[1], |e| eval_ast(e, &ctx));
366 let new_vz = vz_ast.as_ref().map_or(p.vel[2], |e| eval_ast(e, &ctx));
367 let new_rx = rx_ast.as_ref().map_or(p.rot_xy[0], |e| eval_ast(e, &ctx));
368 let new_ry = ry_ast.as_ref().map_or(p.rot_xy[1], |e| eval_ast(e, &ctx));
369 let new_rz = rz_ast.as_ref().map_or(p.rot_z, |e| eval_ast(e, &ctx));
370 let new_sx = sx_ast.as_ref().map_or(p.spin_xy[0], |e| eval_ast(e, &ctx));
371 let new_sy = sy_ast.as_ref().map_or(p.spin_xy[1], |e| eval_ast(e, &ctx));
372 let new_sz = sz_ast.as_ref().map_or(p.spin_z, |e| eval_ast(e, &ctx));
373 let new_r = r_ast.as_ref().map_or(p.color[0], |e| eval_ast(e, &ctx).clamp(0.0, 1.0) as f32);
374 let new_g = g_ast.as_ref().map_or(p.color[1], |e| eval_ast(e, &ctx).clamp(0.0, 1.0) as f32);
375 let new_b = b_ast.as_ref().map_or(p.color[2], |e| eval_ast(e, &ctx).clamp(0.0, 1.0) as f32);
376 let new_a = a_ast.as_ref().map_or(p.color[3], |e| eval_ast(e, &ctx).clamp(0.0, 1.0) as f32);
377 let new_size = size_ast.as_ref().map_or(p.size, |e| eval_ast(e, &ctx).max(0.0));
378 let new_life = life_ast.as_ref().map_or(p.lifespan, |e| eval_ast(e, &ctx).max(1.0));
379 let new_mass = mass_ast.as_ref().map_or(p.mass, |e| eval_ast(e, &ctx));
380 let new_user = user_ast.as_ref().map_or(p.user, |e| eval_ast(e, &ctx));
381 let kill = kill_ast.as_ref().is_some_and(|e| eval_ast(e, &ctx) > 0.5);
382
383 p.pos = [new_px, new_py, new_pz];
384 p.vel = [new_vx, new_vy, new_vz];
385 p.rot_xy = [new_rx, new_ry];
386 p.rot_z = new_rz;
387 p.spin_xy = [new_sx, new_sy];
388 p.spin_z = new_sz;
389 p.color = [new_r, new_g, new_b, new_a];
390 p.size = new_size;
391 p.lifespan = new_life;
392 p.mass = new_mass;
393 p.user = new_user;
394 if kill {
395 p.alive = false;
396 }
397 }
398}
399
400/// Applies `pCustomForce` per-particle acceleration/torque expressions each substep.
401pub(super) fn apply_custom_force(
402 particles: &mut [Particle],
403 comp: &Comp,
404 node: &Node,
405 scope: &HashSet<u64>,
406 img: Option<&SpriteImage>,
407 t: f64,
408 expr: &ExprEngine,
409) {
410 let fx_ast = compile_channel_expr(comp, node, &["ForceXExpression"], "0", t, expr);
411 let fy_ast = compile_channel_expr(comp, node, &["ForceYExpression"], "0", t, expr);
412 let fz_ast = compile_channel_expr(comp, node, &["ForceZExpression"], "0", t, expr);
413 let tx_ast = compile_channel_expr(comp, node, &["TorqueXExpression"], "0", t, expr);
414 let ty_ast = compile_channel_expr(comp, node, &["TorqueYExpression"], "0", t, expr);
415 let tz_ast = compile_channel_expr(comp, node, &["TorqueZExpression"], "0", t, expr);
416 if [&fx_ast, &fy_ast, &fz_ast, &tx_ast, &ty_ast, &tz_ast]
417 .iter()
418 .all(|o| o.is_none())
419 {
420 return;
421 }
422
423 let (n, pt, setups, inters) = read_common_inputs(comp, node, t, expr);
424 let filter = ModifierFilter::from_node(comp, node, t, expr);
425 let seed = comp.eval_number(node, "RandomSeed", t, 0.0, expr).round() as i64;
426 let mut rng = SplitMix64::from_parts(seed, t.round() as i64, &node.name);
427 let force_scale = VELOCITY_SCALE * VELOCITY_SCALE;
428
429 for p in particles.iter_mut() {
430 if !filter.matches(p, scope, &mut rng) {
431 continue;
432 }
433 let mut ctx = EvalCtx {
434 p,
435 time: t,
436 seed,
437 n,
438 pt,
439 s: [0.0; 4],
440 i: [0.0; 8],
441 img,
442 };
443 for idx in 0..4 {
444 if let Some(ref ast) = setups[idx] {
445 ctx.s[idx] = eval_ast(ast, &ctx);
446 }
447 }
448 for idx in 0..8 {
449 if let Some(ref ast) = inters[idx] {
450 ctx.i[idx] = eval_ast(ast, &ctx);
451 }
452 }
453 let inv_m = if p.mass.abs() > 1e-6 { 1.0 / p.mass } else { 1.0 };
454 let dfx = fx_ast.as_ref().map_or(0.0, |ast| eval_ast(ast, &ctx) * force_scale * inv_m);
455 let dfy = fy_ast.as_ref().map_or(0.0, |ast| eval_ast(ast, &ctx) * force_scale * inv_m);
456 let dfz = fz_ast.as_ref().map_or(0.0, |ast| eval_ast(ast, &ctx) * force_scale * inv_m);
457 let dtx = tx_ast.as_ref().map_or(0.0, |ast| eval_ast(ast, &ctx) * inv_m);
458 let dty = ty_ast.as_ref().map_or(0.0, |ast| eval_ast(ast, &ctx) * inv_m);
459 let dtz = tz_ast.as_ref().map_or(0.0, |ast| eval_ast(ast, &ctx) * inv_m);
460 p.vel[0] += dfx;
461 p.vel[1] += dfy;
462 p.vel[2] += dfz;
463 p.spin_xy[0] += dtx;
464 p.spin_xy[1] += dty;
465 p.spin_z += dtz;
466 }
467}