Skip to content

Commit

Permalink
Merge branch 'master' into sphere
Browse files Browse the repository at this point in the history
  • Loading branch information
coillteoir authored May 3, 2024
2 parents 36c2a05 + cc64876 commit fd49318
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 63 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

env:
CARGO_TERM_COLOR: always

Expand Down
154 changes: 93 additions & 61 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
use std::f32::consts::PI;

#[derive(Clone)]
struct Point {
x: f32,
y: f32,
z: f32,
}

impl Point {
pub fn new(x: f32, y: f32, z: f32) -> Self {
Self { x, y, z }
}
}

fn main() {
println!("{}", compile("cuboid 1 4 2".to_string()).unwrap());
println!("{}", compile("prism 1".to_string()).unwrap())
}

fn vertex_string(points: Vec<Point>) -> String {
points
.into_iter()
.map(|p| format!("v {0} {1} {2}", p.x, p.y, p.z))
.collect::<Vec<String>>()
.join("\n")
}

fn prism(_radius: f32) -> Result<String, String> {
let points = [
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!(
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<String, String> {
Expand All @@ -9,79 +51,58 @@ fn cuboid(sx: f32, sy: f32, sz: f32) -> Result<String, String> {
"could not generate cuboid, side length less than or equal to zero".to_string(),
);
}
let points = [
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),
];
Ok(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
"#
"#,
vertex_string(points.to_vec())
))
}

fn cube(_x: f32, _y: f32, _z: f32, size: f32) -> Result<String, String> {
fn cube(size: f32) -> Result<String, String> {
println!("GENERATING CUBE");
if size <= 0.0 {
return Err(String::from(
"ERROR: cannot generate cube of size less than zero",
));
return Err("ERROR: cannot generate cube of size less than zero".to_string());
}
cuboid(size, size, size)
}

fn compile(data: String) -> Result<String, String> {
let mut result = String::new();
let mut error = String::new();
let mut result = String::from("");

let lines = data.split('\n');
for line in lines {
let tokens: Vec<&str> = line.split(' ').collect();
let mut data = String::new();
match tokens.first() {
Some(&"cube") => match cube(
0.0,
0.0,
0.0,
tokens
.get(1)
.expect("please specify a size argument")
.parse::<f32>()
.expect("invalid value given"),
) {
Ok(g) => data = g,
Err(e) => error.push_str(&e),
},
Some(&"cuboid") => match cuboid(
tokens
.get(1)
.expect("invalid number of arguments in cuboid")
.parse::<f32>()
.expect("non numeric value given"),
tokens
.get(2)
.expect("invalid number of arguments in cuboid")
.parse::<f32>()
.expect("non numeric value given"),
tokens
.get(3)
.expect("invalid number of arguments in cuboid")
.parse::<f32>()
.expect("non numeric value given"),
) {
Ok(g) => data = g,
Err(e) => error.push_str(&e),
},
Some(&_) => eprintln!("{} not supported", tokens[0]),
None => unimplemented!(),
match tokens[0] {
"cube" => result
.push_str(&cube(tokens[1].parse::<f32>().expect("invalid value given")).unwrap()),
"cuboid" => result.push_str(
&cuboid(
tokens[1].parse::<f32>().expect("non numeric value given"),
tokens[2].parse::<f32>().expect("non numeric value given"),
tokens[3].parse::<f32>().expect("non numeric value given"),
)
.unwrap(),
),
"prism" => result.push_str(
&prism(tokens[1].parse::<f32>().expect("non numeric value given")).unwrap(),
),
&_ => println!("{} not supported", tokens[0]),
}
result.push_str(&data);
}
Expand All @@ -95,16 +116,27 @@ fn compile(data: String) -> Result<String, String> {
mod tests {
use crate::compile;
use std::fs;
#[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");

fn run_test(input_path: String, result_path: String) {
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_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), Ok(result));
fn walk_tests() {
match fs::read_dir("./tests/") {
Ok(entries) => {
for entry in entries {
let path = entry.expect("could not open dir").path();
let input = path.join("main.kda");
let output = path.join("result.obj");
run_test(
input.into_os_string().into_string().unwrap(),
output.into_os_string().into_string().unwrap(),
)
}
}
Err(e) => eprintln!("{}", e)
}
}
}
1 change: 1 addition & 0 deletions tests/prism/main.kda
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
prism 1
2 changes: 2 additions & 0 deletions tests/prism/result.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Blender 4.0.1 MTL File: 'None'
# www.blender.org
8 changes: 8 additions & 0 deletions tests/prism/result.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
v 0 0.86602545 0
v 0 0 1
v -0.86602545 0 -0.50000006
v 0.8660254 0 -0.50000006
f -3 -2 -1
f -4 -2 -1
f -4 -3 -1
f -4 -2 -3

0 comments on commit fd49318

Please sign in to comment.