points_cloud/
points_cloud.rs

1use graphics_rs::{
2    color,
3    graphics::Graphics,
4    math::vec3::Vec3,
5    shapes::point_cloud::PointCloud,
6    simple_canvas::SimpleCanvas,
7    tools::camera::Camera,
8    traits::{canvas::Canvas, canvas_handler::CanvasHandler},
9};
10
11struct PointsCloudSample {
12    point_cloud: PointCloud,
13}
14
15impl<'a> CanvasHandler for PointsCloudSample {
16    fn update<T: Canvas>(&mut self, canvas: &mut T) {
17        canvas.change_color(color::BLACK);
18        canvas.fill();
19
20        canvas.change_color(color::GREEN);
21        canvas.draw_shape(&mut self.point_cloud);
22
23        let rotation = self.point_cloud.rotation();
24        rotation.set_z(rotation.z() + 1f64);
25
26        return;
27    }
28}
29
30fn create_point_cloud() -> PointCloud {
31    let min = -10;
32    let max = 10;
33    let fov_factor = 150;
34    let mut points = Vec::<Vec3<i64>>::new();
35    let size = 2;
36
37    for x in min..max {
38        for y in min..max {
39            for z in min..max {
40                points.push(Vec3::new(x, y, z));
41            }
42        }
43    }
44
45    let camera = Camera::new(Vec3::new(0, 0, -20), Vec3::new(0, 0, 0), 90);
46
47    PointCloud::new(
48        points,
49        min,
50        max,
51        fov_factor,
52        camera,
53        Vec3::<f64>::new(0f64, 0f64, 0f64),
54        size,
55    )
56}
57
58const WIDTH: usize = 800;
59const HEIGHT: usize = 800;
60
61fn main() -> Result<(), String> {
62    let antialiasing = false;
63    let antialiasing_resolution = 1;
64    let fill_color = Some(color::BLACK);
65
66    let mut canvas = SimpleCanvas::new(
67        WIDTH as usize,
68        HEIGHT as usize,
69        fill_color,
70        antialiasing,
71        antialiasing_resolution,
72    );
73
74    let mut graphics = Graphics::create(&mut canvas)?;
75    graphics.run(&mut PointsCloudSample {
76        point_cloud: create_point_cloud(),
77    })?;
78
79    Ok(())
80}