refactor: restructured the architecture of Renderer
This commit is contained in:
parent
4ff13640a7
commit
72fcf51d63
|
@ -1,2 +1,3 @@
|
||||||
.idea
|
.idea
|
||||||
/target
|
/target
|
||||||
|
image.png
|
|
@ -0,0 +1,5 @@
|
||||||
|
#[derive(thiserror::Error, Debug)]
|
||||||
|
pub enum LithiumError {
|
||||||
|
#[error("There is no camera with the ID {0}")]
|
||||||
|
CameraNotFound(usize),
|
||||||
|
}
|
40
src/main.rs
40
src/main.rs
|
@ -1,46 +1,16 @@
|
||||||
pub mod output;
|
|
||||||
pub mod render;
|
pub mod render;
|
||||||
|
pub mod error;
|
||||||
|
|
||||||
use crate::output::{Metadata, Output};
|
|
||||||
use crate::render::Renderer;
|
use crate::render::Renderer;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use image::RgbImage;
|
|
||||||
|
|
||||||
struct TestOutput {
|
|
||||||
width: u32,
|
|
||||||
height: u32,
|
|
||||||
image: RgbImage,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl output::Output for TestOutput {
|
|
||||||
fn get_meta(&self) -> Metadata {
|
|
||||||
Metadata {
|
|
||||||
width: self.width,
|
|
||||||
height: self.height,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render(&mut self, buffer: &RgbImage) -> Result<()> {
|
|
||||||
self.image = buffer.clone();
|
|
||||||
self.image.save("image.png")?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TestOutput {
|
|
||||||
fn new(width: u32, height: u32) -> Self {
|
|
||||||
Self {
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
image: RgbImage::new(width, height),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
let mut renderer = Renderer::new();
|
let mut renderer = Renderer::new();
|
||||||
renderer.add_camera(TestOutput::new(512, 512));
|
let camera = renderer.add_camera(512, 512);
|
||||||
|
|
||||||
|
let image = renderer.render(camera).await?;
|
||||||
|
image.save("image.png")?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
use anyhow::Result;
|
|
||||||
use image::RgbImage;
|
|
||||||
|
|
||||||
pub struct Metadata {
|
|
||||||
pub height: u32,
|
|
||||||
pub width: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Output {
|
|
||||||
fn get_meta(&self) -> Metadata;
|
|
||||||
fn render(&mut self, buffer: &RgbImage) -> Result<()>;
|
|
||||||
}
|
|
|
@ -1,13 +1,18 @@
|
||||||
use crate::output::Output;
|
|
||||||
|
|
||||||
pub struct Camera {
|
pub struct Camera {
|
||||||
output: Box<dyn Output>,
|
width: u32,
|
||||||
|
height: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Camera {
|
impl Camera {
|
||||||
pub fn new(output: impl Output + 'static) -> Self {
|
pub fn new(width: u32, height: u32) -> Self {
|
||||||
Self {
|
Self { width, height }
|
||||||
output: Box::new(output),
|
}
|
||||||
}
|
|
||||||
|
pub fn height(&self) -> u32 {
|
||||||
|
self.height
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn width(&self) -> u32 {
|
||||||
|
self.width
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
pub mod camera;
|
pub mod camera;
|
||||||
|
|
||||||
use crate::output::Output;
|
use crate::{error::LithiumError, render::camera::Camera};
|
||||||
use crate::render::camera::Camera;
|
use anyhow::Result;
|
||||||
|
use image::RgbImage;
|
||||||
use lithium_loader::Model;
|
use lithium_loader::Model;
|
||||||
use slab::Slab;
|
use slab::Slab;
|
||||||
|
|
||||||
|
@ -19,7 +19,17 @@ impl Renderer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_camera(&mut self, output: impl Output + 'static) -> usize {
|
pub fn add_camera(&mut self, width: u32, height: u32) -> usize {
|
||||||
self.cameras.insert(Camera::new(output))
|
self.cameras.insert(Camera::new(width, height))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn render(&self, camera: usize) -> Result<image::RgbImage, LithiumError> {
|
||||||
|
let camera = self
|
||||||
|
.cameras
|
||||||
|
.get(camera)
|
||||||
|
.ok_or(LithiumError::CameraNotFound(camera))?;
|
||||||
|
let result = RgbImage::new(camera.height(), camera.width());
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue