Xcode simd - issue with Translation and Rotation Matrix Example











up vote
0
down vote

favorite
1












Not only is using column-major vs row-major counter-intuitive, Apple's documentation on "Working with Matrices" further exacerbates the confusion by their examples of "constructing" a "Translate Matrix" and a "Rotation Matrix" in 2D.



Translate Matrix Per Apple's Documentation ()




Translate A translate matrix takes the following form:



1  0  0
0 1 0
tx ty 1


The simd library provides constants for identity matrices (matrices
with ones along the diagonal, and zeros elsewhere). The 3 x 3 Float
identity matrix is matrix_identity_float3x3.



The following function returns a simd_float3x3 matrix using the
specified tx and ty translate values by setting the elements in an
identity matrix:



func makeTranslationMatrix(tx: Float, ty: Float) -> simd_float3x3 {
var matrix = matrix_identity_float3x3

matrix[0, 2] = tx
matrix[1, 2] = ty

return matrix
}



My Issue with it



The line of code matrix[0, 2] = tx sets the value of the first column and the third row to tx. let translationMatrix = makeTranslationMatrix(tx: 1, ty: 3) and printing out the 2nd column print(translationMatrix.columns.2) will yield float3(0.0, 0.0, 1.0). I am very confused regarding why it is the last row that contains the translation values, rather than the column. This convention is not used when using SCNMatrix4MakeTranslation and creating a simd_float4x4 out of the SCNMatrix4 object.



var A = SCNMatrix4MakeTranslation(1,2,3)
var Asimd = simd_float4x4(A)

A.m41 // 1
A.m42 // 2
A.m43 // 3
A.m44 // 1

Asimd.columns.3 // float4(1.0, 2.0, 3.0, 1.0)


Both SCNMatrix4 and simd_float4x4 follow the column major naming convention. In the 2D example from Apple, it is the last row that contains the translation values, whereas with SCNMatrix4 and converting to simd_float4x4, it is the last column that contains the translation values. Apple's example seems to be doing the same with the Rotation Matrices as well.



What am I missing?










share|improve this question


























    up vote
    0
    down vote

    favorite
    1












    Not only is using column-major vs row-major counter-intuitive, Apple's documentation on "Working with Matrices" further exacerbates the confusion by their examples of "constructing" a "Translate Matrix" and a "Rotation Matrix" in 2D.



    Translate Matrix Per Apple's Documentation ()




    Translate A translate matrix takes the following form:



    1  0  0
    0 1 0
    tx ty 1


    The simd library provides constants for identity matrices (matrices
    with ones along the diagonal, and zeros elsewhere). The 3 x 3 Float
    identity matrix is matrix_identity_float3x3.



    The following function returns a simd_float3x3 matrix using the
    specified tx and ty translate values by setting the elements in an
    identity matrix:



    func makeTranslationMatrix(tx: Float, ty: Float) -> simd_float3x3 {
    var matrix = matrix_identity_float3x3

    matrix[0, 2] = tx
    matrix[1, 2] = ty

    return matrix
    }



    My Issue with it



    The line of code matrix[0, 2] = tx sets the value of the first column and the third row to tx. let translationMatrix = makeTranslationMatrix(tx: 1, ty: 3) and printing out the 2nd column print(translationMatrix.columns.2) will yield float3(0.0, 0.0, 1.0). I am very confused regarding why it is the last row that contains the translation values, rather than the column. This convention is not used when using SCNMatrix4MakeTranslation and creating a simd_float4x4 out of the SCNMatrix4 object.



    var A = SCNMatrix4MakeTranslation(1,2,3)
    var Asimd = simd_float4x4(A)

    A.m41 // 1
    A.m42 // 2
    A.m43 // 3
    A.m44 // 1

    Asimd.columns.3 // float4(1.0, 2.0, 3.0, 1.0)


    Both SCNMatrix4 and simd_float4x4 follow the column major naming convention. In the 2D example from Apple, it is the last row that contains the translation values, whereas with SCNMatrix4 and converting to simd_float4x4, it is the last column that contains the translation values. Apple's example seems to be doing the same with the Rotation Matrices as well.



    What am I missing?










    share|improve this question
























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      Not only is using column-major vs row-major counter-intuitive, Apple's documentation on "Working with Matrices" further exacerbates the confusion by their examples of "constructing" a "Translate Matrix" and a "Rotation Matrix" in 2D.



      Translate Matrix Per Apple's Documentation ()




      Translate A translate matrix takes the following form:



      1  0  0
      0 1 0
      tx ty 1


      The simd library provides constants for identity matrices (matrices
      with ones along the diagonal, and zeros elsewhere). The 3 x 3 Float
      identity matrix is matrix_identity_float3x3.



      The following function returns a simd_float3x3 matrix using the
      specified tx and ty translate values by setting the elements in an
      identity matrix:



      func makeTranslationMatrix(tx: Float, ty: Float) -> simd_float3x3 {
      var matrix = matrix_identity_float3x3

      matrix[0, 2] = tx
      matrix[1, 2] = ty

      return matrix
      }



      My Issue with it



      The line of code matrix[0, 2] = tx sets the value of the first column and the third row to tx. let translationMatrix = makeTranslationMatrix(tx: 1, ty: 3) and printing out the 2nd column print(translationMatrix.columns.2) will yield float3(0.0, 0.0, 1.0). I am very confused regarding why it is the last row that contains the translation values, rather than the column. This convention is not used when using SCNMatrix4MakeTranslation and creating a simd_float4x4 out of the SCNMatrix4 object.



      var A = SCNMatrix4MakeTranslation(1,2,3)
      var Asimd = simd_float4x4(A)

      A.m41 // 1
      A.m42 // 2
      A.m43 // 3
      A.m44 // 1

      Asimd.columns.3 // float4(1.0, 2.0, 3.0, 1.0)


      Both SCNMatrix4 and simd_float4x4 follow the column major naming convention. In the 2D example from Apple, it is the last row that contains the translation values, whereas with SCNMatrix4 and converting to simd_float4x4, it is the last column that contains the translation values. Apple's example seems to be doing the same with the Rotation Matrices as well.



      What am I missing?










      share|improve this question













      Not only is using column-major vs row-major counter-intuitive, Apple's documentation on "Working with Matrices" further exacerbates the confusion by their examples of "constructing" a "Translate Matrix" and a "Rotation Matrix" in 2D.



      Translate Matrix Per Apple's Documentation ()




      Translate A translate matrix takes the following form:



      1  0  0
      0 1 0
      tx ty 1


      The simd library provides constants for identity matrices (matrices
      with ones along the diagonal, and zeros elsewhere). The 3 x 3 Float
      identity matrix is matrix_identity_float3x3.



      The following function returns a simd_float3x3 matrix using the
      specified tx and ty translate values by setting the elements in an
      identity matrix:



      func makeTranslationMatrix(tx: Float, ty: Float) -> simd_float3x3 {
      var matrix = matrix_identity_float3x3

      matrix[0, 2] = tx
      matrix[1, 2] = ty

      return matrix
      }



      My Issue with it



      The line of code matrix[0, 2] = tx sets the value of the first column and the third row to tx. let translationMatrix = makeTranslationMatrix(tx: 1, ty: 3) and printing out the 2nd column print(translationMatrix.columns.2) will yield float3(0.0, 0.0, 1.0). I am very confused regarding why it is the last row that contains the translation values, rather than the column. This convention is not used when using SCNMatrix4MakeTranslation and creating a simd_float4x4 out of the SCNMatrix4 object.



      var A = SCNMatrix4MakeTranslation(1,2,3)
      var Asimd = simd_float4x4(A)

      A.m41 // 1
      A.m42 // 2
      A.m43 // 3
      A.m44 // 1

      Asimd.columns.3 // float4(1.0, 2.0, 3.0, 1.0)


      Both SCNMatrix4 and simd_float4x4 follow the column major naming convention. In the 2D example from Apple, it is the last row that contains the translation values, whereas with SCNMatrix4 and converting to simd_float4x4, it is the last column that contains the translation values. Apple's example seems to be doing the same with the Rotation Matrices as well.



      What am I missing?







      matrix scenekit arkit simd






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 17:20









      oneiros

      1,84083253




      1,84083253
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          This can be confusing, yes.



          The documentation you mentions makes the following computation:




          let translatedVector = positionVector * translationMatrix




          Note that the matrix is on the right side of the multiplication.
          You are probably used to the notation b = M * a but if you take the transpose you get b' = a' * M' which is what the sample does.



          In SIMD there's no way to differentiate a vector from its transpose (b from b') and the library allows you to make the multiplication in both ways:



          static simd_float3 SIMD_CFUNC simd_mul(simd_float3x3 __x, simd_float3 __y);
          static simd_float3 SIMD_CFUNC simd_mul(simd_float3 __x, simd_float3x3 __y) { return simd_mul(simd_transpose(__y), __x); }





          share|improve this answer





















          • That explains a lot - however, I am not sure I understand the statement "In SIMD there's no way to differentiate a vector from its transpose (b from b')" - what exactly do you mean? I expect SIMD will multiply any matrices you specify, as long as the dimensions agree. Are you telling me that if initially the dimensions don't agree like a 4x4 times 1x4 matrix it will try to automatically transpose the second and turn it into a 4x4 times 4x1 = 4x1?
            – oneiros
            Nov 23 at 15:53








          • 1




            In SIMD there's no difference between mat4x1 and mat1x4, they are both vec4. As a result the same vec4 variable can be used on both sides of the multiplication, so you have to be extra cautious about the convention you use.
            – mnuages
            Nov 23 at 16:18











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435756%2fxcode-simd-issue-with-translation-and-rotation-matrix-example%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          This can be confusing, yes.



          The documentation you mentions makes the following computation:




          let translatedVector = positionVector * translationMatrix




          Note that the matrix is on the right side of the multiplication.
          You are probably used to the notation b = M * a but if you take the transpose you get b' = a' * M' which is what the sample does.



          In SIMD there's no way to differentiate a vector from its transpose (b from b') and the library allows you to make the multiplication in both ways:



          static simd_float3 SIMD_CFUNC simd_mul(simd_float3x3 __x, simd_float3 __y);
          static simd_float3 SIMD_CFUNC simd_mul(simd_float3 __x, simd_float3x3 __y) { return simd_mul(simd_transpose(__y), __x); }





          share|improve this answer





















          • That explains a lot - however, I am not sure I understand the statement "In SIMD there's no way to differentiate a vector from its transpose (b from b')" - what exactly do you mean? I expect SIMD will multiply any matrices you specify, as long as the dimensions agree. Are you telling me that if initially the dimensions don't agree like a 4x4 times 1x4 matrix it will try to automatically transpose the second and turn it into a 4x4 times 4x1 = 4x1?
            – oneiros
            Nov 23 at 15:53








          • 1




            In SIMD there's no difference between mat4x1 and mat1x4, they are both vec4. As a result the same vec4 variable can be used on both sides of the multiplication, so you have to be extra cautious about the convention you use.
            – mnuages
            Nov 23 at 16:18















          up vote
          1
          down vote



          accepted










          This can be confusing, yes.



          The documentation you mentions makes the following computation:




          let translatedVector = positionVector * translationMatrix




          Note that the matrix is on the right side of the multiplication.
          You are probably used to the notation b = M * a but if you take the transpose you get b' = a' * M' which is what the sample does.



          In SIMD there's no way to differentiate a vector from its transpose (b from b') and the library allows you to make the multiplication in both ways:



          static simd_float3 SIMD_CFUNC simd_mul(simd_float3x3 __x, simd_float3 __y);
          static simd_float3 SIMD_CFUNC simd_mul(simd_float3 __x, simd_float3x3 __y) { return simd_mul(simd_transpose(__y), __x); }





          share|improve this answer





















          • That explains a lot - however, I am not sure I understand the statement "In SIMD there's no way to differentiate a vector from its transpose (b from b')" - what exactly do you mean? I expect SIMD will multiply any matrices you specify, as long as the dimensions agree. Are you telling me that if initially the dimensions don't agree like a 4x4 times 1x4 matrix it will try to automatically transpose the second and turn it into a 4x4 times 4x1 = 4x1?
            – oneiros
            Nov 23 at 15:53








          • 1




            In SIMD there's no difference between mat4x1 and mat1x4, they are both vec4. As a result the same vec4 variable can be used on both sides of the multiplication, so you have to be extra cautious about the convention you use.
            – mnuages
            Nov 23 at 16:18













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          This can be confusing, yes.



          The documentation you mentions makes the following computation:




          let translatedVector = positionVector * translationMatrix




          Note that the matrix is on the right side of the multiplication.
          You are probably used to the notation b = M * a but if you take the transpose you get b' = a' * M' which is what the sample does.



          In SIMD there's no way to differentiate a vector from its transpose (b from b') and the library allows you to make the multiplication in both ways:



          static simd_float3 SIMD_CFUNC simd_mul(simd_float3x3 __x, simd_float3 __y);
          static simd_float3 SIMD_CFUNC simd_mul(simd_float3 __x, simd_float3x3 __y) { return simd_mul(simd_transpose(__y), __x); }





          share|improve this answer












          This can be confusing, yes.



          The documentation you mentions makes the following computation:




          let translatedVector = positionVector * translationMatrix




          Note that the matrix is on the right side of the multiplication.
          You are probably used to the notation b = M * a but if you take the transpose you get b' = a' * M' which is what the sample does.



          In SIMD there's no way to differentiate a vector from its transpose (b from b') and the library allows you to make the multiplication in both ways:



          static simd_float3 SIMD_CFUNC simd_mul(simd_float3x3 __x, simd_float3 __y);
          static simd_float3 SIMD_CFUNC simd_mul(simd_float3 __x, simd_float3x3 __y) { return simd_mul(simd_transpose(__y), __x); }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 18:17









          mnuages

          9,26621329




          9,26621329












          • That explains a lot - however, I am not sure I understand the statement "In SIMD there's no way to differentiate a vector from its transpose (b from b')" - what exactly do you mean? I expect SIMD will multiply any matrices you specify, as long as the dimensions agree. Are you telling me that if initially the dimensions don't agree like a 4x4 times 1x4 matrix it will try to automatically transpose the second and turn it into a 4x4 times 4x1 = 4x1?
            – oneiros
            Nov 23 at 15:53








          • 1




            In SIMD there's no difference between mat4x1 and mat1x4, they are both vec4. As a result the same vec4 variable can be used on both sides of the multiplication, so you have to be extra cautious about the convention you use.
            – mnuages
            Nov 23 at 16:18


















          • That explains a lot - however, I am not sure I understand the statement "In SIMD there's no way to differentiate a vector from its transpose (b from b')" - what exactly do you mean? I expect SIMD will multiply any matrices you specify, as long as the dimensions agree. Are you telling me that if initially the dimensions don't agree like a 4x4 times 1x4 matrix it will try to automatically transpose the second and turn it into a 4x4 times 4x1 = 4x1?
            – oneiros
            Nov 23 at 15:53








          • 1




            In SIMD there's no difference between mat4x1 and mat1x4, they are both vec4. As a result the same vec4 variable can be used on both sides of the multiplication, so you have to be extra cautious about the convention you use.
            – mnuages
            Nov 23 at 16:18
















          That explains a lot - however, I am not sure I understand the statement "In SIMD there's no way to differentiate a vector from its transpose (b from b')" - what exactly do you mean? I expect SIMD will multiply any matrices you specify, as long as the dimensions agree. Are you telling me that if initially the dimensions don't agree like a 4x4 times 1x4 matrix it will try to automatically transpose the second and turn it into a 4x4 times 4x1 = 4x1?
          – oneiros
          Nov 23 at 15:53






          That explains a lot - however, I am not sure I understand the statement "In SIMD there's no way to differentiate a vector from its transpose (b from b')" - what exactly do you mean? I expect SIMD will multiply any matrices you specify, as long as the dimensions agree. Are you telling me that if initially the dimensions don't agree like a 4x4 times 1x4 matrix it will try to automatically transpose the second and turn it into a 4x4 times 4x1 = 4x1?
          – oneiros
          Nov 23 at 15:53






          1




          1




          In SIMD there's no difference between mat4x1 and mat1x4, they are both vec4. As a result the same vec4 variable can be used on both sides of the multiplication, so you have to be extra cautious about the convention you use.
          – mnuages
          Nov 23 at 16:18




          In SIMD there's no difference between mat4x1 and mat1x4, they are both vec4. As a result the same vec4 variable can be used on both sides of the multiplication, so you have to be extra cautious about the convention you use.
          – mnuages
          Nov 23 at 16:18


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435756%2fxcode-simd-issue-with-translation-and-rotation-matrix-example%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          Alexandru Averescu

          Trompette piccolo