11use std:: f64:: consts:: PI ;
22use std:: ops:: Mul ;
33
4+ /// The projection matrix which is used to project the 3D space to the 2D display panel
45#[ derive( Clone , Debug , Copy ) ]
56pub struct ProjectionMatrix ( [ [ f64 ; 4 ] ; 4 ] ) ;
67
@@ -68,6 +69,7 @@ impl Mul<(f64, f64, f64)> for ProjectionMatrix {
6869}
6970
7071impl ProjectionMatrix {
72+ /// Returns the identity matrix
7173 pub fn one ( ) -> Self {
7274 ProjectionMatrix ( [
7375 [ 1.0 , 0.0 , 0.0 , 0.0 ] ,
@@ -76,9 +78,11 @@ impl ProjectionMatrix {
7678 [ 0.0 , 0.0 , 0.0 , 1.0 ] ,
7779 ] )
7880 }
81+ /// Returns the zero maxtrix
7982 pub fn zero ( ) -> Self {
8083 ProjectionMatrix ( [ [ 0.0 ; 4 ] ; 4 ] )
8184 }
85+ /// Returns the matrix which shift the coordinate
8286 pub fn shift ( x : f64 , y : f64 , z : f64 ) -> Self {
8387 ProjectionMatrix ( [
8488 [ 1.0 , 0.0 , 0.0 , x] ,
@@ -87,6 +91,7 @@ impl ProjectionMatrix {
8791 [ 0.0 , 0.0 , 0.0 , 1.0 ] ,
8892 ] )
8993 }
94+ /// Returns the matrix which rotates the coordinate
9095 pub fn rotate ( x : f64 , y : f64 , z : f64 ) -> Self {
9196 let ( c, b, a) = ( x, y, z) ;
9297 ProjectionMatrix ( [
@@ -106,6 +111,7 @@ impl ProjectionMatrix {
106111 [ 0.0 , 0.0 , 0.0 , 1.0 ] ,
107112 ] )
108113 }
114+ /// Returns the matrix that applies a scale factor
109115 pub fn scale ( factor : f64 ) -> Self {
110116 ProjectionMatrix ( [
111117 [ 1.0 , 0.0 , 0.0 , 0.0 ] ,
@@ -114,6 +120,7 @@ impl ProjectionMatrix {
114120 [ 0.0 , 0.0 , 0.0 , 1.0 / factor] ,
115121 ] )
116122 }
123+ /// Normalize the matrix, this will make the metric unit to 1
117124 pub fn normalize ( & mut self ) {
118125 if self . 0 [ 3 ] [ 3 ] > 1e-20 {
119126 for r in 0 ..4 {
@@ -124,12 +131,14 @@ impl ProjectionMatrix {
124131 }
125132 }
126133
134+ /// Get the distance of the point in guest coordinate from the screen in pixels
127135 pub fn projected_depth ( & self , ( x, y, z) : ( i32 , i32 , i32 ) ) -> i32 {
128136 let r = & self . 0 [ 2 ] ;
129137 ( r[ 0 ] * x as f64 + r[ 1 ] * y as f64 + r[ 2 ] * z as f64 + r[ 3 ] ) as i32
130138 }
131139}
132140
141+ /// The helper struct to build a projection matrix
133142#[ derive( Copy , Clone ) ]
134143pub struct ProjectionMatrixBuilder {
135144 pub yaw : f64 ,
@@ -150,12 +159,15 @@ impl ProjectionMatrixBuilder {
150159 }
151160 }
152161
162+ /// Set the pivot point, which means the 3D coordinate "before" should be mapped into
163+ /// the 2D coordinatet "after"
153164 pub fn set_pivot ( & mut self , before : ( i32 , i32 , i32 ) , after : ( i32 , i32 ) ) -> & mut Self {
154165 self . pivot_before = before;
155166 self . pivot_after = after;
156167 self
157168 }
158169
170+ /// Build the matrix based on the configuration
159171 pub fn into_matrix ( self ) -> ProjectionMatrix {
160172 let mut ret = if self . pivot_before == ( 0 , 0 , 0 ) {
161173 ProjectionMatrix :: default ( )
0 commit comments