gg/m4: fix unnecessary uses of [direct_array_access], add [unsafe] (#9059)

pull/9084/head
Nick Treleaven 2021-03-03 08:20:13 +00:00 committed by GitHub
parent 460e06b9ff
commit bd6693efb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 107 deletions

View File

@ -25,9 +25,7 @@ pub fn deg(grad f32) f32 {
} }
// Calculate the perspective matrix // Calculate the perspective matrix
[direct_array_access]
pub fn perspective(fov f32, ar f32, n f32, f f32) Mat4 { pub fn perspective(fov f32, ar f32, n f32, f f32) Mat4 {
unsafe {
ctan := f32(1.0 / math.tan(fov * (f32(math.pi) / 360.0))) // for the FOV we use 360 instead 180 ctan := f32(1.0 / math.tan(fov * (f32(math.pi) / 360.0))) // for the FOV we use 360 instead 180
return Mat4{ e: [ return Mat4{ e: [
ctan / ar, 0, 0, 0, ctan / ar, 0, 0, 0,
@ -36,13 +34,10 @@ pub fn perspective(fov f32, ar f32, n f32, f f32) Mat4 {
0, 0, (2.0 * n * f) / (n - f), 0, 0, 0, (2.0 * n * f) / (n - f), 0,
]! ]!
} }
}
} }
// Calculate the look-at matrix // Calculate the look-at matrix
[direct_array_access]
pub fn look_at(eye Vec4, center Vec4, up Vec4) Mat4 { pub fn look_at(eye Vec4, center Vec4, up Vec4) Mat4 {
unsafe {
f := (center - eye).normalize3() f := (center - eye).normalize3()
s := (f % up).normalize3() s := (f % up).normalize3()
u := (s % f) u := (s % f)
@ -69,7 +64,6 @@ pub fn look_at(eye Vec4, center Vec4, up Vec4) Mat4 {
/* [3][3] */ 1, /* [3][3] */ 1,
]! ]!
} }
}
} }
/* /*

View File

@ -62,15 +62,12 @@ pub fn (a Mat4) clean() Mat4 {
} }
// Sum all the elements of the matrix // Sum all the elements of the matrix
[direct_array_access]
pub fn (x Mat4) sum_all() f32 { pub fn (x Mat4) sum_all() f32 {
unsafe { mut res := f32(0)
res := f32(0) for v in unsafe { x.e } {
for v in x.e {
res += v res += v
} }
return res return res
}
} }
// Check if two matrix are equal using module precision // Check if two matrix are equal using module precision
@ -118,7 +115,6 @@ pub fn (mut x Mat4) set_f(index_col int, index_row int, value f32) {
} }
// Copy a matrix elements from another matrix // Copy a matrix elements from another matrix
[direct_array_access]
pub fn (mut x Mat4) copy(y Mat4) { pub fn (mut x Mat4) copy(y Mat4) {
unsafe { unsafe {
x.e = [ x.e = [
@ -131,7 +127,6 @@ pub fn (mut x Mat4) copy(y Mat4) {
} }
// Set the trace of the matrix using a vec4 // Set the trace of the matrix using a vec4
[direct_array_access]
pub fn (mut x Mat4) set_trace(v3 Vec4) { pub fn (mut x Mat4) set_trace(v3 Vec4) {
unsafe { unsafe {
x.e[0] = v3.e[0] x.e[0] = v3.e[0]
@ -142,7 +137,6 @@ pub fn (mut x Mat4) set_trace(v3 Vec4) {
} }
// Get the trace of the matrix // Get the trace of the matrix
[direct_array_access]
pub fn (x Mat4) get_trace() Vec4 { pub fn (x Mat4) get_trace() Vec4 {
unsafe { unsafe {
return Vec4{ e: [ x.e[0], x.e[5], x.e[10], x.e[15], ]! } return Vec4{ e: [ x.e[0], x.e[5], x.e[10], x.e[15], ]! }
@ -150,7 +144,6 @@ pub fn (x Mat4) get_trace() Vec4 {
} }
// Set all the matrix elements to value // Set all the matrix elements to value
[direct_array_access]
pub fn (mut x Mat4) set_f32(value f32) { pub fn (mut x Mat4) set_f32(value f32) {
unsafe { unsafe {
x.e = [ x.e = [
@ -167,6 +160,7 @@ pub fn (mut x Mat4) set_f32(value f32) {
//------------------------------------- //-------------------------------------
// Set the row as the input vec4 // Set the row as the input vec4
[direct_array_access] [direct_array_access]
[unsafe]
pub fn (mut x Mat4) set_row(row int, v3 Vec4) { pub fn (mut x Mat4) set_row(row int, v3 Vec4) {
unsafe { unsafe {
x.e[row * 4] = v3.e[0] x.e[row * 4] = v3.e[0]
@ -178,6 +172,7 @@ pub fn (mut x Mat4) set_row(row int, v3 Vec4) {
// Get a row from a matrix // Get a row from a matrix
[direct_array_access] [direct_array_access]
[unsafe]
pub fn (x Mat4) get_row(row int) Vec4 { pub fn (x Mat4) get_row(row int) Vec4 {
unsafe { unsafe {
return Vec4{ return Vec4{
@ -193,6 +188,7 @@ pub fn (x Mat4) get_row(row int) Vec4 {
// Set the column as the input vec4 // Set the column as the input vec4
[direct_array_access] [direct_array_access]
[unsafe]
pub fn (mut x Mat4) set_col(col int, v3 Vec4) { pub fn (mut x Mat4) set_col(col int, v3 Vec4) {
unsafe { unsafe {
x.e[col] = v3.e[0] x.e[col] = v3.e[0]
@ -204,6 +200,7 @@ pub fn (mut x Mat4) set_col(col int, v3 Vec4) {
// Get a column from a matrix // Get a column from a matrix
[direct_array_access] [direct_array_access]
[unsafe]
pub fn (x Mat4) get_col(col int) Vec4 { pub fn (x Mat4) get_col(col int) Vec4 {
unsafe { unsafe {
return Vec4{ return Vec4{
@ -219,6 +216,7 @@ pub fn (x Mat4) get_col(col int) Vec4 {
// Swap two columns in the matrix // Swap two columns in the matrix
[direct_array_access] [direct_array_access]
[unsafe]
pub fn (mut x Mat4) swap_col(col1 int, col2 int) { pub fn (mut x Mat4) swap_col(col1 int, col2 int) {
unsafe { unsafe {
v0 := x.e[col1] v0 := x.e[col1]
@ -240,6 +238,7 @@ pub fn (mut x Mat4) swap_col(col1 int, col2 int) {
// Swap two rows in the matrix // Swap two rows in the matrix
[direct_array_access] [direct_array_access]
[unsafe]
pub fn (mut x Mat4) swap_row(row1 int, row2 int) { pub fn (mut x Mat4) swap_row(row1 int, row2 int) {
unsafe { unsafe {
v0 := x.e[row1 * 4] v0 := x.e[row1 * 4]
@ -263,7 +262,6 @@ pub fn (mut x Mat4) swap_row(row1 int, row2 int) {
// Modify data // Modify data
//------------------------------------- //-------------------------------------
// Transpose the matrix // Transpose the matrix
[direct_array_access]
pub fn (x Mat4) transpose() Mat4 { pub fn (x Mat4) transpose() Mat4 {
unsafe { unsafe {
return Mat4{ e: [ return Mat4{ e: [
@ -277,7 +275,6 @@ pub fn (x Mat4) transpose() Mat4 {
} }
// Multiply the all the elements of the matrix by a scalar // Multiply the all the elements of the matrix by a scalar
[direct_array_access]
pub fn (x Mat4) mul_scalar(s f32) Mat4 { pub fn (x Mat4) mul_scalar(s f32) Mat4 {
unsafe { unsafe {
return Mat4{ e: [ return Mat4{ e: [
@ -296,9 +293,7 @@ pub fn (x Mat4) mul_scalar(s f32) Mat4 {
* *
*********************************************************************/ *********************************************************************/
// Return a zero matrix // Return a zero matrix
[direct_array_access]
pub fn zero_m4() Mat4 { pub fn zero_m4() Mat4 {
unsafe {
return Mat4{ e: [ return Mat4{ e: [
f32(0), 0, 0, 0, f32(0), 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
@ -306,13 +301,10 @@ pub fn zero_m4() Mat4 {
0, 0, 0, 0, 0, 0, 0, 0,
]! ]!
} }
}
} }
// Return a unity matrix // Return a unity matrix
[direct_array_access]
pub fn unit_m4() Mat4 { pub fn unit_m4() Mat4 {
unsafe {
return Mat4{ e: [ return Mat4{ e: [
f32(1), 0, 0, 0, f32(1), 0, 0, 0,
0, 1, 0, 0, 0, 1, 0, 0,
@ -320,11 +312,9 @@ pub fn unit_m4() Mat4 {
0, 0, 0, 1, 0, 0, 0, 1,
]! ]!
} }
}
} }
// Return a matrix initialized with value // Return a matrix initialized with value
[direct_array_access]
pub fn set_m4(value f32) Mat4 { pub fn set_m4(value f32) Mat4 {
return Mat4{ e: [ return Mat4{ e: [
value, value, value, value, value, value, value, value,
@ -342,7 +332,6 @@ pub fn set_m4(value f32) Mat4 {
*********************************************************************/ *********************************************************************/
// Sum of matrix, operator + // Sum of matrix, operator +
[direct_array_access]
pub fn (a Mat4) + (b Mat4) Mat4 { pub fn (a Mat4) + (b Mat4) Mat4 {
unsafe { unsafe {
return Mat4{ e: [ return Mat4{ e: [
@ -356,7 +345,6 @@ pub fn (a Mat4) + (b Mat4) Mat4 {
} }
// Subtraction of matrix, operator - // Subtraction of matrix, operator -
[direct_array_access]
pub fn (a Mat4) - (b Mat4) Mat4 { pub fn (a Mat4) - (b Mat4) Mat4 {
unsafe { unsafe {
return Mat4{ e: [ return Mat4{ e: [
@ -370,7 +358,6 @@ pub fn (a Mat4) - (b Mat4) Mat4 {
} }
// Multiplication of matrix, operator * // Multiplication of matrix, operator *
[direct_array_access]
pub fn (a Mat4) * (b Mat4) Mat4 { pub fn (a Mat4) * (b Mat4) Mat4 {
unsafe { unsafe {
return Mat4{ return Mat4{
@ -421,7 +408,6 @@ pub fn mul(a Mat4, b Mat4) Mat4 {
} }
// Multiply a Matrix by a vector // Multiply a Matrix by a vector
[direct_array_access]
pub fn mul_vec(a Mat4, v Vec4) Vec4 { pub fn mul_vec(a Mat4, v Vec4) Vec4 {
unsafe { unsafe {
return Vec4{ e: [ return Vec4{ e: [
@ -435,7 +421,6 @@ pub fn mul_vec(a Mat4, v Vec4) Vec4 {
} }
// Calculate the determinant of the Matrix // Calculate the determinant of the Matrix
[direct_array_access]
pub fn det(x Mat4) f32 { pub fn det(x Mat4) f32 {
unsafe { unsafe {
mut t := [6]f32{} mut t := [6]f32{}
@ -472,7 +457,6 @@ pub fn det(x Mat4) f32 {
} }
// Calculate the inverse of the Matrix // Calculate the inverse of the Matrix
[direct_array_access]
pub fn (x Mat4) inverse() Mat4 { pub fn (x Mat4) inverse() Mat4 {
unsafe { unsafe {
mut t := [6]f32{} mut t := [6]f32{}
@ -538,11 +522,9 @@ pub fn (x Mat4) inverse() Mat4 {
dest.f[3][3] = a * t[2] - b * t[4] + c * t[5] dest.f[3][3] = a * t[2] - b * t[4] + c * t[5]
tmp := (a * dest.f[0][0] + b * dest.f[0][1] + c * dest.f[0][2] + d * dest.f[0][3]) tmp := (a * dest.f[0][0] + b * dest.f[0][1] + c * dest.f[0][2] + d * dest.f[0][3])
if tmp != 0 { if tmp != 0 {
det = f32(1.0) / tmp det = f32(1.0) / tmp
} }
return dest.mul_scalar(det) return dest.mul_scalar(det)
} }
} }
@ -554,13 +536,12 @@ pub fn (x Mat4) inverse() Mat4 {
*********************************************************************/ *********************************************************************/
// Get a rotation matrix using w as rotation axis vector, the angle is in radians // Get a rotation matrix using w as rotation axis vector, the angle is in radians
[direct_array_access]
pub fn rotate(angle f32, w Vec4) Mat4 { pub fn rotate(angle f32, w Vec4) Mat4 {
unsafe {
cs := f32(math.cos(angle)) cs := f32(math.cos(angle))
sn := f32(math.sin(angle)) sn := f32(math.sin(angle))
cv := f32(1.0) - cs cv := f32(1.0) - cs
axis := w.normalize3() axis := w.normalize3()
unsafe {
ax := axis.e[0] ax := axis.e[0]
ay := axis.e[1] ay := axis.e[1]
az := axis.e[2] az := axis.e[2]
@ -596,7 +577,6 @@ pub fn rotate(angle f32, w Vec4) Mat4 {
* *
*********************************************************************/ *********************************************************************/
// Get a matrix translated by a vector w // Get a matrix translated by a vector w
[direct_array_access]
pub fn (x Mat4) translate(w Vec4) Mat4 { pub fn (x Mat4) translate(w Vec4) Mat4 {
unsafe { unsafe {
return Mat4{ e: [ return Mat4{ e: [
@ -610,7 +590,6 @@ pub fn (x Mat4) translate(w Vec4) Mat4 {
} }
// Get a scale matrix, the scale vector is w, only xyz are evaluated. // Get a scale matrix, the scale vector is w, only xyz are evaluated.
[direct_array_access]
pub fn scale(w Vec4) Mat4 { pub fn scale(w Vec4) Mat4 {
unsafe { unsafe {
return Mat4{ e: [ return Mat4{ e: [

View File

@ -29,8 +29,7 @@ pub fn (x Vec4) str() string {
// Remove all the raw zeros // Remove all the raw zeros
[direct_array_access] [direct_array_access]
pub fn (a Vec4) clean() Vec4 { pub fn (a Vec4) clean() Vec4 {
unsafe { mut x := Vec4{}
x := Vec4{}
for c, value in a.e { for c, value in a.e {
if abs(value) < precision { if abs(value) < precision {
x.e[c] = 0 x.e[c] = 0
@ -39,23 +38,19 @@ pub fn (a Vec4) clean() Vec4 {
} }
} }
return x return x
}
} }
// Set all elements to value // Set all elements to value
[direct_array_access]
pub fn (mut x Vec4) copy(value f32) { pub fn (mut x Vec4) copy(value f32) {
x.e = [ value, value, value, value, ]! x.e = [ value, value, value, value, ]!
} }
// Scale the vector using a scalar // Scale the vector using a scalar
[direct_array_access]
pub fn (x Vec4) mul_scalar(value f32) Vec4 { pub fn (x Vec4) mul_scalar(value f32) Vec4 {
return Vec4{ e: [ x.e[0] * value, x.e[1] * value, x.e[2] * value, x.e[3] * value, ]! } return Vec4{ e: [ x.e[0] * value, x.e[1] * value, x.e[2] * value, x.e[3] * value, ]! }
} }
// Reciprocal of the vector // Reciprocal of the vector
[direct_array_access]
pub fn (x Vec4) inv() Vec4 { pub fn (x Vec4) inv() Vec4 {
return Vec4{ return Vec4{
e: [ e: [
@ -68,7 +63,6 @@ pub fn (x Vec4) inv() Vec4 {
} }
// Normalize the vector // Normalize the vector
[direct_array_access]
pub fn (x Vec4) normalize() Vec4 { pub fn (x Vec4) normalize() Vec4 {
m := x.mod() m := x.mod()
if m == 0 { if m == 0 {
@ -85,7 +79,6 @@ pub fn (x Vec4) normalize() Vec4 {
} }
// Normalize only xyz, w set to 0 // Normalize only xyz, w set to 0
[direct_array_access]
pub fn (x Vec4) normalize3() Vec4 { pub fn (x Vec4) normalize3() Vec4 {
m := x.mod3() m := x.mod3()
if m == 0 { if m == 0 {
@ -102,13 +95,11 @@ pub fn (x Vec4) normalize3() Vec4 {
} }
// Module of the vector xyzw // Module of the vector xyzw
[direct_array_access]
pub fn (x Vec4) mod() f32 { pub fn (x Vec4) mod() f32 {
return f32(math.sqrt(x.e[0] * x.e[0] + x.e[1] * x.e[1] + x.e[2] * x.e[2] + x.e[3] * x.e[3])) return f32(math.sqrt(x.e[0] * x.e[0] + x.e[1] * x.e[1] + x.e[2] * x.e[2] + x.e[3] * x.e[3]))
} }
// Module for 3d vector xyz, w ignored // Module for 3d vector xyz, w ignored
[direct_array_access]
pub fn (x Vec4) mod3() f32 { pub fn (x Vec4) mod3() f32 {
return f32(math.sqrt(x.e[0] * x.e[0] + x.e[1] * x.e[1] + x.e[2] * x.e[2])) return f32(math.sqrt(x.e[0] * x.e[0] + x.e[1] * x.e[1] + x.e[2] * x.e[2]))
} }
@ -119,7 +110,6 @@ pub fn (x Vec4) mod3() f32 {
* *
*********************************************************************/ *********************************************************************/
// Return a zero vector // Return a zero vector
[direct_array_access]
pub fn zero_v4() Vec4 { pub fn zero_v4() Vec4 {
return Vec4{ return Vec4{
e: [ e: [
@ -132,7 +122,6 @@ pub fn zero_v4() Vec4 {
} }
// Return all one vector // Return all one vector
[direct_array_access]
pub fn one_v4() Vec4 { pub fn one_v4() Vec4 {
return Vec4{ return Vec4{
e: [ e: [
@ -145,7 +134,6 @@ pub fn one_v4() Vec4 {
} }
// Return a blank vector // Return a blank vector
[direct_array_access]
pub fn blank_v4() Vec4 { pub fn blank_v4() Vec4 {
return Vec4{ return Vec4{
e: [ e: [
@ -158,7 +146,6 @@ pub fn blank_v4() Vec4 {
} }
// Set all elements to value // Set all elements to value
[direct_array_access]
pub fn set_v4(value f32) Vec4 { pub fn set_v4(value f32) Vec4 {
return Vec4{ return Vec4{
e: [ e: [
@ -171,7 +158,6 @@ pub fn set_v4(value f32) Vec4 {
} }
// Sum of all the elements // Sum of all the elements
[direct_array_access]
pub fn (x Vec4) sum() f32 { pub fn (x Vec4) sum() f32 {
return x.e[0] + x.e[1] + x.e[2] + x.e[3] return x.e[0] + x.e[1] + x.e[2] + x.e[3]
} }
@ -182,7 +168,6 @@ pub fn (x Vec4) sum() f32 {
* *
*********************************************************************/ *********************************************************************/
// Addition // Addition
[direct_array_access]
pub fn (a Vec4) + (b Vec4) Vec4 { pub fn (a Vec4) + (b Vec4) Vec4 {
return Vec4{ return Vec4{
e: [ e: [
@ -195,7 +180,6 @@ pub fn (a Vec4) + (b Vec4) Vec4 {
} }
// Subtraction // Subtraction
[direct_array_access]
pub fn (a Vec4) - (b Vec4) Vec4 { pub fn (a Vec4) - (b Vec4) Vec4 {
return Vec4{ return Vec4{
e: [ e: [
@ -208,13 +192,11 @@ pub fn (a Vec4) - (b Vec4) Vec4 {
} }
// Dot product // Dot product
[direct_array_access]
pub fn (a Vec4) * (b Vec4) f32 { pub fn (a Vec4) * (b Vec4) f32 {
return a.e[0] * b.e[0] + a.e[1] * b.e[1] + a.e[2] * b.e[2] + a.e[3] * b.e[3] return a.e[0] * b.e[0] + a.e[1] * b.e[1] + a.e[2] * b.e[2] + a.e[3] * b.e[3]
} }
// Cross product // Cross product
[direct_array_access]
pub fn (a Vec4) % (b Vec4) Vec4 { pub fn (a Vec4) % (b Vec4) Vec4 {
return Vec4{ return Vec4{
e: [ e: [
@ -227,7 +209,6 @@ pub fn (a Vec4) % (b Vec4) Vec4 {
} }
// Components multiplication // Components multiplication
[direct_array_access]
pub fn (x Vec4) mul_vec4(y Vec4) Vec4 { pub fn (x Vec4) mul_vec4(y Vec4) Vec4 {
return Vec4{ return Vec4{
e: [ e: [