ベクトルを回転させ、結果を常に正の x、y、z 平面に保持する次のコードがあります。System.Numerics タイプの Vector3 と Matrix4x4 を使用するようにコードをリファクタリングしたいと考えています。誰かが翻訳を手伝ってくれませんか。
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rotation As Vector = New Vector(45, 0, -90)
Dim delta As Vector = New Vector(10, 10, 10)
Dim result As Vector = InverseVector(delta, rotation)
End Sub
Private Function InverseVector(ByVal _delta As Vector, ByVal _rotation As Vector) As Vector
Dim vChange As New Vector(0, 0, 0)
Dim matX(2, 2) As Single
Dim matY(2, 2) As Single
Dim matZ(2, 2) As Single
Dim negativeFactor As Int32 = 1
Dim dDeterminate As Single
Dim matAdjoin(2, 2) As Single
Dim matTranspose(2, 2) As Single
Dim matInverse(2, 2) As Single
If _delta.X < 0 Or _delta.Y < 0 Or _delta.Z < 0 Then
negativeFactor = -1
End If
Dim dRadians As Single
dRadians = 0.0174532D * _rotation.X
'Load the X Matrix
matX(0, 0) = 1
matX(0, 1) = 0
matX(0, 2) = 0
matX(1, 0) = 0
matX(1, 1) = CDec(Math.Round(Math.Cos(dRadians), 4))
matX(1, 2) = CDec(Math.Round(Math.Sin(dRadians), 4) * -1)
matX(2, 0) = 0
matX(2, 1) = CDec(Math.Round(Math.Sin(dRadians), 4))
matX(2, 2) = CDec(Math.Round(Math.Cos(dRadians), 4))
'Load up the Y Matrix
dRadians = 0.0174532D * _rotation.Y
matY(0, 0) = CDec(Math.Round(Math.Cos(dRadians), 4))
matY(0, 1) = 0
matY(0, 2) = CDec(Math.Round(Math.Sin(dRadians), 4))
matY(1, 0) = 0
matY(1, 1) = 1
matY(1, 2) = 0
matY(2, 0) = CDec(Math.Round(Math.Sin(dRadians), 4) * -1)
matY(2, 1) = 0
matY(2, 2) = CDec(Math.Round(Math.Cos(dRadians), 4))
'Load up the Z Matrix
dRadians = 0.0174532D * _rotation.Z
matZ(0, 0) = CDec(Math.Round(Math.Cos(dRadians), 4))
matZ(0, 1) = CDec(Math.Round(Math.Sin(dRadians), 4) * -1)
matZ(0, 2) = 0
matZ(1, 0) = CDec(Math.Round(Math.Sin(dRadians), 4))
matZ(1, 1) = CDec(Math.Round(Math.Cos(dRadians), 4))
matZ(1, 2) = 0
matZ(2, 0) = 0
matZ(2, 1) = 0
matZ(2, 2) = 1
'multiply the two matrices
Dim resultMatrix1(2, 2) As Single
For i As Integer = 0 To 2
For j As Integer = 0 To 2
resultMatrix1(i, j) = matX(i, 0) * matY(0, j) +
matX(i, 1) * matY(1, j) +
matX(i, 2) * matY(2, j)
Next
Next
'Now mutiply ResultMatrix with X matrix
Dim resultMatrix2(2, 2) As Single
For i As Integer = 0 To 2
For j As Integer = 0 To 2
resultMatrix2(i, j) = matZ(i, 0) * resultMatrix1(0, j) +
matZ(i, 1) * resultMatrix1(1, j) +
matZ(i, 2) * resultMatrix1(2, j)
Next
Next
'Get determinate
dDeterminate = (resultMatrix2(0, 0) * resultMatrix2(1, 1) * resultMatrix2(2, 2)) +
(resultMatrix2(0, 1) * resultMatrix2(1, 2) * resultMatrix2(2, 0)) +
(resultMatrix2(0, 2) * resultMatrix2(2, 1) * resultMatrix2(1, 0)) -
(resultMatrix2(0, 2) * resultMatrix2(1, 1) * resultMatrix2(2, 0)) -
(resultMatrix2(0, 1) * resultMatrix2(1, 0) * resultMatrix2(2, 2)) -
(resultMatrix2(0, 0) * resultMatrix2(1, 2) * resultMatrix2(2, 1))
matAdjoin(0, 0) =
((resultMatrix2(1, 1) * resultMatrix2(2, 2)) - (resultMatrix2(1, 2) * resultMatrix2(2, 1))) * 1
matAdjoin(0, 1) =
((resultMatrix2(1, 0) * resultMatrix2(2, 2)) - (resultMatrix2(1, 2) * resultMatrix2(2, 0))) * -1
matAdjoin(0, 2) =
((resultMatrix2(1, 0) * resultMatrix2(2, 1)) - (resultMatrix2(1, 1) * resultMatrix2(2, 0))) * 1
matAdjoin(1, 0) =
((resultMatrix2(0, 1) * resultMatrix2(2, 2)) - (resultMatrix2(0, 2) * resultMatrix2(2, 1))) * -1
matAdjoin(1, 1) =
((resultMatrix2(0, 0) * resultMatrix2(2, 2)) - (resultMatrix2(0, 2) * resultMatrix2(2, 0))) * 1
matAdjoin(1, 2) =
((resultMatrix2(0, 0) * resultMatrix2(2, 1)) - (resultMatrix2(0, 1) * resultMatrix2(2, 0))) * -1
matAdjoin(2, 0) =
((resultMatrix2(0, 1) * resultMatrix2(1, 2)) - (resultMatrix2(0, 2) * resultMatrix2(1, 1))) * 1
matAdjoin(2, 1) =
((resultMatrix2(0, 0) * resultMatrix2(1, 2)) - (resultMatrix2(0, 2) * resultMatrix2(1, 0))) * -1
matAdjoin(2, 2) =
((resultMatrix2(0, 0) * resultMatrix2(1, 1)) - (resultMatrix2(0, 1) * resultMatrix2(1, 0))) * 1
matTranspose(0, 0) = matAdjoin(0, 0)
matTranspose(0, 1) = matAdjoin(1, 0)
matTranspose(0, 2) = matAdjoin(2, 0)
matTranspose(1, 0) = matAdjoin(0, 1)
matTranspose(1, 1) = matAdjoin(1, 1)
matTranspose(1, 2) = matAdjoin(2, 1)
matTranspose(2, 0) = matAdjoin(0, 2)
matTranspose(2, 1) = matAdjoin(1, 2)
matTranspose(2, 2) = matAdjoin(2, 2)
matInverse(0, 0) = matTranspose(0, 0) / dDeterminate
matInverse(0, 1) = matTranspose(0, 1) / dDeterminate
matInverse(0, 2) = matTranspose(0, 2) / dDeterminate
matInverse(1, 0) = matTranspose(1, 0) / dDeterminate
matInverse(1, 1) = matTranspose(1, 1) / dDeterminate
matInverse(1, 2) = matTranspose(1, 2) / dDeterminate
matInverse(2, 0) = matTranspose(2, 0) / dDeterminate
matInverse(2, 1) = matTranspose(2, 1) / dDeterminate
matInverse(2, 2) = matTranspose(2, 2) / dDeterminate
vChange.X =
(Math.Abs(_delta.X * matInverse(0, 0)) +
Math.Abs(_delta.Y * matInverse(0, 1)) +
Math.Abs(_delta.Z * matInverse(0, 2))) * negativeFactor
vChange.Y =
(Math.Abs(_delta.X * matInverse(1, 0)) +
Math.Abs(_delta.Y * matInverse(1, 1)) +
Math.Abs(_delta.Z * matInverse(1, 2))) * negativeFactor
vChange.Z =
(Math.Abs(_delta.X * matInverse(2, 0)) +
Math.Abs(_delta.Y * matInverse(2, 1)) +
Math.Abs(_delta.Z * matInverse(2, 2))) * negativeFactor
Return vChange
End Function
End Class
Public Class Vector
Public Property X() As Single
Public Property Y() As Single
Public Property Z() As Single
Public Sub New(ByVal _x As Single, ByVal _y As Single, ByVal _z As Single)
X = _x
Y = _y
Z = _z
End Sub
End Class
これまでのところ、これをリファクタリングすることができましたが、matAdjoin にこだわっています
Private Function InverseVector(ByVal _delta As Vector3, ByVal _rotation As Vector3) As Vector3
Dim vChange As New Vector3(0, 0, 0)
Dim matX As Matrix4x4 = Matrix4x4.Identity
Dim matY As Matrix4x4 = Matrix4x4.Identity
Dim matZ As Matrix4x4 = Matrix4x4.Identity
Dim negativeFactor As Int32 = 1
Dim determinate As Single
Dim matAdjoin As Matrix4x4
Dim matTranspose As Matrix4x4
Dim matInverse As Matrix4x4
If _delta.X < 0 Or _delta.Y < 0 Or _delta.Z < 0 Then
negativeFactor = -1
End If
'Load the X Matrix
Dim sRadians As Single = 0.0174532D * _rotation.X
matX = Matrix4x4.CreateRotationX(sRadians)
matX.M23 *= -1
matX.M32 *= -1
'Load up the Y Matrix
sRadians = 0.0174532D * _rotation.Y
matY = Matrix4x4.CreateRotationY(sRadians)
'Load up the Z Matrix
sRadians = 0.0174532D * _rotation.Z
matZ = Matrix4x4.CreateRotationZ(sRadians)
matZ.M12 *= -1
matZ.M21 *= -1
'multiply the two matrices
Dim resultMatrix1 As Matrix4x4 = Matrix4x4.Multiply(matX, matY)
'Now mutiply ResultMatrix with X matrix
Dim resultMatrix2 As Matrix4x4 = Matrix4x4.Multiply(matZ, resultMatrix1)
'Get determinate
determinate = resultMatrix2.GetDeterminant
'stuck on from here on
Return vChange
End Function