matrix_ScriptBinding.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. ConsoleFunctionGroupBegin(MatrixMath, "Matrix manipulation functions.");
  23. /*! @defgroup MatrixMathFunctions Matrix Math
  24. @ingroup TorqueScriptFunctions
  25. @{
  26. */
  27. /*! Use the matrixCreate function to create a transform matrix from a three-element floating-point translation vector and a four-element floating-point rotation vector.
  28. @param posVec A three-element floating-point translation vector: \PosX PosY PosZ\.
  29. @param rotVec A four-element floating-point rotation vector: \RotX RotY RotZ\.
  30. @param These re rotations about the specified axes.
  31. @return Returns a transform matrix of the form \PosX PosY PosZ RotX RotY RotZ theta\.
  32. @sa MatrixCreateFromEuler
  33. */
  34. ConsoleFunctionWithDocs( MatrixCreate, ConsoleString, 3, 3, ( posVec , rotVec ))
  35. {
  36. Point3F pos;
  37. dSscanf(argv[1], "%g %g %g", &pos.x, &pos.y, &pos.z);
  38. AngAxisF aa(Point3F(0,0,0),0);
  39. dSscanf(argv[2], "%g %g %g %g", &aa.axis.x, &aa.axis.y, &aa.axis.z, &aa.angle);
  40. aa.angle = mDegToRad(aa.angle);
  41. char* returnBuffer = Con::getReturnBuffer(256);
  42. dSprintf(returnBuffer, 255, "%g %g %g %g %g %g %g",
  43. pos.x, pos.y, pos.z,
  44. aa.axis.x, aa.axis.y, aa.axis.z,
  45. aa.angle);
  46. return returnBuffer;
  47. }
  48. /*! Use the MatrixCreateFromEuler function to calculate a transform matrix from a three-element floating-point rotation vector.
  49. @param rotVec A three-element floating-point rotation vector: \RotX RotY RotZ\. These are rotations about the specified axes.
  50. @return Returns a transform matrix of the form \0 0 0 X Y Z theta\.
  51. @sa MatrixCreate
  52. */
  53. ConsoleFunctionWithDocs( MatrixCreateFromEuler, ConsoleString, 2, 2, ( rotVec ))
  54. {
  55. EulerF rot;
  56. dSscanf(argv[1], "%g %g %g", &rot.x, &rot.y, &rot.z);
  57. QuatF rotQ(rot);
  58. AngAxisF aa;
  59. aa.set(rotQ);
  60. char* ret = Con::getReturnBuffer(256);
  61. dSprintf(ret, 255, "0 0 0 %g %g %g %g",aa.axis.x,aa.axis.y,aa.axis.z,aa.angle);
  62. return ret;
  63. }
  64. /*! Use the MatrixMultiply function to multiply two seven-element transform matrices to produce a new seven element matrix.
  65. @param transformA A seven-element transform matrix of the form \PosX PosY PosZ RotX RotY RotZ theta\.
  66. @param transformB A seven-element transform matrix of the form \PosX PosY PosZ RotX RotY RotZ theta\.
  67. @return Returns a seven-element matrix resulting from transiformA x transformB.
  68. @sa MatrixMulPoint, MatrixMulVector
  69. */
  70. ConsoleFunctionWithDocs( MatrixMultiply, ConsoleString, 3, 3, ( transformA , transformB ))
  71. {
  72. Point3F pos1;
  73. AngAxisF aa1(Point3F(0,0,0),0);
  74. dSscanf(argv[1], "%g %g %g %g %g %g %g", &pos1.x, &pos1.y, &pos1.z, &aa1.axis.x, &aa1.axis.y, &aa1.axis.z, &aa1.angle);
  75. MatrixF temp1(true);
  76. aa1.setMatrix(&temp1);
  77. temp1.setColumn(3, pos1);
  78. Point3F pos2;
  79. AngAxisF aa2(Point3F(0,0,0),0);
  80. dSscanf(argv[2], "%g %g %g %g %g %g %g", &pos2.x, &pos2.y, &pos2.z, &aa2.axis.x, &aa2.axis.y, &aa2.axis.z, &aa2.angle);
  81. MatrixF temp2(true);
  82. aa2.setMatrix(&temp2);
  83. temp2.setColumn(3, pos2);
  84. temp1.mul(temp2);
  85. Point3F pos;
  86. AngAxisF aa(temp1);
  87. aa.axis.normalize();
  88. temp1.getColumn(3, &pos);
  89. char* ret = Con::getReturnBuffer(256);
  90. dSprintf(ret, 255, "%g %g %g %g %g %g %g",
  91. pos.x, pos.y, pos.z,
  92. aa.axis.x, aa.axis.y, aa.axis.z,
  93. aa.angle);
  94. return ret;
  95. }
  96. /*! Use the MatrixMulVector function to multiply a seven-element transform matrix with a three-element matrix.
  97. @param transform A seven-element transform matrix of the form \PosX PosY PosZ RotX RotY RotZ theta\.
  98. @param vector A three-element vector.
  99. @return Returns three-element resulting from vector * transform.
  100. @sa MatrixMulPoint, MatrixMultiply
  101. */
  102. ConsoleFunctionWithDocs( MatrixMulVector, ConsoleString, 3, 3, ( transform , vector ))
  103. {
  104. Point3F pos1;
  105. AngAxisF aa1(Point3F(0,0,0),0);
  106. dSscanf(argv[1], "%g %g %g %g %g %g %g", &pos1.x, &pos1.y, &pos1.z, &aa1.axis.x, &aa1.axis.y, &aa1.axis.z, &aa1.angle);
  107. MatrixF temp1(true);
  108. aa1.setMatrix(&temp1);
  109. temp1.setColumn(3, pos1);
  110. Point3F vec1;
  111. dSscanf(argv[2], "%g %g %g", &vec1.x, &vec1.y, &vec1.z);
  112. Point3F result;
  113. temp1.mulV(vec1, &result);
  114. char* ret = Con::getReturnBuffer(256);
  115. dSprintf(ret, 255, "%g %g %g", result.x, result.y, result.z);
  116. return ret;
  117. }
  118. /*! Use the MatrixMulPoint function to multiply a seven element transform matrix by a three element point vector, producing a three element position vector.
  119. @param transform A seven-element transform matrix.
  120. @param point A three-element point/position vector.
  121. @return Returns a three-element position vector.
  122. @sa MatrixMultiply, MatrixMulVector
  123. */
  124. ConsoleFunctionWithDocs( MatrixMulPoint, ConsoleString, 3, 3, ( transform , point ))
  125. {
  126. Point3F pos1;
  127. AngAxisF aa1(Point3F(0,0,0),0);
  128. dSscanf(argv[1], "%g %g %g %g %g %g %g", &pos1.x, &pos1.y, &pos1.z, &aa1.axis.x, &aa1.axis.y, &aa1.axis.z, &aa1.angle);
  129. MatrixF temp1(true);
  130. aa1.setMatrix(&temp1);
  131. temp1.setColumn(3, pos1);
  132. Point3F vec1;
  133. dSscanf(argv[2], "%g %g %g", &vec1.x, &vec1.y, &vec1.z);
  134. Point3F result;
  135. temp1.mulP(vec1, &result);
  136. char* ret = Con::getReturnBuffer(256);
  137. dSprintf(ret, 255, "%g %g %g", result.x, result.y, result.z);
  138. return ret;
  139. }
  140. ConsoleFunctionGroupEnd(MatrixMath);
  141. /*! @} */ // group MatrixMathFunctions