2016-06-15 22 views
-2

ベクトルを回転させ、結果を常に正のx、y、z平面に保つ次のコードがあります。 System.Numerics型Vector3とMatrix4x4を使用するようにコードを再因子付けしたいと考えています。 誰かが翻訳を手伝ってもらえますか?System.Numericsによるベクトル演算

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 
+2

この質問は特定のプログラミング上の問題ではなく、私たちが完了するための課題を提示しているため、話題にはなりません。作業コードのリファクタリングに関するヘルプを入手したい場合は、http://codereview.stackexchange.com/を参照してください。しかし、皆さんもあなたの仕事のすべてをやってくれるように頼まれないかもしれないことに注意してください。 –

+0

コード内でmatAdjoinを実行するSystem.Numericsメソッドは何ですか? – muscleman71

+0

コードは、-x、-y、および-z回転行列を構成しているようです。次いで、補因子および補因子を計算する。 System.Numerics.Matrix4x4のソースコードを見ると、Matrix4x4.Invertが正しい呼び出しであるように見えます。 – muscleman71

答えて

0

を立ち往生しています。メソッドの最後の4行でx、y、zを行で掛け合わせることについて私は満足していませんが。

Private Function InverseVector(ByVal _delta As Vector3, ByVal _rotation As Vector3) As Vector3 
    Dim vChange As New Vector3(0, 0, 0) 
    Dim negativeFactor As Int32 = 1 
    Dim matAdjugate As Matrix4x4 
    If _delta.X < 0 Or _delta.Y < 0 Or _delta.Z < 0 Then 
     negativeFactor = -1 
    End If 
    Dim angle As Vector3 = _rotation * 0.0174532D 
    'Load the -X Matrix 
    Dim negativeRotationX As Matrix4x4 = Matrix4x4.CreateRotationX(angle.X) 
    negativeRotationX.M23 *= -1 
    negativeRotationX.M32 *= -1 
    'Load up the -Y Matrix 
    Dim negativeRotationY As Matrix4x4 = Matrix4x4.CreateRotationY(angle.Y) 
    negativeRotationY.M21 *= -1 
    'Load up the -Z Matrix 
    Dim negativeRotationZ As Matrix4x4 = Matrix4x4.CreateRotationZ(angle.Z) 
    negativeRotationZ.M12 *= -1 
    negativeRotationZ.M21 *= -1 
    'multiply the x,y matrices 
    Dim resultMatrix1 As Matrix4x4 = Matrix4x4.Multiply(negativeRotationX, negativeRotationY) 
    'Now mutiply z with x,y matrix 
    Dim resultMatrix2 As Matrix4x4 = Matrix4x4.Multiply(negativeRotationZ, resultMatrix1) 
    Dim can As Boolean = Matrix4x4.Invert(resultMatrix2, matAdjugate) 
    vChange.X = (Math.Abs(_delta.X * matAdjugate.M11) + Math.Abs(_delta.Y * matAdjugate.M12) + Math.Abs(_delta.Z * matAdjugate.M13)) * negativeFactor 
    vChange.Y = (Math.Abs(_delta.X * matAdjugate.M21) + Math.Abs(_delta.Y * matAdjugate.M22) + Math.Abs(_delta.Z * matAdjugate.M23)) * negativeFactor 
    vChange.Z = (Math.Abs(_delta.X * matAdjugate.M31) + Math.Abs(_delta.Y * matAdjugate.M32) + Math.Abs(_delta.Z * matAdjugate.M33)) * negativeFactor 
    Return vChange 
End Function 
0

これも機能します。

Private Function InverseVector(ByVal _delta As Vector3, ByVal _rotation As Vector3) As Vector3 
    Dim vChange As New Vector3(0, 0, 0) 
    Dim negativeFactor As Int32 = 1 
    Dim matAdjugate As Matrix4x4 
    If _delta.X < 0 Or _delta.Y < 0 Or _delta.Z < 0 Then 
     negativeFactor = -1 
    End If 
    Dim angle As Vector3 = _rotation * 0.0174532D 
    Dim negativeRotationX As Matrix4x4 = Matrix4x4.Negate(Matrix4x4.CreateRotationX(angle.X)) 
    Dim negativeRotationY As Matrix4x4 = Matrix4x4.Negate(Matrix4x4.CreateRotationY(angle.Y)) 
    Dim negativeRotationZ As Matrix4x4 = Matrix4x4.Negate(Matrix4x4.CreateRotationZ(angle.Z)) 
    'multiply the x,y matrices 
    Dim resultMatrix1 As Matrix4x4 = Matrix4x4.Multiply(negativeRotationX, negativeRotationY) 
    'Now mutiply z with x,y matrix 
    Dim resultMatrix2 As Matrix4x4 = Matrix4x4.Multiply(negativeRotationZ, resultMatrix1) 
    Dim can As Boolean = Matrix4x4.Invert(resultMatrix2, matAdjugate) 
    vChange.X = (Math.Abs(_delta.X * matAdjugate.M11) + Math.Abs(_delta.Y * matAdjugate.M12) + Math.Abs(_delta.Z * matAdjugate.M13)) * negativeFactor 
    vChange.Y = (Math.Abs(_delta.X * matAdjugate.M21) + Math.Abs(_delta.Y * matAdjugate.M22) + Math.Abs(_delta.Z * matAdjugate.M23)) * negativeFactor 
    vChange.Z = (Math.Abs(_delta.X * matAdjugate.M31) + Math.Abs(_delta.Y * matAdjugate.M32) + Math.Abs(_delta.Z * matAdjugate.M33)) * negativeFactor 
    Return vChange 
End Function**strong text** 
関連する問題