feat: added zoom and colors

This commit is contained in:
Yandrik 2023-01-02 13:21:56 +01:00
parent 99dbfeaaa9
commit b57ed1fa70

View File

@ -1,6 +1,6 @@
use crossterm::cursor::MoveTo;
use crossterm::style::Attribute::Dim;
use crossterm::style::Print;
use crossterm::style::{Color, Print, SetForegroundColor};
use crossterm::terminal::{Clear, ClearType, enable_raw_mode};
use crossterm::{execute, QueueableCommand};
use ndarray::{arr1, arr2, Array1, Array2};
@ -22,6 +22,8 @@ enum Dimension {
struct Mesh {
points: Vec<Array1<f32>>,
edges: Vec<[usize; 2]>,
line_color: Option<Color>,
node_color: Option<Color>,
}
#[inline]
@ -160,6 +162,7 @@ static ANGLE: Mutex<f32> = Mutex::new(0.);
fn draw_rotating_mesh(meshes: Vec<Mesh>) -> anyhow::Result<()> {
let mut angle = (0., 0., 0.);
let mut zoom = 1.;
loop {
@ -172,6 +175,8 @@ fn draw_rotating_mesh(meshes: Vec<Mesh>) -> anyhow::Result<()> {
KeyCode::Char('d') => angle.2 -= 6.,
KeyCode::Char('q') => angle.1 += 6.,
KeyCode::Char('e') => angle.1 -= 6.,
KeyCode::Char(',') => zoom = 0.2f32.max(zoom - 0.02),
KeyCode::Char('.') => zoom = 1f32.min(zoom + 0.02),
KeyCode::Esc | KeyCode::Backspace | KeyCode::Char('c') => exit(0),
_ => {},
}
@ -185,6 +190,7 @@ fn draw_rotating_mesh(meshes: Vec<Mesh>) -> anyhow::Result<()> {
let x_matrix = rotation_matrix(angle.0, Dimension::X);
let y_matrix = rotation_matrix(angle.1, Dimension::Y);
let z_matrix = rotation_matrix(angle.2, Dimension::Z);
let zoom_matrix = scale(zoom, zoom, zoom);
let cam_matrix = ortho_matrix();
let mut stdout = stdout();
@ -200,10 +206,14 @@ fn draw_rotating_mesh(meshes: Vec<Mesh>) -> anyhow::Result<()> {
.map(|pt| x_matrix.dot(&pt))
.map(|pt| y_matrix.dot(&pt))
.map(|pt| z_matrix.dot(&pt))
.map(|pt| zoom_matrix.dot(&pt))
.map(|pt| cam_matrix.dot(&pt))
.map(|pt| pt + arr1(&[30., 30.])) // draw shift
.collect::<Vec<Array1<_>>>();
// set edge color before edge drawing
stdout.queue(SetForegroundColor(mesh.line_color.unwrap_or(Color::White)))?;
for edge in edges.iter() {
let origin = &projected_points[edge[0]];
let dest = &projected_points[edge[1]];
@ -217,6 +227,9 @@ fn draw_rotating_mesh(meshes: Vec<Mesh>) -> anyhow::Result<()> {
)?;
}
// set mesh point color
stdout.queue(SetForegroundColor(mesh.node_color.unwrap_or(Color::White)))?;
for pt in &projected_points {
let pt = pt;
stdout
@ -236,6 +249,26 @@ fn main() -> anyhow::Result<()> {
enable_raw_mode()?;
let random_rectangle = Mesh {
points: vec![
arr1(&[-10., 0., 0.]),
arr1(&[0., 10., 0.]),
arr1(&[10., 0., 0.]),
arr1(&[0., -10., 0.]),
],
edges: vec![
[0, 1],
[1, 2],
[2, 3],
[3, 0],
],
line_color: Some(Color::Red),
node_color: Some(Color::DarkRed)
};
let cube = vec![
arr1(&[-10., -10., 10.]),
arr1(&[-10., 10., 10.]),
@ -294,14 +327,19 @@ fn main() -> anyhow::Result<()> {
[2, 4],
[2, 5],
],
line_color: Some(Color::Blue),
node_color: Some(Color::White),
};
draw_rotating_mesh(vec![
random_rectangle,
thing,
Mesh {
points: cube,
edges,
line_color: Some(Color::Green),
node_color: Some(Color::White),
},
thing,
])?;
unreachable!()