cvec.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef CVEC_H
  19. #define CVEC_H
  20. typedef float Vector[3];
  21. #define FABS(f) (float(fabs(f)))
  22. //---------------------------------------------------------------------------
  23. inline void Add (const Vector u, const Vector v, Vector sum)
  24. {
  25. sum[0] = u[0] + v[0];
  26. sum[1] = u[1] + v[1];
  27. sum[2] = u[2] + v[2];
  28. }
  29. //---------------------------------------------------------------------------
  30. inline void Sub (const Vector u, const Vector v, Vector diff)
  31. {
  32. diff[0] = u[0] - v[0];
  33. diff[1] = u[1] - v[1];
  34. diff[2] = u[2] - v[2];
  35. }
  36. //---------------------------------------------------------------------------
  37. inline float Dot (const Vector u, const Vector v)
  38. {
  39. return u[0]*v[0] + u[1]*v[1] + u[2]*v[2];
  40. }
  41. //---------------------------------------------------------------------------
  42. inline void ScalarMult (const float scalar, const Vector u, Vector product)
  43. {
  44. product[0] = scalar*u[0];
  45. product[1] = scalar*u[1];
  46. product[2] = scalar*u[2];
  47. }
  48. //---------------------------------------------------------------------------
  49. inline int Invert3x3 (const float a[3][3], float ainv[3][3])
  50. {
  51. // Invert a 3x3 using cofactors. This is about 8 times faster than
  52. // the Numerical Recipes code which uses Gaussian elimination.
  53. ainv[0][0] = a[1][1]*a[2][2]-a[1][2]*a[2][1];
  54. ainv[0][1] = a[0][2]*a[2][1]-a[0][1]*a[2][2];
  55. ainv[0][2] = a[0][1]*a[1][2]-a[0][2]*a[1][1];
  56. ainv[1][0] = a[1][2]*a[2][0]-a[1][0]*a[2][2];
  57. ainv[1][1] = a[0][0]*a[2][2]-a[0][2]*a[2][0];
  58. ainv[1][2] = a[0][2]*a[1][0]-a[0][0]*a[1][2];
  59. ainv[2][0] = a[1][0]*a[2][1]-a[1][1]*a[2][0];
  60. ainv[2][1] = a[0][1]*a[2][0]-a[0][0]*a[2][1];
  61. ainv[2][2] = a[0][0]*a[1][1]-a[0][1]*a[1][0];
  62. float det = a[0][0]*ainv[0][0]+a[0][1]*ainv[1][0]+a[0][2]*ainv[2][0];
  63. if (FABS(det) <= 1e-06f )
  64. return 0;
  65. float invdet = 1.0f/det;
  66. for (int row = 0; row < 3; row++)
  67. for (int col = 0; col < 3; col++)
  68. ainv[row][col] *= invdet;
  69. return 1;
  70. }
  71. //---------------------------------------------------------------------------
  72. inline void MultiplyVM (Vector input, float m[3][3], Vector output)
  73. {
  74. output[0] = input[0]*m[0][0] + input[1]*m[1][0] + input[2]*m[2][0];
  75. output[1] = input[0]*m[0][1] + input[1]*m[1][1] + input[2]*m[2][1];
  76. output[2] = input[0]*m[0][2] + input[1]*m[1][2] + input[2]*m[2][2];
  77. }
  78. //---------------------------------------------------------------------------
  79. inline void MultiplyMM (const float A[3][3], const float B[3][3], float AB[3][3])
  80. {
  81. for (int row = 0; row < 3; row++)
  82. {
  83. for (int col = 0; col < 3; col++)
  84. {
  85. AB[row][col] = 0.0f;
  86. for (int mid = 0; mid < 3; mid++)
  87. AB[row][col] += A[row][mid]*B[mid][col];
  88. }
  89. }
  90. }
  91. #endif