Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.6 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: math.c
  3. *
  4. * Misc. useful math utility functions.
  5. *
  6. * Copyright (c) 1994 Microsoft Corporation
  7. *
  8. \**************************************************************************/
  9. #include <windows.h>
  10. #include <GL\gl.h>
  11. #include <math.h>
  12. #include "mtk.h"
  13. #define ZERO_EPS 0.00000001
  14. POINT3D ss_ptZero = {0.0f, 0.0f, 0.0f};
  15. void ss_xformPoint(POINT3D *ptOut, POINT3D *ptIn, MATRIX *mat)
  16. {
  17. double x, y, z;
  18. x = (ptIn->x * mat->M[0][0]) + (ptIn->y * mat->M[0][1]) +
  19. (ptIn->z * mat->M[0][2]) + mat->M[0][3];
  20. y = (ptIn->x * mat->M[1][0]) + (ptIn->y * mat->M[1][1]) +
  21. (ptIn->z * mat->M[1][2]) + mat->M[1][3];
  22. z = (ptIn->x * mat->M[2][0]) + (ptIn->y * mat->M[2][1]) +
  23. (ptIn->z * mat->M[2][2]) + mat->M[2][3];
  24. ptOut->x = (float) x;
  25. ptOut->y = (float) y;
  26. ptOut->z = (float) z;
  27. }
  28. void ss_xformNorm(POINT3D *ptOut, POINT3D *ptIn, MATRIX *mat)
  29. {
  30. double x, y, z;
  31. double len;
  32. x = (ptIn->x * mat->M[0][0]) + (ptIn->y * mat->M[0][1]) +
  33. (ptIn->z * mat->M[0][2]);
  34. y = (ptIn->x * mat->M[1][0]) + (ptIn->y * mat->M[1][1]) +
  35. (ptIn->z * mat->M[1][2]);
  36. z = (ptIn->x * mat->M[2][0]) + (ptIn->y * mat->M[2][1]) +
  37. (ptIn->z * mat->M[2][2]);
  38. len = (x * x) + (y * y) + (z * z);
  39. if (len >= ZERO_EPS)
  40. len = 1.0 / sqrt(len);
  41. else
  42. len = 1.0;
  43. ptOut->x = (float) (x * len);
  44. ptOut->y = (float) (y * len);
  45. ptOut->z = (float) (z * len);
  46. return;
  47. }
  48. void ss_matrixIdent(MATRIX *mat)
  49. {
  50. mat->M[0][0] = 1.0f; mat->M[0][1] = 0.0f;
  51. mat->M[0][2] = 0.0f; mat->M[0][3] = 0.0f;
  52. mat->M[1][0] = 0.0f; mat->M[1][1] = 1.0f;
  53. mat->M[1][2] = 0.0f; mat->M[1][3] = 0.0f;
  54. mat->M[2][0] = 0.0f; mat->M[2][1] = 0.0f;
  55. mat->M[2][2] = 1.0f; mat->M[2][3] = 0.0f;
  56. mat->M[3][0] = 0.0f; mat->M[3][1] = 0.0f;
  57. mat->M[3][2] = 0.0f; mat->M[3][3] = 1.0f;
  58. }
  59. void ss_matrixRotate(MATRIX *m, double xTheta, double yTheta, double zTheta)
  60. {
  61. float xScale, yScale, zScale;
  62. float sinX, cosX;
  63. float sinY, cosY;
  64. float sinZ, cosZ;
  65. xScale = m->M[0][0];
  66. yScale = m->M[1][1];
  67. zScale = m->M[2][2];
  68. sinX = (float) sin(xTheta);
  69. cosX = (float) cos(xTheta);
  70. sinY = (float) sin(yTheta);
  71. cosY = (float) cos(yTheta);
  72. sinZ = (float) sin(zTheta);
  73. cosZ = (float) cos(zTheta);
  74. m->M[0][0] = (float) ((cosZ * cosY) * xScale);
  75. m->M[0][1] = (float) ((cosZ * -sinY * -sinX + sinZ * cosX) * yScale);
  76. m->M[0][2] = (float) ((cosZ * -sinY * cosX + sinZ * sinX) * zScale);
  77. m->M[1][0] = (float) (-sinZ * cosY * xScale);
  78. m->M[1][1] = (float) ((-sinZ * -sinY * -sinX + cosZ * cosX) * yScale);
  79. m->M[1][2] = (float) ((-sinZ * -sinY * cosX + cosZ * sinX) * zScale);
  80. m->M[2][0] = (float) (sinY * xScale);
  81. m->M[2][1] = (float) (cosY * -sinX * yScale);
  82. m->M[2][2] = (float) (cosY * cosX * zScale);
  83. }
  84. void ss_matrixTranslate(MATRIX *m, double xTrans, double yTrans,
  85. double zTrans)
  86. {
  87. m->M[0][3] = (float) xTrans;
  88. m->M[1][3] = (float) yTrans;
  89. m->M[2][3] = (float) zTrans;
  90. }
  91. void ss_matrixMult( MATRIX *m1, MATRIX *m2, MATRIX *m3 )
  92. {
  93. int i, j;
  94. for( j = 0; j < 4; j ++ ) {
  95. for( i = 0; i < 4; i ++ ) {
  96. m1->M[j][i] = m2->M[j][0] * m3->M[0][i] +
  97. m2->M[j][1] * m3->M[1][i] +
  98. m2->M[j][2] * m3->M[2][i] +
  99. m2->M[j][3] * m3->M[3][i];
  100. }
  101. }
  102. }
  103. void ss_calcNorm(POINT3D *norm, POINT3D *p1, POINT3D *p2, POINT3D *p3)
  104. {
  105. float crossX, crossY, crossZ;
  106. float abX, abY, abZ;
  107. float acX, acY, acZ;
  108. float sqrLength;
  109. float invLength;
  110. abX = p2->x - p1->x; // calculate p2 - p1
  111. abY = p2->y - p1->y;
  112. abZ = p2->z - p1->z;
  113. acX = p3->x - p1->x; // calculate p3 - p1
  114. acY = p3->y - p1->y;
  115. acZ = p3->z - p1->z;
  116. crossX = (abY * acZ) - (abZ * acY); // get cross product
  117. crossY = (abZ * acX) - (abX * acZ); // (p2 - p1) X (p3 - p1)
  118. crossZ = (abX * acY) - (abY * acX);
  119. sqrLength = (crossX * crossX) + (crossY * crossY) +
  120. (crossZ * crossZ);
  121. if (sqrLength > ZERO_EPS)
  122. invLength = (float) (1.0 / sqrt(sqrLength));
  123. else
  124. invLength = 1.0f;
  125. norm->x = crossX * invLength;
  126. norm->y = crossY * invLength;
  127. norm->z = crossZ * invLength;
  128. }
  129. void mtk_NormalizePoints(POINT3D *p, ULONG cPts)
  130. {
  131. float len;
  132. ULONG i;
  133. for (i = 0; i < cPts; i++, p++) {
  134. len = (p->x * p->x) + (p->y * p->y) + (p->z * p->z);
  135. if (len > ZERO_EPS)
  136. len = (float) (1.0 / sqrt(len));
  137. else
  138. len = 1.0f;
  139. p->x *= len;
  140. p->y *= len;
  141. p->z *= len;
  142. }
  143. }