Skip to main content

ear_algae/
fmt.rs

1use std::{fmt::*, ops::Index};
2
3use culit::culit;
4
5use crate::{
6    Mat, Nrml, Rig, Rot, Vect,
7    op_wrapper::{Sc, scs},
8    rotor::{Axis, Bivector, RotDim},
9    traits::{Field, Ring},
10};
11
12impl<S: Ring + Display, const N: usize> Display for Vect<N, S> {
13    fn fmt(&self, f: &mut Formatter) -> Result {
14        f.write_str("{")?;
15        for i in 0..N - 1 {
16            f.write_str(&format!("{}, ", self[i]))?;
17        }
18        f.write_str(&format!("{}}}", self[N - 1]))
19    }
20}
21
22impl<S: Ring + Debug, const N: usize> Debug for Vect<N, S> {
23    fn fmt(&self, f: &mut Formatter) -> Result {
24        f.debug_list().entries(self.0).finish()
25    }
26}
27
28impl<S: Field + Display, const N: usize> Display for Nrml<N, S> {
29    fn fmt(&self, f: &mut Formatter) -> Result {
30        f.write_str("N{")?;
31        for i in 0..N - 1 {
32            f.write_str(&format!("{}, ", self[i]))?;
33        }
34        f.write_str(&format!("{}}}", self[N - 1]))
35    }
36}
37
38impl<S: Field + Debug, const N: usize> Debug for Nrml<N, S> {
39    fn fmt(&self, f: &mut Formatter) -> Result {
40        write!(f, "N")?;
41        f.debug_list().entries(Vect::from(*self).0).finish()
42    }
43}
44
45impl<S: Field + Display, const N: usize, const M: usize> Display for Mat<N, M, S> {
46    fn fmt(&self, f: &mut Formatter) -> Result {
47        if N == 0 {
48            f.write_str("[]")?
49        } else {
50            f.write_str(&format!("[{}", self[0][0]))?;
51            for j in 1..M {
52                f.write_str(&format!(", {}", self[0][j]))?;
53            }
54            for i in 1..N {
55                f.write_str(&format!("|{}", self[i][0]))?;
56                for j in 1..M {
57                    f.write_str(&format!(", {}", self[i][j]))?;
58                }
59            }
60            f.write_str("]")?
61        }
62        Ok(())
63    }
64}
65
66impl<const N: usize, S: Field + Display> Display for Rot<N, S>
67where
68    (): RotDim<N>,
69    Bivector<N, S>: Index<usize, Output = S>,
70    Axis<N, S>: Display,
71{
72    fn fmt(&self, f: &mut Formatter) -> Result {
73        if N == 2 {
74            let angle = self.angle();
75            let sign = self.axis_or_zero()[0];
76            scs!(angle, sign);
77            write!(f, "{}π rad", angle / Sc::PI * sign)
78        } else if let Some(axis) = self.axis() {
79            let angle = self.angle();
80            scs!(angle);
81            write!(f, "{}π rad about {}", angle / Sc::PI, axis)
82        } else {
83            write!(f, "0rad")
84        }
85    }
86}
87
88impl<const N: usize, S: Field + Debug> Debug for Rot<N, S>
89where
90    (): RotDim<N>,
91    Axis<N, S>: Debug,
92{
93    #[culit]
94    fn fmt(&self, f: &mut Formatter) -> Result {
95        if let Some(axis) = &self.axis() {
96            f.debug_tuple("Rot")
97                .field(&self.angle())
98                .field(axis)
99                .finish()
100        } else {
101            f.debug_tuple("Rot").field(&0S).finish()
102        }
103    }
104}
105
106impl<const N: usize, S: Field + Debug> Debug for Rig<N, S>
107where
108    (): RotDim<N>,
109    Rot<N, S>: Debug,
110{
111    fn fmt(&self, f: &mut Formatter) -> Result {
112        f.debug_struct("Rig")
113            .field("trans", &self.trans)
114            .field("rot", &self.rot)
115            .finish()
116    }
117}
118
119impl<S: Display> Display for Sc<S> {
120    fn fmt(&self, f: &mut Formatter) -> Result {
121        write!(f, "{}", self.0)
122    }
123}