slam_viewer/window/renderers/
world.rs1use super::super::builder::{WindowBuilder, WindowBuilderDefault};
2use super::super::camera::{CameraControllerConfig, CameraFrustum};
3use super::super::lines::{Line, LineSource, LinesBuilder, LinesRendener};
4use super::super::points::{Point, PointSource, PointsBuilder, PointsRendener};
5use crate::pipes::{PipelineBuilder, PipelineRenderer, VertexFormat};
6
7use nalgebra::Point3;
8use slam_cv::{feature::Landmark, vo::World, Colors, Number};
9
10pub struct WorldRenderer<N, F, W>
11where
12 N: 'static + Number,
13 Point3<N>: VertexFormat<N>,
14 F: 'static + Landmark<Number = N>,
15 W: 'static + World<Landmark = F>,
16{
17 points: PointsRendener<N, W>,
18 lines: LinesRendener<N, W>,
19}
20
21impl<N, F, W> PipelineBuilder for W
22where
23 N: 'static + Number,
24 Point3<N>: VertexFormat<N>,
25 F: 'static + Landmark<Number = N>,
26 W: 'static + World<Landmark = F> + Clone,
27{
28 fn build(
29 self: Box<Self>,
30 device: &wgpu::Device,
31 texture_format: wgpu::TextureFormat,
32 uniform_bind_group_layout: &wgpu::BindGroupLayout,
33 ) -> Box<dyn PipelineRenderer> {
34 let world = *self;
35 Box::new(WorldRenderer {
36 points: PointsBuilder::new(world.clone()).build(
37 device,
38 texture_format,
39 uniform_bind_group_layout,
40 ),
41 lines: LinesBuilder::new(world).build(
42 device,
43 texture_format,
44 uniform_bind_group_layout,
45 ),
46 })
47 }
48}
49
50impl<N, F, W> PipelineRenderer for WorldRenderer<N, F, W>
51where
52 N: 'static + Number,
53 Point3<N>: VertexFormat<N>,
54 F: 'static + Landmark<Number = N>,
55 W: 'static + World<Landmark = F>,
56{
57 fn render<'a>(&'a mut self, device: &wgpu::Device, render_pass: &mut wgpu::RenderPass<'a>) {
58 self.points.render(device, render_pass);
59 self.lines.render(device, render_pass);
60 }
61}
62
63impl<N, F, W> PointSource<N> for W
64where
65 N: 'static + Number,
66 Point3<N>: VertexFormat<N>,
67 F: 'static + Landmark<Number = N>,
68 W: 'static + World<Landmark = F>,
69{
70 fn collect_visual_points(&self) -> Vec<Point<N>> {
71 self.collect_landmarks(|lm| Point {
72 position: lm.point_world(),
73 color: Colors::red(),
74 })
75 }
76}
77
78impl<N, F, W> LineSource<N> for W
79where
80 N: 'static + Number,
81 Point3<N>: VertexFormat<N>,
82 F: 'static + Landmark<Number = N>,
83 W: 'static + World<Landmark = F>,
84{
85 fn collect_visual_lines(&self) -> Vec<Line<N>> {
86 self.collect_landmarks(|lm| Line {
87 start: Point {
88 position: lm.point_world(),
89 color: Colors::red(),
90 },
91 end: Point::default(),
92 })
93 }
94}
95
96impl<F, W> WindowBuilderDefault<f32> for W
97where
98 F: 'static + Landmark<Number = f32>,
99 W: 'static + World<Landmark = F>,
100{
101 fn default_window() -> WindowBuilder<f32> {
102 WindowBuilder {
103 title: Some("Map Viewer".to_string()),
104 framerate: Some(120),
105
106 camera: CameraFrustum {
107 eye: Point3::new(0., 2., 5.),
108 at: Point3::new(0., 0., 0.),
109
110 fovy: std::f32::consts::FRAC_PI_4,
111 znear: 0.1,
112 zfar: 100.0,
113 },
114 camera_controller: CameraControllerConfig {
115 mouse_left_speed: 1.0,
116 mouse_right_speed: 5.0,
117 scroll_speed: 1.0,
118 keyboard_speed: 0.1,
119 },
120 }
121 }
122}