dear_implot3d/plots/
image.rs

1use super::{Plot3D, Plot3DError};
2use crate::{Image3DFlags, Plot3DUi};
3use dear_imgui_rs::texture::TextureRef;
4
5pub struct Image3DByAxes<'a, T: Into<TextureRef> + Copy> {
6    pub label: &'a str,
7    pub tex: T,
8    pub center: [f32; 3],
9    pub axis_u: [f32; 3],
10    pub axis_v: [f32; 3],
11    pub uv0: [f32; 2],
12    pub uv1: [f32; 2],
13    pub tint: [f32; 4],
14    pub flags: Image3DFlags,
15}
16
17impl<'a, T: Into<TextureRef> + Copy> Image3DByAxes<'a, T> {
18    pub fn new(
19        label: &'a str,
20        tex: T,
21        center: [f32; 3],
22        axis_u: [f32; 3],
23        axis_v: [f32; 3],
24    ) -> Self {
25        Self {
26            label,
27            tex,
28            center,
29            axis_u,
30            axis_v,
31            uv0: [0.0, 0.0],
32            uv1: [1.0, 1.0],
33            tint: [1.0, 1.0, 1.0, 1.0],
34            flags: Image3DFlags::NONE,
35        }
36    }
37    pub fn uv(mut self, uv0: [f32; 2], uv1: [f32; 2]) -> Self {
38        self.uv0 = uv0;
39        self.uv1 = uv1;
40        self
41    }
42    pub fn tint(mut self, col: [f32; 4]) -> Self {
43        self.tint = col;
44        self
45    }
46    pub fn flags(mut self, flags: Image3DFlags) -> Self {
47        self.flags = flags;
48        self
49    }
50    pub fn alpha(mut self, a: f32) -> Self {
51        self.tint[3] = a;
52        self
53    }
54    pub fn uv_rect(mut self, u0: f32, v0: f32, u1: f32, v1: f32) -> Self {
55        self.uv0 = [u0, v0];
56        self.uv1 = [u1, v1];
57        self
58    }
59    pub fn flip_v(mut self) -> Self {
60        let (u0, v0) = (self.uv0[0], self.uv0[1]);
61        let (u1, v1) = (self.uv1[0], self.uv1[1]);
62        self.uv0 = [u0, v1];
63        self.uv1 = [u1, v0];
64        self
65    }
66}
67
68impl<'a, T: Into<TextureRef> + Copy> Plot3D for Image3DByAxes<'a, T> {
69    fn label(&self) -> &str {
70        self.label
71    }
72    fn try_plot(&self, ui: &Plot3DUi<'_>) -> Result<(), Plot3DError> {
73        ui.image_by_axes(self.label, self.tex, self.center, self.axis_u, self.axis_v)
74            .uv(self.uv0, self.uv1)
75            .tint(self.tint)
76            .flags(self.flags)
77            .plot();
78        Ok(())
79    }
80}
81
82pub struct Image3DByCorners<'a, T: Into<TextureRef> + Copy> {
83    pub label: &'a str,
84    pub tex: T,
85    pub p0: [f32; 3],
86    pub p1: [f32; 3],
87    pub p2: [f32; 3],
88    pub p3: [f32; 3],
89    pub uv0: [f32; 2],
90    pub uv1: [f32; 2],
91    pub uv2: [f32; 2],
92    pub uv3: [f32; 2],
93    pub tint: [f32; 4],
94    pub flags: Image3DFlags,
95}
96
97impl<'a, T: Into<TextureRef> + Copy> Image3DByCorners<'a, T> {
98    pub fn new(
99        label: &'a str,
100        tex: T,
101        p0: [f32; 3],
102        p1: [f32; 3],
103        p2: [f32; 3],
104        p3: [f32; 3],
105    ) -> Self {
106        Self {
107            label,
108            tex,
109            p0,
110            p1,
111            p2,
112            p3,
113            uv0: [0.0, 0.0],
114            uv1: [1.0, 0.0],
115            uv2: [1.0, 1.0],
116            uv3: [0.0, 1.0],
117            tint: [1.0, 1.0, 1.0, 1.0],
118            flags: Image3DFlags::NONE,
119        }
120    }
121    pub fn uvs(mut self, uv0: [f32; 2], uv1: [f32; 2], uv2: [f32; 2], uv3: [f32; 2]) -> Self {
122        self.uv0 = uv0;
123        self.uv1 = uv1;
124        self.uv2 = uv2;
125        self.uv3 = uv3;
126        self
127    }
128    pub fn tint(mut self, col: [f32; 4]) -> Self {
129        self.tint = col;
130        self
131    }
132    pub fn flags(mut self, flags: Image3DFlags) -> Self {
133        self.flags = flags;
134        self
135    }
136    pub fn alpha(mut self, a: f32) -> Self {
137        self.tint[3] = a;
138        self
139    }
140    pub fn flip_v(mut self) -> Self {
141        std::mem::swap(&mut self.uv0, &mut self.uv3);
142        std::mem::swap(&mut self.uv1, &mut self.uv2);
143        self
144    }
145}
146
147impl<'a, T: Into<TextureRef> + Copy> Plot3D for Image3DByCorners<'a, T> {
148    fn label(&self) -> &str {
149        self.label
150    }
151    fn try_plot(&self, ui: &Plot3DUi<'_>) -> Result<(), Plot3DError> {
152        ui.image_by_corners(self.label, self.tex, self.p0, self.p1, self.p2, self.p3)
153            .uvs(self.uv0, self.uv1, self.uv2, self.uv3)
154            .tint(self.tint)
155            .flags(self.flags)
156            .plot();
157        Ok(())
158    }
159}