oxihuman-viewer 0.2.1

wgpu/WebGPU rendering adapter for OxiHuman
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! 2D grid overlay for UI panels.

#[allow(dead_code)]
pub struct GridOverlayConfig {
    pub cell_width: f32,
    pub cell_height: f32,
    pub origin: [f32; 2],
    pub color: [f32; 4],
    pub major_every: u32,
    pub major_color: [f32; 4],
    pub visible: bool,
}

#[allow(dead_code)]
pub struct GridLine2D {
    pub start: [f32; 2],
    pub end: [f32; 2],
    pub color: [f32; 4],
    pub width: f32,
    pub is_major: bool,
}

#[allow(dead_code)]
pub struct GridOverlay {
    pub config: GridOverlayConfig,
    pub lines: Vec<GridLine2D>,
}

#[allow(dead_code)]
pub fn default_grid_overlay_config() -> GridOverlayConfig {
    GridOverlayConfig {
        cell_width: 32.0,
        cell_height: 32.0,
        origin: [0.0, 0.0],
        color: [0.5, 0.5, 0.5, 0.5],
        major_every: 5,
        major_color: [0.8, 0.8, 0.8, 0.8],
        visible: true,
    }
}

fn build_lines(
    cfg: &GridOverlayConfig,
    viewport_width: f32,
    viewport_height: f32,
) -> Vec<GridLine2D> {
    let mut lines = Vec::new();
    let ox = cfg.origin[0];
    let oy = cfg.origin[1];

    // Vertical lines
    let cols = (viewport_width / cfg.cell_width).ceil() as i32 + 1;
    for col in 0..cols {
        let x = ox + col as f32 * cfg.cell_width;
        let is_major = cfg.major_every > 0 && (col as u32).is_multiple_of(cfg.major_every);
        lines.push(GridLine2D {
            start: [x, 0.0],
            end: [x, viewport_height],
            color: if is_major { cfg.major_color } else { cfg.color },
            width: if is_major { 2.0 } else { 1.0 },
            is_major,
        });
    }

    // Horizontal lines
    let rows = (viewport_height / cfg.cell_height).ceil() as i32 + 1;
    for row in 0..rows {
        let y = oy + row as f32 * cfg.cell_height;
        let is_major = cfg.major_every > 0 && (row as u32).is_multiple_of(cfg.major_every);
        lines.push(GridLine2D {
            start: [0.0, y],
            end: [viewport_width, y],
            color: if is_major { cfg.major_color } else { cfg.color },
            width: if is_major { 2.0 } else { 1.0 },
            is_major,
        });
    }

    lines
}

#[allow(dead_code)]
pub fn build_grid_overlay(
    cfg: &GridOverlayConfig,
    viewport_width: f32,
    viewport_height: f32,
) -> GridOverlay {
    let lines = build_lines(cfg, viewport_width, viewport_height);
    GridOverlay {
        config: GridOverlayConfig {
            cell_width: cfg.cell_width,
            cell_height: cfg.cell_height,
            origin: cfg.origin,
            color: cfg.color,
            major_every: cfg.major_every,
            major_color: cfg.major_color,
            visible: cfg.visible,
        },
        lines,
    }
}

#[allow(dead_code)]
pub fn update_grid_overlay(overlay: &mut GridOverlay, viewport_w: f32, viewport_h: f32) {
    overlay.lines = build_lines(&overlay.config, viewport_w, viewport_h);
}

#[allow(dead_code)]
pub fn grid_line_count_2d(overlay: &GridOverlay) -> usize {
    overlay.lines.len()
}

#[allow(dead_code)]
pub fn major_line_count_2d(overlay: &GridOverlay) -> usize {
    overlay.lines.iter().filter(|l| l.is_major).count()
}

#[allow(dead_code)]
pub fn snap_to_grid_2d(pos: [f32; 2], cfg: &GridOverlayConfig) -> [f32; 2] {
    let ox = cfg.origin[0];
    let oy = cfg.origin[1];
    let sx = ((pos[0] - ox) / cfg.cell_width).round() * cfg.cell_width + ox;
    let sy = ((pos[1] - oy) / cfg.cell_height).round() * cfg.cell_height + oy;
    [sx, sy]
}

#[allow(dead_code)]
pub fn grid_cell_at_2d(pos: [f32; 2], cfg: &GridOverlayConfig) -> [i32; 2] {
    let cx = ((pos[0] - cfg.origin[0]) / cfg.cell_width).floor() as i32;
    let cy = ((pos[1] - cfg.origin[1]) / cfg.cell_height).floor() as i32;
    [cx, cy]
}

#[allow(dead_code)]
pub fn set_grid_visible(overlay: &mut GridOverlay, visible: bool) {
    overlay.config.visible = visible;
}

#[allow(dead_code)]
pub fn set_grid_color(overlay: &mut GridOverlay, color: [f32; 4]) {
    overlay.config.color = color;
    for line in overlay.lines.iter_mut() {
        if !line.is_major {
            line.color = color;
        }
    }
}

#[allow(dead_code)]
pub fn grid_spacing_pixels(cfg: &GridOverlayConfig) -> [f32; 2] {
    [cfg.cell_width, cfg.cell_height]
}

#[allow(dead_code)]
pub fn nearest_grid_point(pos: [f32; 2], cfg: &GridOverlayConfig) -> [f32; 2] {
    snap_to_grid_2d(pos, cfg)
}

/// Returns lines intersecting AABB [x, y, w, h].
#[allow(dead_code)]
pub fn grid_lines_in_rect(overlay: &GridOverlay, rect: [f32; 4]) -> Vec<&GridLine2D> {
    let rx = rect[0];
    let ry = rect[1];
    let rw = rect[2];
    let rh = rect[3];
    overlay
        .lines
        .iter()
        .filter(|line| {
            let min_x = line.start[0].min(line.end[0]);
            let max_x = line.start[0].max(line.end[0]);
            let min_y = line.start[1].min(line.end[1]);
            let max_y = line.start[1].max(line.end[1]);
            max_x >= rx && min_x <= rx + rw && max_y >= ry && min_y <= ry + rh
        })
        .collect()
}

// ── 3-D grid overlay spec API ─────────────────────────────────────────────

/// A single 3-D grid line with start/end in world space and colour.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct GridLine {
    /// World-space start point.
    pub start: [f32; 3],
    /// World-space end point.
    pub end: [f32; 3],
    /// RGBA colour.
    pub color: [f32; 4],
}

/// Configuration for the 3-D viewport grid overlay.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct GridOverlayConfig3D {
    /// Spacing between grid lines in world units.
    pub spacing: f32,
    /// Half-extent of the grid on each axis in world units.
    pub half_extent: f32,
    /// Default grid line colour (RGBA).
    pub color: [f32; 4],
    /// Origin (axis) line colour (RGBA).
    pub origin_line_color: [f32; 4],
    /// Whether the grid overlay is active.
    pub enabled: bool,
}

/// The draw call produced by [`build_grid_lines`] — contains all grid lines.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct GridOverlayDrawCall {
    /// All grid lines to render.
    pub lines: Vec<GridLine>,
}

/// Returns a sensible default [`GridOverlayConfig3D`].
#[allow(dead_code)]
pub fn default_grid_overlay_config_3d() -> GridOverlayConfig3D {
    GridOverlayConfig3D {
        spacing: 1.0,
        half_extent: 10.0,
        color: [0.4, 0.4, 0.4, 1.0],
        origin_line_color: [0.8, 0.2, 0.2, 1.0],
        enabled: true,
    }
}

/// Build the full set of grid lines for the given config.
#[allow(dead_code)]
pub fn build_grid_lines(cfg: &GridOverlayConfig3D) -> GridOverlayDrawCall {
    let mut lines = Vec::new();
    if !cfg.enabled {
        return GridOverlayDrawCall { lines };
    }
    let steps = (cfg.half_extent / cfg.spacing.max(f32::EPSILON)).ceil() as i32;
    // Lines parallel to Z axis
    for i in -steps..=steps {
        let x = i as f32 * cfg.spacing;
        let color = if i == 0 {
            cfg.origin_line_color
        } else {
            cfg.color
        };
        lines.push(GridLine {
            start: [x, 0.0, -cfg.half_extent],
            end: [x, 0.0, cfg.half_extent],
            color,
        });
    }
    // Lines parallel to X axis
    for j in -steps..=steps {
        let z = j as f32 * cfg.spacing;
        let color = if j == 0 {
            cfg.origin_line_color
        } else {
            cfg.color
        };
        lines.push(GridLine {
            start: [-cfg.half_extent, 0.0, z],
            end: [cfg.half_extent, 0.0, z],
            color,
        });
    }
    GridOverlayDrawCall { lines }
}

/// Returns the number of lines in a draw call.
#[allow(dead_code)]
pub fn grid_line_count(call: &GridOverlayDrawCall) -> usize {
    call.lines.len()
}

/// Update the grid spacing.
#[allow(dead_code)]
pub fn set_grid_spacing(cfg: &mut GridOverlayConfig3D, spacing: f32) {
    cfg.spacing = spacing.max(f32::EPSILON);
}

/// Update the half-extent of the grid.
#[allow(dead_code)]
pub fn set_grid_extent(cfg: &mut GridOverlayConfig3D, half_extent: f32) {
    cfg.half_extent = half_extent.max(0.0);
}

/// Set the default grid line colour.
#[allow(dead_code)]
pub fn set_grid_color_3d(cfg: &mut GridOverlayConfig3D, r: f32, g: f32, b: f32, a: f32) {
    cfg.color = [r, g, b, a];
}

/// Returns `true` when the grid overlay is enabled.
#[allow(dead_code)]
pub fn grid_overlay_is_enabled(cfg: &GridOverlayConfig3D) -> bool {
    cfg.enabled
}

/// Enable or disable the grid overlay.
#[allow(dead_code)]
pub fn set_grid_overlay_enabled(cfg: &mut GridOverlayConfig3D, enabled: bool) {
    cfg.enabled = enabled;
}

/// Returns the origin line colour from the config.
#[allow(dead_code)]
pub fn grid_origin_line_color(cfg: &GridOverlayConfig3D) -> [f32; 4] {
    cfg.origin_line_color
}

/// Removes all lines from the draw call.
#[allow(dead_code)]
pub fn grid_overlay_clear(call: &mut GridOverlayDrawCall) {
    call.lines.clear();
}

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

    #[test]
    fn test_default_config() {
        let cfg = default_grid_overlay_config();
        assert_eq!(cfg.cell_width, 32.0);
        assert_eq!(cfg.cell_height, 32.0);
        assert!(cfg.visible);
        assert_eq!(cfg.major_every, 5);
    }

    #[test]
    fn test_build_overlay_produces_lines() {
        let cfg = default_grid_overlay_config();
        let overlay = build_grid_overlay(&cfg, 320.0, 320.0);
        assert!(!overlay.lines.is_empty());
    }

    #[test]
    fn test_grid_line_count() {
        let cfg = default_grid_overlay_config();
        let overlay = build_grid_overlay(&cfg, 320.0, 320.0);
        assert!(grid_line_count_2d(&overlay) > 0);
    }

    #[test]
    fn test_major_line_count() {
        let cfg = default_grid_overlay_config();
        let overlay = build_grid_overlay(&cfg, 320.0, 320.0);
        let major = major_line_count_2d(&overlay);
        let total = grid_line_count_2d(&overlay);
        assert!(major < total);
        assert!(major > 0);
    }

    #[test]
    fn test_snap_to_grid() {
        let cfg = default_grid_overlay_config();
        let snapped = snap_to_grid_2d([50.0, 50.0], &cfg);
        assert!((snapped[0] - 64.0).abs() < 0.01 || (snapped[0] - 32.0).abs() < 0.01);
    }

    #[test]
    fn test_snap_to_grid_on_origin() {
        let cfg = default_grid_overlay_config();
        let snapped = snap_to_grid_2d([0.0, 0.0], &cfg);
        assert!((snapped[0] - 0.0).abs() < 0.01);
        assert!((snapped[1] - 0.0).abs() < 0.01);
    }

    #[test]
    fn test_grid_cell_at() {
        let cfg = default_grid_overlay_config();
        let cell = grid_cell_at_2d([50.0, 70.0], &cfg);
        assert_eq!(cell[0], 1); // 50/32 = 1.56 => floor = 1
        assert_eq!(cell[1], 2); // 70/32 = 2.18 => floor = 2
    }

    #[test]
    fn test_set_visible() {
        let cfg = default_grid_overlay_config();
        let mut overlay = build_grid_overlay(&cfg, 320.0, 320.0);
        set_grid_visible(&mut overlay, false);
        assert!(!overlay.config.visible);
        set_grid_visible(&mut overlay, true);
        assert!(overlay.config.visible);
    }

    #[test]
    fn test_set_color() {
        let cfg = default_grid_overlay_config();
        let mut overlay = build_grid_overlay(&cfg, 320.0, 320.0);
        let new_color = [1.0, 0.0, 0.0, 1.0];
        set_grid_color(&mut overlay, new_color);
        assert_eq!(overlay.config.color, new_color);
    }

    #[test]
    fn test_grid_spacing_pixels() {
        let cfg = default_grid_overlay_config();
        let spacing = grid_spacing_pixels(&cfg);
        assert_eq!(spacing[0], 32.0);
        assert_eq!(spacing[1], 32.0);
    }

    #[test]
    fn test_nearest_grid_point() {
        let cfg = default_grid_overlay_config();
        let pt = nearest_grid_point([15.0, 15.0], &cfg);
        assert!((pt[0] - 0.0).abs() < 0.01 || (pt[0] - 32.0).abs() < 0.01);
    }

    #[test]
    fn test_grid_lines_in_rect() {
        let cfg = default_grid_overlay_config();
        let overlay = build_grid_overlay(&cfg, 320.0, 320.0);
        let lines = grid_lines_in_rect(&overlay, [0.0, 0.0, 100.0, 100.0]);
        assert!(!lines.is_empty());
    }

    #[test]
    fn test_update_grid_overlay() {
        let cfg = default_grid_overlay_config();
        let mut overlay = build_grid_overlay(&cfg, 320.0, 320.0);
        let old_count = grid_line_count_2d(&overlay);
        update_grid_overlay(&mut overlay, 640.0, 640.0);
        let new_count = grid_line_count_2d(&overlay);
        assert!(new_count >= old_count);
    }

    // ── 3-D grid overlay API ─────────────────────────────────────────────────

    #[test]
    fn test_default_grid_overlay_config_3d() {
        let cfg = default_grid_overlay_config_3d();
        assert!(cfg.enabled);
        assert!((cfg.spacing - 1.0).abs() < 1e-5);
        assert!((cfg.half_extent - 10.0).abs() < 1e-5);
    }

    #[test]
    fn test_build_grid_lines_produces_lines() {
        let cfg = default_grid_overlay_config_3d();
        let call = build_grid_lines(&cfg);
        assert!(!call.lines.is_empty());
    }

    #[test]
    fn test_grid_line_count_nonempty() {
        let cfg = default_grid_overlay_config_3d();
        let call = build_grid_lines(&cfg);
        assert!(grid_line_count(&call) > 0);
    }

    #[test]
    fn test_build_grid_lines_disabled_returns_empty() {
        let mut cfg = default_grid_overlay_config_3d();
        cfg.enabled = false;
        let call = build_grid_lines(&cfg);
        assert_eq!(grid_line_count(&call), 0);
    }

    #[test]
    fn test_set_grid_spacing() {
        let mut cfg = default_grid_overlay_config_3d();
        set_grid_spacing(&mut cfg, 2.0);
        assert!((cfg.spacing - 2.0).abs() < 1e-5);
    }

    #[test]
    fn test_set_grid_extent() {
        let mut cfg = default_grid_overlay_config_3d();
        set_grid_extent(&mut cfg, 5.0);
        assert!((cfg.half_extent - 5.0).abs() < 1e-5);
    }

    #[test]
    fn test_set_grid_color_3d() {
        let mut cfg = default_grid_overlay_config_3d();
        set_grid_color_3d(&mut cfg, 0.1, 0.2, 0.3, 0.9);
        assert!((cfg.color[0] - 0.1).abs() < 1e-5);
        assert!((cfg.color[3] - 0.9).abs() < 1e-5);
    }

    #[test]
    fn test_grid_overlay_is_enabled() {
        let cfg = default_grid_overlay_config_3d();
        assert!(grid_overlay_is_enabled(&cfg));
    }

    #[test]
    fn test_set_grid_overlay_enabled() {
        let mut cfg = default_grid_overlay_config_3d();
        set_grid_overlay_enabled(&mut cfg, false);
        assert!(!grid_overlay_is_enabled(&cfg));
        set_grid_overlay_enabled(&mut cfg, true);
        assert!(grid_overlay_is_enabled(&cfg));
    }

    #[test]
    fn test_grid_origin_line_color() {
        let cfg = default_grid_overlay_config_3d();
        let col = grid_origin_line_color(&cfg);
        assert_eq!(col, cfg.origin_line_color);
    }

    #[test]
    fn test_grid_overlay_clear() {
        let cfg = default_grid_overlay_config_3d();
        let mut call = build_grid_lines(&cfg);
        assert!(!call.lines.is_empty());
        grid_overlay_clear(&mut call);
        assert_eq!(grid_line_count(&call), 0);
    }
}