use glam::Vec2;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PickElementType {
#[default]
None,
Point,
Vertex,
Face,
Edge,
Cell,
}
#[derive(Debug, Clone, Default)]
pub struct PickResult {
pub hit: bool,
pub structure_type: String,
pub structure_name: String,
pub element_index: u64,
pub element_type: PickElementType,
pub screen_pos: Vec2,
pub depth: f32,
}
#[must_use]
pub fn color_to_index(r: u8, g: u8, b: u8) -> u32 {
(u32::from(r) << 16) | (u32::from(g) << 8) | u32::from(b)
}
#[repr(C)]
#[derive(Debug, Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
#[allow(clippy::pub_underscore_fields)]
pub struct PickUniforms {
pub global_start: u32,
pub point_radius: f32,
pub _padding: [f32; 2],
}
impl Default for PickUniforms {
fn default() -> Self {
Self {
global_start: 0,
point_radius: 0.01,
_padding: [0.0; 2],
}
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
#[allow(clippy::pub_underscore_fields)]
pub struct TubePickUniforms {
pub global_start: u32,
pub radius: f32,
pub min_pick_radius: f32,
pub _padding: f32,
}
impl Default for TubePickUniforms {
fn default() -> Self {
Self {
global_start: 0,
radius: 0.01,
min_pick_radius: 0.02, _padding: 0.0,
}
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
#[allow(clippy::pub_underscore_fields)]
pub struct MeshPickUniforms {
pub global_start: u32,
pub _padding: [f32; 3],
pub model: [[f32; 4]; 4],
}
impl Default for MeshPickUniforms {
fn default() -> Self {
Self {
global_start: 0,
_padding: [0.0; 3],
model: [
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
],
}
}
}
#[must_use]
pub fn index_to_color(index: u32) -> [u8; 3] {
[
((index >> 16) & 0xFF) as u8,
((index >> 8) & 0xFF) as u8,
(index & 0xFF) as u8,
]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_color_index_roundtrip() {
for index in [
0,
1,
255,
256,
65535,
65536,
0x00FF_FFFF,
12_345_678 & 0x00FF_FFFF,
] {
let color = index_to_color(index);
let decoded = color_to_index(color[0], color[1], color[2]);
assert_eq!(
decoded,
index & 0x00FF_FFFF,
"Roundtrip failed for index {index}",
);
}
}
#[test]
fn test_specific_colors() {
assert_eq!(index_to_color(0), [0, 0, 0]);
assert_eq!(index_to_color(1), [0, 0, 1]);
assert_eq!(index_to_color(255), [0, 0, 255]);
assert_eq!(index_to_color(256), [0, 1, 0]);
assert_eq!(index_to_color(0x00FF_0000), [255, 0, 0]);
assert_eq!(index_to_color(0x0000_FF00), [0, 255, 0]);
assert_eq!(index_to_color(0x0000_00FF), [0, 0, 255]);
}
}