Commit 8274976de63456d92a2048f8191e2a738ff7e8ca

Authored by Georg Hopp
1 parent d522cf78

Add basic geometric transformations

@@ -23,5 +23,8 @@ extern crate lazy_static; @@ -23,5 +23,8 @@ extern crate lazy_static;
23 pub mod fractional; 23 pub mod fractional;
24 pub mod trigonometry; 24 pub mod trigonometry;
25 pub mod vector; 25 pub mod vector;
  26 +pub mod transform;
26 27
27 use fractional::{Fractional}; 28 use fractional::{Fractional};
  29 +use trigonometry::{sin, cos};
  30 +use vector::{Vector};
@@ -25,6 +25,7 @@ use std::f64::consts::PI as FPI; @@ -25,6 +25,7 @@ use std::f64::consts::PI as FPI;
25 use fractional::fractional::{Fractional, from_vector, Continuous}; 25 use fractional::fractional::{Fractional, from_vector, Continuous};
26 use fractional::trigonometry::{sin, cos, tan, PI}; 26 use fractional::trigonometry::{sin, cos, tan, PI};
27 use fractional::vector::{Vector}; 27 use fractional::vector::{Vector};
  28 +use fractional::transform::{translate, rotate_x, rotate_y, rotate_z, rotate_v};
28 29
29 fn mean(v: &Vec<i64>) -> Result<Fractional, TryFromIntError> { 30 fn mean(v: &Vec<i64>) -> Result<Fractional, TryFromIntError> {
30 let r = v.iter().fold(0, |acc, x| acc + x); 31 let r = v.iter().fold(0, |acc, x| acc + x);
@@ -148,6 +149,55 @@ fn _vector() { @@ -148,6 +149,55 @@ fn _vector() {
148 println!("{:>14} : {}", "v2 * v1", v2 * v1); 149 println!("{:>14} : {}", "v2 * v1", v2 * v1);
149 } 150 }
150 151
  152 +fn _transform() {
  153 + let v = Vector(1.into(), 1.into(), 1.into());
  154 + let v1 = Vector(1.into(), 2.into(), 3.into());
  155 + let mt = translate(v);
  156 +
  157 + println!("{:>14} : {}", "Vector v1", v1);
  158 + println!("{:>14} : {}", "translate v1", mt.apply(&v1));
  159 + println!();
  160 +
  161 + let v2 = Vector(1.into(), 1.into(), 0.into());
  162 + println!("{:>14} : {}", "Vector v2", v2);
  163 + for d in [ 30, 45, 60, 90, 120, 135, 150, 180
  164 + , 210, 225, 240, 270, 300, 315, 330 ].iter() {
  165 + let m = rotate_x(*d as i32);
  166 + println!("{:>14} : {}", format!("rot_x {} v2", d), m.apply(&v2));
  167 + }
  168 + println!();
  169 +
  170 + println!("{:>14} : {}", "Vector v2", v2);
  171 + for d in [ 30, 45, 60, 90, 120, 135, 150, 180
  172 + , 210, 225, 240, 270, 300, 315, 330 ].iter() {
  173 + let m = rotate_y(*d as i32);
  174 + println!("{:>14} : {}", format!("rot_y {} v2", d), m.apply(&v2));
  175 + }
  176 + println!();
  177 +
  178 + for d in [ 30, 45, 60, 90, 120, 135, 150, 180
  179 + , 210, 225, 240, 270, 300, 315, 330 ].iter() {
  180 + let m = rotate_x(*d as i32) * rotate_y(*d as i32);
  181 + println!("{:>14} : {}", format!("rot_xy {} v2", d), m.apply(&v2));
  182 + }
  183 + println!();
  184 +
  185 + let v3 = Vector(1.into(), 0.into(), 1.into());
  186 + println!("{:>14} : {}", "Vector v3", v3);
  187 + for d in [ 30, 45, 60, 90, 120, 135, 150, 180
  188 + , 210, 225, 240, 270, 300, 315, 330 ].iter() {
  189 + let m = rotate_z(*d as i32);
  190 + println!("{:>14} : {}", format!("rot_z {} v3", d), m.apply(&v3));
  191 + }
  192 + println!();
  193 +
  194 + for d in [ 30, 45, 60, 90, 120, 135, 150, 180
  195 + , 210, 225, 240, 270, 300, 315, 330 ].iter() {
  196 + let m = rotate_v(&v, *d as i32);
  197 + println!("{:>14} : {}", format!("rot_v {} v2", d), m.apply(&v2));
  198 + }
  199 +}
  200 +
151 fn main() { 201 fn main() {
152 common_fractional(); 202 common_fractional();
153 println!(); 203 println!();
@@ -164,4 +214,6 @@ fn main() { @@ -164,4 +214,6 @@ fn main() {
164 _tan(); 214 _tan();
165 println!(); 215 println!();
166 _vector(); 216 _vector();
  217 + println!();
  218 + _transform();
167 } 219 }
  1 +//
  2 +// Transformation of vectors in a given coordinate system...
  3 +//
  4 +// Georg Hopp <georg@steffers.org>
  5 +//
  6 +// Copyright © 2019 Georg Hopp
  7 +//
  8 +// This program is free software: you can redistribute it and/or modify
  9 +// it under the terms of the GNU General Public License as published by
  10 +// the Free Software Foundation, either version 3 of the License, or
  11 +// (at your option) any later version.
  12 +//
  13 +// This program is distributed in the hope that it will be useful,
  14 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16 +// GNU General Public License for more details.
  17 +//
  18 +// You should have received a copy of the GNU General Public License
  19 +// along with this program. If not, see <http://www.gnu.org/licenses/>.
  20 +//
  21 +use std::ops::{Mul};
  22 +use crate::{Fractional, cos, sin, Vector};
  23 +
  24 +pub struct TMatrix( (Fractional, Fractional, Fractional, Fractional)
  25 + , (Fractional, Fractional, Fractional, Fractional)
  26 + , (Fractional, Fractional, Fractional, Fractional)
  27 + , (Fractional, Fractional, Fractional, Fractional) );
  28 +
  29 +pub fn translate(v :Vector) -> TMatrix {
  30 + let Vector(x, y, z) = v;
  31 +
  32 + TMatrix( (1.into(), 0.into(), 0.into(), 0.into())
  33 + , (0.into(), 1.into(), 0.into(), 0.into())
  34 + , (0.into(), 0.into(), 1.into(), 0.into())
  35 + , ( x, y, z, 1.into()) )
  36 +}
  37 +
  38 +pub fn rotate_x(a :i32) -> TMatrix {
  39 + TMatrix( (1.into(), 0.into(), 0.into(), 0.into())
  40 + , (0.into(), cos(a), -sin(a), 0.into())
  41 + , (0.into(), sin(a), cos(a), 0.into())
  42 + , (0.into(), 0.into(), 0.into(), 1.into()) )
  43 +}
  44 +
  45 +pub fn rotate_y(a :i32) -> TMatrix {
  46 + TMatrix( ( cos(a), 0.into(), sin(a), 0.into())
  47 + , (0.into(), 1.into(), 0.into(), 0.into())
  48 + , ( -sin(a), 0.into(), cos(a), 0.into())
  49 + , (0.into(), 0.into(), 0.into(), 1.into()) )
  50 +}
  51 +
  52 +pub fn rotate_z(a :i32) -> TMatrix {
  53 + TMatrix( ( cos(a), -sin(a), 0.into(), 0.into())
  54 + , ( sin(a), cos(a), 0.into(), 0.into())
  55 + , (0.into(), 0.into(), 1.into(), 0.into())
  56 + , (0.into(), 0.into(), 0.into(), 1.into()) )
  57 +}
  58 +
  59 +pub fn rotate_v(v :&Vector, a :i32) -> TMatrix {
  60 + let Vector(x, y, z) = *v;
  61 +
  62 + let zero :Fractional = 0.into();
  63 + let one :Fractional = 1.into();
  64 +
  65 + TMatrix( ( (one - cos(a)) * x * x + cos(a)
  66 + , (one - cos(a)) * x * y - sin(a) * z
  67 + , (one - cos(a)) * x * z + sin(a) * y
  68 + , zero )
  69 + , ( (one - cos(a)) * x * y + sin(a) * z
  70 + , (one - cos(a)) * y * y + cos(a)
  71 + , (one - cos(a)) * y * z - sin(a) * x
  72 + , zero )
  73 + , ( (one - cos(a)) * x * z - sin(a) * y
  74 + , (one - cos(a)) * y * z + sin(a) * x
  75 + , (one - cos(a)) * z * z + cos(a)
  76 + , zero )
  77 + , (0.into(), 0.into(), 0.into(), 1.into()) )
  78 +}
  79 +
  80 +pub fn scale(v :Vector) -> TMatrix {
  81 + let Vector(x, y, z) = v;
  82 +
  83 + TMatrix( ( x, 0.into(), 0.into(), 0.into())
  84 + , (0.into(), y, 0.into(), 0.into())
  85 + , (0.into(), 0.into(), z, 0.into())
  86 + , (0.into(), 0.into(), 0.into(), 1.into()) )
  87 +}
  88 +
  89 +impl TMatrix {
  90 + pub fn apply(&self, v :&Vector) -> Vector {
  91 + let TMatrix( (a11, a12, a13, a14)
  92 + , (a21, a22, a23, a24)
  93 + , (a31, a32, a33, a34)
  94 + , (a41, a42, a43, a44) ) = *self;
  95 + let Vector(x, y, z) = *v;
  96 +
  97 + let v = Vector( a11 * x + a21 * y + a31 * z + a41
  98 + , a12 * x + a22 * y + a32 * z + a42
  99 + , a13 * x + a23 * y + a33 * z + a43 );
  100 + let Fractional(wn, wd) = a14 * x + a24 * y + a34 * z + a44;
  101 +
  102 + v.mul(&Fractional(wd, wn))
  103 + }
  104 +}
  105 +
  106 +impl Mul for TMatrix {
  107 + type Output = Self;
  108 +
  109 + // ATTENTION: This is not commutative, nor assoziative.
  110 + fn mul(self, other :Self) -> Self {
  111 + let TMatrix( (a11, a12, a13, a14)
  112 + , (a21, a22, a23, a24)
  113 + , (a31, a32, a33, a34)
  114 + , (a41, a42, a43, a44) ) = self;
  115 + let TMatrix( (b11, b12, b13, b14)
  116 + , (b21, b22, b23, b24)
  117 + , (b31, b32, b33, b34)
  118 + , (b41, b42, b43, b44) ) = other;
  119 +
  120 + TMatrix( ( a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41
  121 + , a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42
  122 + , a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43
  123 + , a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44 )
  124 + , ( a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41
  125 + , a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42
  126 + , a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43
  127 + , a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44 )
  128 + , ( a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41
  129 + , a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42
  130 + , a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43
  131 + , a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44 )
  132 + , ( a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41
  133 + , a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42
  134 + , a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43
  135 + , a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44 ) )
  136 + }
  137 +}
Please register or login to post a comment