Blame view

fractional/src/transform.rs 6.77 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
//
// Transformation of vectors in a given coordinate system...
//
// Georg Hopp <georg@steffers.org>
//
// Copyright © 2019 Georg Hopp
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
21
use std::ops::{Add, Sub, Neg, Mul, Div};
22
use std::fmt::Debug;
23
24 25 26
use crate::Vector;
use crate::trigonometry::Trig;
27
#[derive(Debug, Clone, Copy)]
28 29 30 31
pub struct TMatrix<T>( (T, T, T, T)
                     , (T, T, T, T)
                     , (T, T, T, T)
                     , (T, T, T, T) )
32
    where T: Add + Sub + Neg + Mul + Div + Debug + Trig + From<i32> + Copy;
33
Georg Hopp authored
34 35 36 37 38
pub trait Transformable<T>
where T: Add + Sub + Neg + Mul + Div + Debug + Trig + From<i32> + Copy {
    fn transform(&self, m :&TMatrix<T>) -> Self;
}
39
impl<T> TMatrix<T>
40
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
41 42
       + Mul<Output = T> + Div<Output = T>
       + Debug + Trig + From<i32> + Copy {
43 44 45 46 47 48
    pub fn new( r1 :(T, T, T, T)
              , r2 :(T, T, T, T)
              , r3 :(T, T, T, T)
              , r4 :(T, T, T, T) ) -> Self {
        TMatrix(r1, r2, r3, r4)
    }
49
50 51 52 53 54 55
    pub fn unit() -> Self {
        Self::new( (1.into(), 0.into(), 0.into(), 0.into())
                 , (0.into(), 1.into(), 0.into(), 0.into())
                 , (0.into(), 0.into(), 1.into(), 0.into())
                 , (0.into(), 0.into(), 0.into(), 1.into()) )
    }
56
57 58
    pub fn translate(v :Vector<T>) -> Self {
        let Vector(x, y, z) = v;
59
60 61 62 63 64
        Self::new( (1.into(), 0.into(), 0.into(),        x)
                 , (0.into(), 1.into(), 0.into(),        y)
                 , (0.into(), 0.into(), 1.into(),        z)
                 , (0.into(), 0.into(), 0.into(), 1.into()) )
    }
65
66 67 68
    pub fn rotate_x(a :i32) -> Self {
        let sin :T = Trig::sin(a);
        let cos :T = Trig::cos(a);
69
70 71 72 73 74
        Self::new( (1.into(), 0.into(), 0.into(), 0.into())
                 , (0.into(), cos     , -sin    , 0.into())
                 , (0.into(), sin     , cos     , 0.into())
                 , (0.into(), 0.into(), 0.into(), 1.into()) )
    }
75
76 77 78
    pub fn rotate_y(a :i32) -> Self {
        let sin :T = Trig::sin(a);
        let cos :T = Trig::cos(a);
79
80 81 82 83 84
        Self::new( (cos     , 0.into(), sin     , 0.into())
                 , (0.into(), 1.into(), 0.into(), 0.into())
                 , (-sin    , 0.into(), cos     , 0.into())
                 , (0.into(), 0.into(), 0.into(), 1.into()) )
    }
85
86 87 88
    pub fn rotate_z(a :i32) -> Self {
        let sin :T = Trig::sin(a);
        let cos :T = Trig::cos(a);
89
90 91 92 93 94
        Self::new( (cos     , -sin    , 0.into(), 0.into())
                 , (sin     , cos     , 0.into(), 0.into())
                 , (0.into(), 0.into(), 1.into(), 0.into())
                 , (0.into(), 0.into(), 0.into(), 1.into()) )
    }
95
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    pub fn rotate_v(v :&Vector<T>, a :i32) -> Self {
        let Vector(x, y, z) = *v;

        let sin :T = Trig::sin(a);
        let cos :T = Trig::cos(a);

        let zero :T = 0.into();
        let one  :T = 1.into();

        Self::new( ( (one - cos) * x * x + cos
                   , (one - cos) * x * y - sin * z
                   , (one - cos) * x * z + sin * y
                   , zero )
                 , ( (one - cos) * x * y + sin * z
                   , (one - cos) * y * y + cos
                   , (one - cos) * y * z - sin * x
                   , zero )
                 , ( (one - cos) * x * z - sin * y
                   , (one - cos) * y * z + sin * x
                   , (one - cos) * z * z + cos
                   , zero )
                 , (0.into(), 0.into(), 0.into(), 1.into()) )
    }
119
120 121 122 123 124 125 126
    pub fn scale(v :Vector<T>) -> Self {
        let Vector(x, y, z) = v;

        Self::new( (       x, 0.into(), 0.into(), 0.into())
                 , (0.into(),        y, 0.into(), 0.into())
                 , (0.into(), 0.into(),        z, 0.into())
                 , (0.into(), 0.into(), 0.into(), 1.into()) )
127 128
    }
129 130 131
    pub fn combine<I>(mi :I) -> TMatrix<T>
        where I: IntoIterator<Item = TMatrix<T>> {
132
        mi.into_iter().fold(Self::unit(), |acc, x| x * acc)
133 134
    }
Georg Hopp authored
135
    pub fn apply(&self, v :&Vector<T>, w :T) -> (Vector<T>, T) {
136 137 138 139 140 141
        let TMatrix( (a11, a12, a13, a14)
                   , (a21, a22, a23, a24)
                   , (a31, a32, a33, a34)
                   , (a41, a42, a43, a44) ) = *self;
        let Vector(x, y, z) = *v;
Georg Hopp authored
142 143 144 145
        let v = Vector( a11 * x + a12 * y + a13 * z + a14 * w
                      , a21 * x + a22 * y + a23 * z + a24 * w
                      , a31 * x + a32 * y + a33 * z + a34 * w );
        let w = a41 * x + a42 * y + a43 * z + a44 * w;
146
Georg Hopp authored
147 148
        //v.mul(&w.recip())
        (v, w)
149 150 151
    }
}
152 153
impl<T> Mul for TMatrix<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
154 155
       + Mul<Output = T> + Div<Output = T>
       + Debug + Trig + From<i32> + Copy {
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    type Output = Self;

    // ATTENTION: This is not commutative, nor assoziative.
    fn mul(self, other :Self) -> Self {
        let TMatrix( (a11, a12, a13, a14)
                   , (a21, a22, a23, a24)
                   , (a31, a32, a33, a34)
                   , (a41, a42, a43, a44) ) = self;
        let TMatrix( (b11, b12, b13, b14)
                   , (b21, b22, b23, b24)
                   , (b31, b32, b33, b34)
                   , (b41, b42, b43, b44) ) = other;

        TMatrix( ( a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41
                 , a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42
                 , a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43
                 , a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44 )
               , ( a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41
                 , a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42
                 , a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43
                 , a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44 )
               , ( a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41
                 , a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42
                 , a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43
                 , a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44 )
               , ( a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41
                 , a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42
                 , a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43
                 , a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44 ) )
    }
}