matrix.rs (4765B)
1 // qcms 2 // Copyright (C) 2009 Mozilla Foundation 3 // Copyright (C) 1998-2007 Marti Maria 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining 6 // a copy of this software and associated documentation files (the "Software"), 7 // to deal in the Software without restriction, including without limitation 8 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 // and/or sell copies of the Software, and to permit persons to whom the Software 10 // is furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in 13 // all copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 17 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 23 #[derive(Copy, Clone, Debug, Default)] 24 pub struct Matrix { 25 pub m: [[f32; 3]; 3], // Three rows of three elems. 26 } 27 28 #[derive(Copy, Clone)] 29 pub struct Vector { 30 pub v: [f32; 3], 31 } 32 33 impl Matrix { 34 pub fn eval(&self, v: Vector) -> Vector { 35 let mut result: Vector = Vector { v: [0.; 3] }; 36 result.v[0] = self.m[0][0] * v.v[0] + self.m[0][1] * v.v[1] + self.m[0][2] * v.v[2]; 37 result.v[1] = self.m[1][0] * v.v[0] + self.m[1][1] * v.v[1] + self.m[1][2] * v.v[2]; 38 result.v[2] = self.m[2][0] * v.v[0] + self.m[2][1] * v.v[1] + self.m[2][2] * v.v[2]; 39 result 40 } 41 42 pub fn row(&self, r: usize) -> [f32; 3] { 43 self.m[r] 44 } 45 46 //probably reuse this computation in matrix_invert 47 pub fn det(&self) -> f32 { 48 let det: f32 = self.m[0][0] * self.m[1][1] * self.m[2][2] 49 + self.m[0][1] * self.m[1][2] * self.m[2][0] 50 + self.m[0][2] * self.m[1][0] * self.m[2][1] 51 - self.m[0][0] * self.m[1][2] * self.m[2][1] 52 - self.m[0][1] * self.m[1][0] * self.m[2][2] 53 - self.m[0][2] * self.m[1][1] * self.m[2][0]; 54 det 55 } 56 /* from pixman and cairo and Mathematics for Game Programmers */ 57 /* lcms uses gauss-jordan elimination with partial pivoting which is 58 * less efficient and not as numerically stable. See Mathematics for 59 * Game Programmers. */ 60 pub fn invert(&self) -> Option<Matrix> { 61 let mut dest_mat: Matrix = Matrix { m: [[0.; 3]; 3] }; 62 let mut i: i32; 63 64 const a: [i32; 3] = [2, 2, 1]; 65 const b: [i32; 3] = [1, 0, 0]; 66 /* inv (A) = 1/det (A) * adj (A) */ 67 let mut det: f32 = self.det(); 68 if det == 0. { 69 return None; 70 } 71 det = 1. / det; 72 let mut j: i32 = 0; 73 while j < 3 { 74 i = 0; 75 while i < 3 { 76 let ai: i32 = a[i as usize]; 77 let aj: i32 = a[j as usize]; 78 let bi: i32 = b[i as usize]; 79 let bj: i32 = b[j as usize]; 80 let mut p: f64 = (self.m[ai as usize][aj as usize] 81 * self.m[bi as usize][bj as usize] 82 - self.m[ai as usize][bj as usize] * self.m[bi as usize][aj as usize]) 83 as f64; 84 if ((i + j) & 1) != 0 { 85 p = -p 86 } 87 dest_mat.m[j as usize][i as usize] = (det as f64 * p) as f32; 88 i += 1 89 } 90 j += 1 91 } 92 Some(dest_mat) 93 } 94 pub fn identity() -> Matrix { 95 let mut i: Matrix = Matrix { m: [[0.; 3]; 3] }; 96 i.m[0][0] = 1.; 97 i.m[0][1] = 0.; 98 i.m[0][2] = 0.; 99 i.m[1][0] = 0.; 100 i.m[1][1] = 1.; 101 i.m[1][2] = 0.; 102 i.m[2][0] = 0.; 103 i.m[2][1] = 0.; 104 i.m[2][2] = 1.; 105 i 106 } 107 pub fn invalid() -> Option<Matrix> { 108 None 109 } 110 /* from pixman */ 111 /* MAT3per... */ 112 pub fn multiply(a: Matrix, b: Matrix) -> Matrix { 113 let mut result: Matrix = Matrix { m: [[0.; 3]; 3] }; 114 let mut dx: i32; 115 116 let mut o: i32; 117 let mut dy: i32 = 0; 118 while dy < 3 { 119 dx = 0; 120 while dx < 3 { 121 let mut v: f64 = 0f64; 122 o = 0; 123 while o < 3 { 124 v += (a.m[dy as usize][o as usize] * b.m[o as usize][dx as usize]) as f64; 125 o += 1 126 } 127 result.m[dy as usize][dx as usize] = v as f32; 128 dx += 1 129 } 130 dy += 1 131 } 132 result 133 } 134 }