token_value_map/
data_types.rs

1use crate::{DataTypeOps, *};
2use anyhow::Result;
3use std::{
4    fmt::Debug,
5    hash::{Hash, Hasher},
6    ops::{Add, Div, Mul, Sub},
7};
8
9// Macro to implement DataTypeOps for each variant
10macro_rules! impl_data_ops {
11    ($type:ty, $name:expr, $data_type:expr) => {
12        impl DataTypeOps for $type {
13            fn type_name(&self) -> &'static str {
14                $name
15            }
16
17            fn data_type(&self) -> DataType {
18                $data_type
19            }
20        }
21    };
22}
23
24// AIDEV-NOTE: Macro to reduce duplication for nalgebra-based types.
25// Implements Add, Sub, Mul<f32>, and Mul<f64> for wrapper types.
26macro_rules! impl_nalgebra_arithmetic {
27    // For f32-based types
28    ($type:ty) => {
29        impl Add for $type {
30            type Output = $type;
31
32            fn add(self, other: $type) -> $type {
33                Self(self.0 + other.0)
34            }
35        }
36
37        impl Sub for $type {
38            type Output = $type;
39
40            fn sub(self, other: $type) -> $type {
41                Self(self.0 - other.0)
42            }
43        }
44
45        impl Mul<f32> for $type {
46            type Output = $type;
47
48            fn mul(self, scalar: f32) -> $type {
49                Self(self.0 * scalar)
50            }
51        }
52
53        impl Mul<f64> for $type {
54            type Output = $type;
55
56            fn mul(self, scalar: f64) -> $type {
57                Self(self.0 * scalar as f32)
58            }
59        }
60    };
61
62    // For f64-based types
63    ($type:ty, f64) => {
64        impl Add for $type {
65            type Output = $type;
66
67            fn add(self, other: $type) -> $type {
68                Self(self.0 + other.0)
69            }
70        }
71
72        impl Sub for $type {
73            type Output = $type;
74
75            fn sub(self, other: $type) -> $type {
76                Self(self.0 - other.0)
77            }
78        }
79
80        impl Mul<f32> for $type {
81            type Output = $type;
82
83            fn mul(self, scalar: f32) -> $type {
84                Self(self.0 * scalar as f64)
85            }
86        }
87
88        impl Mul<f64> for $type {
89            type Output = $type;
90
91            fn mul(self, scalar: f64) -> $type {
92                Self(self.0 * scalar)
93            }
94        }
95    };
96}
97
98/// A boolean value wrapper.
99#[derive(Clone, Debug, PartialEq, Eq, Hash)]
100#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
101pub struct Boolean(pub bool);
102
103impl From<Data> for Boolean {
104    fn from(data: Data) -> Self {
105        match data {
106            Data::Boolean(b) => b,
107            Data::Real(r) => Boolean(r.0 != 0.0),
108            Data::Integer(i) => Boolean(i.0 != 0),
109            Data::String(s) => Boolean(s.0.parse::<bool>().unwrap_or(false)),
110            _ => panic!("Cannot convert {data:?} to Boolean"),
111        }
112    }
113}
114
115/// A 64-bit signed integer wrapper.
116#[derive(Clone, Debug, PartialEq, Eq, Hash)]
117#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
118pub struct Integer(pub i64);
119
120impl From<Data> for Integer {
121    fn from(data: Data) -> Self {
122        match data {
123            Data::Boolean(b) => Integer(if b.0 { 1 } else { 0 }),
124            Data::Real(r) => Integer(r.0 as i64),
125            Data::Integer(i) => i,
126            Data::String(s) => Integer(s.0.parse::<i64>().unwrap_or(0)),
127            _ => panic!("Cannot convert {data:?} to Integer"),
128        }
129    }
130}
131
132/// A 64-bit floating-point number wrapper.
133#[derive(Clone, Debug, PartialEq)]
134#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
135pub struct Real(pub f64);
136
137impl Eq for Real {}
138
139impl From<Data> for Real {
140    fn from(data: Data) -> Self {
141        match data {
142            Data::Boolean(b) => Real(if b.0 { 1.0 } else { 0.0 }),
143            Data::Real(r) => r,
144            Data::Integer(i) => Real(i.0 as f64),
145            Data::String(s) => Real(s.0.parse::<f64>().unwrap_or(0.0)),
146            _ => panic!("Cannot convert {data:?} to Real"),
147        }
148    }
149}
150
151impl From<f64> for Real {
152    fn from(value: f64) -> Self {
153        Real(value)
154    }
155}
156
157impl From<f32> for Real {
158    fn from(value: f32) -> Self {
159        Real(value as f64)
160    }
161}
162
163/// A UTF-8 string wrapper.
164#[derive(Clone, Debug, PartialEq, Eq, Hash)]
165#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
166pub struct String(pub std::string::String);
167
168impl From<Data> for String {
169    fn from(data: Data) -> Self {
170        match data {
171            Data::Boolean(b) => String(b.0.to_string()),
172            Data::Real(r) => String(r.0.to_string()),
173            Data::Integer(i) => String(i.0.to_string()),
174            Data::String(s) => s,
175            Data::Color(c) => String(format!("{c:?}")),
176            #[cfg(feature = "vector2")]
177            Data::Vector2(v) => String(format!("{v:?}")),
178            #[cfg(feature = "vector3")]
179            Data::Vector3(v) => String(format!("{v:?}")),
180            #[cfg(feature = "matrix3")]
181            Data::Matrix3(m) => String(format!("{m:?}")),
182            #[cfg(feature = "normal3")]
183            Data::Normal3(n) => String(format!("{n:?}")),
184            #[cfg(feature = "point3")]
185            Data::Point3(p) => String(format!("{p:?}")),
186            #[cfg(feature = "matrix4")]
187            Data::Matrix4(m) => String(format!("{m:?}")),
188            Data::BooleanVec(v) => String(format!("{v:?}")),
189            Data::RealVec(v) => String(format!("{v:?}")),
190            Data::IntegerVec(v) => String(format!("{v:?}")),
191            Data::StringVec(v) => String(format!("{v:?}")),
192            Data::ColorVec(v) => String(format!("{v:?}")),
193            #[cfg(all(feature = "vector2", feature = "vec_variants"))]
194            Data::Vector2Vec(v) => String(format!("{v:?}")),
195            #[cfg(all(feature = "vector3", feature = "vec_variants"))]
196            Data::Vector3Vec(v) => String(format!("{v:?}")),
197            #[cfg(all(feature = "matrix3", feature = "vec_variants"))]
198            Data::Matrix3Vec(v) => String(format!("{v:?}")),
199            #[cfg(all(feature = "normal3", feature = "vec_variants"))]
200            Data::Normal3Vec(v) => String(format!("{v:?}")),
201            #[cfg(all(feature = "point3", feature = "vec_variants"))]
202            Data::Point3Vec(v) => String(format!("{v:?}")),
203            #[cfg(all(feature = "matrix4", feature = "vec_variants"))]
204            Data::Matrix4Vec(v) => String(format!("{v:?}")),
205        }
206    }
207}
208
209/// A 4-component RGBA color value.
210#[derive(Clone, Debug, PartialEq)]
211#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
212pub struct Color(pub [f32; 4]);
213
214impl Eq for Color {}
215
216impl From<[f32; 4]> for Color {
217    fn from(array: [f32; 4]) -> Self {
218        Color(array)
219    }
220}
221
222impl From<Data> for Color {
223    fn from(data: Data) -> Self {
224        match data {
225            Data::Boolean(b) => Color([b.0.into(), b.0.into(), b.0.into(), 1.0]),
226            Data::Real(r) => Color([r.0 as _, r.0 as _, r.0 as _, 1.0]),
227            Data::Integer(i) => Color([i.0 as _, i.0 as _, i.0 as _, 1.0]),
228            Data::String(s) => Color([s.0.parse::<f32>().unwrap_or(0.0); 4]),
229            Data::Color(c) => c,
230            #[cfg(feature = "vector2")]
231            Data::Vector2(v) => Color([v.0.x, v.0.y, 0.0, 1.0]),
232            #[cfg(feature = "vector3")]
233            Data::Vector3(v) => Color([v.0.x, v.0.y, v.0.z, 1.0]),
234            Data::BooleanVec(v) => Color([v.0[0].into(), v.0[1].into(), v.0[2].into(), 1.0]),
235            Data::RealVec(v) => Color([v.0[0] as _, v.0[1] as _, v.0[2] as _, 1.0]),
236            Data::IntegerVec(v) => Color([v.0[0] as _, v.0[1] as _, v.0[2] as _, 1.0]),
237            Data::StringVec(v) => Color([v.0[0].parse::<f32>().unwrap_or(0.0); 4]),
238            Data::ColorVec(v) => Color([v.0[0][0], v.0[0][1], v.0[0][2], v.0[0][3]]),
239            _ => panic!("Cannot convert {data:?} to Color"),
240        }
241    }
242}
243
244/// A 2D vector.
245#[cfg(feature = "vector2")]
246#[derive(Clone, Debug, PartialEq)]
247#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
248pub struct Vector2(pub nalgebra::Vector2<f32>);
249
250#[cfg(feature = "vector2")]
251impl Eq for Vector2 {}
252
253/// A 3D vector.
254#[cfg(feature = "vector3")]
255#[derive(Clone, Debug, PartialEq)]
256#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
257pub struct Vector3(pub nalgebra::Vector3<f32>);
258
259#[cfg(feature = "vector3")]
260impl Eq for Vector3 {}
261
262/// A 3×3 transformation matrix.
263#[cfg(feature = "matrix3")]
264#[derive(Clone, Debug, PartialEq)]
265#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
266pub struct Matrix3(pub nalgebra::Matrix3<f32>);
267
268#[cfg(feature = "matrix3")]
269impl Eq for Matrix3 {}
270
271#[cfg(feature = "matrix3")]
272impl From<Vec<f32>> for Matrix3 {
273    fn from(vec: Vec<f32>) -> Self {
274        assert_eq!(vec.len(), 9, "Matrix3 requires exactly 9 elements");
275        Matrix3(nalgebra::Matrix3::from_row_slice(&vec))
276    }
277}
278
279#[cfg(feature = "matrix3")]
280impl From<Vec<f64>> for Matrix3 {
281    fn from(vec: Vec<f64>) -> Self {
282        assert_eq!(vec.len(), 9, "Matrix3 requires exactly 9 elements");
283        let vec_f32: Vec<f32> = vec.into_iter().map(|v| v as f32).collect();
284        Matrix3(nalgebra::Matrix3::from_row_slice(&vec_f32))
285    }
286}
287
288#[cfg(feature = "matrix3")]
289impl From<[f32; 9]> for Matrix3 {
290    fn from(arr: [f32; 9]) -> Self {
291        Matrix3(nalgebra::Matrix3::from_row_slice(&arr))
292    }
293}
294
295/// A 3D normal vector.
296#[cfg(feature = "normal3")]
297#[derive(Clone, Debug, PartialEq)]
298#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
299pub struct Normal3(pub nalgebra::Vector3<f32>);
300
301#[cfg(feature = "normal3")]
302impl Eq for Normal3 {}
303
304/// A 3D point.
305#[cfg(feature = "point3")]
306#[derive(Clone, Debug, PartialEq)]
307#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
308pub struct Point3(pub nalgebra::Point3<f32>);
309
310#[cfg(feature = "point3")]
311impl Eq for Point3 {}
312
313/// A 4×4 transformation matrix.
314#[cfg(feature = "matrix4")]
315#[derive(Clone, Debug, PartialEq)]
316#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
317pub struct Matrix4(pub nalgebra::Matrix4<f64>);
318
319#[cfg(feature = "matrix4")]
320impl Eq for Matrix4 {}
321
322/// A vector of integer values.
323#[derive(Clone, Debug, PartialEq, Eq, Hash)]
324#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
325pub struct IntegerVec(pub Vec<i64>);
326
327impl IntegerVec {
328    pub fn new(vec: Vec<i64>) -> Result<Self> {
329        if vec.is_empty() {
330            return Err(anyhow!("IntegerVec cannot be empty"));
331        }
332        Ok(IntegerVec(vec))
333    }
334}
335
336impl From<Vec<i64>> for IntegerVec {
337    fn from(vec: Vec<i64>) -> Self {
338        IntegerVec(vec)
339    }
340}
341
342impl From<Vec<i32>> for IntegerVec {
343    fn from(vec: Vec<i32>) -> Self {
344        IntegerVec(vec.into_iter().map(|v| v as i64).collect())
345    }
346}
347
348/// A vector of real values.
349#[derive(Clone, Debug, PartialEq)]
350#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
351pub struct RealVec(pub Vec<f64>);
352
353impl RealVec {
354    pub fn new(vec: Vec<f64>) -> Result<Self> {
355        if vec.is_empty() {
356            return Err(anyhow!("RealVec cannot be empty"));
357        }
358        Ok(RealVec(vec))
359    }
360}
361
362impl From<Vec<f64>> for RealVec {
363    fn from(vec: Vec<f64>) -> Self {
364        RealVec(vec)
365    }
366}
367
368impl From<Vec<f32>> for RealVec {
369    fn from(vec: Vec<f32>) -> Self {
370        RealVec(vec.into_iter().map(|v| v as f64).collect())
371    }
372}
373
374impl Eq for RealVec {}
375
376/// A vector of boolean values.
377#[derive(Clone, Debug, PartialEq, Eq, Hash)]
378#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
379pub struct BooleanVec(pub Vec<bool>);
380
381impl BooleanVec {
382    pub fn new(vec: Vec<bool>) -> Result<Self> {
383        if vec.is_empty() {
384            return Err(anyhow!("BooleanVec cannot be empty"));
385        }
386        Ok(BooleanVec(vec))
387    }
388}
389
390/// A vector of string values.
391#[derive(Clone, Debug, PartialEq, Eq, Hash)]
392#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
393pub struct StringVec(pub Vec<std::string::String>);
394
395impl StringVec {
396    pub fn new(vec: Vec<std::string::String>) -> Result<Self> {
397        if vec.is_empty() {
398            return Err(anyhow!("StringVec cannot be empty"));
399        }
400        Ok(StringVec(vec))
401    }
402}
403
404/// A vector of color values.
405#[derive(Clone, Debug, PartialEq)]
406#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
407pub struct ColorVec(pub Vec<[f32; 4]>);
408
409impl ColorVec {
410    pub fn new(vec: Vec<[f32; 4]>) -> Result<Self> {
411        if vec.is_empty() {
412            return Err(anyhow!("ColorVec cannot be empty"));
413        }
414        Ok(ColorVec(vec))
415    }
416}
417
418impl Eq for ColorVec {}
419
420/// A vector of 2D vectors.
421#[cfg(all(feature = "vector2", feature = "vec_variants"))]
422#[derive(Clone, Debug, PartialEq)]
423#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
424pub struct Vector2Vec(pub Vec<nalgebra::Vector2<f32>>);
425
426#[cfg(all(feature = "vector2", feature = "vec_variants"))]
427impl Vector2Vec {
428    pub fn new(vec: Vec<nalgebra::Vector2<f32>>) -> Result<Self> {
429        if vec.is_empty() {
430            return Err(anyhow!("Vector2Vec cannot be empty"));
431        }
432        Ok(Vector2Vec(vec))
433    }
434}
435
436#[cfg(all(feature = "vector2", feature = "vec_variants"))]
437impl Eq for Vector2Vec {}
438
439/// A vector of 3D vectors.
440#[cfg(all(feature = "vector3", feature = "vec_variants"))]
441#[derive(Clone, Debug, PartialEq)]
442#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
443pub struct Vector3Vec(pub Vec<nalgebra::Vector3<f32>>);
444
445#[cfg(all(feature = "vector3", feature = "vec_variants"))]
446impl Vector3Vec {
447    pub fn new(vec: Vec<nalgebra::Vector3<f32>>) -> Result<Self> {
448        if vec.is_empty() {
449            return Err(anyhow!("Vector3Vec cannot be empty"));
450        }
451        Ok(Vector3Vec(vec))
452    }
453}
454
455#[cfg(all(feature = "vector3", feature = "vec_variants"))]
456impl Eq for Vector3Vec {}
457
458/// A vector of transformation matrices.
459#[cfg(all(feature = "matrix3", feature = "vec_variants"))]
460#[derive(Clone, Debug, PartialEq)]
461#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
462pub struct Matrix3Vec(pub Vec<nalgebra::Matrix3<f32>>);
463
464#[cfg(all(feature = "matrix3", feature = "vec_variants"))]
465impl Matrix3Vec {
466    pub fn new(vec: Vec<nalgebra::Matrix3<f32>>) -> Result<Self> {
467        if vec.is_empty() {
468            return Err(anyhow!("Matrix3Vec cannot be empty"));
469        }
470        Ok(Matrix3Vec(vec))
471    }
472}
473
474#[cfg(all(feature = "matrix3", feature = "vec_variants"))]
475impl Eq for Matrix3Vec {}
476
477/// A vector of 3D normals.
478#[cfg(all(feature = "normal3", feature = "vec_variants"))]
479#[derive(Clone, Debug, PartialEq)]
480#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
481pub struct Normal3Vec(pub Vec<nalgebra::Vector3<f32>>);
482
483#[cfg(all(feature = "normal3", feature = "vec_variants"))]
484impl Normal3Vec {
485    pub fn new(vec: Vec<nalgebra::Vector3<f32>>) -> Result<Self> {
486        if vec.is_empty() {
487            return Err(anyhow!("Normal3Vec cannot be empty"));
488        }
489        Ok(Normal3Vec(vec))
490    }
491}
492
493#[cfg(all(feature = "normal3", feature = "vec_variants"))]
494impl Eq for Normal3Vec {}
495
496/// A vector of 3D points.
497#[cfg(all(feature = "point3", feature = "vec_variants"))]
498#[derive(Clone, Debug, PartialEq)]
499#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
500pub struct Point3Vec(pub Vec<nalgebra::Point3<f32>>);
501
502#[cfg(all(feature = "point3", feature = "vec_variants"))]
503impl Point3Vec {
504    pub fn new(vec: Vec<nalgebra::Point3<f32>>) -> Result<Self> {
505        if vec.is_empty() {
506            return Err(anyhow!("Point3Vec cannot be empty"));
507        }
508        Ok(Point3Vec(vec))
509    }
510}
511
512#[cfg(all(feature = "point3", feature = "vec_variants"))]
513impl Eq for Point3Vec {}
514
515/// A vector of 4×4 transformation matrices.
516#[cfg(all(feature = "matrix4", feature = "vec_variants"))]
517#[derive(Clone, Debug, PartialEq)]
518#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
519pub struct Matrix4Vec(pub Vec<nalgebra::Matrix4<f64>>);
520
521#[cfg(all(feature = "matrix4", feature = "vec_variants"))]
522impl Matrix4Vec {
523    pub fn new(vec: Vec<nalgebra::Matrix4<f64>>) -> Result<Self> {
524        if vec.is_empty() {
525            return Err(anyhow!("Matrix4Vec cannot be empty"));
526        }
527        Ok(Matrix4Vec(vec))
528    }
529}
530
531#[cfg(all(feature = "matrix4", feature = "vec_variants"))]
532impl Eq for Matrix4Vec {}
533
534// Arithmetic operations for new types
535#[cfg(feature = "normal3")]
536impl_nalgebra_arithmetic!(Normal3);
537
538#[cfg(feature = "point3")]
539impl Add for Point3 {
540    type Output = Point3;
541
542    fn add(self, other: Point3) -> Point3 {
543        Point3(self.0 + other.0.coords)
544    }
545}
546
547#[cfg(feature = "point3")]
548impl Sub for Point3 {
549    type Output = Point3;
550
551    fn sub(self, other: Point3) -> Point3 {
552        Point3(nalgebra::Point3::from(self.0.coords - other.0.coords))
553    }
554}
555
556#[cfg(feature = "point3")]
557impl Mul<f32> for Point3 {
558    type Output = Point3;
559
560    fn mul(self, scalar: f32) -> Point3 {
561        Point3(self.0 * scalar)
562    }
563}
564
565#[cfg(feature = "point3")]
566impl Mul<f64> for Point3 {
567    type Output = Point3;
568
569    fn mul(self, scalar: f64) -> Point3 {
570        Point3(self.0 * scalar as f32)
571    }
572}
573
574#[cfg(feature = "matrix4")]
575impl_nalgebra_arithmetic!(Matrix4, f64);
576
577// Arithmetic trait implementations for interpolation
578impl Add for Real {
579    type Output = Real;
580
581    fn add(self, other: Real) -> Real {
582        Real(self.0 + other.0)
583    }
584}
585
586impl Sub for Real {
587    type Output = Real;
588
589    fn sub(self, other: Real) -> Real {
590        Real(self.0 - other.0)
591    }
592}
593
594impl Mul<f32> for Real {
595    type Output = Real;
596
597    fn mul(self, scalar: f32) -> Real {
598        Real(self.0 * scalar as f64)
599    }
600}
601
602impl Mul<f64> for Real {
603    type Output = Real;
604
605    fn mul(self, scalar: f64) -> Real {
606        Real(self.0 * scalar)
607    }
608}
609
610impl Add for Integer {
611    type Output = Integer;
612
613    fn add(self, other: Integer) -> Integer {
614        Integer(self.0 + other.0)
615    }
616}
617
618impl Sub for Integer {
619    type Output = Integer;
620
621    fn sub(self, other: Integer) -> Integer {
622        Integer(self.0 - other.0)
623    }
624}
625
626impl Mul<f32> for Integer {
627    type Output = Integer;
628
629    fn mul(self, scalar: f32) -> Integer {
630        Integer((self.0 as f64 * scalar as f64) as i64)
631    }
632}
633
634impl Mul<f64> for Integer {
635    type Output = Integer;
636
637    fn mul(self, scalar: f64) -> Integer {
638        Integer((self.0 as f64 * scalar) as i64)
639    }
640}
641
642// Boolean arithmetic operations (treat as 0.0 and 1.0)
643impl Add for Boolean {
644    type Output = Boolean;
645
646    fn add(self, other: Boolean) -> Boolean {
647        Boolean(self.0 || other.0)
648    }
649}
650
651impl Sub for Boolean {
652    type Output = Boolean;
653
654    fn sub(self, other: Boolean) -> Boolean {
655        Boolean(self.0 && !other.0)
656    }
657}
658
659impl Mul<f32> for Boolean {
660    type Output = Boolean;
661
662    fn mul(self, _scalar: f32) -> Boolean {
663        self
664    }
665}
666
667impl Mul<f64> for Boolean {
668    type Output = Boolean;
669
670    fn mul(self, _scalar: f64) -> Boolean {
671        self
672    }
673}
674
675// String arithmetic operations (concatenation for add, identity for others)
676impl Add for String {
677    type Output = String;
678
679    fn add(self, _other: String) -> String {
680        self
681    }
682}
683
684impl Sub for String {
685    type Output = String;
686
687    fn sub(self, _other: String) -> String {
688        self
689    }
690}
691
692impl Mul<f32> for String {
693    type Output = String;
694
695    fn mul(self, _scalar: f32) -> String {
696        self
697    }
698}
699
700impl Mul<f64> for String {
701    type Output = String;
702
703    fn mul(self, _scalar: f64) -> String {
704        self
705    }
706}
707
708// BooleanVec arithmetic operations
709impl Add for BooleanVec {
710    type Output = BooleanVec;
711
712    fn add(self, other: BooleanVec) -> BooleanVec {
713        if self.0.len() != other.0.len() {
714            panic!("Vector lengths must match for addition");
715        }
716        BooleanVec(
717            self.0
718                .into_iter()
719                .zip(other.0)
720                .map(|(a, b)| a || b)
721                .collect(),
722        )
723    }
724}
725
726impl Sub for BooleanVec {
727    type Output = BooleanVec;
728
729    fn sub(self, other: BooleanVec) -> BooleanVec {
730        if self.0.len() != other.0.len() {
731            panic!("Vector lengths must match for subtraction");
732        }
733        BooleanVec(
734            self.0
735                .into_iter()
736                .zip(other.0)
737                .map(|(a, b)| a && !b)
738                .collect(),
739        )
740    }
741}
742
743impl Mul<f32> for BooleanVec {
744    type Output = BooleanVec;
745
746    fn mul(self, _scalar: f32) -> BooleanVec {
747        self
748    }
749}
750
751impl Mul<f64> for BooleanVec {
752    type Output = BooleanVec;
753
754    fn mul(self, _scalar: f64) -> BooleanVec {
755        self
756    }
757}
758
759// StringVec arithmetic operations
760impl Add for StringVec {
761    type Output = StringVec;
762
763    fn add(self, other: StringVec) -> StringVec {
764        if self.0.len() != other.0.len() {
765            panic!("Vector lengths must match for addition");
766        }
767        StringVec(
768            self.0
769                .into_iter()
770                .zip(other.0)
771                .map(|(a, b)| format!("{a}{b}"))
772                .collect(),
773        )
774    }
775}
776
777impl Sub for StringVec {
778    type Output = StringVec;
779
780    fn sub(self, _other: StringVec) -> StringVec {
781        self
782    }
783}
784
785impl Mul<f32> for StringVec {
786    type Output = StringVec;
787
788    fn mul(self, _scalar: f32) -> StringVec {
789        self
790    }
791}
792
793impl Mul<f64> for StringVec {
794    type Output = StringVec;
795
796    fn mul(self, _scalar: f64) -> StringVec {
797        self
798    }
799}
800
801// Color arithmetic operations
802impl Add for Color {
803    type Output = Color;
804
805    fn add(self, other: Color) -> Color {
806        Color([
807            self.0[0] + other.0[0],
808            self.0[1] + other.0[1],
809            self.0[2] + other.0[2],
810            self.0[3] + other.0[3],
811        ])
812    }
813}
814
815impl Sub for Color {
816    type Output = Color;
817
818    fn sub(self, other: Color) -> Color {
819        Color([
820            self.0[0] - other.0[0],
821            self.0[1] - other.0[1],
822            self.0[2] - other.0[2],
823            self.0[3] - other.0[3],
824        ])
825    }
826}
827
828impl Mul<f32> for Color {
829    type Output = Color;
830
831    fn mul(self, scalar: f32) -> Color {
832        Color([
833            self.0[0] * scalar,
834            self.0[1] * scalar,
835            self.0[2] * scalar,
836            self.0[3] * scalar,
837        ])
838    }
839}
840
841impl Mul<f64> for Color {
842    type Output = Color;
843
844    fn mul(self, scalar: f64) -> Color {
845        let scalar = scalar as f32;
846        Color([
847            self.0[0] * scalar,
848            self.0[1] * scalar,
849            self.0[2] * scalar,
850            self.0[3] * scalar,
851        ])
852    }
853}
854
855// Vector2 arithmetic operations
856#[cfg(feature = "vector2")]
857impl_nalgebra_arithmetic!(Vector2);
858
859// Vector3 arithmetic operations
860#[cfg(feature = "vector3")]
861impl_nalgebra_arithmetic!(Vector3);
862
863// Matrix3 arithmetic operations
864#[cfg(feature = "matrix3")]
865impl_nalgebra_arithmetic!(Matrix3);
866
867// Matrix3 multiplication (matrix * matrix)
868#[cfg(feature = "matrix3")]
869impl Mul for Matrix3 {
870    type Output = Matrix3;
871
872    fn mul(self, other: Matrix3) -> Matrix3 {
873        Matrix3(self.0 * other.0)
874    }
875}
876
877#[cfg(feature = "matrix3")]
878impl Mul<&Matrix3> for Matrix3 {
879    type Output = Matrix3;
880
881    fn mul(self, other: &Matrix3) -> Matrix3 {
882        Matrix3(self.0 * other.0)
883    }
884}
885
886#[cfg(feature = "matrix3")]
887impl Mul<Matrix3> for &Matrix3 {
888    type Output = Matrix3;
889
890    fn mul(self, other: Matrix3) -> Matrix3 {
891        Matrix3(self.0 * other.0)
892    }
893}
894
895#[cfg(feature = "matrix3")]
896impl Mul<&Matrix3> for &Matrix3 {
897    type Output = Matrix3;
898
899    fn mul(self, other: &Matrix3) -> Matrix3 {
900        Matrix3(self.0 * other.0)
901    }
902}
903
904// Vector types arithmetic operations
905impl Add for RealVec {
906    type Output = RealVec;
907
908    fn add(self, other: RealVec) -> RealVec {
909        if self.0.len() != other.0.len() {
910            panic!("Vector lengths must match for addition");
911        }
912        RealVec(
913            self.0
914                .into_iter()
915                .zip(other.0)
916                .map(|(a, b)| a + b)
917                .collect(),
918        )
919    }
920}
921
922impl Sub for RealVec {
923    type Output = RealVec;
924
925    fn sub(self, other: RealVec) -> RealVec {
926        if self.0.len() != other.0.len() {
927            panic!("Vector lengths must match for subtraction");
928        }
929        RealVec(
930            self.0
931                .into_iter()
932                .zip(other.0)
933                .map(|(a, b)| a - b)
934                .collect(),
935        )
936    }
937}
938
939impl Mul<f32> for RealVec {
940    type Output = RealVec;
941
942    fn mul(self, scalar: f32) -> RealVec {
943        RealVec(self.0.into_iter().map(|x| x * scalar as f64).collect())
944    }
945}
946
947impl Mul<f64> for RealVec {
948    type Output = RealVec;
949
950    fn mul(self, scalar: f64) -> RealVec {
951        RealVec(self.0.into_iter().map(|x| x * scalar).collect())
952    }
953}
954
955impl Add for IntegerVec {
956    type Output = IntegerVec;
957
958    fn add(self, other: IntegerVec) -> IntegerVec {
959        if self.0.len() != other.0.len() {
960            panic!("Vector lengths must match for addition");
961        }
962        IntegerVec(
963            self.0
964                .into_iter()
965                .zip(other.0)
966                .map(|(a, b)| a + b)
967                .collect(),
968        )
969    }
970}
971
972impl Sub for IntegerVec {
973    type Output = IntegerVec;
974
975    fn sub(self, other: IntegerVec) -> IntegerVec {
976        if self.0.len() != other.0.len() {
977            panic!("Vector lengths must match for subtraction");
978        }
979        IntegerVec(
980            self.0
981                .into_iter()
982                .zip(other.0)
983                .map(|(a, b)| a - b)
984                .collect(),
985        )
986    }
987}
988
989impl Mul<f32> for IntegerVec {
990    type Output = IntegerVec;
991
992    fn mul(self, scalar: f32) -> IntegerVec {
993        IntegerVec(
994            self.0
995                .into_iter()
996                .map(|x| (x as f64 * scalar as f64) as i64)
997                .collect(),
998        )
999    }
1000}
1001
1002impl Mul<f64> for IntegerVec {
1003    type Output = IntegerVec;
1004
1005    fn mul(self, scalar: f64) -> IntegerVec {
1006        IntegerVec(
1007            self.0
1008                .into_iter()
1009                .map(|x| (x as f64 * scalar) as i64)
1010                .collect(),
1011        )
1012    }
1013}
1014
1015impl Add for ColorVec {
1016    type Output = ColorVec;
1017
1018    fn add(self, other: ColorVec) -> ColorVec {
1019        if self.0.len() != other.0.len() {
1020            panic!("Vector lengths must match for addition");
1021        }
1022        ColorVec(
1023            self.0
1024                .into_iter()
1025                .zip(other.0)
1026                .map(|(a, b)| [a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3]])
1027                .collect(),
1028        )
1029    }
1030}
1031
1032impl Sub for ColorVec {
1033    type Output = ColorVec;
1034
1035    fn sub(self, other: ColorVec) -> ColorVec {
1036        if self.0.len() != other.0.len() {
1037            panic!("Vector lengths must match for subtraction");
1038        }
1039        ColorVec(
1040            self.0
1041                .into_iter()
1042                .zip(other.0)
1043                .map(|(a, b)| [a[0] - b[0], a[1] - b[1], a[2] - b[2], a[3] - b[3]])
1044                .collect(),
1045        )
1046    }
1047}
1048
1049impl Mul<f32> for ColorVec {
1050    type Output = ColorVec;
1051
1052    fn mul(self, scalar: f32) -> ColorVec {
1053        ColorVec(
1054            self.0
1055                .into_iter()
1056                .map(|color| {
1057                    [
1058                        color[0] * scalar,
1059                        color[1] * scalar,
1060                        color[2] * scalar,
1061                        color[3] * scalar,
1062                    ]
1063                })
1064                .collect(),
1065        )
1066    }
1067}
1068
1069impl Mul<f64> for ColorVec {
1070    type Output = ColorVec;
1071
1072    fn mul(self, scalar: f64) -> ColorVec {
1073        let scalar = scalar as f32;
1074        ColorVec(
1075            self.0
1076                .into_iter()
1077                .map(|color| {
1078                    [
1079                        color[0] * scalar,
1080                        color[1] * scalar,
1081                        color[2] * scalar,
1082                        color[3] * scalar,
1083                    ]
1084                })
1085                .collect(),
1086        )
1087    }
1088}
1089
1090#[cfg(all(feature = "vector2", feature = "vec_variants"))]
1091impl Add for Vector2Vec {
1092    type Output = Vector2Vec;
1093
1094    fn add(self, other: Vector2Vec) -> Vector2Vec {
1095        if self.0.len() != other.0.len() {
1096            panic!("Vector lengths must match for addition");
1097        }
1098        Vector2Vec(
1099            self.0
1100                .into_iter()
1101                .zip(other.0)
1102                .map(|(a, b)| a + b)
1103                .collect(),
1104        )
1105    }
1106}
1107
1108#[cfg(all(feature = "vector2", feature = "vec_variants"))]
1109impl Sub for Vector2Vec {
1110    type Output = Vector2Vec;
1111
1112    fn sub(self, other: Vector2Vec) -> Vector2Vec {
1113        if self.0.len() != other.0.len() {
1114            panic!("Vector lengths must match for subtraction");
1115        }
1116        Vector2Vec(
1117            self.0
1118                .into_iter()
1119                .zip(other.0)
1120                .map(|(a, b)| a - b)
1121                .collect(),
1122        )
1123    }
1124}
1125
1126#[cfg(all(feature = "vector2", feature = "vec_variants"))]
1127impl Mul<f32> for Vector2Vec {
1128    type Output = Vector2Vec;
1129
1130    fn mul(self, scalar: f32) -> Vector2Vec {
1131        Vector2Vec(self.0.into_iter().map(|vec| vec * scalar).collect())
1132    }
1133}
1134
1135#[cfg(all(feature = "vector2", feature = "vec_variants"))]
1136impl Mul<f64> for Vector2Vec {
1137    type Output = Vector2Vec;
1138
1139    fn mul(self, scalar: f64) -> Vector2Vec {
1140        let scalar = scalar as f32;
1141        Vector2Vec(self.0.into_iter().map(|vec| vec * scalar).collect())
1142    }
1143}
1144
1145#[cfg(all(feature = "vector3", feature = "vec_variants"))]
1146impl Add for Vector3Vec {
1147    type Output = Vector3Vec;
1148
1149    fn add(self, other: Vector3Vec) -> Vector3Vec {
1150        if self.0.len() != other.0.len() {
1151            panic!("Vector lengths must match for addition");
1152        }
1153        Vector3Vec(
1154            self.0
1155                .into_iter()
1156                .zip(other.0)
1157                .map(|(a, b)| a + b)
1158                .collect(),
1159        )
1160    }
1161}
1162
1163#[cfg(all(feature = "vector3", feature = "vec_variants"))]
1164impl Sub for Vector3Vec {
1165    type Output = Vector3Vec;
1166
1167    fn sub(self, other: Vector3Vec) -> Vector3Vec {
1168        if self.0.len() != other.0.len() {
1169            panic!("Vector lengths must match for subtraction");
1170        }
1171        Vector3Vec(
1172            self.0
1173                .into_iter()
1174                .zip(other.0)
1175                .map(|(a, b)| a - b)
1176                .collect(),
1177        )
1178    }
1179}
1180
1181#[cfg(all(feature = "vector3", feature = "vec_variants"))]
1182impl Mul<f32> for Vector3Vec {
1183    type Output = Vector3Vec;
1184
1185    fn mul(self, scalar: f32) -> Vector3Vec {
1186        Vector3Vec(self.0.into_iter().map(|vec| vec * scalar).collect())
1187    }
1188}
1189
1190#[cfg(all(feature = "vector3", feature = "vec_variants"))]
1191impl Mul<f64> for Vector3Vec {
1192    type Output = Vector3Vec;
1193
1194    fn mul(self, scalar: f64) -> Vector3Vec {
1195        let scalar = scalar as f32;
1196        Vector3Vec(self.0.into_iter().map(|vec| vec * scalar).collect())
1197    }
1198}
1199
1200#[cfg(all(feature = "matrix3", feature = "vec_variants"))]
1201impl Add for Matrix3Vec {
1202    type Output = Matrix3Vec;
1203
1204    fn add(self, other: Matrix3Vec) -> Matrix3Vec {
1205        if self.0.len() != other.0.len() {
1206            panic!("Vector lengths must match for addition");
1207        }
1208        Matrix3Vec(
1209            self.0
1210                .into_iter()
1211                .zip(other.0)
1212                .map(|(a, b)| a + b)
1213                .collect(),
1214        )
1215    }
1216}
1217
1218#[cfg(all(feature = "matrix3", feature = "vec_variants"))]
1219impl Sub for Matrix3Vec {
1220    type Output = Matrix3Vec;
1221
1222    fn sub(self, other: Matrix3Vec) -> Matrix3Vec {
1223        if self.0.len() != other.0.len() {
1224            panic!("Vector lengths must match for subtraction");
1225        }
1226        Matrix3Vec(
1227            self.0
1228                .into_iter()
1229                .zip(other.0)
1230                .map(|(a, b)| a - b)
1231                .collect(),
1232        )
1233    }
1234}
1235
1236#[cfg(all(feature = "matrix3", feature = "vec_variants"))]
1237impl Mul<f32> for Matrix3Vec {
1238    type Output = Matrix3Vec;
1239
1240    fn mul(self, scalar: f32) -> Matrix3Vec {
1241        Matrix3Vec(self.0.into_iter().map(|mat| mat * scalar).collect())
1242    }
1243}
1244
1245#[cfg(all(feature = "matrix3", feature = "vec_variants"))]
1246impl Mul<f64> for Matrix3Vec {
1247    type Output = Matrix3Vec;
1248
1249    fn mul(self, scalar: f64) -> Matrix3Vec {
1250        let scalar = scalar as f32;
1251        Matrix3Vec(self.0.into_iter().map(|mat| mat * scalar).collect())
1252    }
1253}
1254
1255// Normal3Vec arithmetic operations
1256#[cfg(all(feature = "normal3", feature = "vec_variants"))]
1257impl Add for Normal3Vec {
1258    type Output = Normal3Vec;
1259
1260    fn add(self, other: Normal3Vec) -> Self::Output {
1261        Normal3Vec(
1262            self.0
1263                .into_iter()
1264                .zip(other.0)
1265                .map(|(a, b)| a + b)
1266                .collect(),
1267        )
1268    }
1269}
1270
1271#[cfg(all(feature = "normal3", feature = "vec_variants"))]
1272impl Sub for Normal3Vec {
1273    type Output = Normal3Vec;
1274
1275    fn sub(self, other: Normal3Vec) -> Self::Output {
1276        Normal3Vec(
1277            self.0
1278                .into_iter()
1279                .zip(other.0)
1280                .map(|(a, b)| a - b)
1281                .collect(),
1282        )
1283    }
1284}
1285
1286#[cfg(all(feature = "normal3", feature = "vec_variants"))]
1287impl Mul<f32> for Normal3Vec {
1288    type Output = Normal3Vec;
1289
1290    fn mul(self, scalar: f32) -> Self::Output {
1291        Normal3Vec(self.0.into_iter().map(|v| v * scalar).collect())
1292    }
1293}
1294
1295#[cfg(all(feature = "normal3", feature = "vec_variants"))]
1296impl Mul<f64> for Normal3Vec {
1297    type Output = Normal3Vec;
1298
1299    fn mul(self, scalar: f64) -> Self::Output {
1300        Normal3Vec(self.0.into_iter().map(|v| v * scalar as f32).collect())
1301    }
1302}
1303
1304// Point3Vec arithmetic operations
1305#[cfg(all(feature = "point3", feature = "vec_variants"))]
1306impl Add for Point3Vec {
1307    type Output = Point3Vec;
1308
1309    fn add(self, other: Point3Vec) -> Self::Output {
1310        Point3Vec(
1311            self.0
1312                .into_iter()
1313                .zip(other.0)
1314                .map(|(a, b)| a + b.coords)
1315                .collect(),
1316        )
1317    }
1318}
1319
1320#[cfg(all(feature = "point3", feature = "vec_variants"))]
1321impl Sub for Point3Vec {
1322    type Output = Point3Vec;
1323
1324    fn sub(self, other: Point3Vec) -> Self::Output {
1325        Point3Vec(
1326            self.0
1327                .into_iter()
1328                .zip(other.0)
1329                .map(|(a, b)| nalgebra::Point3::from(a.coords - b.coords))
1330                .collect(),
1331        )
1332    }
1333}
1334
1335#[cfg(all(feature = "point3", feature = "vec_variants"))]
1336impl Mul<f32> for Point3Vec {
1337    type Output = Point3Vec;
1338
1339    fn mul(self, scalar: f32) -> Self::Output {
1340        Point3Vec(
1341            self.0
1342                .into_iter()
1343                .map(|p| nalgebra::Point3::from(p.coords * scalar))
1344                .collect(),
1345        )
1346    }
1347}
1348
1349#[cfg(all(feature = "point3", feature = "vec_variants"))]
1350impl Mul<f64> for Point3Vec {
1351    type Output = Point3Vec;
1352
1353    fn mul(self, scalar: f64) -> Self::Output {
1354        Point3Vec(
1355            self.0
1356                .into_iter()
1357                .map(|p| nalgebra::Point3::from(p.coords * scalar as f32))
1358                .collect(),
1359        )
1360    }
1361}
1362
1363// Matrix4Vec arithmetic operations
1364#[cfg(all(feature = "matrix4", feature = "vec_variants"))]
1365impl Add for Matrix4Vec {
1366    type Output = Matrix4Vec;
1367
1368    fn add(self, other: Matrix4Vec) -> Self::Output {
1369        Matrix4Vec(
1370            self.0
1371                .into_iter()
1372                .zip(other.0)
1373                .map(|(a, b)| a + b)
1374                .collect(),
1375        )
1376    }
1377}
1378
1379#[cfg(all(feature = "matrix4", feature = "vec_variants"))]
1380impl Sub for Matrix4Vec {
1381    type Output = Matrix4Vec;
1382
1383    fn sub(self, other: Matrix4Vec) -> Self::Output {
1384        Matrix4Vec(
1385            self.0
1386                .into_iter()
1387                .zip(other.0)
1388                .map(|(a, b)| a - b)
1389                .collect(),
1390        )
1391    }
1392}
1393
1394#[cfg(all(feature = "matrix4", feature = "vec_variants"))]
1395impl Mul<f32> for Matrix4Vec {
1396    type Output = Matrix4Vec;
1397
1398    fn mul(self, scalar: f32) -> Self::Output {
1399        Matrix4Vec(self.0.into_iter().map(|v| v * scalar as f64).collect())
1400    }
1401}
1402
1403#[cfg(all(feature = "matrix4", feature = "vec_variants"))]
1404impl Mul<f64> for Matrix4Vec {
1405    type Output = Matrix4Vec;
1406
1407    fn mul(self, scalar: f64) -> Self::Output {
1408        Matrix4Vec(self.0.into_iter().map(|v| v * scalar).collect())
1409    }
1410}
1411
1412// Division implementations for f32
1413#[cfg(feature = "normal3")]
1414impl Div<f32> for Normal3 {
1415    type Output = Normal3;
1416
1417    fn div(self, scalar: f32) -> Normal3 {
1418        Normal3(self.0 / scalar)
1419    }
1420}
1421
1422#[cfg(feature = "point3")]
1423impl Div<f32> for Point3 {
1424    type Output = Point3;
1425
1426    fn div(self, scalar: f32) -> Point3 {
1427        Point3(self.0 / scalar)
1428    }
1429}
1430
1431#[cfg(feature = "matrix4")]
1432impl Div<f32> for Matrix4 {
1433    type Output = Matrix4;
1434
1435    fn div(self, scalar: f32) -> Matrix4 {
1436        Matrix4(self.0 / scalar as f64)
1437    }
1438}
1439
1440impl Div<f32> for Real {
1441    type Output = Real;
1442
1443    fn div(self, scalar: f32) -> Real {
1444        Real(self.0 / scalar as f64)
1445    }
1446}
1447
1448impl Div<f32> for Integer {
1449    type Output = Integer;
1450
1451    fn div(self, scalar: f32) -> Integer {
1452        Integer((self.0 as f64 / scalar as f64) as i64)
1453    }
1454}
1455
1456impl Div<f32> for Boolean {
1457    type Output = Boolean;
1458
1459    fn div(self, _scalar: f32) -> Boolean {
1460        self
1461    }
1462}
1463
1464impl Div<f32> for String {
1465    type Output = String;
1466
1467    fn div(self, _scalar: f32) -> String {
1468        self
1469    }
1470}
1471
1472impl Div<f32> for BooleanVec {
1473    type Output = BooleanVec;
1474
1475    fn div(self, _scalar: f32) -> BooleanVec {
1476        self
1477    }
1478}
1479
1480impl Div<f32> for StringVec {
1481    type Output = StringVec;
1482
1483    fn div(self, _scalar: f32) -> StringVec {
1484        self
1485    }
1486}
1487
1488impl Div<f32> for Color {
1489    type Output = Color;
1490
1491    fn div(self, scalar: f32) -> Color {
1492        Color([
1493            self.0[0] / scalar,
1494            self.0[1] / scalar,
1495            self.0[2] / scalar,
1496            self.0[3] / scalar,
1497        ])
1498    }
1499}
1500
1501#[cfg(feature = "vector2")]
1502impl Div<f32> for Vector2 {
1503    type Output = Vector2;
1504
1505    fn div(self, scalar: f32) -> Vector2 {
1506        Vector2(self.0 / scalar)
1507    }
1508}
1509
1510#[cfg(feature = "vector3")]
1511impl Div<f32> for Vector3 {
1512    type Output = Vector3;
1513
1514    fn div(self, scalar: f32) -> Vector3 {
1515        Vector3(self.0 / scalar)
1516    }
1517}
1518
1519#[cfg(feature = "matrix3")]
1520impl Div<f32> for Matrix3 {
1521    type Output = Matrix3;
1522
1523    fn div(self, scalar: f32) -> Matrix3 {
1524        Matrix3(self.0 / scalar)
1525    }
1526}
1527
1528#[cfg(feature = "vec_variants")]
1529impl Div<f32> for RealVec {
1530    type Output = RealVec;
1531
1532    fn div(self, scalar: f32) -> RealVec {
1533        RealVec(self.0.into_iter().map(|x| x / scalar as f64).collect())
1534    }
1535}
1536
1537#[cfg(feature = "vec_variants")]
1538impl Div<f32> for IntegerVec {
1539    type Output = IntegerVec;
1540
1541    fn div(self, scalar: f32) -> IntegerVec {
1542        IntegerVec(
1543            self.0
1544                .into_iter()
1545                .map(|x| (x as f64 / scalar as f64) as i64)
1546                .collect(),
1547        )
1548    }
1549}
1550
1551#[cfg(feature = "vec_variants")]
1552impl Div<f32> for ColorVec {
1553    type Output = ColorVec;
1554
1555    fn div(self, scalar: f32) -> ColorVec {
1556        ColorVec(
1557            self.0
1558                .into_iter()
1559                .map(|color| {
1560                    [
1561                        color[0] / scalar,
1562                        color[1] / scalar,
1563                        color[2] / scalar,
1564                        color[3] / scalar,
1565                    ]
1566                })
1567                .collect(),
1568        )
1569    }
1570}
1571
1572#[cfg(all(feature = "vector2", feature = "vec_variants"))]
1573impl Div<f32> for Vector2Vec {
1574    type Output = Vector2Vec;
1575
1576    fn div(self, scalar: f32) -> Vector2Vec {
1577        Vector2Vec(self.0.into_iter().map(|vec| vec / scalar).collect())
1578    }
1579}
1580
1581#[cfg(all(feature = "vector3", feature = "vec_variants"))]
1582impl Div<f32> for Vector3Vec {
1583    type Output = Vector3Vec;
1584
1585    fn div(self, scalar: f32) -> Vector3Vec {
1586        Vector3Vec(self.0.into_iter().map(|vec| vec / scalar).collect())
1587    }
1588}
1589
1590#[cfg(all(feature = "matrix3", feature = "vec_variants"))]
1591impl Div<f32> for Matrix3Vec {
1592    type Output = Matrix3Vec;
1593
1594    fn div(self, scalar: f32) -> Matrix3Vec {
1595        Matrix3Vec(self.0.into_iter().map(|mat| mat / scalar).collect())
1596    }
1597}
1598
1599#[cfg(all(feature = "normal3", feature = "vec_variants"))]
1600impl Div<f32> for Normal3Vec {
1601    type Output = Normal3Vec;
1602
1603    fn div(self, scalar: f32) -> Self::Output {
1604        Normal3Vec(self.0.into_iter().map(|v| v / scalar).collect())
1605    }
1606}
1607
1608#[cfg(all(feature = "point3", feature = "vec_variants"))]
1609impl Div<f32> for Point3Vec {
1610    type Output = Point3Vec;
1611
1612    fn div(self, scalar: f32) -> Self::Output {
1613        Point3Vec(self.0.into_iter().map(|v| v / scalar).collect())
1614    }
1615}
1616
1617#[cfg(all(feature = "matrix4", feature = "vec_variants"))]
1618impl Div<f32> for Matrix4Vec {
1619    type Output = Matrix4Vec;
1620
1621    fn div(self, scalar: f32) -> Self::Output {
1622        Matrix4Vec(self.0.into_iter().map(|v| v / scalar as f64).collect())
1623    }
1624}
1625
1626// Division implementations for f64
1627#[cfg(feature = "normal3")]
1628impl Div<f64> for Normal3 {
1629    type Output = Normal3;
1630
1631    fn div(self, scalar: f64) -> Normal3 {
1632        Normal3(self.0 / scalar as f32)
1633    }
1634}
1635
1636#[cfg(feature = "point3")]
1637impl Div<f64> for Point3 {
1638    type Output = Point3;
1639
1640    fn div(self, scalar: f64) -> Point3 {
1641        Point3(self.0 / scalar as f32)
1642    }
1643}
1644
1645#[cfg(feature = "matrix4")]
1646impl Div<f64> for Matrix4 {
1647    type Output = Matrix4;
1648
1649    fn div(self, scalar: f64) -> Matrix4 {
1650        Matrix4(self.0 / scalar)
1651    }
1652}
1653
1654impl Div<f64> for Real {
1655    type Output = Real;
1656
1657    fn div(self, scalar: f64) -> Real {
1658        Real(self.0 / scalar)
1659    }
1660}
1661
1662impl Div<f64> for Integer {
1663    type Output = Integer;
1664
1665    fn div(self, scalar: f64) -> Integer {
1666        Integer((self.0 as f64 / scalar) as i64)
1667    }
1668}
1669
1670impl Div<f64> for Boolean {
1671    type Output = Boolean;
1672
1673    fn div(self, _scalar: f64) -> Boolean {
1674        self
1675    }
1676}
1677
1678impl Div<f64> for String {
1679    type Output = String;
1680
1681    fn div(self, _scalar: f64) -> String {
1682        self
1683    }
1684}
1685
1686#[cfg(feature = "vec_variants")]
1687impl Div<f64> for BooleanVec {
1688    type Output = BooleanVec;
1689
1690    fn div(self, _scalar: f64) -> BooleanVec {
1691        self
1692    }
1693}
1694
1695#[cfg(feature = "vec_variants")]
1696impl Div<f64> for StringVec {
1697    type Output = StringVec;
1698
1699    fn div(self, _scalar: f64) -> StringVec {
1700        self
1701    }
1702}
1703
1704impl Div<f64> for Color {
1705    type Output = Color;
1706
1707    fn div(self, scalar: f64) -> Color {
1708        let scalar = scalar as f32;
1709        Color([
1710            self.0[0] / scalar,
1711            self.0[1] / scalar,
1712            self.0[2] / scalar,
1713            self.0[3] / scalar,
1714        ])
1715    }
1716}
1717
1718#[cfg(feature = "vector2")]
1719impl Div<f64> for Vector2 {
1720    type Output = Vector2;
1721
1722    fn div(self, scalar: f64) -> Vector2 {
1723        Vector2(self.0 / scalar as f32)
1724    }
1725}
1726
1727#[cfg(feature = "vector3")]
1728impl Div<f64> for Vector3 {
1729    type Output = Vector3;
1730
1731    fn div(self, scalar: f64) -> Vector3 {
1732        Vector3(self.0 / scalar as f32)
1733    }
1734}
1735
1736#[cfg(feature = "matrix3")]
1737impl Div<f64> for Matrix3 {
1738    type Output = Matrix3;
1739
1740    fn div(self, scalar: f64) -> Matrix3 {
1741        Matrix3(self.0 / scalar as f32)
1742    }
1743}
1744
1745#[cfg(feature = "vec_variants")]
1746impl Div<f64> for RealVec {
1747    type Output = RealVec;
1748
1749    fn div(self, scalar: f64) -> RealVec {
1750        RealVec(self.0.into_iter().map(|x| x / scalar).collect())
1751    }
1752}
1753
1754#[cfg(feature = "vec_variants")]
1755impl Div<f64> for IntegerVec {
1756    type Output = IntegerVec;
1757
1758    fn div(self, scalar: f64) -> IntegerVec {
1759        IntegerVec(
1760            self.0
1761                .into_iter()
1762                .map(|x| (x as f64 / scalar) as i64)
1763                .collect(),
1764        )
1765    }
1766}
1767
1768#[cfg(feature = "vec_variants")]
1769impl Div<f64> for ColorVec {
1770    type Output = ColorVec;
1771
1772    fn div(self, scalar: f64) -> ColorVec {
1773        let scalar = scalar as f32;
1774        ColorVec(
1775            self.0
1776                .into_iter()
1777                .map(|color| {
1778                    [
1779                        color[0] / scalar,
1780                        color[1] / scalar,
1781                        color[2] / scalar,
1782                        color[3] / scalar,
1783                    ]
1784                })
1785                .collect(),
1786        )
1787    }
1788}
1789
1790#[cfg(all(feature = "vector2", feature = "vec_variants"))]
1791impl Div<f64> for Vector2Vec {
1792    type Output = Vector2Vec;
1793
1794    fn div(self, scalar: f64) -> Vector2Vec {
1795        let scalar = scalar as f32;
1796        Vector2Vec(self.0.into_iter().map(|vec| vec / scalar).collect())
1797    }
1798}
1799
1800#[cfg(all(feature = "vector3", feature = "vec_variants"))]
1801impl Div<f64> for Vector3Vec {
1802    type Output = Vector3Vec;
1803
1804    fn div(self, scalar: f64) -> Vector3Vec {
1805        let scalar = scalar as f32;
1806        Vector3Vec(self.0.into_iter().map(|vec| vec / scalar).collect())
1807    }
1808}
1809
1810#[cfg(all(feature = "matrix3", feature = "vec_variants"))]
1811impl Div<f64> for Matrix3Vec {
1812    type Output = Matrix3Vec;
1813
1814    fn div(self, scalar: f64) -> Matrix3Vec {
1815        let scalar = scalar as f32;
1816        Matrix3Vec(self.0.into_iter().map(|mat| mat / scalar).collect())
1817    }
1818}
1819
1820#[cfg(all(feature = "normal3", feature = "vec_variants"))]
1821impl Div<f64> for Normal3Vec {
1822    type Output = Normal3Vec;
1823
1824    fn div(self, scalar: f64) -> Self::Output {
1825        Normal3Vec(self.0.into_iter().map(|v| v / scalar as f32).collect())
1826    }
1827}
1828
1829#[cfg(all(feature = "point3", feature = "vec_variants"))]
1830impl Div<f64> for Point3Vec {
1831    type Output = Point3Vec;
1832
1833    fn div(self, scalar: f64) -> Self::Output {
1834        Point3Vec(self.0.into_iter().map(|v| v / scalar as f32).collect())
1835    }
1836}
1837
1838#[cfg(all(feature = "matrix4", feature = "vec_variants"))]
1839impl Div<f64> for Matrix4Vec {
1840    type Output = Matrix4Vec;
1841
1842    fn div(self, scalar: f64) -> Self::Output {
1843        Matrix4Vec(self.0.into_iter().map(|v| v / scalar).collect())
1844    }
1845}
1846
1847// Hash implementations for floating point types
1848// Using bit representation for deterministic hashing
1849
1850impl Hash for Real {
1851    fn hash<H: Hasher>(&self, state: &mut H) {
1852        // Normalize -0.0 to 0.0 for consistent hashing
1853        // Check if the value is zero (either -0.0 or 0.0) and normalize to 0.0
1854        let normalized = if self.0 == 0.0 { 0.0_f64 } else { self.0 };
1855        normalized.to_bits().hash(state);
1856    }
1857}
1858
1859impl Hash for Color {
1860    fn hash<H: Hasher>(&self, state: &mut H) {
1861        for &component in &self.0 {
1862            // Normalize -0.0 to 0.0 for consistent hashing
1863            let normalized = if component == 0.0 { 0.0_f32 } else { component };
1864            normalized.to_bits().hash(state);
1865        }
1866    }
1867}
1868
1869#[cfg(feature = "vector2")]
1870impl Hash for Vector2 {
1871    fn hash<H: Hasher>(&self, state: &mut H) {
1872        // Normalize -0.0 to 0.0 for consistent hashing
1873        let x = if self.0.x == 0.0 { 0.0_f32 } else { self.0.x };
1874        let y = if self.0.y == 0.0 { 0.0_f32 } else { self.0.y };
1875        x.to_bits().hash(state);
1876        y.to_bits().hash(state);
1877    }
1878}
1879
1880#[cfg(feature = "vector3")]
1881impl Hash for Vector3 {
1882    fn hash<H: Hasher>(&self, state: &mut H) {
1883        // Normalize -0.0 to 0.0 for consistent hashing
1884        let x = if self.0.x == 0.0 { 0.0_f32 } else { self.0.x };
1885        let y = if self.0.y == 0.0 { 0.0_f32 } else { self.0.y };
1886        let z = if self.0.z == 0.0 { 0.0_f32 } else { self.0.z };
1887        x.to_bits().hash(state);
1888        y.to_bits().hash(state);
1889        z.to_bits().hash(state);
1890    }
1891}
1892
1893#[cfg(feature = "matrix3")]
1894impl Hash for Matrix3 {
1895    fn hash<H: Hasher>(&self, state: &mut H) {
1896        for &element in self.0.iter() {
1897            // Normalize -0.0 to 0.0 for consistent hashing
1898            let normalized = if element == 0.0 { 0.0_f32 } else { element };
1899            normalized.to_bits().hash(state);
1900        }
1901    }
1902}
1903
1904impl Hash for RealVec {
1905    fn hash<H: Hasher>(&self, state: &mut H) {
1906        self.0.len().hash(state);
1907        for &element in &self.0 {
1908            // Normalize -0.0 to 0.0 for consistent hashing
1909            let normalized = if element == 0.0 { 0.0_f64 } else { element };
1910            normalized.to_bits().hash(state);
1911        }
1912    }
1913}
1914
1915impl Hash for ColorVec {
1916    fn hash<H: Hasher>(&self, state: &mut H) {
1917        self.0.len().hash(state);
1918        for color in &self.0 {
1919            for &component in color {
1920                // Normalize -0.0 to 0.0 for consistent hashing
1921                let normalized = if component == 0.0 { 0.0_f32 } else { component };
1922                normalized.to_bits().hash(state);
1923            }
1924        }
1925    }
1926}
1927
1928#[cfg(all(feature = "vector2", feature = "vec_variants"))]
1929impl Hash for Vector2Vec {
1930    fn hash<H: Hasher>(&self, state: &mut H) {
1931        self.0.len().hash(state);
1932        for vector in &self.0 {
1933            // Normalize -0.0 to 0.0 for consistent hashing
1934            let x = if vector.x == 0.0 { 0.0_f32 } else { vector.x };
1935            let y = if vector.y == 0.0 { 0.0_f32 } else { vector.y };
1936            x.to_bits().hash(state);
1937            y.to_bits().hash(state);
1938        }
1939    }
1940}
1941
1942#[cfg(all(feature = "vector3", feature = "vec_variants"))]
1943impl Hash for Vector3Vec {
1944    fn hash<H: Hasher>(&self, state: &mut H) {
1945        self.0.len().hash(state);
1946        for vector in &self.0 {
1947            // Normalize -0.0 to 0.0 for consistent hashing
1948            let x = if vector.x == 0.0 { 0.0_f32 } else { vector.x };
1949            let y = if vector.y == 0.0 { 0.0_f32 } else { vector.y };
1950            let z = if vector.z == 0.0 { 0.0_f32 } else { vector.z };
1951            x.to_bits().hash(state);
1952            y.to_bits().hash(state);
1953            z.to_bits().hash(state);
1954        }
1955    }
1956}
1957
1958#[cfg(all(feature = "matrix3", feature = "vec_variants"))]
1959impl Hash for Matrix3Vec {
1960    fn hash<H: Hasher>(&self, state: &mut H) {
1961        self.0.len().hash(state);
1962        for matrix in &self.0 {
1963            for &element in matrix.iter() {
1964                // Normalize -0.0 to 0.0 for consistent hashing
1965                let normalized = if element == 0.0 { 0.0_f32 } else { element };
1966                normalized.to_bits().hash(state);
1967            }
1968        }
1969    }
1970}
1971
1972// Hash implementations for new types
1973#[cfg(feature = "normal3")]
1974impl Hash for Normal3 {
1975    fn hash<H: Hasher>(&self, state: &mut H) {
1976        // Normalize -0.0 to 0.0 for consistent hashing
1977        let x = if self.0.x == 0.0 { 0.0_f32 } else { self.0.x };
1978        let y = if self.0.y == 0.0 { 0.0_f32 } else { self.0.y };
1979        let z = if self.0.z == 0.0 { 0.0_f32 } else { self.0.z };
1980        x.to_bits().hash(state);
1981        y.to_bits().hash(state);
1982        z.to_bits().hash(state);
1983    }
1984}
1985
1986#[cfg(feature = "point3")]
1987impl Hash for Point3 {
1988    fn hash<H: Hasher>(&self, state: &mut H) {
1989        // Normalize -0.0 to 0.0 for consistent hashing
1990        let x = if self.0.x == 0.0 { 0.0_f32 } else { self.0.x };
1991        let y = if self.0.y == 0.0 { 0.0_f32 } else { self.0.y };
1992        let z = if self.0.z == 0.0 { 0.0_f32 } else { self.0.z };
1993        x.to_bits().hash(state);
1994        y.to_bits().hash(state);
1995        z.to_bits().hash(state);
1996    }
1997}
1998
1999#[cfg(feature = "matrix4")]
2000impl Hash for Matrix4 {
2001    fn hash<H: Hasher>(&self, state: &mut H) {
2002        for &element in self.0.iter() {
2003            // Normalize -0.0 to 0.0 for consistent hashing
2004            let normalized = if element == 0.0 { 0.0_f64 } else { element };
2005            normalized.to_bits().hash(state);
2006        }
2007    }
2008}
2009
2010#[cfg(all(feature = "normal3", feature = "vec_variants"))]
2011impl Hash for Normal3Vec {
2012    fn hash<H: Hasher>(&self, state: &mut H) {
2013        self.0.len().hash(state);
2014        for vector in &self.0 {
2015            // Normalize -0.0 to 0.0 for consistent hashing
2016            let x = if vector.x == 0.0 { 0.0_f32 } else { vector.x };
2017            let y = if vector.y == 0.0 { 0.0_f32 } else { vector.y };
2018            let z = if vector.z == 0.0 { 0.0_f32 } else { vector.z };
2019            x.to_bits().hash(state);
2020            y.to_bits().hash(state);
2021            z.to_bits().hash(state);
2022        }
2023    }
2024}
2025
2026#[cfg(all(feature = "point3", feature = "vec_variants"))]
2027impl Hash for Point3Vec {
2028    fn hash<H: Hasher>(&self, state: &mut H) {
2029        self.0.len().hash(state);
2030        for point in &self.0 {
2031            // Normalize -0.0 to 0.0 for consistent hashing
2032            let x = if point.x == 0.0 { 0.0_f32 } else { point.x };
2033            let y = if point.y == 0.0 { 0.0_f32 } else { point.y };
2034            let z = if point.z == 0.0 { 0.0_f32 } else { point.z };
2035            x.to_bits().hash(state);
2036            y.to_bits().hash(state);
2037            z.to_bits().hash(state);
2038        }
2039    }
2040}
2041
2042#[cfg(all(feature = "matrix4", feature = "vec_variants"))]
2043impl Hash for Matrix4Vec {
2044    fn hash<H: Hasher>(&self, state: &mut H) {
2045        self.0.len().hash(state);
2046        for matrix in &self.0 {
2047            for &element in matrix.iter() {
2048                // Normalize -0.0 to 0.0 for consistent hashing
2049                let normalized = if element == 0.0 { 0.0_f64 } else { element };
2050                normalized.to_bits().hash(state);
2051            }
2052        }
2053    }
2054}
2055
2056// Implement DataTypeOps for all types
2057impl_data_ops!(Integer, "integer", DataType::Integer);
2058impl_data_ops!(Real, "real", DataType::Real);
2059impl_data_ops!(Boolean, "boolean", DataType::Boolean);
2060impl_data_ops!(String, "string", DataType::String);
2061impl_data_ops!(Color, "color", DataType::Color);
2062#[cfg(feature = "vector2")]
2063impl_data_ops!(Vector2, "vec2", DataType::Vector2);
2064#[cfg(feature = "vector3")]
2065impl_data_ops!(Vector3, "vec3", DataType::Vector3);
2066#[cfg(feature = "matrix3")]
2067impl_data_ops!(Matrix3, "mat3", DataType::Matrix3);
2068
2069impl_data_ops!(IntegerVec, "integer_vec", DataType::IntegerVec);
2070impl_data_ops!(RealVec, "real_vec", DataType::RealVec);
2071impl_data_ops!(BooleanVec, "boolean_vec", DataType::BooleanVec);
2072impl_data_ops!(StringVec, "string_vec", DataType::StringVec);
2073impl_data_ops!(ColorVec, "color_vec", DataType::ColorVec);
2074
2075#[cfg(all(feature = "vector2", feature = "vec_variants"))]
2076impl_data_ops!(Vector2Vec, "vec2_vec", DataType::Vector2Vec);
2077#[cfg(all(feature = "vector3", feature = "vec_variants"))]
2078impl_data_ops!(Vector3Vec, "vec3_vec", DataType::Vector3Vec);
2079#[cfg(all(feature = "matrix3", feature = "vec_variants"))]
2080impl_data_ops!(Matrix3Vec, "mat3_vec", DataType::Matrix3Vec);
2081
2082// New data type implementations
2083#[cfg(feature = "normal3")]
2084impl_data_ops!(Normal3, "normal3", DataType::Normal3);
2085#[cfg(feature = "point3")]
2086impl_data_ops!(Point3, "point3", DataType::Point3);
2087#[cfg(feature = "matrix4")]
2088impl_data_ops!(Matrix4, "matrix4", DataType::Matrix4);
2089
2090#[cfg(all(feature = "normal3", feature = "vec_variants"))]
2091impl_data_ops!(Normal3Vec, "normal3_vec", DataType::Normal3Vec);
2092#[cfg(all(feature = "point3", feature = "vec_variants"))]
2093impl_data_ops!(Point3Vec, "point3_vec", DataType::Point3Vec);
2094#[cfg(all(feature = "matrix4", feature = "vec_variants"))]
2095impl_data_ops!(Matrix4Vec, "matrix4_vec", DataType::Matrix4Vec);
2096
2097// Macro to implement TryFrom<Value> and TryFrom<&Value> for data types using
2098// try_convert
2099macro_rules! impl_try_from_value {
2100    ($type:ty, $data_type:expr, $variant:ident) => {
2101        impl TryFrom<Value> for $type {
2102            type Error = anyhow::Error;
2103
2104            fn try_from(value: Value) -> Result<Self, Self::Error> {
2105                match value {
2106                    Value::Uniform(data) => {
2107                        let converted = data.try_convert($data_type)?;
2108                        match converted {
2109                            Data::$variant(v) => Ok(v),
2110                            _ => unreachable!(
2111                                "try_convert should return {} type",
2112                                stringify!($variant)
2113                            ),
2114                        }
2115                    }
2116                    Value::Animated(_) => Err(anyhow!(
2117                        "Cannot convert animated value to {}",
2118                        stringify!($type)
2119                    )),
2120                }
2121            }
2122        }
2123
2124        impl TryFrom<&Value> for $type {
2125            type Error = anyhow::Error;
2126
2127            fn try_from(value: &Value) -> Result<Self, Self::Error> {
2128                match value {
2129                    Value::Uniform(data) => {
2130                        let converted = data.try_convert($data_type)?;
2131                        match converted {
2132                            Data::$variant(v) => Ok(v),
2133                            _ => unreachable!(
2134                                "try_convert should return {} type",
2135                                stringify!($variant)
2136                            ),
2137                        }
2138                    }
2139                    Value::Animated(_) => Err(anyhow!(
2140                        "Cannot convert animated value to {}",
2141                        stringify!($type)
2142                    )),
2143                }
2144            }
2145        }
2146    };
2147}
2148
2149// Apply the macro to all data types
2150impl_try_from_value!(Boolean, DataType::Boolean, Boolean);
2151impl_try_from_value!(Integer, DataType::Integer, Integer);
2152impl_try_from_value!(Real, DataType::Real, Real);
2153impl_try_from_value!(String, DataType::String, String);
2154impl_try_from_value!(Color, DataType::Color, Color);
2155#[cfg(feature = "vector2")]
2156impl_try_from_value!(Vector2, DataType::Vector2, Vector2);
2157#[cfg(feature = "vector3")]
2158impl_try_from_value!(Vector3, DataType::Vector3, Vector3);
2159#[cfg(feature = "matrix3")]
2160impl_try_from_value!(Matrix3, DataType::Matrix3, Matrix3);
2161impl_try_from_value!(BooleanVec, DataType::BooleanVec, BooleanVec);
2162impl_try_from_value!(IntegerVec, DataType::IntegerVec, IntegerVec);
2163impl_try_from_value!(RealVec, DataType::RealVec, RealVec);
2164impl_try_from_value!(StringVec, DataType::StringVec, StringVec);
2165impl_try_from_value!(ColorVec, DataType::ColorVec, ColorVec);
2166#[cfg(all(feature = "vector2", feature = "vec_variants"))]
2167impl_try_from_value!(Vector2Vec, DataType::Vector2Vec, Vector2Vec);
2168#[cfg(all(feature = "vector3", feature = "vec_variants"))]
2169impl_try_from_value!(Vector3Vec, DataType::Vector3Vec, Vector3Vec);
2170#[cfg(all(feature = "matrix3", feature = "vec_variants"))]
2171impl_try_from_value!(Matrix3Vec, DataType::Matrix3Vec, Matrix3Vec);
2172
2173// New type TryFrom implementations
2174#[cfg(feature = "normal3")]
2175impl_try_from_value!(Normal3, DataType::Normal3, Normal3);
2176#[cfg(feature = "point3")]
2177impl_try_from_value!(Point3, DataType::Point3, Point3);
2178#[cfg(feature = "matrix4")]
2179impl_try_from_value!(Matrix4, DataType::Matrix4, Matrix4);
2180
2181#[cfg(all(feature = "normal3", feature = "vec_variants"))]
2182impl_try_from_value!(Normal3Vec, DataType::Normal3Vec, Normal3Vec);
2183#[cfg(all(feature = "point3", feature = "vec_variants"))]
2184impl_try_from_value!(Point3Vec, DataType::Point3Vec, Point3Vec);
2185#[cfg(all(feature = "matrix4", feature = "vec_variants"))]
2186impl_try_from_value!(Matrix4Vec, DataType::Matrix4Vec, Matrix4Vec);