| |
| |
| |
|
|
| use serde::{Deserialize, Serialize}; |
| use std::collections::HashMap; |
|
|
| pub type Pt = [f64; 2]; |
|
|
| #[derive(Debug, Deserialize)] |
| pub struct Edge { |
| pub id: i64, |
| #[allow(dead_code)] |
| pub node_a: i64, |
| #[allow(dead_code)] |
| pub node_b: i64, |
| pub left_label: i64, |
| pub right_label: i64, |
| |
| pub interior: bool, |
| |
| pub cubics: Vec<Vec<Pt>>, |
| } |
|
|
| #[derive(Debug, Deserialize)] |
| pub struct Face { |
| pub label: i64, |
| |
| pub loops: Vec<Vec<[i64; 2]>>, |
| } |
|
|
| #[derive(Debug, Deserialize)] |
| pub struct Golden { |
| pub e_data: f64, |
| pub coverage_checksums: HashMap<String, Checksum>, |
| pub coverage_samples: Vec<CoverageSample>, |
| pub vertex_gradients: Vec<GradientSample>, |
| } |
|
|
| #[derive(Debug, Deserialize, Serialize, Clone, Copy)] |
| pub struct Checksum { |
| pub sum: f64, |
| pub sumsq: f64, |
| } |
|
|
| #[derive(Debug, Deserialize)] |
| pub struct CoverageSample { |
| pub label: i64, |
| pub row: usize, |
| pub col: usize, |
| pub value: f64, |
| } |
|
|
| #[derive(Debug, Deserialize)] |
| pub struct GradientSample { |
| pub edge: i64, |
| pub cubic: usize, |
| pub vertex: usize, |
| pub gx: f64, |
| pub gy: f64, |
| } |
|
|
| #[derive(Debug, Deserialize)] |
| pub struct Vectors { |
| pub name: String, |
| pub kind: String, |
| pub seed: u64, |
| pub width: usize, |
| pub height: usize, |
| pub background: f64, |
| pub l0: f64, |
| pub colors255: HashMap<String, [f64; 3]>, |
| pub edges: Vec<Edge>, |
| pub faces: Vec<Face>, |
| pub target_u8: Vec<u8>, |
| pub golden: Golden, |
| } |
|
|
| impl Vectors { |
| pub fn edge_by_id(&self) -> HashMap<i64, &Edge> { |
| self.edges.iter().map(|e| (e.id, e)).collect() |
| } |
|
|
| |
| |
| pub fn target(&self) -> Vec<f64> { |
| self.target_u8.iter().map(|&b| b as f64 / 255.0).collect() |
| } |
|
|
| |
| |
| pub fn colors01(&self) -> HashMap<i64, [f64; 3]> { |
| self.colors255 |
| .iter() |
| .map(|(k, v)| { |
| let lb: i64 = k.parse().expect("label key must be an integer"); |
| let c = [ |
| (v[0] / 255.0).clamp(0.0, 1.0), |
| (v[1] / 255.0).clamp(0.0, 1.0), |
| (v[2] / 255.0).clamp(0.0, 1.0), |
| ]; |
| (lb, c) |
| }) |
| .collect() |
| } |
| } |
|
|