From 8eb3cf875ba8eaeea89f9224fa6c4dd9c1f903d6 Mon Sep 17 00:00:00 2001 From: David Lynch Date: Fri, 3 May 2024 01:22:00 +0100 Subject: [PATCH 1/7] defined point struct, and passing tests now --- src/main.rs | 32 +++++++++++++++++++++++--------- tests/prism/main.kda | 1 + 2 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 tests/prism/main.kda diff --git a/src/main.rs b/src/main.rs index 941a4e7..ef23ed7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,28 +1,42 @@ +struct Point { + x: f32, + y: f32, + z: f32, +} + fn main() { println!("{}", compile("cuboid 2 3 4".to_string())) } +fn prism() -> String { + "".to_string() +} + fn cuboid(sx: f32, sy: f32, sz: f32) -> String { println!("GENERATING CUBOID"); if sx <= 0.0 || sy <= 0.0 || sz <= 0.0 { return "could not generate cuboid, side length less than or equal to zero".to_string(); } + let points = [ + Point {x: 0.0,y: sy,z: sz,}, + Point {x: 0.0,y: 0.0,z: sz,}, + Point {x: sx,y: 0.0,z: sz,}, + Point {x: sx,y: sy,z: sz,}, + Point {x: 0.0,y: sy,z: 0.0,}, + Point {x: 0.0,y: 0.0,z: 0.0,}, + Point {x: sx,y: 0.0,z: 0.0,}, + Point {x: sx,y: sy,z: 0.0,}, + ]; + let vertexString = points.into_iter().map(|p| format!("v {0} {1} {2}", p.x, p.y, p.z)).collect::>().join("\n"); format!( - r#"v 0 {sy} {sz} -v 0 0 {sz} -v {sx} 0 {sz} -v {sx} {sy} {sz} -v 0 {sy} 0 -v 0 0 0 -v {sx} 0 0 -v {sx} {sy} 0 + r#"{0} f -8 -7 -6 -5 f -1 -2 -3 -4 f -5 -6 -2 -1 f -4 -8 -5 -1 f -4 -3 -7 -8 f -7 -3 -2 -6 -"# +"#, vertexString ) } diff --git a/tests/prism/main.kda b/tests/prism/main.kda new file mode 100644 index 0000000..a494448 --- /dev/null +++ b/tests/prism/main.kda @@ -0,0 +1 @@ +prism 1 From 1d9c62a6dc221fefad4049426fcd0fc7af904eaf Mon Sep 17 00:00:00 2001 From: David Lynch Date: Fri, 3 May 2024 01:31:13 +0100 Subject: [PATCH 2/7] added constructor to point --- src/main.rs | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index ef23ed7..3dfe577 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,12 @@ struct Point { z: f32, } +impl Point { + pub fn new(x: f32, y: f32, z: f32) -> Self { + Self { x: x, y: y, z: z } + } +} + fn main() { println!("{}", compile("cuboid 2 3 4".to_string())) } @@ -18,16 +24,20 @@ fn cuboid(sx: f32, sy: f32, sz: f32) -> String { return "could not generate cuboid, side length less than or equal to zero".to_string(); } let points = [ - Point {x: 0.0,y: sy,z: sz,}, - Point {x: 0.0,y: 0.0,z: sz,}, - Point {x: sx,y: 0.0,z: sz,}, - Point {x: sx,y: sy,z: sz,}, - Point {x: 0.0,y: sy,z: 0.0,}, - Point {x: 0.0,y: 0.0,z: 0.0,}, - Point {x: sx,y: 0.0,z: 0.0,}, - Point {x: sx,y: sy,z: 0.0,}, + Point::new(0.0, sy, sz), + Point::new(0.0, 0.0, sz), + Point::new(sx, 0.0, sz), + Point::new(sx, sy, sz), + Point::new(0.0, sy, 0.0), + Point::new(0.0, 0.0, 0.0), + Point::new(sx, 0.0, 0.0), + Point::new(sx, sy, 0.0), ]; - let vertexString = points.into_iter().map(|p| format!("v {0} {1} {2}", p.x, p.y, p.z)).collect::>().join("\n"); + let vertexString = points + .into_iter() + .map(|p| format!("v {0} {1} {2}", p.x, p.y, p.z)) + .collect::>() + .join("\n"); format!( r#"{0} f -8 -7 -6 -5 @@ -36,7 +46,8 @@ f -5 -6 -2 -1 f -4 -8 -5 -1 f -4 -3 -7 -8 f -7 -3 -2 -6 -"#, vertexString +"#, + vertexString ) } From 39af0dcb2078790f60971fff533f3d652554f657 Mon Sep 17 00:00:00 2001 From: David Lynch Date: Fri, 3 May 2024 01:39:27 +0100 Subject: [PATCH 3/7] more changes --- src/main.rs | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3dfe577..4ecba43 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ fn cuboid(sx: f32, sy: f32, sz: f32) -> String { Point::new(sx, 0.0, 0.0), Point::new(sx, sy, 0.0), ]; - let vertexString = points + let vertex_string = points .into_iter() .map(|p| format!("v {0} {1} {2}", p.x, p.y, p.z)) .collect::>() @@ -47,35 +47,16 @@ f -4 -8 -5 -1 f -4 -3 -7 -8 f -7 -3 -2 -6 "#, - vertexString + vertex_string ) } -fn cube(x: f32, y: f32, z: f32, size: f32) -> String { +fn cube(size: f32) -> String { println!("GENERATING CUBE"); if size <= 0.0 { return "ERROR: cannot generate cube of size less than zero".to_string(); } - let size_x = size + x; - let size_y = size + y; - let size_z = size + z; - format!( - r#"v {x} {size_y} {size_z} -v {x} {y} {size_z} -v {size_x} {y} {size_z} -v {size_x} {size_y} {size_z} -v {x} {size_y} {z} -v {x} {y} {z} -v {size_x} {y} {z} -v {size_x} {size_y} {z} -f -8 -7 -6 -5 -f -1 -2 -3 -4 -f -5 -6 -2 -1 -f -4 -8 -5 -1 -f -4 -3 -7 -8 -f -7 -3 -2 -6 -"# - ) + cuboid(size, size, size) } fn compile(data: String) -> String { @@ -86,9 +67,6 @@ fn compile(data: String) -> String { let tokens: Vec<&str> = line.split(' ').collect(); match tokens[0] { "cube" => result.push_str(&cube( - 0.0, - 0.0, - 0.0, tokens[1].parse::().expect("invalid value given"), )), "cuboid" => result.push_str(&cuboid( From 776231dccb2c589d670bfd61fd7f1b1e5716f9c7 Mon Sep 17 00:00:00 2001 From: David Lynch Date: Fri, 3 May 2024 01:56:29 +0100 Subject: [PATCH 4/7] result types, and clippy lints --- src/main.rs | 55 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4ecba43..9d8d2f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,22 +6,24 @@ struct Point { impl Point { pub fn new(x: f32, y: f32, z: f32) -> Self { - Self { x: x, y: y, z: z } + Self { x, y, z } } } fn main() { - println!("{}", compile("cuboid 2 3 4".to_string())) + println!("{}", compile("cuboid 2 3 4".to_string()).unwrap()) } fn prism() -> String { "".to_string() } -fn cuboid(sx: f32, sy: f32, sz: f32) -> String { +fn cuboid(sx: f32, sy: f32, sz: f32) -> Result { println!("GENERATING CUBOID"); if sx <= 0.0 || sy <= 0.0 || sz <= 0.0 { - return "could not generate cuboid, side length less than or equal to zero".to_string(); + return Err( + "could not generate cuboid, side length less than or equal to zero".to_string(), + ); } let points = [ Point::new(0.0, sy, sz), @@ -38,7 +40,7 @@ fn cuboid(sx: f32, sy: f32, sz: f32) -> String { .map(|p| format!("v {0} {1} {2}", p.x, p.y, p.z)) .collect::>() .join("\n"); - format!( + Ok(format!( r#"{0} f -8 -7 -6 -5 f -1 -2 -3 -4 @@ -48,52 +50,57 @@ f -4 -3 -7 -8 f -7 -3 -2 -6 "#, vertex_string - ) + )) } -fn cube(size: f32) -> String { +fn cube(size: f32) -> Result { println!("GENERATING CUBE"); if size <= 0.0 { - return "ERROR: cannot generate cube of size less than zero".to_string(); + return Err("ERROR: cannot generate cube of size less than zero".to_string()); } cuboid(size, size, size) } -fn compile(data: String) -> String { +fn compile(data: String) -> Result { let mut result = String::from(""); let lines = data.split('\n'); for line in lines { let tokens: Vec<&str> = line.split(' ').collect(); match tokens[0] { - "cube" => result.push_str(&cube( - tokens[1].parse::().expect("invalid value given"), - )), - "cuboid" => result.push_str(&cuboid( - tokens[1].parse::().expect("non numeric value given"), - tokens[2].parse::().expect("non numeric value given"), - tokens[3].parse::().expect("non numeric value given"), - )), + "cube" => result + .push_str(&cube(tokens[1].parse::().expect("invalid value given")).unwrap()), + "cuboid" => result.push_str( + &cuboid( + tokens[1].parse::().expect("non numeric value given"), + tokens[2].parse::().expect("non numeric value given"), + tokens[3].parse::().expect("non numeric value given"), + ) + .unwrap(), + ), &_ => println!("{} not supported", tokens[0]), } } - result + Ok(result) } #[cfg(test)] mod tests { use crate::compile; use std::fs; + + fn run_test(input_path: &str, result_path: &str) { + let input = fs::read_to_string(input_path).expect("could not load file"); + let result = fs::read_to_string(result_path).expect("could not load file"); + assert_eq!(compile(input), Ok(result)); + } + #[test] fn test_compile_cube() { - let input = fs::read_to_string("./tests/cube/main.kda").expect("could not load file"); - let result = fs::read_to_string("./tests/cube/result.obj").expect("could not load file"); - assert_eq!(compile(input), result); + run_test("./tests/cube/main.kda", "./tests/cube/result.obj"); } #[test] fn test_compile_cuboid() { - let input = fs::read_to_string("./tests/cuboid/main.kda").expect("could not load file"); - let result = fs::read_to_string("./tests/cuboid/result.obj").expect("could not load file"); - assert_eq!(compile(input), result); + run_test("./tests/cuboid/main.kda", "./tests/cuboid/result.obj"); } } From ae4836181d004b6c57fdf284b45773813625799c Mon Sep 17 00:00:00 2001 From: David Lynch Date: Fri, 3 May 2024 03:07:20 +0100 Subject: [PATCH 5/7] added prism to compiler Working through prism, first time doing trigonometry in years!! fun stuff --- src/main.rs | 42 +++++++++++++++++++++++++++++++++--------- tests/prism/result.mtl | 2 ++ tests/prism/result.obj | 9 +++++++++ 3 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 tests/prism/result.mtl create mode 100644 tests/prism/result.obj diff --git a/src/main.rs b/src/main.rs index 9d8d2f9..925bd82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +use std::f32::consts::PI; + +#[derive(Clone)] struct Point { x: f32, y: f32, @@ -11,11 +14,34 @@ impl Point { } fn main() { - println!("{}", compile("cuboid 2 3 4".to_string()).unwrap()) + println!("{}", compile("prism 1".to_string()).unwrap()) } -fn prism() -> String { - "".to_string() +fn vertex_string(points: Vec) -> String { + points + .into_iter() + .map(|p| format!("v {0} {1} {2}", p.x, p.y, p.z)) + .collect::>() + .join("\n") +} + +fn prism(side_len: f32) -> Result { + let points = [ + Point::new(0.000000, 1.333333, 0.000000), + Point::new(0.0, 0.000000, 1.000000), + Point::new((4.0 * PI/3.0).sin(), 0.000000, (-PI/3.0).sin()), + Point::new((2.0 * PI/3.0).sin(), 0.000000, (-PI/3.0).sin()), + ]; + + Ok(format!( + r#"{0} +f -3 -2 -1 +f -4 -2 -1 +f -4 -3 -1 +f -4 -2 -3 +"#, + vertex_string(points.to_vec()) + )) } fn cuboid(sx: f32, sy: f32, sz: f32) -> Result { @@ -35,11 +61,6 @@ fn cuboid(sx: f32, sy: f32, sz: f32) -> Result { Point::new(sx, 0.0, 0.0), Point::new(sx, sy, 0.0), ]; - let vertex_string = points - .into_iter() - .map(|p| format!("v {0} {1} {2}", p.x, p.y, p.z)) - .collect::>() - .join("\n"); Ok(format!( r#"{0} f -8 -7 -6 -5 @@ -49,7 +70,7 @@ f -4 -8 -5 -1 f -4 -3 -7 -8 f -7 -3 -2 -6 "#, - vertex_string + vertex_string(points.to_vec()) )) } @@ -78,6 +99,9 @@ fn compile(data: String) -> Result { ) .unwrap(), ), + "prism" => result.push_str( + &prism(tokens[1].parse::().expect("non numeric value given")).unwrap(), + ), &_ => println!("{} not supported", tokens[0]), } } diff --git a/tests/prism/result.mtl b/tests/prism/result.mtl new file mode 100644 index 0000000..6d43325 --- /dev/null +++ b/tests/prism/result.mtl @@ -0,0 +1,2 @@ +# Blender 4.0.1 MTL File: 'None' +# www.blender.org diff --git a/tests/prism/result.obj b/tests/prism/result.obj new file mode 100644 index 0000000..65c5442 --- /dev/null +++ b/tests/prism/result.obj @@ -0,0 +1,9 @@ +v 0 1.333333 0 +v 0 0 1 +v -0.86602545 0 -0.86602545 +v 0.8660254 0 -0.86602545 +f -3 -2 -1 +f -4 -2 -1 +f -4 -3 -1 +f -4 -2 -3 + From f5050aa0046d94dc6798b03cbca1ad3e84b9a8c6 Mon Sep 17 00:00:00 2001 From: David Lynch Date: Fri, 3 May 2024 03:14:14 +0100 Subject: [PATCH 6/7] happy with this --- src/main.rs | 8 ++++---- tests/prism/result.obj | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 925bd82..366b387 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,10 +27,10 @@ fn vertex_string(points: Vec) -> String { fn prism(side_len: f32) -> Result { let points = [ - Point::new(0.000000, 1.333333, 0.000000), - Point::new(0.0, 0.000000, 1.000000), - Point::new((4.0 * PI/3.0).sin(), 0.000000, (-PI/3.0).sin()), - Point::new((2.0 * PI/3.0).sin(), 0.000000, (-PI/3.0).sin()), + Point::new(0.000000, (PI/3.0).sin(), 0.000000), + Point::new(0.0, 0.000000, 0.0_f32.cos()), + Point::new((4.0 * PI/3.0).sin(), 0.000000, (2.0 * -PI/3.0).cos()), + Point::new((2.0 * PI/3.0).sin(), 0.000000, (2.0 * -PI/3.0).cos()), ]; Ok(format!( diff --git a/tests/prism/result.obj b/tests/prism/result.obj index 65c5442..6a4f3dc 100644 --- a/tests/prism/result.obj +++ b/tests/prism/result.obj @@ -1,7 +1,7 @@ -v 0 1.333333 0 +v 0 0.86602545 0 v 0 0 1 -v -0.86602545 0 -0.86602545 -v 0.8660254 0 -0.86602545 +v -0.86602545 0 -0.50000006 +v 0.8660254 0 -0.50000006 f -3 -2 -1 f -4 -2 -1 f -4 -3 -1 From ad42b937aef1cb341dc8cb017bf4afa1c52d9ff1 Mon Sep 17 00:00:00 2001 From: David Lynch Date: Fri, 3 May 2024 03:16:00 +0100 Subject: [PATCH 7/7] added test case --- src/main.rs | 4 ++++ tests/prism/result.obj | 1 - 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 366b387..f2eb9db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -127,4 +127,8 @@ mod tests { fn test_compile_cuboid() { run_test("./tests/cuboid/main.kda", "./tests/cuboid/result.obj"); } + #[test] + fn test_compile_prism() { + run_test("./tests/prism/main.kda", "./tests/prism/result.obj"); + } } diff --git a/tests/prism/result.obj b/tests/prism/result.obj index 6a4f3dc..a393d93 100644 --- a/tests/prism/result.obj +++ b/tests/prism/result.obj @@ -6,4 +6,3 @@ f -3 -2 -1 f -4 -2 -1 f -4 -3 -1 f -4 -2 -3 -