1
1
use std:: f64:: consts:: PI ;
2
2
use std:: ops:: Mul ;
3
3
4
+ /// The projection matrix which is used to project the 3D space to the 2D display panel
4
5
#[ derive( Clone , Debug , Copy ) ]
5
6
pub struct ProjectionMatrix ( [ [ f64 ; 4 ] ; 4 ] ) ;
6
7
@@ -68,6 +69,7 @@ impl Mul<(f64, f64, f64)> for ProjectionMatrix {
68
69
}
69
70
70
71
impl ProjectionMatrix {
72
+ /// Returns the identity matrix
71
73
pub fn one ( ) -> Self {
72
74
ProjectionMatrix ( [
73
75
[ 1.0 , 0.0 , 0.0 , 0.0 ] ,
@@ -76,9 +78,11 @@ impl ProjectionMatrix {
76
78
[ 0.0 , 0.0 , 0.0 , 1.0 ] ,
77
79
] )
78
80
}
81
+ /// Returns the zero maxtrix
79
82
pub fn zero ( ) -> Self {
80
83
ProjectionMatrix ( [ [ 0.0 ; 4 ] ; 4 ] )
81
84
}
85
+ /// Returns the matrix which shift the coordinate
82
86
pub fn shift ( x : f64 , y : f64 , z : f64 ) -> Self {
83
87
ProjectionMatrix ( [
84
88
[ 1.0 , 0.0 , 0.0 , x] ,
@@ -87,6 +91,7 @@ impl ProjectionMatrix {
87
91
[ 0.0 , 0.0 , 0.0 , 1.0 ] ,
88
92
] )
89
93
}
94
+ /// Returns the matrix which rotates the coordinate
90
95
pub fn rotate ( x : f64 , y : f64 , z : f64 ) -> Self {
91
96
let ( c, b, a) = ( x, y, z) ;
92
97
ProjectionMatrix ( [
@@ -106,6 +111,7 @@ impl ProjectionMatrix {
106
111
[ 0.0 , 0.0 , 0.0 , 1.0 ] ,
107
112
] )
108
113
}
114
+ /// Returns the matrix that applies a scale factor
109
115
pub fn scale ( factor : f64 ) -> Self {
110
116
ProjectionMatrix ( [
111
117
[ 1.0 , 0.0 , 0.0 , 0.0 ] ,
@@ -114,6 +120,7 @@ impl ProjectionMatrix {
114
120
[ 0.0 , 0.0 , 0.0 , 1.0 / factor] ,
115
121
] )
116
122
}
123
+ /// Normalize the matrix, this will make the metric unit to 1
117
124
pub fn normalize ( & mut self ) {
118
125
if self . 0 [ 3 ] [ 3 ] > 1e-20 {
119
126
for r in 0 ..4 {
@@ -124,12 +131,14 @@ impl ProjectionMatrix {
124
131
}
125
132
}
126
133
134
+ /// Get the distance of the point in guest coordinate from the screen in pixels
127
135
pub fn projected_depth ( & self , ( x, y, z) : ( i32 , i32 , i32 ) ) -> i32 {
128
136
let r = & self . 0 [ 2 ] ;
129
137
( r[ 0 ] * x as f64 + r[ 1 ] * y as f64 + r[ 2 ] * z as f64 + r[ 3 ] ) as i32
130
138
}
131
139
}
132
140
141
+ /// The helper struct to build a projection matrix
133
142
#[ derive( Copy , Clone ) ]
134
143
pub struct ProjectionMatrixBuilder {
135
144
pub yaw : f64 ,
@@ -150,12 +159,15 @@ impl ProjectionMatrixBuilder {
150
159
}
151
160
}
152
161
162
+ /// Set the pivot point, which means the 3D coordinate "before" should be mapped into
163
+ /// the 2D coordinatet "after"
153
164
pub fn set_pivot ( & mut self , before : ( i32 , i32 , i32 ) , after : ( i32 , i32 ) ) -> & mut Self {
154
165
self . pivot_before = before;
155
166
self . pivot_after = after;
156
167
self
157
168
}
158
169
170
+ /// Build the matrix based on the configuration
159
171
pub fn into_matrix ( self ) -> ProjectionMatrix {
160
172
let mut ret = if self . pivot_before == ( 0 , 0 , 0 ) {
161
173
ProjectionMatrix :: default ( )
0 commit comments