soorat 1.0.0

Soorat — GPU rendering engine for AGNOS
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
//! Fluid rendering — SPH particle visualization and shallow water surface meshes.
//!
//! Requires feature: `fluids` (dep: pravash).

use crate::color::Color;
#[cfg(feature = "fluids")]
use crate::vertex::Vertex3D;

/// Color mapping mode for fluid particles.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FluidColorMode {
    /// Uniform color for all particles.
    Solid,
    /// Color based on velocity magnitude (blue=slow → red=fast).
    Velocity,
    /// Color based on density (dark=low → bright=high).
    Density,
    /// Color based on pressure (cool=low → hot=high).
    Pressure,
}

use crate::color::visualization_heat_map;

/// Generate XZ-plane quads for SPH particles.
/// Each particle becomes a small axis-aligned quad positioned at its world coordinates.
/// `particle_size`: world-space size of each particle quad.
#[must_use]
#[cfg(feature = "fluids")]
pub fn particles_to_quads(
    particles: &[pravash::common::FluidParticle],
    particle_size: f32,
    color_mode: FluidColorMode,
    base_color: Color,
    max_velocity: f32,
    max_density: f64,
    max_pressure: f64,
) -> (Vec<Vertex3D>, Vec<u32>) {
    let count = particles.len();
    let mut vertices = Vec::with_capacity(count * 4);
    let mut indices = Vec::with_capacity(count * 6);
    let hs = particle_size * 0.5;

    for (i, p) in particles.iter().enumerate() {
        let pos = [
            p.position[0] as f32,
            p.position[1] as f32,
            p.position[2] as f32,
        ];

        let color = match color_mode {
            FluidColorMode::Solid => base_color,
            FluidColorMode::Velocity => {
                let speed = p.speed() as f32;
                let t = (speed / max_velocity.max(0.001)).clamp(0.0, 1.0);
                visualization_heat_map(t)
            }
            FluidColorMode::Density => {
                let t = (p.density / max_density.max(0.001)) as f32;
                visualization_heat_map(t.clamp(0.0, 1.0))
            }
            FluidColorMode::Pressure => {
                let t = (p.pressure / max_pressure.max(0.001)) as f32;
                visualization_heat_map(t.clamp(0.0, 1.0))
            }
        };
        let c = color.to_array();

        // Simple XZ-plane quad (for top-down 2D fluid view)
        let base = match (i as u64).checked_mul(4) {
            Some(v) if v <= u32::MAX as u64 => v as u32,
            _ => continue, // skip particle on overflow
        };
        vertices.push(Vertex3D {
            position: [pos[0] - hs, pos[1], pos[2] - hs],
            normal: [0.0, 1.0, 0.0],
            tex_coords: [0.0, 0.0],
            color: c,
        });
        vertices.push(Vertex3D {
            position: [pos[0] + hs, pos[1], pos[2] - hs],
            normal: [0.0, 1.0, 0.0],
            tex_coords: [1.0, 0.0],
            color: c,
        });
        vertices.push(Vertex3D {
            position: [pos[0] + hs, pos[1], pos[2] + hs],
            normal: [0.0, 1.0, 0.0],
            tex_coords: [1.0, 1.0],
            color: c,
        });
        vertices.push(Vertex3D {
            position: [pos[0] - hs, pos[1], pos[2] + hs],
            normal: [0.0, 1.0, 0.0],
            tex_coords: [0.0, 1.0],
            color: c,
        });

        indices.push(base);
        indices.push(base + 1);
        indices.push(base + 2);
        indices.push(base + 2);
        indices.push(base + 3);
        indices.push(base);
    }

    (vertices, indices)
}

/// Generate a mesh from a pravash ShallowWater height field.
/// The mesh is an XZ-plane grid with Y = water height.
/// Normals are computed from neighboring heights.
#[must_use]
#[cfg(feature = "fluids")]
pub fn shallow_water_to_mesh(
    water: &pravash::shallow::ShallowWater,
    y_scale: f32,
    color: Color,
) -> (Vec<Vertex3D>, Vec<u32>) {
    let nx = water.nx;
    let ny = water.ny;
    let dx = water.dx as f32;
    let c = color.to_array();

    let cols = nx;
    let rows = ny;
    let mut vertices = Vec::with_capacity(cols * rows);
    let mut indices = Vec::with_capacity((cols - 1) * (rows - 1) * 6);

    // Generate vertices
    for z in 0..rows {
        for x in 0..cols {
            let idx = z * cols + x;
            let h = water.height[idx] as f32 * y_scale;
            let px = x as f32 * dx - (cols as f32 * dx * 0.5);
            let pz = z as f32 * dx - (rows as f32 * dx * 0.5);

            // Compute normal from neighbors
            let hl = if x > 0 {
                water.height[z * cols + x - 1] as f32 * y_scale
            } else {
                h
            };
            let hr = if x < cols - 1 {
                water.height[z * cols + x + 1] as f32 * y_scale
            } else {
                h
            };
            let hd = if z > 0 {
                water.height[(z - 1) * cols + x] as f32 * y_scale
            } else {
                h
            };
            let hu = if z < rows - 1 {
                water.height[(z + 1) * cols + x] as f32 * y_scale
            } else {
                h
            };

            let ndx = (hl - hr) / (2.0 * dx);
            let ndz = (hd - hu) / (2.0 * dx);
            let len = (ndx * ndx + 1.0 + ndz * ndz).sqrt();

            vertices.push(Vertex3D {
                position: [px, h, pz],
                normal: [ndx / len, 1.0 / len, ndz / len],
                tex_coords: [
                    x as f32 / (cols - 1).max(1) as f32,
                    z as f32 / (rows - 1).max(1) as f32,
                ],
                color: c,
            });
        }
    }

    // Generate indices
    for z in 0..(rows - 1) {
        for x in 0..(cols - 1) {
            let tl = (z * cols + x) as u32;
            let tr = tl + 1;
            let bl = tl + cols as u32;
            let br = bl + 1;
            indices.push(tl);
            indices.push(bl);
            indices.push(tr);
            indices.push(tr);
            indices.push(bl);
            indices.push(br);
        }
    }

    (vertices, indices)
}

/// Parameters for particle rendering (color mapping + normalization ranges).
pub struct ParticleColorParams {
    pub color_mode: FluidColorMode,
    pub base_color: Color,
    pub max_velocity: f32,
    pub max_density: f64,
    pub max_pressure: f64,
}

impl Default for ParticleColorParams {
    fn default() -> Self {
        Self {
            color_mode: FluidColorMode::Solid,
            base_color: Color::WHITE,
            max_velocity: 10.0,
            max_density: 1000.0,
            max_pressure: 1000.0,
        }
    }
}

/// Generate camera-facing billboard quads for particles.
/// Unlike `particles_to_quads` (XZ-plane), these orient toward the camera.
/// `camera_right`/`camera_up`: camera basis vectors in world space.
#[must_use]
#[cfg(feature = "fluids")]
pub fn particles_to_billboards(
    particles: &[pravash::common::FluidParticle],
    particle_size: f32,
    camera_right: [f32; 3],
    camera_up: [f32; 3],
    params: &ParticleColorParams,
) -> (Vec<Vertex3D>, Vec<u32>) {
    let count = particles.len();
    let mut vertices = Vec::with_capacity(count * 4);
    let mut indices = Vec::with_capacity(count * 6);
    let hs = particle_size * 0.5;

    let r = [
        camera_right[0] * hs,
        camera_right[1] * hs,
        camera_right[2] * hs,
    ];
    let u = [camera_up[0] * hs, camera_up[1] * hs, camera_up[2] * hs];

    // Billboard normal = cross(right, up)
    let n = [
        camera_right[1] * camera_up[2] - camera_right[2] * camera_up[1],
        camera_right[2] * camera_up[0] - camera_right[0] * camera_up[2],
        camera_right[0] * camera_up[1] - camera_right[1] * camera_up[0],
    ];

    for (i, p) in particles.iter().enumerate() {
        let pos = [
            p.position[0] as f32,
            p.position[1] as f32,
            p.position[2] as f32,
        ];

        let color = match params.color_mode {
            FluidColorMode::Solid => params.base_color,
            FluidColorMode::Velocity => {
                let speed = p.speed() as f32;
                visualization_heat_map((speed / params.max_velocity.max(0.001)).clamp(0.0, 1.0))
            }
            FluidColorMode::Density => visualization_heat_map(
                ((p.density / params.max_density.max(0.001)) as f32).clamp(0.0, 1.0),
            ),
            FluidColorMode::Pressure => visualization_heat_map(
                ((p.pressure / params.max_pressure.max(0.001)) as f32).clamp(0.0, 1.0),
            ),
        };
        let c = color.to_array();
        let base = match (i as u64).checked_mul(4) {
            Some(v) if v <= u32::MAX as u64 => v as u32,
            _ => continue, // skip particle on overflow
        };

        // Billboard corners: center ± right ± up
        vertices.push(Vertex3D {
            position: [
                pos[0] - r[0] - u[0],
                pos[1] - r[1] - u[1],
                pos[2] - r[2] - u[2],
            ],
            normal: n,
            tex_coords: [0.0, 0.0],
            color: c,
        });
        vertices.push(Vertex3D {
            position: [
                pos[0] + r[0] - u[0],
                pos[1] + r[1] - u[1],
                pos[2] + r[2] - u[2],
            ],
            normal: n,
            tex_coords: [1.0, 0.0],
            color: c,
        });
        vertices.push(Vertex3D {
            position: [
                pos[0] + r[0] + u[0],
                pos[1] + r[1] + u[1],
                pos[2] + r[2] + u[2],
            ],
            normal: n,
            tex_coords: [1.0, 1.0],
            color: c,
        });
        vertices.push(Vertex3D {
            position: [
                pos[0] - r[0] + u[0],
                pos[1] - r[1] + u[1],
                pos[2] - r[2] + u[2],
            ],
            normal: n,
            tex_coords: [0.0, 1.0],
            color: c,
        });

        indices.push(base);
        indices.push(base + 1);
        indices.push(base + 2);
        indices.push(base + 2);
        indices.push(base + 3);
        indices.push(base);
    }

    (vertices, indices)
}

// ── CPU-only helpers (no feature gate) ──────────────────────────────────────

/// Fluid heat map exposed for general use.
#[must_use]
pub fn fluid_heat_map(t: f32) -> Color {
    visualization_heat_map(t)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn heat_map_endpoints() {
        let cold = visualization_heat_map(0.0);
        assert_eq!(cold.b, 1.0); // blue
        let hot = visualization_heat_map(1.0);
        assert_eq!(hot.r, 1.0); // red
        assert_eq!(hot.g, 0.0);
    }

    #[test]
    fn heat_map_midpoint() {
        let mid = visualization_heat_map(0.5);
        assert_eq!(mid.g, 1.0); // green
    }

    #[test]
    fn heat_map_clamps() {
        let under = visualization_heat_map(-1.0);
        assert_eq!(under.b, 1.0);
        let over = visualization_heat_map(2.0);
        assert_eq!(over.r, 1.0);
    }

    #[test]
    fn heat_map_gradient_smooth() {
        // Should produce valid colors at all points
        for i in 0..=100 {
            let t = i as f32 / 100.0;
            let c = visualization_heat_map(t);
            assert!(c.r >= 0.0 && c.r <= 1.0);
            assert!(c.g >= 0.0 && c.g <= 1.0);
            assert!(c.b >= 0.0 && c.b <= 1.0);
            assert_eq!(c.a, 1.0);
        }
    }

    #[cfg(feature = "fluids")]
    #[test]
    fn particles_to_quads_empty() {
        let (v, i) =
            particles_to_quads(&[], 0.1, FluidColorMode::Solid, Color::BLUE, 1.0, 1.0, 1.0);
        assert!(v.is_empty());
        assert!(i.is_empty());
    }

    #[cfg(feature = "fluids")]
    #[test]
    fn particles_to_quads_single() {
        let p = pravash::common::FluidParticle::new([1.0, 2.0, 3.0].into(), 1.0);
        let (v, i) = particles_to_quads(
            &[p],
            0.5,
            FluidColorMode::Solid,
            Color::BLUE,
            1.0,
            1000.0,
            1.0,
        );
        assert_eq!(v.len(), 4);
        assert_eq!(i.len(), 6);
    }

    #[cfg(feature = "fluids")]
    #[test]
    fn particles_velocity_color() {
        let mut p = pravash::common::FluidParticle::new([0.0; 3].into(), 1.0);
        p.velocity = [10.0, 0.0, 0.0].into();
        let (v, _) = particles_to_quads(
            &[p],
            0.1,
            FluidColorMode::Velocity,
            Color::WHITE,
            10.0,
            1.0,
            1.0,
        );
        // Fast particle should be red (heat map at 1.0)
        assert_eq!(v[0].color[0], 1.0); // red
    }

    #[cfg(feature = "fluids")]
    #[test]
    fn shallow_water_mesh() {
        let water = pravash::shallow::ShallowWater::new(4, 4, 1.0, 1.0).unwrap();
        let (v, i) = shallow_water_to_mesh(&water, 1.0, Color::BLUE);
        assert_eq!(v.len(), 16); // 4*4
        assert_eq!(i.len(), 9 * 6); // 3*3 quads * 6 indices
    }

    #[cfg(feature = "fluids")]
    #[test]
    fn billboard_single_particle() {
        let p = pravash::common::FluidParticle::new([0.0, 1.0, 0.0].into(), 1.0);
        let (v, i) = particles_to_billboards(
            &[p],
            1.0,
            [1.0, 0.0, 0.0],
            [0.0, 1.0, 0.0],
            &ParticleColorParams::default(),
        );
        assert_eq!(v.len(), 4);
        assert_eq!(i.len(), 6);
        // Normal should face camera (cross of right × up = forward = [0,0,1])
        assert!((v[0].normal[2] - 1.0).abs() < 0.01);
    }

    #[cfg(feature = "fluids")]
    #[test]
    fn billboard_empty() {
        let (v, i) = particles_to_billboards(
            &[],
            1.0,
            [1.0, 0.0, 0.0],
            [0.0, 1.0, 0.0],
            &ParticleColorParams::default(),
        );
        assert!(v.is_empty());
        assert!(i.is_empty());
    }

    #[cfg(feature = "fluids")]
    #[test]
    fn shallow_water_normals_flat() {
        let water = pravash::shallow::ShallowWater::new(3, 3, 1.0, 5.0).unwrap();
        let (v, _) = shallow_water_to_mesh(&water, 1.0, Color::BLUE);
        // Flat water → normals point up
        for vert in &v {
            assert!((vert.normal[1] - 1.0).abs() < 0.01);
        }
    }
}