use crate::bezier::BezierSurf;
use crate::bezier::BezierFactory;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
fn parse_patch(line: &String) -> [usize; 16] {
let v = line.split(",").collect::<Vec<&str>>();
let mut ret = [0usize; 16];
let mut index = 0usize;
for i in v {
ret[index] = i.parse().unwrap();
index += 1;
}
ret
}
fn parse_vertex(line: &String) -> [f64; 3] {
let v = line.split(",").collect::<Vec<&str>>();
let mut iter = v.iter();
[
iter.next().unwrap().parse().unwrap(),
iter.next().unwrap().parse().unwrap(),
iter.next().unwrap().parse().unwrap()
]
}
pub fn read_utah_format(file_path: String) -> Result<Vec<BezierSurf<3>>, ()> {
let file = File::open(file_path);
match file {
Err(_) => { return Err(()); }
Ok(file) => {
let reader = BufReader::new(file);
let mut lines = reader.lines();
let patch_count;
match lines.next() {
None => { return Err(()); }
Some(line) => { patch_count = line.unwrap().parse().unwrap(); }
}
let mut patches = Vec::<[usize; 16]>::new();
for _ in 0..patch_count {
match lines.next() {
None => { return Err(()); }
Some(line) => { patches.push(parse_patch(&line.unwrap())); }
}
}
let vertex_count;
match lines.next() {
None => { return Err(()); }
Some(line) => { vertex_count = line.unwrap().parse().unwrap(); }
}
let mut vertices = Vec::<[f64; 3]>::new();
for _ in 0..vertex_count {
match lines.next() {
None => { return Err(()); }
Some(line) => { vertices.push(parse_vertex(&line.unwrap())); }
}
}
return Ok(BezierFactory::from_indexed_vertices(patches, vertices));
}
}
}
#[cfg(test)]
mod tests {
use crate::bezier::read_utah_format;
#[test]
fn test_eq() {
assert_eq!(read_utah_format("utah_teapot_data/teapot".to_string()).unwrap().len(), 32);
assert_eq!(read_utah_format("utah_teapot_data/teacup".to_string()).unwrap().len(), 26);
assert_eq!(read_utah_format("utah_teapot_data/teaspoon".to_string()).unwrap().len(), 16);
}
}