gistools/readers/las/
util.rs1pub fn u8_fold(n: u32) -> u8 {
9 if n > 255 { (n - 256) as u8 } else { n as u8 }
10}
11
12pub fn u8_clamp(n: u32) -> u8 {
20 if n > 255 { 255 } else { n as u8 }
21}
22
23pub fn i8_clamp(n: i32) -> i8 {
31 if n <= -128 {
32 -128
33 } else if n >= 127 {
34 127
35 } else {
36 n as i8
37 }
38}
39
40pub fn u32_zero_bit0(n: u32) -> u32 {
48 n & 0xfffffffe
49}
50
51pub fn i16_quantize(n: f64) -> i16 {
59 if (n) >= 0. { ((n) + 0.5) as i16 } else { ((n) - 0.5) as i16 }
60}
61
62#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
65pub struct U64I64F64 {
66 bytes: [u8; 8],
67}
68
69impl U64I64F64 {
70 pub fn new(value: impl IntoValue64, ty: ValueType64) -> Self {
72 let mut out = Self { bytes: [0; 8] };
73 out.set(value, ty);
74 out
75 }
76 pub fn set(&mut self, value: impl IntoValue64, ty: ValueType64) {
78 match ty {
79 ValueType64::U64 => self.bytes = value.into_u64().to_le_bytes(),
80 ValueType64::I64 => self.bytes = value.into_i64().to_le_bytes(),
81 ValueType64::F64 => self.bytes = value.into_f64().to_le_bytes(),
82 }
83 }
84 pub fn u64(&self) -> u64 {
86 u64::from_le_bytes(self.bytes)
87 }
88 pub fn set_u64(&mut self, value: u64) {
90 self.bytes = value.to_le_bytes();
91 }
92 pub fn i64(&self) -> i64 {
94 i64::from_le_bytes(self.bytes)
95 }
96 pub fn set_i64(&mut self, value: i64) {
98 self.bytes = value.to_le_bytes();
99 }
100 pub fn f64(&self) -> f64 {
102 f64::from_le_bytes(self.bytes)
103 }
104 pub fn set_f64(&mut self, value: f64) {
106 self.bytes = value.to_le_bytes();
107 }
108}
109
110#[derive(Debug, Clone, Copy, PartialEq, Eq)]
113pub enum ValueType64 {
114 U64,
116 I64,
118 F64,
120}
121pub trait IntoValue64 {
123 fn into_u64(self) -> u64;
125 fn into_i64(self) -> i64;
127 fn into_f64(self) -> f64;
129}
130impl IntoValue64 for u64 {
131 fn into_u64(self) -> u64 {
132 self
133 }
134 fn into_i64(self) -> i64 {
135 self as i64
136 }
137 fn into_f64(self) -> f64 {
138 self as f64
139 }
140}
141impl IntoValue64 for i64 {
142 fn into_u64(self) -> u64 {
143 self as u64
144 }
145 fn into_i64(self) -> i64 {
146 self
147 }
148 fn into_f64(self) -> f64 {
149 self as f64
150 }
151}
152impl IntoValue64 for f64 {
153 fn into_u64(self) -> u64 {
154 self.to_bits()
155 }
156 fn into_i64(self) -> i64 {
157 self.to_bits() as i64
158 }
159 fn into_f64(self) -> f64 {
160 self
161 }
162}
163
164#[derive(Debug, Clone, Copy, PartialEq, Eq)]
167pub struct U32I32F32 {
168 bytes: [u8; 4],
169}
170impl U32I32F32 {
171 pub fn new(value: impl IntoValue32, ty: ValueType32) -> Self {
173 let mut out = Self { bytes: [0; 4] };
174 out.set(value, ty);
175 out
176 }
177 pub fn set(&mut self, value: impl IntoValue32, ty: ValueType32) {
179 match ty {
180 ValueType32::U32 => self.bytes = value.into_u32().to_le_bytes(),
181 ValueType32::I32 => self.bytes = value.into_i32().to_le_bytes(),
182 ValueType32::F32 => self.bytes = value.into_f32().to_le_bytes(),
183 }
184 }
185 pub fn u32(&self) -> u32 {
187 u32::from_le_bytes(self.bytes)
188 }
189 pub fn set_u32(&mut self, value: u32) {
191 self.bytes = value.to_le_bytes();
192 }
193 pub fn i32(&self) -> i32 {
195 i32::from_le_bytes(self.bytes)
196 }
197 pub fn set_i32(&mut self, value: i32) {
199 self.bytes = value.to_le_bytes();
200 }
201 pub fn f32(&self) -> f32 {
203 f32::from_le_bytes(self.bytes)
204 }
205 pub fn set_f32(&mut self, value: f32) {
207 self.bytes = value.to_le_bytes();
208 }
209}
210
211#[derive(Debug, Clone, Copy, PartialEq, Eq)]
213pub enum ValueType32 {
214 U32,
216 I32,
218 F32,
220}
221pub trait IntoValue32 {
223 fn into_u32(self) -> u32;
225 fn into_i32(self) -> i32;
227 fn into_f32(self) -> f32;
229}
230
231impl IntoValue32 for u32 {
232 fn into_u32(self) -> u32 {
233 self
234 }
235 fn into_i32(self) -> i32 {
236 self as i32
237 }
238 fn into_f32(self) -> f32 {
239 self as f32
240 }
241}
242impl IntoValue32 for i32 {
243 fn into_u32(self) -> u32 {
244 self as u32
245 }
246 fn into_i32(self) -> i32 {
247 self
248 }
249 fn into_f32(self) -> f32 {
250 self as f32
251 }
252}
253impl IntoValue32 for f32 {
254 fn into_u32(self) -> u32 {
255 self.to_bits()
256 }
257 fn into_i32(self) -> i32 {
258 self.to_bits() as i32
259 }
260 fn into_f32(self) -> f32 {
261 self
262 }
263}