1use json::camera::CameraType;
2
3use crate::Document;
4
5#[derive(Clone, Debug)]
7pub enum Projection<'a> {
8 Orthographic(Orthographic<'a>),
10
11 Perspective(Perspective<'a>),
13}
14
15#[derive(Clone, Debug)]
17pub struct Orthographic<'a> {
18 #[allow(dead_code)]
20 document: &'a Document,
21
22 json: &'a json::camera::Orthographic,
24}
25
26#[derive(Clone, Debug)]
28pub struct Perspective<'a> {
29 #[allow(dead_code)]
31 document: &'a Document,
32
33 json: &'a json::camera::Perspective,
35}
36
37#[derive(Clone, Debug)]
38pub struct Camera<'a> {
39 #[allow(dead_code)]
41 document: &'a Document,
42
43 index: &'a String,
45
46 json: &'a json::Camera,
48}
49
50impl<'a> Camera<'a> {
51 pub(crate) fn new(document: &'a Document, index: &'a String, json: &'a json::Camera) -> Self {
53 Self {
54 document,
55 index,
56 json,
57 }
58 }
59
60 pub fn index(&self) -> &str {
62 self.index
63 }
64 pub fn name(&self) -> Option<&'a str> {
65 self.json.name.as_deref()
66 }
67
68 pub fn projection(&self) -> Projection {
70 match self.json.type_.unwrap() {
71 CameraType::Orthographic => {
72 let json = self.json.orthographic.as_ref().unwrap();
73 Projection::Orthographic(Orthographic::new(self.document, json))
74 }
75 CameraType::Perspective => {
76 let json = self.json.perspective.as_ref().unwrap();
77 Projection::Perspective(Perspective::new(self.document, json))
78 }
79 }
80 }
81}
82impl<'a> Orthographic<'a> {
83 pub(crate) fn new(document: &'a Document, json: &'a json::camera::Orthographic) -> Self {
85 Self { document, json }
86 }
87
88 pub fn xmag(&self) -> f32 {
90 self.json.xmag
91 }
92
93 pub fn ymag(&self) -> f32 {
95 self.json.ymag
96 }
97
98 pub fn zfar(&self) -> f32 {
100 self.json.zfar
101 }
102
103 pub fn znear(&self) -> f32 {
105 self.json.znear
106 }
107}
108
109impl<'a> Perspective<'a> {
110 pub(crate) fn new(document: &'a Document, json: &'a json::camera::Perspective) -> Self {
112 Self { document, json }
113 }
114
115 pub fn aspect_ratio(&self) -> Option<f32> {
117 self.json.aspect_ratio
118 }
119
120 pub fn yfov(&self) -> f32 {
122 self.json.yfov
123 }
124
125 pub fn zfar(&self) -> f32 {
127 self.json.zfar
128 }
129
130 pub fn znear(&self) -> f32 {
132 self.json.znear
133 }
134}
135
136#[derive(Clone, Debug)]
138pub struct Cameras<'a> {
139 pub(crate) iter: indexmap::map::Iter<'a, String, gltf_v1_json::Camera>,
141
142 pub(crate) document: &'a Document,
144}
145
146impl ExactSizeIterator for Cameras<'_> {}
147impl<'a> Iterator for Cameras<'a> {
148 type Item = Camera<'a>;
149 fn next(&mut self) -> Option<Self::Item> {
150 self.iter
151 .next()
152 .map(|(index, json)| Camera::new(self.document, index, json))
153 }
154 fn size_hint(&self) -> (usize, Option<usize>) {
155 self.iter.size_hint()
156 }
157 fn count(self) -> usize {
158 self.iter.count()
159 }
160 fn last(self) -> Option<Self::Item> {
161 let document = self.document;
162 self.iter
163 .last()
164 .map(|(index, json)| Camera::new(document, index, json))
165 }
166 fn nth(&mut self, n: usize) -> Option<Self::Item> {
167 self.iter
168 .nth(n)
169 .map(|(index, json)| Camera::new(self.document, index, json))
170 }
171}